以下脚本实现了对于nmap端口扫描以-oN保存的扫描结果进行自动web连接尝试的功能。
会自动过滤掉开放超过800个端口以上的ip(可能为蜜罐,或是防火墙设置了特殊的响应规则)
连接超时时间设置为5秒
对同一个端口同时尝试http和https连接
import requests
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
fileList = [“result1.log”, “result2.log”]
basePath = “/root/Downloads/“ipInfoList = []
ipCount = 0
portCount = 0for f in fileList:
fObj = open(basePath+f, “r”)
text = fObj.read()
fObj.close()
ipInfoList = text.split(“Nmap scan report for “)[1:]
if len(ipInfoList) != 0:
ipInfoList[len(ipInfoList)-1] = ipInfoList[len(ipInfoList)-1].split(“# Nmap done at “)[0]
for info in ipInfoList:
# print(“has requested %d ip %d port” % (ipCount, portCount), end=”\r”)
ipCount += 1
ip = info.split(“Host is up “)[0].strip()
ports = info.split(“SERVICE”)[1].strip()
portList = ports.split(‘\n’)
if len(portList) < 800:
for p in portList:
if p.find(“open”) != -1:
portCount += 1
port = p.split(“/“)[0]
print(“Connecting %15s:%-5s\tHas tried %d ip %d port.” % (ip, port, ipCount, portCount), end=”\r”)
try:
req = requests.get(“http://“+ ip + “:” + port, timeout=5)
print(“\n”+ ip+”:”+ port)
except:
try:
req = requests.get(“https://“+ ip + “:” + port, verify=False , timeout=5)
print(“\n”+ ip+”:”+ port)
except Exception as e:
print(end=’’)
# print(e)
# finally:
# print(“has requested %d ip %d port” % (ipCount, portCount), end=”\r”)