请教一个关于 API 不能正确 Hook 的问题。 ( 积分: 100 )

  • 主题发起人 主题发起人 yanghaijun
  • 开始时间 开始时间
Y

yanghaijun

Unregistered / Unconfirmed
GUEST, unregistred user!
我用“Delphi 深入核心编程”中所讲的方法写了个 API Hook 程序,但令我不明白的是,在 cmd.exe 中运行命令(诸如:dir *.* &gt; temp.txt),我却无法勾到 CreateFileW 、<br>MoveFileW 之类,当然在其它一般的使用时是可以勾到的,请问这是何原因。
 
我用“Delphi 深入核心编程”中所讲的方法写了个 API Hook 程序,但令我不明白的是,在 cmd.exe 中运行命令(诸如:dir *.* &gt; temp.txt),我却无法勾到 CreateFileW 、<br>MoveFileW 之类,当然在其它一般的使用时是可以勾到的,请问这是何原因。
 
在CMD.EXE中,这时只能勾到:<br>CreateFileA,MoveFileA等
 
是的.做不到.如果要完美的話,得寫驅動.<br>我也不確定原因,不知是沒法得到輸入表,還是SetWindowsHookEx没法进入到在控制台中运行的程序。自己做个实验试试看。
 
我用 API Hook SDK 是可以勾到的(不是驱动,与该书的方法同理),可惜无法得到该 SDK 的源码,并且启动比较慢,有些时候也勾不上。书中方法是不是只能勾住进程而勾不住进程产生的线程?
 
对了,对了!
 
请问那本书在那有下载
 
to 无泪:<br>  有何指教?
 
2k + sp4测试通过。<br>因为看到你的帖子才作的这个东西,所以代码很乱,许多东西都是从以前的代码东拼西凑出来的,有很大的优化空间。<br>因为你会做这类的东西,所以也没写注释,凑活看吧。<br>下面代码还不能hook仅导出Ordinal的函数,需要的话自己加吧,位置已经留出来了。<br>==============Unit1.pas(Hookdll Loader)==============<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton; &nbsp; &nbsp;// only a button<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp;public<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>const<br> &nbsp;VictimExe = 'c:/winnt/system32/cmd.exe';//'C:/Program Files/Borland/Delphi6/Projects/APIHOOK/test/test.exe';<br> &nbsp;HookDll = 'NewHook.dll';<br><br>var<br> &nbsp;si: STARTUPINFO;<br> &nbsp;pi: PROCESS_INFORMATION;<br><br>function EnabledDebugPrivilege(const bEnabled: Boolean):Boolean;<br>var<br> &nbsp;hToken: THandle;<br> &nbsp;tp: TOKEN_PRIVILEGES;<br> &nbsp;a: DWORD;<br>const<br> &nbsp;SE_DEBUG_NAME = 'SeDebugPrivilege';<br>begin<br> &nbsp;Result:=False;<br> &nbsp;if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken)) then<br> &nbsp;begin<br> &nbsp; &nbsp;tp.PrivilegeCount :=1;<br> &nbsp; &nbsp;LookupPrivilegeValue(nil,SE_DEBUG_NAME ,tp.Privileges[0].Luid);<br> &nbsp; &nbsp;if bEnabled then<br> &nbsp; &nbsp; &nbsp;tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;tp.Privileges[0].Attributes := 0;<br> &nbsp; &nbsp;a:=0;<br> &nbsp; &nbsp;AdjustTokenPrivileges(hToken,False,tp,SizeOf(tp),nil,a);<br> &nbsp; &nbsp;Result:= GetLastError = ERROR_SUCCESS;<br> &nbsp; &nbsp;CloseHandle(hToken);<br> &nbsp;end;<br>end;<br><br>function InjectDll(hProc:Cardinal; Dll:string):Cardinal;<br>var<br> &nbsp;wDllPath:PwideChar;<br> &nbsp;pRemote:Pointer;<br> &nbsp;cbSize:cardinal;<br> &nbsp;TempVar:Cardinal;<br>begin<br> &nbsp;result:=0;<br> &nbsp;if hProc=0 then exit;<br> &nbsp;EnabledDebugPrivilege(true);<br> &nbsp;cbSize:= length(Dll) * 2 + 21;<br> &nbsp;GetMem(wDllPath,cbSize);<br> &nbsp;StringToWideChar(Dll,wDllPath,cbSize);<br> &nbsp;try<br> &nbsp; &nbsp;pRemote:=VirtualAllocEx( hProc, nil, cbSize, MEM_COMMIT, PAGE_READWRITE);<br> &nbsp; &nbsp;if WriteProcessMemory(hProc,pRemote, wDllPath, cbSize, TempVar) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;TempVar:=0;<br> &nbsp; &nbsp; &nbsp;Result := CreateRemoteThread(hProc, nil, 0,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetProcAddress(GetModuleHandle('Kernel32'),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'LoadLibraryW'), pRemote, 0, TempVar);<br> &nbsp; &nbsp; &nbsp;VirtualFreeEx(hProc, pRemote, 0, MEM_DECOMMIT or MEM_RELEASE);<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;FreeMem(wDllPath);<br> &nbsp;end;<br>end;<br><br>procedure CreateVictimProcess(Path: String);<br>begin &nbsp; &nbsp;<br> &nbsp;ZeroMemory(@si, sizeof(STARTUPINFO));<br> &nbsp;si.cb := sizeof(STARTUPINFO);<br> &nbsp;if not CreateProcess(PChar(Path), nil, nil, nil, False, CREATE_SUSPENDED or CREATE_DEFAULT_ERROR_MODE, nil, nil, si, pi) then<br> &nbsp;begin<br> &nbsp; &nbsp;ShowMessage('CreateProcess failed!');<br> &nbsp; &nbsp;exit;<br> &nbsp;end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;CreateVictimProcess(VictimExe);<br> &nbsp;InjectDll(pi.hProcess, HookDll);<br> &nbsp;Sleep(20);<br> &nbsp;ResumeThread(pi.hThread);<br>end;<br><br>end.<br>============NewHook.dpr(NewHook.dll)=============<br>library NewHook;<br><br>uses<br> &nbsp;JwaWinType,<br> &nbsp;JwaWinNt,<br> &nbsp;JwaWinUser,<br> &nbsp;JwaWinBase,<br> &nbsp;JwaWinGdi,<br> &nbsp;Classes,<br> &nbsp;SysUtils;<br><br>const<br> &nbsp;Func2Hook = 'CreateFileW';<br> &nbsp;FuncInDll = 'Kernel32.dll';<br><br>var<br> &nbsp;pFunc: Pointer = nil;<br> &nbsp;pOriginFunc: Pointer;<br><br>function NewCreateFileW(lpFileName: LPCWSTR; dwDesiredAccess, dwShareMode: DWORD;<br> &nbsp;lpSecurityAttributes: LPSECURITY_ATTRIBUTES; dwCreationDisposition: DWORD;<br> &nbsp;dwFlagsAndAttributes: DWORD; hTemplateFile: HANDLE): HANDLE; stdcall;<br>var<br>oc: function (lpFileName: LPCWSTR; dwDesiredAccess, dwShareMode: DWORD;<br> &nbsp;lpSecurityAttributes: LPSECURITY_ATTRIBUTES; dwCreationDisposition: DWORD;<br> &nbsp;dwFlagsAndAttributes: DWORD; hTemplateFile: HANDLE): HANDLE; stdcall;<br>dc:hdc;<br>begin<br> &nbsp;dc := getdc(0);<br> &nbsp;textout(dc, 20, 20, 'CreateFileW!', 12);<br> &nbsp;releasedc(0, dc);<br> &nbsp;oc := pOriginFunc;<br> &nbsp;oc(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition,<br> &nbsp;dwFlagsAndAttributes, hTemplateFile);<br>end;<br><br>function NewMessageBoxA(AWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;<br>var<br> &nbsp;ofunc: function (AWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;<br>begin<br> &nbsp;ofunc := pOriginFunc;<br> &nbsp;result := ofunc(AWnd, PChar('Hooked!!! ' + lpText), lpCaption, uType);<br>end;<br><br>function FuncInImage: Cardinal; &nbsp;//Return ImageBase<br>var<br> &nbsp;peb, ldr, flink, p, bs,ep:PDWORD;<br>begin<br> &nbsp;result := 0;<br> &nbsp;asm<br> &nbsp; &nbsp;mov eax,fs:[$30]<br> &nbsp; &nbsp;mov peb,eax<br> &nbsp;end;<br> &nbsp;ldr:=pointer(dword(pointer(dword(peb)+12)^));<br> &nbsp;flink:=pointer(dword(pointer(dword(ldr)+12)^));<br> &nbsp;p:=flink;<br> &nbsp;repeat<br> &nbsp; &nbsp;bs:=pointer(pointer(dword(p)+$18)^); &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp;Base address<br> &nbsp; &nbsp;ep:=pointer(pointer(dword(p)+$20)^); &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp;Size of image<br> &nbsp; &nbsp;if (DWORD(pFunc) &gt; DWORD(bs)) and (DWORD(pFunc) &lt; DWORD(bs) + DWORD(ep)) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;result := DWORD(bs);<br> &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;p:=pointer(dword(p^));<br> &nbsp;until dword(flink)=dword(p^);<br>end;<br><br>procedure FixExport(ImageBase: Cardinal);<br>var<br> &nbsp;PFileHeader: PImageFileHeader;<br> &nbsp;POptionalHeader32: PImageOptionalHeader32;<br> &nbsp;PExportDirectory: PImageExportDirectory;<br> &nbsp;PDataDirectory: PImageDataDirectory;<br><br> &nbsp;ExpCount: integer;<br> &nbsp;pAddr: PDWORD;<br> &nbsp;pName0, pName1: PDWORD;<br> &nbsp;pOrdinal0, pOrdinal1: PWORD;<br> &nbsp;ExpFound: Boolean;<br> &nbsp;i, j: Cardinal;<br>begin<br> &nbsp;PFileHeader := PImageFileHeader(ImageBase + PImageDosHeader(ImageBase)^.e_lfanew + 4);<br> &nbsp;POptionalHeader32 := PImageOptionalHeader32(DWORD(PFileHeader) + IMAGE_SIZEOF_FILE_HEADER);<br> &nbsp;PDataDirectory := PImageDataDirectory(@POptionalHeader32^.DataDirectory[0]);<br> &nbsp;inc(PDataDirectory, IMAGE_DIRECTORY_ENTRY_EXPORT);<br> &nbsp;PExportDirectory := PImageExportDirectory(ImageBase +PDataDirectory^.VirtualAddress);<br><br> &nbsp;ExpCount := PExportDirectory^.NumberOfFunctions;<br> &nbsp;pAddr := Pointer(ImageBase + PExportDirectory^.AddressOfFunctions);<br> &nbsp;pName0 := Pointer(ImageBase + PExportDirectory^.AddressOfNames);<br> &nbsp;pOrdinal0 := Pointer(ImageBase + PExportDirectory^.AddressOfNameOrdinals);<br><br> &nbsp;for i := 0 to ExpCount - 1 do<br> &nbsp;begin<br> &nbsp; &nbsp;if pAddr^ &lt;&gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;ExpFound := false;<br> &nbsp; &nbsp; &nbsp;pName1 := pName0;<br> &nbsp; &nbsp; &nbsp;pOrdinal1 := pOrdinal0;<br> &nbsp; &nbsp; &nbsp;for j := 0 to PExportDirectory^.NumberOfNames - 1 do &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if pOrdinal1^ = i then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if PChar(ImageBase + pName1^) = Func2Hook then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExpFound := true;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pOriginFunc := Pointer(ImageBase + pAddr^);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;inc(pOrdinal1);<br> &nbsp; &nbsp; &nbsp; &nbsp;inc(pName1);<br> &nbsp; &nbsp; &nbsp;end; &nbsp;// for j<br> &nbsp; &nbsp; &nbsp; &nbsp;if ExpFound then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Break;<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmpExp^.Orindal := pOrdinal1^ + ExpPtr^.Base;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br>// &nbsp; &nbsp; &nbsp; &nbsp;else<br>// &nbsp; &nbsp; &nbsp; &nbsp;tmpExp^.Orindal := i + ExpPtr^.Base;<br><br> &nbsp; &nbsp;end; &nbsp;//if pAddr^ &lt;&gt; 0<br> &nbsp; &nbsp;inc(pAddr);<br> &nbsp;end; &nbsp;// for i<br> &nbsp;virtualprotect(pAddr, 4, $40, @i);<br> &nbsp;pAddr^ := DWORD(@NewCreateFileW) - ImageBase;<br>end;<br><br>function FuncUsed(ImageBase: Cardinal): Pointer;<br>var<br> &nbsp;PFileHeader: PImageFileHeader;<br> &nbsp;POptionalHeader32: PImageOptionalHeader32;<br> &nbsp;PImportDecriptor: PImageImportDecriptor;<br> &nbsp;PDataDirectory: PImageDataDirectory;<br> &nbsp;i, j: integer;<br>begin<br> &nbsp;result := nil;<br> &nbsp;PFileHeader := PImageFileHeader(ImageBase + PImageDosHeader(ImageBase)^.e_lfanew + 4);<br> &nbsp;POptionalHeader32 := PImageOptionalHeader32(DWORD(PFileHeader) + IMAGE_SIZEOF_FILE_HEADER);<br> &nbsp;PDataDirectory := PImageDataDirectory(@POptionalHeader32^.DataDirectory[0]);<br> &nbsp;inc(PDataDirectory, IMAGE_DIRECTORY_ENTRY_IMPORT);<br> &nbsp;if PDataDirectory^.Size = 0 then<br> &nbsp; &nbsp;exit;<br> &nbsp;PImportDecriptor := PImageImportDecriptor(ImageBase +PDataDirectory^.VirtualAddress);<br><br> &nbsp;while (PImportDecriptor^.Union.OriginalFirstThunk &lt;&gt; 0) or (PImportDecriptor^.TimeDateStamp &lt;&gt; 0) or<br> &nbsp; &nbsp;(PImportDecriptor^.ForwarderChain &lt;&gt; 0) or (PImportDecriptor^.Name &lt;&gt; 0) or (PImportDecriptor^.FirstThunk &lt;&gt; 0) do<br> &nbsp;begin<br> &nbsp; &nbsp;if CompareText(PChar(ImageBase + PImportDecriptor^.Name), FuncInDll)=0 then<br> &nbsp; &nbsp;begin<br>// &nbsp; &nbsp; &nbsp;if PImportDecriptor^.Union.OriginalFirstThunk &lt;&gt; 0 then<br>// &nbsp; &nbsp; &nbsp; j := ImageBase + PImportDecriptor^.Union.OriginalFirstThunk<br>// &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;j := ImageBase + PImportDecriptor^.FirstThunk;<br> &nbsp; &nbsp; &nbsp;while PDWORD(j)^ &lt;&gt; 0 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if PDWORD(j)^ and IMAGE_ORDINAL_FLAG32 = 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if PDWORD(j)^ = DWORD(pOriginFunc) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;asm int 3 end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;virtualprotect(Pointer(j), 4, $40, @i);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PDWORD(j)^ := &nbsp;DWORD(@NewCreateFileW);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// &nbsp;Ordinal only support here<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;inc(j, 4);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;inc(PImportDecriptor);<br> &nbsp;end;<br>end;<br><br>procedure FixImport;<br>var<br> &nbsp;peb, ldr, flink, p, bs:PDWORD;<br>begin<br> &nbsp;asm<br> &nbsp; &nbsp;mov eax,fs:[$30]<br> &nbsp; &nbsp;mov peb,eax<br> &nbsp;end;<br> &nbsp;ldr:=pointer(dword(pointer(dword(peb)+12)^));<br> &nbsp;flink:=pointer(dword(pointer(dword(ldr)+12)^));<br> &nbsp;p:=flink;<br> &nbsp;repeat<br> &nbsp; &nbsp;bs:=pointer(pointer(dword(p)+$18)^); &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp;Base address<br> &nbsp; &nbsp;FuncUsed(DWORD(bs));<br> &nbsp; &nbsp;p:=pointer(dword(p^));<br> &nbsp;until dword(flink)=dword(p^);<br>end;<br><br>procedure EntryPointProc(Reason: Integer);<br>var<br> &nbsp;ExpImageBase: Cardinal;<br>begin &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp;if Reason &lt;&gt; DLL_PROCESS_ATTACH then<br> &nbsp; &nbsp;exit;<br> &nbsp;pFunc := GetProcAddress(GetModuleHandle(FuncInDll), Func2Hook);<br> &nbsp;ExpImageBase := FuncInImage;<br> &nbsp;if ExpImageBase &lt;&gt; GetModuleHandle(nil) then<br> &nbsp; &nbsp;FixExport(ExpImageBase);<br> &nbsp;FixImport;<br>end;<br><br>begin &nbsp; &nbsp; <br> &nbsp;DllProc := @EntryPointProc;<br> &nbsp;EntryPointProc(DLL_PROCESS_ATTACH);<br>end.
 
谢谢先,我试试看
 
这个东西现在还有问题,我还要再改改
 
改进完成,试试这个<br>//------------------------------<br>object Form1: TForm1<br> &nbsp;Left = 195<br> &nbsp;Top = 109<br> &nbsp;Width = 248<br> &nbsp;Height = 167<br> &nbsp;Caption = 'Form1'<br> &nbsp;Color = clBtnFace<br> &nbsp;Font.Charset = DEFAULT_CHARSET<br> &nbsp;Font.Color = clWindowText<br> &nbsp;Font.Height = -11<br> &nbsp;Font.Name = 'MS Sans Serif'<br> &nbsp;Font.Style = []<br> &nbsp;OldCreateOrder = False<br> &nbsp;PixelsPerInch = 96<br> &nbsp;TextHeight = 13<br> &nbsp;object Button1: TButton<br> &nbsp; &nbsp;Left = 48<br> &nbsp; &nbsp;Top = 72<br> &nbsp; &nbsp;Width = 145<br> &nbsp; &nbsp;Height = 57<br> &nbsp; &nbsp;Caption = 'Button1'<br> &nbsp; &nbsp;TabOrder = 0<br> &nbsp; &nbsp;OnClick = Button1Click<br> &nbsp;end<br> &nbsp;object RadioButton1: TRadioButton<br> &nbsp; &nbsp;Left = 64<br> &nbsp; &nbsp;Top = 8<br> &nbsp; &nbsp;Width = 113<br> &nbsp; &nbsp;Height = 17<br> &nbsp; &nbsp;Caption = 'cmd.exe'<br> &nbsp; &nbsp;Checked = True<br> &nbsp; &nbsp;TabOrder = 1<br> &nbsp; &nbsp;TabStop = True<br> &nbsp;end<br> &nbsp;object RadioButton2: TRadioButton<br> &nbsp; &nbsp;Left = 64<br> &nbsp; &nbsp;Top = 40<br> &nbsp; &nbsp;Width = 113<br> &nbsp; &nbsp;Height = 17<br> &nbsp; &nbsp;Caption = 'fsg.exe'<br> &nbsp; &nbsp;TabOrder = 2<br> &nbsp;end<br>end<br>//-----------------------------<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;RadioButton1: TRadioButton;<br> &nbsp; &nbsp;RadioButton2: TRadioButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp;public<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>const<br> &nbsp;HookDll = 'C:/Program Files/Borland/Delphi6/Projects/APIHOOK/HookDll/HookDll.dll';<br><br>var<br> &nbsp;si: STARTUPINFO;<br> &nbsp;pi: PROCESS_INFORMATION;<br><br>function EnabledDebugPrivilege(const bEnabled: Boolean):Boolean;<br>var<br> &nbsp;hToken: THandle;<br> &nbsp;tp: TOKEN_PRIVILEGES;<br> &nbsp;a: DWORD;<br>const<br> &nbsp;SE_DEBUG_NAME = 'SeDebugPrivilege';<br>begin<br> &nbsp;Result:=False;<br> &nbsp;if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken)) then<br> &nbsp;begin<br> &nbsp; &nbsp;tp.PrivilegeCount :=1;<br> &nbsp; &nbsp;LookupPrivilegeValue(nil,SE_DEBUG_NAME ,tp.Privileges[0].Luid);<br> &nbsp; &nbsp;if bEnabled then<br> &nbsp; &nbsp; &nbsp;tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;tp.Privileges[0].Attributes := 0;<br> &nbsp; &nbsp;a:=0;<br> &nbsp; &nbsp;AdjustTokenPrivileges(hToken,False,tp,SizeOf(tp),nil,a);<br> &nbsp; &nbsp;Result:= GetLastError = ERROR_SUCCESS;<br> &nbsp; &nbsp;CloseHandle(hToken);<br> &nbsp;end;<br>end;<br><br>function InjectDll(hProc:Cardinal; Dll:string):Cardinal;<br>var<br> &nbsp;wDllPath:PwideChar;<br> &nbsp;pRemote:Pointer;<br> &nbsp;cbSize:cardinal;<br> &nbsp;TempVar:Cardinal;<br>begin<br> &nbsp;result:=0;<br> &nbsp;if hProc=0 then exit;<br> &nbsp;EnabledDebugPrivilege(true);<br> &nbsp;cbSize:= length(Dll) * 2 + 21;<br> &nbsp;GetMem(wDllPath,cbSize);<br> &nbsp;StringToWideChar(Dll,wDllPath,cbSize);<br> &nbsp;try<br> &nbsp; &nbsp;pRemote:=VirtualAllocEx( hProc, nil, cbSize, MEM_COMMIT, PAGE_READWRITE);<br> &nbsp; &nbsp;if WriteProcessMemory(hProc,pRemote, wDllPath, cbSize, TempVar) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;TempVar:=0;<br> &nbsp; &nbsp; &nbsp;Result := CreateRemoteThread(hProc, nil, 0,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetProcAddress(GetModuleHandle('Kernel32'),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'LoadLibraryW'), pRemote, 0, TempVar);<br> &nbsp; &nbsp; &nbsp;VirtualFreeEx(hProc, pRemote, 0, MEM_DECOMMIT or MEM_RELEASE);<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;FreeMem(wDllPath);<br> &nbsp;end;<br>end;<br><br>procedure CreateVictimProcess(Path: String);<br>begin &nbsp; &nbsp;<br> &nbsp;ZeroMemory(@si, sizeof(STARTUPINFO));<br> &nbsp;si.cb := sizeof(STARTUPINFO);<br> &nbsp;if not CreateProcess(PChar(Path), nil, nil, nil, False, CREATE_SUSPENDED or CREATE_DEFAULT_ERROR_MODE, nil,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PChar(ExtractFilePath(Path)), si, pi) then<br> &nbsp;begin<br> &nbsp; &nbsp;ShowMessage('CreateProcess failed! ' + syserrormessage(getlasterror));<br> &nbsp; &nbsp;exit;<br> &nbsp;end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;VictimExe: string;<br>begin<br> &nbsp;if RadioButton1.Checked then<br> &nbsp; &nbsp;VictimExe := 'c:/winnt/system32/cmd.exe'<br> &nbsp;else<br> &nbsp; &nbsp;VictimExe := 'E:/CB/fsg/fsg.exe';<br> &nbsp;CreateVictimProcess(VictimExe);<br> &nbsp;InjectDll(pi.hProcess, HookDll);<br> &nbsp;Sleep(20);<br> &nbsp;ResumeThread(pi.hThread);<br>end;<br><br>end.<br>//-----------------------------<br>library NewHook;<br><br>uses<br> &nbsp;{Jedi header}<br> &nbsp;JwaWinType,<br> &nbsp;JwaWinNt,<br> &nbsp;JwaWinUser,<br> &nbsp;JwaWinBase,<br> &nbsp;JwaWinGdi,<br> &nbsp;JwaWinNLS;<br><br>const<br> &nbsp;Func2Hook: PChar = 'CreateFileW';<br> &nbsp;FuncInDll: PChar = 'Kernel32.dll';<br><br>var<br> &nbsp;pOriginFunc: Pointer = nil;<br> &nbsp;pNewFunc: Pointer = nil;<br> &nbsp;DllHash: Cardinal = 0;<br> &nbsp;MainProcImageBase: Cardinal = 0;<br> &nbsp;MainProcImageSize: Cardinal = 0; &nbsp; &nbsp;<br><br>function NewCreateFileW(lpFileName: LPCWSTR; dwDesiredAccess, dwShareMode: DWORD;<br> &nbsp;lpSecurityAttributes: LPSECURITY_ATTRIBUTES; dwCreationDisposition: DWORD;<br> &nbsp;dwFlagsAndAttributes: DWORD; hTemplateFile: HANDLE): HANDLE; stdcall;<br>var<br> &nbsp;oc: function (lpFileName: LPCWSTR; dwDesiredAccess, dwShareMode: DWORD;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpSecurityAttributes: LPSECURITY_ATTRIBUTES; dwCreationDisposition: DWORD;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dwFlagsAndAttributes: DWORD; hTemplateFile: HANDLE): HANDLE; stdcall;<br> &nbsp;dc:hdc;<br> &nbsp;Path: PChar;<br> &nbsp;lPath: Cardinal;<br> &nbsp;FileName: String;<br> &nbsp;RtnAddress: DWORD;<br>begin<br> &nbsp;asm<br> &nbsp; &nbsp;mov eax, [esp + $C] &nbsp; //overleap 3 &quot;push&quot;, generated by compiler<br> &nbsp; &nbsp;mov RtnAddress, eax<br> &nbsp;end;<br>// &nbsp;if (RtnAddress &gt;= MainProcImageBase) and (RtnAddress &lt;= MainProcImageBase + MainProcImageSize) then<br> &nbsp;begin<br> &nbsp; &nbsp;{the call is from the main process }<br> &nbsp; &nbsp;dc := getdc(0);<br> &nbsp; &nbsp;textout(dc, 10, 20, PChar(Func2Hook + ' detected!'), length(Func2Hook) + 10); &nbsp; //show we did it!<br> &nbsp; &nbsp;releasedc(0, dc);<br> &nbsp;end;<br> &nbsp;lPath := GetCurrentDirectory(0, nil);<br> &nbsp;GetMem(Path, lPath);<br> &nbsp;ZeroMemory(Path, lPath);<br> &nbsp;GetCurrentDirectory(lPath, Path);<br> &nbsp;FileName := WideCharToString(lpFileName);<br> &nbsp;try<br> &nbsp; &nbsp;MessageBox(0, PChar('File path: ' + Path + FileName), 'CreateFileW Notify', MB_OK);<br> &nbsp;finally<br> &nbsp; &nbsp;FreeMem(Path);<br> &nbsp;end;<br> &nbsp;oc := pOriginFunc;<br> &nbsp;result := oc(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwFlagsAndAttributes, hTemplateFile);<br>end;<br><br>(*************************************************)<br>(* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *)<br>(* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hook Process Start Here &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *)<br>(* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *)<br>(*************************************************)<br><br><br>function CalcHash(pText: Pointer): Cardinal;<br>begin<br> &nbsp;result := 0;<br> &nbsp;while PByte(pText)^ &lt;&gt; 0 do<br> &nbsp;begin<br> &nbsp; &nbsp;result := ((result shl 5) or (result shr 15)) + PByte(pText)^;<br> &nbsp; &nbsp;if (PByte(pText)^ &gt;= $61) and (PByte(pText)^ &lt;= $7A) then &nbsp;//UpperCase (Makes hash case insensitive)<br> &nbsp; &nbsp; &nbsp;dec(result, 32);<br> &nbsp; &nbsp;inc(PByte(pText));<br> &nbsp;end;<br>end;<br><br>function GetSectionPtr(ImageBase: Cardinal; SectionIndex: integer): Pointer;<br>var<br> &nbsp;PFileHeader: PImageFileHeader;<br> &nbsp;POptionalHeader32: PImageOptionalHeader32;<br> &nbsp;PDataDirectory: PImageDataDirectory;<br>begin<br> &nbsp;result := nil;<br> &nbsp;PFileHeader := PImageFileHeader(ImageBase + DWORD(PImageDosHeader(ImageBase)^.e_lfanew) + 4);<br> &nbsp;POptionalHeader32 := PImageOptionalHeader32(DWORD(PFileHeader) + IMAGE_SIZEOF_FILE_HEADER);<br> &nbsp;PDataDirectory := PImageDataDirectory(@POptionalHeader32^.DataDirectory[0]);<br> &nbsp;inc(PDataDirectory, SectionIndex); &nbsp; &nbsp;<br> &nbsp;if PDataDirectory^.Size = 0 then<br> &nbsp; &nbsp;exit;<br> &nbsp;result := PImageExportDirectory(ImageBase +PDataDirectory^.VirtualAddress);<br>end;<br><br>procedure FixExport(ImageBase: Cardinal; OriginExpRVA: DWORD; NewExpRVA: DWORD);<br>var<br> &nbsp;PExportDirectory: PImageExportDirectory;<br> &nbsp;pAddr: PDWORD;<br> &nbsp;i, OldProtect: Cardinal;<br>begin<br> &nbsp;PExportDirectory := GetSectionPtr(ImageBase, IMAGE_DIRECTORY_ENTRY_EXPORT);<br> &nbsp;if PExportDirectory = nil then<br> &nbsp; &nbsp;exit;<br><br> &nbsp;pAddr := Pointer(ImageBase + PExportDirectory^.AddressOfFunctions);<br> &nbsp;for i := 0 to PExportDirectory^.NumberOfFunctions - 1 do<br> &nbsp;begin<br> &nbsp; &nbsp;if ImageBase + pAddr^ = OriginExpRVA then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;VirtualProtect(pAddr, SizeOf(DWORD), PAGE_READWRITE, @OldProtect);<br> &nbsp; &nbsp; &nbsp;pAddr^ := NewExpRVA;<br> &nbsp; &nbsp; &nbsp;VirtualProtect(pAddr, SizeOf(DWORD), OldProtect, @OldProtect);<br> &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;inc(pAddr);<br> &nbsp;end; &nbsp;// for i<br>end;<br><br>procedure FixImport(ImageBase: Cardinal; OriginAddr: DWORD; NewAddr: DWORD);<br>var<br> &nbsp;PImportDecriptor: PImageImportDecriptor;<br> &nbsp;TmpVar: DWORD;<br> &nbsp;pThunk: PDWORD;<br>begin<br> &nbsp;PImportDecriptor := GetSectionPtr(ImageBase, IMAGE_DIRECTORY_ENTRY_IMPORT);<br> &nbsp;if PImportDecriptor = nil then<br> &nbsp; &nbsp;exit;<br><br> &nbsp;while PImportDecriptor^.FirstThunk &lt;&gt; 0 do &nbsp;//After loaded, FirstThunk must be valid !<br> &nbsp;begin<br> &nbsp; &nbsp;if CalcHash(PChar(ImageBase + PImportDecriptor^.Name)) = DllHash then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;pThunk := PDWORD(ImageBase + PImportDecriptor^.FirstThunk);<br> &nbsp; &nbsp; &nbsp;while pThunk^ &lt;&gt; 0 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if pThunk^ = OriginAddr then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VirtualProtect(pThunk, 4, $40, @TmpVar);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pThunk^ := NewAddr;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;inc(pThunk);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;inc(PImportDecriptor);<br> &nbsp;end;<br>end;<br><br>procedure DoHook(OldAddr, NewAddr, NewRVA: DWORD);<br>var<br> &nbsp;PEB, Ldr, fLink: DWORD;<br> &nbsp;p: PDWORD;<br> &nbsp;ImageBase, ImageSize: DWORD;<br>begin<br> &nbsp;asm<br> &nbsp; &nbsp;mov eax, fs: [$30]<br> &nbsp; &nbsp;mov PEB, eax<br> &nbsp;end;<br> &nbsp;Ldr := PDWORD(PEB + $C)^;<br> &nbsp;fLink := PDWORD(Ldr + $C)^;<br> &nbsp;p := PDWORD(fLink);<br> &nbsp;repeat<br> &nbsp; &nbsp;ImageBase := PDWORD(DWORD(p) + $18)^; &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp;Base address<br> &nbsp; &nbsp;ImageSize := PDWORD(DWORD(p) + $20)^; &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp;Size of image<br> &nbsp; &nbsp;if ImageBase = GetModuleHandle(nil) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;MainProcImageBase := ImageBase;<br> &nbsp; &nbsp; &nbsp;MainProcImageSize := ImageSize;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;if (DWORD(pOriginFunc) &gt; ImageBase) and (DWORD(pOriginFunc) &lt; ImageBase + ImageSize) then<br> &nbsp; &nbsp; &nbsp;FixExport(ImageBase, OldAddr, NewRVA - ImageBase);<br> &nbsp; &nbsp;FixImport(ImageBase, OldAddr, NewAddr);<br> &nbsp; &nbsp;p := PDWORD(p^);<br> &nbsp;until fLink = p^;<br>end;<br><br>procedure EntryPointProc(Reason: Integer);<br>begin<br> &nbsp;if Reason = DLL_PROCESS_ATTACH then<br> &nbsp;begin<br> &nbsp; &nbsp;DllHash := CalcHash(FuncInDll);<br> &nbsp; &nbsp;pNewFunc := @NewCreateFileW;<br>// &nbsp; &nbsp;pOriginFunc := GetProcAddress(GetModuleHandle(FuncInDll), PChar(MakelParam($3C, 0)));<br>{ &nbsp; &nbsp;GetProcAddress by ordinal. $3C is the ordinal of CreateFileW}<br> &nbsp; &nbsp;pOriginFunc := GetProcAddress(GetModuleHandle(FuncInDll), Func2Hook);<br>{ &nbsp; &nbsp;GetProcAddress by name}<br> &nbsp; &nbsp;DoHook(DWORD(pOriginFunc), DWORD(pNewFunc), $100000000 + DWORD(pNewFunc));<br> &nbsp;end<br> &nbsp;else if Reason = DLL_PROCESS_DETACH then<br> &nbsp;begin<br> &nbsp; &nbsp;//Reset to original state.<br> &nbsp; &nbsp;DoHook(DWORD(pNewFunc), DWORD(pOriginFunc), DWORD(pOriginFunc));<br> &nbsp;end;<br>end;<br><br>begin &nbsp; &nbsp; <br> &nbsp;DllProc := @EntryPointProc;<br> &nbsp;EntryPointProc(DLL_PROCESS_ATTACH);<br>end.
 
uses<br> &nbsp;{Jedi header}<br> &nbsp;JwaWinType,<br> &nbsp;JwaWinNt,<br> &nbsp;JwaWinUser,<br> &nbsp;JwaWinBase,<br> &nbsp;JwaWinGdi,<br> &nbsp;JwaWinNLS;<br><br>这些单元是什么?在哪?
 
http://www.delphi-jedi.org/APILIBRARY:297648<br>ftp://delphi-jedi.org/api/win32api.zip
 
谢谢,好东西
 
楼主哪去了,行不行到时露个面啊
 
这个好象有针对性,如果我想HOOK全局,所有进程好象不行吧
 
确实是有针对性,不过不要指望ring 3级的api hook能有多么好的兼容性,毕竟这是一种不规范的做法。对付普通程序还行,要是对付某些壳就会比较麻烦。
 
你的意思是说,还是用驱动?
 
正如前面所说,具有针对性,因而适应的情况不广
 
后退
顶部