Romain Thomas - Static instrumentation based on executable file formats

Similar documents
LIEF: Library to Instrument Executable Formats

Advances in Linux process forensics with ECFS

Runtime Process Insemination

A novel runtime technique for identifying malicious applications

July 14, EPITA Systems/Security Laboratory (LSE) Code sandboxing. Alpha Abdoulaye - Pierre Marsais. Introduction. Solutions.

First order of Business

Reverse Engineering Malware Dynamic Analysis of Binary Malware II

secubt Hacking the Hackers with User Space Virtualization

Fixing/Making Holes in Binaries

Hackveda Training - Ethical Hacking, Networking & Security

syscall_intercept A user space library for intercepting system calls Author Name, Company Krzysztof Czuryło, Intel

Protecting Against Unexpected System Calls

Post exploitation techniques on OSX and Iphone. Vincenzo Iozzo

VirtualSwindle: An Automated Attack Against In-App Billing on Android

On The Effectiveness of Address-Space Randomization. H. Shacham, M. Page, B. Pfaff, E.-J. Goh, N. Modadugu, and D. Boneh Stanford University CCS 2004

Link 8.A Dynamic Linking

Incremental Linking with Gold

Tackling runtime-based obfuscation in Android with TIRO

Reverse Engineering Malware Binary Obfuscation and Protection

Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC.

PRESS ROOT TO CONTINUE: DETECTING OSX AND WINDOWS BOOTKITS WITH RDFU

A tale of ELFs and DWARFs

Lecture 4 Processes. Dynamic Analysis. GDB

String Oriented Programming Exploring Format String Attacks. Mathias Payer

Android Dynamic Linker - Marshmallow

Virtuoso: Narrowing the Semantic Gap in Virtual Machine Introspection

Q: Exploit Hardening Made Easy

Android Application Sandbox. Thomas Bläsing DAI-Labor TU Berlin

Lecture 08 Control-flow Hijacking Defenses

Security Workshop HTS. LSE Team. February 3rd, 2016 EPITA / 40

Metasm. a ruby (dis)assembler. Yoann Guillot. 20 october 2007

LIVEPATCH MODULE CREATION. LPC 2016 Josh Poimboeuf

Writing Exploits. Nethemba s.r.o.

Università Ca Foscari Venezia

Implementing your own generic unpacker

Pangu 9 Internals. Tielei Wang and Hao Xu

Reverse Engineering Swift Apps. Michael Gianarakis Rootcon X 2016

CMPSC 497 Buffer Overflow Vulnerabilities

PROTOTYPING AND REVERSE ENGINEERING WITH FRIDA BSIDES LONDON 2017 JAHMEL HARRIS

Link 8. Dynamic Linking

Rootkits n Stuff

Let's cyber: hacking, 0days and vulnerability research. PATROKLOS ARGYROUDIS CENSUS S.A.

Link 7. Dynamic Linking

CYSE 411/AIT681 Secure Software Engineering Topic #8. Secure Coding: Pointer Subterfuge

Windows HIPS evaluation with Slipfest

JUN-OH LEE JU-SEON LEE JEONG-MIN LEE

Secure Coding Topics. Readings. CYSE 411/AIT681 Secure Software Engineering. Pointer Subterfuge. Outline. Data Locations (cont d) Data Locations

Secure Coding Topics. CYSE 411/AIT681 Secure Software Engineering. Readings. Outline. This lecture: Topic #8. Secure Coding: Pointer Subterfuge

A tool for the. Symbolic Execution. of Linux binaries

Hack in the Box Trends and Tools. H D Moore

Evinrude. How to build a Petri Net-Based IDS from Program Sources. MeFoSyLoMa Meeting

PROGRAMMING IN C++ CVIČENÍ

Advanced Mac OS X Rootkits. Dino Dai Zovi Chief Scientist Endgame Systems

Buffer Overflows. A brief Introduction to the detection and prevention of buffer overflows for intermediate programmers.

executable-only-memory-switch (XOM-Switch)

ECE 598 Advanced Operating Systems Lecture 10

Linkers and Loaders. CS 167 VI 1 Copyright 2008 Thomas W. Doeppner. All rights reserved.

ARMv8.3 Pointer Authentication

Unpacking the Packed Unpacker

Lab 1: First Steps in C++ - Eclipse

Building Advanced Coverage-guided Fuzzer for Program Binaries

EDURange Student s Manual. September 14, 2015

most elaborate jailbreak to ever hack your phone forbes Used 800,000 times in 6 hours after its release

Reverse Engineering III: PE Format

64-bit Imports Rebuilding and Unpacking

The Attacker s POV Hacking Mobile Apps. in Your Enterprise to Reveal Real Vulns and Protect the Business. Tony Ramirez

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

.kkapture: Guided tour

Practical and Efficient Exploit Mitigation for Embedded Devices

Lecture 9. PSiOS: Bring Your Own Privacy & Security to ios Devices. Tim Werthmann, Ralf Hund, Lucas Davi, Ahmad-Reza Sadeghi and Thorsten Holz

Another introduction into radare2. {condret Lukas}

CSC 405 Computer Security Shellcode

McSema: Static Translation of X86 Instructions to LLVM

Remix: On-demand Live Randomization

Automatic Generation of Memory Analysis Plugins. Brendan Dolan-Gavitt OMFW 2011

Giridhar Ravipati University of Wisconsin, Madison. The Deconstruction of Dyninst: Part 1- the SymtabAPI

Buffer Overflow Vulnerability

General ways to find and exploit directory traversals on Android. Xiang Xiaobo 360 Alpha Team

Command Line Interface / Application Programming Interface (cliapi) Kevin Sheldrake rtfc.org.uk

Win API hooking by using DBI: log me, baby!

Roadmap: Security in the software lifecycle. Memory corruption vulnerabilities

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

Advanced Buffer Overflow

Finding and Exploiting Access Control Vulnerabilities in Graphical User Interfaces

March 10, Linux Live Patching. Adrien schischi Schildknecht. Why? Who? How? When? (consistency model) Conclusion

Dirty COW Attack Lab

Adaptive Android Kernel Live Patching

Re-entrant code and Libraries

The Terminator to Android Hardening Services. Yueqian Zhang, Xiapu Luo, Haoyang Yin Department of Computing The Hong Kong Polytechnic University

Collect Linux Hardware Trace for ARMv8 User Space and Kernel Space Applications

CSC 591 Systems Attacks and Defenses Stack Canaries & ASLR

HW 8 CS681 & CS392 Computer Security Understanding and Experimenting with Memory Corruption Vulnerabilities DUE 12/18/2005

Using Hashing to Improve Volatile Memory Forensic Analysis

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Outline. Format string attack layout. Null pointer dereference

Outline. Outline. Common Linux tools to explore object/executable files. Revealing Internals of Loader. Zhiqiang Lin

CSE 509: Computer Security

C03c: Linkers and Loaders

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Are Your Mobile Apps Well Protected? Daniel Xiapu Luo Department of Computing The Hong Kong Polytechnic Unviersity

Transcription:

Romain Thomas - rthomas@quarkslab.com Static instrumentation based on executable file formats

About Romain Thomas - Security engineer at Quarkslab Working on various topics: Android, (de)obfuscation, software protection and reverse engineering Author of LIEF

Executable Formats Executable Formats: Overview

Executable Formats First layer of information when analysing a binary 1 entrypoint, libraries,

Executable Formats First layer of information when analysing a binary Provide metadata 1 used by the operating system to load the binary. 1 entrypoint, libraries,

Executable Formats OSX / ios: Mach-O Linux: ELF Windows: PE Android: ELF, OAT

Executable Formats Why modify formats?

Executable Formats Change segments Disable / sections ASLR permissions Userland More privileged area Format Loader Kernel Add shared libraries Load Perform shared relocations libraries Set Create Map permissions content Process

Executable Formats Change segments Disable / sections ASLR permissions Userland More privileged area Format Loader Kernel Add shared libraries Load Perform shared relocations libraries Set Create Map permissions content Process

Executable Formats Change segments Disable / sections ASLR permissions Userland More privileged area Format Loader Kernel Add shared libraries Load Perform shared relocations libraries Set Create Map permissions content Process

Executable Formats Change segments Disable / sections ASLR permissions Userland More privileged area Format Loader Kernel Add shared libraries Load Perform shared relocations libraries Set Create Map permissions content Process

Executable Formats Change segments Disable / sections ASLR permissions Userland More privileged area Format Loader Kernel Add shared libraries Load Perform shared relocations libraries Set Create Map permissions content Process

Executable Formats Change segments Disable / sections ASLR permissions Userland More privileged area Format Loader Kernel Add shared libraries Load Perform shared relocations libraries Set Create Map permissions content Process

Executable Formats Change segments Disable / sections ASLR permissions Userland More privileged area Format Loader Kernel Add shared libraries Load Perform shared relocations libraries Set Create Map permissions content Process

Executable Formats Change segments Disable / sections ASLR permissions Userland More privileged area Format Loader Kernel Add shared libraries Load Perform shared relocations libraries Set Create Map permissions content Process

Executable Formats Change segments Disable / sections ASLR permissions Userland More privileged area Format Loader Kernel Add shared libraries Load Perform shared relocations libraries Set Create Map permissions content Process

Executable Formats LIEF: Library to Instrument Executable Formats

LIEF One library to deal with ELF, PE, Mach-O Core in C++ Bindings for different languages: Python, C 2,... Enable modification on these formats User friendly API 2 C binding is not as mature as Python and C++

Executable Formats DEX File Object VDEX File Object ART File Object Parser ELF Binary Object PE Binary Object Mach-O Fat Binary Object OAT Binary Object Builder Abstract Binary Information extraction Information adding...

Executable Formats import lief target = lief.parse("elf/pe/mach-o/oat") print(target.entrypoint)

Executable Formats import lief target = lief.parse("elf/pe/mach-o/oat") for section in target.sections: print(section.virtual_address) process(section.content)

Executable Formats import lief target = lief.parse("some.exe") target.tls.callbacks.append(0x.) target.write("new.exe")

Executable Formats import lief target = lief.parse() section = lief.elf.section(".text2") section.content = [0x90] * 0x1000 target += section target.write("new.elf")

Executable Formats Next parts introduce interesting modifications on formats: Hooking Exporting hidden functions Code injection through shared libraries

PE Hooking PE Hooking

PE Hooking Regarding to PE files, LIEF enables to rebuild the import table elsewhere in the binary so that one can add new functions, new libraries or patch the Import Address Table.

Figure Original IAT Example

Example The following code patch the IAT entry of acrt_iob_func with a trampoline to the function 0x140008000 pe = lief.parse("some.exe") pe.hook_function(" acrt_iob_func", 0x140008000) builder = lief.pe.builder(pe) builder.build_imports(true).patch_imports(true) builder.build() builder.write("hooked.exe")

Example

Figure Original IAT patched with trampoline functions Example

Example Figure Trampoline for non-hooked function Figure Trampoline for hooked function

Example Limitations This method only works if accesses to the IAT are performed with call instructions. Especially it doesn t if there is lea on the original IAT

ELF Hooking Regarding to ELF files, hooking can be done with a patch of the plt/got.

ELF plt/got.text 400637: jmp 400480 <memcmp@plt> 1.plt 400480: jmp 201028 <memcmp@got> 400486: push 0x2 40048b: jmp 400450 <.plt> 3.got 2 201028: 0x400486

ELF plt/got.text 400637: jmp 400480 <memcmp@plt> 1.plt 400480: jmp 201028 <memcmp@got> 400486: push 0x2 40048b: jmp 400450 <.plt> 3.got 2 201028: 0x400486

ELF plt/got.text 400637: jmp 400480 <memcmp@plt> 1.plt 400480: jmp 201028 <memcmp@got> 400486: push 0x2 40048b: jmp 400450 <.plt> 3.got 2 201028: 0x400486

ELF plt/got.text 400637: jmp 400480 <memcmp@plt> 1.plt 400480: jmp 201028 <memcmp@got> 400486: push 0x2 40048b: jmp 400450 <.plt> 3.got 2 201028: 0x400486

Figure Relocations associated with plt/got ELF plt/got

ELF plt/got import lief elf = lief.parse("some_elf") elf.patch_pltgot("memcmp", 0xAAAAAAAA) elf.write("elf_modified")

https://lief.quarkslab.com/pts18/demo1 ELF plt/got.text 400637: jmp 400480 <memcmp@plt> 1.plt 400480: jmp 201028 <memcmp@got> 400486: push 0x2 40048b: jmp 400450 <.plt>.got 2 201028: XXXXXX <memcmp@hook> 3.hook XXXXXX: memcmp hooked

https://lief.quarkslab.com/pts18/demo1 ELF plt/got.text 400637: jmp 400480 <memcmp@plt> 1.plt 400480: jmp 201028 <memcmp@got> 400486: push 0x2 40048b: jmp 400450 <.plt>.got 2 201028: XXXXXX <memcmp@hook> 3.hook XXXXXX: memcmp hooked

https://lief.quarkslab.com/pts18/demo1 ELF plt/got.text 400637: jmp 400480 <memcmp@plt> 1.plt 400480: jmp 201028 <memcmp@got> 400486: push 0x2 40048b: jmp 400450 <.plt>.got 2 201028: XXXXXX <memcmp@hook> 3.hook XXXXXX: memcmp hooked

https://lief.quarkslab.com/pts18/demo1 ELF plt/got.text 400637: jmp 400480 <memcmp@plt> 1.plt 400480: jmp 201028 <memcmp@got> 400486: push 0x2 40048b: jmp 400450 <.plt>.got 2 201028: XXXXXX <memcmp@hook> 3.hook XXXXXX: memcmp hooked

Exporting Functions Exporting Functions

Idea

Idea

Example int main(int argc, char argv[]) { if (COMPLICATED CONDITION) { fuzz_me(argv[1]); } return 0; }

Example

Figure Original Symbol Table Example

Example import lief target = lief.parse("target") target.add_exported_function(0x63a, "to_fuzz") target.write("target_modified")

Example

Figure New Symbol Table Example

Example typedef void(*fnc_t)(const char*); // Access with dlopen / dlsym void* hdl = dlopen("./target_modified", RTLD_LAZY); fnc_t to_fuzz = (fnc_t)dlsym(hdl, "to_fuzz"); to_fuzz(to FEED); https://lief.quarkslab.com/pts18/demo2

Code injection Code injection through shared libraries

Context Different techniques exist to inject code: Using environment variables: LD_PRELOAD, DYLD_INSERT_LIBRARIES,... Using operating system API: WriteProcessMemory, ptrace,... Using custom kernel drivers Using executable formats

Context Depending on the scenario, methods can be suitable or not. Next part shows a method based on shared libraries and executable formats to leverage code injection.

Linked Libraries - Loading New library: libexample.so Userland More privileged area Format Loader Kernel Execute: my_constructor()

Injection process 1. Declare a constructor attribute ((constructor)) void my_constructor(void) { printf("run payload\n"); } gcc -fpic -shared libexample.c -o libexample.so gcc -fpic -shared libexample.c -o libexample.dylib

Injection process 2. Add a dependency import lief # ELF elf = lief.parse("/usr/bin/ssh") elf.add_library("libexample.so") elf.write("ssh_modified") # Mach-O macho = lief.parse("/bin/ls") macho.add_library("/users/romain/libexample.dylib") macho.write("ls_modified") # PE: Not implemented yet

Injection process https://lief.quarkslab.com/pts18/demo3

Injection process https://lief.quarkslab.com/pts18/demo3

Frida & LIEF Frida & LIEF: Frida injection in an Android application

Frida & LIEF Using the techniques previously described, we can use Frida on an APK having at least one native library without root privileges.

Figure Original APK Frida & LIEF

Frida & LIEF Figure Original APK Figure APK embedding Frida

Figure Original native library Frida & LIEF

Figure Modified native library Frida & LIEF

libgadget.config.so Frida & LIEF "interaction": { "type": "script", "path": "/data/local/tmp/myscript.js", "on_change": "reload" } /data/local/tmp/myscript.js Java.perform(function () { var Log = Java.use("android.util.Log"); var tag = "frida-lief"; Log.v(tag, "I'm in the process!"); Process.enumerateModules({ onmatch: function (module) { Log.v(tag, "Module: " + module.name); }, oncomplete: function () {} });});

Frida & LIEF Demo https://lief.quarkslab.com/pts18/demo4

Format modification Such modifications on formats are not new 34. However, it s implemented in LIEF with a new approach that doesn t rely on replacing existing entries, using padding, removing entries,... 3 Mayem Phrack #61 4 https://github.com/tyilo/insert_dylib

Format modification Instead, it keeps a consistent state of the format: Export trie Symbol hash tables Relocations Symbol versions Rebase opcodes...

What s next LIEF 0.9 comes with new formats related to Android: OAT VDEX DEX ART Modification of these formats is not available yet but further version will support it.

What s next How Samsung Secures Your Wallet & How To Break It - Black Hat 2017 Tencent s Xuanwu Lab

What s next Inside Android s SafetyNet Attestation: Attack and Defense - Ekoparty 2017 Collin Mulliner

What s next Next version will also include support for Mach-O modifications: Add unlimited number of Load commands Add libraries Change signature...

Thank You https://lief.quarkslab.com https://github.com/lief-project/lief @LIEF_Project - @rh0main