Guest User

Untitled

a guest
Apr 26th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import argparse
  4. from scapy.all import *
  5. from scapy.contrib.gtp_v2 import *
  6.  
  7. def isValidIPv4(ipv4):
  8. try:
  9. socket.inet_aton(ipv4)
  10. except socket.error:
  11. return False
  12. return True
  13.  
  14. def get_args():
  15. """ Get User Arguements. """
  16.  
  17. desc = """
  18. Used to receive and parse Packet.
  19. """
  20.  
  21. parser = argparse.ArgumentParser(description = desc)
  22. parser.add_argument('--filter', action = 'store', type = str, dest = 'filter', default = None, help = "Filter string used for sniff")
  23.  
  24. usr_args = parser.parse_args()
  25.  
  26. return usr_args
  27.  
  28. def pkt_handler(pkt):
  29. try:
  30. msg = None
  31.  
  32. if pkt[Ether].type == 0x800 and pkt[IP].proto == 17 and pkt[UDP].dport == 2152:
  33.  
  34. """ Try to parse the packet as GTP packet. """
  35. pkt[UDP].decode_payload_as(GTPHeader)
  36. pkt[GTPHeader].decode_payload_as(IP)
  37. if isValidIPv4(pkt[GTPHeader][IP].src) and isValidIPv4(pkt[GTPHeader][IP].dst):
  38. msg = pkt.sprintf(''
  39. 'Outer IP:: %IP.src% -> %IP.dst% \n'
  40. '\t Outer UDP: %UDP.sport% : %UDP.dport% \n'
  41. '\t GTP:: version: %GTPHeader.version%, teid: %GTPHeader.teid%, seq: %GTPHeader.seq%, type: %GTPHeader.gtp_type% \n'
  42. '')
  43. msg += pkt[GTPHeader].sprintf(''
  44. '\t Inner IP:: %IP.src% -> %IP.dst% \n'
  45. '{ICMP: \t Inner ICMP:: type: %ICMP.type%, code: %ICMP.code%, seq: %ICMP.seq% \n}'
  46. '{TCP: \t Inner TCP:: %TCP.sport% : %TCP.dport% \n'
  47. '{Raw: \t %Raw.load% \n}}'
  48. '{UDP: \t Innere UDP:: %UDP.sport% : %UDP.dport% \n'
  49. '{Raw: \t %Raw.load% \n}}'
  50. '\n')
  51. if msg is None:
  52. """ For all other packets. """
  53. msg = pkt.sprintf(''
  54. 'IP:: %IP.src% -> %IP.dst% \n'
  55. '{ICMP: \t ICMP:: type: %ICMP.type%, code: %ICMP.code%, seq: %ICMP.seq% \n}'
  56. '{UDP: \t UDP:: %UDP.sport% : %UDP.dport% \n'
  57. '{Raw: \t %Raw.load% \n}}'
  58. '{TCP: \t TCP:: %TCP.sport% : %TCP.dport% \n'
  59. '{Raw: \t %Raw.load% \n}}'
  60. '\n')
  61. except:
  62. print "[Warn] Unhandled packet.\n"
  63. pkt.summary()
  64. else:
  65. return msg
  66.  
  67. if __name__ == '__main__':
  68. usr_args = get_args()
  69.  
  70. sniff(prn = pkt_handler, filter = usr_args.filter)
Add Comment
Please, Sign In to add comment