forked from zuojx1013/NyPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
52 lines (45 loc) · 1.41 KB
/
Copy pathscanner.py
File metadata and controls
52 lines (45 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import socket
import os
from netaddr import IPNetwork,IPAddress
import time
import threading
class Sniffer():
def __init__(self,host):
if os.name=='nt':
self.socket_protocol=socket.IPPROTO_IP
else:
self.socket_protocol=socket.IPPROTO_ICMP
self.sniffer=socket.socket(socket.AF_INET,socket.SOCK_RAW,self.socket_protocol)
self.sniffer.bind((host,0))
self.sniffer.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL,1)
if os.name=='nt':
self.sniffer.ioctl(socket.SIO_RCVALL,socket.RCVALL_ON)
def read(self):
raw_buffer=self.sniffer.recvfrom(65565)
return raw_buffer
def close(self):
if os.name=='nt':
self.sniffer.ioctl(socket.SIO_RCVALL,socket.RCVALL_OFF)
def udp_sender(subnet,magic_message):
time.sleep(2)
sender=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
for ip in IPNetwork(subnet):
try:
sender.sendto(magic_message.encode(),('%s'%ip,65212))
except:
pass
def scanner():
sniffer=Sniffer('10.11.184.163')
subnet='10.11.0.0/24'
magic_message="hello"
t=threading.Thread(target=udp_sender,args=(subnet,magic_message))
t.start()
try:
while True:
print(sniffer.read()[1][0])
except KeyboardInterrupt:
print("CTRL-C")
sniffer.close()
t._stop()
if __name__=='__main__':
scanner()