libnmap 0.4.19 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,197 +1,76 @@
1
1
  /*!
2
2
  * libnmap
3
- * Copyright(c) 2013-2019 Jason Gerfen <jason.gerfen@gmail.com>
4
- * License: MIT
3
+ * Copyright(c) 2013 Jason Gerfen <jason.gerfen@gmail.com>
4
+ * License: NPSL-0.95
5
5
  */
6
6
 
7
7
  'use strict'
8
8
 
9
- const fs = require('fs');
10
- const hasbin = require('hasbin');
11
-
12
-
13
9
  class validation {
14
-
15
-
16
10
  constructor() {
17
-
18
- /**
19
- * @var messages
20
- * Error messages
21
- */
22
11
  this.messages = {
23
-
24
- version: 'Discover method requires nodejs v0.11.2 or greater; network ' +
25
- 'range(s) could not be determined',
26
-
27
- path: 'Supplied path for nmap binary is invalid',
28
-
29
- block: 'Supplied blocksize must not exceed 128',
12
+ adapters: 'Discover method found no IPv4 network on the network ' +
13
+ 'adapters',
30
14
 
31
15
  range: 'Range must be an array of host(s). Examples: ' +
32
16
  '192.168.2.10 (single), 10.0.2.0/24 (CIDR), 10.0.10.5-20 (range)',
33
17
 
18
+ flags: 'Flags must be an array of strings without NUL characters',
19
+
34
20
  port: 'Port(s) must match one of the following examples: ' +
35
21
  '512 (single) | 0-65535 (range) | 10-30,80,443,3306-10000 (multiple)',
36
22
  };
37
23
 
38
-
39
-
40
- /**
41
- * @var patterns
42
- * Object with various REGEX patterns to validate network params
43
- */
44
24
  this.patterns = {
25
+ ports: /^(?:[TUSP]:)?(?:\d{1,5}(?:-\d{0,5})?|-\d{1,5})(?:,(?:[TUSP]:)?(?:\d{1,5}(?:-\d{0,5})?|-\d{1,5}))*$/,
26
+
27
+ hostname: /^(?=.{1,253}$)(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/,
45
28
 
46
- /**
47
- * @var ports
48
- * Regex for matching port ranges
49
- * @ref http://stackoverflow.com/a/21075138/901697
50
- */
51
- ports: /\d{1,6}(?:-\d+)?/g,
52
-
53
- /**
54
- * @var hostname
55
- * Regex for matching hostnames (RFC-1123)
56
- */
57
- hostname: /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])|localhost$/,
58
-
59
- /**
60
- * @var IPv4
61
- * Regex for matching IPv4 address types
62
- */
63
29
  IPv4: /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/,
64
30
 
65
- /**
66
- * @var IPv4CIDR
67
- * Regex for matching IPv4 CIDR notation
68
- */
69
- IPv4CIDR: /(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([1-2]\d|3[0-2]|\d))/,
31
+ IPv4CIDR: /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([1-2]\d|3[0-2]|\d))$/,
70
32
 
71
- /**
72
- * @var IPv4Range
73
- * Regex for matching IPv4 Range notation
74
- */
75
33
  IPv4Range: /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\-([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/,
76
34
 
77
- /**
78
- * @var IPv6
79
- * Regex for matching IPv6 address types
80
- */
81
- IPv6: /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(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d|[1-9]?\d)){3}))|:)))(%[\w.-]+)?(\/(\d|\d\d|1[0-1]\d|12[0-8]))$/,
88
38
  };
89
39
  }
90
40
 
91
-
92
- /**
93
- * @function init
94
- * Construct for network/port validation
95
- *
96
- * @param {Object} opts - User supplied options
97
- * @param {Function} cb - Callback
98
- */
99
41
  init(opts, cb) {
100
- const scope = this;
101
- let errors = [];
102
- let result;
103
-
104
- scope.exists(opts.nmap, (result) => {
105
- if (!result) {
106
-
107
- // Try full path vs. process.env.PATH
108
- fs.access(opts.nmap, fs.constants.F_OK | fs.constants.X_OK, e => {
109
- if (e)
110
- return cb(scope.messages.path);
111
- });
112
- }
113
- });
114
-
115
- if (opts.blocksize > 128)
116
- return cb(scope.messages.block);
117
-
118
- if ((!opts.range) || (!/array|object/.test(typeof (opts.range))) ||
119
- (opts.range.length === 0))
120
- return cb(scope.messages.range);
121
-
122
- if (opts.range.length >= 1) {
123
- opts.range.forEach(value => {
124
- scope.verify(value, (err, result) => {
125
- if (err) return cb(scope.messages.range);
126
- });
127
- });
128
- }
129
-
130
- if (!opts.ports)
131
- errors.push(new Error(scope.messages.port));
42
+ if (!Array.isArray(opts.range) || opts.range.length === 0)
43
+ return cb(this.messages.range);
132
44
 
133
- if (opts.ports) {
134
- result = opts.ports.match(scope.patterns.ports).join(",");
135
- if (result == "")
136
- return cb(scope.messages.port);
137
- }
138
-
139
- return cb(null, true);
140
- }
45
+ // The C strings of nmap end at a NUL, so the remainder would be lost
46
+ if (!Array.isArray(opts.flags) || opts.flags.some(flag =>
47
+ typeof flag !== 'string' || flag.indexOf('\0') !== -1))
48
+ return cb(this.messages.flags);
141
49
 
50
+ for (const host of opts.range) {
51
+ const err = this.verify(host);
142
52
 
143
- /**
144
- * @function verify
145
- * Verify options provided
146
- *
147
- * @param {String} host User supplied configuration object
148
- * @param {Function} cb - Callback
149
- *
150
- * @returns {Function}
151
- */
152
- verify(host, cb) {
153
-
154
- if (this.test(this.patterns.hostname, host) ||
155
- this.test(this.patterns.IPv4, host) ||
156
- this.test(this.patterns.IPv6, host) ||
157
- this.test(this.patterns.IPv4CIDR, host) ||
158
- this.test(this.patterns.IPv6CIDR, host) ||
159
- this.test(this.patterns.IPv4Range, host)) {
160
- return cb(null, true);
161
- }
162
- else {
163
- return cb(`Supplied host (${host}) did not pass validation.
164
- ${this.messages.range}`);
53
+ if (err)
54
+ return cb(err);
165
55
  }
166
- }
167
56
 
57
+ if (opts.ports && !this.patterns.ports.test(String(opts.ports)))
58
+ return cb(this.messages.port);
168
59
 
169
- /**
170
- * @function test
171
- * Test specified regex test on string
172
- *
173
- * @param {Object} regex - Regex test case
174
- * @param {String} str - String to perform test on
175
- *
176
- * @returns {Boolean}
177
- */
178
- test(regex, str) {
179
- return regex.test(str);
60
+ return cb(null, true);
180
61
  }
181
62
 
63
+ verify(host) {
64
+ const names = ['hostname', 'IPv4', 'IPv6', 'IPv4CIDR', 'IPv6CIDR',
65
+ 'IPv4Range'];
66
+
67
+ if (typeof host === 'string' &&
68
+ names.some(name => this.patterns[name].test(host)))
69
+ return null;
182
70
 
183
- /**
184
- * @function exists
185
- * Binary file tests
186
- *
187
- * @param {String} path - Path for file
188
- *
189
- * @returns {Boolean}
190
- */
191
- exists(path) {
192
- return hasbin.sync(path);
71
+ return `Supplied host (${host}) did not pass validation. ` +
72
+ this.messages.range;
193
73
  }
194
74
  }
195
75
 
196
-
197
76
  module.exports = new validation;
@@ -0,0 +1,192 @@
1
+ /*!
2
+ * libnmap
3
+ * Copyright(c) 2013 Jason Gerfen <jason.gerfen@gmail.com>
4
+ * License: NPSL-0.95
5
+ */
6
+
7
+ 'use strict'
8
+
9
+ const entities = {
10
+ amp: '&',
11
+ lt: '<',
12
+ gt: '>',
13
+ quot: '"',
14
+ apos: "'"
15
+ };
16
+
17
+ // Sets an own property. A name from the report, such as __proto__, cannot
18
+ // change the prototype of the object.
19
+ const define = (obj, name, value) => Object.defineProperty(obj, name, {
20
+ value, enumerable: true, writable: true, configurable: true
21
+ });
22
+
23
+ class xml {
24
+ decode(str) {
25
+ if (str.indexOf('&') === -1)
26
+ return str;
27
+
28
+ return str.replace(/&(#x[0-9a-fA-F]+|#[0-9]+|[a-zA-Z]+);/g, (all, name) => {
29
+ if (name[0] !== '#')
30
+ return entities.hasOwnProperty(name) ? entities[name] : all;
31
+
32
+ const code = (name[1] === 'x') ?
33
+ parseInt(name.slice(2), 16) : parseInt(name.slice(1), 10);
34
+
35
+ // A surrogate alone is not a character, and gives a string that is
36
+ // not well-formed.
37
+ if (code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff))
38
+ return all;
39
+
40
+ return String.fromCodePoint(code);
41
+ });
42
+ }
43
+
44
+ attributes(str) {
45
+ const regex = /([^\s=\/]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
46
+ let result = null;
47
+ let match;
48
+
49
+ while ((match = regex.exec(str)) !== null) {
50
+ result = result || {};
51
+ define(result, match[1], this.decode(match[2] !== undefined ?
52
+ match[2] : match[3]));
53
+ }
54
+
55
+ return result;
56
+ }
57
+
58
+ close(node) {
59
+ const obj = node.obj;
60
+
61
+ if (Object.keys(obj).length === 0)
62
+ return node.text;
63
+
64
+ if (!/\S/.test(node.text))
65
+ return obj;
66
+
67
+ obj._ = node.text;
68
+
69
+ return obj;
70
+ }
71
+
72
+ parse(str) {
73
+ const stack = [];
74
+ const result = {};
75
+ const length = str.length;
76
+ let pos = 0;
77
+
78
+ const find = (what, from) => {
79
+ const idx = str.indexOf(what, from);
80
+
81
+ if (idx === -1)
82
+ throw new Error(`XML report is not complete: no '${what}' found`);
83
+
84
+ return idx;
85
+ };
86
+
87
+ while (pos < length) {
88
+ const lt = str.indexOf('<', pos);
89
+ const top = stack[stack.length - 1];
90
+
91
+ if (lt !== pos) {
92
+ const end = (lt === -1) ? length : lt;
93
+
94
+ if (top)
95
+ top.text += this.decode(str.slice(pos, end));
96
+
97
+ pos = end;
98
+ continue;
99
+ }
100
+
101
+ if (str.startsWith('<!--', pos)) {
102
+ pos = find('-->', pos + 4) + 3;
103
+
104
+ } else if (str.startsWith('<![CDATA[', pos)) {
105
+ const end = find(']]>', pos + 9);
106
+
107
+ if (top)
108
+ top.text += str.slice(pos + 9, end);
109
+
110
+ pos = end + 3;
111
+
112
+ } else if (str.startsWith('<?', pos)) {
113
+ pos = find('?>', pos + 2) + 2;
114
+
115
+ } else if (str.startsWith('<!', pos)) {
116
+ const gt = find('>', pos);
117
+ const bracket = str.indexOf('[', pos);
118
+
119
+ pos = (bracket !== -1 && bracket < gt) ?
120
+ find('>', find(']', bracket)) + 1 : gt + 1;
121
+
122
+ } else if (str[pos + 1] === '/') {
123
+ const gt = find('>', pos);
124
+ const name = str.slice(pos + 2, gt).trim();
125
+ const node = stack.pop();
126
+
127
+ if (!node || node.name !== name)
128
+ throw new Error(`XML report is not valid: unexpected </${name}>`);
129
+
130
+ this.add(stack, result, name, this.close(node));
131
+ pos = gt + 1;
132
+
133
+ } else {
134
+ let gt = pos + 1;
135
+ let quote = '';
136
+
137
+ for (; gt < length; gt++) {
138
+ const c = str[gt];
139
+
140
+ if (quote) {
141
+ if (c === quote) quote = '';
142
+ } else if (c === '"' || c === "'") {
143
+ quote = c;
144
+ } else if (c === '>') {
145
+ break;
146
+ }
147
+ }
148
+
149
+ if (gt >= length)
150
+ throw new Error("XML report is not complete: no '>' found");
151
+
152
+ const empty = (str[gt - 1] === '/');
153
+ const tag = str.slice(pos + 1, empty ? gt - 1 : gt);
154
+ const space = tag.search(/\s/);
155
+ const name = (space === -1) ? tag : tag.slice(0, space);
156
+ const attrs = (space === -1) ? null : this.attributes(tag.slice(space));
157
+ const node = { name, obj: {}, text: '' };
158
+
159
+ if (attrs)
160
+ node.obj.item = attrs;
161
+
162
+ if (empty)
163
+ this.add(stack, result, name, this.close(node));
164
+ else
165
+ stack.push(node);
166
+
167
+ pos = gt + 1;
168
+ }
169
+ }
170
+
171
+ if (stack.length > 0)
172
+ throw new Error(`XML report is not complete: no </${stack.pop().name}>`);
173
+
174
+ return result;
175
+ }
176
+
177
+ add(stack, result, name, value) {
178
+ if (stack.length === 0) {
179
+ define(result, name, value);
180
+ return;
181
+ }
182
+
183
+ const parent = stack[stack.length - 1].obj;
184
+
185
+ if (!Object.prototype.hasOwnProperty.call(parent, name))
186
+ define(parent, name, []);
187
+
188
+ parent[name].push(value);
189
+ }
190
+ }
191
+
192
+ module.exports = new xml;
package/lib/libnmap.js CHANGED
@@ -1,121 +1,46 @@
1
1
  /*!
2
2
  * libnmap
3
- * Copyright(c) 2013-2019 Jason Gerfen <jason.gerfen@gmail.com>
4
- * License: MIT
3
+ * Copyright(c) 2013 Jason Gerfen <jason.gerfen@gmail.com>
4
+ * License: NPSL-0.95
5
5
  */
6
6
 
7
7
  'use strict'
8
8
 
9
- const os = require('os');
10
- const tools = require('./classes/tools.js')
11
- const reporting = require('./classes/reporting.js')
12
-
13
-
14
- /**
15
- * @function nmap
16
- * The libnmap robot
17
- *
18
- * @param {Object} options - Optional object of default overrides
19
- * @param {Function} fn - Callback function providing errors and reports
20
- */
21
- const nmap = function(options, fn) {
22
-
23
-
24
- /**
25
- * @object defaults
26
- * Default set of options
27
- *
28
- * @param {String} nmap - Path to NMAP binary
29
- * @param {Boolean} verbose - Turn on verbosity during scan(s)
30
- * @param {String} ports - Range of ports to scan
31
- * @param {Array} range - An array of hostnames/ipv4/ipv6, CIDR or ranges
32
- * @param {Number} timeout - Number of seconds to wait for host/port response
33
- * @param {Number} blocksize - Number of hosts per network scanning block
34
- * @param {Number} threshold - Max number of spawned process
35
- * @param {Array} flags - Array of flags for .spawn()
36
- * @param {Boolean} udp - Perform a scan using the UDP protocol
37
- * @param {Boolean} json - JSON object as output, false produces XML
38
- */
39
- const defaults = {
40
- nmap: 'nmap',
41
- verbose: false,
42
- ports: '1-1024',
43
- range: [],
44
- timeout: 120,
45
- blocksize: 16,
46
- threshold: os.cpus().length * 4,
47
- flags: [
48
- '-T4', // Scan optimization is default
49
- ],
50
- udp: false,
51
- json: true
52
- };
53
-
54
-
55
- /**
56
- * @function discover
57
- * Finds online neighbors
58
- *
59
- * @param {Object} obj User supplied options
60
- * @param {Function} cb User supplied callback function
61
- */
62
- nmap.prototype.discover = (obj, cb) => {
63
- cb = cb || obj;
64
-
65
- let opts = {};
66
-
67
- tools.init(defaults, obj, (err, settings) => {
68
- if (err)
69
- return cb(new Error(err));
70
-
71
- opts = settings.opts;
72
- opts.funcs = settings.funcs;
73
-
74
- tools.worker(opts, (err, data) => {
75
- if (err)
76
- return cb(new Error(err));
77
-
78
- cb(null, data);
79
- });
80
- });
81
- };
9
+ const tools = require('./classes/tools.js');
82
10
 
11
+ const defaults = {
12
+ ports: '1-1024',
13
+ range: [],
14
+ timeout: 120,
15
+ flags: ['-T4'],
16
+ udp: false,
17
+ json: true
18
+ };
83
19
 
84
- /**
85
- * @function scan
86
- * Performs scan of specified host/port combination
87
- *
88
- * @param {Object} obj User supplied options
89
- * @param {Function} cb User supplied callback function
90
- */
91
- nmap.prototype.scan = (obj, cb) => {
92
- cb = cb || obj;
20
+ const run = (obj, cb, discover) => {
21
+ cb = cb || obj;
93
22
 
94
- let opts = {};
23
+ tools.init(defaults, obj, (err, opts) => {
24
+ if (err)
25
+ return cb(new Error(err));
95
26
 
96
- tools.init(defaults, obj, (err, settings) => {
27
+ tools.worker(opts, (err, report) => {
97
28
  if (err)
98
- return cb(new Error(err));
99
-
100
- opts = settings.opts;
101
- opts.funcs = settings.funcs;
29
+ return cb((err instanceof Error) ? err : new Error(err));
102
30
 
103
- tools.worker(opts, (err, data) => {
104
- if (err)
105
- return cb(new Error(err));
106
-
107
- cb(null, data);
108
- });
31
+ cb(null, report);
109
32
  });
110
- };
33
+ }, discover);
111
34
  };
112
35
 
36
+ class nmap {
37
+ discover(obj, cb) {
38
+ run(obj, cb, true);
39
+ }
113
40
 
114
- /* Catch uncaught exceptions */
115
- process.on('uncaughtException', (err) => {
116
- console.trace(new Error(err));
117
- });
118
-
41
+ scan(obj, cb) {
42
+ run(obj, cb, false);
43
+ }
44
+ }
119
45
 
120
- /* robot, do work */
121
46
  module.exports = new nmap();
package/lib/worker.js ADDED
@@ -0,0 +1,24 @@
1
+ /*!
2
+ * libnmap
3
+ * Copyright(c) 2013 Jason Gerfen <jason.gerfen@gmail.com>
4
+ * License: NPSL-0.95
5
+ */
6
+
7
+ 'use strict'
8
+
9
+ // Runs one nmap scan in this process. tools.js starts one worker for each
10
+ // network block, because nmap can run one time only in a process.
11
+ // The arguments are nmap arguments. The report goes to stdout.
12
+
13
+ let binding;
14
+
15
+ try {
16
+ binding = require('../build/Release/nmap.node');
17
+ } catch (err) {
18
+ // The package manager did not run the install script, or the build failed
19
+ console.error('libnmap: the nmap module is not compiled. ' +
20
+ `Run "npm rebuild libnmap" and allow its install script. (${err.code})`);
21
+ process.exit(1);
22
+ }
23
+
24
+ process.exitCode = binding.run(process.argv.slice(2));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libnmap",
3
- "version": "v0.4.19",
3
+ "version": "1.0.1",
4
4
  "description": "libnmap for node.js",
5
5
  "author": "Jason Gerfen <jason.gerfen@gmail.com>",
6
6
  "keywords": [
@@ -29,22 +29,38 @@
29
29
  "url": "https://github.com/jas-/node-libnmap/issues"
30
30
  },
31
31
  "scripts": {
32
- "start": "node index.js",
33
- "test": "./node_modules/.bin/mocha -R spec"
32
+ "install": "node scripts/fetch-sources.js && node-gyp rebuild",
33
+ "build": "node scripts/fetch-sources.js && node-gyp rebuild",
34
+ "prepack": "node scripts/fetch-sources.js --check",
35
+ "test": "node --test test/unit.js test/local.js",
36
+ "test:network": "node --test test/scan.js test/discover.js"
34
37
  },
35
- "dependencies": {
36
- "async": "^2.6.3",
37
- "cidr-js": "git+https://github.com/jas-/cidr-js.git#v2.3.2",
38
- "deepmerge": "^2.2.1",
39
- "hasbin": "^1.2.3",
40
- "ip-address": "^5.9.4",
41
- "netmask": "^1.0.6",
42
- "stack-trace": "0.0.10",
43
- "xml2js": "^0.4.22"
38
+ "license": "SEE LICENSE IN LICENSE",
39
+ "main": "index.js",
40
+ "gypfile": true,
41
+ "os": [
42
+ "linux",
43
+ "darwin"
44
+ ],
45
+ "engines": {
46
+ "node": ">=22"
44
47
  },
45
- "devDependencies": {
46
- "mocha": "latest",
47
- "chai": "latest"
48
+ "files": [
49
+ "index.js",
50
+ "lib/",
51
+ "binding.gyp",
52
+ "scripts/",
53
+ "src/addon.cc",
54
+ "examples/"
55
+ ],
56
+ "nmap": {
57
+ "repository": "https://github.com/nmap/nmap",
58
+ "version": "7.992",
59
+ "commit": "0ee561663c1d7d2d4e708cd414dccfd3d9df30d9"
48
60
  },
49
- "license": "MIT"
61
+ "openssl": {
62
+ "repository": "https://github.com/openssl/openssl",
63
+ "version": "3.5.9",
64
+ "commit": "45e844fa2a14ec92d146bd8f5778ac130b6625fb"
65
+ }
50
66
  }