日期:2014-05-18  浏览次数:20974 次

请问为什么C#远程注入DLL执行不了呢
我在网上随便下载了个注射器可以注射成功我的DLL 

但是 我参考C#注入方法
提示成功了 但是效果却没有
晕 代码贴上好乱
http://www.cnblogs.com/hcbin/archive/2010/04/17/1714134.html


C# code

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Diagnostics;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        [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);        private void button1_Click(object sender, EventArgs e)        {            int ok1;            //int ok2;            //int hwnd;            int baseaddress;            int temp = 0;            int hack;            int yan;            string dllname;            dllname = "c:\\dll.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("notepad") != -1) //所示记事本,那么下面开始注入                {                    baseaddress = VirtualAllocEx(name.Handle, 0, dlllength, 4096, 4);   //申请内存空间                    if (baseaddress == 0) //返回0则操作失败,下面都是                    {                        MessageBox.Show("申请内存空间失败!!");                        Application.Exit();                    }                    ok1 = WriteProcessMemory(name.Handle, baseaddress, dllname, dlllength, temp); //写内存                    if (ok1 == 0)                    {                        MessageBox.Show("写内存失败!!");                        Application.Exit();                    }                    hack = GetProcAddress(GetModuleHandleA("Kernel32"), "LoadLibraryA"); //取得loadlibarary在kernek32.dll地址                    if (hack == 0)                    {                        MessageBox.Show("无法取得函数的入口点!!");                        Application.Exit();                    }                    yan = CreateRemoteThread(name.Handle, 0, 0, hack, baseaddress, 0, temp); //创建远程线程。                    if (yan == 0)                    {                        MessageBox.Show("创建远程线程失败!!");                        Application.Exit();                    }                    else                    {                        MessageBox.Show("已成功注入dll!!");                    }                }            }        }    }}




------解决方案--------------------
呵呵 看了一下楼主的连接 和当时我第一次下载的那个一样 。、现在一看 还真不觉得咋滴、、、、
不过 楼主 这个代码 的确能注入成功 、、我不知道niiT所谓的效果是什么效果 ?、、、
如果你的Dll同样C#的做的话 、、、、
现在 我要注入的话Dll都是用c++写、、怎么说呢 首先 第一c++的dll可以有DllMain函数 Dll的Main函数这个不解释吧 这个函数在 Dll文件被加载的时候和被卸载的时候都会执行、、所以 在远程线程上面是用loadlibrary的时候 就会执行DllMain函数至于要在DllMain里面写什么那么 就看你的想想吧
如果 你的dll文件是c#写的的话 那么第一 没有DllMain这个函数 第二托管代码、、不排除这个东西注入到其他程序的时候 被托管的垃圾回收期灭掉 因为我以前Hook的时候 就遇到过这个问题