本文摘自PHP中文网,作者V,侵删。
事件原由
(推荐教程:web服务器安全)
笔者在写一个小工具,针对渗透测试中需要搜集的信息,使用脚本自动化采集。而在这个模块中有个很难搞的部分就是端口banner 信息搜集,起初我尝试使用了python+nmap+多线程扫描,扫描20+的ip,等的花都谢了。。。而笔者目标是扫描200+的ip。下面我就针对端口扫描的技术进行分析。
1、nmap探测端口
nmap在扫描多个主机的时候可以设置参数 --min-hostgroup ,设置这个参数可以并行扫描多个主机,将这些主机划分成组,然后一次扫描一个组。
举例:
--min-hostgroup 50 nmap 以50个主机为一组,在扫描完50个主机之前不会显示结果。
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 | #coding=utf-8
import nmap
from queue import Queue
from threading import Thread
def portscan(ip):
portlist = []
nm = nmap.PortScannerYield()
for r in nm.scan(ip,ports='1-10000',arguments='-sS --min-hostgroup'):
m = r[1]['scan'][ip]['tcp']
for p in m:
temp = str(p) + "----" +m[p]['state']
portlist.append(temp)
print(portlist)
class Consumer(Thread):
def __init__(self, q):
Thread.__init__(self)
self.q = q
def run(self):
while not self.q.empty():
ip = self.q.get()
try:
portscan(ip)
except Exception as e:
print(e)
continue
def producer(ip_list):
num = 10
threads = []
q = Queue()
for i in ip_list:
print(i)
q.put(i)
threads = [Consumer(q) for i in range(0,int(num))]
for t in threads:
t.start()
for t in threads:
t.join()
ip_list =['120.78.207.76', '120.78.207.231', '120.78.207.18', '120.78.207.233', '120.78.207.165', '120.78.207.48',
'120.78.207.112', '120.78.207.27', '120.78.207.51', '120.78.207.8']
producer(ip_list)
|
如图,运行10个ip需要318s。
2、masscan探测端口
(1)调用python masscan
默认情况下,masscan 发送的是syn数据包,如果目标主机返回ack+syn,则说明端口开放。具体流程如下
A:192.168.70.142
B:192.168.0.143 开放端口3306
(1)A->B syn
(2)B->A syn+ack
(3)A->B RST
探测未开放的端口
A->B syn
B->A rst
举例:
1 2 3 4 | def portscan(ip):
mas = masscan.PortScanner()
mas.scan(ip,ports= '1-65535' )
print (mas.scan_result)
|
使用系统命令探测
阅读剩余部分
相关阅读 >>
端口扫描有哪几种方式
更多相关阅读请进入《端口扫描》频道 >>
转载请注明出处:木庄网络博客 » 端口扫描有哪几种方式