用WIN32汇编写的超简单钩子(初学者适用)
<strong><br/></strong>大家好!我是菜鸟一名(菜中之菜^_^)!说真的!本人汇编水平不高!刚学会了这些东东只想在这里与大家学习学习!在这里看到的关于怎样<br/>编写钩子的文章!大多数都是用VC++编写的(可能以前已经有高手们用汇编写过!只是我没有看到而已!呵呵!请不要见怪)现在我来简单介<br/>绍怎样用WIN32ASM来编写超简单钩子!(本人是第一次写这些东东的!难免有一些不对的地方!请高手们指正指正。) <p></p><p>我使用的工具:Masm32v8(安装到C:\Masm32v8)+记事本</p><p>第一步,执行文件源程序的建立。打开记事本,输入以下的源程序,保存为hook.asm,先不要编译!<br/>=========================================<br/>.386<br/>.model flat, stdcall<br/>option casemap :none </p><p>include windows.inc<br/>include user32.inc<br/>include kernel32.inc<br/>includelib user32.lib<br/>includelib kernel32.lib<br/>include hookdll.inc<br/>includelib hookdll.lib</p><p>.data<br/>ClassName db 'MyClass',0<br/>Caption db '我的第一个窗口',0</p><p>.data?<br/>hInstance dd ?<br/>hWnd dd ?</p><p>.code<br/> GoGo:<br/> call WinMain<br/> invoke ExitProcess,NULL</p><p>WinMain proc<br/> LOCAL wc:WNDCLASSEX<br/> LOCAL msg:MSG<br/> invoke GetModuleHandle,NULL<br/> mov hInstance,eax<br/> mov wc.cbSize,sizeof WNDCLASSEX<br/> mov wc.style,CS_HREDRAW or CS_VREDRAW<br/> mov wc.lpfnWndProc,offset WndPro<br/> mov wc.cbClsExtra,NULL<br/> mov wc.cbWndExtra,NULL<br/> mov eax,hInstance<br/> mov wc.hInstance,eax<br/> invoke LoadIcon,NULL,IDI_APPLICATION<br/> mov wc.hIcon,eax<br/> mov wc.hIconSm,eax<br/> invoke LoadCursor,NULL,IDC_ARROW<br/> mov wc.hCursor,eax<br/> mov wc.hbrBackground,COLOR_WINDOW+1<br/> mov wc.lpszMenuName,NULL<br/> mov wc.lpszClassName,offset ClassName<br/> invoke RegisterClassEx,addr wc<br/> invoke CreateWindowEx,\<br/> NULL,addr ClassName,\<br/> addr Caption,\<br/> WS_OVERLAPPEDWINDOW,\<br/> CW_USEDEFAULT,\<br/> CW_USEDEFAULT,\<br/> CW_USEDEFAULT,\<br/> CW_USEDEFAULT,\<br/> NULL,\<br/> NULL,\<br/> hInstance,\<br/> NULL<br/> mov hWnd,eax<br/> invoke ShowWindow,hWnd,SW_SHOW<br/> invoke UpdateWindow,hWnd<br/> .while TRUE<br/> invoke GetMessage,addr msg,NULL,0,0<br/> .break .if (!eax)<br/> invoke TranslateMessage, addr msg <br/> invoke DispatchMessage, addr msg<br/> .endw<br/> mov eax,msg.wParam<br/> ret<br/>WinMain endp</p><p>WndPro proc hwnd:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD<br/> .if uMsg==WM_DESTROY <br/> invoke PostQuitMessage,NULL<br/> .elseif uMsg==WM_CREATE<br/> invoke installhook<br/> .else<br/> invoke DefWindowProc,hwnd,uMsg,wParam,lParam <br/> ret <br/> .endif <br/> xor eax,eax <br/>ret<br/>WndPro endp</p><p>end GoGo<br/>=========================================</p><p>第二步,建立一个批处理文件(用记事本建立就可以了,文件名:make.bat)这个批处理文件的功能是用来编译执行文件的,内容如下:<br/>=========================================<br/>set include=c:\Masm32v8\Include<br/>set lib=c:\Masm32v8\lib<br/>set path=c:\Masm32v8\Bin</p><p>ml -c -coff hook.asm<br/>link /subsystem:windows hook.obj<br/>=========================================</p><p>第三步,建立DLL源程序(文件名:hookdll.asm),同样打开记事本输入源程序:<br/>=========================================<br/>.386 <br/>.model flat,stdcall <br/>option casemap:none </p><p>include windows.inc <br/>include user32.inc <br/>include kernel32.inc <br/>includelib user32.lib <br/>includelib kernel32.lib</p><p>.data<br/>msgTitle db "提示",0<br/>msgText db "win32asm钩子实例",0</p><p>.data?<br/>hInstdll dd ? ;dll 句柄<br/>hHook dd ? ;钩子句柄</p><p>.code<br/> DllMain proc hInst:HINSTANCE, reason:DWORD, reserved1:DWORD<br/> .if reason==DLL_PROCESS_ATTACH <br/> push hInst<br/> pop hInstdll<br/> .elseif reason==DLL_PROCESS_DETACH<br/> call uninstallhook<br/> .endif<br/> mov eax,TRUE<br/> ret<br/> DllMain endp</p><p> showme proc nCode:DWORD,wParam:DWORD,lParam:DWORD ;简单的钩子函数<br/> invoke CallNextHookEx,hHook,nCode,wParam,lParam <br/> .if wParam==79h<br/> invoke MessageBox,NULL,addr msgText,addr msgTitle,MB_OK<br/> .endif<br/> ret<br/> showme endp</p><p> installhook proc ;安装hook的子程序<br/> invoke SetWindowsHookEx,WH_KEYBOARD,addr showme,hInstdll,NULL<br/> mov hHook,eax<br/> ret<br/> installhook endp<br/> <br/> uninstallhook proc ;清除hook的子程序 <br/> invoke UnhookWindowsHookEx,hHook<br/> ret<br/> uninstallhook endp<br/> <br/> end DllMain<br/>=========================================</p><p><br/>第四步,建立扩展名为.DEF的文件hookdll.def,这文件是用来说明DLL中哪些函数可以给应用程序调用,它连跟DLL目标文件一齐链接,也可以<br/>用记事本编写:<br/>=========================================<br/>LIBRARY hookdll<br/>EXPORTS installhook<br/>EXPORTS uninstallhook<br/>=========================================<br/>呵呵!就这么几行!上面的LIBRARY关键字就是DLL的名!EXPORTS就是说明哪些函可以给应用程序调用</p><p><br/>第五步,建立扩展名为.inc的文件hookdll.inc,这个文件用于说明DLL中函数的参数跟类型,我因为太懒了~!大多数这些文件我都是用记事本</p><p>写的!编译执行文件的时候需要它哦!~~~.inc文件的内容如下:<br/>=========================================<br/>installhook proto<br/>uninstallhook proto<br/>=========================================</p><p><br/>第六步,呵呵!是不是有点累?好了好了!剩下最后要建立的一个文件!其实建立这些文件只是为了方便而已(费话少说)~!建立用于编译<br/>和链接DLL的批处理文件(文件名:makedll.bat),内容如下:<br/>=========================================<br/>set include=c:\Masm32v8\Include<br/>set lib=c:\Masm32v8\lib<br/>set path=c:\Masm32v8\Bin</p><p>ml -c -coff hookdll.asm<br/>link /dll /subsystem:windows /def:hookdll.def hookdll.obj<br/>=========================================</p><p>好了!所需要的文件建立好了!把刚才建立的文件都放在同一目录中!先运行用于编译DLL的批处理文件,如果无出错既话!在目录会产生四个</p><p>文件:hookdll.obj,hookdll.exp,hookdll.lib,hookdll.dll!如果没有产生这些文件!就说明出错了~555555!hookdll.dll已经产生出来的话</p><p>就运行用于编译执行文件的批处理!成功的话就会产生出hook.exe了!运行hook.exe!按F10!有信息框(内容为:win32asm钩子)弹出的话!</p><p>恭喜你!你用win32asm编写的超简单钩子出炉了~!爽</p><p><br/>(例子源代码:<font color="#9c0000">http://www.cc-365.com/lzq/asmhook.rar</font>)</p><p>作者:懒虫<br/>QQ:270909303<br/>mail:lzq@cc-365.com</p>
页:
[1]