hardikcloud/KeyGuard

GitHub: hardikcloud/KeyGuard

一个用于安全教学演示的实时键盘记录监控工具,通过 Telegram Bot 远程推送按键数据和屏幕截图。

Stars: 0 | Forks: 0

# KeyGuard - 笔记本电脑安全监控器 ![KeyGuard 横幅](https://static.pigsec.cn/wp-content/uploads/repos/cas/a1/a1f3d299a43186b0aa4bbc343ca96d489faeff876d18c270e0252377f3081133.png) **Hardik Prajapati — 安全研究员** 一个带有 Telegram 告警功能的实时键盘记录监控器,作为大学安全项目开发。演示了键盘记录技术、daemon 进程管理以及 Telegram Bot API 集成。 ## 功能 - **全局按键捕获** — 通过 `pynput` (X11 后端) 捕获系统范围内的所有按键 - **智能缓冲** — 将按键累积成完整的单词,在按下 Enter 键或 3 秒不活动后发送 - **修饰键检测** — 检测 Ctrl、Alt、Shift 组合键 (`[Ctrl+C]`, `[Alt+Tab]`) - **窗口跟踪** — 自动检测活动窗口的变化 - **实时 Telegram 报告** — 每个按键/缓冲区刷新会立即发送到你的 Telegram - **带时间戳的消息** — 每条消息都包含 `[HH:MM:SS]` 时间戳 - **两种执行模式** — 前台 (可见终端) 和后台 (daemon) - **Telegram 命令** — 通过手机使用 `/status`、`/screenshot`、`/stop` 进行控制 ## 前置条件 - 使用 **X11** 显示服务器的 **Linux** (运行 `echo $XDG_SESSION_TYPE` 进行验证) - **Python 3.8+** - **xdotool** — 窗口标题检测 (`apt install xdotool`) - **ImageMagick** — 截屏捕获 (`apt install imagemagick`) ## 快速开始 ### 1. 安装系统依赖 ``` sudo apt install xdotool imagemagick ``` ### 2. 设置项目 ``` cd keyguard python3 -m venv venv source venv/bin/activate pip install pynput requests ``` ### 3. 创建 Telegram bot 1. 打开 Telegram → 搜索 **[@BotFather](https://t.me/BotFather)** 2. 发送 `/newbot` → 选择一个名称 (例如 `Sniffer`) → 获取你的 **bot token** 3. 在 Telegram 上搜索你的 bot → 按下 **Start** 4. 获取你的 **chat ID**: ``` # 替换为您的 token curl "https://api.telegram.org/bot/getUpdates" ``` 在响应中查找 `"chat":{"id":123456789}`。 ### 4. 配置 编辑 `config.py`: ``` BOT_TOKEN = "123456789:ABCdef123GhIjklMNOpqrsTUVwxyz" CHAT_ID = "123456789" ``` ### 5. 运行 ``` source venv/bin/activate # 在前台测试 python main.py --foreground # 作为 daemon 运行 python main.py --background # 检查状态 python main.py --status # 停止 daemon python main.py --stop ``` ![在前台模式下运行 KeyGuard](https://static.pigsec.cn/wp-content/uploads/repos/cas/13/13a292baf180df1a504cd581482def91fe02904c379065cf74dafb5037568a05.png) ## 用法 ### CLI 选项 | 选项 | 描述 | |---|---| | `--foreground` | 在可见终端模式下运行 (Ctrl+C 停止) | | `--background` | 作为后台 daemon 运行 | | `--status` | 检查 daemon 是否正在运行 | | `--stop` | 停止后台 daemon | ### Telegram 命令 在 Telegram 上向你的 bot 发送以下命令: | 命令 | 操作 | |---|---| | `/status` | 显示运行时间、捕获的按键、活动窗口 | | `/screenshot` | 捕获屏幕并发送图像 | | `/stop` | 优雅地停止 KeyGuard | | `/help` | 列出可用命令 | ### Telegram 输出示例 ![显示按键、窗口标题和缓冲文本的 Telegram 聊天](https://static.pigsec.cn/wp-content/uploads/repos/cas/38/383c2fee0112fa82e8fff22b4f56882c4f657646c5f232cd07a459720ec668b3.jpg) Telegram bot 会显示窗口变化、带时间戳的按键、修饰键组合,并响应 `/screenshot` 等命令。 ![带有窗口跟踪的 Instagram 实时输入捕获](https://static.pigsec.cn/wp-content/uploads/repos/cas/14/14f4359f8c37438e9e26aca3efe3c6caafe63b4aae5dd55fe1bf7e108a79cbc1.png) 活动窗口跟踪会自动检测你何时在应用程序之间切换 — Instagram、终端、浏览器或代码编辑器。 ![Telegram 命令:status、screenshot、help](https://static.pigsec.cn/wp-content/uploads/repos/cas/3f/3fbda99b1ebe7ffa5785ca34090847203a8d0df40dcaf4bd6677710ffa0e7092.png) 通过手机使用 `/status`、`/screenshot`、`/stop` 和 `/help` 命令远程控制 KeyGuard。 ``` [Window] Firefox — Instagram [10:58:32] hello from instagram [10:59:01] my name is john [Ctrl+C] [10:59:05] testing world [Window] Code — main.py [11:00:12] def new_feature(): ``` ## 架构 ``` Keystroke → pynput.Listener → queue.Queue → Reporter thread → Telegram API ↑ Window tracker ↑ Command listener (polling) ``` - **keylogger.py** — 通过 `pynput` 捕获全局按键,检测修饰键组合,将格式化后的按键排入队列 - **reporter.py** — 带有文本缓冲区的工作线程,在按下 Enter/3秒不活动/溢出时刷新,跟踪窗口标题 - **commands.py** — 长轮询 Telegram `getUpdates` 以获取 `/status`、`/screenshot`、`/stop` 命令 - **main.py** — 带有前台/后台模式、daemon 化、信号处理的 CLI 入口点 ## 项目结构 ``` keyguard/ ├── main.py # CLI entry point ├── keylogger.py # Key capture engine ├── reporter.py # Telegram reporter + buffer ├── commands.py # Telegram command listener ├── config.py # Bot token & chat ID ├── requirements.txt # Python dependencies ├── .gitignore ├── images/ │ ├── img1.jpeg # Telegram chat output │ ├── img2.jpeg # Monitored apps overview │ ├── insta.png # Instagram tracking demo │ ├── commands.png # Telegram commands │ └── run.png # Foreground mode execution └── README.md ``` ## 许可证 MIT — 仅限教育用途。 ## 作者 **Hardik Prajapati** — 安全研究员与渗透测试员
标签:Python, 守护进程, 数据外发, 无后门, 网络信息收集, 逆向工具, 键盘记录器