溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#中怎么實(shí)現(xiàn)dll注入

發(fā)布時間:2021-07-07 16:03:56 來源:億速云 閱讀:886 作者:Leah 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)C#中怎么實(shí)現(xiàn)dll注入,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

首先需要加入以下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)用自身的變得動態(tài)鏈接庫是倒是很方便,直接加個引用就ok了,調(diào)用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函數(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)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI