Hello again.. There was a long time since my last post, cos i was busy with several issues, but most time with this tool.. Checking it’s operation, testing the hooked PE in different OSs, changing the method used, etc..

Finally i came up with this tool, and below i will explain exactly how it works, give you the source code and a binary, and show you some applications of this tool with video and/or shots…

This is an API hooking tool, which uses the PE IAT patch method, and runs the payload, injecting the code in the PE permanently, changing the PE Header apropriately (section sizes, OEP, ..) The final executable produced (hooked, and code-injected) tested on most OSs:

Windows 7 x64 – SUCCEED

Windows 7 x32 – SUCCEED

Windows 2008 x32 – SUCCEED

Windows 2000 – FAILED (The VEH APIs not supported by OSs <= Win2k)

Windows 2003 x32 – SUCCEED

Windows XP x32 – SUCCEED

MAC OS running wine – SUCCEED

Here is the tool! CPP Source with injected asm (i could say ASM with injected cpp 😛 ) and the compiled .exe .. if you don’t trust me to run binary 😛 just cl hooking.cpp /w and you got it 🙂

And ofcourse for the IAT and EAT of the PE Structure, I visited IcZelion’s guides (IAT, EAT) and used those methods in this tool.. Also I use the PEB method to obtain the Kernel Base in runtime on standalone codes, method used by most shellcodes.

Why whould you need an API Hooker ?? There are several reasons.. like :

Backdoor files

Change the API parameters on runtime

Spy API execution on specific process

etc

The method used from this tool, is physical modifying the Import Table of the PE, and patching/injection the user’s code so it is executed right BEFORE the API execution begins.. The tool just asks you which API you want to hook, and which is the file with the raw data (asm binary code) that will be inserted before the api execution.. The code that will be inserted must, for sure, be absolutely stand alone, what means that it must keep stack calibrated, must not refer to memory location out of the code (except from locals made in stack or imported functions from PEB) .. You have to know what you’re doing 🙂

BUT.. there is also another option of the tool. You can use the Exception Handling option.. which enables you to use code that may cause exceptions, code that does not manage it’s stack well, and maybe some 3rd party code.. This option, uses VEH (Vectored exception handling), installing the handler before your code execution, and removing the handler after the execution of your code, or after your code causes an exception.. Also the stack is being saved, so you dont have to care about the EBP and ESP values 🙂 The whole consept will be explained.

Here are two very nice and easy to understand API Hooking papers by Brian Mariani from High-Tech Bridge SA, published in exploit-db :

I will try to explain some basic things about how my tool is working. I hope you understand.. You can make your comments ofcourse! 🙂 Here are some Images about the operation.. :

Basic Hooking tool explanation

If you select not to use VEH, and you make a standalone payload with calibrated stack, then the implementation is very simple, as explained in the previous image.. When you select to use VEH, your payload will be shielded by an exception handler, and the injected code will be much more complicated, and the structure of the injected code for VEH and when your payload will be executed, can be cleared by the following image!

The VEH Code structure:

Vectored Exception Handling Additional Code Structure

Now in order to understand the tool in practice, i made some videos doing some simple but practical examples.. I’ll enumerate them here :

1. Hooking the “About” window of notepad.exe with the following payloads:

3. Notepad.exe : ShellAboutW : Windows Meterpreter reverse : Doesn’t work with any exitfunc (it destroys the process) so I use Custom ASM Code creating Thread (like the 1.c. step with Reverse Shell).. You can use this example code to run payloads and backdoors in a new thread.. So you’ll wonder why you didn’t group it in the 1st section with all other payloads? I didn’t cos it needed some more time.. Read more about this below..

<>================[ Videos and Descriptions.. ]==================<>

1) a. The notepad.exe hooked on ShellAboutW with a WinExec payload to run calc with EXITFUNC=none with no VEH
In this video you’ll see:

Compilation of hooking.cpp

Use of hooking.exe to show some imports

Creation of payload for winexec “calc” and exitfunc=none

Use of that payload with hooking.exe to hook notepad

and in this video I used VEH to handle the exception caused by the payload.. Also i play around with some other exitfuncs and veh/without veh, without specific reason.. only VEH makes the difference… :

1) b. In the next vid I show the same logic as previously.. I use VEH cos WinExec payload after execution, tries to execute the text area (the command text) and provokes exceptions.. The interesting part here is the command that adds an administrator user (user “adm”), disables firewall, and enables Remote Desktop silently by just looking the about of notepad..

..ofc the user who will execute this command/payload must have admin privs… ;p

But there is a problem running this payload… is not exactly “silent” … I also show how you can make it silent by changing the payload manually 😉

1) c. Windows Reverse Shell :

Now things are being complicated.. We’ll talk for the reverse shell (more practical for remote scenarios via internet & nat). If we use the tool as is, the about window will not appear immediately. The program will try to make a connection to the IP hardcoded in the RShell payload.. If it will connect, it will give the shell and About dialog will appear when the reverse tcp connection will end.. But if the IP and port that it wants to connect is invalid, the About dialog will appear after the TIMEOUT of the connection. We don’t like such backdoor cos it must be executed silently. To do it, we have to make it in a seperate thread.. Below I show the 1st method (without a thread) and the 2nd too (with thread) and how to implement such code… Using the code below, you execute any payload in a seperate thread, and it’s absolutely stand alone (full kernelbase finding+exports finder included in the code).. Here is the video, and after the video i have the code used (raw data), so you can copy/paste it in your binary.

Raw-code used for the Thread implementation of the video… :

>> Header of the code:

E8 00 00 00 00 59 83 C1 0C 81 C1 EF BE AD DE FF E1

size: 3A 01 00 00 (change EF BE AD DE to the size of payload in little endian) … [PAYLOAD TO BE RUN IN THREAD] …

3. Meterpreter Reverse in a Thread (notepad.exe hooked on ShellAboutW)

In this video i show how to hook again ShellAboutW with meterpreter payload in a thread (using the code from 1.c. ) ..But here things are different. Meterpreter does not handle the “EXITFUNC” and it always calls “ExitProcess” as a result the Termination of whole application (and not only of a thread as we expected). Read the here details about that issue.. So when meterpreter doesn’t find the IP to connect to, or the port is refusing connection, it kills the whole process after 5 retries…

In the video below, i hook in notepad.exe a meterpreter payload with thread code, watching that it kills the whole process, and then patching the payload in order to a) make more tries to connect b) call RtlExitUserThread instead of ExitProcess..

If you have questions about the code i use, read the descriptions of 2 videos before.. 🙂