這篇文章將為大家詳細(xì)講解有關(guān)C#中怎么實(shí)現(xiàn)dll注入,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、雙遼ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的雙遼網(wǎng)站制作公司
首先需要加入以下API函數(shù):
[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比較復(fù)雜,因?yàn)槭钦{(diào)用非托管的dll,所以要用到DllImport來調(diào)用非托管的dll,他還有很多屬性在這就不多說了,網(wǎng)上有很介紹,可以去查一下,不過c#調(diào)用自身的變得動(dòng)態(tài)鏈接庫是倒是很方便,直接加個(gè)引用就ok了,調(diào)用dll要用的一個(gè)引用:using System.Runtime.InteropServices;這個(gè)不要忘了加上,下面是編好的所有代碼:
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函數(shù) 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(); //取得所有進(jìn)程 foreach (Process name in pname) //遍歷進(jìn)程 { //MessageBox.Show(name.ProcessName.ToLower()); if (name.ProcessName.ToLower().IndexOf("notepad") != -1) //所示記事本,那么下面開始注入 { baseaddress = VirtualAllocEx(name.Handle, 0, dlllength , 4096, 4); //申請內(nèi)存空間 if (baseaddress == 0) //返回0則操作失敗,下面都是 { MessageBox.Show("申請內(nèi)存空間失?。。?); Application.Exit(); } ok1 = WriteProcessMemory(name.Handle, baseaddress, dllname, dlllength, temp); //寫內(nèi)存 if (ok1 == 0) { MessageBox.Show("寫內(nèi)存失敗??!"); Application.Exit(); } hack = GetProcAddress(GetModuleHandleA("Kernel32"), "LoadLibraryA"); //取得loadlibarary在kernek32.dll地址 if (hack == 0) { MessageBox.Show("無法取得函數(shù)的入口點(diǎn)??!"); Application.Exit(); } yan = CreateRemoteThread(name.Handle, 0, 0, hack, baseaddress, 0, temp); //創(chuàng)建遠(yuǎn)程線程。 if (yan == 0) { MessageBox.Show("創(chuàng)建遠(yuǎn)程線程失?。?!"); Application.Exit(); } else { MessageBox.Show("已成功注入dll!!"); } } } } }
關(guān)于C#中怎么實(shí)現(xiàn)dll注入就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。