清家当产,求delphi 通过hook技术拦截某个程序的数据包代码 ( 积分: 100 )

  • 主题发起人 主题发起人 tanafenga
  • 开始时间 开始时间
T

tanafenga

Unregistered / Unconfirmed
GUEST, unregistred user!
只要求提供简单代码!<br>将截获的数据内容显示到拦截控制窗体的memo中就可以!<br>网络上的很多代码,象外挂等等,都是不提供dll部分的源代码!哭死,找了几天了!<br><br>250全部给他!!!我就这么多了!!
 
我刚做完的hook<br>比较乱&nbsp;,&nbsp;<br><br><br>unit&nbsp;UnitHookDll;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;SysUtils,&nbsp;Classes,&nbsp;math,&nbsp;messages,&nbsp;dialogs,&nbsp;UnitNt2000Hook,<br>&nbsp;&nbsp;UnitHookType,Winsock;<br><br>const<br>&nbsp;&nbsp;Trap=True;&nbsp;&nbsp;//True陷阱式,False表示改引入表式<br><br>&nbsp;&nbsp;procedure&nbsp;StartHook;stdcall;<br>&nbsp;&nbsp;procedure&nbsp;StopHook;stdcall;<br><br>implementation<br><br>var<br>&nbsp;&nbsp;GetMsgHook:THandle;<br>&nbsp;&nbsp;pShMem:&nbsp;PShareMem;<br>&nbsp;&nbsp;hMappingFile:&nbsp;THandle;<br>&nbsp;&nbsp;HookSend:THookClass;{API&nbsp;HOOK类}<br>&nbsp;&nbsp;HookRecv:THookClass;{API&nbsp;HOOK类}<br><br>procedure&nbsp;SendData(const&nbsp;SendText:&nbsp;string);<br>var<br>&nbsp;&nbsp;DS:&nbsp;TCopyDataStruct;<br>begin<br>&nbsp;&nbsp;DS.dwData&nbsp;:=&nbsp;0;<br>&nbsp;&nbsp;DS.cbData&nbsp;:=&nbsp;Length(SendText);<br>&nbsp;&nbsp;DS.lpData&nbsp;:=&nbsp;@SendText[1];<br>&nbsp;&nbsp;SendMessage(pShMem^.hProcWnd,&nbsp;WM_COPYDATA,&nbsp;0,&nbsp;LongWord(@DS));<br>end;<br><br>function&nbsp;MyRecv(s:&nbsp;TSocket;&nbsp;var&nbsp;Buf;&nbsp;len,&nbsp;flags:&nbsp;Integer):&nbsp;Integer;&nbsp;stdcall;<br>var<br>&nbsp;&nbsp;RemoteAddr:TSockAddr;<br>&nbsp;&nbsp;RemoteAddrLen:Integer;<br>&nbsp;&nbsp;LocalAddr:TSockAddr;<br>&nbsp;&nbsp;LocalAddrLen:Integer;<br>&nbsp;&nbsp;str:array&nbsp;of&nbsp;char;<br>begin<br>&nbsp;&nbsp;HookRecv.Restore;<br><br>&nbsp;&nbsp;RemoteAddrLen:=Sizeof(RemoteAddr);<br>&nbsp;&nbsp;LocalAddrLen:=Sizeof(LocalAddr);<br>&nbsp;&nbsp;getpeername(s,RemoteAddr,RemoteAddrLen);<br>&nbsp;&nbsp;getsockname(s,LocalAddr,&nbsp;LocalAddrLen);<br><br>&nbsp;&nbsp;pShMem^.LocalAddr:=LocalAddr;<br>&nbsp;&nbsp;pShMem^.RemoteAddr:=RemoteAddr;<br><br>&nbsp;&nbsp;MyRecv:=Recv(s,Buf,len,flags);<br><br>&nbsp;&nbsp;PostMessage(pShMem^.hProcWnd,&nbsp;WM_CALLHOOKAPI,&nbsp;FN_RECV,&nbsp;FN_RECV);<br><br>&nbsp;&nbsp;setLength(str,len);<br>&nbsp;&nbsp;copymemory(str,@buf,len);<br>&nbsp;&nbsp;SendData(string(str));<br><br>&nbsp;&nbsp;HookRecv.Change;<br>end;<br><br>function&nbsp;MySend(s:&nbsp;TSocket;&nbsp;var&nbsp;Buf;&nbsp;len,&nbsp;flags:&nbsp;Integer):&nbsp;Integer;&nbsp;stdcall;<br>var<br>&nbsp;&nbsp;RemoteAddr:TSockAddr;<br>&nbsp;&nbsp;RemoteAddrLen:Integer;<br>&nbsp;&nbsp;LocalAddr:TSockAddr;<br>&nbsp;&nbsp;LocalAddrLen:Integer;<br>&nbsp;&nbsp;str:array&nbsp;of&nbsp;char;<br>begin<br>&nbsp;&nbsp;HookSend.Restore;<br><br>&nbsp;&nbsp;RemoteAddrLen:=Sizeof(RemoteAddr);<br>&nbsp;&nbsp;LocalAddrLen:=Sizeof(LocalAddr);<br>&nbsp;&nbsp;getpeername(s,RemoteAddr,RemoteAddrLen);<br> getsockname(s,LocalAddr,&nbsp;LocalAddrLen);<br><br>&nbsp;&nbsp;pShMem^.LocalAddr:=LocalAddr;<br>&nbsp;&nbsp;pShMem^.RemoteAddr:=RemoteAddr;<br><br>&nbsp;&nbsp;MySend:=Send(s,Buf,len,flags);<br><br>&nbsp;&nbsp;PostMessage(pShMem^.hProcWnd,&nbsp;WM_CALLHOOKAPI,&nbsp;FN_SEND,&nbsp;FN_SEND);<br><br>&nbsp;&nbsp;setlength(str,len);<br>&nbsp;&nbsp;copymemory(str,@buf,len);<br>&nbsp;&nbsp;SendData(string(str));<br><br>&nbsp;&nbsp;HookSend.Change;<br>end;<br><br><br>{消息钩子}<br>function&nbsp;GetMsgHookProc(nCode:&nbsp;integer;&nbsp;wPar:&nbsp;WParam;&nbsp;lPar:&nbsp;LParam):&nbsp;lResult;stdcall;<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;CallNextHookEx(GetMsgHook,&nbsp;nCode,&nbsp;wPar,&nbsp;lPar);<br>end;<br><br>procedure&nbsp;StartHook;&nbsp;stdcall;<br>begin<br>&nbsp;&nbsp;&nbsp;if&nbsp;GetMsgHook=0&nbsp;then<br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetMsgHook&nbsp;:=&nbsp;SetWindowsHookEx(WH_GETMESSAGE,&nbsp;GetMsgHookProc,&nbsp;HInstance,&nbsp;0);<br>&nbsp;&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;StopHook;&nbsp;stdcall;<br>begin<br>&nbsp;&nbsp;&nbsp;if&nbsp;GetMsgHook&lt;&gt;0&nbsp;then<br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UnhookWindowsHookEx(GetMsgHook);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetMsgHook:=0;<br>&nbsp;&nbsp;&nbsp;end;<br>end;<br><br>initialization<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hMappingFile&nbsp;:=&nbsp;OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;hMappingFile=0&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hMappingFile&nbsp;:=&nbsp;CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShareMem),MappingFileName)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;hMappingFile=0&nbsp;then&nbsp;Exception.Create('不能建立共享内存!');<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pShMem&nbsp;:=&nbsp;&nbsp;MapViewOfFile(hMappingFile,FILE_MAP_WRITE&nbsp;or&nbsp;FILE_MAP_READ,0,0,0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;pShMem&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hMappingFile);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exception.Create('不能映射共享内存!');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HookSend:=THookClass.Create(Trap,@Send,@MySend);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HookRecv:=THookClass.Create(Trap,@Recv,@MyRecv);<br><br>finalization<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HookSend.Destroy;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HookRecv.Destroy;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UnMapViewOfFile(pShMem);&nbsp;{取消映射视图}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hMappingFile);&nbsp;{关闭映射文件句柄}<br><br>end.
 
分给少了!不好意思,心急没注意!<br>收到满意答复一定给,全部家产都给
 
//这个是HookAPI的类,不是我写的,是delphi深入windows下编程那本书里的<br><br>unit&nbsp;UnitNt2000Hook;<br><br>interface<br><br>uses&nbsp;classes,&nbsp;Windows,SysUtils,&nbsp;messages,dialogs;<br><br>type<br>&nbsp;&nbsp;TImportCode&nbsp;=&nbsp;packed&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JumpInstruction:&nbsp;Word;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddressOfPointerToFunction:&nbsp;PPointer;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;PImportCode&nbsp;=&nbsp;^TImportCode;<br>&nbsp;&nbsp;PImage_Import_Entry&nbsp;=&nbsp;^Image_Import_Entry;<br>&nbsp;&nbsp;Image_Import_Entry&nbsp;=&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;Characteristics:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;TimeDateStamp:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;MajorVersion:&nbsp;Word;<br>&nbsp;&nbsp;&nbsp;&nbsp;MinorVersion:&nbsp;Word;<br>&nbsp;&nbsp;&nbsp;&nbsp;Name:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;LookupTable:&nbsp;DWORD;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;TLongJmp&nbsp;=&nbsp;packed&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JmpCode:&nbsp;ShortInt;&nbsp;{指令,用$E9来代替系统的指令}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FuncAddr:&nbsp;DWORD;&nbsp;{函数地址}<br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;THookClass&nbsp;=&nbsp;class<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Trap:boolean;&nbsp;{调用方式:True陷阱式,False改引入表式}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hProcess:&nbsp;Cardinal;&nbsp;{进程句柄,只用于陷阱式}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AlreadyHook:boolean;&nbsp;{是否已安装Hook,只用于陷阱式}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AllowChange:boolean;&nbsp;{是否允许安装、卸载Hook,只用于改引入表式}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Oldcode:&nbsp;array[0..4]of&nbsp;byte;&nbsp;{系统函数原来的前5个字节}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Newcode:&nbsp;TLongJmp;&nbsp;{将要写在系统函数的前5个字节}<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OldFunction,NewFunction:Pointer;{被截函数、自定义函数}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;constructor&nbsp;Create(IsTrap:boolean;OldFun,NewFun:pointer);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;constructor&nbsp;Destroy;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Restore;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Change;<br>&nbsp;&nbsp;published<br>&nbsp;&nbsp;end;<br><br>implementation<br><br>{取函数的实际地址。如果函数的第一个指令是Jmp,则取出它的跳转地址(实际地址),这往往是由于程序中含有Debug调试信息引起的}<br>function&nbsp;FinalFunctionAddress(Code:&nbsp;Pointer):&nbsp;Pointer;<br>Var<br>&nbsp;&nbsp;func:&nbsp;PImportCode;<br>begin<br>&nbsp;&nbsp;Result:=Code;<br>&nbsp;&nbsp;if&nbsp;Code=nil&nbsp;then&nbsp;exit;<br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;func:=code;<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(func.JumpInstruction=$25FF)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{指令二进制码FF&nbsp;25&nbsp;&nbsp;汇编指令jmp&nbsp;[...]}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Func:=func.AddressOfPointerToFunction^;<br>&nbsp;&nbsp;&nbsp;&nbsp;result:=Func;<br>&nbsp;&nbsp;except<br>&nbsp;&nbsp;&nbsp;&nbsp;Result:=nil;<br>&nbsp;&nbsp;end;<br>end;<br><br>{更改引入表中指定函数的地址,只用于改引入表式}<br>function&nbsp;PatchAddressInModule(BeenDone:Tlist;hModule:&nbsp;THandle;&nbsp;OldFunc,NewFunc:&nbsp;Pointer):integer;<br>const<br>&nbsp;&nbsp;&nbsp;SIZE=4;<br>Var<br>&nbsp;&nbsp;&nbsp;Dos:&nbsp;PImageDosHeader;<br>&nbsp;&nbsp;&nbsp;NT:&nbsp;PImageNTHeaders;<br>&nbsp;&nbsp;&nbsp;ImportDesc:&nbsp;PImage_Import_Entry;<br>&nbsp;&nbsp;&nbsp;rva:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;Func:&nbsp;PPointer;<br>&nbsp;&nbsp;&nbsp;DLL:&nbsp;String;<br>&nbsp;&nbsp;&nbsp;f:&nbsp;Pointer;<br>&nbsp;&nbsp;&nbsp;written:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;mbi_thunk:TMemoryBasicInformation;<br>&nbsp;&nbsp;&nbsp;dwOldProtect:DWORD;<br>begin<br>&nbsp;&nbsp;Result:=0;<br>&nbsp;&nbsp;if&nbsp;hModule=0&nbsp;then&nbsp;exit;<br>&nbsp;&nbsp;Dos:=Pointer(hModule);<br>&nbsp;&nbsp;{如果这个DLL模块已经处理过,则退出。BeenDone包含已处理的DLL模块}<br>&nbsp;&nbsp;if&nbsp;BeenDone.IndexOf(Dos)&gt;=0&nbsp;then&nbsp;exit;<br>&nbsp;&nbsp;BeenDone.Add(Dos);{把DLL模块名加入BeenDone}<br>&nbsp;&nbsp;OldFunc:=FinalFunctionAddress(OldFunc);{取函数的实际地址}<br><br>&nbsp;&nbsp;{如果这个DLL模块的地址不能访问,则退出}<br>&nbsp;&nbsp;if&nbsp;IsBadReadPtr(Dos,SizeOf(TImageDosHeader))&nbsp;then&nbsp;exit;<br>&nbsp;&nbsp;{如果这个模块不是以'MZ'开头,表明不是DLL,则退出}<br>&nbsp;&nbsp;if&nbsp;Dos.e_magic&lt;&gt;IMAGE_DOS_SIGNATURE&nbsp;then&nbsp;exit;{IMAGE_DOS_SIGNATURE='MZ'}<br><br>&nbsp;&nbsp;{定位至NT&nbsp;Header}<br>&nbsp;&nbsp;NT&nbsp;:=Pointer(Integer(Dos)&nbsp;+&nbsp;dos._lfanew);<br>&nbsp;&nbsp;{定位至引入函数表}<br>&nbsp;&nbsp;RVA:=NT^.OptionalHeader.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;<br>&nbsp;&nbsp;if&nbsp;RVA=0&nbsp;then&nbsp;exit;{如果引入函数表为空,则退出}<br>&nbsp;&nbsp;{把函数引入表的相对地址RVA转换为绝对地址}<br>&nbsp;&nbsp;ImportDesc&nbsp;:=&nbsp;pointer(DWORD(Dos)+RVA);{Dos是此DLL模块的首地址}<br><br>&nbsp;&nbsp;{遍历所有被引入的下级DLL模块}<br>&nbsp;&nbsp;While&nbsp;(ImportDesc^.Name&lt;&gt;0)&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;{被引入的下级DLL模块名字}<br>&nbsp;&nbsp;&nbsp;&nbsp;DLL:=PChar(DWORD(Dos)+ImportDesc^.Name);<br>&nbsp;&nbsp;&nbsp;&nbsp;{把被导入的下级DLL模块当做当前模块,进行递归调用}<br>&nbsp;&nbsp;&nbsp;&nbsp;PatchAddressInModule(BeenDone,GetModuleHandle(PChar(DLL)),OldFunc,NewFunc);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;{定位至被引入的下级DLL模块的函数表}<br>&nbsp;&nbsp;&nbsp;&nbsp;Func:=Pointer(DWORD(DOS)+ImportDesc.LookupTable);<br>&nbsp;&nbsp;&nbsp;&nbsp;{遍历被引入的下级DLL模块的所有函数}<br>&nbsp;&nbsp;&nbsp;&nbsp;While&nbsp;Func^&lt;&gt;nil&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f:=FinalFunctionAddress(Func^);{取实际地址}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;f=OldFunc&nbsp;then&nbsp;{如果函数实际地址就是所要找的地址}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VirtualQuery(Func,mbi_thunk,&nbsp;sizeof(TMemoryBasicInformation));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VirtualProtect(Func,SIZE,PAGE_EXECUTE_WRITECOPY,mbi_thunk.Protect);{更改内存属性}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WriteProcessMemory(GetCurrentProcess,Func,@NewFunc,SIZE,written);{把新函数地址覆盖它}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VirtualProtect(Func,&nbsp;SIZE,&nbsp;mbi_thunk.Protect,dwOldProtect);{恢复内存属性}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;Written=4&nbsp;then&nbsp;Inc(Result);<br>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;showmessagefmt('error:%d',[Written]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Inc(Func);{下一个功能函数}<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;Inc(ImportDesc);{下一个被引入的下级DLL模块}<br>&nbsp;&nbsp;end;<br>end;<br><br>{HOOK的入口,其中IsTrap表示是否采用陷阱式}<br>constructor&nbsp;THookClass.Create(IsTrap:boolean;OldFun,NewFun:pointer);<br>begin<br>&nbsp;&nbsp;&nbsp;{求被截函数、自定义函数的实际地址}<br>&nbsp;&nbsp;&nbsp;OldFunction:=FinalFunctionAddress(OldFun);<br>&nbsp;&nbsp;&nbsp;NewFunction:=FinalFunctionAddress(NewFun);<br><br>&nbsp;&nbsp;&nbsp;Trap:=IsTrap;<br>&nbsp;&nbsp;&nbsp;if&nbsp;Trap&nbsp;then{如果是陷阱式}<br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{以特权的方式来打开当前进程}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hProcess&nbsp;:=&nbsp;OpenProcess(PROCESS_ALL_ACCESS,FALSE,&nbsp;GetCurrentProcessID);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{生成jmp&nbsp;xxxx的代码,共5字节}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Newcode.JmpCode&nbsp;:=&nbsp;ShortInt($E9);&nbsp;{jmp指令的十六进制代码是E9}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NewCode.FuncAddr&nbsp;:=&nbsp;DWORD(NewFunction)&nbsp;-&nbsp;DWORD(OldFunction)&nbsp;-&nbsp;5;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{保存被截函数的前5个字节}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;move(OldFunction^,OldCode,5);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{设置为还没有开始HOOK}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AlreadyHook:=false;<br>&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;{如果是改引入表式,将允许HOOK}<br>&nbsp;&nbsp;&nbsp;if&nbsp;not&nbsp;Trap&nbsp;then&nbsp;AllowChange:=true;<br>&nbsp;&nbsp;&nbsp;Change;&nbsp;{开始HOOK}<br>&nbsp;&nbsp;&nbsp;{如果是改引入表式,将暂时不允许HOOK}<br>&nbsp;&nbsp;&nbsp;if&nbsp;not&nbsp;Trap&nbsp;then&nbsp;AllowChange:=false;<br>end;<br><br>{HOOK的出口}<br>constructor&nbsp;THookClass.Destroy;<br>begin<br>&nbsp;&nbsp;&nbsp;{如果是改引入表式,将允许HOOK}<br>&nbsp;&nbsp;&nbsp;if&nbsp;not&nbsp;Trap&nbsp;then&nbsp;AllowChange:=true;<br>&nbsp;&nbsp;&nbsp;Restore;&nbsp;{停止HOOK}<br>&nbsp;&nbsp;&nbsp;if&nbsp;Trap&nbsp;then{如果是陷阱式}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hProcess);<br>end;<br><br>{开始HOOK}<br>procedure&nbsp;THookClass.Change;<br>var<br>&nbsp;&nbsp;&nbsp;nCount:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;BeenDone:&nbsp;TList;<br>begin<br>&nbsp;&nbsp;if&nbsp;Trap&nbsp;then{如果是陷阱式}<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(AlreadyHook)or&nbsp;(hProcess&nbsp;=&nbsp;0)&nbsp;or&nbsp;(OldFunction&nbsp;=&nbsp;nil)&nbsp;or&nbsp;(NewFunction&nbsp;=&nbsp;nil)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br>&nbsp;&nbsp;&nbsp;&nbsp;AlreadyHook:=true;{表示已经HOOK}<br>&nbsp;&nbsp;&nbsp;&nbsp;WriteProcessMemory(hProcess,&nbsp;OldFunction,&nbsp;@(Newcode),&nbsp;5,&nbsp;nCount);<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else&nbsp;begin{如果是改引入表式}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(not&nbsp;AllowChange)or(OldFunction=nil)or(NewFunction=nil)then&nbsp;exit;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BeenDone:=TList.Create;&nbsp;{用于存放当前进程所有DLL模块的名字}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PatchAddressInModule(BeenDone,GetModuleHandle(nil),OldFunction,NewFunction);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BeenDone.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end;<br>end;<br><br>{恢复系统函数的调用}<br>procedure&nbsp;THookClass.Restore;<br>var<br>&nbsp;&nbsp;&nbsp;nCount:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;BeenDone:&nbsp;TList;<br>begin<br>&nbsp;&nbsp;if&nbsp;Trap&nbsp;then{如果是陷阱式}<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(not&nbsp;AlreadyHook)&nbsp;or&nbsp;(hProcess&nbsp;=&nbsp;0)&nbsp;or&nbsp;(OldFunction&nbsp;=&nbsp;nil)&nbsp;or&nbsp;(NewFunction&nbsp;=&nbsp;nil)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br>&nbsp;&nbsp;&nbsp;&nbsp;WriteProcessMemory(hProcess,&nbsp;OldFunction,&nbsp;@(Oldcode),&nbsp;5,&nbsp;nCount);<br>&nbsp;&nbsp;&nbsp;&nbsp;AlreadyHook:=false;{表示退出HOOK}<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else&nbsp;begin{如果是改引入表式}<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(not&nbsp;AllowChange)or(OldFunction=nil)or(NewFunction=nil)then&nbsp;exit;<br>&nbsp;&nbsp;&nbsp;&nbsp;BeenDone:=TList.Create;{用于存放当前进程所有DLL模块的名字}<br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PatchAddressInModule(BeenDone,GetModuleHandle(nil),NewFunction,OldFunction);<br>&nbsp;&nbsp;&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BeenDone.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end;<br>end;<br><br>end.
 
暗夜中独舞<br>谢谢!你这个是专门在2000下的吧!<br>能写个xp下简单的代码吗?
 
unit&nbsp;UnitHookType;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;windows,&nbsp;messages,winsock;<br><br>const<br>&nbsp;&nbsp;&nbsp;&nbsp;MaxStringLen&nbsp;=&nbsp;100;<br>&nbsp;&nbsp;&nbsp;&nbsp;WM_CALLHOOKAPI&nbsp;=&nbsp;WM_USER&nbsp;+&nbsp;1139;<br>&nbsp;&nbsp;&nbsp;&nbsp;FN_SEND=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;FN_RECV=2;<br>&nbsp;&nbsp;&nbsp;&nbsp;MappingFileName&nbsp;=&nbsp;'Hook&nbsp;API&nbsp;for&nbsp;NT&nbsp;2000&nbsp;XP';<br>type<br>&nbsp;&nbsp;&nbsp;&nbsp;TShareMem&nbsp;=&nbsp;packed&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hProcWnd:&nbsp;HWND;&nbsp;{主应用窗口句柄}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RemoteAddr:TSockAddr;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LocalAddr:TSockAddr;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;PShareMem&nbsp;=&nbsp;^TShareMem;<br><br>implementation<br><br>end.
 
这个XP下也可以用&nbsp;我就是XP下调试过的
 
//EXE的主要代码<br>//如果有需要我把EXE的部分也全贴出来<br><br>procedure&nbsp;TForm1.WMCopyData(var&nbsp;Msg:&nbsp;TWMCopyData);<br>var<br>&nbsp;&nbsp;S:&nbsp;string;<br>begin<br>&nbsp;&nbsp;SetLength(S,&nbsp;Msg.CopyDataStruct.cbData);<br>&nbsp;&nbsp;CopyMemory(@S[1],&nbsp;Msg.CopyDataStruct.lpData,&nbsp;Msg.CopyDataStruct.cbData);<br>&nbsp;&nbsp;buttondown(S);<br><br>&nbsp;&nbsp;inherited;<br>end;<br><br>//把提交的头给你,你自己处理<br>procedure&nbsp;TForm1.buttondown(s:string);<br>var<br>&nbsp;&nbsp;List1:TStringList;<br>&nbsp;&nbsp;i:integer;<br>begin<br>&nbsp;&nbsp;List1:=TStringList.Create;<br>&nbsp;&nbsp;List1.Text:=s;<br>&nbsp;&nbsp;for&nbsp;i:=0&nbsp;to&nbsp;List1.Count-1&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ListBox1.Items.Add(list1);<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;List1.Free;<br>end;
 
暗夜中独舞<br>那太好了!<br>我看了一下你使用的了共享内存!你的控制程序的代码能提供吗?<br>或者写个简单的,就把数据内容显示在memo内就好了!<br><br>谢谢谢谢谢谢谢谢谢谢!跪求了
 
//控制程序的代码我是把数据发到TListBox上<br>//你看看吧。。。。。。。。。。。<br><br>unit&nbsp;UnitMain;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;StdCtrls,&nbsp;ExtCtrls,UnitHookType;<br><br>type<br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;Panel1:&nbsp;TPanel;<br>&nbsp;&nbsp;&nbsp;&nbsp;ListBox1:&nbsp;TListBox;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button1:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit1:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit2:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit3:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit4:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit5:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit6:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit7:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit8:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button2:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button3:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;FormCreate(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;FormClose(Sender:&nbsp;TObject;&nbsp;var&nbsp;Action:&nbsp;TCloseAction);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button2Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button3Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;hMapObj&nbsp;:&nbsp;THandle;<br>&nbsp;&nbsp;&nbsp;&nbsp;pShMem&nbsp;:&nbsp;PShareMem;<br>&nbsp;&nbsp;&nbsp;&nbsp;fWndClosed:boolean;{是否正在退出主程序}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;WMCopyData(var&nbsp;Msg:&nbsp;TWMCopyData);&nbsp;message&nbsp;WM_COPYDATA;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;WMCallHookAPI(var&nbsp;Msg:TMessage);message&nbsp;WM_CALLHOOKAPI;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;buttondown(s:string);<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br><br>&nbsp;&nbsp;procedure&nbsp;StartHook;&nbsp;stdcall;&nbsp;external&nbsp;'HookDll.dll';<br>&nbsp;&nbsp;procedure&nbsp;StopHook;&nbsp;stdcall;&nbsp;external&nbsp;'HookDll.dll';<br><br>implementation<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;TForm1.WMCallHookAPI(var&nbsp;Msg:TMessage);<br>begin<br>&nbsp;&nbsp;if&nbsp;Msg.WParam=FN_RECV&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ListBox1.Items.Add('接受:')<br>&nbsp;&nbsp;else&nbsp;if&nbsp;Msg.WParam=FN_SEND&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ListBox1.Items.Add('发送');<br>&nbsp;&nbsp;Edit1.Text:=IntToStr(pShMem^.RemoteAddr.sin_port);<br>&nbsp;&nbsp;Edit2.Text:=IntToStr(pShMem^.LocalAddr.sin_family);<br>&nbsp;&nbsp;//Edit3.Text:=IntToStr(pShMem^.RemoteAddr.sin_addr);<br>&nbsp;&nbsp;Edit4.Text:=IntToStr(pShMem^.RemoteAddr.sa_family);<br>&nbsp;&nbsp;Edit6.Text:=IntToStr(pShMem^.LocalAddr.sin_family);<br>&nbsp;&nbsp;Edit5.Text:=IntToStr(pShMem^.LocalAddr.sin_port);<br>&nbsp;&nbsp;//Edit7.Text:=IntToStr(pShMem^.LocalAddr.sin_addr);<br>&nbsp;&nbsp;Edit8.Text:=IntToStr(pShMem^.LocalAddr.sa_family);<br>end;<br><br>procedure&nbsp;TForm1.WMCopyData(var&nbsp;Msg:&nbsp;TWMCopyData);<br>var<br>&nbsp;&nbsp;S:&nbsp;string;<br>begin<br>&nbsp;&nbsp;SetLength(S,&nbsp;Msg.CopyDataStruct.cbData);<br>&nbsp;&nbsp;CopyMemory(@S[1],&nbsp;Msg.CopyDataStruct.lpData,&nbsp;Msg.CopyDataStruct.cbData);<br>&nbsp;&nbsp;buttondown(S);<br><br>&nbsp;&nbsp;inherited;<br>end;<br><br>//把提交的头给你,你自己处理<br>procedure&nbsp;TForm1.buttondown(s:string);<br>var<br>&nbsp;&nbsp;List1:TStringList;<br>&nbsp;&nbsp;i:integer;<br>begin<br>&nbsp;&nbsp;List1:=TStringList.Create;<br>&nbsp;&nbsp;List1.Text:=s;<br>&nbsp;&nbsp;for&nbsp;i:=0&nbsp;to&nbsp;List1.Count-1&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ListBox1.Items.Add(list1);<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;List1.Free;<br>end;<br><br>procedure&nbsp;TForm1.FormCreate(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;hMapObj&nbsp;:=&nbsp;OpenFileMapping(FILE_MAP_WRITE,{获取完全访问映射文件}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;False,{不可继承的}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LPCTSTR(MappingFileName));{映射文件名字}<br>&nbsp;&nbsp;{if&nbsp;hMapObj&nbsp;=&nbsp;0&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;hMapObj&nbsp;:=&nbsp;CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShareMem),MappingFileName)<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;if&nbsp;hMapObj=0&nbsp;then&nbsp;Exception.Create('不能建立共享内存!');}<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;hMapObj&nbsp;=&nbsp;0&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage('不能定位内存映射文件块!');<br>&nbsp;&nbsp;&nbsp;&nbsp;Halt;<br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;pShMem&nbsp;:=&nbsp;MapViewOfFile(hMapObj,FILE_MAP_WRITE,0,0,0);<br>&nbsp;&nbsp;if&nbsp;pShMem&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage('映射文件错误'+&nbsp;IntToStr(GetLastError));<br>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hMapObj);<br>&nbsp;&nbsp;&nbsp;&nbsp;Halt;<br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;FillChar(pShMem^,&nbsp;SizeOf(TShareMem),&nbsp;0);<br>&nbsp;&nbsp;pShMem^.hProcWnd&nbsp;:=&nbsp;Self.Handle;<br>&nbsp;&nbsp;fWndClosed:=false;<br>end;<br><br>procedure&nbsp;TForm1.FormClose(Sender:&nbsp;TObject;&nbsp;var&nbsp;Action:&nbsp;TCloseAction);<br>begin<br>&nbsp;&nbsp;fWndClosed&nbsp;:=&nbsp;True;{正在退出主程序}<br>&nbsp;&nbsp;if&nbsp;Button1.caption&lt;&gt;'开始'&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Button1.Click;<br>end;<br><br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;if&nbsp;Button1.caption='开始'&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartHook;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Button1.caption:='停止';<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StopHook;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Button1.caption:='开始';<br>&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;TForm1.Button2Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;ListBox1.Clear;<br>end;<br><br>procedure&nbsp;TForm1.Button3Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;ListBox1.Items.SaveToFile(ExtractFilePath(Application.ExeName)+'result.txt');<br>end;<br><br>end.
 
可以给您的QQ或者EMAIL吗?有机会向多想您请教
 
这是用hook技术来实现的,我只把数据包拦截下来&nbsp;<br>但是不会分析<br>有机会我们再一起讨论下如何分析
 
我的QQ:66054635<br>Email:chenyi.mailbox@163.com
 
接受答案了.
 

Similar threads

后退
顶部