Python how to send a gtp packet using scapy. I tried converting the existing hex stream of gtp message but not able to create it properly. some links are there but not ans is on it. scapy not parsing GTP layers
2
There are 2 best solutions below
0

Credit to chat gpt :)
from scapy.all import IP, UDP, Raw
from scapy.contrib.gtp import GTPHeader
# Define GTP header fields
gtp_version = 1
gtp_type = 1 # GTP-U
gtp_length = 20 # GTP header length (in bytes)
gtp_teid = 12345 # Tunnel Endpoint Identifier
gtp_sequence_number = 0 # Sequence number
gtp_n_pdu_number = 0 # N-PDU number
# Create GTP header (as Raw layer)
gtp_header = GTPHeader(version=gtp_version, gtp_type=gtp_type, length=gtp_length, teid=gtp_teid, seq=gtp_sequence_number, npdu=gtp_n_pdu_number)
# Define GTP destination port
DPORT = 2152
# Create a UDP packet with source/destination ports (replace with actual values)
udp_packet = UDP(dport=DPORT)
# Create a payload (e.g., raw data) to include in the GTP packet
payload_data = b'This is a GTP packet payload.'
# Construct the final packet by combining IP, UDP, GTP headers, and payload
gtp_packet = IP(dst='1.2.3.4') / udp_packet / gtp_header / Raw(load=payload_data)
# Display the packet summary
print(gtp_packet.summary())
# Send the packet (replace 'eth0' with the appropriate interface)
# send(gtp_packet, iface='eth0')
Quick example just tried, first hex stream is as below
enter scapy interactive mode and assign the above hex stream to a variable,
here scapy does not dissect and show GPRS, but we can still send out, i.e. windows nc
so the packet sent out with the interface, showing by wireshark is as