Created
November 27, 2018 12:49
-
-
Save superswanman/52cfa4147ad2c31196d07d1540d91cd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit RioGetItFix; | |
interface | |
procedure Register; | |
implementation | |
uses | |
Winapi.Windows, System.SysUtils, Vcl.Themes; | |
const | |
PackageName = 'GetIt260.bpl'; | |
PatchOffset = $8AB14; | |
OriginalCodes: array[0..4] of Byte = ($8B, $45, $F4, $8B, $10); | |
var | |
IsHooked: Boolean; | |
ReturnAddr1: Pointer; | |
ReturnAddr2: Pointer; | |
function GetStyleServices: TCustomStyleServices; | |
begin | |
Result := TStyleManager.ActiveStyle; | |
end; | |
procedure Trampoline; | |
asm | |
MOV EAX,DWORD PTR [EBP-$C] | |
TEST EAX,EAX | |
JE @@1 | |
MOV EDX,DWORD PTR [EAX] | |
JMP [ReturnAddr1] | |
@@1: | |
CALL GetStyleServices | |
JMP [ReturnAddr2] | |
end; | |
procedure Patch(Address: Pointer; const Data: array of Byte); | |
var | |
oldProtect: DWORD; | |
begin | |
VirtualProtect(Address, Length(Data), PAGE_READWRITE, oldProtect); | |
Move(Data[0], Address^, Length(Data)); | |
VirtualProtect(Address, Length(Data), oldProtect, nil); | |
FlushInstructionCache(GetCurrentProcess, Address, Length(Data)); | |
end; | |
procedure Register; | |
var | |
hModule: THandle; | |
patchAddr: Pointer; | |
jumpOffset: NativeInt; | |
bytes: TBytes; | |
begin | |
hModule := GetModuleHandle(PackageName); | |
if hModule = 0 then Exit; | |
patchAddr := Pointer(hModule + PatchOffset); | |
if not CompareMem(patchAddr, @OriginalCodes[0], SizeOf(OriginalCodes)) then Exit; | |
ReturnAddr1 := Pointer(hModule + PatchOffset + SizeOf(OriginalCodes)); | |
ReturnAddr2 := Pointer(hModule + PatchOffset + SizeOf(OriginalCodes) + 6); | |
jumpOffset := NativeInt(@Trampoline) - NativeInt(ReturnAddr1); | |
bytes := [$E9] + BytesOf(@jumpOffset, SizeOf(jumpOffset)); | |
Patch(patchAddr, bytes); | |
IsHooked := True; | |
end; | |
procedure Unregister; | |
var | |
hModule: THandle; | |
begin | |
if not IsHooked then Exit; | |
hModule := GetModuleHandle(PackageName); | |
if hModule = 0 then Exit; | |
Patch(Pointer(hModule + PatchOffset), OriginalCodes); | |
end; | |
initialization | |
finalization | |
Unregister; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment