C# DLL注入后无法执行,求大神帮助。
近日做期末计算机程序结课设计,题目是C#开发一个DLL并注入到explorer.exe并执行,好不容易贪黑写完了终于能注入了,但是注入后无法自动执行,各种百度也不行,眼看日期临近实在没有办法了,望各路大神出手相助,谢谢!
附C# dll注入源代码(XP下可运行(32位)):
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace DllInjectA
{
class Program
{
[DllImport("kernel32.dll")] //声明API函数
public static extern int VirtualAllocEx(IntPtr hwnd, int lpaddress, int size, int type, int tect);
[DllImport("kernel32.dll")]
public static extern int WriteProcessMemory(IntPtr hwnd, int baseaddress, string buffer, int nsize, int filewriten);
[DllImport("kernel32.dll")]
public static extern int GetProcAddress(int hwnd, string lpname);
[DllImport("kernel32.dll")]
public static extern int GetModuleHandleA(string name);
[DllImport("kernel32.dll")]
public static extern int CreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, int flags, int threadid);
static void Main(string[] args)
{
int ok1;
int baseaddress;
int temp = 0;
int hack;
int yan;
string dllname;
dllname = "c:\\test.dll";
int dlllength;
dlllength = dllname.Length + 1;
Process[] pname = Process.GetProcesses(); //取得所有进程
foreach (Process name in pname) //遍历进程
{
//MessageBox.Show(name.ProcessName.ToLower());
if (name.ProcessName.ToLower().IndexOf("svchost") != -1) //所示记事本,那么下面开始注入
{
baseaddress = VirtualAllocEx(name.Handle, 0, dlllength, 4096, 4); //申请内存空间
if (baseaddress == 0) //返回0则操作失败
{
Console.WriteLine("申请内存空间失败!!");
return;
}
ok1 = WriteProcessMemory(name.Handle, baseaddress, dllname, dlllength, temp); //写内存
if (ok1 == 0)
{
Console.WriteLine("写内存失败!!");
return;
}
hack = GetProcAddress(GetModuleHandleA("Kernel32"), "LoadLibraryA"); //取得loadlibarary在kernek32.dll地址
if (hack == 0)
{
Console.WriteLine("无法取得函数的入口点!!");
return;
}
yan = CreateRemoteThread(name.Handle, 0, 0, hack, baseaddress, 0, temp); //创建远程线程
if (yan == 0)
{
Console.WriteLine("创建远程线程失败!!");
return;
}
else
{
Console.WriteLine("已成功注入dll!");
Console.ReadKey();
return;
}
}
}
}
}
}