完整代码,原创无藏私,绝对实用。Windows10 X64 下调试通过,对 w3wp.exe, sqlserver.exe,notepad.exe,iexporer.exe 注入后,长时间运行稳定,未见异常。

要注入的全局dll(需强命名):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using EasyHook;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms; namespace ClassLibrary1
{
[Serializable]
public class HookParameter
{
public string Msg { get; set; }
public int HostProcessId { get; set; }
} public class Main : EasyHook.IEntryPoint
{
public LocalHook MessageBoxWHook = null;
public LocalHook MessageBoxAHook = null; public Main(
RemoteHooking.IContext context,
String channelName
, HookParameter parameter
)
{
MessageBox.Show(parameter.Msg, "Hooked");
} public void Run(
RemoteHooking.IContext context,
String channelName
, HookParameter parameter
)
{
try
{
MessageBoxWHook = LocalHook.Create(
LocalHook.GetProcAddress("user32.dll", "MessageBoxW"),
new DMessageBoxW(MessageBoxW_Hooked),
this);
MessageBoxWHook.ThreadACL.SetExclusiveACL(new Int32[]); MessageBoxAHook = LocalHook.Create(
LocalHook.GetProcAddress("user32.dll", "MessageBoxA"),
new DMessageBoxW(MessageBoxA_Hooked),
this);
MessageBoxAHook.ThreadACL.SetExclusiveACL(new Int32[]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
} try
{
while (true)
{
Thread.Sleep();
}
}
catch
{ }
} #region MessageBoxW [DllImport("user32.dll", EntryPoint = "MessageBoxW", CharSet = CharSet.Unicode)]
public static extern IntPtr MessageBoxW(int hWnd, string text, string caption, uint type); [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
delegate IntPtr DMessageBoxW(int hWnd, string text, string caption, uint type); static IntPtr MessageBoxW_Hooked(int hWnd, string text, string caption, uint type)
{
return MessageBoxW(hWnd, "Hooked - " + text, "Hooked - " + caption, type);
} #endregion #region MessageBoxA [DllImport("user32.dll", EntryPoint = "MessageBoxA", CharSet = CharSet.Ansi)]
public static extern IntPtr MessageBoxA(int hWnd, string text, string caption, uint type); [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
delegate IntPtr DMessageBoxA(int hWnd, string text, string caption, uint type); static IntPtr MessageBoxA_Hooked(int hWnd, string text, string caption, uint type)
{
return MessageBoxA(hWnd, "Hooked - " + text, "Hooked - " + caption, type);
} #endregion
}
}

注入主程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Reflection;
using ClassLibrary1;
using EasyHook;
using System.Diagnostics;
using System.Runtime.InteropServices; namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process); public Form1()
{
InitializeComponent();
} private bool RegGACAssembly()
{
var dllName = "EasyHook.dll";
var dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
if (!System.Runtime.InteropServices.RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
{
new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
Thread.Sleep();
} dllName = "ClassLibrary1.dll";
dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
new System.EnterpriseServices.Internal.Publish().GacRemove(dllPath);
if (!System.Runtime.InteropServices.RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
{
new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
Thread.Sleep();
} return true;
} private static bool InstallHookInternal(int processId)
{
try
{
var parameter = new HookParameter
{
Msg = "已经成功注入目标进程",
HostProcessId = RemoteHooking.GetCurrentProcessId()
}; RemoteHooking.Inject(
processId,
InjectionOptions.Default,
typeof(HookParameter).Assembly.Location,
typeof(HookParameter).Assembly.Location,
string.Empty,
parameter
);
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
return false;
} return true;
} private static bool IsWin64Emulator(int processId)
{
var process = Process.GetProcessById(processId);
if (process == null)
return false; if ((Environment.OSVersion.Version.Major > )
|| ((Environment.OSVersion.Version.Major == ) && (Environment.OSVersion.Version.Minor >= )))
{
bool retVal; return !(IsWow64Process(process.Handle, out retVal) && retVal);
} return false; // not on 64-bit Windows Emulator
} private void button1_Click(object sender, EventArgs e)
{
var p = Process.GetProcessById(int.Parse(textBox1.Text));
if (p == null)
{
MessageBox.Show("指定的进程不存在!");
return;
} if(IsWin64Emulator(p.Id) != IsWin64Emulator(Process.GetCurrentProcess().Id))
{
var currentPlat = IsWin64Emulator(Process.GetCurrentProcess().Id) ? : ;
var targetPlat = IsWin64Emulator(p.Id) ? : ;
MessageBox.Show(string.Format("当前程序是{0}位程序,目标进程是{1}位程序,请调整编译选项重新编译后重试!", currentPlat, targetPlat));
return;
} RegGACAssembly();
InstallHookInternal(p.Id);
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}

完整代码下载地址:http://download.csdn.net/download/nanfei01055/9999598

C# EasyHook MessageBox 示例(极简而全)的更多相关文章

  1. 你一定看得懂的 DDD+CQRS+EDA+ES 核心思想与极简可运行代码示例

    前言 随着分布式架构微服务的兴起,DDD(领域驱动设计).CQRS(命令查询职责分离).EDA(事件驱动架构).ES(事件溯源)等概念也一并成为时下的火热概念,我也在早些时候阅读了一些大佬的分析文,学 ...

  2. ServiceFabric极简文档-0. ServiceFabric简介

    前言: 最近ServiceFabric开源了,大家热情都比较高,官方文档大而全,但快速入手不容易找到头绪.发几篇极简的文档,跟大家分享一下,顺便为Ray的ServiceFabric部署做一下铺垫.因为 ...

  3. Asky极简教程:零基础1小时学编程,已更新前8节

    Asky极简架构 开源Asky极简架构.超轻量级.高并发.水平扩展.微服务架构 <Asky极简教程:零基础1小时学编程>开源教程 零基础入门,从零开始全程演示,如何开发一个大型互联网系统, ...

  4. 【转】手摸手,带你用vue撸后台 系列四(vueAdmin 一个极简的后台基础模板)

    前言 做这个 vueAdmin-template 的主要原因是: vue-element-admin 这个项目的初衷是一个vue的管理后台集成方案,把平时用到的一些组件或者经验分享给大家,同时它也在不 ...

  5. HTML5 极简的JS函数

    页面初始化 mui框架将很多功能配置都集中在mui.init方法中,要使用某项功能,只需要在mui.init方法中完成对应参数配置即可,目前支持在mui.init方法中配置的功能包括:创建子页面.关闭 ...

  6. CentOS下使用Postfix + Dovecot + Dnsmasq搭建极简局域网邮件系统

    背景 开发环境为局域网,工作内容需要经常查看邮件文件(*.eml),可恶的Foxmail必须验证账户才能进入主界面,才能打开eml文件查看. 无奈搭一个局域网内的邮件系统吧.极简搭建,仅用于通过Fox ...

  7. Resty 一款极简的restful轻量级的web框架

    https://github.com/Dreampie/Resty Resty 一款极简的restful轻量级的web框架 开发文档 如果你还不是很了解restful,或者认为restful只是一种规 ...

  8. 极极极极极简的的增删查改(CRUD)解决方案

    去年这个时候写过一篇全自动数据表格的文章http://www.cnblogs.com/liuyh/p/5747331.html.文章对自己写的一个js组件做了个概述,很多人把它当作了一款功能相似的纯前 ...

  9. 工具(5): 极简开发文档编写(How-to)

    缘起 一个合格的可维护项目,必须要有足够的文档,因此一个项目开发到一定阶段后需要适当的编写文档.项目的类型多种多样,有许多项目属于内部项目,例如一个内部的开发引擎,或者一个本身就是面向开发者的项目. ...

随机推荐

  1. Vue常见指令

    文本相关指令 <div id="app"> <!-- 插值表达式 --> <p>{{ msg }}</p> <!-- eg:原 ...

  2. CSS布局学习(三) - position属性定义及解释(官网直译)

    static ①元素的位置是在文档正常布局流中的位置. ②设置top right bottom left与z-index无效. ③在未指定position时,static是默认值 以下例子进行说明: ...

  3. _mount_vendor

    允许NPC售卖坐骑时进行预览

  4. FTP:500 OOPS: failed to open vsftpd log file:/var/log/vsftpd.log

    如下:从10.12.8.165 FTP 到 10.1.3.34,报failed to open vsftpd log[a4_csbdc@localhost ~]$ ftp  10.1.3.34Conn ...

  5. React Native 开发日常、常见问题总结及解决

    优点: 1.写 UI 快,跟写 HTML 差不多,flex 布局写起来很爽,而且跨平台: 2.调试方便,command + R 直接刷新 Simulator,不用像 Xcode 等待编译: 3.体验好 ...

  6. Python3+pyshark捕获数据包并保存为文件

    一.直接使用wireshark捕获数据包并保存为文件 可以使用wireshark通过图形界面的操作来实现捕获数据包并保存为文件. wireshark默认捕获的数据包保存为临时文件,如果最后退出时不选择 ...

  7. Python查看与安装

    官网下载最新的版本  https://www.python.org mac系统,最近版本的os系统默认自带python 2.7,可以通过在终端输入python或python -V zhanyunjiu ...

  8. cl 命令行配置

    VS2013啊什么老是要license,而且打开还特别庞大. 当想测试一个小东西的时候,我并不需要创建一个很大的工程,只需要编译下,运行下即可. 这时候采用 cl 命令编译会快很多. 下面是步骤: 1 ...

  9. 跟随我在oracle学习php(5)

    框架(把一个页面引入当前页面 易维护 扩展 复用)<iframe src=”” frameborder=“”> 格式:iframe <frameset> <frame&g ...

  10. private、public、protected和默认

    类中的域最好标记为private: 方法最好标记为public: private:仅对本类可见 public:对所有类可见 protected:对本包和对所有子类可见 默认(什么都不写):对本包可见 ...