为什么不执行 DllMain,C#注入DLL成功

找到DLL注入的例程  http://blog.sina.com.cn/s/blog_4d5428240100reac.html

百度到的所有例程几乎跟以下代码一模一样。但经使用发现,虽然提示成功,但并没有执行 DLL入口函数 DllMain,这个广泛流传的代码,没人验证过吗? 问题在哪里呢??

另外,我使用 MHScn.exe 注入工具,注入同一个DLL,同一个进程,就能够正常执行 DllMain ,可以确定的是 DLL 是没有问题的。

实在找不到关键问题在哪里,求助求助!



主要步骤:

        A:通过窗体的Title或者进程信息找到程序的句柄。

        B:获得进程句柄、分配内存。

        C:写入数据。

        D:创建线程执行。

 

A:查找到目标程序

主要用的API有:

        [DllImport("user32.dll", EntryPoint = "FindWindow")]

private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

 

[DllImport("User32.dll", CharSet = CharSet.Auto)]

public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

         

当然获得句柄的方法有很多,这里我就用这种方法做演示。

 

调用函数找到窗体(这里找的是窗体的):

IntPtr hwnd = FindWindow(null, "目标程序");

查找进程的ID

int PID;

GetWindowThreadProcessId(hwnd, out PID);

 

B:

        主要用的API有:

        [DllImport("kernel32.dll")]

    public static extern int OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

    

    [DllImport("kernel32.dll")]

    public static extern int VirtualAllocEx(int hwnd, int lpaddress, int size, int type, int tect);

 

        调用函数获得操作的句柄

        int calcProcess = OpenProcess(2 | 8| 32, false, PID);

    分配内存空间,获得首地址

    int address = VirtualAllocEx(calcProcess, 0, @"D:\\dll.dll".Length + 1, 4096, 64);

 

C:

        主要用到的API是:

        [DllImport("kernel32.dll")]

    public static extern int WriteProcessMemory(int hwnd, int baseaddress, string buffer, int nsize, int filewriten);

    

    调用函数写入内存

    if (WriteProcessMemory(calcProcess, address, @"D:\\dll.dll", @"D:\\dll.dll".Length + 1, 0) == 0)

    {

      MessageBox.Show("写入内存失败!");

    }

 

D:

        主要用到的API有:

        [DllImport("kernel32.dll")]

    public static extern int GetModuleHandleA(string name);

 

   [DllImport("kernel32.dll")]

public static extern int GetProcAddress(int hwnd, string lpname);

 

[DllImport("kernel32.dll")]

public static extern int CreateRemoteThread(int hwnd, int attrib, int size, int address, int par, int flags, int threadid);

 

调用Kernel32 的LoadLibraryA 方法来加载咱们的DLL

if (CreateRemoteThread(calcProcess, 0, 0, GetProcAddress(GetModuleHandleA("Kernel32"), "LoadLibraryA"), address, 0, 0) == 0)

       MessageBox.Show("创建失败!");

   else

       MessageBox.Show("成功");

用C++先验证吧,避免掉到C#的托管坑里了

 WriteProcessMemory写入普通进程地址 .Net  ImagePE文件是 NT_Header可选头部的第十四个RVA,跟普通的DLL应该是有所不同的

  你可以 HOOk 链表注入,但是也需 非托管DLL作为Module

搬运:dll目录不能有中文名字

你的visual studio没装全,这种dll库调用很大可能要“C++桌面开发”和“C++移动开发"的模块,所以安装visual studio最好把这小蜜蜂论坛发帖机两个模块都选上,不管你是否使用C++。

using System.Runtime.InteropServices;看看引用是不是少了这句

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值