SlideShare a Scribd company logo
Devirtualizing FinSpy
  by Tora (@onceuponanop)
Outline
● Background info
● Main binary
● The first drop
  ○ Virtualization analysis
  ○ De-virtualization
● Further analysis
  ○ Collection of anti- tricks
  ○ The big picture
  ○ Crypto, MBR...
● Lessons learned
Background
● "From Bahrain with Love" post at citizenlab
  ○ Emails from a fake Aljazeera reporter account sent
    to Bahrein "activists".
  ○ Using the RTL trick to pretend to be .jpg's
  ○ citizenlab analyzed the malware and announced it
    as a component of FinFisher from Gamma Intl.
  ○ The post provides hashes for all the samples
    analyzed. Let's take a look at
    49000fc53412bfda157417e2335410cf69ac26b66b0
    818a3be7eff589669d040
Main sample
● Looks like an apparently harmless Windows
  application (WndProc does nothing)
The first drop
● Entry point looks normal, but then...
The first drop
● Very simple obfuscation
Virtualization analysis
● VM setup

                   Context
ThreadID                               Instruction                Decrypted
                    Index
                                       Context (CTX)              VM code
                    Table




●   Offset to VM instruction code            ●   Copy of stack pointer
●   Max valid address inside context         ●   Search VirtualEIP function
●   Temp register                            ●   Current instruction VEIP
●   Return address                           ●   Opcode
●   Return via epilogue                      ●   Relocation information
●   Obfuscation relative offset              ●   Raw bytes
●   Process imagebase                        ●   First free address
Virtualization analysis
Basic flow of main loop
  1. Disable NX if possible
  2. Allocate an array of "VM
     context" handles
  3. Allocate a context for current
     thread (CTX)
  4. Unpack VM
  5. Search for entry point
  6. Prepare VM OP instruction
  7. Decrypt VM code
  8. Execute virtual OP
  9. Goto 6
Virtualization analysis
Decrypted VM code
Virtualization analysis
Virtualization analysis
● Opcodes: 11 opcodes used. Two types
  ○ Native: "Raw bytes" are used to construct x86 native
    code and executed.
  ○ VM-level: just modifications on the CTX structure,
    basically operations with the temp register
Virtualization analysis
● Native-execution opcodes

                      CTX struct


                   first free address
                                        RDTSC
                         ...            & 0x3F
     CALL SUB?         POPFD
     +0x30             POPAD
                         ...

                         ...
                       POPFD
                       POPAD
                         ...
Virtualization analysis
Opcodes 0x01 and 0x04: Execute native code
  start+0: POPFD
  start+1: POPAD
  start+2: [native code]
  ...
  ret_code+0: PUSH <VM_loop>
  ret_code+5: RETN
  VM_loop+0: PUSHA
  VM_loop+1: PUSHF
Virtualization analysis
Opcode 0x06: Native register to temp register
  idx = 7 - BYTE PTR:CTX[op_bytes]
  saved_esp = CTX[esp]
  CTX[temp] = saved_esp[idx*4+4]
  CTX[VEIP_off] += 0x18
  EAX = VM_loop
  ESP = CTX[esp]
  JMP CTX[ret_eax] ; Ret-to-EAX epilogue
Virtualization analysis
Opcode 0x06: Native register to temp register
   PUSHFD + PUSHAD
   EFLAGS            (+4)
   POP EDI           (CTX[op_bytes]   =   7)
   POP ESI           (CTX[op_bytes]   =   6)
   POP ESP           (CTX[op_bytes]   =   5)
   POP EBP           (CTX[op_bytes]   =   4)
   POP EBX           (CTX[op_bytes]   =   3)
   POP EDX           (CTX[op_bytes]   =   2)
   POP ECX           (CTX[op_bytes]   =   1)
   POP EAX           (CTX[op_bytes]   =   0)
Virtualization analysis
CTX[ret_eax] epilogue
  [ESP-4] = EAX
  POPF      POPAD does
  POP EDI   POP EDI
  POP ESI   POP ESI
  POP EBP   POP EBP
  POP EBX   INC ESP+4          POPAD
  POP EBX   POP EBX
  POP EDX   POP EDX
  POP ECX   POP ECX
  POP EAX   POP EAX
  JMP [ESP-0x28] ; Initial EAX value
Virtualization analysis
Opcode 0x09: Temp register to native register
  index = 7 - BYTE PTR:CTX[op_bytes]
  saved_esp = CTX[esp]
  temp = CTX[0x08]
  saved_esp[index*4+4] = temp
  CTX[VEIP_off] += 0x18
  EAX = VM_loop
  ESP = CTX[esp]
  JMP CTX[ret_eax] ; Ret-to-EAX epilogue
Virtualization analysis
Opcode 0x07: Push temp register
  saved_esp = CTX[esp]
  memmove(saved_esp-4, saved_esp, 0x24)
  CTX[esp] -= 4
  temp_register = CTX[temp]
  saved_esp = CTX[esp]
  saved_esp[0x24] = temp_register
  CTX[VEIP_off] += 18
  EAX = VM_loop
  ESP = CTX[esp]
  JMP CTX[ret_eax] ; Ret-to-EAX epilogue
Virtualization analysis
 Opcode 0x07: Push temp register
0x00000000

                             VM           VM
                          saved regs   saved regs
                VM
             saved regs
                                       temp_reg




               Native       Native       Native
               Stack        Stack        Stack




 0xFFFFFFFF
Virtualization analysis
Opcode 0x05: Move raw value to temp register
  CTX[temp] = CTX[op_bytes]
  CTX[VEIP_off] += 0x18
  EAX = VM_loop
  ESP = CTX[esp]
  JMP CTX[ret_eax] ; Ret-to-EAX epilogue
Virtualization analysis
Ocpode 0x08: Dereference temp register
  CTX[temp] = DWORD PTR[CTX[temp]]
  CTX[VEIP_off] += 0x18
  EAX = VM_loop
  ESP = CTX[esp]
  JMP CTX[ret_eax] ; Ret-to-EAX epilogue
Virtualization analysis
Opcode 0x0A: Temp register to address
  address = CTX[op_bytes]
  [address] = CTX[temp]
  CTX[VEIP_off] += 0x18
  EAX = VM_loop
  ESP = CTX[esp]
  JMP CTX[ret_eax] ; Ret-to-EAX epilogue
Virtualization analysis
Opcode 0x03: Call native/imports
  start+0: POPFD; POPAD
  start+2: PUSH <api_ret>
  start+7: [native_jmp]
  api_ret+0: PUSH [VirtualEIP]
  api_ret+5: PUSHAD; PUSHFD
  api_ret+7: PUSH <&CTX>
  api_ret+8: POP EBX
  api_ret+9: PUSH <call_epilogue>
  api_ret+A: RETN
Virtualization analysis
call epilogue
  EAX = VirtualEIP
  offset = search_instr_code(EAX, VM_code)
  memmove(ESP+4, ESP, 0x24)
  ESP += 4
  CTX[VEIP_off] = offset
  CTX[first_free_addr] -= 0x30
  EAX = VM_loop
  JMP CTX[ret_eax] ; Ret-to-EAX epilogue
Virtualization analysis
Opcode 0x02: Call native (direct)
  start+0: POPFD+POPAD
  start+2: PUSH <native_ret>
  start+7: PUSH [target]
  start+C: RETN
  native_ret+0: PUSH [VirtualEIP]
  native_ret+5: PUSHAD; PUSHFD
  native_ret+7: PUSH <&CTX>; POP EBX
  native_ret+D: PUSH <CTX[call_epilogue]>
  native_ret+13: RETN
Virtualization analysis
Opcode 0x00: Conditional jump
  from VM code: POPFD
  start: XX02 -> 7X02 -> JZ/JS/JO.. start+4
  start+2: F8 -> CLC
  start+3: B0F9 -> MOV AL, 0xF9
  start+4:   F9 -> STC
  start+5: MOV EAX, <condition_check>
  start+A: JMP EAX
Virtualization analysis
Opcode 0x00: Conditional jump
  JB <jump_taken>
  CTX[VEIP_off] += 0x18
  [...]
  JMP CTX[ret_eax] ; Ret-to-EAX epilogue
  jump_taken: if CTX[op_bytes+1] == 0:
     VEIPtoOffset()
     CTX[VEIP_off] += 0x18 [...]
  else
     EAX = imagebase + CTX[op_bytes+5]
     JMP CTX[ret_eax] ; Ret-to-EAX epilogue
Virtualization analysis
How disasembly actually looks like:
0xf6db9a6a 040600008B3D3C10 mov edi,[0x40103c] KERNEL32.dll_GetModuleHandleW
0xf6db9a70 0402000033F60000 xor esi,esi
0xf6db9a74 0600000006000000 mov temp, esi
0xc27f370e 0700000000000000 push temp     (esi)
0xf6db9a75 030200007B9ADBF6 jmp edi ; jmp TAG:0xf6db9a7b
[...]
0xf6db9a86 0600000000000000 mov temp, eax
0xdd2ca350 0A000000807F4000 mov [0x407f80], temp   (eax)
0xf6db9a8f 040600008D85C0FD lea eax,[ebp-0x240]
0xf6db9a97 0600000006000000 mov temp, esi
0x27227d8a 0700000000000000 push temp     (esi)
0xf6db9aa0 0600000000000000 mov temp, eax
0x5d32a971 0700000000000000 push temp     (eax)
0xf6db9aa1 02000000A99ADBF6 call 0x405cf0; jmp TAG:0xf6db9aa9
0xf6db9aa9 0500000008020000 mov temp, 0x208
De-virtualization
● Scan code for jump to the VM (PUSH
  <VirtualEIP> + JMP VM_start)
● Calculate padding to next function (optional)
● Unpack and decrypt VM code
● Search for each VirtualEIP
● Translate VM into x86 code (easy!)
● Overwrite padding with generated x86 code
  ○ Stop when VirtualEIP is referenced by another VM
    jump, as that's the entry point of another function.
  ○ Yes, we're lucky that instructions are sequential ;)
Analysis of de-virtualized code
● De-virtualized code contains several anti-*
  tricks
● All of them are known, so not so much fun
● Lots of blacklisted id's (who were they trying
  to avoid?)
Analysis of de-virtualized code
● Blacklisted values
  ○   SOFTWAREMicrosoftCryptographyMachineGuid   != 6ba1d002-
      21ed-4dbe-afb5-08cf8b81ca32
  ○   SOFTWAREMicrosoftWindows NTCurrentVersionDigitalProductId   !
      = 55274-649-6478953-23109, A22-00001, 47220
  ○   HARDWAREDescriptionSystemSystemBiosDate != 01/02/03
  ○   GetVersion() != 5 (major version)
  ○   CS (code segment) == 0x1b || 0x23 (user-mode
      check?)
  ○   Hashes module path (and all its substrings) and
      checks that hash != 0xA51198F4
Analysis of de-virtualized code
● Anti-debug:
  ○ Checks PEB for the BeingDebugged flag
  ○ Uses the Ollydbg float exploit (FFFF...3D40)
  ○ Replace DbgBreakPoint function (is a single int3) with
    a NOP.
  ○ ZwSetInformationThread with ThreadInformationClass
    == 0x11 (detach debugger)
  ○ CloseHandle() with invalid handle
  ○ ZwQueryInformationProcess with
    ThreadInformationClass == 0x7 ProcessDebugPort and
    0x1E ProcessDebugObjectHandle
  ○ ZwSetInformationThread enabling
     ThreadHideFromDebugger
Analysis of de-virtualized code
● Misc anti-*:
   ○ Manual load of DLLs. Open, read, apply relocs and
     then parse export directory to resolve APIs by hash.
   ○ Opens JobObjects with names like
     LocalCOMODO_SANDBOX_0x%X_R%d (%X is PID and %d is
     in range [1-6]).
      ■ If it succeeds, call BasicUIRestrictions and
         ExtendLimitInformation (seems limiting memory
         usage to a really low limit)
   ○ (Continues...)
Analysis of de-virtualized code
● Misc anti-*:
   ○ Check running process and modules looking for:
         ●   cmdguard.sys and cfp.exe for Comodo
         ●   klif.sys and avp.exe for Kaspersky
         ●   bdspy.sys and bullguard.exe for BullGuard
         ●   ccsvchst.exe for Symantec
         ●   fsm32.exe and fsma32.exe for F-Secure
         ●   rfwtdi.sys and rsfwdrv.sys for Beijing Rising
      ■ No AVKills, but depending on present AV the
        sample uses different drop/inject methods
      ■ For Kaspersky, it even opens the avp.exe file and
        checks for the version inside (ver 0xB000)
Analysis of de-virtualized code
● Misc anti-*:
   ○ DLLs with invalid IATs




                         Import name hashes
Analysis of de-virtualized code
● Enough anti-stuff, what is the payload??
● Actually it just drops more samples,
  depending on the environment.
   ○ Sample drops a 32-bit or 64-bit DLL depending on
     the OS
   ○ DLL is loaded/injected depending on what AV
     product is present
● So, all this boring stuff just to get a couple of
  dropped files? Now what?
   ○ Now we have a de-virtualizer, we can automate and
     get rid of all this much faster...
RT_DIALOG                      RT_BITMAP                                RT_"BIN"
                       (XOR)                          (XOR)            Drop101                (XOR)              Drop101
Original                                                                  DLL
                                 Drop101                                                                          driver
sample                                                                 encrypted
                                                                        imports



                                        RT_BITMAP                                                         RT_"BIN"
                                          (XOR)                                                            (XOR +
    RT_DIALOG                                                                                             DEFLATE)
      (XOR)

                                 Drop102
                                  64-bit                                                                             Drop102                                                          injected in iexplore.exe
                                                                                     RT_"BIN"                                                                                        (communication module)
                                                                                     (PLAIN)                            DLL
Doc,                                                                                                                  fltlib.dll
PNG,
PDF                                                                                                                                                                                         Drop 101
                                                                                                                                                                                              DLL




                                                                                                                                   de loa
                                  ...




                                                                                                                                     cr ds
                                                                                                                                       yp
                                                                                                                                          ts
                                                                                                                                                         RT_"BIN"




                                                                                                                                             &
                                                                                                                                                         (PLAIN)
                                                                                                    Drop108
                                                                                                     MBR
                                                                          RT_"BIN"
                                                                          (PLAIN)                                                                              RT_"BIN"
                                                                                                                                         Drop 257               (XOR)
                                                                                                                                           DLL                                            Drop 102
                                                                                                                                                                                           driver




                                                                                                                     RT_24 (XOR
                                                    RT_ICON                                                          + DEFLATE)
                                                     (RC4)                                                                                                                                             Drop 104
                                                                                                                                                    RT_"BIN"
                                                                                                                                                                             RT_"BIN"                DLL encrypted
                                                                                                                                                    (PLAIN)
                                                                                           Drop109                                                                           (PLAIN)                    imports
                                                                                         DLL encrypted
                                                                                            imports

                                                                                                                                                                       Drop 103
                                                                                                                                                                     DLL encrypted
                                                                                                                                                                        imports
                                                                                     RT_24
                                                                                     (RC4)
                                                                                                                              RT_24
                                                                                                                           (XOR, RC4?...)
                                                                                     RES_261
                                                                                      (RC4)
                                                                                                                                                                                                     RC4 keys (size
                                                                                                                                    RES_260                                                            = 0x400)
                                                                     !
                                                                  me
                                                                sa
       Plain executable                                                                                            Two blobs of data. First xor-
                                                                                                                 encrypted contains C2 hostname
                                                                                                                 and other strings. Second is RC4-
                                                                                                                  encrypted (key in the RT_ICON
       Virtualized code
                                               RT_ICON (RC4                                                                  resource)
                                                 encrypted)

       kernel + virtualized



       unknown
Analysis of de-virtualized code
● Crypto
  ○ XOR for most drops (key fixed or in some cases key
    is timestamp from PE header)
  ○ RC4 for critical data resources, keys are stored in a
    common config file.
  ○ In some cases, filename is the key.
● MBR
  ○ Probably worth another talk ;)
  ○ Is in charge to load the hiding driver during boot
  ○ MBR payload is constructed from a template, so
    component that installs it has to "fill the blanks" like
    disk geometry params and payloads.
  ○ Infection check: if MBR[0x2C:0x2D] == CD 18
    (int18h), then you may have a problem
Lessons learned
● VM really well designed
  ○ Same VM works for x86-32 and 64bit
  ○ The conditional jump emulation was the key to avoid
    having to worry about EFLAGS emulation.
● Complex malware == modular project.
  ○ However modular means you can face older/buggy
    versions of components you already analyzed (ex:
    APLib).
● Removing virtualization is sometimes
  possible (cost < benefit)
  ○ In this case, benefit was obvious because of the
    number of virtualized modules using the same VM
Questions?

More Related Content

What's hot

Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
Matt Provost
 
Instruction Combine in LLVM
Instruction Combine in LLVMInstruction Combine in LLVM
Instruction Combine in LLVM
Wang Hsiangkai
 
LLVM Register Allocation
LLVM Register AllocationLLVM Register Allocation
LLVM Register Allocation
Wang Hsiangkai
 
Reverse engineering20151112
Reverse engineering20151112Reverse engineering20151112
Reverse engineering20151112
Bordeaux I
 
ofxTonic und Sound
ofxTonic und SoundofxTonic und Sound
ofxTonic und Sound
JeongHo Park
 
Machine Trace Metrics
Machine Trace MetricsMachine Trace Metrics
Machine Trace Metrics
Wang Hsiangkai
 
Scale17x buffer overflows
Scale17x buffer overflowsScale17x buffer overflows
Scale17x buffer overflows
johseg
 
Алексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereАлексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhere
Sergey Platonov
 
TDOH x 台科 pwn課程
TDOH x 台科 pwn課程TDOH x 台科 pwn課程
TDOH x 台科 pwn課程
Weber Tsai
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScript
Jens Siebert
 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
Rodrigo Almeida
 
Exploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelExploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernel
Vitaly Nikolenko
 
Javascript compilation execution
Javascript compilation executionJavascript compilation execution
Javascript compilation execution
Fanis Prodromou
 
PVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentPVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications development
OOO "Program Verification Systems"
 
Qemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System EmulationQemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System Emulation
National Cheng Kung University
 
Secure coding for developers
Secure coding for developersSecure coding for developers
Secure coding for developers
sluge
 
Global Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the SealGlobal Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the Seal
Tzung-Bi Shih
 
Process management
Process managementProcess management
Process management
Utkarsh Kulshrestha
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...Positive Hack Days
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPMiller Lee
 

What's hot (20)

Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 
Instruction Combine in LLVM
Instruction Combine in LLVMInstruction Combine in LLVM
Instruction Combine in LLVM
 
LLVM Register Allocation
LLVM Register AllocationLLVM Register Allocation
LLVM Register Allocation
 
Reverse engineering20151112
Reverse engineering20151112Reverse engineering20151112
Reverse engineering20151112
 
ofxTonic und Sound
ofxTonic und SoundofxTonic und Sound
ofxTonic und Sound
 
Machine Trace Metrics
Machine Trace MetricsMachine Trace Metrics
Machine Trace Metrics
 
Scale17x buffer overflows
Scale17x buffer overflowsScale17x buffer overflows
Scale17x buffer overflows
 
Алексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereАлексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhere
 
TDOH x 台科 pwn課程
TDOH x 台科 pwn課程TDOH x 台科 pwn課程
TDOH x 台科 pwn課程
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScript
 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
 
Exploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelExploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernel
 
Javascript compilation execution
Javascript compilation executionJavascript compilation execution
Javascript compilation execution
 
PVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentPVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications development
 
Qemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System EmulationQemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System Emulation
 
Secure coding for developers
Secure coding for developersSecure coding for developers
Secure coding for developers
 
Global Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the SealGlobal Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the Seal
 
Process management
Process managementProcess management
Process management
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMP
 

Similar to Devirtualizing FinSpy

Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Asuka Nakajima
 
Debugging TV Frame 0x09
Debugging TV Frame 0x09Debugging TV Frame 0x09
Debugging TV Frame 0x09
Dmitry Vostokov
 
X86 assembly & GDB
X86 assembly & GDBX86 assembly & GDB
X86 assembly & GDB
Jian-Yu Li
 
class04_x86assembly.ppt hy there u need be
class04_x86assembly.ppt hy there u need beclass04_x86assembly.ppt hy there u need be
class04_x86assembly.ppt hy there u need be
mnewg218
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
Ji Hun Kim
 
Demystify eBPF JIT Compiler
Demystify eBPF JIT CompilerDemystify eBPF JIT Compiler
Demystify eBPF JIT Compiler
Netronome
 
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
Andrey Karpov
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)bolovv
 
20190521 pwn 101_by_roy
20190521 pwn 101_by_roy20190521 pwn 101_by_roy
20190521 pwn 101_by_roy
Roy
 
Bypassing DEP using ROP
Bypassing DEP using ROPBypassing DEP using ROP
Bypassing DEP using ROP
Japneet Singh
 
HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1
Linaro
 
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321Teddy Hsiung
 
An Example MIPS
An Example  MIPSAn Example  MIPS
An Example MIPS
Sandra Long
 
NYU hacknight, april 6, 2016
NYU hacknight, april 6, 2016NYU hacknight, april 6, 2016
NYU hacknight, april 6, 2016
Mikhail Sosonkin
 
Javascript engine performance
Javascript engine performanceJavascript engine performance
Javascript engine performanceDuoyi Wu
 
The Stack and Buffer Overflows
The Stack and Buffer OverflowsThe Stack and Buffer Overflows
The Stack and Buffer Overflows
UTD Computer Security Group
 
Return Oriented Programming - ROP
Return Oriented Programming - ROPReturn Oriented Programming - ROP
Return Oriented Programming - ROP
Mihir Shah
 
JIT compilation for CPython
JIT compilation for CPythonJIT compilation for CPython
JIT compilation for CPython
delimitry
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to knowRoberto Agostino Vitillo
 
A compact bytecode format for JavaScriptCore
A compact bytecode format for JavaScriptCoreA compact bytecode format for JavaScriptCore
A compact bytecode format for JavaScriptCore
Tadeu Zagallo
 

Similar to Devirtualizing FinSpy (20)

Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
 
Debugging TV Frame 0x09
Debugging TV Frame 0x09Debugging TV Frame 0x09
Debugging TV Frame 0x09
 
X86 assembly & GDB
X86 assembly & GDBX86 assembly & GDB
X86 assembly & GDB
 
class04_x86assembly.ppt hy there u need be
class04_x86assembly.ppt hy there u need beclass04_x86assembly.ppt hy there u need be
class04_x86assembly.ppt hy there u need be
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
 
Demystify eBPF JIT Compiler
Demystify eBPF JIT CompilerDemystify eBPF JIT Compiler
Demystify eBPF JIT Compiler
 
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
20190521 pwn 101_by_roy
20190521 pwn 101_by_roy20190521 pwn 101_by_roy
20190521 pwn 101_by_roy
 
Bypassing DEP using ROP
Bypassing DEP using ROPBypassing DEP using ROP
Bypassing DEP using ROP
 
HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1
 
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
 
An Example MIPS
An Example  MIPSAn Example  MIPS
An Example MIPS
 
NYU hacknight, april 6, 2016
NYU hacknight, april 6, 2016NYU hacknight, april 6, 2016
NYU hacknight, april 6, 2016
 
Javascript engine performance
Javascript engine performanceJavascript engine performance
Javascript engine performance
 
The Stack and Buffer Overflows
The Stack and Buffer OverflowsThe Stack and Buffer Overflows
The Stack and Buffer Overflows
 
Return Oriented Programming - ROP
Return Oriented Programming - ROPReturn Oriented Programming - ROP
Return Oriented Programming - ROP
 
JIT compilation for CPython
JIT compilation for CPythonJIT compilation for CPython
JIT compilation for CPython
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to know
 
A compact bytecode format for JavaScriptCore
A compact bytecode format for JavaScriptCoreA compact bytecode format for JavaScriptCore
A compact bytecode format for JavaScriptCore
 

Recently uploaded

To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 

Devirtualizing FinSpy

  • 1. Devirtualizing FinSpy by Tora (@onceuponanop)
  • 2. Outline ● Background info ● Main binary ● The first drop ○ Virtualization analysis ○ De-virtualization ● Further analysis ○ Collection of anti- tricks ○ The big picture ○ Crypto, MBR... ● Lessons learned
  • 3. Background ● "From Bahrain with Love" post at citizenlab ○ Emails from a fake Aljazeera reporter account sent to Bahrein "activists". ○ Using the RTL trick to pretend to be .jpg's ○ citizenlab analyzed the malware and announced it as a component of FinFisher from Gamma Intl. ○ The post provides hashes for all the samples analyzed. Let's take a look at 49000fc53412bfda157417e2335410cf69ac26b66b0 818a3be7eff589669d040
  • 4. Main sample ● Looks like an apparently harmless Windows application (WndProc does nothing)
  • 5. The first drop ● Entry point looks normal, but then...
  • 6. The first drop ● Very simple obfuscation
  • 7. Virtualization analysis ● VM setup Context ThreadID Instruction Decrypted Index Context (CTX) VM code Table ● Offset to VM instruction code ● Copy of stack pointer ● Max valid address inside context ● Search VirtualEIP function ● Temp register ● Current instruction VEIP ● Return address ● Opcode ● Return via epilogue ● Relocation information ● Obfuscation relative offset ● Raw bytes ● Process imagebase ● First free address
  • 8. Virtualization analysis Basic flow of main loop 1. Disable NX if possible 2. Allocate an array of "VM context" handles 3. Allocate a context for current thread (CTX) 4. Unpack VM 5. Search for entry point 6. Prepare VM OP instruction 7. Decrypt VM code 8. Execute virtual OP 9. Goto 6
  • 11. Virtualization analysis ● Opcodes: 11 opcodes used. Two types ○ Native: "Raw bytes" are used to construct x86 native code and executed. ○ VM-level: just modifications on the CTX structure, basically operations with the temp register
  • 12. Virtualization analysis ● Native-execution opcodes CTX struct first free address RDTSC ... & 0x3F CALL SUB? POPFD +0x30 POPAD ... ... POPFD POPAD ...
  • 13. Virtualization analysis Opcodes 0x01 and 0x04: Execute native code start+0: POPFD start+1: POPAD start+2: [native code] ... ret_code+0: PUSH <VM_loop> ret_code+5: RETN VM_loop+0: PUSHA VM_loop+1: PUSHF
  • 14. Virtualization analysis Opcode 0x06: Native register to temp register idx = 7 - BYTE PTR:CTX[op_bytes] saved_esp = CTX[esp] CTX[temp] = saved_esp[idx*4+4] CTX[VEIP_off] += 0x18 EAX = VM_loop ESP = CTX[esp] JMP CTX[ret_eax] ; Ret-to-EAX epilogue
  • 15. Virtualization analysis Opcode 0x06: Native register to temp register PUSHFD + PUSHAD EFLAGS (+4) POP EDI (CTX[op_bytes] = 7) POP ESI (CTX[op_bytes] = 6) POP ESP (CTX[op_bytes] = 5) POP EBP (CTX[op_bytes] = 4) POP EBX (CTX[op_bytes] = 3) POP EDX (CTX[op_bytes] = 2) POP ECX (CTX[op_bytes] = 1) POP EAX (CTX[op_bytes] = 0)
  • 16. Virtualization analysis CTX[ret_eax] epilogue [ESP-4] = EAX POPF POPAD does POP EDI POP EDI POP ESI POP ESI POP EBP POP EBP POP EBX INC ESP+4 POPAD POP EBX POP EBX POP EDX POP EDX POP ECX POP ECX POP EAX POP EAX JMP [ESP-0x28] ; Initial EAX value
  • 17. Virtualization analysis Opcode 0x09: Temp register to native register index = 7 - BYTE PTR:CTX[op_bytes] saved_esp = CTX[esp] temp = CTX[0x08] saved_esp[index*4+4] = temp CTX[VEIP_off] += 0x18 EAX = VM_loop ESP = CTX[esp] JMP CTX[ret_eax] ; Ret-to-EAX epilogue
  • 18. Virtualization analysis Opcode 0x07: Push temp register saved_esp = CTX[esp] memmove(saved_esp-4, saved_esp, 0x24) CTX[esp] -= 4 temp_register = CTX[temp] saved_esp = CTX[esp] saved_esp[0x24] = temp_register CTX[VEIP_off] += 18 EAX = VM_loop ESP = CTX[esp] JMP CTX[ret_eax] ; Ret-to-EAX epilogue
  • 19. Virtualization analysis Opcode 0x07: Push temp register 0x00000000 VM VM saved regs saved regs VM saved regs temp_reg Native Native Native Stack Stack Stack 0xFFFFFFFF
  • 20. Virtualization analysis Opcode 0x05: Move raw value to temp register CTX[temp] = CTX[op_bytes] CTX[VEIP_off] += 0x18 EAX = VM_loop ESP = CTX[esp] JMP CTX[ret_eax] ; Ret-to-EAX epilogue
  • 21. Virtualization analysis Ocpode 0x08: Dereference temp register CTX[temp] = DWORD PTR[CTX[temp]] CTX[VEIP_off] += 0x18 EAX = VM_loop ESP = CTX[esp] JMP CTX[ret_eax] ; Ret-to-EAX epilogue
  • 22. Virtualization analysis Opcode 0x0A: Temp register to address address = CTX[op_bytes] [address] = CTX[temp] CTX[VEIP_off] += 0x18 EAX = VM_loop ESP = CTX[esp] JMP CTX[ret_eax] ; Ret-to-EAX epilogue
  • 23. Virtualization analysis Opcode 0x03: Call native/imports start+0: POPFD; POPAD start+2: PUSH <api_ret> start+7: [native_jmp] api_ret+0: PUSH [VirtualEIP] api_ret+5: PUSHAD; PUSHFD api_ret+7: PUSH <&CTX> api_ret+8: POP EBX api_ret+9: PUSH <call_epilogue> api_ret+A: RETN
  • 24. Virtualization analysis call epilogue EAX = VirtualEIP offset = search_instr_code(EAX, VM_code) memmove(ESP+4, ESP, 0x24) ESP += 4 CTX[VEIP_off] = offset CTX[first_free_addr] -= 0x30 EAX = VM_loop JMP CTX[ret_eax] ; Ret-to-EAX epilogue
  • 25. Virtualization analysis Opcode 0x02: Call native (direct) start+0: POPFD+POPAD start+2: PUSH <native_ret> start+7: PUSH [target] start+C: RETN native_ret+0: PUSH [VirtualEIP] native_ret+5: PUSHAD; PUSHFD native_ret+7: PUSH <&CTX>; POP EBX native_ret+D: PUSH <CTX[call_epilogue]> native_ret+13: RETN
  • 26. Virtualization analysis Opcode 0x00: Conditional jump from VM code: POPFD start: XX02 -> 7X02 -> JZ/JS/JO.. start+4 start+2: F8 -> CLC start+3: B0F9 -> MOV AL, 0xF9 start+4: F9 -> STC start+5: MOV EAX, <condition_check> start+A: JMP EAX
  • 27. Virtualization analysis Opcode 0x00: Conditional jump JB <jump_taken> CTX[VEIP_off] += 0x18 [...] JMP CTX[ret_eax] ; Ret-to-EAX epilogue jump_taken: if CTX[op_bytes+1] == 0: VEIPtoOffset() CTX[VEIP_off] += 0x18 [...] else EAX = imagebase + CTX[op_bytes+5] JMP CTX[ret_eax] ; Ret-to-EAX epilogue
  • 28. Virtualization analysis How disasembly actually looks like: 0xf6db9a6a 040600008B3D3C10 mov edi,[0x40103c] KERNEL32.dll_GetModuleHandleW 0xf6db9a70 0402000033F60000 xor esi,esi 0xf6db9a74 0600000006000000 mov temp, esi 0xc27f370e 0700000000000000 push temp (esi) 0xf6db9a75 030200007B9ADBF6 jmp edi ; jmp TAG:0xf6db9a7b [...] 0xf6db9a86 0600000000000000 mov temp, eax 0xdd2ca350 0A000000807F4000 mov [0x407f80], temp (eax) 0xf6db9a8f 040600008D85C0FD lea eax,[ebp-0x240] 0xf6db9a97 0600000006000000 mov temp, esi 0x27227d8a 0700000000000000 push temp (esi) 0xf6db9aa0 0600000000000000 mov temp, eax 0x5d32a971 0700000000000000 push temp (eax) 0xf6db9aa1 02000000A99ADBF6 call 0x405cf0; jmp TAG:0xf6db9aa9 0xf6db9aa9 0500000008020000 mov temp, 0x208
  • 29. De-virtualization ● Scan code for jump to the VM (PUSH <VirtualEIP> + JMP VM_start) ● Calculate padding to next function (optional) ● Unpack and decrypt VM code ● Search for each VirtualEIP ● Translate VM into x86 code (easy!) ● Overwrite padding with generated x86 code ○ Stop when VirtualEIP is referenced by another VM jump, as that's the entry point of another function. ○ Yes, we're lucky that instructions are sequential ;)
  • 30. Analysis of de-virtualized code ● De-virtualized code contains several anti-* tricks ● All of them are known, so not so much fun ● Lots of blacklisted id's (who were they trying to avoid?)
  • 31. Analysis of de-virtualized code ● Blacklisted values ○ SOFTWAREMicrosoftCryptographyMachineGuid != 6ba1d002- 21ed-4dbe-afb5-08cf8b81ca32 ○ SOFTWAREMicrosoftWindows NTCurrentVersionDigitalProductId ! = 55274-649-6478953-23109, A22-00001, 47220 ○ HARDWAREDescriptionSystemSystemBiosDate != 01/02/03 ○ GetVersion() != 5 (major version) ○ CS (code segment) == 0x1b || 0x23 (user-mode check?) ○ Hashes module path (and all its substrings) and checks that hash != 0xA51198F4
  • 32. Analysis of de-virtualized code ● Anti-debug: ○ Checks PEB for the BeingDebugged flag ○ Uses the Ollydbg float exploit (FFFF...3D40) ○ Replace DbgBreakPoint function (is a single int3) with a NOP. ○ ZwSetInformationThread with ThreadInformationClass == 0x11 (detach debugger) ○ CloseHandle() with invalid handle ○ ZwQueryInformationProcess with ThreadInformationClass == 0x7 ProcessDebugPort and 0x1E ProcessDebugObjectHandle ○ ZwSetInformationThread enabling ThreadHideFromDebugger
  • 33. Analysis of de-virtualized code ● Misc anti-*: ○ Manual load of DLLs. Open, read, apply relocs and then parse export directory to resolve APIs by hash. ○ Opens JobObjects with names like LocalCOMODO_SANDBOX_0x%X_R%d (%X is PID and %d is in range [1-6]). ■ If it succeeds, call BasicUIRestrictions and ExtendLimitInformation (seems limiting memory usage to a really low limit) ○ (Continues...)
  • 34. Analysis of de-virtualized code ● Misc anti-*: ○ Check running process and modules looking for: ● cmdguard.sys and cfp.exe for Comodo ● klif.sys and avp.exe for Kaspersky ● bdspy.sys and bullguard.exe for BullGuard ● ccsvchst.exe for Symantec ● fsm32.exe and fsma32.exe for F-Secure ● rfwtdi.sys and rsfwdrv.sys for Beijing Rising ■ No AVKills, but depending on present AV the sample uses different drop/inject methods ■ For Kaspersky, it even opens the avp.exe file and checks for the version inside (ver 0xB000)
  • 35. Analysis of de-virtualized code ● Misc anti-*: ○ DLLs with invalid IATs Import name hashes
  • 36. Analysis of de-virtualized code ● Enough anti-stuff, what is the payload?? ● Actually it just drops more samples, depending on the environment. ○ Sample drops a 32-bit or 64-bit DLL depending on the OS ○ DLL is loaded/injected depending on what AV product is present ● So, all this boring stuff just to get a couple of dropped files? Now what? ○ Now we have a de-virtualizer, we can automate and get rid of all this much faster...
  • 37. RT_DIALOG RT_BITMAP RT_"BIN" (XOR) (XOR) Drop101 (XOR) Drop101 Original DLL Drop101 driver sample encrypted imports RT_BITMAP RT_"BIN" (XOR) (XOR + RT_DIALOG DEFLATE) (XOR) Drop102 64-bit Drop102 injected in iexplore.exe RT_"BIN" (communication module) (PLAIN) DLL Doc, fltlib.dll PNG, PDF Drop 101 DLL de loa ... cr ds yp ts RT_"BIN" & (PLAIN) Drop108 MBR RT_"BIN" (PLAIN) RT_"BIN" Drop 257 (XOR) DLL Drop 102 driver RT_24 (XOR RT_ICON + DEFLATE) (RC4) Drop 104 RT_"BIN" RT_"BIN" DLL encrypted (PLAIN) Drop109 (PLAIN) imports DLL encrypted imports Drop 103 DLL encrypted imports RT_24 (RC4) RT_24 (XOR, RC4?...) RES_261 (RC4) RC4 keys (size RES_260 = 0x400) ! me sa Plain executable Two blobs of data. First xor- encrypted contains C2 hostname and other strings. Second is RC4- encrypted (key in the RT_ICON Virtualized code RT_ICON (RC4 resource) encrypted) kernel + virtualized unknown
  • 38. Analysis of de-virtualized code ● Crypto ○ XOR for most drops (key fixed or in some cases key is timestamp from PE header) ○ RC4 for critical data resources, keys are stored in a common config file. ○ In some cases, filename is the key. ● MBR ○ Probably worth another talk ;) ○ Is in charge to load the hiding driver during boot ○ MBR payload is constructed from a template, so component that installs it has to "fill the blanks" like disk geometry params and payloads. ○ Infection check: if MBR[0x2C:0x2D] == CD 18 (int18h), then you may have a problem
  • 39. Lessons learned ● VM really well designed ○ Same VM works for x86-32 and 64bit ○ The conditional jump emulation was the key to avoid having to worry about EFLAGS emulation. ● Complex malware == modular project. ○ However modular means you can face older/buggy versions of components you already analyzed (ex: APLib). ● Removing virtualization is sometimes possible (cost < benefit) ○ In this case, benefit was obvious because of the number of virtualized modules using the same VM