hunting rootkits with windbg

41
Hunting rootkits with Windbg v1.1 Windbg v1.1 Frank Boldewin Frank Boldewin March 2011

Upload: frank-boldewin

Post on 20-May-2015

858 views

Category:

Technology


2 download

DESCRIPTION

In these slides i will introduce several ways to find well known rootkits like Rustock or TDL Versions 3+4 with Windbg and scripts.

TRANSCRIPT

Page 1: Hunting rootkits with windbg

Hunting rootkits with Windbg v1.1Windbg v1.1

Frank BoldewinFrank BoldewinMarch 2011

Page 2: Hunting rootkits with windbg

Scope of this Talk

In the next few slides the audience learns how to hunt for rootkits with WindbggTo get a good overview of the different ways how rootkits hide itself from being recognized several techniques from rootkits like Runtime2, Rustock.B, Alipop, Stuxnet as well as TDL3 and TDL4 are introducedwell as TDL3 and TDL4 are introducedOf course the techniques used to detect a special rootkit are not limited to the shownspecial rootkit are not limited to the shown cases. ;-)Prerequisites are a good understanding

2

q g gabout Windows internals and basic Windbg skills

Page 3: Hunting rootkits with windbg

Finding SSDT hooks

The SSDT is a data array in kernel memory, that stores pointers to the native API pfunctions of Windows, e.g. NtCreateFileThese functions are handled in NTOSKRNLOlder rootkits used to hook some distinctive functions to hide its files or registry entries

h i d f dwhen queried from usermodeAlmost every run-of-the-mill antirootkit tool is able to detect such hooks todayis able to detect such hooks today

3

Page 4: Hunting rootkits with windbg

Finding SSDT hooks

Viewing the SSDT manually

4

Page 5: Hunting rootkits with windbg

Finding Shadow SSDT hooks

The Shadow SSDT is another array and stores pointers to functions in the Win32k.sysp yTo view its entries we first have to switch to a GUI process context and reload the symbols for the specific module

!process 0 0 winlogon.exe!process 0 0 winlogon.exePROCESS 81ebf6f8 SessionId: .....

.process /p 81ebf6f8reload.reload

5

Page 6: Hunting rootkits with windbg

Finding Shadow SSDT hooks

6

Page 7: Hunting rootkits with windbg

Finding Shadow SSDT hooks

fTo find SSDT and Shadow SSDT hooks automatically we can use a Windbg script from Lionel d'Hauenens of Laboskopia

7

Page 8: Hunting rootkits with windbg

Runtime2 Rootkit – Finding SSDT/Shadow SSDT hooks with a Windbg script

8

Page 9: Hunting rootkits with windbg

Rustock.B Rootkit – SYSENTER_EIP hook

The SYSENTER_EIP (MSR 0x176) usually points toKiFastCallEntry to serve requests from the usermodeto access native functions in the SSDTto access native functions in the SSDTThis pointer gets hooked by the Rustock.B rootkitIf Sysenter gets called Rustock checks in its own SDTIf Sysenter gets called Rustock checks in its own SDT table if a function is hooked or not. Non hookednative functions have a null pointer. Hookedfunctions have a pointer to its own handlerfunctions have a pointer to its own handler.To avoid easy hook detections the Sysenter_EIPaddress points to the same module (NTOSKRNL.EXE)

KiF tC llE tas KiFastCallEntry.It overwrites a textstring „FATAL_UNHANDLED_HARD_ERROR“ with a 5 bytes jump to its real rootkit code.

9

y j p

Page 10: Hunting rootkits with windbg

Rustock.B Rootkit – SYSENTER_EIP hook

10

Page 11: Hunting rootkits with windbg

Rustock.B Rootkit – SYSENTER_EIP hook

Another Laboskopia Windbg command shows us thehook automatically

11

Page 12: Hunting rootkits with windbg

Rustock.B Rootkit – Finding hidden registry entries

To find the hidden registry entries Rustock uses tosurvive a reboot, we walk the windows hive with the!reg“ command and its parameters„!reg“ command and its parameters

A hive is a logical group of keys, subkeys, and values in the registry that has a set of supporting files + g gbackup copiesHives are stored as files on diskNe t to standa d hi es e e se has his o n hi esNext to standard hives every user has his own hives file

12

Page 13: Hunting rootkits with windbg

Rustock.B Rootkit – Finding hidden registry entries

Table of standard hives and their supporting files

Registry hive Supporting filesHKEY_CURRENT_CONFIG System, System.alt, System.log, System.sav_ _ y , y , y g, y

HKEY_CURRENT_USER Ntuser.dat, Ntuser.dat.log

HKEY_LOCAL_MACHINE\SAM Sam, Sam.log, Sam.sav

HKEY_LOCAL_MACHINE\Security Security, Security.log, Security.sav

HKEY_LOCAL_MACHINE\Software Software, Software.log, Software.sav

HKEY_LOCAL_MACHINE\System System, System.alt, System.log, System.sav

HKEY_USERS\.DEFAULT Default, Default.log, Default.sav

13

Page 14: Hunting rootkits with windbg

Rustock.B Rootkit – Finding hidden registry entries

14

Page 15: Hunting rootkits with windbg

Rustock.B Rootkit – Finding hidden registry entries

15

Page 16: Hunting rootkits with windbg

Rustock.B Rootkit – Finding the Hidden Registry Entry

16

Page 17: Hunting rootkits with windbg

Rustock.B Rootkit – pIofCallDriver Hook

Hooks at pIofCallDriver are often used to filter special IRP requests to driversRustock filters any attempt to directly communicatewith NTFS.SYS or FASTFAT.SYS. These files arehidden, can‘t be copied, nor overwritten or renamed

17

Page 18: Hunting rootkits with windbg

Rustock.B Rootkit – IDT hooks

The Interrupt Descriptor Table (IDT) is a structure which is used when dispatching interruptsInterrupts can interrupt an execution of a program to to handle an eventInterrupts could be a result of a hardware signal orInterrupts could be a result of a hardware signal or software based using the INT instructionThe IDT descriptor table can handle 256 entriesThe descriptor to the table can be written with the instruction LIDT and read with SIDTRustock hooks INT 2Eh, which is usually pointing toRustock hooks INT 2Eh, which is usually pointing to KiSystemService, a Zw* functions dispatcher and handler for usermode INT 2Eh calls on old hardware not supporting fastcalls via the SYSENTER command

18

not supporting fastcalls via the SYSENTER command

Page 19: Hunting rootkits with windbg

Rustock.B Rootkit – SYSENTER_EIP hook

Rustock hooks INT 2Eh to communicate betweenRustock hooks INT 2Eh to communicate betweenusermode and kernelmode componentsThe „IDT“ command shows us the pointer to thehandler. KiSystemService is ok, otherwise it‘s hooked

19

Page 20: Hunting rootkits with windbg

Alipop Rootkit – GDT Callgate

A callgate is a mechanism in Intel x86 arch to change privilege level of the CPUThe Alipop rootkit installs such a callgate to execute code with the highest privilege (Ring 0) from usermode (Ring 3) without the need to have a driver, ge.g. by calling DeviceIOControlCallgate usage works by executing “call far ptr <addr>” from usermode codefrom usermode codeInstallation of the callgate is done by the bootkit part of AlipopOther malware seen in the wild used \Device\PhysicalMemory to install a callgate in the GDT (works only on older windows versions)

20

( y )

Page 21: Hunting rootkits with windbg

ALIPOP Rootkit – GDT Callgate

21

Page 22: Hunting rootkits with windbg

ALIPOP Rootkit – GDT Callgate

22

Page 23: Hunting rootkits with windbg

TDL3 Rootkit – ATAPI IRP hooks

The TDL3 rootkit usually infects the ATAPI driverThe TDL3 rootkit usually infects the ATAPI driverwith a small loader for the real rootkit code in the PE resource area of atapi.sys and changes theentrypoint to its loader codeentrypoint to its loader codeThe real rootkit part is being stored encrypted on disk sectorsThe loader uses low level disk operations to read thesectors, decrypts the mini TDL file system and startsthe real rootkit codethe real rootkit codeTo hide and protect its sectors TDL3 uses IRP hooking in ATAPI.SYS

23

Page 24: Hunting rootkits with windbg

TDL3 Rootkit – ATAPI IRP hooks

24

Page 25: Hunting rootkits with windbg

TDL3 Rootkit – ATAPI IRP hooks

25

Page 26: Hunting rootkits with windbg

TDL3 Rootkit – Shared Memory structure (Kernel-/User mode)

To share information with its usermode componentsTo share information with its usermode componentsTDL3 uses the structure KUSER_SHARED_DATAThis structure is accessable from kernel at address0xFFDF0000 and is mapped to userspace at0x7FFE0000Kernel mode has read/write access to this structure,Kernel mode has read/write access to this structure, usermode has only read accessAt KUSER_SHARED_DATA+0308h (SystemCallPad) TDL3 t i t t t tTDL3 stores a pointer to an own structureThis structure stores a bunch of things likekernelbase, original ATAPI IRPs, TDL3 FS start, path, g , , pto its config file …

26

Page 27: Hunting rootkits with windbg

TDL3 Rootkit – Shared Memory structure (Kernel-/User mode)

27

Page 28: Hunting rootkits with windbg

TDL3 Rootkit – Shared Memory structure (Kernel-/User mode)

28

Page 29: Hunting rootkits with windbg

TDL3 Rootkit – TDL mini FS (file system)

29

Page 30: Hunting rootkits with windbg

TDL3 Rootkit – Traces in the system worker threads

Drivers requiring delayed processing usually use a work item, using IoQueueWorkItem with a pointer toits callback routineits callback routineWhen a system worker thread processes the queueditem it gets removed and the callback gets invokedg gSystem worker threads run in the system processcontext (PID 4)TDL3 ootkit is sing o k items as ellTDL3 rootkit is using work items as wellWhenever work items have been processed or othersystem threads have been created this leaves tracesyon the callstackAs TDL3 does not belong to any known module, the process thread view informs us about this problem

30

process thread view informs us about this problem

Page 31: Hunting rootkits with windbg

TDL3 Rootkit – Traces in the system worker threads

31

Page 32: Hunting rootkits with windbg

TDL4 Rootkit – Finding TDL4 with its invalid device object

32

Page 33: Hunting rootkits with windbg

TDL4 Rootkit – ATAPI DriverStartIO hook

TDL4 rootkit hooks the ATAPI driver as well, but in a lower level way than its precedessorAs more and more tools were easily able to dump its files even from usermode via IOCTL_SCSI_PASS_THROUGH_DIRECT calls directly to the port device, TDL4 changed the hook method to DriverStartIOFor standard windows miniport drivers like atapi sysFor standard windows miniport drivers like atapi.sys, any SCSI request dispatching is always reduced toDriverStartIOThi k it l t h d t d th TDL4 filThis makes it a lot harder to dump the TDL4 files

33

Page 34: Hunting rootkits with windbg

TDL4 Rootkit – ATAPI DriverStartIO hook

34

Page 35: Hunting rootkits with windbg

TDL4 Rootkit – Finding the Kernel Callback with a Windbg script

Rootkits often use kernelcallbacks to get notifiedwhen files are loaded, processes or threads arecreated as well as Registry events occurcreated as well as Registry events occur.TDL4 installs a kernelcallback to inject its usermodepayload in distinctive windows processes

35

Page 36: Hunting rootkits with windbg

TDL4 Rootkit – Dropper dumping after TDL4 infection (before reboot)

36

Page 37: Hunting rootkits with windbg

TDL4 Rootkit – Dumping injected user mode payload

37

Page 38: Hunting rootkits with windbg

TDL4 Rootkit – Finding inline hooks in user mode payload

38

Page 39: Hunting rootkits with windbg

Stuxnet Rootkit – IoFsRegistrationChange

Stuxnet mrxnet.sys driver adds a new device objectand attaches to the device chain with the objecttype\FileSystem (default fastfat ntfs cdfs)\FileSystem (default fastfat, ntfs, cdfs)This makes it possible to control and intercept IRP requestsA filesystem registration callback makes it possible toattach the device chain of newly created filesystems

39

Page 40: Hunting rootkits with windbg

Stuxnet Rootkit – IoFsRegistrationChange

40

Page 41: Hunting rootkits with windbg

Questions?

Thanks for good discussions and ideas

Michael Hale LighEP_X0FF

Cr4shMatthieu SuicheMatthieu Suiche

41