C# dll注入(指定进程注入指定dll)

原文出处:https://www.cnblogs.com/lonelyxmas/p/5088028.html
直接上代码:(亲测可用)

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
{
    publicpartialclass Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("kernel32.dll")] //声明API函数
publicstaticexternint VirtualAllocEx(IntPtr hwnd, int lpaddress, int size, int type, int tect);

        [DllImport("kernel32.dll")]
        publicstaticexternint WriteProcessMemory(IntPtr hwnd, int baseaddress, string buffer, int nsize, int filewriten);

        [DllImport("kernel32.dll")]
        publicstaticexternint GetProcAddress(int hwnd, string lpname);

        [DllImport("kernel32.dll")]
        publicstaticexternint GetModuleHandleA(string name);

        [DllImport("kernel32.dll")]
        publicstaticexternint CreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, int flags, int threadid);

        privatevoid 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!!");
                    }
                }
            }
        }
    }
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值