gcedugc/TFG_Audit_Logs
GitHub: gcedugc/TFG_Audit_Logs
基于以太坊智能合约和Merkle树的日志审计系统,确保审计记录的完整性和不可篡改性。
Stars: 0 | Forks: 0
# TFG Audit Logs - 基于 Blockchain 的日志审计系统
该系统利用 Merkle 树和 Ethereum 上的 Smart Contracts,确保审计记录的 **完整性和不可篡改性**。
如果攻击者修改或删除了系统日志,验证器会通过将重新计算的哈希值与不可变地存储在 blockchain 上的哈希值进行比对,从而检测到篡改行为。
## 架构
该系统遵循 **Off-Chain / On-Chain** 混合架构:
```
Logs (disco) --> Middleware (Merkle Tree) --> Smart Contract (Ethereum)
|
Dashboard (Flask) <-- Verificador <-------------------+
```
| 层级 | 组件 | 描述 |
|------|-----------|-------------|
| 生成 | `creador_logs.py` | Honeypot 模拟器,生成攻击流量(SSH, SQL injection 等) |
| 中间件 | `middleware.py` + `merkle.py` | 将日志分批打包,构建 Merkle 树并将根哈希锚定到 blockchain |
| Blockchain | `LogAuditor.sol` | Smart Contract,以不可变方式存储 Merkle Roots |
| 验证 | `verificador.py` | 重新计算哈希值并与 blockchain 交叉比对以检测篡改 |
| 展示 | `app.py` + `index.html` | Web Dashboard,包含实时日志和审计按钮 |
## 技术栈
- **Blockchain:** Ethereum (Ganache v7.x, 本地)
- **Smart Contract:** Solidity 0.8.0
- **后端:** Python 3.11, Web3.py, Flask
- **加密:** SHA-256, Merkle Trees
- **前端:** HTML5, JavaScript, TailwindCSS
## 项目结构
```
TFG_Audit_Logs/
├── src/
│ ├── contracts/
│ │ └── LogAuditor.sol # Smart Contract
│ ├── middleware/
│ │ ├── merkle.py # Implementacion del Arbol de Merkle
│ │ ├── middleware.py # Agente de anclaje (watchdog + batching)
│ │ └── verificador.py # Motor de verificacion de integridad
│ └── web/
│ ├── app.py # Servidor Flask (API + dashboard)
│ └── templates/
│ └── index.html # Dashboard de auditoria
├── scripts/
│ ├── creador_logs.py # Simulador de logs (honeypot)
│ └── deploy.py # Despliegue del Smart Contract
├── start.py # Pipeline de inicio unificado
├── requirements.txt # Dependencias Python
└── .env # Variables de entorno (no incluido)
```
## 前置条件
- Python 3.11+
- Node.js (用于 Ganache)
- Ganache CLI (`npm install -g ganache`)
## 安装
```
# 克隆仓库
git clone https://github.com/gcedugc/TFG_Audit_Logs.git
cd TFG_Audit_Logs
# 创建虚拟环境并安装依赖
python -m venv venv
venv\Scripts\activate # Windows
# source venv/bin/activate # Linux/Mac
pip install -r requirements.txt
# 配置环境变量
# 在项目根目录下创建一个 .env 文件,内容如下:
# GANACHE_URL=http://127.0.0.1:7545
# PRIVATE_KEY=tu_clave_privada_de_ganache
```
## 使用
### 快速启动(推荐)
```
python start.py
```
这将自动启动:Ganache,部署合约,启动日志模拟器、锚定中间件和 Web Dashboard。
在浏览器中打开 **http://localhost:5000**。
### 手动启动
```
# 1. 在另一个终端启动 Ganache
ganache --port 7545
# 2. 部署 Smart Contract
python scripts/deploy.py
# 3. 启动日志模拟器
python scripts/creador_logs.py
# 4. 启动锚定 middleware
python src/middleware/middleware.py
# 5. 启动 web dashboard
python src/web/app.py
```
## 工作原理
1. **模拟器** 生成模拟攻击日志(SSH 暴力破解、SQL 注入、端口扫描)
2. **中间件** 监控日志文件,每 5 行分为一批
3. 为每批日志构建 **Merkle 树** 并提取根哈希(32 字节)
4. 根哈希通过单笔交易发送到 **Smart Contract**(节省 Gas)
5. 在 Dashboard 中点击“执行审计”时,**验证器** 从磁盘重新计算哈希值并与 blockchain 上的数据比对
6. 如果任何日志被修改或删除,系统将检测到并显示原始证据
## Gas 成本
| 操作 | Gas |
|-----------|-----|
| 合约部署 | ~765,000 |
| `saveBatchRoot` (每批) | ~210,000 |
使用 Merkle Trees 将成本降低至 **每批日志仅需一笔交易**,与分组日志的数量无关。
标签:Flask, Ganache, Merkle Tree, OISF, Python, SHA-256, Solidity, SSH注入, TailwindCSS, TFG, Web3.py, 中间件, 以太坊, 分布式账本, 区块链, 后端开发, 子域名变形, 审计日志, 数据可视化, 数据防篡改, 无后门, 日志完整性, 日志管理, 智能合约, 毕业设计, 网络安全, 蜜罐, 证书利用, 逆向工具, 隐私保护, 默克尔树