Skip to content

Instantly share code, notes, and snippets.

@wmnsk
Last active February 6, 2017 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmnsk/fbc08370f626ea425b7a769313d8946b to your computer and use it in GitHub Desktop.
Save wmnsk/fbc08370f626ea425b7a769313d8946b to your computer and use it in GitHub Desktop.
Test snippet for GTP-C implementaion on dpkt(https://github.com/wmnsk/dpkt/tree/gtpc_implementation)
# -*- coding: utf-8 -*-
import dpkt
import sys
import socket
cli_mac = bytes(bytearray.fromhex('080042000001'))
srv_mac = bytes(bytearray.fromhex('080042000002'))
ethtype = dpkt.ethernet.ETH_TYPE_IP
cli_ip = socket.inet_aton('1.1.1.1')
srv_ip = socket.inet_aton('2.2.2.2')
ipproto = dpkt.ip.IP_PROTO_UDP
cli_port = 53001
srv_port = 2123
eth_cli = dpkt.ethernet.Ethernet(
src=cli_mac,
dst=srv_mac,
type=ethtype
)
ip_cli = dpkt.ip.IP(
id=0,
src=cli_ip,
dst=srv_ip,
p=ipproto
)
udp_cli = dpkt.udp.UDP(
sport=cli_port,
dport=srv_port
)
tcp_cli = dpkt.tcp.TCP(
sport=cli_port,
dport=srv_port
)
v2c_cli = dpkt.gtp_c.GTPv2C(
version=2,
p_flag=0,
t_flag=1,
type=dpkt.gtp_c.V2_CREATE_SESSION_REQ,
teid=0xffffffff,
seqnum=0xdadadada
)
v1c_cli = dpkt.gtp_c.GTPv1C(
version=1,
proto_type=1,
e_flag=0,
s_flag=1,
np_flag=0,
type=dpkt.gtp_c.V1_CREATE_PDP_CXT_REQ,
teid=0xdeadbeef,
seqnum=0xcafe,
npdu=0,
next_type=0x00
)
ie2 = [
dpkt.gtp_c.IEv2(
type=1,
cr_flag=0,
instance=0,
data=b'D@D\x102Tv\x98'
),
dpkt.gtp_c.IEv2(
type=71,
cr_flag=0,
instance=0,
data=b'some.node.mnc044.mcc440.3gppnetwork.org'
)
]
ie1 = [
dpkt.gtp_c.IEv1(
type=2,
data=b'D@D\x102Tv\x98'
),
dpkt.gtp_c.IEv1(
type=131,
data=b'*some.node.mnc044.mcc440.gprs'
)
]
with open(sys.argv[1], 'wb') as f:
writer = dpkt.pcap.Writer(f)
v1c_cli.data = ie1
udp_cli.data = v1c_cli
udp_cli.ulen = len(bytes(udp_cli))
ip_cli.data = udp_cli
eth_cli.data = ip_cli
v1 = eth_cli
writer.writepkt(v1)
v2c_cli.data = ie2
udp_cli.data = v2c_cli
udp_cli.ulen = len(bytes(udp_cli))
ip_cli.data = udp_cli
eth_cli.data = ip_cli
v2 = eth_cli
writer.writepkt(v2)
Frame 1: 95 bytes on wire (760 bits), 95 bytes captured (760 bits)
Encapsulation type: Ethernet (1)
Arrival Time: Feb 6, 2017 23:48:14.074269000 JST
[Time shift for this packet: 0.000000000 seconds]
Epoch Time: 1486392494.074269000 seconds
[Time delta from previous captured frame: 0.000000000 seconds]
[Time delta from previous displayed frame: 0.000000000 seconds]
[Time since reference or first frame: 0.000000000 seconds]
Frame Number: 1
Frame Length: 95 bytes (760 bits)
Capture Length: 95 bytes (760 bits)
[Frame is marked: False]
[Frame is ignored: False]
[Protocols in frame: eth:ethertype:ip:udp:gtp]
Ethernet II, Src: JapanMac_00:00:01 (08:00:42:00:00:01), Dst: JapanMac_00:00:02 (08:00:42:00:00:02)
Destination: JapanMac_00:00:02 (08:00:42:00:00:02)
Address: JapanMac_00:00:02 (08:00:42:00:00:02)
.... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
.... ...0 .... .... .... .... = IG bit: Individual address (unicast)
Source: JapanMac_00:00:01 (08:00:42:00:00:01)
Address: JapanMac_00:00:01 (08:00:42:00:00:01)
.... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
.... ...0 .... .... .... .... = IG bit: Individual address (unicast)
Type: IPv4 (0x0800)
Internet Protocol Version 4, Src: 1.1.1.1, Dst: 2.2.2.2
0100 .... = Version: 4
.... 0101 = Header Length: 20 bytes (5)
Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
0000 00.. = Differentiated Services Codepoint: Default (0)
.... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
Total Length: 81
Identification: 0x0000 (0)
Flags: 0x00
0... .... = Reserved bit: Not set
.0.. .... = Don't fragment: Not set
..0. .... = More fragments: Not set
Fragment offset: 0
Time to live: 64
Protocol: UDP (17)
Header checksum: 0x7497 [validation disabled]
[Header checksum status: Unverified]
Source: 1.1.1.1
Destination: 2.2.2.2
[Source GeoIP: Unknown]
[Destination GeoIP: Unknown]
User Datagram Protocol, Src Port: 53001, Dst Port: 2123
Source Port: 53001
Destination Port: 2123
Length: 61
Checksum: 0x5db6 [unverified]
[Checksum Status: Unverified]
[Stream index: 0]
GPRS Tunneling Protocol
Flags: 0x32
001. .... = Version: GTP release 99 version (1)
...1 .... = Protocol type: GTP (1)
.... 0... = Reserved: 0
.... .0.. = Is Next Extension Header present?: No
.... ..1. = Is Sequence Number present?: Yes
.... ...0 = Is N-PDU number present?: No
Message Type: Create PDP context request (0x10)
Length: 45
TEID: 0xdeadbeef
Sequence number: 0xcafe
IMSI: 4404440123456789
Mobile Country Code (MCC): Japan (440)
Mobile Network Code (MNC): SoftBank (44)
Access Point Name: some.node.mnc044.mcc440.gprs
APN length: 29
APN: some.node.mnc044.mcc440.gprs
Frame 2: 109 bytes on wire (872 bits), 109 bytes captured (872 bits)
Encapsulation type: Ethernet (1)
Arrival Time: Feb 6, 2017 23:48:14.074430000 JST
[Time shift for this packet: 0.000000000 seconds]
Epoch Time: 1486392494.074430000 seconds
[Time delta from previous captured frame: 0.000161000 seconds]
[Time delta from previous displayed frame: 0.000161000 seconds]
[Time since reference or first frame: 0.000161000 seconds]
Frame Number: 2
Frame Length: 109 bytes (872 bits)
Capture Length: 109 bytes (872 bits)
[Frame is marked: False]
[Frame is ignored: False]
[Protocols in frame: eth:ethertype:ip:udp:gtp:gtpv2]
Ethernet II, Src: JapanMac_00:00:01 (08:00:42:00:00:01), Dst: JapanMac_00:00:02 (08:00:42:00:00:02)
Destination: JapanMac_00:00:02 (08:00:42:00:00:02)
Address: JapanMac_00:00:02 (08:00:42:00:00:02)
.... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
.... ...0 .... .... .... .... = IG bit: Individual address (unicast)
Source: JapanMac_00:00:01 (08:00:42:00:00:01)
Address: JapanMac_00:00:01 (08:00:42:00:00:01)
.... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
.... ...0 .... .... .... .... = IG bit: Individual address (unicast)
Type: IPv4 (0x0800)
Internet Protocol Version 4, Src: 1.1.1.1, Dst: 2.2.2.2
0100 .... = Version: 4
.... 0101 = Header Length: 20 bytes (5)
Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
0000 00.. = Differentiated Services Codepoint: Default (0)
.... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
Total Length: 95
Identification: 0x0000 (0)
Flags: 0x00
0... .... = Reserved bit: Not set
.0.. .... = Don't fragment: Not set
..0. .... = More fragments: Not set
Fragment offset: 0
Time to live: 64
Protocol: UDP (17)
Header checksum: 0x7497 [validation disabled]
[Header checksum status: Unverified]
Source: 1.1.1.1
Destination: 2.2.2.2
[Source GeoIP: Unknown]
[Destination GeoIP: Unknown]
User Datagram Protocol, Src Port: 53001, Dst Port: 2123
Source Port: 53001
Destination Port: 2123
Length: 75
Checksum: 0x5db6 [unverified]
[Checksum Status: Unverified]
[Stream index: 0]
GPRS Tunneling Protocol V2
Create Session Request
Flags: 0x48
010. .... = Version: 2
...0 .... = Piggybacking flag (P): 0
.... 1... = TEID flag (T): 1
Message Type: Create Session Request (32)
Message Length: 63
Tunnel Endpoint Identifier: 0xffffffff (4294967295)
Sequence Number: 0x00dadada (14342874)
Spare: 0
International Mobile Subscriber Identity (IMSI) : 4404440123456789
IE Type: International Mobile Subscriber Identity (IMSI) (1)
IE Length: 8
0000 .... = CR flag: 0
.... 0000 = Instance: 0
IMSI: 4404440123456789
Mobile Country Code (MCC): Japan (440)
Mobile Network Code (MNC): SoftBank (44)
Access Point Name (APN) : some.node.mnc044.mcc440.3gppnetwork.org
IE Type: Access Point Name (APN) (71)
IE Length: 39
0000 .... = CR flag: 0
.... 0000 = Instance: 0
APN (Access Point Name): some.node.mnc044.mcc440.3gppnetwork.org
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment