1 / 40

Revealing Stealth Malware UMD CMSC389M

Revealing Stealth Malware UMD CMSC389M. Xeno Kovah – Jan. 2013 xkovah at gmail Subject line starting with "UMD: ". All materials is licensed under a Creative Commons “ Share Alike ” license. http://creativecommons.org/licenses/by-sa/3.0/. Outline. Newz u can uze

rance
Download Presentation

Revealing Stealth Malware UMD CMSC389M

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Revealing Stealth MalwareUMD CMSC389M Xeno Kovah – Jan. 2013 xkovah at gmail Subject line starting with "UMD:"

  2. All materials is licensed under a Creative Commons “Share Alike” license. • http://creativecommons.org/licenses/by-sa/3.0/

  3. Outline • Newz u can uze • How "system calls" work on Windows

  4. Awwwyiss! GMER 2.0 released! Supports Win 7/8 x64!

  5. System Calls • They're how userspace asks the kernel to do stuff for it • Implemented fairly similarly across OSes, usually a table of exported functions in kernel and userspace code just puts the function parameters in some place defined by that OS's own internal convention, and then they just say "dear kernel, I'd like to call the function at index <#> in the system call table please", and off the kernel goes • You need to see the full path, and know that attackers can hook basically everywhere along the path.

  6. SSDT Hook From: http://www.blackhat.com/presentations/bh-europe-06/bh-eu-06-Silberman-Butler.pdf

  7. SSDT Hook hehehe hehehe hehehe hehehe From: http://www.blackhat.com/presentations/bh-europe-06/bh-eu-06-Silberman-Butler.pdf

  8. Conceptual Separation of Duties Hardware ReadSector(123) Data Kernel OpenFile(PvZ.exe) Memory Mapped File Userspace Open Vid-yo-game! User

  9. Conceptual Separation of Duties Hardware ReadSector(423) Data Kernel ReadKey(HKLM\Bla) Registry Information Userspace Open Vid-yo-game! User

  10. Conceptual System Call Interface Kernel NtReadKey() NtWriteFile() NtShutdownSystem() NtOpenSemaphore() Userspace ReadKey() WriteFile() ShutdownSystem() OpenSemaphore() Yay Comp-u-tors! User

  11. "Native API" • The Native API refers to the way that Microsoft Windows' libraries are able to call from userspace to the kernel • This API is mostly undocumented, though people have long ago reverse engineered a lot of it. • Native API calls will start with with the prefix Nt or Zw, with the Nt* versions being the ones which are preferred to be called from userspace, and the Zw* versions being preferred to be called from kernel.

  12. Slightly More Accurate System Call Interface Kernel NtReadKey() NtWriteFile() NtShutdownSystem() NtOpenSemaphore() KiFastCallEntry() or KiSystemService() Native API calling interface ntdll.dll ReadKey() WriteFile() ShutdownSystem() OpenSemaphore() Yay Comp-u-tors! User

  13. Kernel User Ntdll.dll NtWriteFile(){ mov eax, 0x112 int 0x2E ORsysenter } Kernel32.dll WriteFile(){ Call [IAT:NtWriteFile] } Hook inline at target (seen it) Start Here MyApp.exe … Call [IAT:WriteFile] … Hook IAT (seen it) Example from Win XP

  14. Let's dig back into cmd.exe file listing for a sec… Somewhere deep in the belly of KERNELBASE!FindFirstFileExW… What the heck is a "syscall" instruction?

  15. But wait a sec…remember when we were trying to use ProcMon to get a better idea of what functions were used by cmd.exe to list files, but nothing Find*File-y was coming up?... And now you're telling me that behind the scenes, even though we got our first break on the FindFirstFileExW() function, that it eventually calls to this NtQueryDirectoryFile() Native API system call?

  16. Kernel Ntdll.dll NtQueryDirectoryFile(){ mov eax, 0x32 syscall } User kernelbase.dll FindFirstFileExW(){ call [IAT:NtQueryDirectoryFile] } kernel32.dll FindFirstFileExW(){ jmp [IAT:FindFirstFileExW] } Start Here cmd.Exe FindFirst(){ Call [IAT:FindFirstFileExW] } Example from Win 7 x64

  17. ntoskrnl.exe … KiSystemService(){ } The INT 0x2E path is the Win2k path IDT From userspace INT 0x2E Hook IDT new (prob going to be out of scope for class KiSystemService Kernel User

  18. The sysenter path is the more common Win XP path ntoskrnl.exe … KiFastCallEntry(){ } KiFastCallEntry != KiSystemService From userspace sysenter Hook sysenter (IA32_SYSENTER_EIP MSR) new Kernel User

  19. The syscall path is the more common Win * x64 path ntoskrnl.exe … KiFastCallEntry(){ } KiFastCallEntry != KiSystemService From userspace syscall Hook syscall (IA32_LSTAR MSR) new Kernel User

  20. Sysenter vs. syscall? • AMD came up with syscall first. • Intel wanted to go their own way, and made up sysenter to do basically the same thing. • Interesting confessional from the guy who designed sysenter in the notes • Eventually they relented and licensed syscall from AMD • And they all lived happily ever after • The End

  21. KeServiceDescriptorTable KeServiceDescriptorTableShadow ntoskrnl.exe … KiSystemService() or KiFastCallEntry(){ * Consult Thread Info * Extract address of System Service Descriptor Table (SSDT) which is KeServiceDescriptorTable normally or KeServiceDescriptorTableShadow if the process has used any graphical (GDI) routines * Parse eax for specific table entry } Kernel User

  22. Index to function mappings change between releases to discourage assumptions and SSDT hooking KeServiceDescriptorTable service number = eax = 0x112 00000100010010 2 bits table index Kernel 12 bits service index User

  23. Hook SSDT (new) ntoskrnl.exe … NtWriteFile(){ } Hook inline at target

  24. Kernel User GDI32.dll NtGdiUpdateColors(){ mov eax, 0x112E int 0x2E ORsysenter } UpdateColors(){ Call IAT:NtGdiUpdateColors () } Hook inline at target (seen it) Start Here MyApp.exe … Call IAT:UpdateColors() … Hook IAT (seen it)

  25. KeServiceDescriptorTable KeServiceDescriptorTableShadow ntoskrnl.exe … KiSystemService() or KiFastCallEntry(){ * Consult Thread Info * Extract address of System Service Descriptor Table (SSDT) which is KeServiceDescriptorTable normally or KeServiceDescriptorTableShadow if the process has used any graphical (GDI) routines * Parse eax for specific table entry } Kernel User

  26. Index to function mappings change between releases to discourage assumptions and SSDT hooking KeServiceDescriptorTableShadow service number = eax = 0x112E 01000100101110 2 bits table index Kernel 12 bits service index User

  27. Hook SSDT (new) win32k.sys … NtGdiUpdateColors(){ } Hook inline at target

  28. Big Picture … … KeServiceDescriptorTable win32k KeServiceDescriptorTableShadow nt … KiSystemService … KiFastCallEntry kernel IDT user sysenter/syscall (depending on Windows\HW version) INT 2e (depending on Windows/HW version) Ntdll.dll Kernel32.dll MyApp.exe

  29. ZO.M.G. it's Yoshi!!! (and Mario is doing the splits) … … KeServiceDescriptorTable win32k KeServiceDescriptorTableShadow nt … KiSystemService … KiFastCallEntry kernel user sysenter INT 2e Ntdll.dll Kernel32.dll MyApp.exe

  30. SSDT Hook From: http://www.blackhat.com/presentations/bh-europe-06/bh-eu-06-Silberman-Butler.pdf

  31. SSDT Hook hehehe hehehe hehehe hehehe From: http://www.blackhat.com/presentations/bh-europe-06/bh-eu-06-Silberman-Butler.pdf

  32. Nu2U - sysenter • We never talked about the sysenter instruction in Intermediate x86, due to lack of time, even though it would work well in that class. • Sort of as a background notion when we were talking about interrupts that they were 1) a way for hardware to get the CPU's attention, and 2) a way to get some kernel code to execute (BreakOnThruToTheOtherSide lab, discussion of interrupts underlying debugging.) • So "back in the day" systems would implement the "system call table"(*nix) or "system service descriptor table" (Windows) as a way for userspace code to ask the kernel to do specific actions for it. E.g. open a file, allocate some memory, • This was achieved by putting a system call number in some register and then calling int 0x80 (linux), or int 0x2e (Windows). The code on the kernel side would then just check the designated register(s) which were input parameters and call the appropriate kernel library function.

  33. Out with the old, in with the Nu2U • Intel and AMD introduced a specific instruction for achieving this same sort of system call table capability for kernels, but doing it more efficiently. • The instructions for doing this are syscall/sysret on AMD and sysenter/sysexit on Intel. • Linux used int 0x80 <= 2.4, and sys* >= 2.5, Windows used int 0x2e <= Win2k, sys* >= XP

  34. MiSeRly MiSeRy MiSanthRopy • The syscall/sysenter instructions basically just jump to a predefined location in the kernel ala an interrupt. That location is predefined by using a "Model Specific Register" (MSR) • MSRs are special registers which exist on specific models and have specific purposes (not a "general purpose" register like eax, ebx, etc.) • You read and write MSRs with "rdmsr" (read msr) and "wrmsr“ (write MSR) • IA32_SYSENTER_EIP = 0x176

  35. MiSeRly MiSeRy MiSanthRopy • IA32_SYSENTER_EIP = 0x176 • Reading from the MSR • mov ecx, 0x176 • rdmsr • (eax now contains value that was in the MSR) • Writing a MSR • mov eax, 0xdeadbeef • mov ecx, 0x176 • wrmsr • (IA32_SYSENTER_EIP now holds the value 0xdeadbeef)

  36. More about system calls • For more into on intvs sys*, as well as how interrupts work and worked on Windows: • How Do Windows NT System Calls REALLY Work? - http://www.codeguru.com/Cpp/W-P/system/devicedriverdevelopment/article.php/c8035/ • System Call Optimization with the SYSENTER Instruction - http://www.codeguru.com/cpp/w-p/system/devicedriverdevelopment/print.php/c8223 • It would have made a whole lot more sense if you had already taken Intermediate x86 :-/

  37. NooTooYoo - SSDT • WinDbg command to print tables: • !for_each_thread ".echo Thread: @#Thread; dt nt!_kthread ServiceTable @#Thread" • (from http://www.securabit.com/wp-content/uploads/2010/03/Rootkit-Analysis-Hiding-SSDT-Hooks1.pdf)

  38. SSDT Hook From: http://www.blackhat.com/presentations/bh-europe-06/bh-eu-06-Silberman-Butler.pdf

  39. SSDT Hook hehehe hehehe hehehe hehehe From: http://www.blackhat.com/presentations/bh-europe-06/bh-eu-06-Silberman-Butler.pdf

  40. SSDT False Positives(go look at the overall SSDT results again at this point) How you could determine these are due to symantecis out of scope for this class

More Related