D dcba Unregistered / Unconfirmed GUEST, unregistred user! 2003-12-29 #1 如何使一个外部程序加载我写的动态链接库?<br>或者如何使我的程序进入到其他程序的内存空间
D dcba Unregistered / Unconfirmed GUEST, unregistred user! 2003-12-30 #4 不是做木马,是客户要求修改已有的exe程序,但是没有源代码<br>所以只有这样
W weiwei81123 Unregistered / Unconfirmed GUEST, unregistred user! 2003-12-30 #5 (******************************************************************************<br>* CopyRight (c) By GanHuaXin 2002<br>* All Right Reserved<br>* Email : huiyugan@263.net<br>* Date :<br>* New Develop : 2002-x-x<br>* Modified : 2001-05-26<br>******************************************************************************)<br><br>unit untInjectCode;<br><br>interface<br><br>uses<br> Windows, Sysutils;<br><br>function LoadDllToProcess(hProcess:Thandle;<br> strDllNameChar;<br> var dllHandle:HModule):BOOL;<br>function UnLoadDllFromProcess(hProcess:THandle;<br> hLibModule: HModule;<br> var bOK:BOOL):BOOL;<br><br>implementation<br><br>type<br> TLoadLibraryA = function (lpLibFileName: PAnsiChar): HMODULE; stdcall;<br> TLoadInjectInfo = Record<br> fnLoadLibraryA : TLoadLibraryA;<br> szDllName : array[0..255] of AnsiChar;<br> hDLL : HModule;<br> InjectCode : array [0..99] of byte;<br> end;<br> PLoadInjectInfo = ^TLoadInjectInfo;<br><br> TMessageBeep = function (uType: UINT): BOOL; stdcall;<br> TFreeLibrary = function (hLibModule: HMODULE): BOOL; stdcall;<br> TFreeInjectInfo = Record<br> fnFreeLibrary : TFreeLibrary;<br> hLibModule : HMODULE;<br> fnMessageBeep : TMessageBeep;<br> uBeep : UINT;<br> InjectCode : array[0..99] of byte;<br> end;<br> PFreeInjectInfo = ^TFreeInjectInfo;<br><br>function RemoteLoadFunc(p : PLoadInjectInfo)WORD;stdcall;<br>begin<br> Result := DWORD(p.fnLoadLibraryA(p.szDllName));<br>end;<br><br>function RemoteFreeFunc(p : PFreeInjectInfo)WORD;stdcall;<br>begin<br> p.fnMessageBeep(p.uBeep);<br> Result := DWORD(p.fnFreeLibrary(p.hLibModule));<br>end;<br><br>function LoadDllToProcess(hProcess:THandle;<br> strDllNameChar;<br> var dllHandle:HModule):BOOL;<br>var<br> pCode : ^Byte;<br> i : Integer;<br> InjectInfo : TLoadInjectInfo;<br> pRemoteCode : PLoadInjectInfo;<br> dwCount : DWORD;<br> dwThreadID : DWORD;<br> hThread : THandle;<br> dwExitCode : DWORD;<br><br>begin<br> result := TRUE;<br> dllHandle := 0;<br>try<br> pCode := Addr(RemoteLoadFunc);<br><br> for i:=0 to SizeOf(InjectInfo.InjectCode) - 1 do begin<br> InjectInfo.InjectCode := pCode^;<br> Inc(pCode);<br> end;<br><br> InjectInfo.fnLoadLibraryA := GetProcAddress(GetModuleHandle('Kernel32.dll'),<br> 'LoadLibraryA');<br> for i:=0 to strlen(strDllName) do begin<br> InjectInfo.szDllName := strDllName;<br> end;<br> InjectInfo.szDllName[strlen(strDllName)] := Char(0);<br><br> pRemoteCode := nil;<br> pRemoteCode := VirtualAllocEx( hProcess,<br> nil,<br> SizeOf(TLoadInjectInfo),<br> MEM_COMMIT,<br> PAGE_EXECUTE_READWRITE);<br> if (pRemoteCode = nil) then<br> RaiseLastWin32Error;<br><br> if not WriteProcessMemory(hProcess,<br> pRemoteCode,<br> @InjectInfo,<br> SizeOf(TLoadInjectInfo),<br> dwCount) then<br> RaiseLastWin32Error;<br><br> hThread := 0;<br> hThread := CreateRemoteThread( hProcess,<br> nil,<br> 0,<br> Addr(pRemoteCode^.InjectCode[0]),<br> pRemoteCode,<br> 0,<br> dwThreadId);<br> if hThread=0 then<br> RaiseLastWin32Error;<br><br> WaitForSingleObject(hThread, INFINITE);<br><br> GetExitCodeThread(hThread, dwExitCode);<br><br> dllHandle := dwExitCode;<br><br> CloseHandle(hThread);<br>finally<br> if Assigned(pRemoteCode) then<br> VirtualFreeEx( hProcess,<br> pRemoteCode,<br> SizeOf(TLoadInjectInfo),<br> MEM_RELEASE);<br>end;<br><br>end;<br><br>function UnLoadDllFromProcess(hProcess:THandle;<br> hLibModule: HModule;<br> var bOK:BOOL):BOOL;<br>var<br> pCode : ^Byte;<br> i : Integer;<br> InjectInfo : TFreeInjectInfo;<br> pRemoteCode : PFreeInjectInfo;<br> dwCount : DWORD;<br> dwThreadID : DWORD;<br> hThread : THandle;<br> dwExitCode : DWORD;<br><br>begin<br> result := TRUE;<br> bOK := TRUE;<br>try<br> pCode := Addr(RemoteFreeFunc);<br><br> for i:=0 to SizeOf(InjectInfo.InjectCode) - 1 do begin<br> InjectInfo.InjectCode := pCode^;<br> Inc(pCode);<br> end;<br><br> InjectInfo.fnFreeLibrary := GetProcAddress(GetModuleHandle('Kernel32.dll'),<br> 'FreeLibrary');<br> InjectInfo.hLibModule := hLibModule;<br> InjectInfo.fnMessageBeep := GetProcAddress(GetModuleHandle('User32.dll'),<br> 'MessageBeep');<br> InjectInfo.uBeep := 0;<br><br> pRemoteCode := nil;<br> pRemoteCode := VirtualAllocEx( hProcess,<br> nil,<br> SizeOf(TFreeInjectInfo),<br> MEM_COMMIT,<br> PAGE_EXECUTE_READWRITE);<br> if (pRemoteCode = nil) then<br> RaiseLastWin32Error;<br><br> if not WriteProcessMemory(hProcess,<br> pRemoteCode,<br> @InjectInfo,<br> SizeOf(TFreeInjectInfo),<br> dwCount) then<br> RaiseLastWin32Error;<br><br> hThread := 0;<br> hThread := CreateRemoteThread( hProcess,<br> nil,<br> 0,<br> Addr(pRemoteCode^.InjectCode[0]),<br> pRemoteCode,<br> 0,<br> dwThreadId);<br> if hThread=0 then<br> RaiseLastWin32Error;<br><br> WaitForSingleObject(hThread, INFINITE);<br><br> GetExitCodeThread(hThread, dwExitCode);<br><br> bOK := BOOL(dwExitCode);<br><br> CloseHandle(hThread);<br>finally<br> if Assigned(pRemoteCode) then<br> VirtualFreeEx( hProcess,<br> pRemoteCode,<br> SizeOf(TLoadInjectInfo),<br> MEM_RELEASE);<br>end;<br><br>end;<br><br><br>end.<br>
(******************************************************************************<br>* CopyRight (c) By GanHuaXin 2002<br>* All Right Reserved<br>* Email : huiyugan@263.net<br>* Date :<br>* New Develop : 2002-x-x<br>* Modified : 2001-05-26<br>******************************************************************************)<br><br>unit untInjectCode;<br><br>interface<br><br>uses<br> Windows, Sysutils;<br><br>function LoadDllToProcess(hProcess:Thandle;<br> strDllNameChar;<br> var dllHandle:HModule):BOOL;<br>function UnLoadDllFromProcess(hProcess:THandle;<br> hLibModule: HModule;<br> var bOK:BOOL):BOOL;<br><br>implementation<br><br>type<br> TLoadLibraryA = function (lpLibFileName: PAnsiChar): HMODULE; stdcall;<br> TLoadInjectInfo = Record<br> fnLoadLibraryA : TLoadLibraryA;<br> szDllName : array[0..255] of AnsiChar;<br> hDLL : HModule;<br> InjectCode : array [0..99] of byte;<br> end;<br> PLoadInjectInfo = ^TLoadInjectInfo;<br><br> TMessageBeep = function (uType: UINT): BOOL; stdcall;<br> TFreeLibrary = function (hLibModule: HMODULE): BOOL; stdcall;<br> TFreeInjectInfo = Record<br> fnFreeLibrary : TFreeLibrary;<br> hLibModule : HMODULE;<br> fnMessageBeep : TMessageBeep;<br> uBeep : UINT;<br> InjectCode : array[0..99] of byte;<br> end;<br> PFreeInjectInfo = ^TFreeInjectInfo;<br><br>function RemoteLoadFunc(p : PLoadInjectInfo)WORD;stdcall;<br>begin<br> Result := DWORD(p.fnLoadLibraryA(p.szDllName));<br>end;<br><br>function RemoteFreeFunc(p : PFreeInjectInfo)WORD;stdcall;<br>begin<br> p.fnMessageBeep(p.uBeep);<br> Result := DWORD(p.fnFreeLibrary(p.hLibModule));<br>end;<br><br>function LoadDllToProcess(hProcess:THandle;<br> strDllNameChar;<br> var dllHandle:HModule):BOOL;<br>var<br> pCode : ^Byte;<br> i : Integer;<br> InjectInfo : TLoadInjectInfo;<br> pRemoteCode : PLoadInjectInfo;<br> dwCount : DWORD;<br> dwThreadID : DWORD;<br> hThread : THandle;<br> dwExitCode : DWORD;<br><br>begin<br> result := TRUE;<br> dllHandle := 0;<br>try<br> pCode := Addr(RemoteLoadFunc);<br><br> for i:=0 to SizeOf(InjectInfo.InjectCode) - 1 do begin<br> InjectInfo.InjectCode := pCode^;<br> Inc(pCode);<br> end;<br><br> InjectInfo.fnLoadLibraryA := GetProcAddress(GetModuleHandle('Kernel32.dll'),<br> 'LoadLibraryA');<br> for i:=0 to strlen(strDllName) do begin<br> InjectInfo.szDllName := strDllName;<br> end;<br> InjectInfo.szDllName[strlen(strDllName)] := Char(0);<br><br> pRemoteCode := nil;<br> pRemoteCode := VirtualAllocEx( hProcess,<br> nil,<br> SizeOf(TLoadInjectInfo),<br> MEM_COMMIT,<br> PAGE_EXECUTE_READWRITE);<br> if (pRemoteCode = nil) then<br> RaiseLastWin32Error;<br><br> if not WriteProcessMemory(hProcess,<br> pRemoteCode,<br> @InjectInfo,<br> SizeOf(TLoadInjectInfo),<br> dwCount) then<br> RaiseLastWin32Error;<br><br> hThread := 0;<br> hThread := CreateRemoteThread( hProcess,<br> nil,<br> 0,<br> Addr(pRemoteCode^.InjectCode[0]),<br> pRemoteCode,<br> 0,<br> dwThreadId);<br> if hThread=0 then<br> RaiseLastWin32Error;<br><br> WaitForSingleObject(hThread, INFINITE);<br><br> GetExitCodeThread(hThread, dwExitCode);<br><br> dllHandle := dwExitCode;<br><br> CloseHandle(hThread);<br>finally<br> if Assigned(pRemoteCode) then<br> VirtualFreeEx( hProcess,<br> pRemoteCode,<br> SizeOf(TLoadInjectInfo),<br> MEM_RELEASE);<br>end;<br><br>end;<br><br>function UnLoadDllFromProcess(hProcess:THandle;<br> hLibModule: HModule;<br> var bOK:BOOL):BOOL;<br>var<br> pCode : ^Byte;<br> i : Integer;<br> InjectInfo : TFreeInjectInfo;<br> pRemoteCode : PFreeInjectInfo;<br> dwCount : DWORD;<br> dwThreadID : DWORD;<br> hThread : THandle;<br> dwExitCode : DWORD;<br><br>begin<br> result := TRUE;<br> bOK := TRUE;<br>try<br> pCode := Addr(RemoteFreeFunc);<br><br> for i:=0 to SizeOf(InjectInfo.InjectCode) - 1 do begin<br> InjectInfo.InjectCode := pCode^;<br> Inc(pCode);<br> end;<br><br> InjectInfo.fnFreeLibrary := GetProcAddress(GetModuleHandle('Kernel32.dll'),<br> 'FreeLibrary');<br> InjectInfo.hLibModule := hLibModule;<br> InjectInfo.fnMessageBeep := GetProcAddress(GetModuleHandle('User32.dll'),<br> 'MessageBeep');<br> InjectInfo.uBeep := 0;<br><br> pRemoteCode := nil;<br> pRemoteCode := VirtualAllocEx( hProcess,<br> nil,<br> SizeOf(TFreeInjectInfo),<br> MEM_COMMIT,<br> PAGE_EXECUTE_READWRITE);<br> if (pRemoteCode = nil) then<br> RaiseLastWin32Error;<br><br> if not WriteProcessMemory(hProcess,<br> pRemoteCode,<br> @InjectInfo,<br> SizeOf(TFreeInjectInfo),<br> dwCount) then<br> RaiseLastWin32Error;<br><br> hThread := 0;<br> hThread := CreateRemoteThread( hProcess,<br> nil,<br> 0,<br> Addr(pRemoteCode^.InjectCode[0]),<br> pRemoteCode,<br> 0,<br> dwThreadId);<br> if hThread=0 then<br> RaiseLastWin32Error;<br><br> WaitForSingleObject(hThread, INFINITE);<br><br> GetExitCodeThread(hThread, dwExitCode);<br><br> bOK := BOOL(dwExitCode);<br><br> CloseHandle(hThread);<br>finally<br> if Assigned(pRemoteCode) then<br> VirtualFreeEx( hProcess,<br> pRemoteCode,<br> SizeOf(TLoadInjectInfo),<br> MEM_RELEASE);<br>end;<br><br>end;<br><br><br>end.<br>
P panxiaosen Unregistered / Unconfirmed GUEST, unregistred user! 2003-12-30 #6 CreateRemoteThread 只适用于2000