前段时间研究了了一下dll注入,虽然这项技术已经被写烂了,而且现在而言,这项技术已经落后了,基本上编程都到内核的级别了,不过再过时的技术对于我们刚学编程的菜鸟来说都是新鲜的,我们还是要一点一点的来学,先把基础的东西学会了,以后才能成为大鸟吗!,我开始用vb编了一个dll注入的程序,事实上dll注入很简单,无非就是调用virtualAllocEx,WriteProcessMemory,OpenProcess,CreateRemoteThread等API函数,因为我是学c#的,所以也想看一下c#这方面的文章,但在网上找了半天,没有找到一篇,也许是c#刚兴起的缘故,学c#的并不多,没办法,只好自己移植一下,因为凡是用到API函数,所有的编程的语言都是相同的,这就为我们的移植带来了方便,学c#的一般应该对API的调用概念很淡,因为c#通常不会去调用API函数,因为这些已经被封装了,在vb,vc++等语言中要结束一个进程,首先就必须要得到这个进程的句柄,然后才能进行相应的关闭进程等操作,得到句柄要用到OpenProcess API函数,结束进程要用到TerminateProcess API函数,但是在c#中你根本不需要知道这些API函数就能完成同样的功能,所以你要是想了解一下API的相关知识,学一点vb是一个很好的选择。好了!下面就开始我们的c# dll注入之旅吧!
首先需要加入以下API函数:
[DllImport("kernel32.dll")]
public static extern int VirtualAllocEx(IntPtr hwnd, Int32 lpaddress, int size, int type, Int32 tect);
[DllImport("kernel32.dll")]
public static extern Boolean 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 IntPtr CreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, int flags, int threadid);
[DllImport("kernel32.dll")]
public static extern Int32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);
[DllImport("kernel32.dll")]
public static extern Boolean VirtualFree(IntPtr lpAddress, Int32 dwSize,Int32 dwFreeType);
public static extern int VirtualAllocEx(IntPtr hwnd, Int32 lpaddress, int size, int type, Int32 tect);
[DllImport("kernel32.dll")]
public static extern Boolean 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 IntPtr CreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, int flags, int threadid);
[DllImport("kernel32.dll")]
public static extern Int32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);
[DllImport("kernel32.dll")]
public static extern Boolean VirtualFree(IntPtr lpAddress, Int32 dwSize,Int32 dwFreeType);
C#声明API比较复杂,因为是调用非托管的dll,所以要用到DllImport来调用非托管的dll,他还有很多属性在这就不多说了,网上有很介绍,可以去查一下,不过c#调用自身的变得动态链接库是倒是很方便,直接加个引用就ok了,调用dll要用的一个引用:
using System.Runtime.InteropServices;这个不要忘了加上,下面是编好的所有代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace dllinject
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
public static extern int VirtualAllocEx(IntPtr hwnd, Int32 lpaddress, int size, int type, Int32 tect);
[DllImport("kernel32.dll")]
public static extern Boolean 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 IntPtr CreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, int flags, int threadid);
[DllImport("kernel32.dll")]
public static extern Int32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);
[DllImport("kernel32.dll")]
public static extern Boolean VirtualFree(IntPtr lpAddress, Int32 dwSize,Int32 dwFreeType);
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace dllinject
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
public static extern int VirtualAllocEx(IntPtr hwnd, Int32 lpaddress, int size, int type, Int32 tect);
[DllImport("kernel32.dll")]
public static extern Boolean 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 IntPtr CreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, int flags, int threadid);
[DllImport("kernel32.dll")]
public static extern Int32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);
[DllImport("kernel32.dll")]
public static extern Boolean VirtualFree(IntPtr lpAddress, Int32 dwSize,Int32 dwFreeType);
Process pname;
UInt32 INFINITE= 0xFFFFFFFF;
Int32 PAGE_EXECUTE_READWRITE = 0x40;
Int32 MEM_COMMIT= 0x1000;
Int32 MEM_RESERVE= 0x2000;
Int32 MEM_RELEASE= 0x8000;
Int32 AllocBaseAddress;
IntPtr hwnd;
string dllname;
Int32 Pid;
Boolean ok;
Int32 loadaddr;
IntPtr ThreadHwnd;
public Form1()
{
InitializeComponent();
}
UInt32 INFINITE= 0xFFFFFFFF;
Int32 PAGE_EXECUTE_READWRITE = 0x40;
Int32 MEM_COMMIT= 0x1000;
Int32 MEM_RESERVE= 0x2000;
Int32 MEM_RELEASE= 0x8000;
Int32 AllocBaseAddress;
IntPtr hwnd;
string dllname;
Int32 Pid;
Boolean ok;
Int32 loadaddr;
IntPtr ThreadHwnd;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text == "" || textBox1.Text == null)
{
MessageBox.Show("Pid is null"); return;
}
if (textBox2.Text == "" || textBox2.Text == null)
{
MessageBox.Show("dll name is null"); return;
}
Pid = Int32.Parse(textBox1.Text);
dllname = textBox2.Text;
}
catch(Exception error)
{
MessageBox.Show(error.Message); return;
}
try
{
pname = Process.GetProcessById(Pid);
hwnd = pname.Handle;
}
catch(Exception error)
{ //当标示pid的进程不存在时发生异常;
MessageBox.Show (error.Message); return;
}
AllocBaseAddress= VirtualAllocEx(hwnd, 0, dllname.Length + 1, MEM_COMMIT+ MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (AllocBaseAddress == 0)
{
MessageBox.Show("virtualallocex fail"); return;
}
ok=WriteProcessMemory(hwnd, AllocBaseAddress, dllname, dllname.Length + 1,0);
if (!ok)
{
MessageBox.Show("writeprocessmemory fail"); return;
}
loadaddr = GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
if (loadaddr == 0)
{ //取得LoadLibraryA的地址失败时返回
MessageBox.Show("get loadlibraryA fail"); return;
}
ThreadHwnd=CreateRemoteThread(hwnd, 0, 0, loadaddr, AllocBaseAddress,0, 0);
if (ThreadHwnd ==IntPtr.Zero)
{
MessageBox.Show("createremotethread fail"); return;
}
MessageBox.Show("ok ,you can check now!!!");
WaitForSingleObject(ThreadHwnd, INFINITE);
VirtualFree(hwnd, 0, MEM_RELEASE);
//下面开始枚举模块列表;
ProcessModuleCollection pmodule = pname.Modules;
foreach (ProcessModule processm in pmodule)
{
listBox1.Items.Add(processm.FileName);
}
pname.Dispose();
}
}
}
{
try
{
if (textBox1.Text == "" || textBox1.Text == null)
{
MessageBox.Show("Pid is null"); return;
}
if (textBox2.Text == "" || textBox2.Text == null)
{
MessageBox.Show("dll name is null"); return;
}
Pid = Int32.Parse(textBox1.Text);
dllname = textBox2.Text;
}
catch(Exception error)
{
MessageBox.Show(error.Message); return;
}
try
{
pname = Process.GetProcessById(Pid);
hwnd = pname.Handle;
}
catch(Exception error)
{ //当标示pid的进程不存在时发生异常;
MessageBox.Show (error.Message); return;
}
AllocBaseAddress= VirtualAllocEx(hwnd, 0, dllname.Length + 1, MEM_COMMIT+ MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (AllocBaseAddress == 0)
{
MessageBox.Show("virtualallocex fail"); return;
}
ok=WriteProcessMemory(hwnd, AllocBaseAddress, dllname, dllname.Length + 1,0);
if (!ok)
{
MessageBox.Show("writeprocessmemory fail"); return;
}
loadaddr = GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
if (loadaddr == 0)
{ //取得LoadLibraryA的地址失败时返回
MessageBox.Show("get loadlibraryA fail"); return;
}
ThreadHwnd=CreateRemoteThread(hwnd, 0, 0, loadaddr, AllocBaseAddress,0, 0);
if (ThreadHwnd ==IntPtr.Zero)
{
MessageBox.Show("createremotethread fail"); return;
}
MessageBox.Show("ok ,you can check now!!!");
WaitForSingleObject(ThreadHwnd, INFINITE);
VirtualFree(hwnd, 0, MEM_RELEASE);
//下面开始枚举模块列表;
ProcessModuleCollection pmodule = pname.Modules;
foreach (ProcessModule processm in pmodule)
{
listBox1.Items.Add(processm.FileName);
}
pname.Dispose();
}
}
}
可以看到dll注入是很简单的,用了很短的代码就完成了所有的功能,也可以看到我没有用到OpenProcess函数,而是用了process类,这个在vb,vc++中是没有这么方便的,他们要得到进程的句柄,就要用到OpenProcess函数,关于dll的编写,我用的只是新建的一个类库,由于不知道c#有没有dll入口函数,我开始用main()当入口点,但是好像不起作用,建议dll用vc++编写,因为c++有dllmain入口函数,可以取得很好的执行。
希望本文对刚刚踏进c#编程大门的编程爱好者有所帮助,由于本身水平有限,要是文章有什么不对的地方,希望能够谅解。