NET实现注入代码的问题
时间:2011-03-19 来源:唯笑志在
最近想用NET实现注入代码,代码基本上没有什么问题,但是,被注入的DLL无法执行。但是用“DLL注入进程器.exe”测试是可以执行的,让我很郁闷。大家帮忙看看。
这是注入的代码:
public static bool Inject(int pid, string dllname) { try { int hProcess = -1,libFileRemote=-1; hProcess = API.OpenProcess(API.PROCESS_ALL_ACCESS, false, pid); if (hProcess <= 0) return false; int len=dllname.Length + 1; libFileRemote = API.VirtualAllocEx(hProcess, 0,len , API.MEM_COMMIT, API.PAGE_READWRITE); if(libFileRemote<=0) return false; int written = 0; int result = API.WriteProcessMemory(hProcess, libFileRemote, dllname, len, ref written); if (result <= 0) return false; int Kernel32=API.GetModuleHandle("kernel32"); if(Kernel32<=0) return false; int ThreadRtn = API.GetProcAddress(Kernel32,"LoadLibraryA"); if(ThreadRtn<=0) return false; int threadid=0; int hRemoteThread = API.CreateRemoteThread(hProcess, 0, 0, ThreadRtn, libFileRemote, 0, ref threadid); API.WaitForSingleObject(hRemoteThread, API.INFINITE); API.CloseHandle(hRemoteThread); API.VirtualFreeEx(hProcess, libFileRemote, 0, API.MEM_RELEASE); return true; } catch (System.Exception ex) { throw ex; } }
注入C#代码和测试DLL 下载:
http://www.bmpj.net/forum-viewthread-tid-312-fromuid-19.html
相关阅读 更多 +