Tutorial 29: Win32 Debug API PART 2
.
THEORY:
In the previous tutorial, we know how to load the debuggee and handle debug events that occur in its process. In order to be useful, our program must be able to modify the debuggee process. There are several APIs just for this purpose.
ReadProcessMemory This function allows you to read memory in the specified process The function prototype is as follows:. ReadProcessMemory proto hProcess: DWORD, lpBaseAddress: DWORD, lpBuffer: DWORD, nSize: DWORD, lpNumberOfBytesRead: DWORD hProcess is the handle to the process you want to read.lpBaseAddress is the address in the target process you want to start reading. For example, if you want to read 4 bytes from the debuggee process starting at 401000h, the value in this parameter must be 401000h.lpBuffer is the address of the buffer to receive the bytes read from the process. nSize is the number of bytes you want to readlpNumberOfBytesRead is the address of the variable of dword size that receives the number of bytes actually read. If you do not care about it, you can use NULL. WriteProcessMemory is the counterpart of ReadProcessMemory. It enables you to write memory of the target process. Its parameters are exactly the same as those of ReadProcessMemory The next two API functions nee da little background on context. Under a multitasking OS like Windows, there can be several programs running at the same time. Windows gives each thread a timeslice. When that timeslice expires, Windows freezes the present thread and switches to the next thread that has the highest priority. Just before switching to the other thread, Windows saves values in registers of the present thread so that when the time comes to resume the thread, Windows can restore the last * environment * of that thread. The saved values of the registers are Collectively Called A Context. Back to Our Subject. When a debug event occurs, Windows Suspends the debuggee. The debuggee '
s context is saved. Since the debuggee is suspended, we can be sure that the values in the context will remain unchanged. We can get the values in the context with GetThreadContext and we can change them with SetThreadContext.These two APIs are very powerful. With them, you have at your fingertips the VxD-like power over the debuggee: you can alter the saved register values and just before the debuggee resumes execution, the values in the context will be written back into the registers Any change you made to. . the context is reflected back to the debuggee Think about it: you can even alter the value of the eip register and divert the flow of execution to anywhere you like you will not be able to do that under normal circumstance GetThreadContext proto hThread!. : DWORD, lpContext: DWORD hThread is the handle to the thread that you want to obtain the context fromlpContext is the address of the cONTEXT structure that will be filled when the function returns successfully SetThreadContext has. EXACTLY THE What A Context Structure Looks Like: Context Struct ContextFlags Dd?; ---------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- --------------------------; this section upgrans the value confext_debug_registers; ------------ -------------------------------------------------- -------------------------------------------- IdR0 DD? IDR1 DD ? idr2 DD? idr3 DD? idR6 DD? idR7 DD?; -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- --------------------; this section section keturnediff;
-------------------------------------------------- -------------------------------------------------- ------- Floatsave floating_save_area <>; -------------------------------------- -------------------------------------------------- ------------------; this section next is returned if content_segments; --------------------------------------------------------------------------------------------- -------------------------------------------------- ------------------------------------- Reggs DD? regfs dd?; -------------------------------------------------- -------------------------------------------------- ------; this section upgrans the value context_integer; ------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- ------------------------- Regedi DD? Regesi DD? regebx dd? regex dd?; -------- -------------------------------------------------- -------------------------------------------------- This section is returned if contextflags co NTAINS The Value Context_Control; --------------------------------------------- -------------------------------------------------- ------------ Regebp DD? Regflag DD? Regesp Dd? Regss dd?; ------------------- -------------------------------------------------- -------------------------------------; this section upgrans the value limited_extended_registers; -------------------------------------------------- -------------------------------------------------- ------ Extendedregisters DB Maximum_supported_extension dup (?) Context ends as you can Observe, The Members of this Structures Are Mimics of The Real Processor '
s registers. Before you can use this structure, you need to specify which groups of registers you want to read / write in ContextFlags member. For example, if you want to read / write all registers, you must specify CONTEXT_FULL in ContextFlags. If you want only to read / write regEbp, regEip, regCs, regFlag, regEsp or regSs, you must specify CONTEXT_CONTROL in ContextFlags One thing you must remember when using the CONTEXT structure:. it must be aligned on dword boundary else you'd get strange results Under Nt. You Must Above The Line That Declares It, Like this: Align DWORDMYCONTEXT CONTEXT <> EXAMPLE:
The first example demonstrates the use of DebugActiveProcess. First, you need to run a target named win.exe which goes in an infinite loop just before the window is shown on the screen. Then you run the example, it will attach itself to win. Exe and modify the code of win.exe such That Win.exe EXITS The Infinite loop and shows its ingwindow.
.386 .model flat, stdcall option casemap: none include /masm32/include/windows.inc include /masm32/include/kernel32.inc include /masm32/include/comdlg32.inc include /masm32/include/user32.inc includelib / masm32 /lib/kernel32.lib includelib /masm32/lib/comdlg32.lib includelib /masm32/lib/user32.lib .data AppName db "Win32 Debug Example no.2", 0 ClassName db "SimpleWinClass", 0 SearchFail db "Can not find !??? the target process ", 0 TargetPatched db" target patched ", 0 buffer dw 9090h.data DBEvent DEBUG_EVENT <> ProcessId dd ThreadId dd align dword context cONTEXT <> .code start: invoke FindWindow, addr ClassName, NULL .if eax! = NULL invoke GetWindowThreadProcessId, eax, addr ProcessId mov ThreadId, eax invoke DebugActiveProcess, ProcessId .while TRUE invoke WaitForDebugEvent, addr DBEvent, INFINITE .break .if DBEvent.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT .if DBEvent.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT mov context. Contextflags, Context _CONTROL invoke GetThreadContext, DBEvent.u.CreateProcessInfo.hThread, addr context invoke WriteProcessMemory, DBEvent.u.CreateProcessInfo.hProcess, context.regEip, addr buffer, 2, NULL invoke MessageBox, 0, addr TargetPatched, addr AppName, MB_OK MB_ICONINFORMATION. Elseif dbevent.dwdebugeventcode == exception_debug_event .IF dbeivent.u.exception.pexceptionRecord.exceptioncode ==
EXCEPTION_BREAKPOINT invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_CONTINUE .continue .endif .endif invoke ContinueDebugEvent, DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_EXCEPTION_NOT_HANDLED .endw .else invoke MessageBox, 0, addr SearchFail, addr AppName, MB_OK MB_ICONERROR. Endif Invoke EXITPROCESS, 0 End Start; ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------; The Partial Source Code of Win.ASM, Our Debuggee. It's Actually; The Simple Window Example In Tutorial 2 with An infinite loop insert; Just Before it enters the message loop.; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------------------------------
...... mov wc.hIconSm, eax invoke LoadCursor, NULL, IDC_ARROW mov wc.hCursor, eax invoke RegisterClassEx, addr wc INVOKE CreateWindowEx, NULL, ADDR ClassName, ADDR AppName, / WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, / CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, / hInst, NULL mov hwnd, eax jmp $ <---- Here's our infinite loop. It assembles to EB FEinvoke ShowWindow, hwnd, SW_SHOWNORMAL invoke UpdateWindow, hwnd .while TRUE invoke GetMessage, ADDR msg, NULL , 0,0 .break .if (! EAX) Invoke TranslateMessage, Addr Msg .Endw Mov Eax, Msg.wParam Ret Winmain Endp
Analysis:
Invoke Findwindow, AddR ClassName, Null
Our program needs to attach itself to the debuggee with DebugActiveProcess which requires the process Id of the debuggee. We can obtain the process Id by calling GetWindowThreadProcessId which in turn needs the window handle as its parameter. So we need to obtain the window handle first. With FindWindow, we can specify the name of the window class we need. It returns the handle to the window created by that window class. If it returns NULL, no window of that class is present..if eax! = NULL invoke GetWindowThreadProcessId, Eax, Addr Processid Mov Threadid, Eax Invoke DebugActiveProcess, Processid
After We Obtain The Process ID, We can Call DebugActiveProcess. The Debug Loop Waiting for the Debug Events.
.IF dbEvent.dwdebugeventcode == create_process_debug_event mov context.contextFlags, Context_Control Invoke GetthreadContext, Dbevent.u.createProcessInfo.hthread, Addr Context
When we get CREATE_PROCESS_DEBUG_INFO, it means the debuggee is suspended, ready for us to do surgery upon its process. In this example, we will overwrite the infinite loop instruction in the debuggee (0EBh 0FEh) with NOPs (90h 90h). First, we need to obtain the address of the instruction. Since the debuggee is already in the loop by the time our program attached to it, eip will always point to the instruction. All we need to do is obtain the value of eip. We use GetThreadContext to Achieve That Goal. We set the contextflags member to context_control so as to Tell GetthreadContext That We want it to fill the "Control" Register Members of The Context Structure.
Invoke WriteProcessMemory, Dbevent.u.createProcessInfo.hprocess, Context.Regeip, Addr Buffer, 2, Null
Now that we get the value of eip, we can call WriteProcessMemory to overwrite the "jmp $" instruction with NOPs, thus effectively help the debuggee exit the infinite loop. After that we display the message to the user and then call ContinueDebugEvent to resume the debuggee. Since the "jmp $" instruction is overwritten by NOPs, the debuggee will be able to continue with showing its window and enter the message loop. The evidence is we will see its window on screen.The other example uses a slightly different approach To Break the debuggee out of the infinite loop.
............... if DBEvent.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT mov context.ContextFlags, CONTEXT_CONTROL invoke GetThreadContext, DBEvent.u.CreateProcessInfo.hThread, addr context add context.regEip, 2 invoke SetThreadContext, Dbevent.u.createProcessInfo.hthread, Addr Context Invoke Messagebox, 0, Addr Loopskipped, Addr Appname, MB_OK MB_ICONInformation ............
It still calls GetThreadContext to obtain the current value of eip but instead of overwriting the "jmp $" instruction, it increments the value of regEip by 2 to "skip over" the instruction. The result is that when the debuggee regains control, it resumes Execution At the next instructionAfter "JMP $".
Now you can see the power of Get / SetThreadContext. You can also modify the other register images as well and their values will be reflected back to the debuggee. You can even insert int 3h instruction to put breakpoints in the debuggee process.