插入远程线程大体代码
unit RemoteThread;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TlHelp32;
type
TForm1 = class(TForm)
lbProcList: TListBox;
Label1: TLabel;
Button1: TButton;
Button2: TButton;
edtDllName: TEdit;
Button3: TButton;
fd: TOpenDialog;
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure lbProcListDblClick(Sender: TObject);
private
{ Private declarations }
ArrId : array[0..99] of Cardinal;
procedure InsertRemoteThread;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button3Click(Sender: TObject);
begin
if fd.Execute then
edtDllName.Text := fd.FileName;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
hSnap : Cardinal;
procinfo : TProcessEntry32;
bResult : Boolean;
i : Integer;
begin
lbProcList.Clear;
hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap <> -1) then
begin
// 初始化进程信息结构
procinfo.dwSize := sizeof(procinfo);
//初始化进程id数组
FillChar(ArrId, 100, 0);
bResult := Process32First(hSnap, procinfo);
i := 0;
while bResult <> false do
begin
lbProcList.Items.Add(procinfo.szExeFile);
ArrId := procinfo.th32ProcessID;
Inc(i);
bResult:= Process32Next(hSnap, procinfo);
end;
// 释放句柄对象资源
CloseHandle(hSnap);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if ((edtDllName.Text <> '') and (lbProcList.ItemIndex >= 0)) then
//执行插入
InsertRemoteThread
else
ShowMessage('请选择被插入进程或dll名');
end;
procedure TForm1.InsertRemoteThread;
var
hProcess : Cardinal;
nLen : Integer;
ptRemoteDllName : Pointer;
tmp : DWORD;
bResult : Boolean;
begin
// 打开进程 返回进程句柄
hProcess := OpenProcess(PROCESS_CREATE_THREAD OR PROCESS_VM_WRITE OR PROCESS_VM_OPERATION, false, ArrId[lbProcList.ItemIndex]);
// ansi 版本的参数调用
nLen := strlen(PAnsiChar(edtDllName.Text)) + 1;
ptRemoteDllName := nil;
ptRemoteDllName := VirtualAllocEx(hProcess, nil, nLen, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if Assigned(ptRemoteDllName) then else begin showmessage('远程分配虚拟内存失败'); exit; end;
bResult := WriteProcessMemory(hProcess, ptRemoteDllName, PAnsiChar(edtDllName.Text) ,nLen, tmp);
if bResult then
begin
CreateRemoteThread(hProcess, nil, 0, GetProcAddress(GetModuleHandle('kernel32.dll'), 'LoadLibraryA'), ptRemoteDllName, 0, tmp);
end
else
ShowMessage('写内存出错啦');
CloseHandle(hProcess);
end;
procedure TForm1.lbProcListDblClick(Sender: TObject);
var
hproc : Cardinal;
begin
if IDCANCEL = MessageBox(Handle, '您要结束该进程么?', '询问', MB_OKCANCEL)then exit;
hproc := OpenProcess(PROCESS_ALL_ACCESS, false, ArrId[lbProcList.ItemIndex]);
if TerminateProcess(hproc, 0) = true then showmessage('成功结束进程');
end;
end.