2010-07-05 25 views
1

我使用此代碼注入我的函數,但它導致目標進程崩潰。有誰知道爲什麼?使用remoteThread注入函數時目標進程崩潰

program Sky; 
    {$IMAGEBASE $13140000} 
uses 
    Unit2 in 'Unit2.pas', 
    chstrDec in 'chstrDec.pas',Psapi, 

    unitinject in 'unitinject.pas', ShellAPI,dialogs,registry, Windows, Messages, tlhelp32, SysUtils, Variants, Classes, Graphics, Controls, Forms; 


{$R *.res} 

function GetProcessID(ProcessName:string):Integer; 
var 
Handle:tHandle; 
Process:tProcessEntry32; 
GotProcess:Boolean; 
begin 
Handle:=CreateToolHelp32SnapShot(TH32CS_SNAPALL,0) ; 
Process.dwSize:=SizeOf(Process); 
GotProcess := Process32First(Handle,Process); 
{$B-} 
if GotProcess and (Process.szExeFile<>ProcessName) then 
repeat 
GotProcess := Process32Next(Handle,Process); 
until (not GotProcess) or (Process.szExeFile=ProcessName); 
{$B+} 
if GotProcess then Result := Process.th32ProcessID 
else Result := 0; 
CloseHandle(Handle); 
end; 

{$IMAGEBASE $13140000} 
function Main(dwEntryPoint: Pointer): longword; stdcall; 
var 
    s : String; 
begin 
    ShowMessage('hi'); 
    Result := 0; 
    Sleep(2000); 
    Main(dwEntryPoint); 
end; 
var 
x:pointer; 
    Handle:tHandle; 
PID:Cardinal; 
begin 



Pid:=getProcessID('calc.exe'); 
    Handle := OpenProcess(PROCESS_ALL_ACCESS, False, PID); 

Inject(Handle,@Main); 
CloseHandle(Handle); 

    end. 


//inject 
procedure Inject(ProcessHandle: longword; EntryPoint: pointer); 
var 
    Module, NewModule: Pointer; 
    Size, BytesWritten, TID: longword; 
begin 
    Module := Pointer(GetModuleHandle(nil)); 
    Size := PImageOptionalHeader(Pointer(integer(Module) + PImageDosHeader(Module)._lfanew + SizeOf(dword) + SizeOf(TImageFileHeader))).SizeOfImage; 
    VirtualFreeEx(ProcessHandle, Module, 0, MEM_RELEASE); 
    NewModule := VirtualAllocEx(ProcessHandle, Module, Size, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE); 
    WriteProcessMemory(ProcessHandle, NewModule, Module, Size, BytesWritten); 
    CreateRemoteThread(ProcessHandle, nil, 0, EntryPoint, Module, 0, TID); 
end; 
+0

嗨,山姆。我將你的代碼複製到這裏,讓人們更容易看到。我們傾向於通過這裏的鏈接。 :) – 2010-07-05 14:17:01

+0

thx很多梅森惠勒 – sam 2010-07-05 14:19:00

+2

世界上哪裏有你認爲打開*完成布爾評估*的想法是個好主意? – 2010-07-06 16:22:05

回答

2

Err,GetModuleHandle(nil)將成爲您的進程的值,而不是目標進程的值。即使這些值恰好相同(或者即使它們不相同),虛擬自由從流程下退出內存也是一個壞主意,可能您知道,在執行某些代碼的過程中。這是我看到的第一個可能導致潛在崩潰的地方。但是,讓我們假設它以某種方式工作。所以你分配一些新的內存來寫你的代碼,你在做什麼。但是,如果您需要,您還沒有重新定位,並且您還直接使用EntryPoint,而不再重新定位。爲什麼不使用像窗口鉤子那樣的「簡單」代碼注入方法?

下面是一些例子:

http://www.codeproject.com/KB/threads/winspy.aspx

他們是在C++中,但你似乎能夠 「德爾福ifying」 他們。

通過編寫一個包含要注入的代碼的DLL,並使用LoadLibrary加載它(通過CreateRemoteThread的方式),可以簡化當前的工作。您使用VirtualAllocEx爲DLL名稱分配空間,WriteProcessMemory將其寫入,GetModuleHandle(「kernel32.dll」)用於與CreateRemoteThread和GetProcAddress(「LoadLibraryW」)(或LoadLibraryA)一起使用的句柄傳遞給CreateRemoteThread。你絕對不應該釋放你現在沒有分配的內存。每個進程都保證在同一個地方加載kernel32(即使使用ASLR),所以通過使用LoadLibrary進行引導,可以避免許多問題需要處理,以使當前代碼可靠地工作。

+0

我沒有發現任何比這個,如果你有一個鏈接離開請 – sam 2010-07-05 14:29:57

+0

我的目標進程沒有UI,即窗口 – sam 2010-07-05 14:34:53

+0

哦,原因是在「ShowMessage('嗨')」遠程線程可以不執行相關的代碼,它應該是這樣的: LoadLibrary('kernel32.dll'); LoadLibrary('user32.dll'); MessageBox(0,'Hello World','Process Injection',0); ExitThread(0); – sam 2010-07-05 21:21:15