Pythonã§ãã¤ããªãã¼ã¿ãããã¯ãã¦UDPã¡ãã»ã¼ã¸ãéã£ã¦ã¿ã
ã¯ããã«
Pythonã§ãã¤ããªãã¼ã¿ãããã¯ãã¦UDPã¡ãã»ã¼ã¸ãéãã«ã¯ãstruct
ã¢ã¸ã¥ã¼ã«ã使ããç°¡åãªä½¿ãæ¹ã¯Py MOTW: struct â Working with Binary Dataã§ç¢ºèªã§ãããæ¬ã¨ã³ããªã§ã¯ãstruct
ã¢ã¸ã¥ã¼ã«ã®ä½¿ãæ¹ãæ¼ãããUDPä¸ã®ãã¤ããªãã¼ã¿ã§æ§æããããããã³ã«ã§ããGTPv2ã®Echo Requestã¡ãã»ã¼ã¸ã試ãã«éä¿¡ãã¦ã¿ãã
GTPv2ã®Echo Requestã¡ãã»ã¼ã¸ã®ãããã³ã«ãã©ã¼ãããã¯ãã³ã¢ã®ãããã¯ã¼ã¯ã«GTPæ¥ç¶(ã¬ã¤ã¤ã¼2)ããããã®ã¡ã¢ã®ã¨ã³ããªã§åç
§ãã以ä¸ã®ããã¥ã¡ã³ãã§ç¢ºèªã§ããã
structã¢ã¸ã¥ã¼ã«ã§ãã¤ããªãã¼ã¿ãããã¯ãã¢ã³ããã¯
åæºåã¨ãã¦ãstruct
ã¢ã¸ã¥ã¼ã«ã®ä½¿ãæ¹ãæ¼ãããã
structã¢ã¸ã¥ã¼ã«ã¯ãã¼ã¿ã®ããã¯æ¹æ³ã¨ãã¦ãé¢æ°ã¬ãã«ã¨ã¯ã©ã¹ã¬ãã«ã®2ã¤ãç¨æãã¦ããããã¼ã¿ãããã¯ããã¨ãã«ã¯ããã©ã¼ãããæååï¼ä¾ãã°ããã¼ã¿ããããã¯ã¼ã¯ãã¤ããªã¼ãã§4ãªã¯ãããã§ãããã¨ãï¼ãæå®ããå¿ è¦ããããPy MOTW: struct â Working with Binary Dataã«ããã¨ãé¢æ°ã¬ãã«ã®ãããã³ã°ãå©ç¨ããå ´åããã©ã¼ãããæååã¯ãããã³ã°ã®éã«æ¯åº¦ã³ã³ãã¤ã«ãããã®ã«å¯¾ããã¯ã©ã¹ã¬ãã«ã®ãããã³ã°ãå©ç¨ããå ´åããªãã¸ã§ã¯ããã¤ã³ã¹ã¿ã³ã¹åããã¨ãã«ãã©ã¼ãããæååãã³ã³ãã¤ã«ããããããã¼ã¿ã®ãããã³ã°ãé«éã«ãªãã¨ã®ãã¨ã ãã£ã¦ãããã§ã¯ãã¯ã©ã¹ãã¼ã¹ã®ãããã³ã°ããã¦ã¿ãã
ã¾ãããã©ã¼ãããæååã¨ãã¦ããã¼ã¿ããããã¯ã¼ã¯ãã¤ããªã¼ãã§4ãªã¯ãããã§ãããã¨ãæå®ãã¦ãªãã¸ã§ã¯ããã¤ã³ã¹ã¿ã³ã¹åããã«ã¯ä»¥ä¸ã®ããã«ããããã¯ã¼ã¯ãã¤ããªã¼ãã示ã"!"ã¨4ãªã¯ãããã§ãããã¨ã示ã"i"ãã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ã«ä¸ããããã©ã¼ãããæååã®è©³ç´°ã¯pythonã®ããã¥ã¢ã«ã§ç¢ºèªãããã¨ãã§ããã
>>> import struct >>> s = struct.Struct("!i") >>> s <Struct object at 0x7f7e09518880>
ä¾ãã°ãã¤ããªãã¼ã¿ã¨ãã¦ã"0x04 0x03 0x02 0x01"ã§æ§æããã4ãªã¯ãããã®ãã¼ã¿ãä½ããä¸è¨ã®ãã©ã¼ãããæååã§ãããã³ã°ããã«ã¯ä»¥ä¸ã®ããã«ããã
>>> data = (0x04 << 24) + (0x03 << 16) + (0x02 << 8) + 0x01 >>> packed_data = s.pack(data) >>> packed_data b'\x04\x03\x02\x01'
ä¸è¨ã®ä¾ã¯ããã©ã¼ãããæååã¨ãã¦ããããã¯ã¼ã¯ãã¤ããªã¼ãã§1ãªã¯ãããã4ã¤ä¸¦ã¹ã以ä¸ã®ããã«ãã§ããã
>>> s = struct.Struct("!BBBB") >>> data = [0x04, 0x03, 0x02, 0x01] >>> packed_data = s.pack(*data) >>> packed_data b'\x04\x03\x02\x01'
ããã¯ãããã¼ã¿ããUDPã§éä¿¡ããã«ã¯ãsocket
ã¢ã¸ã¥ã¼ã«ã®sendto
ã®å¼æ°ã«ããã¯ãããã¼ã¿ã渡ãã°è¯ãã
ããã¯ãããã¼ã¿ãã¢ã³ããã¯ããã¨ããããã¯ããã¨ãã¨åæ§ã«ãã¾ããã©ã¼ãããæååãæå®ãã¦ãªãã¸ã§ã¯ããã¤ã³ã¹ã¿ã³ã¹åããããã¯ããããã¼ã¿ãunpack
ã¡ã½ããã®å¼æ°ã«æ¸¡ãã°è¯ãã
>>> s1 = struct.Struct("!BBBB") >>> packed_data = s1.pack(*data) >>> s1.unpack(packed_data) (4, 3, 2, 1)
GTPv2ã®Echo Requestã¡ãã»ã¼ã¸ãUDPã§éä¿¡
ããã§ã¯ã試ãã«GTPv2ã®Echo Requestã¡ãã»ã¼ã¸ããã¤ããªãã¼ã¿ã¨ãã¦æ§æãããããã³ã°ããUDPã§éä¿¡ãã¦ã¿ãã ç¨æããããã°ã©ã ã¯ä¸è¨ã®2ã¤ã * gtpv2c.py - GTPv2ã®ã¡ãã»ã¼ã¸ã¯ããããã¨æ å ±è¦ç´ (Information Element, IE)ã§æ§æããããæ¬ãã¡ã¤ã«ã§ã¯ããããã®ãã¼ã¿ãå®ç¾©ããã - æ¬ãã¡ã¤ã«ã§ã¯ããããã³ã°ãããã©ã¼ãããæååã¨ãã¤ããªãã¼ã¿ã®æ§ç¯ã®ã¿ãè¡ãã * send_gtpv2c.py - æ¬ãã¡ã¤ã«ã«ããã¦ãgtpv2c.pyãèªã¿è¾¼ã¿ãããã§å®ç¾©ãããEcho Requestã®ãããã¨IEãçµã¿ç«ã¦ããããã³ã°ããUDPã§ãã¼ã¿éä¿¡ããã
gtpv2c.py
#!/usr/bin/env python3 PORT = 2123 ECHO_REQUEST = 1 # ... RECOVERY_TYPE = 3 # ... def header(msg_type, msg_len, teid=None, seq_no=None): version = 2 p = 0 # piggyback off formatter = "!i" octets = [] octets1_4 = 0 octets1_4 += version << 29 octets1_4 += p << 28 if teid != None: octets1_4 += 1 << 27 octets1_4 += msg_type << 16 octets1_4 += msg_len octets.append(octets1_4) if teid != None: formatter += "i" octets.append(teid) if seq_no != None: formatter += "i" octets.append(seq_no << 8) return [ formatter, octets ] def recovery_ie(ins_id, recovery_val): ie_len = 5 formatter = 'ib' octets = [] octets1_4 = 0 octets1_4 += RECOVERY_TYPE << 24 octets1_4 += 1 << 8 octets1_4 += ins_id octets.append(octets1_4) octets.append(recovery_val) return [ ie_len, formatter, octets ]
send_gtpv2c.py
#!/usr/bin/env python3 import socket import time from contextlib import closing import gtpv2c import struct if __name__ == '__main__': host = '127.0.0.1' port = gtpv2c.PORT sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) with closing(sock): ie_len, ie_f, ie_msg = gtpv2c.recovery_ie(0, 100) hdr_f, hdr_msg = gtpv2c.header(gtpv2c.ECHO_REQUEST, ie_len, None, 1234) s = struct.Struct(hdr_f + ie_f) msg = hdr_msg + ie_msg packed_data = s.pack(*msg) sock.sendto(packed_data, (host, port))
ãµã³ãã«ããã°ã©ã ã®å®è¡
以ä¸ã®ããã«ã³ãã³ããå®è¡ãããã¼ã«ã«ãã¹ãã«gtpv2 echoã¡ãã»ã¼ã¸ãéä¿¡ãã
$ chmod +x send_gtpv2c.py $ ./send_gtpv2c.py
loããã¤ã¹ãwiresharkã§ã¿ã¦ã¿ãã¨ããã¾ãecho requestã¡ãã»ã¼ã¸ãéä¿¡ã§ãã¦ãã:)