Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example on how to easily decode QUIC SNI field #45

Open
m-peko opened this issue Sep 20, 2021 · 1 comment
Open

Example on how to easily decode QUIC SNI field #45

m-peko opened this issue Sep 20, 2021 · 1 comment

Comments

@m-peko
Copy link

m-peko commented Sep 20, 2021

Hi there,

my use case is the following:

  • I get the packet bytes from the network or PCAP file
  • I would like to extract SNI field related to that specific session that the packet belongs to
  • session management is done by my side

Is it possible? How would the example look like?

Thanks and regards

@ljluestc
Copy link

ljluestc commented Sep 5, 2023

from scapy.all import *
from scapy.layers import tls

Replace these values with your actual session identification criteria

source_ip = "source_ip"
source_port = 12345
destination_ip = "destination_ip"
destination_port = 443

def extract_sni(packet):
if packet.haslayer(tls.TLSClientHello):
client_hello = packet[tls.TLSClientHello]
for ext_type, ext_data in client_hello.extensions:
if ext_type == tls.TLSExtensionType.SERVER_NAME:
sni_info = tls.TLSServerName.parse(ext_data)
return sni_info[0].data.decode("utf-8")
return None

def process_packet(packet):
if IP in packet and TCP in packet:
if (
packet[IP].src == source_ip
and packet[TCP].sport == source_port
and packet[IP].dst == destination_ip
and packet[TCP].dport == destination_port
):
sni = extract_sni(packet)
if sni:
print(f"Session: {source_ip}:{source_port} -> {destination_ip}:{destination_port}")
print(f"SNI: {sni}")
print("=====================================")

pcap_file = "path_to_your_pcap_file.pcap"
packets = rdpcap(pcap_file)

for packet in packets:
process_packet(packet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants