DBI Techniques to Address Native Code Obfuscation

DBI Techniques to Address Native Code Obfuscation

DBI Techniques to Address Native Code Obfuscation Romain Thomas <[email protected]> www.quarkslab.com About I Security engineer at Quarkslab I Working in a team that develops tools for reverse-engineering and vulnerabilities research I Author of LIEF I Enjoy Android and reverse-engineering Romain Thomas @rh0main 2 What this talk is about? 3 Obfuscators I Commercial solutions: Arxan, Cloakware, Epona I In-house LLVM based solution: Snapchat, TikTok, . I Open-source projects: O-LLVM, Hikari, Armariris, YANSOllvm, Goron, . 4 Research I Articles by R. Rolles on https://www.msreverseengineering.com/research I Breaking Spotify DRM with PANDA Recon 14, B. Dolan-Gavitt & al I DRM obfuscation versus auxiliary attacks Recon 14, M. Camille & F. Gabriel I API Deobfuscator Resolving Obfuscated API Functions In Modern Packers BH15-USA, S. Choi 5 Research I Automation Techniques in C++ Reverse Engineering Recon 19, R. Rolles I Breaking Mobile Userland NowSecure 19, G. Rocca I Symbolic Deobfuscation: From Virtualized Code Back to the Original Dimva 18, J. Salwan, S. Bardin & M. Potet I Differential Computation Analysis:Hiding your White-Box Designs is Not Enough SSTIC 16, C. Hubain, P. Teuwen 6 Research TL;DR:A DBI seems a good primitive to address obfuscation. 7 What is a DBI Binary Code Dynamic Binary Instrumentation is Dynamic an analysis process that aims to Instrumentation dynamically observe binary behavior: 0x01df4cmov w8,wzr |x08:0x0000000000 0x01df50cbz x0,#188 |x00:0x7fe8c5f5e8 0x01df54cbz x1,#184 |x01:0x790f9064c0 I Instructions 0x01df58tst w2,#0x7 |x02:0x000005fd00 0x01df5cmov w8,wzr |x08:0x0000000000 I Register values 0x01df60b.ne#172 | 0x01df64cbz w3,#168 |x03:0x0000000003 Basic blocks 0x01df68lsr w8,w2,#3 |x02:0x000005fd00 x08:0x000000bfa0 I 0x01df6ccbz w8,#156 |x08:0x000000bfa0 0x01df70mov w11,#31161 |x11:0x00000079b9 Dynamic trace at instruction level with registers information 8 DBI Frameworks Intel PIN: x86, x86-64 Valgrind: x86, x86-64, ARM, AArch64 DynamoRIO: x86, x86-64, ARM, AArch64 DynInst: x86, x86-64, ARM?, AArch64? Frida: Function hooking and stalker 9 DBI Frameworks += QBDI 10 QBDI Modular DBI framework created by Charles Hubain1 and C´edricTessier: Based on LLVM: Assembler, disassembler, instruction analysis and abstraction2 Architecture support: x86, x86-64, ARM3, AArch64 Open-source: https://github.com/quarkslab/QBDI4 1Co-author of Differential Computation Analysis:Hiding your White-Box Designs is Not Enough 2Thanks to the llvm::MCInst interface 3Full support which includes Thumb/Thumb2 4Except for ARM and AArch64 12 QBDI Features 0x17505A LDR R0, [R5] 0x17505C MOV R8, R1 0x17505E LDR R4, [R1, R6] 0x175060 LDR R2, [R0, #0x18] 0x175062 MOV R0, R5 0x175064 MOV R1, R4 0x175066 BLX R2 0x175068 MOV R1, R0 0x175068 MOV R1, R7 0x17506A MOV R0, R7 0x17506C SVC #0 0x17506E MOV R1, R0 0x175070 AND.W R1, R0, R3 0x175074 EOR.W R0, R0, R3 0x175078 BLX sub_175094 13 QBDI Features 0x17505A LDR R0, [R5] 0x17505C MOV R8, R1 1 VMAction callback(VMInstanceRef vm, 0x17505E LDR R4, [R1, R6] 2 GPRState *gprState, FPRState *fprState, 0x175060 3LDR R2, [R0, #0x18]F* user data F/ void* ctx){ 4 FF. 0x175062 MOV R0, R5 5 return VMActionF:CONTINUE; 0x175064 6MOV} R1, R4 7 0x175066 BLX R2 8 F/ ==================================================== F/ 0x175068 9MOV R1, R0 10 QBDIF:VM vm; 0x175068 11MOV R1, R7 0x17506A 12MOVvm.addCodeCBR0, R7(QBDIF:InstPositionF:PREINST, callback, 13 F* user data F/&ctx); 0x17506C 14SVC #0 0x17506E 15MOVvm.callR1,(FF., R0 function, fnc_args); 0x175070 AND.W R1, R0, R3 0x175074 EOR.W R0, R0, R3 0x175078 BLX sub_175094 14 QBDI Features QBDI InstructionCallback 0x17505A LDR R0, [R5] .text:0x17505A LDR R0,[R5] QBDI InstructionCallback 0x17505C MOV R8, R1 .text:0x17505C MOV R8,R1 QBDI InstructionCallback 0x17505E LDR R4, [R1, R6].text:0x17505E LDR R4,[R1,R6] QBDI InstructionCallback 0x175060 LDR R2, [R0, #0x18].text:0x175060 LDR R2,[R0,#0x18] 0x175062 MOV R0, R5 QBDI InstructionCallback .text:0x175062 MOV R0,R5 0x175064 MOV R1, R4 QBDI InstructionCallback .text:0x175064 MOV R1,R4 0x175066 BLX R2 QBDI InstructionCallback .text:0x175066 BLX R2 0x175068 MOV R1, R0 QBDI InstructionCallback .text:0x175068 MOV R1,R0 0x175068 MOV R1, R7 QBDI InstructionCallback .text:0x175068 MOV R1,R7 0x17506A MOV R0, R7 QBDI InstructionCallback .text:0x17506A MOV R0,R7 0x17506C SVC #0 QBDI InstructionCallback 0x17506E MOV R1, R0 .text:0x17506C SVC #0 QBDI InstructionCallback 0x175070 AND.W R1, R0, R3 .text:0x17506E MOV R1,R0 QBDI InstructionCallback 0x175074 EOR.W R0, R0, R3 .text:0x175070 AND.W R1,R0,R3 QBDI InstructionCallback 0x175078 BLX sub_175094 .text:0x175074 EOR.W R0,R0,R3 QBDI InstructionCallback .text:0x175078 BLX sub_175094 15 Moreover, instrumenting all the instructions can add a significant overhead! ) Be smart QBDI Obfuscators tend to add noisy instructions that are not relevant to understand the function's logic. 16 ) Be smart QBDI Obfuscators tend to add noisy instructions that are not relevant to understand the function's logic. Moreover, instrumenting all the instructions can add a significant overhead! 16 QBDI Obfuscators tend to add noisy instructions that are not relevant to understand the function's logic. Moreover, instrumenting all the instructions can add a significant overhead! ) Be smart 16 QBDI Features 0x17505A LDR R0, [R5] 0x17505C MOV R8, R1 0x17505A LDR R0, [R5] 0x17505E LDR R4, [R1,0x17505C R6] MOV R8, R1 0x175060 LDR R2, [R0,0x17505E #0x18]LDR R4, [R1, R6] 0x175060 LDR R2, [R0, #0x18] 0x175062 MOV R0, R5 0x175062 MOV R0, R5 0x175064 MOV R1, R4 0x175064 MOV R1, R4 0x175066 BLX R2 0x175066 BLX R2 0x175068 MOV R1, R0 0x175068 MOV R1, R0 0x175068 MOV R1, R7 0x175068 MOV R1, R7 0x17506A MOV R0, R7 0x17506A MOV R0, R7 0x17506C SVC #0 0x17506C SVC #0 0x17506E MOV R1, R0 0x17506E MOV R1, R0 0x175070 AND.W R1, R0, R3 0x175074 EOR.W R0, R0, R3 0x175070 AND.W R1, R0, R3 0x175078 BLX sub_175094 0x175074 EOR.W R0, R0, R3 0x175078 BLX sub_175094 17 QBDI Features 0x17505ALDR R0,[R5] 0x17505CMOV R8,R1 0x17505ELDR R4,[R1,R6] 0x175060LDR R2,[R0,#0x18] 1 QBDI::VMvm; 2 0x175062MOV R0,R5 3 //Callbacksbeforesyscallinstructions 0x175064MOV R1,R4 4 vm.addSyscallCB(PRE_SYSCALL,syscall_cbk,&ctx); 0x175066BLX R2 5 6 0x175068MOV R1,R0 //Callbacksbeforeblx/blinstructions 7 vm.addCallCB(PRE_CALL,call_cbk,&ctx); 0x175068MOV R1,R7 8 0x17506AMOV R0,R7 9 //Callbacksbeforememoryloadandstoreinstructions 10 0x17506CSVC #0 vm.addMemAccessCB(MEMORY_READ_WRITE,mem_cbk,&ctx); 11 0x17506EMOV R1,R0 12 vm.call(...,function,fnc_args); 0x175070AND.WR1,R0,R3 0x175074EOR.WR0,R0,R3 0x175078BLX sub 175094 18 QBDI Features QBDI Memory Callback .text:0x17505A LDR R0, [R5] .text:0x17505C0x17505A LDRMOV R0,R8, [R5] R1 QBDI0x17505C MOVMemoryR8, Callback R1 .text:0x17505E0x17505E LDRLDR R4,R4, [R1, [R1, R6] R6] QBDI0x175060 LDRMemoryR2, Callback [R0, #0x18] .text:0x1750600x175062 MOVLDR R0,R2, R5 [R0, #0x18] .text:0x175062 MOV R0, R5 0x175064 MOV R1, R4 .text:0x175064 MOV R1, R4 0x175066 BLX R2 QBDI Call Callback .text:0x1750660x175068 MOVBLX R1,R2 R0 .text:0x1750680x175068 MOVMOV R1,R1, R7 R0 .text:0x1750680x17506A MOVMOV R0,R1, R7 R7 .text:0x17506A0x17506C SVCMOV #0R0, R7 QBDI0x17506E MOVSyscallR1, Callback R0 .text:0x17506C0x175070 AND.WSVC R1,#0 R0, R3 .text:0x17506E MOV R1, R0 0x175074 EOR.W R0, R0, R3 .text:0x175070 AND.W R1, R0, R3 0x175078 BLX sub_175094 .text:0x175074 EOR.W R0, R0, R3 QBDI Call Callback .text:0x175078 BLX sub_175094 19 QBDI Features In addition, QBDI is able to resolve the effective memory address for free! 20 QBDI Features 0x17505A LDR R0, [R5] 1 VMAction mem_cbk(VMInstanceRef vm, FF.) { 0x17505C MOV R8, R1 2 3 F/ Memory info 0x17505E LDR R4, [R1, R6] 4 MemoryAccess info =0x175060vmF>getInstMemoryAccessLDR R2, [R0, #0x18]().back(); 5 F/ Provide: 0x175062 MOV R0, R5 6 F/ LDR R0, [R5] 0x175064 MOV F>R1, R5 R4 7 F/ LDR R4, [R1, R6]0x175066 BLX F>R2 R1 + R6 8 F/ LDR R2, [R3, #0x18]0x175068 MOV F>R1, R3 R0 + 0x18 9 F/ STRB R8, [R1, -R2,0x175068 LSL #4]MOV F>R1, R1 R7 - (R2 F< 4) 10 uintptr_t addr = maccess.accessAddress; 0x17506A MOV R0, R7 11 0x17506C SVC #0 12 F/ Value read or written (R0, R4, R2, R8) 13 uintptr_t value = maccess0x17506E.valueMOV ; R1, R0 14 FF. 0x175070 AND.W R1, R0, R3 15 return VMActionF:CONTINUE0x175074; EOR.W R0, R0, R3 16 } 0x175078 BLX sub_175094 21 QBDI Features Same mechanism for blx, bl, br, blr instructions1. 1not available in the public branch yet 22 QBDI Features 0x17505A LDR R0, [R5] 0x17505C MOV R8, R1 0x17505E LDR R4, [R1, R6] 1 VMAction call_cbk(VMInstanceRef vm, FF.) { 0x175060 LDR R2, [R0, #0x18] 2 0x175062 MOV R0, R5 3 F/ Provide: 4 F/ BLX R2 F> Value0x175064 of R2 MOV R1, R4 5 F/ BLX PC F> PC +0x175066 CPU ModeBLX shiftR2 + Alignment 6 F/ BLX #42 F> PC +0x175068 42 + CPUMOV ModeR1, shift R0 + Alignment 7 uintptr_t call_target0x175068= vmF>MOVgetInstCallAccessR1, R7 ().back(); 8 0x17506A MOV R0, R7 9 FF. 0x17506C SVC #0 10 return VMActionF:CONTINUE; 0x17506E MOV R1, R0 11 } 0x175070 AND.W R1, R0, R3 0x175074 EOR.W R0, R0, R3 0x175078 BLX sub_175094 23 External Calls What about external calls to malloc(), printf(), env->FindClass(),...? 24 QBDI implements a mechanism to stop the instrumentation process, leave the function executing its own code (without instrumentation) and catch when it returns.1 External Calls It would not be relevant to instrument their code right? 1By changing the return address (lr) to a dedicated stub that will resume the instrumentation process 25 External Calls It would not be relevant to instrument their code right? QBDI implements a mechanism to stop the instrumentation process, leave the function executing its own code (without instrumentation) and catch when it returns.1 1By changing the return address (lr) to a dedicated stub that will resume the instrumentation process 25 /system/lib/libc.so libc.so base address 0xeea90000 0x78850 [email protected] QBDI ExecBrocker LIEF .text:005B2A74 MOVS R0, #4 pc:0xeeb08850 .text:005B2A76 BLX.W malloc Not Instrumented ExecBrocker .text:00078850 PUSH.W R4-R11,LR QBDI::EXEC TRANSFER CALL .text:00078854 SUB SP, SP, #0x1C .text:00078856 MOV R4, R0 .text:00078858 LDR.W R0, =( stack chk guard ptr - 0x78862) .text:0007885C CMP R4, #0 QBDI::EXEC TRANSFER RETURN .text:0007885E ADD R0, PC ExecBrocker ..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    86 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us