procedure Runtest; stdcall;
begin
WriteLog('DLL: runtest');
end;
procedure InstallHook; stdcall;
begin
WriteLog('DLL: 正在安装Hook');
try
@TrampolineGetLocalTime := InterceptCreate(kernel32, 'GetLocalTime', @HookedGetLocalTime);
if Assigned(TrampolineGetLocalTime) then
WriteLog('DLL: Hook安装成功,原始函数地址: ' + IntToHex(NativeInt(@TrampolineGetLocalTime), 8))
else
WriteLog('DLL: Hook安装失败 - InterceptCreate返回nil');
except
on E: Exception do
WriteLog('DLL: Hook安装异常: ' + E.Message);
end;
end;
procedure RemoveHook; stdcall;
begin
WriteLog('DLL: 正在移除Hook');
if Assigned(TrampolineGetLocalTime) then
begin
InterceptRemove(@TrampolineGetLocalTime);
TrampolineGetLocalTime := nil;
WriteLog('DLL: Hook已移除');
end;
end;
// DLL 入口(初始化)
procedure DLLMain(Reason: DWORD);
begin
case Reason of
DLL_PROCESS_ATTACH:
begin
// 获取目标进程句柄
hProcess := OpenProcess(PROCESS_CREATE_THREAD or PROCESS_QUERY_INFORMATION or
PROCESS_VM_OPERATION or PROCESS_VM_WRITE or PROCESS_VM_READ, False,
ProcessID);
if hProcess = 0 then
begin
WriteLog('打开进程失败,错误代码: ' + inttostr(GetLastError));
RaiseLastOSError;
end;
try
// 在目标进程中分配内存
pRemoteMemory := VirtualAllocEx(hProcess, nil, Length(DllPath) + 1,
MEM_COMMIT or MEM_RESERVE, PAGE_READWRITE);
if pRemoteMemory = nil then
begin
WriteLog('分配远程内存失败');
RaiseLastOSError;
end;
try
// 写入DLL路径到目标进程
if not WriteProcessMemory(hProcess, pRemoteMemory, PChar(DllPath),
Length(DllPath) + 1, BytesWritten) then
begin
WriteLog('写入远程内存失败');
RaiseLastOSError;
end;
// 获取 LoadLibraryA 地址
LoadLibAddr := GetProcAddress(GetModuleHandle('kernel32.dll'),
'LoadLibraryA');
if LoadLibAddr = nil then
begin
WriteLog('获取LoadLibraryA地址失败');
RaiseLastOSError;
end;
// 创建远程线程调用 LoadLibraryA
hThread := CreateRemoteThread(hProcess, nil, 0, LoadLibAddr,
pRemoteMemory, 0, ThreadId);
if hThread = 0 then
begin
WriteLog('创建远程线程失败');
RaiseLastOSError;
end;