C#进程注入
来源:互联网 发布:五笔练习软件 编辑:程序博客网 时间:2024/05/22 17:38
1. 得到远程进程的HANDLE(使用OpenProcess)。
2. 在远程进程中为DLL文件名分配内存(VirtualAllocEx)。
3. 把DLL的文件名(全路径)写到分配的内存中(WriteProcessMemory)
4. 使用CreateRemoteThread和LoadLibrary把你的DLL映射近远程进程。
5. 等待远程线程结束(WaitForSingleObject),即等待LoadLibrary返回。也就是说当我们的DllMain(是以DLL_PROCESS_ATTACH为参数调用的)返回时远程线程也就立即结束了。
6. 取回远程线程的结束码(GetExitCodeThtread),即LoadLibrary的返回值――我们DLL加载后的基地址(HMODULE)。
7. 释放第2步分配的内存(VirtualFreeEx)。
8. 用CreateRemoteThread和FreeLibrary把DLL从远程进程中卸载。调用时传递第6步取得的HMODULE给FreeLibrary(通过CreateRemoteThread的lpParameter参数)。
9. 等待线程的结束(WaitSingleObject)。
10.采用C++/CLI编写:
#pragma once#include <windows.h>using namespace system; namespace InjectDll { public ref class InjectDllManager { public: // szLibPath - 要加载的Dll,hProcess - 被加载的远程进程,iLibPathSize-要加载的Dll的名称的长度 void InjectLib2Process(charszLibPath[],HANDLE hProcess,int iLibPathSize) { HANDLE hThread; void* pLibRemote; DWORD hLibModule;//已加载的DLL的基地址(HMODULE); HMODULE hKernel32 =GetModuleHandle(L"Kernel32"); //初始化 szLibPath // // 1. 在远程进程中为szLibPath 分配内存 // 2. 写szLibPath到分配的内存 pLibRemote = VirtualAllocEx( hProcess,NULL, iLibPathSize,MEM_COMMIT, PAGE_READWRITE ); WriteProcessMemory( hProcess, pLibRemote,(void*)szLibPath,sizeof(szLibPath), NULL ); // 加载 "szLibPath.dll" 到远程进程 // (通过 CreateRemoteThread & LoadLibrary) hThread = CreateRemoteThread( hProcess,NULL, 0,(LPTHREAD_START_ROUTINE) GetProcAddress(hKernel32,"LoadLibraryA" ),pLibRemote, 0,NULL ); WaitForSingleObject( hThread, INFINITE ); //取得DLL的基地址 GetExitCodeThread( hThread, &hLibModule); //扫尾工作 CloseHandle( hThread ); VirtualFreeEx( hProcess, pLibRemote,iLibPathSize, MEM_RELEASE ); // 从目标进程卸载LibSpu.dll // (通过 CreateRemoteThread & FreeLibrary) hThread = CreateRemoteThread( hProcess,NULL, 0, (LPTHREAD_START_ROUTINE) GetProcAddress(hKernel32,"FreeLibrary" ),(void*)hLibModule, 0, NULL ); WaitForSingleObject( hThread, INFINITE ); // 扫尾工作 CloseHandle( hThread ); } };}
C#进程注入,模拟注入一个记事本
C#中的DLL注入
事实上dll注入很简单,无非就是调用virtualAllocEx,WriteProcessMemory,OpenProcess,CreateRemoteThread等API函数,因为我是学c#的,所以也想看一下c#这方面的文章,但在网上找了半天,没有找到一篇,也许是c#刚兴起的缘故,学c#的并不多,没办法,只好自己移植一下,因为凡是用到API函数,所有的编程的语言都是相同的,这就为我们的移植带来了方便,学c#的一般应该对API的调用概念很淡,因为c#通常不会去调用API函数,因为这些已经被封装了,在vb,vc++等语言中要结束一个进程,首先就必须要得到这个进程的句柄,然后才能进行相应的关闭进程等操作,得到句柄要用到OpenProcess API函数,结束进程要用到TerminateProcessAPI函数,但是在c#中你根本不需要知道这些API函数就能完成同样的功能,所以你要是想了解一下API的相关知识,学一点vb是一个很好的选择。好了!下面就开始我们的c# dll注入之旅吧!
首先需要加入以下API函数:
[DllImport("kernel32.dll")] public static extern intVirtualAllocEx(IntPtr hwnd, int lpaddress, int size, int type, int tect); [DllImport("kernel32.dll")] public static extern intWriteProcessMemory(IntPtr hwnd, int baseaddress, string buffer, int nsize, intfilewriten ); [DllImport("kernel32.dll")] public static extern int GetProcAddress(inthwnd, string lpname); [DllImport("kernel32.dll")] public static extern intGetModuleHandleA(string name); [DllImport("kernel32.dll")] public static extern intCreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, intflags, int threadid);
C#声明API比较复杂,因为是调用非托管的dll,所以要用到DllImport来调用非托管的dll,他还有很多属性在这就不多说了,网上有很介绍,可以去查一下,不过c#调用自身的变得动态链接库是倒是很方便,直接加个引用就ok了,调用dll要用的一个引用:usingSystem.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(IntPtrhwnd, int lpaddress, int size, int type, int tect); [DllImport("kernel32.dll")] public static extern intWriteProcessMemory(IntPtr hwnd, int baseaddress, string buffer, int nsize, intfilewriten ); [DllImport("kernel32.dll")] public static extern int GetProcAddress(inthwnd, string lpname); [DllImport("kernel32.dll")] public static extern intGetModuleHandleA(string name); [DllImport("kernel32.dll")] public static extern intCreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, intflags, 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 inpname) //遍历进程 { //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#如何:使用 MessageWindow 类来响应Windows的消息
.NET Compact Framework 提供MessageWindow 和 Message 类以生成和接收基于 Windows 的消息。MessageWindow 类使用本机代码以及窗体的句柄创建一个窗口,并执行所需的平台调用来调用本机 Windows 函数。MessageWindow类仅在.NET Compact Framework 中提供。
请使用消息窗口的窗口句柄 Hwnd 向消息窗口发送 Windows 消息。您可以使用托管代码中的 Create 或使用应用程序中的本机控件生成消息。您只能接收您生成的消息。不能使用 MessageWindow 监视操作系统消息。
当消息窗口检测到特定消息时,它将使用 WndProc 方法通知窗体,这样您就能提供代码来响应基于 Windows 的消息。
此示例演示 MessageWindow 的功能,但是没有使用本机组件进行演示。当鼠标指针处于自定义控件或 Panel 控件中的Rectangle 内时,它向一个窗体发送包含当前指针的 x 和y 坐标的 Windows 消息。自定义控件在体上显示为一个框。这两个控件都使用 OnMouseMove 方法发送消息。这些消息包含 x 坐标和 y 坐标。该窗体通过更新标签(使用当前 x 坐标和 y 坐标)和发送消息的控件来响应用户定义的 WM_BOXUPDATE 和 WM_PNLUPDATE 消息。
当鼠标位于框和面板之外时,窗体的 OnMouseMove 方法在窗体的上下文中使用 x 坐标和 y 坐标来更新标签。
using System;using System.Windows.Forms;using Microsoft.WindowsCE.Forms;namespace MsgWindow{ public class MessageWindowForm : System.Windows.Forms.Form { private System.Windows.Forms.MainMenu mainMenu1; // Create an instance of MsgWindow, aderived MessageWindow class. MsgWindow MsgWin; public MessageWindowForm() { InitializeComponent(); // Create the message window usingthis form for its constructor. this.MsgWin = new MsgWindow(this); } protected override void Dispose(bool disposing) { base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.Menu = this.mainMenu1; this.Text = "Message Window Test"; } #endregion static void Main() { Application.Run(new MessageWindowForm()); } // Process taps to generate messages // with the WParam and LParam parameters // using the X and Y mouse coordinates. protected override void OnMouseMove(MouseEventArgs e) { Message msg = Message.Create(MsgWin.Hwnd, MsgWindow.WM_CUSTOMMSG, (IntPtr)e.X, (IntPtr)e.Y); MessageWindow.SendMessage(ref msg); base.OnMouseMove(e); } // This callback method responds tothe Windows-based message. public void RespondToMessage(int x, int y) { this.Text = "X = " + x.ToString() + ", Y= " + y.ToString(); } } // Derive MessageWindow to respond to // Windows messages and to notify the // form when they are received. public class MsgWindow : MessageWindow { // Assign integers to messages. // Note that custom Window messages start at WM_USER = 0x400. public const int WM_CUSTOMMSG = 0x0400; // Create an instance of the form. private MessageWindowForm msgform; // Save a reference to the form so itcan // be notified when messages are received. public MsgWindow(MessageWindowForm msgform) { this.msgform = msgform; } // Override the default WndProcbehavior to examine messages. protected override void WndProc(ref Message msg) { switch (msg.Msg) { // If message is of interest, invoke the method on the formthat // functions as a callback to perform actions in response tothe message. case WM_CUSTOMMSG: this.msgform.RespondToMessage((int)msg.WParam, (int)msg.LParam); break; } // Call the base WndProc method // to process any messages not handled. base.WndProc(ref msg); } }}
原文出处已经无法考证,如侵犯原作者的版权可联系我。
- C#进程注入
- C#进程注入
- 进程注入方法
- 进程注入 1
- 进程注入 1
- 进程注入代码
- dll注入到进程
- 游戏进程注入代码
- DLL 注入进程
- 进程注入技术
- 进程注入方法
- 远程进程注入
- DLL注入进程技术
- 进程注入检测方法
- DLL注入、进程保护
- dll进程注入
- 进程代码注入
- 致注入进程崩溃
- Oracle 获取某个日期为星期几的方法(不积跬步,无以至千里)
- 被管理员和谐了的最高票答案“知乎数据抓取程序”(.net、c#数据挖掘)
- 母牛生娃
- malloc与free机制探索
- java中实现指定位数“0000”,缺失部分用0补充
- C#进程注入
- Android应用程序窗口设计框架四
- A Stack or A Queue?
- oracle监控自动发邮件预警
- android源码中的c c++库( android中动态和静态版本都有的库)
- android 制作.9.png图片
- 滚动条事件,当页面滚动到距顶部一定高度时某DIV自动隐藏和显示
- hi3516A 的U-boot 及linux kernel 的启动流程
- windows根据商品杀进程