Skip to content
This repository was archived by the owner on Feb 26, 2021. It is now read-only.

Commit 5605b96

Browse files
authored
Merge pull request #11 from secureCodeBox/develop
Merge develop into master
2 parents d460bf8 + 905de4a commit 5605b96

12 files changed

Lines changed: 6789 additions & 6024 deletions

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
script: npm test
55
language: node_js
66
node_js:
7-
- '8'
7+
- '10'
88
- stage: build docker image
99
services:
1010
- docker

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ RUN npm install --production
1212

1313
COPY . /src
1414

15+
HEALTHCHECK --interval=30s --timeout=5s --start-period=120s --retries=3 CMD node healthcheck.js || exit 1
16+
1517
RUN addgroup -S nmap_group && adduser -S -g nmap_group nmap_user
1618

1719
USER nmap_user
@@ -40,4 +42,4 @@ LABEL org.opencontainers.image.title="secureCodeBox scanner-infrastructure-nmap"
4042
org.opencontainers.image.revision=$COMMIT_ID \
4143
org.opencontainers.image.created=$BUILD_DATE
4244

43-
ENTRYPOINT [ "npm", "start" ]
45+
ENTRYPOINT [ "npm", "start" ]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![Build Status](https://travis-ci.com/secureCodeBox/scanner-infrastructure-nmap.svg?token=2Rsf2E9Bq3FduSxRf6tz&branch=develop)
1+
[![Build Status](https://travis-ci.com/secureCodeBox/scanner-infrastructure-nmap.svg?branch=develop)](https://travis-ci.com/secureCodeBox/scanner-infrastructure-nmap)
22
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
33
[![Known Vulnerabilities](https://snyk.io/test/github/secureCodeBox/scanner-infrastructure-nmap/badge.svg)](https://snyk.io/test/github/secureCodeBox/scanner-infrastructure-nmap)
44
[![GitHub release](https://img.shields.io/github/release/secureCodeBox/scanner-infrastructure-nmap.svg)](https://github.com/secureCodeBox/scanner-infrastructure-nmap/releases/latest)

healthcheck.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
try {
2+
3+
require('http').request('http://localhost:8080/status', response => {
4+
5+
// exit with error for any non 2xx status code
6+
process.exit(response.statusCode >= 300 ? 1 : 0);
7+
8+
}).end();
9+
10+
} catch (err) {
11+
process.exit(1);
12+
}

package-lock.json

Lines changed: 6420 additions & 6018 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"url": "git@github.com:secureCodeBox/scanner-infrastructure-nmap.git"
1818
},
1919
"dependencies": {
20-
"@securecodebox/scanner-scaffolding": "^2.1.3",
20+
"@securecodebox/scanner-scaffolding": "^2.2.0",
2121
"lodash": "^4.17.10",
2222
"node-nmap": "^4.0.0",
2323
"uuid": "^3.2.1"

src/__snapshots__/nmap.test.js.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Object {
1818
"operating_system": null,
1919
"port": 631,
2020
"protocol": "tcp",
21+
"scripts": null,
2122
"service": "ipp",
2223
},
2324
"category": "Open Port",
@@ -39,6 +40,7 @@ Object {
3940
"operating_system": null,
4041
"port": 7778,
4142
"protocol": "tcp",
43+
"scripts": null,
4244
"service": "interwise",
4345
},
4446
"category": "Open Port",
@@ -60,6 +62,7 @@ Object {
6062
"operating_system": null,
6163
"port": 8080,
6264
"protocol": "tcp",
65+
"scripts": null,
6366
"service": "http-proxy",
6467
},
6568
"category": "Open Port",
@@ -81,6 +84,7 @@ Object {
8184
"operating_system": null,
8285
"port": 9200,
8386
"protocol": "tcp",
87+
"scripts": null,
8488
"service": "wap-wsp",
8589
},
8690
"category": "Open Port",
@@ -102,6 +106,7 @@ Object {
102106
"operating_system": null,
103107
"port": 631,
104108
"protocol": "tcp",
109+
"scripts": null,
105110
"service": "ipp",
106111
},
107112
"category": "Open Port",
@@ -123,6 +128,7 @@ Object {
123128
"operating_system": null,
124129
"port": 7778,
125130
"protocol": "tcp",
131+
"scripts": null,
126132
"service": "interwise",
127133
},
128134
"category": "Open Port",
@@ -144,6 +150,7 @@ Object {
144150
"operating_system": null,
145151
"port": 8080,
146152
"protocol": "tcp",
153+
"scripts": null,
147154
"service": "http-proxy",
148155
},
149156
"category": "Open Port",
@@ -165,6 +172,7 @@ Object {
165172
"operating_system": null,
166173
"port": 9200,
167174
"protocol": "tcp",
175+
"scripts": null,
168176
"service": "wap-wsp",
169177
},
170178
"category": "Open Port",
@@ -197,6 +205,7 @@ Object {
197205
"operating_system": null,
198206
"port": 631,
199207
"protocol": "tcp",
208+
"scripts": null,
200209
"service": "ipp",
201210
},
202211
"category": "Open Port",
@@ -218,6 +227,7 @@ Object {
218227
"operating_system": null,
219228
"port": 7778,
220229
"protocol": "tcp",
230+
"scripts": null,
221231
"service": "interwise",
222232
},
223233
"category": "Open Port",
@@ -239,6 +249,7 @@ Object {
239249
"operating_system": null,
240250
"port": 8080,
241251
"protocol": "tcp",
252+
"scripts": null,
242253
"service": "http-proxy",
243254
},
244255
"category": "Open Port",
@@ -260,6 +271,7 @@ Object {
260271
"operating_system": null,
261272
"port": 9200,
262273
"protocol": "tcp",
274+
"scripts": null,
263275
"service": "wap-wsp",
264276
},
265277
"category": "Open Port",
@@ -294,6 +306,7 @@ Object {
294306
"operating_system": null,
295307
"port": 631,
296308
"protocol": "tcp",
309+
"scripts": null,
297310
"service": "ipp",
298311
},
299312
"category": "Open Port",
@@ -315,6 +328,7 @@ Object {
315328
"operating_system": null,
316329
"port": 7778,
317330
"protocol": "tcp",
331+
"scripts": null,
318332
"service": "interwise",
319333
},
320334
"category": "Open Port",
@@ -336,6 +350,7 @@ Object {
336350
"operating_system": null,
337351
"port": 8080,
338352
"protocol": "tcp",
353+
"scripts": null,
339354
"service": "http-proxy",
340355
},
341356
"category": "Open Port",
@@ -357,6 +372,7 @@ Object {
357372
"operating_system": null,
358373
"port": 9200,
359374
"protocol": "tcp",
375+
"scripts": null,
360376
"service": "wap-wsp",
361377
},
362378
"category": "Open Port",

src/nmap.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616
* limitations under the License.
1717
* /
1818
*/
19+
20+
/**
21+
* @typedef {{ id: string, name: string, description: string, osi_layer: string, reference: any, severity: string, attributes: { port: any, ip_address: any, mac_address: any, protocol: any, hostname: any, method: any, operating_system: any, service: any, scripts: [scriptname: string]: string }, hint: any, category: any, location: any }} Finding
22+
*/
23+
1924
const _ = require('lodash');
2025
const uuid = require('uuid/v4');
2126

2227
const portscan = require('../lib/portscan');
2328

29+
const resultsXmlParser = require('./results-xml');
30+
2431
function createFinding({
2532
id = uuid(),
2633
name,
@@ -56,6 +63,7 @@ function createFinding({
5663
method,
5764
operating_system,
5865
service,
66+
scripts: null,
5967
},
6068
hint,
6169
category,
@@ -67,6 +75,7 @@ function createFinding({
6775
* Transforms the array of hosts into an array of open ports with host information included in each port entry.
6876
*
6977
* @param {array<host>} hosts An array of hosts
78+
* @returns {Finding[]}
7079
*/
7180
function transform(hosts) {
7281
return _.flatMap(hosts, ({ openPorts = [], ...hostInfo }) => {
@@ -89,6 +98,28 @@ function transform(hosts) {
8998
});
9099
}
91100

101+
/**
102+
*
103+
* @param {{ ip: string, hostname: string, port: number, scriptOutputs: {[scriptName:string]:string} }} findingFromXml
104+
* @param {Finding[]} findings
105+
*/
106+
function addScriptOutputsToFindings(findingFromXml, findings) {
107+
var res = findings.find(
108+
finding =>
109+
finding.attributes.port === findingFromXml.port &&
110+
finding.attributes.hostname === findingFromXml.hostname
111+
);
112+
if (res) {
113+
if (res.attributes.scripts === null) {
114+
res.attributes.scripts = findingFromXml.scriptOutputs;
115+
} else {
116+
Object.assign(res.attributes.scripts, findingFromXml.scriptOutputs);
117+
}
118+
} else {
119+
console.warn('found script outputs for ports that are not in the findings');
120+
}
121+
}
122+
92123
function joinResults(results) {
93124
const findings = _.flatMap(results, result => result.findings);
94125
const rawFindings = _.map(results, result => result.raw);
@@ -110,9 +141,20 @@ async function worker(targets) {
110141
const { hosts, raw } = await portscan(location, parameter);
111142
const result = transform(hosts);
112143

144+
if (
145+
typeof parameter === 'string' &&
146+
(parameter.includes('--script=') || parameter.includes('-s'))
147+
) {
148+
const findingsWithScriptOutput = await resultsXmlParser(raw);
149+
findingsWithScriptOutput.forEach(xmlFinding =>
150+
addScriptOutputsToFindings(xmlFinding, result)
151+
);
152+
}
153+
113154
results.push({ findings: result, raw });
114155
} catch (err) {
115-
if (err.startsWith(`Failed to resolve "${location}".`) || err === '\n') {
156+
var stringErr = extractErrorMessage(err);
157+
if (stringErr.startsWith(`Failed to resolve "${location}".`) || stringErr === '\n') {
116158
console.warn(err);
117159
results.push({
118160
findings: [
@@ -127,7 +169,7 @@ async function worker(targets) {
127169
],
128170
raw: '',
129171
});
130-
} else if (err.startsWith('Error converting XML to JSON in xml2js')) {
172+
} else if (stringErr.startsWith('Error converting XML to JSON in xml2js')) {
131173
const error = new Error('Failed to transform nmap xml to json.');
132174
error.name = 'TransformationError';
133175
throw error;
@@ -141,5 +183,11 @@ async function worker(targets) {
141183
return joinResults(results);
142184
}
143185

186+
function extractErrorMessage(err) {
187+
if (err.message) return err.message;
188+
if (err.toString) return err.toString();
189+
return '' + err;
190+
}
191+
144192
module.exports.transform = transform;
145193
module.exports.worker = worker;

src/nmap.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ describe('nmap', () => {
102102
hostname: 'securebox',
103103
mac_address: null,
104104
operating_system: null,
105+
scripts: null,
105106
},
106107
});
107108
});
@@ -151,6 +152,7 @@ describe('nmap', () => {
151152
hostname: 'securebox',
152153
mac_address: null,
153154
operating_system: null,
155+
scripts: null,
154156
},
155157
},
156158
{
@@ -172,6 +174,7 @@ describe('nmap', () => {
172174
hostname: 'securebox',
173175
mac_address: null,
174176
operating_system: null,
177+
scripts: null,
175178
},
176179
},
177180
]);
@@ -230,6 +233,7 @@ describe('nmap', () => {
230233
hostname: 'securebox',
231234
mac_address: null,
232235
operating_system: null,
236+
scripts: null,
233237
},
234238
},
235239
{
@@ -251,6 +255,7 @@ describe('nmap', () => {
251255
hostname: 'test',
252256
mac_address: null,
253257
operating_system: null,
258+
scripts: null,
254259
},
255260
},
256261
]);
@@ -337,6 +342,7 @@ describe('nmap', () => {
337342
ip_address: null,
338343
protocol: null,
339344
service: null,
345+
scripts: null,
340346
method: null,
341347
hostname: 'foobar',
342348
mac_address: null,

0 commit comments

Comments
 (0)