Description
===========
This is the main machinary of the whole process.
REQUIRED READING
================
Api
[---]
GetWindowThreadProcessId
VirtualProtect
GetCurrentProcessId
GetModuleHandle
SetWindowsHookEx
General
[-------]
DLL Creation.
LOADER INDEX JOB LIST
=====================
INDEX NUMBER (1) - LOAD ALL FUNCTIONS FROM D3D_DRAW DLL
INDEX NUMBER (2) - WORK OUT ADDRESS FOR EACH FUNCTION U WISH TO HOOK
INDEX NUMBER (3) - INJECT CODE TO HOOK FUNCTIONS
INDEX NUMBER (4) - YOUR FUNCTIONS WITHIN HOOK
INDEX NUMBER (5) - SETWINDOWSHOOKEX KEYBOARD HOOK CODE
INDEX NUMBER (6) - MAIN DLL INSTALLTION FUNCTION
HOOK SOURCE (ASM)
=================
.386 ; Usual setup.
.model flat,stdcall ;
option casemap:none ;
include d:\masm32\include\windows.inc ;
include d:\masm32\include\user32.inc ;
include d:\masm32\include\kernel32.inc ;
include d:\masm32\include\gdi32.inc ;
;
includelib d:\masm32\lib\user32.lib ;
includelib d:\masm32\lib\kernel32.lib ;
includelib d:\masm32\lib\gdi32.lib ;
.const
TRUE equ 1
FALSE equ 0
.data
menutoggle db 4 ; Toggle state of Menu ON/OFF
EndScene dd 0 ; Endscene raw address. (passed by loader)
Release dd 0 ; Release raw address. (passed by loader)
ProcId dd 0 ; ProcessId. (passed by loader)
hInst dd 0 ; Storage for dll instance.
hHook dd 0 ; hHook
oldprotection dd 0 ; Old PAGE protection stage.
d3d8 db "d3d8.dll",0 ; d3d8.dll
dllname db "dxdx.dll",0 ; Our own D3D_DRAW DLL for drawing the menu onscreen.
h_dll dd 0 ; h_dll
draw_func db "DrawDX",0 ; Functions exported by DXDX.DLL
init_func db "InitDX",0 ;
obdevice_func db "ObtainDevice",0 ;
EndMe_func db "EndMe",0 ;
draw_func_addy dd 0 ; Address of each exported function.
init_func_addy dd 0 ;
obdevice_func_addy dd 0 ;
EndMe_func_addy dd 0 ;
init_done db 0 ; BOOL to indicate state of progress.
d3d8base dd 0 ; Base address of d3d8.dll used to calculate hooked function addresses.
endscene_code_buffer db 30 dup (0) ; Buffer to store original endscene code.
release_code_buffer db 30 dup (0) ; Buffer to store original release code.
.code
MAIN proc hInstDLL:HINSTANCE, reason

WORD, reserved1

WORD
push edi ; Save important regs..
push esi ;
push hInstDLL ; Save dll hInstance
pop hInst ;
cmp reason, DLL_PROCESS_ATTACH ; Check to see if main is being run because of attachment..
jnz not_attached ; if no, skip hook installation..
invoke GetCurrentProcessId ; Get the process id of the current running process
cmp ProcId,eax ; compare it with the games procid (passed by loader)
jnz not_game_process ; if no, skip hook installation..
;INDEX NUMBER (1) - LOAD ALL FUNCTIONS FROM D3D_DRAW DLL START
invoke GetModuleHandle,offset d3d8 ; Get base address of d3d8.dll..
mov d3d8base,eax ; Save base addy.
invoke LoadLibraryA,offset dllname ; Load the D3D_DRAW dll into the game process.
mov h_dll,eax ; Save handle.
invoke GetProcAddress,eax,offset draw_func ; Work out and Save all exported functions
mov draw_func_addy,eax ; from the D3D_DRAW dll.
;
invoke GetProcAddress,h_dll,offset init_func ;
mov init_func_addy,eax ;
;
invoke GetProcAddress,h_dll,offset obdevice_func ;
mov obdevice_func_addy,eax ;
;
invoke GetProcAddress,h_dll,offset EndMe_func ;
mov EndMe_func_addy,eax ;
;INDEX NUMBER (1) - LOAD ALL FUNCTIONS FROM D3D_DRAW DLL END
;INDEX NU<BER (2) - WORK OUT ADDRESS FOR EACH FUNCTION U WISH TO HOOK START
mov ecx, EndScene ; Move offset of ENDSCENE() into ecx
add ecx,eax ; add base address of d3d8.dll to offset ENDSCENE()
mov EndScene, ecx ; save new REAL ENDSCENE ADDRESS..
mov ecx, Release ; Move offset of RELEASE() into ecx
add ecx,eax ; add base address of d3d8.dll to offset ENDSCENE()
mov Release, ecx ; save new REAL RELEASE ADDRESS..
;INDEX NU<BER (2) - WORK OUT ADDRESS FOR EACH FUNCTION U WISH TO HOOK END
;INDEX NUMBER (3) - INJECT CODE TO HOOK FUNCTIONS START
;-- copy first bytes of hooked functions into buffers..
;ENDSCENE..
mov ecx,5 ; Move 5 into ecx (amount to copy)
mov edi, offset endscene_code_buffer ; Buffer to store copied code into
mov esi, EndScene ; Location to copy from
mov edx,esi ; Save addy
add edx,5 ; Jmp position = endscene+5
rep movsb ; Do copy ..
;(calculates jmp back inside buffer)
mov byte ptr [edi],0e9h ; e9 = first opcode of a far jmp
sub edx,edi ; Work out other 4 op codes from calculation
sub edx,5 ; Sub length of actual instruction (5 opcodes e9,xx,xx,xx,xx)
inc edi ; Move 1 past 0e9h
mov dword ptr [edi],edx ; Complete jmp opcodes now will read (e9,xx,xx,xx,xx)
invoke VirtualProtect, EndScene, 5, PAGE_EXECUTE_READWRITE,offset oldprotection
;(calculates jmp back inside dll)
mov eax,EndScene ; Move REAL endscene address into eax
mov edi,eax ; Destination of hook..
mov ecx,offset EndSceneHook ; Move location to jmp to into ecx
sub ecx,eax ; Calculate last 4 opcodes for far jmp
mov byte ptr [edi],0e9h ; Insert first opcode for far jmp
sub ecx,5 ; Subtract instruction length
inc edi ; Move 1 past 0e9h
mov dword ptr [edi],ecx ; Complete far jmp instruction...
invoke VirtualProtect, EndScene, 5, oldprotection, offset oldprotection
;RELEASE
mov ecx,6 ; Move 6 into ecx (amount to copy)
mov edi, offset release_code_buffer ; Buffer to store copied code
mov esi, Release ; Location to copy from
mov edx,esi ; Save addy
add edx,5 ; Jmp back position = release+5
rep movsb ; Do copy ..
;(calculates jmp back inside buffer)
mov byte ptr [edi],0e9h ; e9 = first opcode of a far jmp
sub edx,edi ; Work out other 4 op codes from calculation
sub edx,5 ; Sub length of actual instruction (5 opcodes e9,xx,xx,xx,xx)
inc edi ; Move 1 past 0e9h
mov dword ptr [edi],edx ; Complete jmp opcodes now will read (e9,xx,xx,xx,xx)
;(calculates jmp back inside dll)
invoke VirtualProtect, Release, 5, PAGE_EXECUTE_READWRITE,offset oldprotection
mov eax,Release ; Move REAL release address into eax
mov edi,eax ; Destination of hook..
mov ecx,offset ReleaseHook ; Move location to jmp to into ecx
sub ecx,eax ; Calculate last 4 opcodes for far jmp
mov byte ptr [edi],0e9h ; Insert first opcode for far jmp
sub ecx,5 ; Subtract instruction length
inc edi ; Move 1 past 0e9h
mov dword ptr [edi],ecx ; Complete far jmp instruction...
add edi,4
mov byte ptr [edi],090h ; Because Release has 6 opcodes to copy not 5,
; we just nop the last opcode.
;restore old page protection..
invoke VirtualProtect, Release, 5, oldprotection, offset oldprotection
;INDEX NUMBER (3) -INJECT CODE TO HOOK FUNCTIONS END
not_attached:
not_game_process:
pop esi ; Restore regs
pop edi ;
mov eax,TRUE ; Return TRUE
ret
MAIN Endp
;INDEX NUMBER (4) - YOUR FUNCTIONS WITHIN HOOK START
;RELEASE HOOK FUNCTION CODE....
ReleaseHook:
pushfd ; Save Flagstatus.
pushad ; Save Regstatus.
; do ur stuff here. ; Any code u wish to execute goes here.
popad ; Restore Regstatus.
popfd ; Restore Flagstatus.
mov eax, offset release_code_buffer ; Jmp to RELEASE() stub.
jmp eax ; Do jmp.
;ENDSCENE HOOK FUNCTION CODE....
EndSceneHook:
pushfd ; Save Flagstatus.
pushad ; Save Regstatus.
cmp menutoggle,4 ; Check if menu is in initial state.
jz nodraw ; If yes then no drawing is done.
cmp init_done,1 ; Check if all is initialised.
jz @
Already_init ; If yes then skip init functions.
push DWORD PTR [esp+028h] ; Push the games Device interface pointer.
call [obdevice_func_addy] ; Call obtain device function to pass the pointer
; to the D3D_DRAW dll.
add esp,4 ; balance stack.
call [init_func_addy] ; Initalise everything inside the D3D_DRAW dll.
mov init_done,1 ; Set BOOL for initialisation.
@
Already_init:
cmp menutoggle,0 ; Check if menu is turned on.
jz nodraw ; If no then no drawing is done.
call [draw_func_addy] ; Call draw function from the D3D_DRAW dll.
nodraw:
popad ; Restore Regstatus.
popfd ; Restore Flagstatus.
mov eax, offset endscene_code_buffer ; Jmp to ENDSCENE() stub.
jmp eax ; Do jmp.
;INDEX NUMBER (4) - YOUR FUNCTIONS WITHIN HOOK END
;INDEX NUMBER (5) - SETWINDOWSHOOKEX KEYBOARD HOOK CODE START
hook_processing PROC code

WORD, wparam:WPARAM, lparam:LPARAM
cmp wparam,VK_F1 ; Was F1 pressed?
jnz nkey1 ; If no then jmp to next check.
mov menutoggle,1 ; Toggle menu ON!
nkey1:
cmp wparam,VK_F2 ; Was F2 pressed?
jnz end_fn ; If no then end checks.
mov menutoggle,0 ; Toggle menu OFF!
end_fn:
ret ; return.
hook_processing endp
;INDEX NUMBER (5) - SETWINDOWSHOOKEX KEYBOARD HOOK CODE END
;INDEX NUMBER (6) - MAIN DLL INSTALLTION FUNCTION START
install_hook PROC ThreadId

WORD, EndSceneRVA

WORD, ProcID

WORD, ReleaseRVA

WORD
push ReleaseRVA ; Save releaserva worked out in the Loader section...
pop Release ;
push EndSceneRVA ; Save endscenerva worked out in the Loader section...
pop EndScene ;
push ProcID ; Save procid
pop ProcId ;
invoke SetWindowsHookEx, WH_KEYBOARD, offset hook_processing , hInst, ThreadId ; Install keyboard hook...
mov hHook, eax ; Save hook process handle..
ret ; return..
install_hook endp
;INDEX NUMBER (6) - MAIN DLL INSTALLTION FUNCTION END
End MAIN