[英]Select packets from pcap with Scapy
I'm tryng to get ciphersuites from pcap file with Scapy.我正在尝试使用 Scapy 从 pcap 文件中获取密码套件。 I would like to get only the first packet from every file, beacause I'm using a filter.我只想从每个文件中获取第一个数据包,因为我正在使用过滤器。
This is my code:这是我的代码:
from scapy.all import *
from scapy.layers.radius import Radius
import re
import os
import pathlib
def get_ciphersuite():
ciphersuites = {
#'0000' : 'TLS_NULL_WITH_NULL_NULL',
'002f' : 'TLS_RSA_WITH_AES_128_CBC_SHA',
'0035' : 'TLS_RSA_WITH_AES_256_CBC_SHA',
'003c' : 'TLS_RSA_WITH_AES_128_CBC_SHA256',
'003d' : 'TLS_RSA_WITH_AES_256_CBC_SHA256',
'009c' : 'TLS_RSA_WITH_AES_128_GCM_SHA256',
'009d' : 'TLS_RSA_WITH_AES_256_GCM_SHA384',
'c02c' : 'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384'
}
dir_pcaps = "./pcaps/"
pcaps = os.listdir(dir_pcaps)
for pcap in pcaps:
if pathlib.Path(pcap).suffix in [".pcap", ".cap", ".pcapng"]:
packets = rdpcap(dir_pcaps+pcap)
print(packets)
for packet in packets:
if packet.haslayer("Radius") and packet[IP].src == "10.10.10.40":
rp = packet.getlayer("Radius")
rp_hex = bytes_hex(rp).decode()
for ciphersuite in ciphersuites:
r = re.findall(ciphersuite, rp_hex)
if r:
print(ciphersuites[ciphersuite]," --> "+pcap)
break
else:
pass
get_ciphersuite()
This is the output:这是输出:
<128CBCSHA.pcapng: TCP:0 UDP:20 ICMP:0 Other:1>
TLS_RSA_WITH_AES_128_CBC_SHA --> 128CBCSHA.pcapng
TLS_RSA_WITH_AES_128_CBC_SHA256 --> 128CBCSHA.pcapng
TLS_RSA_WITH_AES_128_CBC_SHA256 --> 128CBCSHA.pcapng
TLS_RSA_WITH_AES_128_CBC_SHA256 --> 128CBCSHA.pcapng
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 --> 128CBCSHA.pcapng
<test.pcapng: TCP:0 UDP:11 ICMP:0 Other:1>
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 --> test.pcapng
TLS_RSA_WITH_AES_256_CBC_SHA256 --> test.pcapng
This script returns me the ciphersuite for every packet in every pcap file, but I need only the first packet in every file.该脚本为我返回每个 pcap 文件中每个数据包的密码套件,但我只需要每个文件中的第一个数据包。
You could try using sniff
.您可以尝试使用sniff
。 For instance, replace例如,替换
packets = rdpcap(dir_pcaps+pcap)
with和
packets = sniff(offline=dir_pcaps+pcap, count=1,
lfilter=lambda x: x.haslayer("Radius") and x[IP].src == "10.10.10.40")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.