进程防杀Delphi版(DLL部分)

本文介绍了一种通过修改DLL导入表实现API劫持的技术,详细解释了如何使用PIMAGE_IMPORT_DESCRIPTOR等数据结构定位并替换目标API,以实现对OpenProcess函数的劫持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 DLL部分:
PIMAGE_IMPORT_DESCRIPTOR = ^_IMAGE_IMPORT_DESCRIPTOR;
  PImageImportDescriptor = PIMAGE_IMPORT_DESCRIPTOR;
  _IMAGE_IMPORT_DESCRIPTOR = packed record
    CharacteristicsOrOriginalFirstThunk: DWord;
    TimeDateStamp: DWord;
    ForwarderChain: DWord;
    Name: DWord;
    FirstThunk: DWord;
  end;
  PIMAGE_THUNK_DATA = ^_IMAGE_THUNK_DATA;
  PImageThunkData = PIMAGE_THUNK_DATA;
  _IMAGE_THUNK_DATA = packed record
    Case Integer of
      0 : (ForwarderString: DWord);
      1 : (Function_: DWord);
      2 : (Ordinal: DWord);
      3 : (AddressOfData: DWord);
  end;

var

OriginalOpenProcess : function (dwDesiredAccess: DWORD; bInheritHandle: BOOL;
                                  dwProcessId: DWORD): THandle; stdcall;

function HookAPIFunction(hFromModule: HMODULE;pszFunctionModule: PAnsiChar;
  pszFunctionName: PAnsiChar;pfnNewProc: Pointer): Pointer;
var
  pfnOriginalProc: Pointer;
  pDosHeader: PImageDosHeader;
  pNTHeader: PImageNtHeaders;
  pImportDesc: PImageImportDescriptor;
  pThunk: PImageThunkData;
  dwProtectionFlags,dwScratch: DWORD;
  pszModName: PAnsiChar;
begin
  Result := nil;
  pfnOriginalProc := GetProcAddress(GetModuleHandle(pszFunctionModule),
    pszFunctionName);
  pDosHeader := PImageDosHeader(hFromModule);
  pNTHeader := PImageNTHeaders(DWORD(pDosHeader)+DWORD(pDosHeader^._lfanew));
  pImportDesc := PImageImportDescriptor(DWORD(pDosHeader)+
                                        DWORD(pNTHeader^.OptionalHeader.
                                        DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].
                                        VirtualAddress));
  while pImportDesc^.Name <> 0 do
  begin
    pszModName := PAnsiChar(Pointer(DWORD(pDosHeader) + DWORD(pImportDesc^.Name)));
    if LowerCase(pszModName) = LowerCase(pszFunctionModule) then Break;
    Inc(pImportDesc);
  end;
  if pImportDesc^.Name = 0 then Exit;
  pThunk := PImageThunkData(DWORD(pDosHeader) + DWORD(pImportDesc^.FirstThunk));
  while pThunk^.Function_ <> 0 do
  begin
    if (pThunk^.Function_ = DWORD(pfnOriginalProc)) then
    begin
      dwProtectionFlags := PAGE_READWRITE;
      VirtualProtect(@pThunk^.Function_,4096,dwProtectionFlags,@dwScratch);
      pThunk^.Function_ := DWORD(pfnNewProc);
      Result := pfnOriginalProc ;
      Break;
    end;
    Inc(pThunk);     
  end;
end;

function OpenProcessHandler(dwDesiredAccess: DWORD; bInheritHandle: BOOL;
    dwProcessId: DWORD): THandle; stdcall;
begin
  Result := OriginalOpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
  if (dwProcessID = PID) and (PID <> 0) then Result := 0;
end;

//防杀的进程ID,从注册表中获得
procedure GetHookProcessID;
var
  TempKey: HKEY;
  DataType,Size: Integer;
begin
  PID := 0;
  Size := Sizeof(Integer);
  if RegOpenKeyEx(HKEY_LOCAL_MACHINE,’Software/Vssoft’, 0,KEY_READ,
    TempKey) = ERROR_SUCCESS then
  begin
    RegQueryValueEx(TempKey,’ProcessID’,nil,@DataType,PByte(@PID),@Size);
    RegCloseKey(TempKey);
  end;
end;

function HookOpenProcess(nCode: Integer;wParam: WPARAM;lParam: LPARAM): LRESULT;stdcall;
begin
  GetHookProcessID;
  if not Assigned(OriginalOpenProcess) then
    OriginalOpenProcess := HookAPIFunction(GetModuleHandle(nil),
      ’KERNEL32.DLL’,’OpenProcess’,@OpenProcessHandler);
  Result := 0; 
end;

exports
  HookOpenProcess;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值