-
Автор темы
- #1
Добрый день.
C#:
using System;
namespace beefhook;
class Hook
{
private void* mOriginal;
private void* mReplacement;
private static uint8[] sReplacementBytes = new uint8[](0xB8, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xE0);
private uint8[] mOriginalBytes = new uint8[sReplacementBytes.Count];
[Import("KERNEL32.dll"), CLink]
static extern bool VirtualProtect(void* lpAddress, uint32 dwSize, uint32 flNewProtect, out uint32 lpflOldProtect);
private this(void* original, void* replacement)
{
mOriginal = original;
mReplacement = replacement;
}
public static Hook Apply(void* original, void* replacement)
{
let hook = new Hook(original, replacement);
Internal.MemCpy(hook.mOriginalBytes.Ptr, (void*)hook.mOriginal, hook.mOriginalBytes.Count);
uint32 oldProtect = 0;
VirtualProtect(
(void*)hook.mOriginal,
(uint32)hook.mOriginalBytes.Count,
Windows.PAGE_EXECUTE_READWRITE,
out oldProtect
);
*(void**)(sReplacementBytes.Ptr + 1) = hook.mReplacement;
Internal.MemCpy((void*)hook.mOriginal, sReplacementBytes.Ptr, sReplacementBytes.Count);
VirtualProtect(
(void*)hook.mOriginal,
(uint32)hook.mOriginalBytes.Count,
oldProtect,
out oldProtect
);
return hook;
}
public void Remove()
{
uint32 oldProtect = 0;
VirtualProtect(
(void*)mOriginal,
(uint32)mOriginalBytes.Count,
Windows.PAGE_READWRITE,
out oldProtect
);
Internal.MemCpy((void*)mOriginal, mOriginalBytes.Ptr, mOriginalBytes.Count);
VirtualProtect(
(void*)mOriginal,
(uint32)mOriginalBytes.Count,
oldProtect,
out oldProtect
);
}
public ~this()
{
delete mOriginalBytes;
}
}