自己装载进程的问题。 ( 积分: 100 )

  • 主题发起人 主题发起人 deve
  • 开始时间 开始时间
D

deve

Unregistered / Unconfirmed
GUEST, unregistred user!
用CreateProcess函数好像只能装载磁盘上的程序。我想在程序A中包含一段程序B的的代码,然后让A把B的代码装入内存,再让系统执行,该怎么做了?
 
用CreateProcess函数好像只能装载磁盘上的程序。我想在程序A中包含一段程序B的的代码,然后让A把B的代码装入内存,再让系统执行,该怎么做了?
 
你要先把b程序作为磁盘上的程序再说啊!
 
我就是想把b程序的代码镶入A中,一般一个程序能创建许多线程,线程就是这个程序的一部分。我就是想能不能把程序的一部分新开一个进程。
 
哇噻,要做病毒呀
 
借用一个以前的帖子<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls, tlhelp32;<br>{type<br> &nbsp;TProcessEntry32 = record<br> &nbsp; &nbsp;dwSize: DWORD;<br> &nbsp; &nbsp;cntUsage: DWORD;<br> &nbsp; &nbsp;th32ProcessID: DWORD;<br> &nbsp; &nbsp;th32DefaultHeapID: DWORD;<br> &nbsp; &nbsp;th32ModuleID: DWORD;<br> &nbsp; &nbsp;cntThreads: DWORD;<br> &nbsp; &nbsp;th32ParentProcessID: DWORD;<br> &nbsp; &nbsp;pcPriClassBase: integer;<br> &nbsp; &nbsp;dwFlags: DWORD;<br> &nbsp; &nbsp;szExeFile: array[0..MAX_PATH - 1] of char;<br> &nbsp;end; }<br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure FindAProcess(const AFilename: string; const PathMatch: Boolean; var ProcessID: DWORD);<br>var<br> &nbsp;lppe: TProcessEntry32;<br> &nbsp;SsHandle: Thandle;<br> &nbsp;FoundAProc, FoundOK: boolean;<br>begin<br> &nbsp;ProcessID :=0;<br> &nbsp;SsHandle := CreateToolHelp32SnapShot(TH32CS_SnapProcess, 0);<br> &nbsp;FoundAProc := Process32First(Sshandle, lppe);<br> &nbsp;while FoundAProc do<br> &nbsp;begin<br> &nbsp; &nbsp;if PathMatch then<br> &nbsp; &nbsp; &nbsp;FoundOK := AnsiStricomp(lppe.szExefile, PChar(AFilename)) = 0<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;FoundOK := AnsiStricomp(PChar(ExtractFilename(lppe.szExefile)), PChar(ExtractFilename(AFilename))) = 0;<br> &nbsp; &nbsp;if FoundOK then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;ProcessID := lppe.th32ProcessID;<br> &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;FoundAProc := Process32Next(SsHandle, lppe);<br> &nbsp;end;<br> &nbsp;CloseHandle(SsHandle);<br>end;<br><br>function EnabledDebugPrivilege(const bEnabled: Boolean): Boolean;<br>var<br> &nbsp;hToken: THandle;<br> &nbsp;tp: TOKEN_PRIVILEGES;<br> &nbsp;a: DWORD;<br>const<br> &nbsp;SE_DEBUG_NAME = 'SeDebugPrivilege';<br>begin<br> &nbsp;Result := False;<br> &nbsp;if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken)) then<br> &nbsp;begin<br> &nbsp; &nbsp;tp.PrivilegeCount := 1;<br> &nbsp; &nbsp;LookupPrivilegeValue(nil, SE_DEBUG_NAME, tp.Privileges[0].Luid);<br> &nbsp; &nbsp;if bEnabled then<br> &nbsp; &nbsp; &nbsp;tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;tp.Privileges[0].Attributes := 0;<br> &nbsp; &nbsp;a := 0;<br> &nbsp; &nbsp;AdjustTokenPrivileges(hToken, False, tp, SizeOf(tp), nil, a);<br> &nbsp; &nbsp;Result := GetLastError = ERROR_SUCCESS;<br> &nbsp; &nbsp;CloseHandle(hToken);<br> &nbsp;end;<br>end;<br><br>function AttachToProcess(const HostFile, GuestFile: string; const PID: DWORD = 0): DWORD;<br>var<br> &nbsp;hRemoteProcess: THandle;<br> &nbsp;dwRemoteProcessId: DWORD;<br> &nbsp;cb: DWORD;<br> &nbsp;pszLibFileRemote: Pointer;<br> &nbsp;iReturnCode: Boolean;<br> &nbsp;TempVar: DWORD;<br> &nbsp;pfnStartAddr: TFNThreadStartRoutine;<br> &nbsp;pszLibAFilename: PwideChar;<br>begin<br> &nbsp;Result := 0;<br> &nbsp;EnabledDebugPrivilege(True);<br> &nbsp;Getmem(pszLibAFilename, Length(GuestFile) * 2 + 1);<br> &nbsp;StringToWideChar(GuestFile, pszLibAFilename, Length(GuestFile) * 2 + 1);<br> &nbsp;if PID &amp;gt; 0 then<br> &nbsp; &nbsp; dwRemoteProcessID := PID<br> &nbsp;else<br> &nbsp; &nbsp; FindAProcess(HostFile, False, dwRemoteProcessID);<br> &nbsp;hRemoteProcess := OpenProcess(PROCESS_CREATE_THREAD + {允许远程创建线程}<br> &nbsp; &nbsp; &nbsp;PROCESS_VM_OPERATION + {允许远程VM操作}<br> &nbsp; &nbsp; &nbsp;PROCESS_VM_WRITE, {允许远程VM写}<br> &nbsp; &nbsp; &nbsp;FALSE, dwRemoteProcessId);<br> &nbsp;cb := (1 + lstrlenW(pszLibAFilename)) * sizeof(WCHAR);<br> &nbsp;pszLibFileRemote := PWIDESTRING(VirtualAllocEx(hRemoteProcess, nil, cb, MEM_COMMIT, PAGE_READWRITE));<br> &nbsp;TempVar := 0;<br> &nbsp;iReturnCode := WriteProcessMemory(hRemoteProcess, pszLibFileRemote, pszLibAFilename, cb, TempVar);<br> &nbsp;if iReturnCode then<br> &nbsp;begin<br> &nbsp; &nbsp;pfnStartAddr := GetProcAddress(GetModuleHandle('Kernel32'), 'LoadLibraryW');<br> &nbsp; &nbsp;TempVar := 0;<br> &nbsp; &nbsp;Result := CreateRemoteThread(hRemoteProcess, nil, 0, pfnStartAddr, pszLibFileRemote, 0, TempVar);<br> &nbsp;end;<br> &nbsp;Freemem(pszLibAFilename);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;AttachToProcess('Explorer.exe', extractfilepath(paramstr(0))+'Project2.dll');<br>end;<br>end.<br>///////////////下面是DLL文件<br>unit UnitDll;<br><br>interface<br><br>uses<br> &nbsp;SysUtils,<br> &nbsp;Classes,<br> &nbsp;Windows,<br> &nbsp;Dialogs;<br><br>var<br> &nbsp;hThreadHandle: Dword;<br> &nbsp;dwThreadID: Dword;<br> &nbsp;<br>implementation<br><br>{在左上角显示时间}<br>procedure ThreadProc;<br>var<br> &nbsp;hScreenDC: hdc;<br> &nbsp;SystemTime: _SYSTEMTIME;<br> &nbsp;Temp: string;<br> &nbsp;MyOutput: PChar;<br>begin<br> &nbsp;while true do<br> &nbsp;begin<br> &nbsp; &nbsp;Sleep(100);<br> &nbsp; &nbsp;hScreenDC := GetDC(0);<br> &nbsp; &nbsp;GetLocalTime(SystemTime);<br> &nbsp; &nbsp;Temp := format('Current Time is %d-%d-%d %d:%d:%d', [SystemTime.wYear,<br> &nbsp; &nbsp; &nbsp;SystemTime.wMonth,<br> &nbsp; &nbsp; &nbsp; &nbsp;SystemTime.wDay,<br> &nbsp; &nbsp; &nbsp; &nbsp;SystemTime.wHour,<br> &nbsp; &nbsp; &nbsp; &nbsp;SystemTime.wMinute,<br> &nbsp; &nbsp; &nbsp; &nbsp;SystemTime.wSecond]);<br> &nbsp; &nbsp;MyOutPut := Pchar(temp);<br> &nbsp; &nbsp;TextOut(hScreenDC, 0, 0, MyOutPut, lstrlen(MyOutPut));<br> &nbsp; &nbsp;ReleaseDC(0, hScreenDC);<br> &nbsp;end;<br>end;<br><br>initialization<br> &nbsp; &nbsp; &nbsp;hThreadHandle := CreateThread(nil, 0, @ThreadProc, nil, 0, dwThreadID);<br>finalization<br> &nbsp; &nbsp; &nbsp;if (hThreadHandle &amp;lt;&amp;gt; 0) then<br> &nbsp; &nbsp; &nbsp; &nbsp;TerminateThread(hThreadHandle, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp;<br>end.<br>
 
funxu,你好,我不是想做病毒啊,我只是想深入了解windows装载进程的原理,顺便想在软件保护方面做应用。你提供给我的代码只是把dll连接进一个应用程序中去。当主程序结束时,你系上的dll应该也背关掉吧。我就是不想dll一起死掉。<br> &nbsp; &nbsp;我想了一下,在nt中,一个应用进程并不知道自己4g空间以外的空间,它要访问其他资源只有通过操作系统。而我的要求相当于代替操作系统要完成的一些功能了,就是不知到在nt中行不行。<br> &nbsp; &nbsp;我再等等,如果没有更好的回答,100就送给你了。
 
接受答案了.
 
后退
顶部