Files
DOH_tools/main.py
T

45 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from scapy.all import *
import argparse
def capture_doh_packets(interface, output_file, port=443, timeout=30):
"""
捕获指定端口的HTTPS流量(默认443,即DoH端口)
:param interface: 监听的网卡(如"eth0"或"lo"本地回环)
:param output_file: 保存的PCAP文件名(如"doh.pcap")
:param port: HTTPS端口(默认443)
:param timeout: 抓包时长(秒)
"""
# 定义过滤条件:目标端口为443的TCP流量
filter_str = f"tcp port {port}"
# 开始抓包
packets = sniff(
iface=interface,
filter=filter_str,
timeout=timeout,
prn=lambda pkt: print(f"捕获到数据包: {pkt.summary()}") # 实时输出抓包信息
)
# 保存为PCAP文件
if packets:
wrpcap(output_file, packets)
print(f"已保存 {len(packets)} 个数据包到 {output_file}")
else:
print("未捕获到数据包")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="捕获DoH流量")
parser.add_argument("-i", "--interface", default="lo", help="监听的网卡(默认:lo)")
parser.add_argument("-o", "--output", default="doh.pcap", help="输出文件名(默认:doh.pcap)")
parser.add_argument("-p", "--port", type=int, default=443, help="HTTPS端口(默认:443)")
parser.add_argument("-t", "--timeout", type=int, default=30, help="抓包时长(秒,默认:30)")
args = parser.parse_args()
# 执行抓包
capture_doh_packets(
interface=args.interface,
output_file=args.output,
port=args.port,
timeout=args.timeout
)
# python main.py -i ens33 -o doh.pcap -t 30