Catch the stun protocol

98 Views Asked by At

how can I catch stun protocol packets using python?

from scapy.all import *

def packet_handler(packet):
    if packet.haslayer(UDP) and (packet[UDP].dport == 3478 or packet[UDP].sport == 3478):
        raw_data = bytes(packet[UDP].payload)
        if len(raw_data) >= 20:
            msg_type, msg_length, magic_cookie = struct.unpack('!HHI', raw_data[:8])
            if magic_cookie == 0x2112A442: # This is a STUN packet
                print('Detected STUN packet: Type: {} Length: {} Magic Cookie: {}'.format(msg_type, msg_length, magic_cookie))
                packet.show()

sniff(filter='udp', prn=packet_handler)

I tried like this, but the stun packets are still not caught exactly, what am I doing wrong?

0

There are 0 best solutions below