0

プログラムを使用して 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が、同じ結果が得られます。

私は実際にはプログラマーではないので、これに関連するすべての情報が含まれていない場合は申し訳ありません。

4

2 に答える 2

0

これは、実際に何かが見つかったかどうかを確認せずに、属性を呼び出しfind()てすぐにアクセスしているために発生します。.textfind()

代わりに次のようにします。

element = vuln.find("./host/hostname")
if element:
    vuln_host_name = element.text
else:
    vuln_host_name = "N/A"
于 2021-06-16T18:22:08.283 に答える
-1

交換:

vuln_host = vuln.find("./host").text

と:

if vuln.find("./host") > -1:
  vuln_host = vuln.find("./host").text
else:
  vuln_host = "N/A"
于 2021-06-16T15:53:25.433 に答える