我在下面的代码中遇到了一些困难。当我尝试运行它时,出现错误。
def parse_Nmap(filename):
tmp_http_list = []
tmp_https_list = []
tmp_ports_list = []
tmp_os_list = []
notrelevant = ['name', 'method', 'conf', 'cpelist', 'servicefp', 'tunnel']
relevant = ['@product', '@version', '@extrainfo']
with open(filename,mode= "rt") as f:
tree = xmltodict.parse(f.read())
root = tree['nmaprun']
for host_addr in root['host']['address']:
if host_addr['@addrtype'] == "ipv4":
ip = host_addr['@addr']
os_data = root['host']['os']['osmatch']
os_dist = os_data.get('@name')
if "Linux" in os_dist.lower() or "Unix" in os_dist.lower():
if [ip, "Linux"] not in tmp_os_list:
tmp_os_list.append([ip, "Linux"])
if [ip, "Unix"] not in tmp_os_list:
tmp_os_list.append([ip, "Unix"])
if "windows" in os_dist.lower():
tmp_os_list.append([ip, "windows"])
if "apple" in os_dist.lower() or "apple OS x" in os_dist.lower():
tmp_os_list.append([ip, "osx"])
if "solaris" in os_dist.lower():
tmp_os_list.append([ip, "solaris"])
for port in root['host']['ports']['port']:
if port['state']['@state'] == "open":
if [ip, port['@portid']] not in host_port_list:
host_port_list.append([ip, port['@portid']])
try:
banner = ""
service_keys = port['service'].keys()
if '@method' in port['service'] and port['service']['@method'] == "probed":
for rel_key in relevant:
if rel_key in service_keys:
banner += "{0}: {1} ".format(rel_key.strip("@"), port['service'][rel_key])
for m_key in service_keys:
if m_key not in notrelevant and m_key not in relevant:
banner += '{0}: {1} '.format(m_key.strip("@"), port['service'][m_key])
banner_data = banner.rstrip()
if len(banner_data.split(" ")[1]) > 2:
service_banner = (banner_data.split(" ")[1]).lower()
if [ip, port['@portid'], service_banner] not in unique_svc_bannerlist:
unique_svc_bannerlist.append([ip, port['@portid'], service_banner])
tmp_banner = banner_data.replace("product: ", "")
if [str(port['@portid']) + "/" + port['@protocol'], tmp_banner] not in service_bannerlist:
service_bannerlist.append([str(port['@portid']) + "/" + port['@protocol'], tmp_banner])
except IndexError:
pass
tmp_ports_list.append([str(ip), str(port['@portid']), port['@protocol'], port['service']['@name']])
if port['service']['@name'] != "http":
if port['service']['@name'] not in unique_svc_namelist and "?" not in str(port['service']['@name']):
unique_svc_namelist.append([ip, port['@portid'], port['service']['@name']])
else:
if '@tunnel' in port['service']:
if port['service']['@tunnel'] == "ssl":
tmp_https_list.append(
[str(ip), str(port['@portid']), port['@protocol'], port['service']['@name']])
else:
tmp_http_list.append(
[str(ip), str(port['@portid']), port['@protocol'], port['service']['@name']])
return tmp_http_list, tmp_https_list, tmp_ports_list, tmp_os_list
Traceback (most recent call last):
File "APMS/APMS.py", line 135, in <module>
main()
File "APMS/APMS.py", line 118, in main
MSFW_scanner.MSFW(ip)
File "/root/Desktop/APMS/APMS/MSFW/MSFW_scanner.py", line 99, in MSFW
http_list, https_list, ports_list, os_list = self.parse_Nmap(str(Nmap_filename) + ".xml")
File "/root/Desktop/APMS/APMS/MSFW/MSFW_scanner.py", line 378, in parse_Nmap
os_dist = os_data.get('@name')
AttributeError: 'list' object has no attribute 'get'