From 1a522ed134021a74e698faa3661ba2d237542bf4 Mon Sep 17 00:00:00 2001 From: Evandro Date: Thu, 25 Nov 2021 12:05:40 +0100 Subject: [PATCH 1/3] add opt "noDefaults" to deactivate default options (flags, timeout and ports) Added an option to disable the default options "flags, ports and timeouts" called "noDefaults", check in the code if this option has value "true", if it does remove "ports and timeouts", the option "flags" is cleared because cannot be deleted because of checking the "-oX -" option. --- lib/classes/tools.js | 14 +++++++++----- lib/classes/validation.js | 1 - 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/classes/tools.js b/lib/classes/tools.js index c8d8bd8..d7c640a 100644 --- a/lib/classes/tools.js +++ b/lib/classes/tools.js @@ -158,14 +158,13 @@ class tools extends emitter { */ command(opts, block) { const flags = opts.flags.join(' '); - const proto = (opts.udp) ? ' -sU' : ' '; - const to = `--host-timeout=${opts.timeout}s `; + const proto = (opts.udp) ? ' -sU ' : ' '; + const to = opts.timeout ? `--host-timeout=${opts.timeout}s ` :''; const ipv6 = (validation.test(validation.patterns.IPv6, block)) ? ' -6 ' : ' '; - return (opts.ports) ? - `${opts.nmap+proto} ${to}${flags}${ipv6}-p${opts.ports} ${block}` : - `${opts.nmap+proto} ${to}${flags}${ipv6}${block}`; + `${opts.nmap+proto}${to}${flags}${ipv6}-p${opts.ports} ${block}` : + `${opts.nmap+proto}${to}${flags}${ipv6}${block}`; } @@ -197,6 +196,11 @@ class tools extends emitter { const ranges = []; const called = caller.get()[1].getFunctionName(); + /** Remove defaults options if user disabled it (flags, ports, timeout) */ + if(opts.noDefaults) { + const {ports, timeout, ...defaultsRest} = defaults; + defaults = {...defaultsRest, flags: []}; + } /* Override 'defaults.flags' array with 'opts.flags' (prevents merge) */ if (/array/.test(typeof opts.flags)) defaults.flags = opts.flags; diff --git a/lib/classes/validation.js b/lib/classes/validation.js index 3e8b21c..ffa323b 100644 --- a/lib/classes/validation.js +++ b/lib/classes/validation.js @@ -100,7 +100,6 @@ class validation { const scope = this; let errors = []; let result; - scope.exists(opts.nmap, (result) => { if (!result) { From de61d8daf07b29046d29d56b771ed852f479bf3e Mon Sep 17 00:00:00 2001 From: Evandro Date: Thu, 25 Nov 2021 12:06:00 +0100 Subject: [PATCH 2/3] Added test and example In the tests I had to add a command to run the specific test "no-defaults.spec.js" because I have some kind of error in "discover" --- examples/no-defaults.js | 22 ++++++++++++++++++ package.json | 3 ++- test/no-defaults.spec.js | 50 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 examples/no-defaults.js create mode 100644 test/no-defaults.spec.js diff --git a/examples/no-defaults.js b/examples/no-defaults.js new file mode 100644 index 0000000..4ce507a --- /dev/null +++ b/examples/no-defaults.js @@ -0,0 +1,22 @@ +/*! + * libnmap + * Copyright(c) 2021 Evandro Brandão + * License: MIT + */ + +'use strict' + +const nmap = require('../'); +const opts = { + range: ['scanme.nmap.org', '127.0.0.1'], + noDefaults: true +}; + + +nmap.scan(opts, function(err, report) { + if (err) throw new Error(err); + + for (let item in report) { + console.log(JSON.stringify(report[item], null, 2)); + } +}); \ No newline at end of file diff --git a/package.json b/package.json index 94a74b5..5c7d3f3 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ }, "scripts": { "start": "node index.js", - "test": "./node_modules/.bin/mocha -R spec" + "test": "./node_modules/.bin/mocha -R spec", + "test:no-defaults": "./node_modules/.bin/mocha test/no-defaults.spec.js" }, "dependencies": { "async": "^2.6.3", diff --git a/test/no-defaults.spec.js b/test/no-defaults.spec.js new file mode 100644 index 0000000..f0d9b9f --- /dev/null +++ b/test/no-defaults.spec.js @@ -0,0 +1,50 @@ +/*! + * libnmap + * Copyright(c) 2013-2019 Jason Gerfen + * License: MIT + */ + +'use strict' + +const nmap = require('../'); +const timeout = 1024 * 1024 * 3; +const chai = require('chai'); +const should = chai.should(); +const expect = chai.expect; +const opts = { + range: [ + '127.0.0.1', + 'scanme.nmap.org', + '::1' + ], + noDefaults: true +}; + + +describe('scan method', function () { + + context('reporting', function () { + it('json', function (done) { + this.timeout(timeout); + + nmap.scan(opts, function (err, report) { + should.not.exist(err); + + report.should.be.a('object'); + done(); + }); + }); + + it('xml', function (done) { + this.timeout(timeout); + opts.json = false; + + nmap.scan(opts, function (err, report) { + should.not.exist(err); + + report.should.be.a('object'); + done(); + }); + }); + }); +}); From 0b93d69c9e42099591b0fa02e3a7b4b0065930bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Brand=C3=A3o?= Date: Fri, 26 Nov 2021 15:13:36 +0100 Subject: [PATCH 3/3] changed repo --- package.json | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 5c7d3f3..bd41ff6 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { - "name": "libnmap", + "name": "libnmap-xtr", "version": "v0.4.19", "description": "libnmap for node.js", - "author": "Jason Gerfen ", + "author": "Jason Gerfen , Evandro Brandão ", "keywords": [ "nmap", "libnmap", @@ -23,15 +23,11 @@ ], "repository": { "type": "git", - "url": "https://github.com/jas-/node-libnmap.git" - }, - "bugs": { - "url": "https://github.com/jas-/node-libnmap/issues" + "url": "https://github.com/kingaribel/node-libnmap.git" }, "scripts": { "start": "node index.js", - "test": "./node_modules/.bin/mocha -R spec", - "test:no-defaults": "./node_modules/.bin/mocha test/no-defaults.spec.js" + "test": "./node_modules/.bin/mocha " }, "dependencies": { "async": "^2.6.3",