nessrest icon indicating copy to clipboard operation
nessrest copied to clipboard

nessrest problem download a scan to pdf, html or csv. File output is corrupt.

Open pythoncode17 opened this issue 9 years ago • 8 comments

Using mod: from nessrest import ness6rest Objective: download a scan to a pdf, html or csv I could not find the nessrest function to do this, so I use the function in python program: nessus6_scan_demo.py Problem: every format output comes out corrupt file

import json import requests from nessrest import ness6rest “”” Download scan results fuinction

“”” def add(name, desc, targets, pid): scan = {'uuid': pid, 'settings': { 'name': name, 'description': desc, 'text_targets': targets}} data = connect('POST', '/scans', data=scan) return data['scan']

def get_history_ids(sid): params = {'history_id': hid} data = connect('GET', '/scans/{0}'.format(sid), params) return data['info']

def export(sid, hid): data = {'history_id': hid, 'format': 'nessus'} data = connect('POST', '/scans/{0}/export'.format(sid), data=data) fid = data['file'] while export_status(sid, fid) is False: time.sleep(5) return fid

def download(sid, fid): data = connect('GET', '/scans/{0}/export/{1}/download'.format(sid, fid)) filename = 'nessus_{0}_{1}.nessus'.format(sid, fid) print('Saving scan results to {0}.'.format(filename)) with open(filename, 'w') as f: f.write(data)

scan = ness6rest.Scanner(url="https://777.777.7.77:8834", login="bozo", password="+=*&^%werT", insecure=True) print("scan=%s"%(scan))

scan.policy_add(name="Test_Scan2_777.777.7.77",plugins="21156") scan.scan_add(targets="777.777.7.77") scan.scan_run() ''' Parse scan results ''' print(scan.scan_results())

print('') scan_id = scan_data['id'] print(‘'scan_id='%s'%(scan_id)) history_ids = get_history_ids(scan_id) history_id = history_ids[scan_uuid] print('history_id=%s'%(history_id)) print('Exporting the completed scan.') file_id = export(scan_id, history_id) download(scan_id, file_id) print('Finish')

----------------------------------------------------------------------------------------

Run above python script ERROR At: TypeError: must be str, not bytes f.write(data) So I change it to: f.write(str(data))

-----------------------------------nessus------------------------------------------------

Run program again It write the following file to my pc: nessus_67_1103118399.nessus Buy I cannot display a nessus file on my window box. So I did the following

----------------------------------pdf----------------------------------------------------

Line: filename = 'nessus_{0}{1}.nessus'.format(sid, fid) Change to: filename = 'nessus{0}_{1}.pdf'.format(sid, fid) Run program It saves a pdf file: nessus_64_1006323990.pdf I tried to read it using Adobe Reader, It could not display it.

----------------------------------html----------------------------------------------------

I tried: filename = 'nessus_{0}_{1}.html'.format(sid, fid) Run It saves a pdf file: nessus_61_1603712528.html I tried to display it in explorer browser, but it look like the html was truncated.

--------------------------------csv----------------------------------------------------

I tried: filename = 'csv_{0}_{1}.csvl'.format(sid, fid) Run When I display the csv file on windows, it is unreadable, all the data is on line one.

-----------------------------------------------------------------------------------------

So I did the following change to: f.write(data)

f.write(data)

f.write(str(data))

f.write(data.decode('utf-8')) Run All of the output are corrupt.

--------------------------------------------------------------------------------------------

In mod: from nessrest import ness6rest

Do you have a working example, on downloading a scan to pdf, html or csv file?

Thanks for your help Pythoncode17

pythoncode17 avatar Jun 20 '16 20:06 pythoncode17