admin管理员组

文章数量:1287489

I want to place jump to my function on address of other function declaration, specifically CreatePen function for gdi. I'm using zig and that's what i've made so far:

const windows = @import("std").os.windows;
const FunctionAddress: *u32 = @ptrFromInt(0x142c46108);
const ActualFunction: fn(c_int, c_int, windows.DWORD) ?anyopaque = *FunctionAddress;

pub export fn DllMain(Handle: ?anyopaque, _:windows.DWORD, _:?anyopaque) !bool {
  //const ProcessHandle = windows.kernel32.GetModuleHandleW(null);
  const HookAddress = windows.kernel32.GetProcAddress(Handle, "CreatePenHook");
  const HookAddressInt: u64 = @intFromPtr(HookAddress);
  _ = windows.VirtualProtect(FunctionAddress, 9, windows.PAGE_EXECUTE_READWRITE, null);
  const JumpCode: [2]u8 = .{0xFF, HookAddressInt};
  FunctionAddress[0] = JumpCode[0];
  const SecondAddress: *u64 = &FunctionAddress[2];
  SecondAddress[0] = JumpCode[1];
}

pub fn CreatePenHook(Arg1: c_int, Arg2: c_int, Arg3: windows.DWORD) ?anyopaque {
  _ = try windows.WriteFile(windows.GetStdHandle(-11), "Created Pen", null);
  return ActualFunction(Arg1, Arg2, Arg3);
}

Basically this should get pointer to needed function declaration, pointer to my function, and place jump to my function into address of function declaration, while keeping the call to the original one. I'm scared to run this and first need an opinion of you. Here is C code for this:

#include "windows.h"
volatile uint32_t *FunctionAddress = (volatile uint32_t *)0x142c46108;
typedef void *CreatePenType(int, int, COLORREF);
CreatePenType *ActualFunction;

__declspec(dllexport) BOOL DllMain(HINSTANCE Handle, DWORD Reason, LPVOID Reserved) {
  ActualFunction = (CreatePenType *)(*FunctionAddress);
  const HANDLE Hook = GetProcAddress(Handle, "CreatePenHook");
  const long long HookInt = (long long)Hook;
  VirtualProtect(FunctionAddress, 9, PAGE_EXECUTE_READWRITE, NULL);

  const JumpCode[2] = {0xFF, HookInt};
  FunctionAddress[0] = JumpCode[0];
  (long long)FunctionAddress[1] = JumpCode[1];
}

void *CreatePenHook(int Arg1, int Arg2, COLORREF Arg3) {
  WriteFile(GetStdHandle(-11), "Created Pen", 12, NULL, NULL);
  return ActualFunction(Arg1, Arg2, Arg3);
}

I didn't compile it yet and ran, i want this to place jump to my function at exact address and also save what was there before, my function should output "Created Pen" in console each time pen is created

本文标签: cInject jmp to my function at address of symbol declarationStack Overflow