13.07.2015 Views

Kernel-mode exploits primer - International Secure System Lab

Kernel-mode exploits primer - International Secure System Lab

Kernel-mode exploits primer - International Secure System Lab

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

y <strong>System</strong> call hooks. Basically, the idea is to replacethe pointer to the system call dispatcher routine with apointer to the payload. Obviously, the payload must becopied to a readily accessible and unused location forthis to work.• The next scenario discussed by bugcheck and skape involvesWindows’ thread notify routine. Analogouslyto the system call hooks described above, for this strategyto work the payload must be copied to a suitablememory location, where it will be available when thenotify is called. Depending on the Windows versionthe process of registering a thread notify routine differs(see [2] for details), but in essence a callback structureor a simple pointer to the payload must be installed, sothat the payload will be called every time a thread iscreated or deleted.• In [5] Conover suggests yet another similar technique:hooking object type initializer procedures. The ideahere is to replace callbacks in the object manager fora certain object type. That way, the payload will becalled for example every time an object of the respectivetype is created.During the migration phase of the exploit, we alreadymentioned that it may be necessary to copy a part of thepayload to an easily accessible memory location to be executedlater in a safer context. The component of the exploitthat is responsible for this process is also known as thestager. Typically the code to be copied by the stager willcome piggy-backed with the stager itself, but it may as wellhave been planted in advance or be otherwise present somewherein the address space already. Usually, a stager will trystaging a payload from kernel- to user-space, i.e. in order toallow for well known payloads to be re-used in kernel-<strong>mode</strong>exploit scenarios. Not surprisingly, stagers come in differentflavors, for example:• The first approach works in combination with the systemcall hook migration technique described earlier,bugcheck and skape call this the system call returnaddress overwrite stager [2]. Again, the first step is tocopy the payload to be executed to a suitable memorylocation. In this case we are dealing with a user-spacepayload, so the location should be somewhere in theuser part of the virtual memory address space. Next,the hook for the system call instruction must be set.Now, when a system call is executed, the hooked routinealters the return address of the user-<strong>mode</strong> stack topoint to the payload. Also, the original return addressmust be saved somewhere in order to divert back tothe calling process after the payload has executed successfully.When this is done, the hooked routine callsthe system call as usual. When the call returns it willreturn via the payload back to the original process.• The next technique is mentioned in [2] and discussedat length in [8], Thread Asynchronous ProcedureCalls (APC). Once again, the payload must be storedsomewhere in userland. Then, a suitable thread withina user-<strong>mode</strong> process must be identified. For obviousreasons this should be a privileged process in mostcases. Furthermore, the thread in question must be inthe alertable state, in order to allow for an APC beingqueued to it. In a nutshell, the stager has initialize theAPC structure and point the APC routine to the payload’slocation. Finally, the APC must be added to thethread’s APC queue, and the payload will be executedin the context of the privileged user-<strong>mode</strong> process.• A user-<strong>mode</strong> function pointer hook is another optionfor a stager, if the exploit is already executed in thecontext of a process. Basically, the stager could redirectany function pointer of that process to the user<strong>mode</strong>payload. Obviously, this approach depends verymuch on the vulnerable process itself.• Very effective, from an attacker’s perspective, is theSharedUserData system call hook. The SharedUser-Data structure on Windows XP starting with SP0 2 containsfunction pointers to the system call dispatcherand return routines at a well known offset; these pointersare used by all system call stubs [2]. The stager itselfworks as follows: at first, as usual, the user-<strong>mode</strong>payload must be copied to an appropriate memory location;secondly, the function pointer to the system calldispatcher must be changed to the new location of thepayload; at the end of the payload a jump instructionto the real dispatcher routine must be added, in orderfor the system calls to still be processed. A big advantageof this staging technique is that it does not requireprior migration, because SharedUserData cannotbe paged out and can therefore be accessed regardlessof the IRQL [2].So far, we have frequently mentioned the payload thatwill ultimately be executed, so a few words on the topicseem in order. Obviously, the whole point of the exploit isto execute this payload. After migration, the stager puts thepayload in place and ensures that it will be executed eitherin user- or kernel-<strong>mode</strong> in the desired context. As you willprobably have guessed by now, this payload is also calledthe stage component of the exploit. Usually, the stage will2 Actually, SP0 featured an executable SharedUserData which directlyexecuted the sysenter instruction. Since XP SP2 and 2003 SP1, however,this instruction set has been replaced by function pointers, because it isnow possible to mark SharedUserData non-executable in the PermissionTable [2].3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!