写了一段Hook API的代码,拿出来和大家共享
最近写了一段Hook API的代码,可以Hook API函数MessageBoxA,当有程序调用这个函数的时候,会把文字改为Hooked,利用同样的技术,可以实现鼠标抓词,系统变速等功能。当然由于用到了ring3转ring0的代码,程序只能在win9x下面运行
FREE_INT_NUM=5
include e:\tasm\include\windows.inc
.386P
.model flat,stdCall
;APIs being used in the program
extrn ExitProcess:Proc
extrn CreateFileMappingA:Proc
extrn MapViewOfFileEx:Proc
extrn MessageBoxA:Proc
extrn LoadLibraryA:Proc
extrn GetProcAddress:Proc
extrn GetMessageA:Proc
extrn TranslateMessage:Proc
extrn DispatchMessageA:Proc
extrn UpdateWindow:Proc
extrn CreateWindowExA:Proc
extrn ShowWindow:Proc
extrn RegisterClassA:Proc
extrn LoadIconA:Proc
extrn LoadCursorA:Proc
extrn GetStockObject:Proc
extrn GetModuleHandleA:Proc
extrn PostQuitMessage:Proc
extrn DefWindowProcA:Proc
POINT struc
xx dd ?
yy dd ?
ends
WNDCLASS struc
clsStyle dd ?
clsLpfnWndProc dd ?
clsCbClsExtra dd ?
clsCbWndExtra dd ?
clsHInstance dd ?
clsHIcon dd ?
clsHCursor dd ?
clsHbrBackground dd ?
clsLpszMenuName dd ?
clsLpszClassName dd ?
ends
MSG struc
hwnd dd ?
emsg dd ?
wParam dd ?
lParam dd ?
etime dd ?
pt POINT<>
ends
.data
DllName db "User32.dll",0
ProcName db "MessageBoxA",0
HookName db "HookMsg",0
ErrText db "Error!",0
ErrCaption db "HookMsg",0
Caption db "Information",0
Text db "Processing",0
WindowCaption db "HookMsg",0
ClassName db "HookMsgClass",0
hInst dd ?
hWnd dd ?
align 4
msg MSG<>
wc WNDCLASS<>
.code
start:
;create a memory mapping file
call CreateFileMappingA,0FFFFFFFFh,NULL,4,0,10000h,offset HookName
cmp eax,NULL
jz DisplayErr
push eax
;apply a memory whose address is higer than 80000000 which will be shared by all proecsses
mov ebx,80000000h
LoopMapping:
mov eax,[esp]
call MapViewOfFileEx,eax,2,0,0,0,ebx
cmp eax,NULL
jz AddEbx ;if failed,add ebx and retry
jmp CopyCode
AddEbx:
add ebx,10000h
jmp LoopMapping
CopyCode:
add esp,4
;copy code to the high address
mov edi,eax
mov esi,offset HookCode
mov ecx,EndCopy-StartCopy
cld
rep movsb
call LoadLibraryA,offset DllName
cmp eax,NULL
jz DisplayErr
call GetProcAddress,eax,offset ProcName
cmp eax,NULL
jz DisplayErr
mov [ebx+ProcAddress-StartCopy],eax ;let's store the original MessageBoxA address to the data buffer in the high address
;let's stroe ebx into esp and then use ebp to represent the base address
mov ebp,ebx
push ebx ;store the base of the code mapped into the high address
sub ebx,eax ;now ebx is the relative address of the two instructions
sub ebx,5 ;add the lenth of the first intruction
mov dword ptr [ebp+NewInstruction+1-StartCopy],ebx ;store the new instruction which will take the place of the first 5 bytes of instrucion in MessageBoxA
mov bl,byte ptr [eax]
mov byte ptr [ebp+OldInstruction-StartCopy],bl ;save the first byte
mov ebx,dword ptr[eax+1]
mov dword ptr [ebp+OldInstruction+1-StartCopy],ebx ;save the next four bytes
mov ebx,[esp] ;ebx is the base of the code
lea eax,[ebx+NewInstruction-StartCopy]
push eax
lea edx,[ebx+offset ModifyCode-StartCopy]
;add ebx,@0-StartCopy
call edx ;call modifycode the modify the first five bytes of instruction of the messageboxa
;now the first five bytes' instruction is a JMP
;let's create a window
call GetModuleHandleA,0
mov [hInst],eax
mov eax,offset ClassName
mov wc.clsLpszClassName,eax
mov eax,[hInst]
mov wc.clsHInstance,eax
mov eax,offset WindowProc
mov wc.clsLpfnWndProc,eax
call LoadIconA,0,IDI_APPLICATION
mov wc.clsHIcon,eax
call LoadCursorA,0,IDC_IBEAM
mov wc.clsHCursor,eax
xor eax,eax
mov wc.clsLpszMenuName,eax
call GetStockObject,WHITE_BRUSH
mov wc.clsHbrBackground,eax
mov eax,CS_HREDRAW or CS_VREDRAW or CS_GLOBALCLASS
mov wc.clsStyle,eax
xor eax,eax
mov wc.clsCbClsExtra,eax
mov wc.clsCbWndExtra,eax
call RegisterClassA,offset wc
call CreateWindowExA,0,offset ClassName,offset WindowCaption,WS_OVERLAPPEDWINDOW,\
100,100,450,300,0,0,\
[hInst],0
mov hWnd,eax
call ShowWindow,[hWnd],1
call UpdateWindow,[hWnd]