Skip to content

Instantly share code, notes, and snippets.

@mtcoffee
Created July 28, 2023 01:15
Show Gist options
  • Select an option

  • Save mtcoffee/f458b46ab75ba92f15bfca8fc0156b3a to your computer and use it in GitHub Desktop.

Select an option

Save mtcoffee/f458b46ab75ba92f15bfca8fc0156b3a to your computer and use it in GitHub Desktop.
Nessus Scanner API Query ServiceNow background script
/*
MID SERVER WILL NEED TO TRUST YOUR CERTIFICATE OR YOU CAN UPDATE MID CERTIFICATE POLICY FOR Intranet
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0864769
*/
var username = 'admin';
var pwd = 'pass';
var nessusHost = 'secops.domain.home';
var scanName = 'My Scan for Lab';
var midserver = 'winmid01';
//Get token with basic credentials
r = new sn_ws.RESTMessageV2();
r.setEndpoint('https://' + nessusHost + ':8834/session');
r.setRequestHeader('Content-Type', 'application/json');
r.setHttpMethod("POST");
var body = {
"username": username,
"password": pwd
};
r.setRequestBody(JSON.stringify(body));
r.setMIDServer(midserver); //MID Server Name
var response = r.execute();
var responseBody = response.getBody();
var parsedResponse = JSON.parse(responseBody);
var token = parsedResponse.token
//now to get scan list using token
r2 = new sn_ws.RESTMessageV2();
r2.setEndpoint('https://' + nessusHost + ':8834/scans');
r2.setRequestHeader('Content-Type', 'application/json');
r2.setRequestHeader('X-Cookie', 'token=' + token);
r2.setHttpMethod("GET");
r2.setMIDServer(midserver); //MID Server Name
var response2 = r2.execute();
var scans = response2.getBody();
var parsedScans = JSON.parse(scans);
var scanList = parsedScans.scans
//now get scan ID based on name filter
for (var i = 0; i < scanList.length; i++) {
if (scanList[i].name === scanName) {
var scanID = (scanList[i].id);
}
}
//now get the scan
r3 = new sn_ws.RESTMessageV2();
r3.setEndpoint('https://' + nessusHost + ':8834/scans/' + scanID);
r3.setRequestHeader('Content-Type', 'application/json');
r3.setRequestHeader('X-Cookie', 'token=' + token);
r3.setHttpMethod("GET");
r3.setMIDServer(midserver); //MID Server Name
var response3 = r3.execute();
var scanbody = response3.getBody();
var parsedScan = JSON.parse(scanbody);
var scan = parsedScan.hosts
//now loop through each host in the scan and get its vulnerability list
for (var host in scan) {
gs.info('Fetching vulnerabilities for ID =' + scan[host].host_id + ' hostname =' + scan[host].hostname)
r4 = new sn_ws.RESTMessageV2();
r4.setEndpoint('https://' + nessusHost + ':8834/scans/' + scanID + '/hosts/' + scan[host].host_id);
r4.setRequestHeader('Content-Type', 'application/json');
r4.setRequestHeader('X-Cookie', 'token=' + token);
r4.setHttpMethod("GET");
r4.setMIDServer(midserver); //MID Server Name
var response4 = r4.execute();
var vulnbody = response4.getBody();
var parsedVulns = JSON.parse(vulnbody);
var vulns = parsedVulns.vulnerabilities;
for (var vuln in vulns) {
if (vulns[vuln].severity >= 3) { //only get High Risk Vulnerabilities
gs.info(scan[host].hostname + ',' + vulns[vuln].plugin_name + ',' + vulns[vuln].severity)
}
}
gs.info('\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment