Python 如何使用 scapy 发送 gtp 数据包。我尝试转换 gtp 消息的现有十六进制流,但无法正确创建它。 那里有一些链接,但上面没有。 scapy 不解析 GTP 层
刚刚尝试过的简单示例, 第一个十六进制流如下
000c29dad1de000c29e3c64d08004500007c00004000401167bbc0a828b3c0a828b2086808680068bf6432ff00580000000128db0000450000540000400040015ea5ca0b289ec0a828b20800bee70000287b0411204bf43d0d0008090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
进入scapy交互模式并将上面的十六进制流分配给变量,
a = '000c29dad1de000c29e3c64d08004500007c00004000401167bbc0a828b3c0a828b2086808680068bf6432ff00580000000128db0000450000540000400040015ea5ca0b289ec0a828b20800bee70000287b0411204bf43d0d0008090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'
b = a.decode('hex')
B = Ether(b)
B.show()
###[ Ethernet ]###
dst= 00:0c:29:da:d1:de
src= 00:0c:29:e3:c6:4d
type= 0x800
###[ IP ]###
version= 4L
ihl= 5L
tos= 0x0
len= 124
id= 0
flags= DF
frag= 0L
ttl= 64
proto= udp
chksum= 0x67bb
src= 192.168.40.179
dst= 192.168.40.178
\options\
###[ UDP ]###
sport= 2152
dport= 2152
len= 104
chksum= 0xbf64
###[ Raw ]###
load= '2\xff\x00X\x00\x00\x00\x01(\xdb\x00\x00E\x00\x00T\x00\x00@\x00@\x01^\xa5\xca\x0b(\x9e\xc0\xa8(\xb2\x08\x00\xbe\xe7\x00\x00({\x04\x11 K\xf4=\r\x00\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./01234567'
这里scapy没有解剖并显示GPRS,但我们仍然可以发送出去,即windows nc
send(B, iface='Intel(R) Dual Band Wireless-N 7260')
感谢聊天 gpt :)
from scapy.all import IP, UDP, Raw
# 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 = b''
gtp_header += bytes([(gtp_version << 5) | gtp_type])
gtp_header += gtp_length.to_bytes(2, byteorder='big')
gtp_header += gtp_teid.to_bytes(4, byteorder='big')
gtp_header += gtp_sequence_number.to_bytes(1, byteorder='big')
gtp_header += gtp_n_pdu_number.to_bytes(1, byteorder='big')
# Create a UDP packet with source/destination ports (replace with actual values)
udp_packet = UDP(sport=1234, dport=5678)
# 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 / Raw(load=gtp_header + payload_data)
# Display the packet summary
print(gtp_packet.summary())
# Send the packet (replace 'eth0' with the appropriate interface)
# send(gtp_packet, iface='eth0')