function charu(const HostFile, GuestFile: string; const PID: DWORD = 0): DWORD; //插入线程
var 
hRemoteProcess: THandle; 
dwRemoteProcessId: DWORD; 
cb: DWORD; 
pszLibFileRemote: Pointer; 
iReturnCode: Boolean; 
TempVar: DWORD; 
pfnStartAddr: TFNThreadStartRoutine; 
pszLibAFilename: PwideChar; 
begin 
Result := 0; 
Debugger(True);
Getmem(pszLibAFilename, Length(GuestFile) * 2 + 1); 
StringToWideChar(GuestFile, pszLibAFilename, Length(GuestFile) * 2 + 1); 
if PID > 0 then 
dwRemoteProcessID := PID 
else 
FindAProcess(HostFile, False, dwRemoteProcessID);
hRemoteProcess := OpenProcess(PROCESS_CREATE_THREAD + {允许远程创建线程} 
PROCESS_VM_OPERATION + {允许远程VM操作} 
PROCESS_VM_WRITE, {允许远程VM写} 
FALSE, dwRemoteProcessId); 
cb := (1 + lstrlenW(pszLibAFilename)) * sizeof(WCHAR); 
pszLibFileRemote := PWIDESTRING(VirtualAllocEx(hRemoteProcess, nil, cb, MEM_COMMIT, PAGE_READWRITE)); 
TempVar := 0; 
iReturnCode := WriteProcessMemory(hRemoteProcess, pszLibFileRemote, pszLibAFilename, cb, TempVar); 
if iReturnCode then 
begin 
pfnStartAddr := GetProcAddress(GetModuleHandle('Kernel32'), 'LoadLibraryW'); 
TempVar := 0; 
Result := CreateRemoteThread(hRemoteProcess, nil, 0, pfnStartAddr, pszLibFileRemote, 0, TempVar); 
end; 
Freemem(pszLibAFilename); 
end;这句在XE2里就出错了iReturnCode := WriteProcessMemory(hRemoteProcess, pszLibFileRemote, pszLibAFilename, cb, TempVar); 那位大大能帮修改下

解决方案 »

  1.   

    TempVar: THandle;
      

  2.   


    iReturnCode := WriteProcessMemory(hRemoteProcess, 
        pszLibFileRemote, pszLibAFilename, cb, SIZE_T(TempVar)); 
      

  3.   

    谢谢2位了,能编译了,不过D7里可以成功注入DLL,XE2做的就注入不了了。
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,TlHelp32, StdCtrls;type
      TForm1 = class(TForm)
        btn1: TButton;
        procedure btn1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure FindAProcess(const AFilename: string; const PathMatch: Boolean; var ProcessID: DWORD); //查找进程
    var
    lppe: TProcessEntry32;
    SsHandle: Thandle;
    FoundAProc, FoundOK: boolean;
    begin
    ProcessID :=0;
    SsHandle := CreateToolHelp32SnapShot(TH32CS_SnapProcess, 0);
    FoundAProc := Process32First(Sshandle, lppe);
    while FoundAProc do
    begin
    if PathMatch then
    FoundOK := AnsiStricomp(lppe.szExefile, PChar(AFilename)) = 0
    else
    FoundOK := AnsiStricomp(PChar(ExtractFilename(lppe.szExefile)), PChar(ExtractFilename(AFilename))) = 0;
    if FoundOK then
    begin
    ProcessID := lppe.th32ProcessID;
    break;
    end;
    FoundAProc := Process32Next(SsHandle, lppe);
    end;
    CloseHandle(SsHandle);
    end;function Debugger(const bEnabled: Boolean): Boolean; //提升DeBug
    var
    hToken: THandle;
    tp: TOKEN_PRIVILEGES;
    a: DWORD;
    const
    SE_DEBUG_NAME = 'SeDebugPrivilege';
    begin
    Result := False;
    if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken)) then
    begin
    tp.PrivilegeCount := 1;
    LookupPrivilegeValue(nil, SE_DEBUG_NAME, tp.Privileges[0].Luid);
    if bEnabled then
    tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
    else
    tp.Privileges[0].Attributes := 0;
    a := 0;
    AdjustTokenPrivileges(hToken, False, tp, SizeOf(tp), nil, a);
    Result := GetLastError = ERROR_SUCCESS;
    CloseHandle(hToken);
    end;
    end;function charu(const HostFile, GuestFile: string; const PID: DWORD = 0): DWORD; //插入线程
    var
    hRemoteProcess: THandle;
    dwRemoteProcessId: DWORD;
    cb: DWORD;
    pszLibFileRemote: Pointer;
    iReturnCode: Boolean;
    TempVar: DWORD;
    tempvar2:THandle;
    pfnStartAddr: TFNThreadStartRoutine;
    pszLibAFilename: PwideChar;
    begin
    Result := 0;
    Debugger(True);
    Getmem(pszLibAFilename, Length(GuestFile) * 2 + 1);
    StringToWideChar(GuestFile, pszLibAFilename, Length(GuestFile) * 2 + 1);
    if PID > 0 then
    dwRemoteProcessID := PID
    else
    FindAProcess(HostFile, False, dwRemoteProcessID);
    hRemoteProcess := OpenProcess(PROCESS_CREATE_THREAD + {允许远程创建线程}
    PROCESS_VM_OPERATION + {允许远程VM操作}
    PROCESS_VM_WRITE, {允许远程VM写}
    FALSE, dwRemoteProcessId);
    cb := (1 + lstrlenW(pszLibAFilename)) * sizeof(WCHAR);
    pszLibFileRemote := PWIDESTRING(VirtualAllocEx(hRemoteProcess, nil, cb, MEM_COMMIT, PAGE_READWRITE));
    TempVar := 0;
    tempvar2:=0;
    iReturnCode := WriteProcessMemory(hRemoteProcess, pszLibFileRemote, pszLibAFilename, cb, tempvar2);
    if iReturnCode then
    begin
    pfnStartAddr := GetProcAddress(GetModuleHandle('Kernel32'), 'LoadLibraryW');
    TempVar := 0;
    Result := CreateRemoteThread(hRemoteProcess, nil, 0, pfnStartAddr, pszLibFileRemote, 0, TempVar);
    end;
    Freemem(pszLibAFilename);
    end;procedure TForm1.btn1Click(Sender: TObject);
    begin
      charu('cstrike.exe',extractfilepath(paramstr(0))+'MetaProtect.dll');
    end;end.