Reputation: 5
I'm tryng to get ciphersuites from pcap file with Scapy. 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.
Upvotes: 0
Views: 577
Reputation: 5411
You could try using 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")
Upvotes: 0