您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關C#中怎么實現dll注入,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
首先需要加入以下API函數:
[DllImport("kernel32.dll")] 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);
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")] //聲明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); public Form1() { InitializeComponent(); } 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!!"); } } } } }
關于C#中怎么實現dll注入就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。