fedyaqq34356/KeyLogger
GitHub: fedyaqq34356/KeyLogger
一个基于 Python 的轻量级跨平台键盘记录器,能够实时捕获并保存键盘输入到文本文件中。
Stars: 0 | Forks: 0
# KeyLogger
一个用 Python 编写的轻量级、跨平台键盘记录器。
可捕获键盘输入并实时保存到 `.txt` 文件中。
支持 **Linux**(任何发行版)和 **Windows 10/11**。
## 功能
### 核心功能
- **实时保存**:每次按键都会被立即写入并刷新到磁盘
- **自动文件命名**:如果日志文件已存在,会自动创建带编号的文件
- **Unicode 支持**:正确记录所有语言——俄语、乌克兰语、中文、阿拉伯语等
- **特殊按键处理**:空格、Enter、Tab 写入为实际字符;其他按键如 `[BS]`、`[DEL]` 写入在括号中
- **安全退出**:按 `ESC` 键可优雅地停止记录器
### 文件命名逻辑
| 情况 | 创建的文件 |
|-----------|-------------|
| 首次运行 | `txt.txt` |
| `txt.txt` 已存在 | `txt1.txt` |
| `txt.txt` 和 `txt1.txt` 已存在 | `txt2.txt` |
| 依此类推... | `txt3.txt`, `txt4.txt`, ... |
## 环境要求
- Python 3.8+
- `pynput` 库
## 安装说明
**克隆仓库:**
```
git clone https://github.com/fedyaqq34356/KeyLogger.git
cd keylogger
```
**创建虚拟环境:**
```
python -m venv venv
```
**激活虚拟环境:**
Windows:
```
venv\Scripts\activate
```
Linux / macOS:
```
source venv/bin/activate
```
**安装依赖:**
```
pip install -r requirements.txt
```
## 用法
**运行键盘记录器:**
```
python main.py
```
在 Linux 上,您可能需要 root 权限:
```
sudo /path/to/venv/bin/python main.py
```
**控制台输出:**
```
[KeyLogger] Saving to: txt.txt
[KeyLogger] Press ESC to stop.
```
**停止键盘记录器:**
按下 `ESC` —— 文件将自动保存并关闭。
## 项目结构
```
keylogger/
├── main.py # Entry point, keyboard listener
├── logger.py # File management and write logic
├── config.py # Configuration (filename, encoding)
├── requirements.txt # Python dependencies
└── README.md # This file
```
## 工作原理
```
User presses a key
↓
on_press() in main.py
↓
├─ Regular key (letter, digit, symbol) → write char as-is
├─ Space / Enter / Tab → write as real whitespace
├─ Backspace / Delete → write [BS] / [DEL]
├─ Shift / Ctrl / Alt → skip (write nothing)
└─ Other special key → write [key_name]
↓
KeyLogger.write() in logger.py
↓
Immediately flushed to txt.txt
```
## 按键行为参考
| 按键 | 记录为 |
|-----|-----------|
| 字母、数字、符号 | 原样记录(`a`, `1`, `!`) |
| 空格 | ` `(空格字符) |
| Enter | `\n`(换行符) |
| Tab | `\t`(制表符) |
| Backspace | `[BS]` |
| Delete | `[DEL]` |
| Caps Lock | `[CAPS]` |
| Shift / Ctrl / Alt | *(无记录)* |
| F1–F12、方向键等 | `[f1]`、`[up]` 等 |
| ESC | 停止程序 |
## 配置
编辑 `config.py` 以更改文件命名或编码:
```
BASE_FILENAME = "txt" # Base name for log files
EXTENSION = ".txt" # File extension
ENCODING = "utf-8" # File encoding (supports all languages)
```
## 故障排除
**问题:使用 sudo 时出现 `ModuleNotFoundError: No module named 'pynput'`**
当使用 `sudo` 时,它使用的是系统 Python,而不是您的 venv。
解决方案 —— 使用 venv 中 Python 的完整路径运行:
```
sudo /path/to/venv/bin/python main.py
```
或者将 pynput 安装到系统 Python 中:
```
sudo pip install pynput --break-system-packages
```
**问题:在 Linux 上无法捕获任何按键**
某些 Linux 发行版要求用户位于 `input` 组中或以 root 身份运行。
```
sudo usermod -aG input $USER
# 然后注销并重新登录
```
**问题:非拉丁语系布局在文件中显示为乱码**
确保在 `config.py` 中设置了 `ENCODING = "utf-8"`。请使用兼容 UTF-8 的编辑器打开 `.txt` 文件。
## 依赖项
- [pynput](https://pypi.org/project/pynput/) —— 跨平台键盘和鼠标监控
## 许可证
本项目基于 **GNU General Public License v3.0** 授权。
```
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
```
完整许可证请参见:https://www.gnu.org/licenses/gpl-3.0.en.html
标签:meg, Python, 信息安全, 无后门, 监控工具, 逆向工具, 键盘记录器