text stringlengths 1 654k |
|---|
74. arp_hdr = resp[0][14:42] |
75. arp_det = struct.unpack("2s2s1s1s2s6s4s6s4s", arp_hdr) |
76. |
77. # skip non-ARP packets |
78. etype = eth_det[2] |
79. if etype != '\x08\x06': |
80. #print('\t\t\t... non-arp packet') |
81. return 0 |
82. |
83. src_mac = str(EUI(binascii.hexlify(arp_det[5]))).replace('-', ':') |
84. src_ip4 = socket.inet_ntoa(arp_det[6]) |
85. dst_mac = str(EUI(binascii.hexlify(arp_det[7]))).replace('-', ':') |
86. dst_ip4 = socket.inet_ntoa(arp_det[8]) |
87. |
88. iface = loc_iface(jdict, dst_ip4, dst_mac) |
89. #print('\t...received ARP reply on interface %s from\n\t\tIP: %s\n\t\tMAC: %s\n' % (iface, src_ip4, src_mac)) |
90. |
91. # I don't care about what I accidentily pick up between other machines |
92. if iface is None: |
93. return 0 |
94. |
95. jdict["machines"][iface][src_ip4] = {} |
96. jdict["machines"][iface][src_ip4]["mac"] = src_mac |
97. jdict["machines"][iface][src_ip4]["tcp"] = {} |
98. |
99. return 1 |
100. |
101. # send an ARP request to every IP in the subnet for the provided interface |
102. def iter_sock(jdict, nmdict, iface): |
103. print('\tBeginning ARP scan for interface %s.' % (iface)) |
104. s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.htons(3)) |
105. s.bind((iface, 0)) |
106. |
107. s.settimeout(.02) |
108. |
109. iface_ip = jdict["machines"][iface].keys()[0] |
110. iface_nm = nmdict[iface] |
111. |
112. ptime = time.time() |
113. count = 0 |
114. |
115. for ip in IPNetwork(iface_ip + "/" + iface_nm): |
116. src_mac = binascii.unhexlify(str(jdict["machines"][iface][iface_ip]["mac"]).replace(':', '')) # source MAC |
117. src_ip4 = iface_ip # source IP |
118. dst_mac = '\xff\xff\xff\xff\xff\xff' # target MAC |
119. dst_ip4 = str(ip) # target IP |
120. |
121. # ethernet header |
122. protocol = 0x0806 # 0x0806 for ARP |
123. eth_hdr = struct.pack("!6s6sH", dst_mac, src_mac, protocol) |
124. |
125. # ARP header |
126. htype = 1 # hardware type is ethernet |
127. ptype = 0x0800 # protocol type is TCP |
128. hlen = 6 # hardware addr len is 6 |
129. plen = 4 # protocol addr len is 4 |
130. opcode = 1 # opcode 1 = request |
131. src = socket.inet_aton(src_ip4) |
132. dst = socket.inet_aton(dst_ip4) |
133. arp_hdr = struct.pack("!HHBBH6s4s6s4s", htype, ptype, hlen, plen, opcode, src_mac, src, dst_mac, dst) |
134. |
135. packet = eth_hdr + arp_hdr |
136. try: |
137. s.send(packet) |
138. except socket.error as e: |
139. if e.errno == errno.ENETDOWN: |
140. print('\t...network was down on interface %s.' % (iface)) |
141. return |
142. |
143. try: |
144. count = count + recv_sock(s, jdict) |
145. |
146. # print status every 8 seconds |
147. if time.time() - ptime > 8: |
148. ptime = time.time() |
149. print('\t...received %d ARP replies on interface %s' % (count, iface)) |
150. except socket.timeout: |
151. #print('... socket timed out, moving on...') |
152. continue |
153. |
154. s.close() |
155. print('\tCompleted ARP scan for interface %s with %d replies.' % (iface, count)) |
156. |
157. # initialize ARP request process for all interfaces |
158. def scan_net(jdict, nmdict): |
159. print('Starting ARP scan on all discovered interfaces.\n') |
160. thread_list = [] |
161. |
162. # setup concurrent processeses for all ARP requests |
163. for iface in nmdict: |
164. # uncomment the following line, and comment the 3 after that, to run arp reqests without multithreading |
165. #iter_sock(jdict, nmdict, iface) |
166. t = threading.Thread(target=iter_sock, args=(jdict, nmdict, iface)) |
167. t.start() |
168. thread_list.append(t) |
169. |
170. # wait for all processes to complete |
171. for t in thread_list: |
172. t.join() |
173. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.