1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285:
| library mylib;
uses windows; const PC_WRITEABLE = $00020000; PC_USER = $00040000; PC_STATIC = $20000000;
HOOK_NEED_CHECK = 0; HOOK_CAN_WRITE = 1; HOOK_ONLY_READ = 2;
BUFFERLEN = 7;
type _tagApiHookStruct = record
lpszApiModuleName: LPSTR; lpszApiName: LPSTR; dwApiOffset: DWORD; lpWinApiProc: Pointer; oldWinAPIProc: Pointer; WinApiFiveByte: array[0..6] of BYTE;
lpszHookApiModuleName: LPSTR; lpszHookApiName: LPSTR; lpHookApiProc: POINTER; NewHookApiProc: POINTER; HookApiFiveByte: array[0..6] of BYTE; hInst: LongWord;
WinApiBakByte: array[0..6] of BYTE; end; TKS_APIHOOKSTRUCT = _tagApiHookStruct; PKS_APIHOOKSTRUCT = ^_tagApiHookStruct;
function MyGetFuncAddress(hInst: LongWord; lpMod: LPCSTR; lpFunc: LPCSTR): tfarproc; stdcall; procedure MakeJMPCode(var lpJMPCode: array of BYTE ; lpCodePoint: Pointer); procedure HookWin32Api(lpApiHook: PKS_APIHOOKSTRUCT; nSysMemStatus: integer); procedure RestoreWin32Api(lpApiHook: PKS_APIHOOKSTRUCT; nSysMemStatus: integer); function MyExtTextOutW(hdc: HDC; X, Y: integer; fuOptions: Longint; lprc: PRect; lpString: PWideChar; cbCount: Longint; lpDx: PInteger): BOOL; stdcall;
function ByteArraylcomp(const Str1, Str2: array of byte; MaxLen: Cardinal): Integer; var i: Cardinal; begin result := 0; for i := 0 to MaxLen do if i < MaxLen then begin if str1[i] > str2[i] then begin result := 1; exit; end else if str1[i] < str2[i] then begin result := -1; exit; end end; end;
function MyExtTextOutW(hdc: HDC; X, Y: integer; fuOptions: Longint; lprc: PRect; lpString: PWideChar; cbCount: Longint; lpDx: PInteger): BOOL; stdcall;
begin RestoreWin32Api(@g_ExtTextOutWHook, HOOK_NEED_CHECK); ExtTextOutW(hdc, X, Y, fuOptions, lprc, lpString, cbCount, lpDx);
HookWin32Api(@g_ExtTextOutWHook, HOOK_NEED_CHECK);
result := TRUE; end;
function MyGetFuncAddress(hInst: LongWord; lpMod: LPCSTR; lpFunc: LPCSTR): tfarproc; stdcall; var hMod: HMODULE; procFunc: FARPROC; begin if ('' <> lpMod) then begin hMod := GetModuleHandle(lpMod); procFunc := GetProcAddress(hMod, lpFunc); end else begin procFunc := GetProcAddress(hInst, lpFunc); end;
result := procFunc; end;
procedure MakeJMPCode(var lpJMPCode: array of BYTE; lpCodePoint: Pointer); var temp: BYTE; wHiWord: WORD; wLoWord: WORD; wCS: WORD; begin wHiWord := HIWORD(DWORD(lpCodePoint)); wLoWord := LOWORD(DWORD(lpCodePoint)); asm push ax; push cs; pop ax; mov wCS, ax; pop ax; end;
lpJMPCode[0] := $EA;
temp := LOBYTE(wLoWord); lpJMPCode[1] := temp; temp := HIBYTE(wLoWord); lpJMPCode[2] := temp; temp := LOBYTE(wHiWord); lpJMPCode[3] := temp; temp := HIBYTE(wHiWord); lpJMPCode[4] := temp;
temp := LOBYTE(wCS); lpJMPCode[5] := temp; temp := HIBYTE(wCS); lpJMPCode[6] := temp;
end;
procedure HookWin32Api(lpApiHook: PKS_APIHOOKSTRUCT; nSysMemStatus: integer); var dwReserved: DWORD; dwTemp: DWORD; bWin32Api: array[0..4] of BYTE; begin bWin32Api[0] := $00; if (lpApiHook.lpWinApiProc = nil) then begin lpApiHook.lpWinApiProc := MyGetFuncAddress(lpApiHook.hInst, lpApiHook.lpszApiModuleName, lpApiHook.lpszApiName); if (lpApiHook.dwApiOffset <> 0) then lpApiHook.lpWinApiProc := Pointer(DWORD(lpApiHook.lpWinApiProc) + lpApiHook.dwApiOffset); end;
if (lpApiHook.lpHookApiProc = nil) then begin lpApiHook.lpHookApiProc := MyGetFuncAddress( lpApiHook.hInst, lpApiHook.lpszHookApiModuleName, lpApiHook.lpszHookApiName);
end;
if (lpApiHook.HookApiFiveByte[0] = $00) then begin MakeJMPCode(lpApiHook.HookApiFiveByte, lpApiHook.lpHookApiProc);
end;
if (not VirtualProtect(lpApiHook.lpWinApiProc, 16, PAGE_READWRITE, @dwReserved)) then begin exit; end;
if (nSysMemStatus = HOOK_NEED_CHECK) then begin CopyMemory(lpApiHook.lpWinApiProc, @lpApiHook.HookApiFiveByte, BUFFERLEN); end else begin if (lpApiHook.WinApiFiveByte[0] = $00) then begin CopyMemory(@lpApiHook.WinApiFiveByte, lpApiHook.lpWinApiProc, BUFFERLEN); if (ByteArraylcomp(lpApiHook.WinApiFiveByte, lpApiHook.HookApiFiveByte, BUFFERLEN) = 0) then begin CopyMemory(@lpApiHook.WinApiFiveByte, @lpApiHook.WinApiBakByte, BUFFERLEN); end; end else begin CopyMemory(@bWin32Api, @lpApiHook.lpWinApiProc, BUFFERLEN); end;
if (ByteArraylcomp(bWin32Api, lpApiHook.HookApiFiveByte, BUFFERLEN) <> 0) then begin CopyMemory(@lpApiHook.lpWinApiProc, @lpApiHook.HookApiFiveByte, BUFFERLEN); end; end;
if (not VirtualProtect(lpApiHook.lpWinApiProc, 16, dwReserved, @dwTemp)) then begin exit; end; end;
procedure RestoreWin32Api(lpApiHook: PKS_APIHOOKSTRUCT; nSysMemStatus: integer); var dwReserved, dwTemp: DWORD; begin if (lpApiHook.lpWinApiProc = nil) then begin exit; end;
if (not VirtualProtect(lpApiHook.lpWinApiProc, 16, PAGE_READWRITE, @dwReserved)) then begin exit; end; CopyMemory(@lpApiHook.lpWinApiProc, @lpApiHook.WinApiFiveByte, BUFFERLEN); if (not VirtualProtect(lpApiHook.lpWinApiProc, 16, dwReserved, @dwTemp)) then begin exit; end;
end; end.
var g_ExtTextOutWHook: TKS_APIHOOKSTRUCT = ( lpszApiModuleName: 'gdi32.dll'; lpszApiName: 'ExtTextOutW'; dwApiOffset: 0; lpWinApiProc: nil; oldWinAPIProc: nil; WinApiFiveByte: (0, 0, 0, 0, 0, 0, 0); lpszHookApiModuleName: 'MyLib.dll'; lpszHookApiName: 'MyExtTextOutW'; lpHookApiProc: nil; NewHookApiProc: nil; HookApiFiveByte: (0, 0, 0, 0, 0, 0, 0); hInst: 0; WinApiBakByte: ($FF, $15, $FA, $13, $F3, $BF, $33) );
HookWin32Api(@g_ExtTextOutWHook, HOOK_CAN_WRITE); |