请给我一段用scapy进行大量IP和端口扫描的python3代码,其中,有多个进程在发包,只有一个进程在监听收包端口,获得返回包并得到可达ip
时间: 2024-03-13 13:44:44 浏览: 143
以下是一个使用Scapy进行多进程IP和端口扫描的Python 3代码示例:
```python
from scapy.all import *
from multiprocessing import Process, Queue
def scan(ip, ports, result_queue):
open_ports = []
for port in ports:
packet = IP(dst=ip)/TCP(dport=port, flags='S')
response = sr1(packet, timeout=1, verbose=0)
if response:
if response.haslayer(TCP) and response.getlayer(TCP).flags == 0x12:
open_ports.append(port)
elif response.haslayer(ICMP) and int(response.getlayer(ICMP).type) == 3:
pass
result_queue.put((ip, open_ports))
def listen(result_queue):
while True:
if not result_queue.empty():
ip, open_ports = result_queue.get()
if open_ports:
print(f"IP {ip} has the following open ports: {open_ports}")
else:
print(f"No open ports found on {ip}")
if __name__ == '__main__':
ip = '192.168.1.1'
ports = range(1, 1001)
result_queue = Queue()
# Start listening process
listening_process = Process(target=listen, args=(result_queue,))
listening_process.start()
# Start scanning processes
processes = []
for i in range(10):
process = Process(target=scan, args=(ip, ports[i::10], result_queue))
process.start()
processes.append(process)
# Wait for scanning processes to finish
for process in processes:
process.join()
# Stop listening process
listening_process.terminate()
```
在这个示例中,我们使用Scapy发送TCP SYN包来扫描指定IP地址的指定端口。我们使用多进程来同时执行扫描任务,其中一个进程用于监听扫描结果并将其打印出来。由于Scapy的sniff()方法是阻塞的,因此我们使用一个单独的进程来监听结果队列。每个扫描进程运行时,它将扫描指定IP地址的指定端口,如果发现一个端口是打开的,它将把结果添加到结果队列中。当结果队列不为空时,监听进程将从队列中获取结果并将其打印出来。
阅读全文