这是一款判断恶意爬虫以python编写的工具
Access rate limit exceeded. Please log in to continue.
python判断恶意爬虫工具
这是一款判断恶意爬虫以python编写的工具
使用说明
每周更新一下TOR.txt将https://check.torproject.org/torbulkexitlist内的ip复制到TOR.txt文档内,TOD.txt文档需要和脚本放在同一目录下
安装依赖
在运行脚本前,需要安装必要的Python包:
bash
pip install elasticsearch
针对不同ES版本需要安装不同依赖哦,一下命令安装的是8版本的ES
例如
python3 -m pip install "elasticsearch>=8.0.0,<9.0.0"
命令行参数
脚本支持以下命令行参数:
--hosts: Elasticsearch主机地址(必需),例如--hosts http://localhost:9200--username: Elasticsearch用户名(可选)--password: Elasticsearch密码(可选)--index: 索引模式,默认logstash-*--hours: 分析的时间范围(小时),默认24--export: 导出爬虫IP到指定文件--test-ua: 测试单个User-Agent字符串--version: 指定ES版本,默认是8--test-fields: 测试字段映射是否正确
使用示例
在开始前需要先执行
python3 crawler_detector.py --hosts http://安装ES服务器ip地址:ES开放的端口号 --username ES账号名字 --password ES密码 --index *nginx* --test-fields
执行这个目的是看ES与脚本内的参数是否一致,不一致需要更改脚本内的参数,同时需要注意的是--index后面的索引,有的时候直接用泛泛的索引可能同时匹配到多个索引,会出现报错,出现报错后可以通过kibana找到一条日志找到index索引名字直接填写到命令参数--index后面。不想更改脚本的参数需要在输命令的时候输入你ES上面的参数具体可以执行
python3 crawler_detector.py --help
-
测试单个User-Agent:
bash
python3 crawler_detector.py --test-ua "Googlebot/2.1" -
分析最近24小时的日志:
bash
python3 crawler_detector.py --hosts http://localhost:9200 --index nginx-* -
分析最近72小时的日志并导出爬虫IP:
bash
python3 crawler_detector.py --hosts http://localhost:9200 --hours 72 --export crawler_ips.txtcrawler_ips.txt可以自己直接手动创建在脚本同一目录下
-
使用认证信息连接Elasticsearch:
bash
python3 crawler_detector.py --hosts http://localhost:9200 --username elastic --password yourpassword
注意事项(脚本以python3.8编写)
- 脚本假设日志中包含以下字段:
user_agent:http_user_agent字符串clientip,remote_addr或ip: 客户端IP地址@timestamp: 时间戳
- 如果您的日志使用不同的字段名,需要修改脚本中的相应部分。
- 对于生产环境,您可能需要:
- 添加更详细的错误处理
- 实现分页查询以处理大量数据
- 添加日志记录功能
- 考虑性能优化,如使用异步查询
- 脚本默认不验证SSL证书,如果需要验证,请修改
verify_certs参数为True。 - --index后面的索引也需要注意,有的时候直接用泛泛的索引可能同时匹配到多个索引,会出现报错,出现报错后可以通过kibana找到一条日志找到index索引名字直接填写到命令参数--index后面
这个脚本可以帮助您分析Kibana/Elasticsearch中的日志,识别爬虫请求,并统计爬虫IP的活动情况