DLL文件
;*************************************************************
;****** ******
;****** 本程序为HookAPI、禁止调试Hook ******
;****** 作者:残々剑 ******
;****** 写于2012年07月15日 ******
;****** ******
;*************************************************************
.386
.model flat,stdcall
option casemap:none
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;<<<<<<<<<<<<<<<<<<<<<<<<<定义一个结构体用来存放命令<<<<<<<<<<<<<<<<<<<<
HOOKAPI struct
a byte ?
PMyapi DWORD ?
d BYTE ?
e BYTE ?
HOOKAPI ends
.const
szKernel32 db 'kernel32.dll',0
szOpenProcess db 'OpenProcess',0
szName db '温馨提示',0
szText db '以下程序在执行进程操作!',0
szText1 db '安全卸载Dll',0
.data
.data?
hacker HOOKAPI <?>
hInstance dd ? ;Dll句柄
Apiaddr dd ? ;被Hook的函数地址
hCurProcID dd ? ;进程虚拟句柄
hHookApi dd ?
hHookKey dd ?
BalckCode db 10 dup (?) ;原来函数代码
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.code
;HookKey proc _dwCode,_wParam,_lParam
;
; invoke CallNextHookEx,hKeyHook,_dwCode,_wParam,_lParam
; cmp _wParam,VK_F12
; jne _RET
; invoke FindWindow,NULL,offset szWindow
; .if eax
; invoke ShowWindow,eax,SW_SHOW
; .endif
; _RET:
; ret
;
;HookKey endp
SetProcess proto :dword,:dword,:dword,:dword
GetApiaddr proto :dword,:dword,:dword
DllEnter proc _hInstance,_dwReason,_dwReserved
mov eax,_dwReason
.if eax == DLL_PROCESS_ATTACH
push _hInstance
pop hInstance
.elseif eax == DLL_PROCESS_DETACH
invoke SetProcess,Apiaddr,offset BalckCode,7h,0 ;恢愎原函数
.endif
mov eax,TRUE
ret
DllEnter endp
;******取得被Hook的函数地址存入变量
GetApiaddr proc _lpDllName,_lpApiName,_Apiaddr
invoke GetModuleHandle,_lpDllName
invoke GetProcAddress,eax,_lpApiName
mov ebx,_Apiaddr
mov dword ptr [ebx],eax ;返回值就是api的入口点
ret
GetApiaddr endp
;*****读取原函数、修改入口和恢愎被Hook的函数
SetProcess proc _ApiAdd,_lpBuffer,_Len,_Flag ;0还原旧代码、写入新代码,1<非零>读出原值
LOCAL mbi:MEMORY_BASIC_INFORMATION
LOCAL msize:DWORD
invoke VirtualQueryEx,hCurProcID,_ApiAdd,addr mbi,SIZEOF MEMORY_BASIC_INFORMATION ;返回页面虚拟信息
invoke VirtualProtectEx,hCurProcID,mbi.BaseAddress,_Len,PAGE_EXECUTE_READWRITE,addr mbi.Protect ;注:最后一个是个地址
mov eax,_Flag
.if eax == 0
invoke WriteProcessMemory,hCurProcID,_ApiAdd,_lpBuffer,_Len,NULL
.else
invoke ReadProcessMemory,hCurProcID,_ApiAdd,_lpBuffer,_Len,NULL
.endif
invoke VirtualProtectEx,hCurProcID,mbi.BaseAddress,_Len,PAGE_EXECUTE_READ,addr mbi.Protect ;注:最后一个是个地址
ret
SetProcess endp
;******加入钩子链表以便之后Hook调用
HookProc proc _dwCode,_wParam,lParam
invoke CallNextHookEx,hHookApi,_dwCode,_wParam,lParam
mov eax,TRUE
ret
HookProc endp
;******Hook函数入口
MyAPI proc
invoke MessageBox,NULL,addr szText,addr szName,MB_YESNO ;弹出信息框选择是否阻止
.if eax == 6 ;如果选择否(NO)
;*********************************
;** 填入HOOK的函数 **
;*********************************
.endif
invoke SetProcess,Apiaddr,offset BalckCode,7h,0 ;先还原API
jmp Apiaddr ;再调用原API地址
ret
MyAPI endp
;******安装钩子函数
InstallHook proc
mov hacker.a,0B8h ;mov eax, ;初始化
mov hacker.PMyapi,offset MyAPI ;0x0000,这里设置替代API的函数地址
mov hacker.d,0FFh ;jmp
mov hacker.e,0E0h ;eax
invoke GetCurrentProcess
mov hCurProcID,eax
invoke GetApiaddr,offset szKernel32,offset szOpenProcess,offset Apiaddr
invoke SetProcess,Apiaddr,offset BalckCode,7h,1 ;读取原函数前8个字节至缓充区(OpenProcessCode)
invoke SetProcess,Apiaddr,addr hacker,7h,0 ;写入新字节,实现在跳转
invoke SetWindowsHookEx,WH_GETMESSAGE,addr HookProc,hInstance,NULL
mov hHookApi,eax
ret
InstallHook endp
;******卸载钩子函数
UninstallHook proc
invoke SetProcess,Apiaddr,offset BalckCode,7h,0 ;恢愎原函数
invoke UnhookWindowsHookEx,hHookApi
ret
UninstallHook endp
;//////////////////////////////////////////////////////////
;/// 以下是键盘Hook ///
;//////////////////////////////////////////////////////////
HookKeyProc proc uses esi edi ebx _dwCode,_wParam,_lParam
mov eax,_wParam
.if (eax == WH_KEYBOARD) || (eax == WH_JOURNALRECORD) || (eax == WH_KEYBOARD_LL)
mov eax,TRUE
ret
.endif
invoke CallNextHookEx,hHookKey,_dwCode,_wParam,_lParam
ret
HookKeyProc endp
InstallHookKey proc
invoke SetWindowsHookEx,WH_DEBUG,addr HookKeyProc,hInstance,NULL
mov hHookKey,eax
ret
InstallHookKey endp
UninstallHookKey proc
invoke UnhookWindowsHookEx,hHookKey
ret
UninstallHookKey endp
end DllEnter
主程序文件
;*************************************************************
;****** ******
;****** 本程序为HookAPI、禁止调试Hook ******
;****** 作者:残々剑 ******
;****** 写于2012年07月15日 ******
;****** ******
;*************************************************************
.386
.model flat,stdcall
option casemap:none
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;<<<<<<<<<<<<<<<<<<<<<<<<<定义一个结构体用来存放命令<<<<<<<<<<<<<<<<<<<<
HOOKAPI struct
a byte ?
PMyapi DWORD ?
d BYTE ?
e BYTE ?
HOOKAPI ends
.const
szKernel32 db 'kernel32.dll',0
szOpenProcess db 'OpenProcess',0
szName db '温馨提示',0
szText db '以下程序在执行进程操作!',0
szText1 db '安全卸载Dll',0
.data
.data?
hacker HOOKAPI <?>
hInstance dd ? ;Dll句柄
Apiaddr dd ? ;被Hook的函数地址
hCurProcID dd ? ;进程虚拟句柄
hHookApi dd ?
hHookKey dd ?
BalckCode db 10 dup (?) ;原来函数代码
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.code
;HookKey proc _dwCode,_wParam,_lParam
;
; invoke CallNextHookEx,hKeyHook,_dwCode,_wParam,_lParam
; cmp _wParam,VK_F12
; jne _RET
; invoke FindWindow,NULL,offset szWindow
; .if eax
; invoke ShowWindow,eax,SW_SHOW
; .endif
; _RET:
; ret
;
;HookKey endp
SetProcess proto :dword,:dword,:dword,:dword
GetApiaddr proto :dword,:dword,:dword
DllEnter proc _hInstance,_dwReason,_dwReserved
mov eax,_dwReason
.if eax == DLL_PROCESS_ATTACH
push _hInstance
pop hInstance
.elseif eax == DLL_PROCESS_DETACH
invoke SetProcess,Apiaddr,offset BalckCode,7h,0 ;恢愎原函数
.endif
mov eax,TRUE
ret
DllEnter endp
;******取得被Hook的函数地址存入变量
GetApiaddr proc _lpDllName,_lpApiName,_Apiaddr
invoke GetModuleHandle,_lpDllName
invoke GetProcAddress,eax,_lpApiName
mov ebx,_Apiaddr
mov dword ptr [ebx],eax ;返回值就是api的入口点
ret
GetApiaddr endp
;*****读取原函数、修改入口和恢愎被Hook的函数
SetProcess proc _ApiAdd,_lpBuffer,_Len,_Flag ;0还原旧代码、写入新代码,1<非零>读出原值
LOCAL mbi:MEMORY_BASIC_INFORMATION
LOCAL msize:DWORD
invoke VirtualQueryEx,hCurProcID,_ApiAdd,addr mbi,SIZEOF MEMORY_BASIC_INFORMATION ;返回页面虚拟信息
invoke VirtualProtectEx,hCurProcID,mbi.BaseAddress,_Len,PAGE_EXECUTE_READWRITE,addr mbi.Protect ;注:最后一个是个地址
mov eax,_Flag
.if eax == 0
invoke WriteProcessMemory,hCurProcID,_ApiAdd,_lpBuffer,_Len,NULL
.else
invoke ReadProcessMemory,hCurProcID,_ApiAdd,_lpBuffer,_Len,NULL
.endif
invoke VirtualProtectEx,hCurProcID,mbi.BaseAddress,_Len,PAGE_EXECUTE_READ,addr mbi.Protect ;注:最后一个是个地址
ret
SetProcess endp
;******加入钩子链表以便之后Hook调用
HookProc proc _dwCode,_wParam,lParam
invoke CallNextHookEx,hHookApi,_dwCode,_wParam,lParam
mov eax,TRUE
ret
HookProc endp
;******Hook函数入口
MyAPI proc
invoke MessageBox,NULL,addr szText,addr szName,MB_YESNO ;弹出信息框选择是否阻止
.if eax == 6 ;如果选择否(NO)
;*********************************
;** 填入HOOK的函数 **
;*********************************
.endif
invoke SetProcess,Apiaddr,offset BalckCode,7h,0 ;先还原API
jmp Apiaddr ;再调用原API地址
ret
MyAPI endp
;******安装钩子函数
InstallHook proc
mov hacker.a,0B8h ;mov eax, ;初始化
mov hacker.PMyapi,offset MyAPI ;0x0000,这里设置替代API的函数地址
mov hacker.d,0FFh ;jmp
mov hacker.e,0E0h ;eax
invoke GetCurrentProcess
mov hCurProcID,eax
invoke GetApiaddr,offset szKernel32,offset szOpenProcess,offset Apiaddr
invoke SetProcess,Apiaddr,offset BalckCode,7h,1 ;读取原函数前8个字节至缓充区(OpenProcessCode)
invoke SetProcess,Apiaddr,addr hacker,7h,0 ;写入新字节,实现在跳转
invoke SetWindowsHookEx,WH_GETMESSAGE,addr HookProc,hInstance,NULL
mov hHookApi,eax
ret
InstallHook endp
;******卸载钩子函数
UninstallHook proc
invoke SetProcess,Apiaddr,offset BalckCode,7h,0 ;恢愎原函数
invoke UnhookWindowsHookEx,hHookApi
ret
UninstallHook endp
;//////////////////////////////////////////////////////////
;/// 以下是键盘Hook ///
;//////////////////////////////////////////////////////////
HookKeyProc proc uses esi edi ebx _dwCode,_wParam,_lParam
mov eax,_wParam
.if (eax == WH_KEYBOARD) || (eax == WH_JOURNALRECORD) || (eax == WH_KEYBOARD_LL)
mov eax,TRUE
ret
.endif
invoke CallNextHookEx,hHookKey,_dwCode,_wParam,_lParam
ret
HookKeyProc endp
InstallHookKey proc
invoke SetWindowsHookEx,WH_DEBUG,addr HookKeyProc,hInstance,NULL
mov hHookKey,eax
ret
InstallHookKey endp
UninstallHookKey proc
invoke UnhookWindowsHookEx,hHookKey
ret
UninstallHookKey endp
end DllEnter
#include "resource.h"
#define IDD_DLG1 1000
#define ICO_MAIN 1001
#define IDC_CHK1 1002
#define IDC_CHK2 1003
#define IDC_EDT1 1004
#define IDC_STC1 1005
#define IDC_BTN1 1006
#define IDC_BTN2 1007
ICO_MAIN ICON "Main.ico"
IDD_DLG1 DIALOGEX 6,6,100,70
CAPTION "HooK示例-->残剑"
FONT 9,"宋体",400,0,134
STYLE 0x94C800C4
EXSTYLE 0x00010101
BEGIN
CONTROL "HookAPI",IDC_CHK1,"Button",0x50010003,2,18,46,10,0x00000000
CONTROL "HookKey",IDC_CHK2,"Button",0x50010003,52,18,46,10,0x00000000
CONTROL "请输入需要HooKApi",IDC_EDT1,"Edit",0x50010000,4,2,94,12,0x00000200
CONTROL "提示:按Home显示or隐藏",IDC_STC1,"Static",0x50000000,0,32,102,10,0x00000000
CONTROL "Hook",IDC_BTN1,"Button",0x50010000,2,48,36,12,0x00000000
CONTROL "UnHook",IDC_BTN2,"Button",0x50010000,60,48,38,12,0x00000000
END
def文件:
EXPORTS InstallHook
EXPORTS UninstallHook
EXPORTS InstallHookKey
EXPORTS UninstallHookKey
inc文件:
InstallHook proto
UninstallHook proto
InstallHookKey proto
UninstallHookKey proto
有一问题想请教各位班竹:
如何HOOK指定进程的API 比如我要HOOK 进程A 的OpenProcsse 在这程序里我应该在哪里下手呢?
注:本程序HookKey没有写了。本人QQ:有兴趣的一起讨论:50325232 本程序完全原创
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课