1) 读取文件内容
-
检查是否为 Wireshark/Tcpdump 格式的流量日志
-
提取 HTTP/HTTPS 请求(GET/POST)
-
识别文件下载行为(如 Content-Disposition: attachment 或常见文件扩展名)
2) 检测下载行为
-
检查是否有 GET /file.exe、GET /download.zip 等请求
-
检查是否有大流量传输(可能对应文件下载)
-
检查 DNS 查询记录(解析下载域名)
3) 输出结果
-
如果发现下载记录,返回 时间、来源网站、文件名
-
如果没有,说明原因(如加密流量、抓包不完整等)
import re
from pathlib import Path
def analyze_downloads(file_path):
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read(100000) # 读取前100KB(避免大文件卡顿)
# 检查是否包含HTTP流量
if "HTTP/1." not in content and "Host: " not in content:
print("⚠️ 未检测到HTTP流量,可能是加密流量(HTTPS)或其他协议")
return {"status": "no_http", "message": "No HTTP traffic detected"}
# 提取可能的下载请求(GET + 文件扩展名)
file_extensions = ['.exe', '.zip', '.rar', '.msi', '.pdf', '.doc', '.xls']
download_pattern = r'(GET|POST)s+(.*?(' + '|'.join(re.escape(ext) for ext in file_extensions) + r'))[?s
downloads = re.findall(download_pattern, content, re.IGNORECASE)
if not downloads:
print("🔍 未找到文件下载请求,但检测到HTTP流量")
print("可能原因:")
print("- 流量被加密(HTTPS)")
print("- 抓包不完整")
print("- 下载使用了非标准端口或协议(如FTP)")
return {"status": "no_downloads", "message": "No file downloads detected"}
# 提取下载记录
results = []
for method, url, _ in downloads:
domain = re.search(r'Host:s*([^rn]+)', content)
domain = domain.group(1).strip() if domain else "Unknown"
filename = url.split('/')[-1].split('?')[0]
results.append({
"method": method,
"domain": domain,
"filename": filename,
"url": url
})
# 输出结果
print("✅ 检测到文件下载记录:")
for i, dl in enumerate(results, 1):
print(f"{i}. 从【{dl['domain']}】下载了【{dl['filename']}】")
print(f" 🔗 {dl['method']} {dl['url']}n")
return {
"status": "success",
"downloads": results
}
except Exception as e:
print(f"❌ 分析失败: {str(e)}")
return {"status": "error", "message": str(e)}
# 分析文件
file_path = Path("c:/users/administrator/desktop/81test-all.txt")
if not file_path.exists():
print(f"❌ 文件不存在: {file_path}")
else:
__result__ = analyze_downloads(file_path)
原文始发于微信公众号(MicroPest):智能体分析wireshark流量包
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论