Skip to main content
Email Password   helpLost your password?

Introduction

API calls interception is the task that allows to get access to some parts of other's programs. Lots of programmers spend time on developing and describing various methods which allow that access. Such methods are used in many anti-viruses and anti-spyware. Besides, sometimes, intercepting can help you to find errors in your application. However, it is not a secret that some viruses use it too. I spent much time to find and understand the technique of interception. I would like to describe here the results of my research.

Method description

First of all, you need to read the following article to understand the basics of the interception mechanism: HookSys (written by Ivo Ivanov). It was very helpful for me, and I used the sample code from it. However, it does not solve all my problems because Ivo's samples sometimes miss very important API calls. It happens when the application starts up too fast and the intercepting service has no time to inject the DLL. After some research, I found the actual problem, and it was related to using the kernel mode function, SetCreateProcessNotificationRoutine. This function is used to receive notification events about any new process creation. Such a notification is often fired when the process has already been started. Therefore, I needed to find a way to improve Ivo's code.

As far as I know, the execution of all Windows processes consists of the following steps:

The step right before the main thread resuming looks like the most comfortable for injection because the process is in suspended state and none of its instructions have been executed yet.

Most of the work on the process creation is done in the kernel mode, so to change this algorithm, you need to intercept the kernel mode functions NtCreateProcess() and NtCreateThread(). The CONTEXT structure, the pointer to which is passed to the function NtCreateThread(), contains a member called EAX. I found that it equals to the process' start address in user mode, so if you can change it, then you can get the control right after process creation and before starting. To solve this task, I wrote a kernel mode driver. It starts while the system starts up.

There are some initialization steps:

  1. starting;
  2. receiving configuration from the user mode;
  3. intercepting kernel mode functions such as: NtCreateProcess(), NtCreateThread(), NtTerminateProcess(), NewNtCreateProcessEx() - for Windows 2003 Server.

A handler to the NtCreateThread() function contains code that will do most of the interesting jobs. Here is a brief description of its algorithm:

  1. allow access to the creating process by calling ObReferenceObjectByHandle();
  2. remember the main thread start address (ThreadContext->EAX);
  3. "jump" to the context of the creating process by calling KeAttachProcess();
  4. allocate memory for my code by calling ZwAllocateVirtualMemory(), similar to the well known technique for CreateRemoteThread() in user mode;
  5. copy the small code to the allocated memory that will load my DLL. This code looks like:
    push pszDllName
    mov  ebx, LoadLibraryAddr
    call [ebx]
    mov  eax, Win32StartAddr
    push eax
    ret
    pszDllName: db 'example.dll';
  6. "jump" to the initial process;
  7. change the thread start address (ThreadContext->EAX) so it will point to the allocated memory.

That is all. You can download and compile the complete source code for this article. Note: the sample is fully functional and quite enough for basic understanding, but for real usage, it might be rewritten.

Compiling the code

You need the NTDDK to be installed on your computer. I'm using MSVS 6.0 for compiling NtProcDrv, and MSVS 7.1 for the rest of the projects.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralDoesn`t work Pin
Cold-Fire
11:16 16 Aug '06  
GeneralRe: Doesn`t work Pin
Andriy Oriekhov
4:44 22 Aug '06  
QuestionNow it works but... Pin
Cold-Fire
4:40 23 Aug '06  
AnswerRe: Now it works but... Pin
Andriy Oriekhov
13:01 23 Aug '06  
GeneralRe: Now it works but... Pin
Cold-Fire
2:49 24 Aug '06  
QuestionWhy the "ParentProcess" is 0xFFFFFFFF? Pin
Fu Hao
21:33 5 Jul '06  
GeneralHow about non x86 ? Pin
leandrobecker
13:31 5 Jun '06  
GeneralRe: How about non x86 ? Pin
Andriy Oriekhov
1:05 7 Jun '06  
GeneralReboot computer Pin
nhthiemsvt
22:58 4 May '06  
GeneralRe: Reboot computer Pin
Andriy Oriekhov
0:55 26 May '06  
Generalnon-admin Pin
ep
22:21 19 Mar '06  
GeneralRe: non-admin Pin
Andriy Oriekhov
7:36 20 Mar '06  
GeneralRe: non-admin Pin
ricardo fernandes
14:28 11 May '06  
GeneralRe: non-admin Pin
Andriy Oriekhov
1:01 26 May '06  
GeneralRe: non-admin Pin
girm
8:46 28 Jun '06  
GeneralThumbs up Pin
Ivo Ivanov
9:38 15 Mar '06  
GeneralAwesome! Pin
yafan
15:44 6 Mar '06  


Last Updated 31 May 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009