J
jinanifku
Unregistered / Unconfirmed
GUEST, unregistred user!
{<br>制作:深圳方达软件<br>日期:2005-8-22<br>用途:勾子api,检测有新进程启动<br>}<br>unit APIHookProc;<br><br>interface<br><br>uses<br> SysUtils,<br> Windows, WinSock, Dialogs, registry;<br>type<br> SECURITY_ATTRIBUTES=^TSECURITY_ATTRIBUTES;<br> TSECURITY_ATTRIBUTES=record<br> nLengthWORD ;<br> lpSecurityDescriptor:string ;<br> bInheritHandle:BOOL ;<br>end;<br>type<br> STARTUPINFO=^TSTARTUPINFO ;<br> TSTARTUPINFO=record<br> cbWORD ;<br> lpReserved:LPTSTR ;<br> lpDesktop:LPTSTR;<br> lpTitle:LPTSTR;<br> dwXWORD;<br> dwYWORD;<br> dwXSizeWORD;<br> dwYSizeWORD;<br> dwXCountCharsWORD;<br> dwYCountCharsWORD;<br> dwFillAttributeWORD ;<br> dwFlags: DWORD;<br> wShowWindow:WORD;<br> cbReserved2:WORD;<br> lpReserved2Word;<br> hStdInput:tHANDLE ;<br> hStdOutput:tHANDLE ;<br> hStdError:tHANDLE;<br>end;<br>type PROCESS_INFORMATION =^TPROCESS_INFORMATION ;<br> TPROCESS_INFORMATION=record<br> hProcess:tHANDLE;<br> hThread:tHANDLE;<br> dwProcessIdWORD;<br> dwThreadIdWORD;<br>end;<br>type<br> //要HOOK的API函数定义<br> //CreateProcessW<br> Tregproc=Function(lpApplicationNameAnsiChar;lpcommandLineAnsiChar;lpProcessAttributes:SECURITY_ATTRIBUTES;lpThreadAttributes:SECURITY_ATTRIBUTES;bInheritHandles:bool;dwCreationFlags:dword;lpEnvironmentAnsiChar;lpCurrentDirectoryAnsiChar;lpStartupInfo:STARTUPINFO;lpProcessInformationROCESS_INFORMATION):longint;stdcall;<br> PJmpCode = ^TJmpCode;<br> TJmpCode = packed record<br> JmpCode: BYTE;<br> Address: Tregproc;<br> MovEAX: array[0..2] of BYTE;<br> end;<br><br> //--------------------函数声明---------------------------<br>procedure HookAPI;<br>procedure UnHookAPI;<br><br>var<br> Oldreg : Tregproc; //原来的API地址<br> JmpCode : TJmpCode;<br> Oldproc : array[0..1] of TJmpCode;<br> Addreg : Pointer; //API地址<br> TmpJmp : TJmpCode;<br> ProcessHandle : THandle;<br><br>implementation<br>procedure show_numberPassword(v:string);<br>var<br> dc:thandle;<br>begin<br> DC:=GetDC(0);<br> TextOut(DC,100,100,Pchar(V),Length(V));<br> ReleaseDC(0,DC);<br>end;<br>{---------------------------------------}<br>{函数功能:CreateProcessW函数的HOOK<br>{函数参数:同CreateProcessW<br>{函数返回值:integer<br>{---------------------------------------}<br>function Myreg(lpApplicationNameAnsiChar;lpcommandLineAnsiChar;lpProcessAttributes:SECURITY_ATTRIBUTES;lpThreadAttributes:SECURITY_ATTRIBUTES;bInheritHandles:bool;dwCreationFlags:dword;lpEnvironmentAnsiChar;lpCurrentDirectoryAnsiChar;lpStartupInfo:STARTUPINFO;lpProcessInformationROCESS_INFORMATION):longint;stdcall;<br>var<br> dwSize : cardinal;<br>begin<br> //调用直正的CreateProcessW函数<br> result:=1;<br> WriteProcessMemory(ProcessHandle, Addreg, @Oldproc[0], 8, dwSize);<br> //对操作进行处理<br> {<br> }<br> JmpCode.Address := @Myreg;<br> //定义hook的CreateProcessW函数<br> WriteProcessMemory(ProcessHandle, Addreg, @JmpCode, 8, dwSize);<br> result:=0;<br>end;<br><br>{------------------------------------}<br>{过程功能:HookAPI<br>{过程参数:无<br>{------------------------------------}<br>procedure HookAPI;<br>var<br> DLLModule : THandle;<br> dwSize : cardinal;<br>begin<br>try<br> ProcessHandle := GetCurrentProcess;<br> DLLModule := LoadLibrary('kernel32.dll');<br> Addreg := GetProcAddress(DLLModule, 'CreateProcessW');<br> JmpCode.JmpCode :=$B8; //$25FF<br> JmpCode.MovEAX[0] := $FF;<br> JmpCode.MovEAX[1] := $E0;<br> JmpCode.MovEAX[2] := 0;<br> ReadProcessMemory(ProcessHandle, Addreg, @Oldproc[0], 8, dwSize);//8<br> JmpCode.Address := @Myreg;<br> WriteProcessMemory(ProcessHandle, Addreg, @JmpCode, 8, dwSize);//8 //修改CreateProcessW入口<br><br> Oldreg := Addreg;<br> except<br> <br> end;<br>end;<br><br>{------------------------------------}<br>{过程功能:取消HOOKAPI<br>{过程参数:无<br>{------------------------------------}<br>procedure UnHookAPI;<br>var<br> dwSize : cardinal;<br>begin<br> WriteProcessMemory(ProcessHandle, Addreg, @Oldproc[0], 8, dwSize); //8<br>end;<br><br>end.