终于写好了 Hook API 的控件了,发分庆贺~~(200分)

  • 主题发起人 主题发起人 白河愁
  • 开始时间 开始时间
或者你要求用户声明的函数多一个参数,放在最前面或者最后面都行,用户的函数对这个参数进行修改,你根据修改的结果判断是否继续执行被 hook 的函数,或者进行其他操作,岂不是两全其美?
 
祝贺啊,能不能发一份给我学习一下呢?<br><br>394089feng@163.com
 
感兴趣,但是还没有去研究过
 
写个回调函数吧<br><br>我最烦OCX了。
 
dll写了半天搞不好,能不能写个例子,反而ocx快写好了
 
老白,恭喜哦,哈哈,对了,你给我说的加<br>asm<br> &nbsp;int 3<br>end;<br>这个代码加入到dll中,不能调试了。
 
为什么不能调试了 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?
 
xiling_221@126.com 谢谢分享!
 
定义一个回调函数,函数原型声明要写出来,别人才能使用。<br><br>如:<br>TOnAPIHookProc = function HookAPIAPIHookProc(AParam: PDWORD; AParamCount: Integer; AData: Pointer): DWORD; stdcall;<br><br>然后AParam是你的参数,通过指针递增进行访问其它值,AData是调用者自定参数<br>for I := 0 to AParamCount - 1 do<br>begin<br> &nbsp;ShowMessage(IntToStr(AParam^));<br> &nbsp;Inc(AParam);<br>end;<br><br>像你上面的调用:<br> &nbsp;...<br> &nbsp;AA.API.OnAPIHookProc:= @MyAPIHookProc; //API 执行后发生的回调<br> &nbsp;AA.API.Data := SomeData;<br><br>function MyAPIHookProc(AParam: PDWORD; AParamCount: Integer; AData: Pointer): DWORD; stdcall;<br>begin<br> &nbsp;....<br>end;<br><br><br>你原代码使用事件的,调用:<br>AA.API.OnAPIHookProc(...)<br>变成这样:<br>AA.API.OnAPIHookProc(@AParam[1], High(AParam) - Low(AParam), AA.API.Data);<br>大概这样吧
 
AA.API.OnAPIHookProc(@AParam[Low(AParam)], High(AParam) - Low(AParam), AA.API.Data);<br><br>是这样,错了
 
//请白河愁 解释一下这段汇编好吗?<br>procedure ThreadPro;<br>var<br>VarList: TThreadProVarList;<br>begin<br>asm<br>mov eax, $FFFFFFFF {到$FFFFFFFF的偏移是7} &nbsp;<br>mov VarList.SendMessage, eax &nbsp;// 这里是把$FFFFFFFF 赋值给VarList.SendMessage这个变量吗? 为什么要赋这个值啊? <br>mov eax, $FFFFFFFF {这个$FFFFFFFF是在上一个偏移位置加8}<br>mov VarList.WndHandle, eax<br>mov eax, $FFFFFFFF<br>mov VarList.ExitProcess, eax<br>mov eax, $FFFFFFFF<br>mov VarList.ExitThread, eax<br>push 0 &nbsp;//lParam = 0<br>push 0 &nbsp;//Wparam = 0<br>push 4245 {4245就是自定义的WM_HOOKED} <br>push VarList.WndHandle &nbsp;//窗口句柄<br>call VarList.SendMessage &nbsp;//调用发送消息过程<br>push 0 &nbsp;//提供给ExitThread的参数为0 , <br>call VarList.ExitThread <br>end;<br>end;<br>//这句<br>WriteProcessMemory(PHandle, Pointer(LongInt(ThreadAdd)+7), @SendPro, SizeOf(DWORD), WriteCount);<br>//下面不就是对ThreadPro VarList.SendMessage的变量写入了发送消息涵数的地址吗?为什么要开始mov eax, $FFFFFFFF {到$FFFFFFFF的偏移是7} &nbsp;<br>mov VarList.SendMessage, eax &nbsp;// 这里是把$FFFFFFFF 赋值给VarList.SendMessage这个变量吗? 为什么要赋这个值啊? 搞不懂?? <br>TmpHandle := Self.Handle;<br>//这个是写入窗口句柄<br>WriteProcessMemory(PHandle, Pointer(LongInt(ThreadAdd)+15), @TmpHandle, SizeOf(DWORD), WriteCount);<br>//这个是写入ExitProcess<br>WriteProcessMemory(PHandle, Pointer(LongInt(ThreadAdd)+23), @ExitPro, SizeOf(DWORD), WriteCount);<br>//这个是写入ExitThread<br>WriteProcessMemory(PHandle, Pointer(LongInt(ThreadAdd)+31), @ExitTPro, SizeOf(DWORD), WriteCount);
 
谢谢<br>dey-999@163.com
 
{<br> &nbsp;Delphi Hooking Library by Aphex<br> &nbsp;http://www.iamaphex.cjb.net/<br> &nbsp;unremote@knology.net<br>}<br>unit afxCodeHook;<br><br>{$IMAGEBASE $13140000}<br><br>interface<br><br>uses<br> &nbsp;Windows;<br><br>function SizeOfCode(Code: pointer): dword;<br>function SizeOfProc(Proc: pointer): dword;<br><br>function InjectString(Process: LongWord; Text: pchar): pchar;<br>function InjectMemory(Process: LongWord; Memory: pointer; Len: dword): pointer;<br>function InjectThread(Process: dword; Thread: pointer; Info: pointer; InfoLen: dword; Results: boolean): THandle;<br>function InjectLibrary(Process: LongWord; ModulePath: string): boolean; overload;<br>function InjectLibrary(Process: LongWord; Src: pointer): boolean; overload;<br>function InjectExe(Process: LongWord; EntryPoint: pointer): boolean;<br>function UninjectLibrary(Process: LongWord; ModulePath: string): boolean;<br><br>function CreateProcessEx(lpApplicationName: pchar; lpCommandLine: pchar; lpProcessAttributes, lpThreadAttributes: PSecurityAttributes; bInheritHandles: boolean; dwCreationFlags: longword; lpEnvironment: pointer; lpCurrentDirectory: pchar; const lpStartupInfo: TStartupInfo; var lpProcessInformation: TProcessInformation; ModulePath: &nbsp;string): boolean; overload;<br>function CreateProcessEx(lpApplicationName: pchar; lpCommandLine: pchar; lpProcessAttributes, lpThreadAttributes: PSecurityAttributes; bInheritHandles: boolean; dwCreationFlags: longword; lpEnvironment: pointer; lpCurrentDirectory: pchar; const lpStartupInfo: TStartupInfo; var lpProcessInformation: TProcessInformation; Src: pointer): boolean; overload;<br><br>function HookCode(TargetModule, TargetProc: string; NewProc: pointer; var OldProc: pointer): boolean;<br>function UnhookCode(OldProc: pointer): boolean;<br><br>function DeleteFileEx(FilePath: pchar): boolean;<br>function DisableSFC: boolean;<br><br>implementation<br><br>type<br> &nbsp;TModuleList = array of cardinal;<br><br> &nbsp;PImageImportDescriptor = ^TImageImportDescriptor;<br> &nbsp;TImageImportDescriptor = packed record<br> &nbsp; &nbsp;OriginalFirstThunk: longword;<br> &nbsp; &nbsp;TimeDateStamp: longword;<br> &nbsp; &nbsp;ForwarderChain: longword;<br> &nbsp; &nbsp;Name: longword;<br> &nbsp; &nbsp;FirstThunk: longword;<br> &nbsp;end;<br><br> &nbsp;PImageBaseRelocation = ^TImageBaseRelocation;<br> &nbsp;TImageBaseRelocation = packed record<br> &nbsp; &nbsp;VirtualAddress: cardinal;<br> &nbsp; &nbsp;SizeOfBlock: cardinal;<br> &nbsp;end;<br><br> &nbsp;TDllEntryProc = function(hinstDLL: HMODULE; dwReason: longword; lpvReserved: pointer): boolean; stdcall;<br><br> &nbsp;TStringArray = array of string;<br><br> &nbsp;TLibInfo = record<br> &nbsp; &nbsp;ImageBase: pointer;<br> &nbsp; &nbsp;ImageSize: longint;<br> &nbsp; &nbsp;DllProc: TDllEntryProc;<br> &nbsp; &nbsp;DllProcAddress: pointer;<br> &nbsp; &nbsp;LibsUsed: TStringArray;<br> &nbsp;end;<br><br> &nbsp;PLibInfo = ^TLibInfo;<br> &nbsp;Ppointer = ^pointer;<br><br> &nbsp;TSections = array [0..0] of TImageSectionHeader;<br><br>const<br> &nbsp;IMPORTED_NAME_OFFSET = $00000002;<br> &nbsp;IMAGE_ORDINAL_FLAG32 = $80000000;<br> &nbsp;IMAGE_ORDINAL_MASK32 = $0000FFFF;<br><br> &nbsp;Opcodes1: array [0..255] of word =<br> &nbsp;(<br> &nbsp; &nbsp;(16913),(17124),(8209),(8420),(33793),(35906),(0),(0),(16913),(17124),(8209),(8420),(33793),(35906),(0),(0),(16913),<br> &nbsp; &nbsp;(17124),(8209),(8420),(33793),(35906),(0),(0),(16913),(17124),(8209),(8420),(33793),(35906),(0),(0),(16913),<br> &nbsp; &nbsp;(17124),(8209),(8420),(33793),(35906),(0),(32768),(16913),(17124),(8209),(8420),(33793),(35906),(0),(32768),(16913),<br> &nbsp; &nbsp;(17124),(8209),(8420),(33793),(35906),(0),(32768),(529),(740),(17),(228),(1025),(3138),(0),(32768),(24645),<br> &nbsp; &nbsp;(24645),(24645),(24645),(24645),(24645),(24645),(24645),(24645),(24645),(24645),(24645),(24645),(24645),(24645),(24645),(69),<br> &nbsp; &nbsp;(69),(69),(69),(69),(69),(69),(69),(24645),(24645),(24645),(24645),(24645),(24645),(24645),(24645),(0),<br> &nbsp; &nbsp;(32768),(228),(16922),(0),(0),(0),(0),(3072),(11492),(1024),(9444),(0),(0),(0),(0),(5120),<br> &nbsp; &nbsp;(5120),(5120),(5120),(5120),(5120),(5120),(5120),(5120),(5120),(5120),(5120),(5120),(5120),(5120),(5120),(1296),<br> &nbsp; &nbsp;(3488),(1296),(1440),(529),(740),(41489),(41700),(16913),(17124),(8209),(8420),(17123),(8420),(227),(416),(0),<br> &nbsp; &nbsp;(57414),(57414),(57414),(57414),(57414),(57414),(57414),(32768),(0),(0),(0),(0),(0),(0),(32768),(33025),<br> &nbsp; &nbsp;(33090),(769),(834),(0),(0),(0),(0),(1025),(3138),(0),(0),(32768),(32768),(0),(0),(25604),<br> &nbsp; &nbsp;(25604),(25604),(25604),(25604),(25604),(25604),(25604),(27717),(27717),(27717),(27717),(27717),(27717),(27717),(27717),(17680),<br> &nbsp; &nbsp;(17824),(2048),(0),(8420),(8420),(17680),(19872),(0),(0),(2048),(0),(0),(1024),(0),(0),(16656),<br> &nbsp; &nbsp;(16800),(16656),(16800),(33792),(33792),(0),(32768),(8),(8),(8),(8),(8),(8),(8),(8),(5120),<br> &nbsp; &nbsp;(5120),(5120),(5120),(33793),(33858),(1537),(1602),(7168),(7168),(0),(5120),(32775),(32839),(519),(583),(0),<br> &nbsp; &nbsp;(0),(0),(0),(0),(0),(8),(8),(0),(0),(0),(0),(0),(0),(16656),(416)<br> &nbsp;);<br><br> &nbsp;Opcodes2: array [0..255] of word =<br> &nbsp;(<br> &nbsp; &nbsp;(280),(288),(8420),(8420),(65535),(0),(0),(0),(0),(0),(65535),(65535),(65535),(272),(0),(1325),(63),<br> &nbsp; &nbsp;(575),(63),(575),(63),(63),(63),(575),(272),(65535),(65535),(65535),(65535),(65535),(65535),(65535),(16419),<br> &nbsp; &nbsp;(16419),(547),(547),(65535),(65535),(65535),(65535),(63),(575),(47),(575),(61),(61),(63),(63),(0),<br> &nbsp; &nbsp;(32768),(32768),(32768),(0),(0),(65535),(65535),(65535),(65535),(65535),(65535),(65535),(65535),(65535),(65535),(8420),<br> &nbsp; &nbsp;(8420),(8420),(8420),(8420),(8420),(8420),(8420),(8420),(8420),(8420),(8420),(8420),(8420),(8420),(8420),(16935),<br> &nbsp; &nbsp;(63),(63),(63),(63),(63),(63),(63),(63),(63),(63),(63),(63),(63),(63),(63),(237),<br> &nbsp; &nbsp;(237),(237),(237),(237),(237),(237),(237),(237),(237),(237),(237),(237),(237),(101),(237),(1261),<br> &nbsp; &nbsp;(1192),(1192),(1192),(237),(237),(237),(0),(65535),(65535),(65535),(65535),(65535),(65535),(613),(749),(7168),<br> &nbsp; &nbsp;(7168),(7168),(7168),(7168),(7168),(7168),(7168),(7168),(7168),(7168),(7168),(7168),(7168),(7168),(7168),(16656),<br> &nbsp; &nbsp;(16656),(16656),(16656),(16656),(16656),(16656),(16656),(16656),(16656),(16656),(16656),(16656),(16656),(16656),(16656),(0),<br> &nbsp; &nbsp;(0),(32768),(740),(18404),(17380),(49681),(49892),(0),(0),(0),(17124),(18404),(17380),(32),(8420),(49681),<br> &nbsp; &nbsp;(49892),(8420),(17124),(8420),(8932),(8532),(8476),(65535),(65535),(1440),(17124),(8420),(8420),(8532),(8476),(41489),<br> &nbsp; &nbsp;(41700),(1087),(548),(1125),(9388),(1087),(33064),(24581),(24581),(24581),(24581),(24581),(24581),(24581),(24581),(65535),<br> &nbsp; &nbsp;(237),(237),(237),(237),(237),(749),(8364),(237),(237),(237),(237),(237),(237),(237),(237),(237),<br> &nbsp; &nbsp;(237),(237),(237),(237),(237),(63),(749),(237),(237),(237),(237),(237),(237),(237),(237),(65535),<br> &nbsp; &nbsp;(237),(237),(237),(237),(237),(237),(237),(237),(237),(237),(237),(237),(237),(237),(0)<br> &nbsp;);<br><br> &nbsp;Opcodes3: array [0..9] of array [0..15] of word =<br> &nbsp;(<br> &nbsp; &nbsp;((1296),(65535),(16656),(16656),(33040),(33040),(33040),(33040),(1296),(65535),(16656),(16656),(33040),(33040),(33040),(33040)),<br> &nbsp; &nbsp;((3488),(65535),(16800),(16800),(33184),(33184),(33184),(33184),(3488),(65535),(16800),(16800),(33184),(33184),(33184),(33184)),<br> &nbsp; &nbsp;((288),(288),(288),(288),(288),(288),(288),(288),(54),(54),(48),(48),(54),(54),(54),(54)),<br> &nbsp; &nbsp;((288),(65535),(288),(288),(272),(280),(272),(280),(48),(48),(0),(48),(0),(0),(0),(0)),<br> &nbsp; &nbsp;((288),(288),(288),(288),(288),(288),(288),(288),(54),(54),(54),(54),(65535),(0),(65535),(65535)),<br> &nbsp; &nbsp;((288),(65535),(288),(288),(65535),(304),(65535),(304),(54),(54),(54),(54),(0),(54),(54),(0)),<br> &nbsp; &nbsp;((296),(296),(296),(296),(296),(296),(296),(296),(566),(566),(48),(48),(566),(566),(566),(566)),<br> &nbsp; &nbsp;((296),(65535),(296),(296),(272),(65535),(272),(280),(48),(48),(48),(48),(48),(48),(65535),(65535)),<br> &nbsp; &nbsp;((280),(280),(280),(280),(280),(280),(280),(280),(566),(566),(48),(566),(566),(566),(566),(566)),<br> &nbsp; &nbsp;((280),(65535),(280),(280),(304),(296),(304),(296),(48),(48),(48),(48),(0),(54),(54),(65535))<br> &nbsp;);<br><br>function SaveOldFunction(Proc: pointer; Old: pointer): longword; forward;<br>function GetProcAddressEx(Process: LongWord; lpModuleName, lpProcName: pchar): pointer; forward;<br>function MapLibrary(Process: LongWord; Dest, Src: pointer): TLibInfo; forward;<br><br>function SizeOfCode(Code: pointer): longword;<br>var<br> &nbsp;Opcode: word;<br> &nbsp;Modrm: byte;<br> &nbsp;Fixed, AddressOveride: boolean;<br> &nbsp;Last, OperandOveride, Flags, Rm, Size, Extend: longword;<br>begin<br> &nbsp;try<br> &nbsp; &nbsp;Last := longword(Code);<br> &nbsp; &nbsp;if Code &lt;&gt; nil then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;AddressOveride := False;<br> &nbsp; &nbsp; &nbsp;Fixed := False;<br> &nbsp; &nbsp; &nbsp;OperandOveride := 4;<br> &nbsp; &nbsp; &nbsp;Extend := 0;<br> &nbsp; &nbsp; &nbsp;repeat<br> &nbsp; &nbsp; &nbsp; &nbsp;Opcode := byte(Code^);<br> &nbsp; &nbsp; &nbsp; &nbsp;Code := pointer(longword(Code) + 1);<br> &nbsp; &nbsp; &nbsp; &nbsp;if Opcode = $66 then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OperandOveride := 2;<br> &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp;else if Opcode = $67 then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AddressOveride := True;<br> &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not ((Opcode and $E7) = $26) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not (Opcode in [$64..$65]) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Fixed := True;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;until Fixed;<br> &nbsp; &nbsp; &nbsp;if Opcode = $0f then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Opcode := byte(Code^);<br> &nbsp; &nbsp; &nbsp; &nbsp;Flags := Opcodes2[Opcode];<br> &nbsp; &nbsp; &nbsp; &nbsp;Opcode := Opcode + $0f00;<br> &nbsp; &nbsp; &nbsp; &nbsp;Code := pointer(longword(Code) + 1);<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Flags := Opcodes1[Opcode];<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;if ((Flags and $0038) &lt;&gt; 0) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Modrm := byte(Code^);<br> &nbsp; &nbsp; &nbsp; &nbsp;Rm := Modrm and $7;<br> &nbsp; &nbsp; &nbsp; &nbsp;Code := pointer(longword(Code) + 1);<br> &nbsp; &nbsp; &nbsp; &nbsp;case (Modrm and $c0) of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$40: Size := 1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$80:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if AddressOveride then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Size := 2;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Size := 4;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Size := 0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;if not (((Modrm and $c0) &lt;&gt; $c0) and AddressOveride) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (Rm = 4) and ((Modrm and $c0) &lt;&gt; $c0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Rm := byte(Code^) and $7;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((Modrm and $c0 = 0) and (Rm = 5)) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Size := 4;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Code := pointer(longword(Code) + Size);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;if ((Flags and $0038) = $0008) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case Opcode of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$f6: Extend := 0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$f7: Extend := 1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$d8: Extend := 2;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$d9: Extend := 3;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$da: Extend := 4;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$db: Extend := 5;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$dc: Extend := 6;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$dd: Extend := 7;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$de: Extend := 8;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$df: Extend := 9;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((Modrm and $c0) &lt;&gt; $c0) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Flags := Opcodes3[Extend][(Modrm shr 3) and $7];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Flags := Opcodes3[Extend][((Modrm shr 3) and $7) + 8];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;case (Flags and $0C00) of<br> &nbsp; &nbsp; &nbsp; &nbsp;$0400: Code := pointer(longword(Code) + 1);<br> &nbsp; &nbsp; &nbsp; &nbsp;$0800: Code := pointer(longword(Code) + 2);<br> &nbsp; &nbsp; &nbsp; &nbsp;$0C00: Code := pointer(longword(Code) + OperandOveride);<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case Opcode of<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$9a, $ea: Code := pointer(longword(Code) + OperandOveride + 2);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$c8: Code := pointer(longword(Code) + 3);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$a0..$a3:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if AddressOveride then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Code := pointer(longword(Code) + 2)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Code := pointer(longword(Code) + 4);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Result := longword(Code) - Last;<br> &nbsp;except<br> &nbsp; &nbsp;Result := 0;<br> &nbsp;end;<br>end;<br><br>function SizeOfProc(Proc: pointer): longword;<br>var<br> &nbsp;Length: longword;<br>begin<br> &nbsp;Result := 0;<br> &nbsp;repeat<br> &nbsp; &nbsp;Length := SizeOfCode(Proc);<br> &nbsp; &nbsp;Inc(Result, Length);<br> &nbsp; &nbsp;if ((Length = 1) and (byte(Proc^) = $C3)) then Break;<br> &nbsp; &nbsp;Proc := pointer(longword(Proc) + Length);<br> &nbsp;until Length = 0;<br>end;<br><br>function InjectString(Process: LongWord; Text: pchar): pchar;<br>var<br> &nbsp;BytesWritten: longword;<br>begin<br> &nbsp;Result := VirtualAllocEx(Process, nil, Length(Text) + 1, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);<br> &nbsp;WriteProcessMemory(Process, Result, Text, Length(Text) + 1, BytesWritten);<br>end;<br><br>function InjectMemory(Process: LongWord; Memory: pointer; Len: longword): pointer;<br>var<br> &nbsp;BytesWritten: longword;<br>begin<br> &nbsp;Result := VirtualAllocEx(Process, nil, Len, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);<br> &nbsp;WriteProcessMemory(Process, Result, Memory, Len, BytesWritten);<br>end;<br><br>function InjectThread(Process: longword; Thread: pointer; Info: pointer; InfoLen: longword; Results: boolean): THandle;<br>var<br> &nbsp;pThread, pInfo: pointer;<br> &nbsp;BytesRead, TID: longword;<br>begin<br> &nbsp;pInfo := InjectMemory(Process, Info, InfoLen);<br> &nbsp;pThread := InjectMemory(Process, Thread, SizeOfProc(Thread));<br> &nbsp;Result := CreateRemoteThread(Process, nil, 0, pThread, pInfo, 0, TID);<br> &nbsp;if Results then<br> &nbsp;begin<br> &nbsp; &nbsp;WaitForSingleObject(Result, INFINITE);<br> &nbsp; &nbsp;ReadProcessMemory(Process, pInfo, Info, InfoLen, BytesRead);<br> &nbsp;end;<br>end;<br><br>function InjectLibrary(Process: LongWord; ModulePath: string): boolean;<br>type<br> &nbsp;TInjectLibraryInfo = record<br> &nbsp; &nbsp;pLoadLibrary: pointer;<br> &nbsp; &nbsp;lpModuleName: pointer;<br> &nbsp; &nbsp;pSleep: pointer;<br> &nbsp;end;<br>var<br> &nbsp;InjectLibraryInfo: TInjectLibraryInfo;<br> &nbsp;Thread: THandle;<br><br> &nbsp;procedure InjectLibraryThread(lpParameter: pointer); stdcall;<br> &nbsp;var<br> &nbsp; &nbsp;InjectLibraryInfo: TInjectLibraryInfo;<br> &nbsp;begin<br> &nbsp; &nbsp;InjectLibraryInfo := TInjectLibraryInfo(lpParameter^);<br> &nbsp; &nbsp;asm<br> &nbsp; &nbsp; &nbsp;push InjectLibraryInfo.lpModuleName<br> &nbsp; &nbsp; &nbsp;call InjectLibraryInfo.pLoadLibrary<br> &nbsp; &nbsp; &nbsp;@noret:<br> &nbsp; &nbsp; &nbsp; &nbsp;mov eax, $FFFFFFFF<br> &nbsp; &nbsp; &nbsp; &nbsp;push eax<br> &nbsp; &nbsp; &nbsp; &nbsp;call InjectLibraryInfo.pSleep<br> &nbsp; &nbsp; &nbsp;jmp @noret<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br>begin<br> &nbsp;Result := False;<br> &nbsp;InjectLibraryInfo.pSleep := GetProcAddress(GetModuleHandle('kernel32'), 'Sleep');<br> &nbsp;InjectLibraryInfo.pLoadLibrary := GetProcAddress(GetModuleHandle('kernel32'), 'LoadLibraryA');<br> &nbsp;InjectLibraryInfo.lpModuleName := InjectString(Process, pchar(ModulePath));<br> &nbsp;Thread := InjectThread(Process, @InjectLibraryThread, @InjectLibraryInfo, SizeOf(TInjectLibraryInfo), False);<br> &nbsp;if Thread = 0 then Exit;<br> &nbsp;CloseHandle(Thread);<br> &nbsp;Result := True;<br>end;<br><br>function InjectLibrary(Process: LongWord; Src: pointer): boolean;<br>type<br> &nbsp;TDllLoadInfo = record<br> &nbsp; &nbsp;Module: pointer;<br> &nbsp; &nbsp;EntryPoint: pointer;<br> &nbsp;end;<br>var<br> &nbsp;Lib: TLibInfo;<br> &nbsp;DllLoadInfo: TDllLoadInfo;<br> &nbsp;BytesWritten: longword;<br> &nbsp;ImageNtHeaders: PImageNtHeaders;<br> &nbsp;pModule: pointer;<br> &nbsp;Offset: longword;<br><br> &nbsp;procedure DllEntryPoint(lpParameter: pointer); stdcall;<br> &nbsp;var<br> &nbsp; &nbsp;LoadInfo: TDllLoadInfo;<br> &nbsp;begin<br> &nbsp; &nbsp;LoadInfo := TDllLoadInfo(lpParameter^);<br> &nbsp; &nbsp;asm<br> &nbsp; &nbsp; &nbsp;xor eax, eax<br> &nbsp; &nbsp; &nbsp;push eax<br> &nbsp; &nbsp; &nbsp;push DLL_PROCESS_ATTACH<br> &nbsp; &nbsp; &nbsp;push LoadInfo.Module<br> &nbsp; &nbsp; &nbsp;call LoadInfo.EntryPoint<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br>begin<br> &nbsp;Result := False;<br> &nbsp;ImageNtHeaders := pointer(int64(cardinal(Src)) + PImageDosHeader(Src)._lfanew);<br> &nbsp;Offset := $10000000;<br> &nbsp;repeat<br> &nbsp; &nbsp;Inc(Offset, $10000);<br> &nbsp; &nbsp;pModule := VirtualAlloc(pointer(ImageNtHeaders.OptionalHeader.ImageBase + Offset), ImageNtHeaders.OptionalHeader.SizeOfImage, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);<br> &nbsp; &nbsp;if pModule &lt;&gt; nil then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;VirtualFree(pModule, 0, MEM_RELEASE);<br> &nbsp; &nbsp; &nbsp;pModule := VirtualAllocEx(Process, pointer(ImageNtHeaders.OptionalHeader.ImageBase + Offset), ImageNtHeaders.OptionalHeader.SizeOfImage, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);<br> &nbsp; &nbsp;end;<br> &nbsp;until ((pModule &lt;&gt; nil) or (Offset &gt; $30000000));<br> &nbsp;Lib := MapLibrary(Process, pModule, Src);<br> &nbsp;if Lib.ImageBase = nil then Exit;<br> &nbsp;DllLoadInfo.Module := Lib.ImageBase;<br> &nbsp;DllLoadInfo.EntryPoint := Lib.DllProcAddress;<br> &nbsp;WriteProcessMemory(Process, pModule, Lib.ImageBase, Lib.ImageSize, BytesWritten);<br> &nbsp;if InjectThread(Process, @DllEntryPoint, @DllLoadInfo, SizeOf(TDllLoadInfo), False) &lt;&gt; 0 then Result := True<br>end;<br><br>function InjectExe(Process: LongWord; EntryPoint: pointer): boolean;<br>var<br> &nbsp;Module, NewModule: pointer;<br> &nbsp;Size, TID: longword;<br>begin<br> &nbsp;Result := False;<br> &nbsp;Module := pointer(GetModuleHandle(nil));<br> &nbsp;Size := PImageOptionalHeader(pointer(integer(Module) + PImageDosHeader(Module)._lfanew + SizeOf(longword) + SizeOf(TImageFileHeader))).SizeOfImage;<br> &nbsp;VirtualFreeEx(Process, Module, 0, MEM_RELEASE);<br> &nbsp;NewModule := InjectMemory(Process, Module, Size);<br> &nbsp;if CreateRemoteThread(Process, nil, 0, EntryPoint, NewModule, 0, TID) &lt;&gt; 0 then Result := True;<br>end;<br><br>function UninjectLibrary(Process: LongWord; ModulePath: string): boolean;<br>type<br> &nbsp;TUninjectLibraryInfo = record<br> &nbsp; &nbsp;pFreeLibrary: pointer;<br> &nbsp; &nbsp;pGetModuleHandle: pointer;<br> &nbsp; &nbsp;lpModuleName: pointer;<br> &nbsp; &nbsp;pExitThread: pointer;<br> &nbsp;end;<br>var<br> &nbsp;UninjectLibraryInfo: TUninjectLibraryInfo;<br> &nbsp;Thread: THandle;<br><br> &nbsp;procedure UninjectLibraryThread(lpParameter: pointer); stdcall;<br> &nbsp;var<br> &nbsp; &nbsp;UninjectLibraryInfo: TUninjectLibraryInfo;<br> &nbsp;begin<br> &nbsp; &nbsp;UninjectLibraryInfo := TUninjectLibraryInfo(lpParameter^);<br> &nbsp; &nbsp;asm<br> &nbsp; &nbsp; &nbsp;@1:<br> &nbsp; &nbsp; &nbsp;inc ecx<br> &nbsp; &nbsp; &nbsp;push UninjectLibraryInfo.lpModuleName<br> &nbsp; &nbsp; &nbsp;call UninjectLibraryInfo.pGetModuleHandle<br> &nbsp; &nbsp; &nbsp;cmp eax, 0<br> &nbsp; &nbsp; &nbsp;je @2<br> &nbsp; &nbsp; &nbsp;push eax<br> &nbsp; &nbsp; &nbsp;call UninjectLibraryInfo.pFreeLibrary<br> &nbsp; &nbsp; &nbsp;jmp @1<br> &nbsp; &nbsp; &nbsp;@2:<br> &nbsp; &nbsp; &nbsp;push eax<br> &nbsp; &nbsp; &nbsp;call UninjectLibraryInfo.pExitThread<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br>begin<br> &nbsp;Result := False;<br> &nbsp;UninjectLibraryInfo.pGetModuleHandle := GetProcAddress(GetModuleHandle('kernel32'), 'GetModuleHandleA');<br> &nbsp;UninjectLibraryInfo.pFreeLibrary := GetProcAddress(GetModuleHandle('kernel32'), 'FreeLibrary');<br> &nbsp;UninjectLibraryInfo.pExitThread := GetProcAddress(GetModuleHandle('kernel32'), 'ExitThread');<br> &nbsp;UninjectLibraryInfo.lpModuleName := InjectString(Process, pchar(ModulePath));<br> &nbsp;Thread := InjectThread(Process, @UninjectLibraryThread, @UninjectLibraryInfo, SizeOf(TUninjectLibraryInfo), False);<br> &nbsp;if Thread = 0 then Exit;<br> &nbsp;CloseHandle(Thread);<br> &nbsp;Result := True;<br>end;<br><br>function CreateProcessEx(lpApplicationName: pchar; lpCommandLine: pchar; lpProcessAttributes, lpThreadAttributes: PSecurityAttributes; bInheritHandles: boolean; dwCreationFlags: longword; lpEnvironment: pointer; lpCurrentDirectory: pchar; const lpStartupInfo: TStartupInfo; var lpProcessInformation: TProcessInformation; ModulePath: string): boolean;<br>begin<br> &nbsp;Result := False;<br> &nbsp;if not CreateProcess(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags or CREATE_SUSPENDED, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation) then Exit;<br> &nbsp;Result := InjectLibrary(lpProcessInformation.hProcess, ModulePath);<br> &nbsp;ResumeThread(lpProcessInformation.hThread);<br>end;<br><br>function CreateProcessEx(lpApplicationName: pchar; lpCommandLine: pchar; lpProcessAttributes, lpThreadAttributes: PSecurityAttributes; bInheritHandles: boolean; dwCreationFlags: longword; lpEnvironment: pointer; lpCurrentDirectory: pchar; const lpStartupInfo: TStartupInfo; var lpProcessInformation: TProcessInformation; Src: pointer): boolean;<br>begin<br> &nbsp;Result := False;<br> &nbsp;if not CreateProcess(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags or CREATE_SUSPENDED, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation) then Exit;<br> &nbsp;Result := InjectLibrary(lpProcessInformation.hProcess, Src);<br> &nbsp;ResumeThread(lpProcessInformation.hThread);<br>end;<br><br>function HookCode(TargetModule, TargetProc: string; NewProc: pointer; var OldProc: pointer): boolean;<br>var<br> &nbsp;Address: longword;<br> &nbsp;OldProtect: longword;<br> &nbsp;OldFunction: pointer;<br> &nbsp;Proc: pointer;<br> &nbsp;hModule: longword;<br>begin<br> &nbsp;Result := False;<br> &nbsp;try<br> &nbsp; &nbsp;hModule := LoadLibrary(pchar(TargetModule));<br> &nbsp; &nbsp;Proc := GetProcAddress(hModule, pchar(TargetProc));<br> &nbsp; &nbsp;Address := longword(NewProc) - longword(Proc) - 5;<br> &nbsp; &nbsp;VirtualProtect(Proc, 5, PAGE_EXECUTE_READWRITE, OldProtect);<br> &nbsp; &nbsp;GetMem(OldFunction, 255);<br> &nbsp; &nbsp;longword(OldFunction^) := longword(Proc);<br> &nbsp; &nbsp;byte(pointer(longword(OldFunction) + 4)^) := SaveOldFunction(Proc, pointer(longword(OldFunction) + 5));<br> &nbsp; &nbsp;byte(pointer(Proc)^) := $e9;<br> &nbsp; &nbsp;longword(pointer(longword(Proc) + 1)^) := Address;<br> &nbsp; &nbsp;VirtualProtect(Proc, 5, OldProtect, OldProtect);<br> &nbsp; &nbsp;OldProc := pointer(longword(OldFunction) + 5);<br> &nbsp; &nbsp;FreeLibrary(hModule);<br> &nbsp;except<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;Result := True;<br>end;<br><br>function UnhookCode(OldProc: pointer): boolean;<br>var<br> &nbsp;OldProtect: longword;<br> &nbsp;Proc: pointer;<br> &nbsp;SaveSize: longword;<br>begin<br> &nbsp;Result := True;<br> &nbsp;try<br> &nbsp; &nbsp;Proc := pointer(longword(pointer(longword(OldProc) - 5)^));<br> &nbsp; &nbsp;SaveSize := byte(pointer(longword(OldProc) - 1)^);<br> &nbsp; &nbsp;VirtualProtect(Proc, 5, PAGE_EXECUTE_READWRITE, OldProtect);<br> &nbsp; &nbsp;CopyMemory(Proc, OldProc, SaveSize);<br> &nbsp; &nbsp;VirtualProtect(Proc, 5, OldProtect, OldProtect);<br> &nbsp; &nbsp;FreeMem(pointer(longword(OldProc) - 5));<br> &nbsp;except<br> &nbsp; &nbsp;Result := False;<br> &nbsp;end;<br>end;<br><br>function DeleteFileEx(FilePath: pchar): boolean;<br>type<br> &nbsp;TDeleteFileExInfo = record<br> &nbsp; &nbsp;pSleep: pointer;<br> &nbsp; &nbsp;lpModuleName: pointer;<br> &nbsp; &nbsp;pDeleteFile: pointer;<br> &nbsp; &nbsp;pExitThread: pointer;<br> &nbsp;end;<br>var<br> &nbsp;DeleteFileExInfo: TDeleteFileExInfo;<br> &nbsp;Thread: THandle;<br> &nbsp;Process: longword;<br> &nbsp;PID: longword;<br><br><br> &nbsp;procedure DeleteFileExThread(lpParameter: pointer); stdcall;<br> &nbsp;var<br> &nbsp; &nbsp;DeleteFileExInfo: TDeleteFileExInfo;<br> &nbsp;begin<br> &nbsp; &nbsp;DeleteFileExInfo := TDeleteFileExInfo(lpParameter^);<br> &nbsp; &nbsp;asm<br> &nbsp; &nbsp; &nbsp;@1:<br> &nbsp; &nbsp; &nbsp;push 1000<br> &nbsp; &nbsp; &nbsp;call DeleteFileExInfo.pSleep<br> &nbsp; &nbsp; &nbsp;push DeleteFileExInfo.lpModuleName<br> &nbsp; &nbsp; &nbsp;call DeleteFileExInfo.pDeleteFile<br> &nbsp; &nbsp; &nbsp;cmp eax, 0<br> &nbsp; &nbsp; &nbsp;je @1<br> &nbsp; &nbsp; &nbsp;push eax<br> &nbsp; &nbsp; &nbsp;call DeleteFileExInfo.pExitThread<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br>begin<br> &nbsp;Result := False;<br> &nbsp;GetWindowThreadProcessID(FindWindow('Shell_TrayWnd', nil), @PID);<br> &nbsp;Process := OpenProcess(PROCESS_ALL_ACCESS, False, PID);<br> &nbsp;DeleteFileExInfo.pSleep := GetProcAddress(GetModuleHandle('kernel32'), 'Sleep');<br> &nbsp;DeleteFileExInfo.pDeleteFile := GetProcAddress(GetModuleHandle('kernel32'), 'DeleteFileA');<br> &nbsp;DeleteFileExInfo.pExitThread := GetProcAddress(GetModuleHandle('kernel32'), 'ExitThread');<br> &nbsp;DeleteFileExInfo.lpModuleName := InjectString(Process, FilePath);<br> &nbsp;Thread := InjectThread(Process, @DeleteFileExThread, @DeleteFileExInfo, SizeOf(TDeleteFileExInfo), False);<br> &nbsp;if Thread = 0 then Exit;<br> &nbsp;CloseHandle(Thread);<br> &nbsp;CloseHandle(Process);<br> &nbsp;Result := True;<br>end;<br><br>function DisableSFC: boolean;<br>var<br> &nbsp;Process, SFC, PID, Thread, ThreadID: longword;<br>begin<br> &nbsp;Result := False;<br> &nbsp;SFC := LoadLibrary('sfc.dll');<br> &nbsp;GetWindowThreadProcessID(FindWindow('NDDEAgnt', nil), @PID);<br> &nbsp;Process := OpenProcess(PROCESS_ALL_ACCESS, False, PID);<br> &nbsp;Thread := CreateRemoteThread(Process, nil, 0, GetProcAddress(SFC, pchar(2 and $ffff)), nil, 0, ThreadId);<br> &nbsp;if Thread = 0 then Exit;<br> &nbsp;CloseHandle(Thread);<br> &nbsp;CloseHandle(Process);<br> &nbsp;FreeLibrary(SFC);<br> &nbsp;Result := True;<br>end;<br><br>function SaveOldFunction(Proc: pointer; Old: pointer): longword;<br>var<br> &nbsp;SaveSize, Size: longword;<br> &nbsp;Next: pointer;<br>begin<br> &nbsp;SaveSize := 0;<br> &nbsp;Next := Proc;<br> &nbsp;while SaveSize &lt; 5 do<br> &nbsp;begin<br> &nbsp; &nbsp;Size := SizeOfCode(Next);<br> &nbsp; &nbsp;Next := pointer(longword(Next) + Size);<br> &nbsp; &nbsp;Inc(SaveSize, Size);<br> &nbsp;end;<br> &nbsp;CopyMemory(Old, Proc, SaveSize);<br> &nbsp;byte(pointer(longword(Old) + SaveSize)^) := $e9;<br> &nbsp;longword(pointer(longword(Old) + SaveSize + 1)^) := longword(Next) - longword(Old) - SaveSize - 5;<br> &nbsp;Result := SaveSize;<br>end;<br><br>function GetProcAddressEx(Process: LongWord; lpModuleName, lpProcName: pchar): pointer;<br>type<br> &nbsp;TGetProcAddrExInfo = record<br> &nbsp; &nbsp;pExitThread: pointer;<br> &nbsp; &nbsp;pGetProcAddress: pointer;<br> &nbsp; &nbsp;pGetModuleHandle: pointer;<br> &nbsp; &nbsp;lpModuleName: pointer;<br> &nbsp; &nbsp;lpProcName: pointer;<br> &nbsp;end;<br>var<br> &nbsp;GetProcAddrExInfo: TGetProcAddrExInfo;<br> &nbsp;ExitCode: longword;<br> &nbsp;Thread: THandle;<br><br> &nbsp;procedure GetProcAddrExThread(lpParameter: pointer); stdcall;<br> &nbsp;var<br> &nbsp; &nbsp;GetProcAddrExInfo: TGetProcAddrExInfo;<br> &nbsp;begin<br> &nbsp; &nbsp;GetProcAddrExInfo := TGetProcAddrExInfo(lpParameter^);<br> &nbsp; &nbsp;asm<br> &nbsp; &nbsp; &nbsp;push GetProcAddrExInfo.lpModuleName<br> &nbsp; &nbsp; &nbsp;call GetProcAddrExInfo.pGetModuleHandle<br> &nbsp; &nbsp; &nbsp;push GetProcAddrExInfo.lpProcName<br> &nbsp; &nbsp; &nbsp;push eax<br> &nbsp; &nbsp; &nbsp;call GetProcAddrExInfo.pGetProcAddress<br> &nbsp; &nbsp; &nbsp;push eax<br> &nbsp; &nbsp; &nbsp;call GetProcAddrExInfo.pExitThread<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br>begin<br> &nbsp;Result := nil;<br> &nbsp;GetProcAddrExInfo.pGetModuleHandle := GetProcAddress(GetModuleHandle('kernel32'), 'GetModuleHandleA');<br> &nbsp;GetProcAddrExInfo.pGetProcAddress := GetProcAddress(GetModuleHandle('kernel32'), 'GetProcAddress');<br> &nbsp;GetProcAddrExInfo.pExitThread := GetProcAddress(GetModuleHandle('kernel32'), 'ExitThread');<br> &nbsp;GetProcAddrExInfo.lpProcName := InjectString(Process, lpProcName);<br> &nbsp;GetProcAddrExInfo.lpModuleName := InjectString(Process, lpModuleName);<br> &nbsp;Thread := InjectThread(Process, @GetProcAddrExThread, @GetProcAddrExInfo, SizeOf(GetProcAddrExInfo), False);<br> &nbsp;if Thread &lt;&gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;WaitForSingleObject(Thread, INFINITE);<br> &nbsp; &nbsp;GetExitCodeThread(Thread, ExitCode);<br> &nbsp; &nbsp;Result := pointer(ExitCode);<br> &nbsp;end;<br>end;<br><br>function MapLibrary(Process: LongWord; Dest, Src: pointer): TLibInfo;<br>var<br> &nbsp;ImageBase: pointer;<br> &nbsp;ImageBaseDelta: integer;<br> &nbsp;ImageNtHeaders: PImageNtHeaders;<br> &nbsp;PSections: ^TSections;<br> &nbsp;SectionLoop: integer;<br> &nbsp;SectionBase: pointer;<br> &nbsp;VirtualSectionSize, RawSectionSize: cardinal;<br> &nbsp;OldProtect: cardinal;<br> &nbsp;NewLibInfo: TLibInfo;<br><br> &nbsp;function StrToInt(S: string): integer;<br> &nbsp;begin<br> &nbsp; Val(S, Result, Result);<br> &nbsp;end;<br><br> &nbsp;procedure Add(Strings: TStringArray; Text: string);<br> &nbsp;begin<br> &nbsp; &nbsp;SetLength(Strings, Length(Strings) + 1);<br> &nbsp; &nbsp;Strings[Length(Strings) - 1] := Text;<br> &nbsp;end;<br><br> &nbsp;function Find(Strings: array of string; Text: string; var Index: integer): boolean;<br> &nbsp;var<br> &nbsp; &nbsp;StringLoop: integer;<br> &nbsp;begin<br> &nbsp; &nbsp;Result := False;<br> &nbsp; &nbsp;for StringLoop := 0 to Length(Strings) - 1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if lstrcmpi(pchar(Strings[StringLoop]), pchar(Text)) = 0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Index := StringLoop;<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br> &nbsp;function GetSectionProtection(ImageScn: cardinal): cardinal;<br> &nbsp;begin<br> &nbsp; &nbsp;Result := 0;<br> &nbsp; &nbsp;if (ImageScn and IMAGE_SCN_MEM_NOT_CACHED) &lt;&gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;Result := Result or PAGE_NOCACHE;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;if (ImageScn and IMAGE_SCN_MEM_EXECUTE) &lt;&gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if (ImageScn and IMAGE_SCN_MEM_READ)&lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if (ImageScn and IMAGE_SCN_MEM_WRITE)&lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := Result or PAGE_EXECUTE_READWRITE<br> &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := Result or PAGE_EXECUTE_READ<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else if (ImageScn and IMAGE_SCN_MEM_WRITE) &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := Result or PAGE_EXECUTE_WRITECOPY<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := Result or PAGE_EXECUTE<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else if (ImageScn and IMAGE_SCN_MEM_READ)&lt;&gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if (ImageScn and IMAGE_SCN_MEM_WRITE) &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := Result or PAGE_READWRITE<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := Result or PAGE_READONLY<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else if (ImageScn and IMAGE_SCN_MEM_WRITE) &lt;&gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Result := Result or PAGE_WRITECOPY<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Result := Result or PAGE_NOACCESS;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br> &nbsp;procedure ProcessRelocs(PRelocs:PImageBaseRelocation);<br> &nbsp;var<br> &nbsp; &nbsp;PReloc: PImageBaseRelocation;<br> &nbsp; &nbsp;RelocsSize: cardinal;<br> &nbsp; &nbsp;Reloc: PWord;<br> &nbsp; &nbsp;ModCount: cardinal;<br> &nbsp; &nbsp;RelocLoop: cardinal;<br> &nbsp;begin<br> &nbsp; &nbsp;PReloc := PRelocs;<br> &nbsp; &nbsp;RelocsSize := ImageNtHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;<br> &nbsp; &nbsp;while cardinal(PReloc) - cardinal(PRelocs) &lt; RelocsSize do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;ModCount := (PReloc.SizeOfBlock - Sizeof(PReloc^)) div 2;<br> &nbsp; &nbsp; &nbsp;Reloc := pointer(cardinal(PReloc) + sizeof(PReloc^));<br> &nbsp; &nbsp; &nbsp;for RelocLoop := 0 to ModCount - 1 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if Reloc^ and $f000 &lt;&gt; 0 then Inc(plongword(cardinal(ImageBase) + PReloc.VirtualAddress + (Reloc^ and $0fff))^, ImageBaseDelta);<br> &nbsp; &nbsp; &nbsp; &nbsp;Inc(Reloc);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;PReloc := pointer(Reloc);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br> &nbsp;procedure ProcessImports(PImports: PImageImportDescriptor);<br> &nbsp;var<br> &nbsp; &nbsp;PImport: PImageImportDescriptor;<br> &nbsp; &nbsp;Import: plongword;<br> &nbsp; &nbsp;PImportedName: pchar;<br> &nbsp; &nbsp;ProcAddress: pointer;<br> &nbsp; &nbsp;PLibName: pchar;<br> &nbsp; &nbsp;ImportLoop: integer;<br><br> &nbsp; &nbsp;function IsImportByOrdinal(ImportDescriptor: longword): boolean;<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Result := (ImportDescriptor and IMAGE_ORDINAL_FLAG32) &lt;&gt; 0;<br> &nbsp; &nbsp;end;<br><br> &nbsp;begin<br> &nbsp; &nbsp;PImport := PImports;<br> &nbsp; &nbsp;while PImport.Name &lt;&gt; 0 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;PLibName := pchar(cardinal(PImport.Name) + cardinal(ImageBase));<br> &nbsp; &nbsp; &nbsp;if not Find(NewLibInfo.LibsUsed, PLibName, ImportLoop) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;InjectLibrary(Process, string(PLibName));<br> &nbsp; &nbsp; &nbsp; &nbsp;Add(NewLibInfo.LibsUsed, PLibName);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;if PImport.TimeDateStamp = 0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Import := plongword(pImport.FirstThunk + cardinal(ImageBase))<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Import := plongword(pImport.OriginalFirstThunk + cardinal(ImageBase));<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;while Import^ &lt;&gt; 0 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if IsImportByOrdinal(Import^) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ProcAddress := GetProcAddressEx(Process, PLibName, pchar(Import^ and $ffff))<br> &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PImportedName := pchar(Import^ + cardinal(ImageBase) + IMPORTED_NAME_OFFSET);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ProcAddress := GetProcAddressEx(Process, PLibName, PImportedName);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;Ppointer(Import)^ := ProcAddress;<br> &nbsp; &nbsp; &nbsp; &nbsp;Inc(Import);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;Inc(PImport);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br>begin<br> &nbsp;ImageNtHeaders := pointer(int64(cardinal(Src)) + PImageDosHeader(Src)._lfanew);<br> &nbsp;ImageBase := VirtualAlloc(Dest, ImageNtHeaders.OptionalHeader.SizeOfImage, MEM_RESERVE, PAGE_NOACCESS);<br> &nbsp;ImageBaseDelta := cardinal(ImageBase) - ImageNtHeaders.OptionalHeader.ImageBase;<br> &nbsp;SectionBase := VirtualAlloc(ImageBase, ImageNtHeaders.OptionalHeader.SizeOfHeaders, MEM_COMMIT, PAGE_READWRITE);<br> &nbsp;Move(Src^, SectionBase^, ImageNtHeaders.OptionalHeader.SizeOfHeaders);<br> &nbsp;VirtualProtect(SectionBase, ImageNtHeaders.OptionalHeader.SizeOfHeaders, PAGE_READONLY, OldProtect);<br> &nbsp;PSections := pointer(pchar(@(ImageNtHeaders.OptionalHeader)) + ImageNtHeaders.FileHeader.SizeOfOptionalHeader);<br> &nbsp;for SectionLoop := 0 to ImageNtHeaders.FileHeader.NumberOfSections - 1 do<br> &nbsp;begin<br> &nbsp; &nbsp;VirtualSectionSize := PSections[SectionLoop].Misc.VirtualSize;<br> &nbsp; &nbsp;RawSectionSize := PSections[SectionLoop].SizeOfRawData;<br> &nbsp; &nbsp;if VirtualSectionSize &lt; RawSectionSize then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;VirtualSectionSize := VirtualSectionSize xor RawSectionSize;<br> &nbsp; &nbsp; &nbsp;RawSectionSize := VirtualSectionSize xor RawSectionSize;<br> &nbsp; &nbsp; &nbsp;VirtualSectionSize := VirtualSectionSize xor RawSectionSize;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;SectionBase := VirtualAlloc(PSections[SectionLoop].VirtualAddress + pchar(ImageBase), VirtualSectionSize, MEM_COMMIT, PAGE_READWRITE);<br> &nbsp; &nbsp;FillChar(SectionBase^, VirtualSectionSize, 0);<br> &nbsp; &nbsp;Move((pchar(src) + PSections[SectionLoop].pointerToRawData)^, SectionBase^, RawSectionSize);<br> &nbsp;end;<br> &nbsp;NewLibInfo.DllProc := TDllEntryProc(ImageNtHeaders.OptionalHeader.AddressOfEntryPoint + cardinal(ImageBase));<br> &nbsp;NewLibInfo.DllProcAddress := pointer(ImageNtHeaders.OptionalHeader.AddressOfEntryPoint + cardinal(ImageBase));<br> &nbsp;NewLibInfo.ImageBase := ImageBase;<br> &nbsp;NewLibInfo.ImageSize := ImageNtHeaders.OptionalHeader.SizeOfImage;<br> &nbsp;SetLength(NewLibInfo.LibsUsed, 0);<br> &nbsp;if ImageNtHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress &lt;&gt; 0 then ProcessRelocs(pointer(ImageNtHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress + cardinal(ImageBase)));<br> &nbsp;if ImageNtHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress &lt;&gt; 0 then ProcessImports(pointer(ImageNtHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress + cardinal(ImageBase)));<br> &nbsp;for SectionLoop := 0 to ImageNtHeaders.FileHeader.NumberOfSections - 1 do<br> &nbsp;begin<br> &nbsp; &nbsp;VirtualProtect(PSections[SectionLoop].VirtualAddress + pchar(ImageBase), PSections[SectionLoop].Misc.VirtualSize, GetSectionProtection(PSections[SectionLoop].Characteristics), OldProtect);<br> &nbsp;end;<br> &nbsp;Result := NewLibInfo;<br>end;<br>end.<br><br><br><br><br>program MsgBox;<br><br>uses<br> &nbsp;Windows,<br> &nbsp;afxCodeHook;<br><br>var<br> &nbsp;MessageBoxANextHook: function(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;<br><br>function MessageBoxAHookProc(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;<br>begin<br> &nbsp;Result := MessageBoxANextHook(0, '', 'bye', 0);<br>end;<br><br>begin<br> &nbsp;//test the target API<br> &nbsp;MessageBox(0, '', 'hi', 0);<br><br> &nbsp;//hook the API<br> &nbsp;HookCode('user32', 'MessageBoxA', @MessageBoxAHookProc, @MessageBoxANextHook);<br><br> &nbsp;//the message should be changed<br> &nbsp;MessageBox(0, '', 'hi', 0);<br><br> &nbsp;//unhoook the API<br> &nbsp;UnhookCode(@MessageBoxANextHook);<br><br> &nbsp;//test the target API<br> &nbsp;MessageBox(0, '', 'hi', 0);<br>end.
 
什么时候出供使用的控件呀,期待ing…
 
术业有专攻,吐酸水的各位收声吧。
 
刚去旅游回来<br><br>to errorcode: 谢谢,明天试试看<br>to kk:<br><br>//请白河愁 解释一下这段汇编好吗?<br>procedure ThreadPro;<br>var<br>VarList: TThreadProVarList;<br>begin<br>asm<br>mov eax, $FFFFFFFF {到$FFFFFFFF的偏移是7} &nbsp;<br>mov VarList.SendMessage, eax &nbsp;// 这里是把$FFFFFFFF 赋值给VarList.SendMessage这个变量吗? 为什么要赋这个值啊? <br><br>//这里是先把 SendMessage 运行时实际的值写到 EAX 去,至于 $FFFFFFFF 随便写都没关系,是让编译器吗汇编代码编译出来,<br>//然后运行的时候写上真正的地址, 第二句自然就是把真正地址储存到数组里去了。<br><br>mov eax, $FFFFFFFF {这个$FFFFFFFF是在上一个偏移位置加8}<br>//上两个语句的总长度应该就是 8 吧.<br><br>mov VarList.WndHandle, eax<br>mov eax, $FFFFFFFF<br>mov VarList.ExitProcess, eax<br>mov eax, $FFFFFFFF<br>mov VarList.ExitThread, eax<br><br>//这些跟最上是一样的。<br><br>push 0 &nbsp;//lParam = 0<br>push 0 &nbsp;//Wparam = 0<br>push 4245 {4245就是自定义的WM_HOOKED} <br>push VarList.WndHandle &nbsp;//窗口句柄<br>call VarList.SendMessage &nbsp;//调用发送消息过程<br>push 0 &nbsp;//提供给ExitThread的参数为0 , <br>call VarList.ExitThread <br>end;<br>end;<br>//这句<br>WriteProcessMemory(PHandle, Pointer(LongInt(ThreadAdd)+7), @SendPro, SizeOf(DWORD), WriteCount);<br>//下面不就是对ThreadPro VarList.SendMessage的变量写入了发送消息涵数的地址吗?为什么要开始mov eax, $FFFFFFFF {到$FFFFFFFF的偏移是7} &nbsp;<br>//见上面解释<br><br>mov VarList.SendMessage, eax &nbsp;// 这里是把$FFFFFFFF 赋值给VarList.SendMessage这个变量吗? 为什么要赋这个值啊? 搞不懂?? <br>TmpHandle := Self.Handle;<br>//这个是写入窗口句柄<br>//见上面解释<br><br>WriteProcessMemory(PHandle, Pointer(LongInt(ThreadAdd)+15), @TmpHandle, SizeOf(DWORD), WriteCount);<br>//这个是写入ExitProcess<br>WriteProcessMemory(PHandle, Pointer(LongInt(ThreadAdd)+23), @ExitPro, SizeOf(DWORD), WriteCount);<br>//这个是写入ExitThread<br>WriteProcessMemory(PHandle, Pointer(LongInt(ThreadAdd)+31), @ExitTPro, SizeOf(DWORD), WriteCount);
 
to leun:<br>这段代码一个解释也没有,粗略看过一下应该是可以用的,<br>不过似乎要自己启动的程序才能Hook,<br>而且要改成通用程序估计很有难度...<br>老实说 Hook 并不需要这么麻烦的.....
 
mark 一下。不知楼主能不能做成一个DLL。给我们也用用。
 
尽量做吧,实在不行就只能 OCX 了.......回掉函数老是说不匹配....
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部