Guest User

Untitled

a guest
Dec 16th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. import threading
  2. import time
  3. from netaddr import IPNetwork, IPAddress
  4. import os
  5. import struct
  6. from ctypes import *
  7. import socket
  8. #from decoding_icmp import * #we need IP and ICMP class, defined in this file
  9.  
  10. #our ip header
  11. class IP(Structure):
  12. _fields_ = [
  13. ("ihl", c_ubyte, 4), #ihl is internet header length i.e size of ip header (offset to the data) in block of 32 bits (i.e. 1 means 32 bits)
  14. ("version", c_ubyte, 4),
  15. ('tos', c_ubyte),
  16. ('len', c_ushort),
  17. ('id', c_ushort),
  18. ('offset', c_ushort),
  19. ('ttl', c_ubyte),
  20. ('protocol_num', c_ubyte),
  21. ("sum", c_ushort),
  22. ("src", c_ulong),
  23. ("dst", c_ulong)
  24. ]
  25.  
  26.  
  27. def __new__(self, socket_buffer = None):
  28. return self.from_buffer_copy(socket_buffer)
  29.  
  30. def __init__(self, socket_buffer = None):
  31. #map protocol constants to their names
  32. self.protocol_map = {1:'ICMP', 6:'TCP', 17:"UDP"} #user defined dictionary
  33.  
  34. #human readable ip addresses from binary to string
  35. self.src_address = socket.inet_ntoa(struct.pack('<L', self.src)) #< means little endian and L means unsigned long
  36. self.dst_address = socket.inet_ntoa(struct.pack("<L", self.dst)) #pack converts given data to format like x0100x0000....
  37.  
  38. #human readable protocol
  39. try:
  40. self.protocol = self.protocol_map[self.protocol_num]
  41. except:
  42. self.protocol = str(self.protocol_num)
  43.  
  44.  
  45.  
  46.  
  47. class ICMP(Structure):
  48. _fields_ = [
  49. ('type', c_ubyte),
  50. ('code', c_ubyte),
  51. ('checksum', c_ushort),
  52. ('unused', c_ushort),
  53. ('next_hop_mtu', c_ushort)
  54. ]
  55.  
  56. def __new__(self, socket_buffer):
  57. return self.from_buffer_copy(socket_buffer)
  58.  
  59.  
  60. def __init__(self, socket_buffer):
  61. pass
  62.  
  63.  
  64. #host to listen on
  65. host = '192.168.0.106'
  66.  
  67. #subnet to target
  68. subnet = '192.168.0.0/24'
  69.  
  70. #magic string for which we'll check ICMP responses
  71. magic_message = "PYTHONRULES!"
  72.  
  73. #this sprays out of the UDP datagrams
  74. def udp_sender(subnet, magic_message):
  75. time.sleep(5)
  76. sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  77.  
  78. for ip in IPNetwork(subnet):
  79. sender.sendto(magic_message, (("%s" % ip), 65212))
  80.  
  81.  
  82.  
  83. if os.name == 'nt':
  84. socket_protocol = socket.IPPROTO_IP
  85. else:
  86. socket_protocol = socket.IPPROTO_ICMP
  87.  
  88.  
  89. sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
  90.  
  91. sniffer.bind((host, 0)) #0 for random port
  92.  
  93. sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  94.  
  95. if os.name == 'nt':
  96. sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) #can sniff all packets only on windows
  97.  
  98. t = threading.Thread(target = udp_sender, args = (subnet, magic_message))
  99. t.start()
  100.  
  101.  
  102. try:
  103. while True:
  104. #read in a packet
  105. raw_buffer = sniffer.recvfrom(65565)[0] #a pair(recvd_data, sender_addr) is returned. [0] access the first element of the pair
  106.  
  107. #create an IP header from the first 20 bytes of the buffer i.e. 160 bits
  108. ip_header = IP(raw_buffer[0:20])
  109.  
  110. #print "Protocol: %s %s -> %s" % (ip_header.protocol, ip_header.src_address, ip_header.dst_address)
  111.  
  112. if ip_header.protocol == 'ICMP':
  113.  
  114. #calculate where our icmp packet starts
  115. offset = ip_header.ihl * 4 #in ihl, 1 means 32 bits
  116.  
  117. buf = raw_buffer[offset : (offset + sizeof(ICMP))]
  118.  
  119. #create our ICMP structure
  120. icmp_header = ICMP(buf)
  121.  
  122. #print "SOURCE: %s DESTINATION: %s ICMP -> Type: %d code: %d" % (ip_header.src_address, ip_header.dst_address, icmp_header.type, icmp_header.code)
  123.  
  124. #check for type 3 code and type
  125. if icmp_header.code == 3 and icmp_header.type == 3:
  126.  
  127. #make sure host is in our subnet
  128. if IPAddress(ip_header.src_address) in IPNetwork(subnet):
  129.  
  130. #make sure it is our magic msg
  131. if raw_buffer[len(raw_buffer) - len(magic_message):] == magic_message:
  132. print "host up: %s" % ip_header.src_address
  133.  
  134. #if ip_header.protocol == 'UDP':
  135. #print "UDP :::: SRC: %s and DST: %s" % (ip_header.src_address, ip_header.dst_address)
  136.  
  137. except KeyboardInterrupt:
  138. #if using windows close promiscuous mode
  139. if os.name == 'nt':
  140. sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
Add Comment
Please, Sign In to add comment