利用Scapy实现网络主机扫描以及调试过程中两个错误(多线程模块以及ipaddress模块)的解决

  代码本身应该比较简单,就是利用Scapy生成ICMP报文,看是否有返回结果,同时利用多线程加速,调试完成的代码如下:

 1 from scapy.all import *
 2 import sys
 3 import optparse
 4 import threading
 5 import queue
 6 import ipaddress
 7 
 8 class PingNetwork:
 9     def __init__(self) -> None:
10         self.target = self.transform_network(self.get_params())
11         self.q = Queue()
12         self.alive_hosts = []
13         
14 
15     def get_params(self):
16         parser = optparse.OptionParser('Usage: <Progrma> -t target network  format  x.x.x.x/24')
17         parser.add_option('-t', '--target', dest='target', type='string', help='Specify target network to scan ')
18         options, args = parser.parse_args()
19         if options.target is None:
20             print("Please specify network to scan ")
21             sys.exit(0)
22         return options.target
23     
24     def transform_network(self, net):
25         try:
26             return ipaddress.ip_network(net)
27         except:
28             print("Network is incorrect")
29             sys.exit(0)
30     
31     def ping_host(self,host):
32         try:
33            
34             packet = IP(dst=str(host))/ICMP()
35             res = sr1(packet, timeout=2, verbose=False)
36             
37             if res:
38                 self.q.put(host)
39         except Exception as e:
40             pass
41     
42     def run(self):
43         try:
44             for host in self.target:
45                 print("Ping %s" % host)
46                 t = threading.Thread(target=self.ping_host, args=(host,))
47                 t.start()
48                 time.sleep(0.5)
49         except:
50             pass
51             
52         
53         while not self.q.empty():
54             self.alive_hosts.append(self.q.get())
55         
56         hosts_set = sorted(self.alive_hosts)
57         for host in hosts_set:
58             print("[-] Host %s is alive" % host)
59 
60 
61     
62 if __name__ == "__main__":
63     pinger = PingNetwork()
64     pinger.run()

  但是在调试过程中发现了两个错误:

         1. 是由于我用到了ipaddress模块,该模块的ip_network可以返回该网络的所有主机的可迭代对象,但是一定要注意,迭代出来的主机并不是字符串,需要用Str函数转换一下即可,否则在发送报文的时候会报错。

    2. 是由于用到了threading多线程模块。但是报了下面的错误:

Ping 192.168.140.198
Traceback (most recent call last):
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 168, in __del__
    self.close()
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 161, in close
    os.close(self.__rd)
AttributeError: 'ObjectPipe' object has no attribute '_ObjectPipe__rd'
Traceback (most recent call last):
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 168, in __del__
    self.close()
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 161, in close
Exception ignored in: <function ObjectPipe.__del__ at 0x7f823b190040>
Traceback (most recent call last):
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 168, in __del__
    self.close()
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 161, in close
    os.close(self.__rd)
AttributeError: 'ObjectPipe' object has no attribute '_ObjectPipe__rd'
    os.close(self.__rd)
AttributeError: 'ObjectPipe' object has no attribute '_ObjectPipe__rd'
Ping 192.168.140.199
Ping 192.168.140.200
Ping 192.168.140.201
Exception ignored in: <function ObjectPipe.__del__ at 0x7f823b190040>
Traceback (most recent call last):
Ping 192.168.140.202
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 168, in __del__
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 161, in close
Ping 192.168.140.203
AttributeError: 'ObjectPipe' object has no attribute '_ObjectPipe__rd'
Ping 192.168.140.204
Exception ignored in: <function ObjectPipe.__del__ at 0x7f823b190040>
Traceback (most recent call last):
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 168, in __del__
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 161, in close
AttributeError: 'ObjectPipe' object has no attribute '_ObjectPipe__rd'
Ping 192.168.140.205
Exception ignored in: <function ObjectPipe.__del__ at 0x7f823b190040>
Ping 192.168.140.206
Traceback (most recent call last):
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 168, in __del__
Ping 192.168.140.207
    self.close()
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 161, in close
    os.close(self.__rd)
AttributeError: 'ObjectPipe' object has no attribute '_ObjectPipe__rd'
Ping 192.168.140.208
Ping 192.168.140.209
Exception ignored in: <function ObjectPipe.__del__ at 0x7f823b190040>
Traceback (most recent call last):
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 168, in __del__
  File "/root/Desktop/hack_env/lib/python3.9/site-packages/scapy/automaton.py", line 161, in clos

  似乎是打开socket太多,加一个time.sleep即可。

posted @ 2022-05-26 10:25  Jason_huawen  阅读(205)  评论(0编辑  收藏  举报