# 导入所需库
import base64 # Base64编解码处理
import pyshark # 网络流量包分析工具
import json # JSON格式数据处理
import subprocess # 执行系统命令
from tqdm import tqdm # 进度条显示
def find_Keyword(file_name: str):
"""流量包关键词分析函数
Args:
file_name: pcap/pcapng流量包文件路径
"""
print("在跑了...再催报错")
decode_list = [] # 存储解码结果的列表
# 自动查找Wireshark安装路径(存在找不到路径时的异常风险)
# 执行系统命令 where wireshark.exe 获取路径
tshark_path = subprocess.check_output(['where', 'wireshark.exe'],
stderr=subprocess.STDOUT,
text=True).strip().splitlines()
# 创建流量包解析对象
cap = pyshark.FileCapture(file_name, tshark_path=tshark_path[0])
# 带进度条的流量包遍历(进度描述显示'decode:使用base64格式')
for i in tqdm(cap, desc='decode:使用base64格式'):
# 协议过滤条件
if 'TCP' in i and i.highest_layer == 'DATA':
try:
# HEX数据预处理(去除冒号分隔符)
hex_payload = str(i.tcp.payload).replace(':', '')
# 十六进制转字节
ascii_bytes = bytes.fromhex(hex_payload)
# 尝试UTF-8解码
ascii_output = ascii_bytes.decode(encoding='utf-8', errors='ignore')
# Base64解码处理
decoded_bytes = base64.b64decode(ascii_output)
# 最终结果解码
status = decoded_bytes.decode('utf-8')
try:
# JSON格式解析尝试
json_data = json.loads(decoded_bytes.decode('utf-8'))
# 提取info字段并进行二次Base64解码
d_data = json_data['info']
d_b = base64.b64decode(d_data)
# 结果存入列表(忽略解码错误)
decode_list.append(d_b.decode('utf-8', errors='ignore'))
except Exception as e:
# 捕获所有JSON相关异常,存储原始数据
decode_list.append(status) # 异常处理不够细化
else:
continue # 不符合协议条件时跳过
print('decode完毕!by信安一把索') # 带签名的完成提示
# 结果写入文件
with open('decode_base.txt', 'w', encoding='utf-8') as f:
# 简单替换逗号为换行符
formatted = str(decode_list).replace(',', 'n').replace('\n', ' ')
f.write(formatted)
# 函数调用
find_Keyword(file_name=r'E:取证文件encode.pcapng')
原文始发于微信公众号(信安一把索):流量分析 - encode
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论