如何使用nessrest api(python)在xml中导出nessus扫描报告?

时间:2016-09-24 00:41:50

标签: python-2.7 nessus

我正在尝试使用python自动运行和下载nessus扫描。我一直在使用nessrest api for python,并且能够成功运行扫描,但是没有成功地以nessus格式下载报告。

我有什么想法可以做到这一点?我一直在使用模块scan_download,但实际上在我的扫描完成之前执行。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

回顾一下这个问题,下面是一个使用Nessrest API从您的主人主机中导出CSV报告的示例,

#!/usr/bin/python2.7
import sys
import os
import io
from nessrest import ness6rest 

file_format = 'csv'  # options: nessus, csv, db, html
dbpasswd = ''

scan = ness6rest.Scanner(url="https://nessus:8834", login="admin", password="P@ssword123", insecure=True)

scan.action(action='scans', method='get')
folders = scan.res['folders']
scans = scan.res['scans']

if scan:
    scan.action(action='scans', method='get')
    folders = scan.res['folders']
    scans = scan.res['scans']

    for f in folders:
        if not os.path.exists(f['name']):
            if not f['type'] == 'trash':
                os.mkdir(f['name'])

    for s in scans:
        scan.scan_name = s['name']
        scan.scan_id = s['id']
        folder_name = next(f['name'] for f in folders if f['id'] == s['folder_id'])
        folder_type = next(f['type'] for f in folders if f['id'] == s['folder_id'])

        # skip trash items
        if folder_type == 'trash':
            continue

        if s['status'] == 'completed':
            file_name = '%s_%s.%s' % (scan.scan_name, scan.scan_id, file_format)
            file_name = file_name.replace('\\','_')
            file_name = file_name.replace('/','_')
            file_name = file_name.strip()
            relative_path_name = folder_name + '/' + file_name
            # PDF not yet supported
            # python API wrapper nessrest returns the PDF as a string object instead of a byte object, making writing and correctly encoding the file a chore...
            # other formats can be written out in text mode
            file_modes = 'wb'
            # DB is binary mode
            #if args.format == "db":
            #  file_modes = 'wb'
            with io.open(relative_path_name, file_modes) as fp:
                if file_format != "db":
                    fp.write(scan.download_scan(export_format=file_format))
                else:
                    fp.write(scan.download_scan(export_format=file_format, dbpasswd=dbpasswd))

可以在此处看到更多示例,

https://github.com/tenable/nessrest/tree/master/scripts