一个用于检测服务运行日志中潜在安全风险并自动触发修复操作的系统。
本系统能够从混合格式的日志文件中检测潜在风险(如SQL注入、XSS攻击、敏感信息泄露等),并在检测到风险时自动触发修复操作(如屏蔽敏感字段、生成告警信息等)。
- 日志解析:支持解析多种格式的日志(JSON、普通文本、半结构化字段)
- 风险检测:
- 基于规则的检测:检测明显的恶意模式
- 基于机器学习的检测:使用TF-IDF + Logistic Regression区分正常请求和恶意请求
- 修复与响应:
- 替换日志中的敏感字段(如邮箱、手机号等)
- 生成JSON格式告警信息
- 提供修复建议
- 系统集成:
- 提供CLI命令行工具
- 提供REST API接口
系统由以下几个主要模块组成:
- parser.py:日志解析器,负责从混合格式日志中提取关键字段
- detector.py:风险检测模块,负责检测日志中的潜在风险
- repair.py:修复与响应模块,负责在检测到风险时执行修复操作
- main.py:主程序,提供CLI和REST API接口
- Python 3.6+
- scikit-learn:用于机器学习模型
- numpy:科学计算库
- flask(可选):用于REST API服务
安装依赖:
pip install scikit-learn numpy flask
python main.py --file sample_logs.txt --output results.json
python main.py --text "User registered with email: user@example.com and password: 123456" --format text
python main.py --file sample_logs.txt --summary
python main.py --file sample_logs.txt --verbose
启动API服务:
python main.py --api --port 8000
-
分析单条日志
POST /analyze Content-Type: application/json { "log": "User registered with email: user@example.com and password: 123456" }
-
分析日志文件
POST /analyze_file Content-Type: multipart/form-data file: [上传日志文件]
-
健康检查
GET /health
192.168.1.103 - - [20/Sep/2025:10:18:22 +0800] "GET /index.php?id=1' OR '1'='1 HTTP/1.1" 403 287
{
"original_log": {
"format": "structured",
"ip": "192.168.1.103",
"timestamp": "20/Sep/2025:10:18:22 +0800",
"request": "GET /index.php?id=1' OR '1'='1 HTTP/1.1",
"status": "403",
"size": "287",
"method": "GET",
"path": "/index.php?id=1' OR '1'='1",
"line_number": 4,
"raw": "192.168.1.103 - - [20/Sep/2025:10:18:22 +0800] \"GET /index.php?id=1' OR '1'='1 HTTP/1.1\" 403 287"
},
"risks": [
{
"type": "sql_injection",
"pattern_index": 1,
"matches": ["OR '1'='1"],
"confidence": "high"
}
],
"risk_level": "high",
"risk_score": 10.0,
"needs_action": true,
"repaired_log": {
"format": "structured",
"ip": "192.168.1.103",
"timestamp": "20/Sep/2025:10:18:22 +0800",
"request": "GET /index.php?id=1' OR '1'='1 HTTP/1.1",
"status": "403",
"size": "287",
"method": "GET",
"path": "/index.php?id=1' OR '1'='1",
"line_number": 4,
"raw": "192.168.1.103 - - [20/Sep/2025:10:18:22 +0800] \"GET /index.php?id=1' OR '1'='1 HTTP/1.1\" 403 287"
},
"sensitive_fields": [],
"suggestions": [
"使用参数化查询替代字符串拼接",
"实施输入验证和过滤",
"限制数据库用户权限",
"使用ORM框架"
],
"alert": {
"level": "high",
"ip": "192.168.1.103",
"action": "blocked",
"reason": "sql_injection",
"timestamp": "20/Sep/2025:10:18:22 +0800",
"request": "GET /index.php?id=1' OR '1'='1",
"risk_score": 10.0
}
}
系统能够检测以下类型的风险:
- SQL注入:检测SQL注入攻击模式
- XSS攻击:检测跨站脚本攻击模式
- 路径遍历:检测目录遍历攻击模式
- 命令注入:检测命令注入攻击模式
- 敏感信息泄露:检测日志中的敏感信息(如密码、API密钥、邮箱、手机号等)
- 异常行为:检测异常行为模式(如登录失败、暴力破解等)
在detector.py
中的RiskDetector
类中,可以通过扩展risk_patterns
字典来添加新的风险检测规则:
self.risk_patterns = {
'new_risk_type': [
r'(?i)(your_regex_pattern_here)',
],
# ...
}
在parser.py
中的LogParser
类中,可以通过扩展log_formats
列表来添加新的日志格式支持:
self.log_formats = [
# 添加新的日志格式正则表达式
re.compile(r'your_log_format_regex_here'),
# ...
]
- 系统默认会在首次运行时生成并保存机器学习模型
- 处理大型日志文件时,建议使用
--summary
选项查看处理摘要 - REST API服务默认只监听本地地址(127.0.0.1),如需对外提供服务,请指定
--host 0.0.0.0
MIT License