プログラムを使用して OpenVAS (他の誰かのコード) から xml ファイルを解析していますが、AttributeError: 'NoneType' object has no attribute 'text'
. 一部のスキャンでは「ホスト名」フィールドが空になる可能性があるため、これは予想どおりです。空白の場合は、ホスト名として N/A を入力するのが理想的です。
エラー:
File "/var/lib/openvasreporting/openvasreporting/libs/parser.py", line 115, in openvas_parser
vuln_host_name = vuln.find("./host/hostname").text
AttributeError: 'NoneType' object has no attribute 'text'
parser.py
ファイル内の対応する行は次のとおりです。
# --------------------
#
# VULN_HOST
vuln_host = vuln.find("./host").text
vuln_host_name = vuln.find("./host/hostname").text
if vuln_host_name is None:
vuln_host_name = "N/A"
logging.debug("* hostname:\t{}".format(vuln_host_name)) # DEBUG
vuln_port = vuln.find("./port").text
logging.debug(
"* vuln_host:\t{} port:\t{}".format(vuln_host, vuln_port)) # DEBUG
# --------------------
parser.py
補足として、これはファイルの他の 2 つのセクションのバグでした。解決されたセクションは次のようになります。
# --------------------
#
# VULN_CVES
#vuln_cves = nvt_tmp.findall("./refs/ref")
vuln_cves = []
ref_list = []
for reference in nvt_tmp.findall('./refs/ref'):
if reference.attrib.get('type') == 'cve':
vuln_cves.append(reference.attrib.get('id'))
else:
ref_list.append(reference.attrib.get('id'))
logging.debug("* vuln_cves:\t{}".format(vuln_cves)) # DEBUG
logging.debug("* vuln_cves:\t{}".format(Et.tostring(vuln_cves).decode())) # DEBUG
if vuln_cves is None or vuln_cves.text.lower() == "nocve":
vuln_cves = []
else:
vuln_cves = [vuln_cves.text.lower()]
vuln_references = ' , '.join(ref_list)
logging.debug("* vuln_cves:\t{}".format(vuln_cves)) # DEBUG
logging.debug("* vuln_references:\t{}".format(vuln_references))
# --------------------
#
# VULN_REFERENCES
vuln_references = nvt_tmp.find("./xref")
if vuln_references is None or vuln_references.text.lower() == "noxref":
vuln_references = []
else:
vuln_references = vuln_references.text.lower().replace("url:", "\n")
logging.debug("* vuln_references:\t{}".format(vuln_references)) # DEBUG
#
# --------------------
で追加しようとしましたelse: pass
が、同じ結果が得られます。
私は実際にはプログラマーではないので、これに関連するすべての情報が含まれていない場合は申し訳ありません。