C# dll注入方法之介绍

开发 后端
本文带着读者共同体验C# DLL注入之旅。

事实上dll注入很简单,无非就是调用virtualAllocEx,WriteProcessMemory,OpenProcess,CreateRemoteThread等API函数,因为我是学c#的,所以也想看一下c# dll注入这方面的文章,但在网上找了半天,没有找到一篇,也许是c#刚兴起的缘故,学c#的并不多,没办法,只好自己移植一下,因为凡是用到API函数,所有的编程的语言都是相同的,这就为我们的移植带来了方便,学c#的一般应该对API的调用概念很淡,因为c#通常不会去调用API函数,因为这些已经被封装了,在vb,vc++等语言中要结束一个进程,首先就必须要得到这个进程的句柄,然后才能进行相应的关闭进程等操作,得到句柄要用到OpenProcess API函数,结束进程要用到TerminateProcess API函数,但是在c#中你根本不需要知道这些API函数就能完成同样的功能,所以你要是想了解一下API的相关知识,学一点vb是一个很好的选择。好了!下面就开始我们的c# dll注入之旅吧!

首先需要加入以下API函数:

  1. [DllImport("kernel32.dll")]  
  2. public static extern int VirtualAllocEx(IntPtr hwnd, int lpaddress, int size, int type, int tect);  
  3. [DllImport("kernel32.dll")]  
  4. public static extern int WriteProcessMemory(IntPtr hwnd, int baseaddress, string buffer, int nsize, int filewriten );  
  5. [DllImport("kernel32.dll")]  
  6. public static extern int GetProcAddress(int hwnd, string lpname);  
  7. [DllImport("kernel32.dll")]  
  8. public static extern int GetModuleHandleA(string name);  
  9. [DllImport("kernel32.dll")]  
  10. public static extern int CreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, int flags, int threadid);  
  11.  

C#声明API比较复杂,因为是调用非托管的dll,所以要用到DllImport来调用非托管的dll,他还有很多属性在这就不多说了,网上有很介绍,可以去查一下,不过c#调用自身的变得动态链接库是倒是很方便,直接加个引用就ok了,调用dll要用的一个引用:using System.Runtime.InteropServices;这个不要忘了加上,下面是编好的所有代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Runtime.InteropServices;  
  9. using System.Diagnostics;  
  10. namespace dllinject  
  11. {  
  12. public partial class Form1 : Form  
  13. {  
  14. [DllImport("kernel32.dll")] //声明API函数  
  15. public static extern int VirtualAllocEx(IntPtr hwnd, int lpaddress, int size, int type, int tect);  
  16. [DllImport("kernel32.dll")]  
  17. public static extern int WriteProcessMemory(IntPtr hwnd, int baseaddress, string buffer, int nsize, int filewriten );  
  18. [DllImport("kernel32.dll")]  
  19. public static extern int GetProcAddress(int hwnd, string lpname);  
  20. [DllImport("kernel32.dll")]  
  21. public static extern int GetModuleHandleA(string name);  
  22. [DllImport("kernel32.dll")]  
  23. public static extern int CreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, int flags, int threadid);  
  24. public Form1()  
  25. {  
  26. InitializeComponent();  
  27. }  
  28.  
  29. private void button1_Click(object sender, EventArgs e)  
  30. {  
  31. int ok1;  
  32. //int ok2;  
  33. //int hwnd;  
  34. int baseaddress;  
  35. int temp=0;  
  36. int hack;  
  37. int yan;  
  38. string dllname;  
  39. dllname = "c:\\dll.dll";  
  40. int dlllength;  
  41. dlllength = dllname.Length + 1;  
  42. Process[] pname = Process.GetProcesses(); //取得所有进程  
  43. foreach (Process name in pname) //遍历进程  
  44. {  
  45. //MessageBox.Show(name.ProcessName.ToLower());  
  46. if (name.ProcessName.ToLower().IndexOf("notepad") != -1) //所示记事本,那么下面开始注入  
  47. {  
  48.  
  49. baseaddress = VirtualAllocEx(name.Handle, 0, dlllength , 4096, 4); //申请内存空间  
  50. if (baseaddress == 0) //返回0则操作失败,下面都是  
  51. {  
  52. MessageBox.Show("申请内存空间失败!!");  
  53. Application.Exit();  
  54. }  
  55. ok1 = WriteProcessMemory(name.Handle, baseaddress, dllname, dlllength, temp); //写内存  
  56. if (ok1 == 0)  
  57. {  
  58.  
  59. MessageBox.Show("写内存失败!!");  
  60. Application.Exit();  
  61. }  
  62. hack = GetProcAddress(GetModuleHandleA("Kernel32"), "LoadLibraryA"); //取得loadlibarary在kernek32.dll地址  
  63. if (hack == 0)  
  64. {  
  65. MessageBox.Show("无法取得函数的入口点!!");  
  66. Application.Exit();  
  67. }  
  68. yan = CreateRemoteThread(name.Handle, 0, 0, hack, baseaddress, 0, temp); //创建远程线程。  
  69. if (yan == 0)  
  70. {  
  71. MessageBox.Show("创建远程线程失败!!");  
  72. Application.Exit();  
  73. }  
  74. else 
  75. {  
  76. MessageBox.Show("已成功注入dll!!");  
  77. }  
  78.  
  79. }  
  80.  
  81. }  
  82.  
  83. }  
  84. }  
  85.  

至此,我们的C# DLL注入之旅就圆满结束了。

【编辑推荐】

  1. C#自定义事件步步 通
  2. 如何在C#中使用存储过程(SQL Server 2000)
  3. 存储过程自动转成C#源码过程
  4. C#事件处理和自定义事件
  5. 通过C#反射特性查看自定义特性
责任编辑:book05 来源: cnblogs
相关推荐

2009-08-07 17:12:07

C# DLL函数

2009-08-03 12:57:27

C#调用DLL

2009-08-05 09:09:14

C#调用VC DLL接

2009-08-05 09:30:39

C#调用DLL函数

2009-08-05 09:40:02

C#调用DLL函数

2011-04-08 09:52:44

C++C#DLL

2009-08-20 16:25:59

C# 匿名方法

2009-08-14 17:27:56

C#方法参数

2009-07-31 14:54:48

dll函数C#导出

2009-08-24 17:46:54

C#创建XML文档

2009-09-04 11:35:05

C#方法重写

2009-08-05 17:06:39

ASP调用C# DLL

2009-08-07 17:22:36

C#调用dll导出函数

2009-07-31 17:28:35

C#语言调用DLL

2009-08-18 16:31:19

Visual C#编写

2009-08-28 17:01:43

C#构造函数

2009-08-12 18:35:36

C# ArrayLis

2009-08-10 13:05:06

C# DLLC# Delphi开发

2009-08-05 16:41:36

C#调用VC dll

2009-09-02 17:16:01

冒泡排序
点赞
收藏

51CTO技术栈公众号