-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.js
More file actions
1074 lines (984 loc) · 33.8 KB
/
Copy pathscanner.js
File metadata and controls
1074 lines (984 loc) · 33.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
/**
* NodeJS-PortScanner — a fast, lightweight TCP port scanner with service fingerprinting.
*
* The module exports small, pure-ish building blocks (port parsing, argument
* parsing, fingerprinting, formatting) alongside the network primitives
* (`scanPort`, `scanPorts`) so the logic can be unit-tested against a local
* mock server without any real network access. When executed directly it runs
* as a CLI.
*/
import net from "node:net";
import dgram from "node:dgram";
import { writeFile } from "node:fs/promises";
import { pathToFileURL } from "node:url";
/** Default option values, shared between the CLI and programmatic API. */
export const DEFAULTS = Object.freeze({
ports: "1-1024",
protocol: "tcp",
concurrency: 100,
timeout: 2000,
rate: 0,
});
/** Largest banner (in bytes) we are willing to buffer per connection. */
const MAX_BANNER_BYTES = 4096;
/** How long to keep reading after the first banner byte arrives (ms). */
const BANNER_SETTLE_MS = 60;
// ---------------------------------------------------------------------------
// Port parsing
// ---------------------------------------------------------------------------
const MIN_PORT = 1;
const MAX_PORT = 65535;
/**
* Convert a single textual port into a validated number.
* @param {string} value
* @param {string} original - the original token, used for error messages
* @returns {number}
*/
function toPort(value, original) {
if (!/^\d+$/.test(value)) {
throw new Error(
`Invalid port: "${original}" (must be an integer between ${MIN_PORT} and ${MAX_PORT})`,
);
}
const port = Number(value);
if (port < MIN_PORT || port > MAX_PORT) {
throw new Error(
`Invalid port: "${original}" (must be between ${MIN_PORT} and ${MAX_PORT})`,
);
}
return port;
}
/**
* Parse a port specification into a sorted, de-duplicated list of ports.
* Supports comma-separated values and inclusive ranges, e.g.
* `"1-1024"`, `"80,443,8080"`, or `"22,80,8000-8010"`.
*
* @param {string} spec
* @returns {number[]}
*/
export function parsePorts(spec) {
if (typeof spec !== "string" || spec.trim() === "") {
throw new Error("Port specification must be a non-empty string");
}
const ports = new Set();
const tokens = spec
.split(",")
.map((t) => t.trim())
.filter((t) => t.length > 0);
if (tokens.length === 0) {
throw new Error("Port specification must contain at least one port");
}
for (const token of tokens) {
if (token.includes("-")) {
const bounds = token.split("-");
if (bounds.length !== 2) {
throw new Error(`Invalid port range: "${token}"`);
}
const start = toPort(bounds[0].trim(), token);
const end = toPort(bounds[1].trim(), token);
if (start > end) {
throw new Error(`Invalid port range (start > end): "${token}"`);
}
for (let p = start; p <= end; p++) {
ports.add(p);
}
} else {
ports.add(toPort(token, token));
}
}
return [...ports].sort((a, b) => a - b);
}
// ---------------------------------------------------------------------------
// Argument parsing
// ---------------------------------------------------------------------------
const ALIASES = Object.freeze({
"-h": "--host",
"-p": "--ports",
"-P": "--protocol",
"-c": "--concurrency",
"-r": "--rate",
"-t": "--timeout",
"-o": "--output",
});
/**
* Require that an option was supplied a value.
* @param {string} flag
* @param {string|undefined} value
* @returns {string}
*/
function requireValue(flag, value) {
if (value === undefined) {
throw new Error(`Option "${flag}" requires a value`);
}
return value;
}
/**
* Parse a positive integer option value.
* @param {string} value
* @param {string} flag
* @returns {number}
*/
function toPositiveInt(value, flag) {
if (!/^\d+$/.test(value) || Number(value) < 1) {
throw new Error(
`Option "${flag}" requires a positive integer (got "${value}")`,
);
}
return Number(value);
}
/**
* Validate and normalise the transport protocol option.
* @param {string} value
* @param {string} flag
* @returns {("tcp"|"udp")}
*/
function toProtocol(value, flag) {
const normalized = String(value).toLowerCase();
if (normalized !== "tcp" && normalized !== "udp") {
throw new Error(
`Option "${flag}" must be "tcp" or "udp" (got "${value}")`,
);
}
return normalized;
}
/**
* Parse CLI arguments (everything after `node scanner.js`).
*
* @param {string[]} argv
* @returns {{host: string|null, ports: string, protocol: ("tcp"|"udp"),
* concurrency: number, timeout: number, rate: number,
* output: string|null, help: boolean}}
*/
export function parseArgs(argv) {
const options = {
host: null,
ports: DEFAULTS.ports,
protocol: DEFAULTS.protocol,
concurrency: DEFAULTS.concurrency,
timeout: DEFAULTS.timeout,
rate: DEFAULTS.rate,
output: null,
help: false,
};
for (let i = 0; i < argv.length; i++) {
let flag = argv[i];
// Support `--flag=value` syntax by splitting on the first `=`.
let inlineValue;
const eq = flag.indexOf("=");
if (flag.startsWith("--") && eq !== -1) {
inlineValue = flag.slice(eq + 1);
flag = flag.slice(0, eq);
}
if (ALIASES[flag]) {
flag = ALIASES[flag];
}
const nextValue = () =>
inlineValue !== undefined
? inlineValue
: requireValue(flag, argv[++i]);
switch (flag) {
case "--help":
options.help = true;
break;
case "--host":
options.host = nextValue();
break;
case "--ports":
options.ports = nextValue();
break;
case "--protocol":
options.protocol = toProtocol(nextValue(), flag);
break;
case "--concurrency":
options.concurrency = toPositiveInt(nextValue(), flag);
break;
case "--timeout":
options.timeout = toPositiveInt(nextValue(), flag);
break;
case "--rate":
options.rate = toPositiveInt(nextValue(), flag);
break;
case "--output":
options.output = nextValue();
break;
default:
throw new Error(`Unknown option: "${argv[i]}"`);
}
}
return options;
}
// ---------------------------------------------------------------------------
// Service fingerprinting
// ---------------------------------------------------------------------------
/** Map of well-known ports to a default service label. */
export const WELL_KNOWN_PORTS = Object.freeze({
21: "FTP",
22: "SSH",
23: "Telnet",
25: "SMTP",
53: "DNS",
80: "HTTP",
110: "POP3",
111: "RPC",
135: "MSRPC",
139: "NetBIOS",
143: "IMAP",
443: "HTTPS",
445: "SMB",
465: "SMTPS",
587: "SMTP",
993: "IMAPS",
995: "POP3S",
1433: "MSSQL",
1521: "Oracle",
2049: "NFS",
3306: "MySQL",
3389: "RDP",
5432: "PostgreSQL",
5900: "VNC",
6379: "Redis",
8080: "HTTP-Proxy",
8443: "HTTPS-Alt",
9200: "Elasticsearch",
11211: "Memcached",
27017: "MongoDB",
// Commonly UDP-only services.
123: "NTP",
137: "NetBIOS-NS",
161: "SNMP",
500: "ISAKMP",
514: "Syslog",
1900: "SSDP",
5353: "mDNS",
});
/**
* Ordered banner signatures. The first matching pattern wins, so more specific
* patterns must come before more generic ones.
* @type {Array<{pattern: RegExp, label: (m: RegExpMatchArray) => string}>}
*/
export const BANNER_SIGNATURES = Object.freeze([
{
pattern: /SSH-\d+(?:\.\d+)?-OpenSSH[_/-]?([\d.]+)/i,
label: (m) => `SSH (OpenSSH ${m[1]})`,
},
{ pattern: /SSH-\d/i, label: () => "SSH" },
{
pattern: /server:\s*nginx(?:\/([\d.]+))?/i,
label: (m) => (m[1] ? `HTTP (nginx ${m[1]})` : "HTTP (nginx)"),
},
{
pattern: /server:\s*Apache(?:\/([\d.]+))?/i,
label: (m) => (m[1] ? `HTTP (Apache ${m[1]})` : "HTTP (Apache)"),
},
{ pattern: /HTTP\/\d\.\d/i, label: () => "HTTP" },
{
pattern: /vsftpd\s*([\d.]+)?/i,
label: (m) => (m[1] ? `FTP (vsFTPd ${m[1]})` : "FTP"),
},
{ pattern: /220[\s-].*(?:ftp|filezilla|pure-ftpd)/i, label: () => "FTP" },
{ pattern: /220[\s-].*(?:smtp|esmtp)/i, label: () => "SMTP" },
{ pattern: /\+OK.*pop3/i, label: () => "POP3" },
{ pattern: /\*\s*OK.*IMAP/i, label: () => "IMAP" },
{
pattern: /mysql_native_password|mariadb|\bmysql\b/i,
label: () => "MySQL",
},
{ pattern: /-ERR.*redis|redis_version/i, label: () => "Redis" },
]);
/**
* Identify the service running on a port using its banner (preferred) and a
* well-known-port fallback.
*
* @param {number} port
* @param {string} [banner]
* @returns {string}
*/
export function fingerprint(port, banner = "") {
const text = String(banner ?? "");
if (text.length > 0) {
for (const sig of BANNER_SIGNATURES) {
const match = text.match(sig.pattern);
if (match) {
return sig.label(match);
}
}
}
return WELL_KNOWN_PORTS[port] ?? "unknown";
}
// ---------------------------------------------------------------------------
// Scanning
// ---------------------------------------------------------------------------
/**
* @typedef {Object} ScanResult
* @property {number} port
* @property {("tcp"|"udp")} protocol
* @property {boolean} open - true only when the state is definitively "open"
* @property {("open"|"closed"|"open|filtered")} state
* @property {string} banner
* @property {string|null} service
*/
/**
* Attempt a TCP connection to a single port and, when open, grab any banner
* the server volunteers in order to fingerprint the service.
*
* Always resolves (never rejects) with a {@link ScanResult}.
*
* @param {string} host
* @param {number} port
* @param {{timeout?: number, grabBanner?: boolean, bannerTimeout?: number}} [options]
* @returns {Promise<ScanResult>}
*/
export function scanPort(host, port, options = {}) {
const {
timeout = DEFAULTS.timeout,
grabBanner = true,
bannerTimeout = Math.min(750, timeout),
} = options;
return new Promise((resolve) => {
const socket = new net.Socket();
let banner = "";
let connected = false;
let settled = false;
let bannerTimer = null;
const finish = (open) => {
if (settled) return;
settled = true;
if (bannerTimer) clearTimeout(bannerTimer);
socket.destroy();
const trimmed = banner.trim();
resolve({
port,
protocol: "tcp",
open,
state: open ? "open" : "closed",
banner: trimmed,
service: open ? fingerprint(port, trimmed) : null,
});
};
socket.setTimeout(timeout);
socket.once("connect", () => {
connected = true;
// Connection succeeded; the connect timeout no longer applies. From here
// we only wait a short window for an optional banner.
socket.setTimeout(0);
if (!grabBanner) {
finish(true);
return;
}
bannerTimer = setTimeout(() => finish(true), bannerTimeout);
});
socket.on("data", (chunk) => {
banner += chunk.toString("utf8");
if (banner.length >= MAX_BANNER_BYTES) {
finish(true);
return;
}
// Keep collecting briefly in case the banner spans multiple packets.
if (bannerTimer) clearTimeout(bannerTimer);
bannerTimer = setTimeout(() => finish(true), BANNER_SETTLE_MS);
});
// A connect timeout means the port is filtered/closed; once connected the
// timeout is disabled so this only fires before a successful connection.
socket.once("timeout", () => finish(false));
socket.once("error", () => finish(false));
socket.once("close", () => finish(connected));
socket.connect(port, host);
});
}
// ---------------------------------------------------------------------------
// UDP scanning
// ---------------------------------------------------------------------------
/** Choose a dgram socket type based on the target's address family. */
function udpSocketType(host) {
return net.isIPv6(host) ? "udp6" : "udp4";
}
/**
* A minimal DNS query (root NS record, recursion desired) used to coax a
* response out of a resolver listening on UDP/53.
*/
const DNS_PROBE = Buffer.from([
0x12,
0x34, // transaction ID
0x01,
0x00, // flags: standard query, recursion desired
0x00,
0x01, // QDCOUNT = 1
0x00,
0x00, // ANCOUNT = 0
0x00,
0x00, // NSCOUNT = 0
0x00,
0x00, // ARCOUNT = 0
0x00, // QNAME = root
0x00,
0x02, // QTYPE = NS
0x00,
0x01, // QCLASS = IN
]);
/** An NTPv3 client request (mode 3) used to probe UDP/123. */
const NTP_PROBE = (() => {
const buf = Buffer.alloc(48);
buf[0] = 0x1b; // LI = 0, Version = 3, Mode = 3 (client)
return buf;
})();
/** Fallback payload for UDP ports without a tailored probe. */
const DEFAULT_UDP_PROBE = Buffer.from("\r\n");
/**
* Select a probe payload for a UDP port. Tailored probes elicit replies from
* services that ignore arbitrary data; everything else gets a generic nudge.
* @param {number} port
* @returns {Buffer}
*/
function udpProbe(port) {
switch (port) {
case 53:
return DNS_PROBE;
case 123:
return NTP_PROBE;
default:
return DEFAULT_UDP_PROBE;
}
}
/**
* Probe a single UDP port. Because UDP is connectionless there is no handshake
* to observe, so the state is inferred from how the target responds:
*
* - a datagram reply -> `"open"`
* - an ICMP port-unreachable (ECONNREFUSED / ECONNRESET) -> `"closed"`
* - silence until the timeout -> `"open|filtered"` (open or firewalled)
*
* Always resolves (never rejects) with a {@link ScanResult}.
*
* @param {string} host
* @param {number} port
* @param {{timeout?: number}} [options]
* @returns {Promise<ScanResult>}
*/
export function scanUdpPort(host, port, options = {}) {
const { timeout = DEFAULTS.timeout } = options;
return new Promise((resolve) => {
const socket = dgram.createSocket(udpSocketType(host));
let response = "";
let settled = false;
let timer = null;
const finish = (state) => {
if (settled) return;
settled = true;
if (timer) clearTimeout(timer);
try {
socket.close();
} catch {
// The socket may already be closing; nothing to do.
}
const banner = response.trim();
resolve({
port,
protocol: "udp",
open: state === "open",
state,
banner,
service: state === "closed" ? null : fingerprint(port, banner),
});
};
// An ICMP "port unreachable" surfaces as ECONNREFUSED on POSIX and
// ECONNRESET on Windows: that means the port is closed. Treat anything
// else (e.g. a DNS failure) as inconclusive.
const classifyError = (err) => {
const code = err && err.code;
finish(
code === "ECONNREFUSED" || code === "ECONNRESET"
? "closed"
: "open|filtered",
);
};
socket.on("message", (msg) => {
response += msg.toString("utf8");
finish("open");
});
socket.on("error", classifyError);
// No reply within the timeout: UDP cannot distinguish a silent-but-open
// service from a dropped packet, so report the ambiguous state.
timer = setTimeout(() => finish("open|filtered"), timeout);
// Connecting the socket lets the OS deliver ICMP errors back to us, which
// is what makes closed-port detection possible.
socket.connect(port, host, () => {
socket.send(udpProbe(port), (err) => {
if (err) classifyError(err);
});
});
});
}
/**
* Create a rate limiter that spaces acquisitions so no more than
* `ratePerSecond` of them are released each second. A rate of `0` (or any
* falsy value) disables limiting and resolves immediately.
*
* The returned function reserves its slot synchronously before awaiting, so it
* stays correct when shared across the worker pool.
*
* @param {number} ratePerSecond
* @returns {() => Promise<void>}
*/
export function createRateLimiter(ratePerSecond) {
if (!ratePerSecond || ratePerSecond <= 0) {
return () => Promise.resolve();
}
const interval = 1000 / ratePerSecond;
let nextSlot = 0;
return () => {
const now = Date.now();
const slot = Math.max(now, nextSlot);
nextSlot = slot + interval;
const delay = slot - now;
return delay > 0
? new Promise((resolve) => setTimeout(resolve, delay))
: Promise.resolve();
};
}
/**
* Scan many ports concurrently using a bounded worker pool so we never exceed
* the configured number of simultaneous connections. An optional `rate` caps
* how many new probes are started per second to reduce network noise.
*
* @param {string} host
* @param {number[]} ports
* @param {{protocol?: ("tcp"|"udp"), concurrency?: number, timeout?: number,
* rate?: number, grabBanner?: boolean,
* onResult?: (result: ScanResult) => void}} [options]
* @returns {Promise<ScanResult[]>} results sorted ascending by port
*/
export async function scanPorts(host, ports, options = {}) {
const {
concurrency = DEFAULTS.concurrency,
protocol = DEFAULTS.protocol,
rate = DEFAULTS.rate,
onResult,
} = options;
const scanOne = protocol === "udp" ? scanUdpPort : scanPort;
const acquire = createRateLimiter(rate);
const results = [];
let next = 0;
const worker = async () => {
// `next++` runs synchronously before any await, so there is no race
// between workers grabbing the same index.
while (next < ports.length) {
const port = ports[next++];
await acquire();
const result = await scanOne(host, port, options);
results.push(result);
if (onResult) onResult(result);
}
};
const poolSize = Math.max(1, Math.min(concurrency, ports.length));
await Promise.all(Array.from({ length: poolSize }, () => worker()));
results.sort((a, b) => a.port - b.port);
return results;
}
// ---------------------------------------------------------------------------
// Output formatting
// ---------------------------------------------------------------------------
/**
* Resolve a result's textual state, tolerating results that only carry the
* older boolean `open` flag.
* @param {{state?: string, open?: boolean}} result
* @returns {("open"|"closed"|"open|filtered")}
*/
function stateOf(result) {
if (result.state) return result.state;
return result.open ? "open" : "closed";
}
/**
* Render the open ports as an aligned table matching the documented output.
* @param {ScanResult[]} results
* @returns {string}
*/
export function formatTable(results) {
const rows = results
.filter((r) => stateOf(r) !== "closed")
.map((r) => ({
port: `${r.port}/${r.protocol ?? "tcp"}`,
state: stateOf(r),
service: r.service || "unknown",
}));
const portWidth = Math.max(
"PORT".length,
...rows.map((r) => r.port.length),
);
const stateWidth = Math.max(
"STATE".length,
...rows.map((r) => r.state.length),
);
const lines = [
`${"PORT".padEnd(portWidth)} ${"STATE".padEnd(stateWidth)} SERVICE`,
];
for (const row of rows) {
lines.push(
`${row.port.padEnd(portWidth)} ${row.state.padEnd(stateWidth)} ${row.service}`,
);
}
return lines.join("\n");
}
/**
* Build the one-line summary, e.g. `Scan complete: 4 open ports found in 3.2s`.
* @param {ScanResult[]} results
* @param {number} elapsedMs
* @returns {string}
*/
export function formatSummary(results, elapsedMs) {
const open = results.filter((r) => stateOf(r) === "open").length;
const filtered = results.filter(
(r) => stateOf(r) === "open|filtered",
).length;
const seconds = (elapsedMs / 1000).toFixed(1);
const noun = open === 1 ? "open port" : "open ports";
let summary = `Scan complete: ${open} ${noun} found in ${seconds}s`;
if (filtered > 0) {
const filteredNoun = filtered === 1 ? "port" : "ports";
summary += `, ${filtered} open|filtered ${filteredNoun}`;
}
return summary;
}
/**
* Build a structured, JSON-serialisable report of a scan.
* @param {{host: string, protocol?: ("tcp"|"udp"), ports: string,
* results: ScanResult[], elapsedMs: number}} params
* @returns {object}
*/
export function buildJsonReport({
host,
protocol = "tcp",
ports,
results,
elapsedMs,
}) {
const toEntry = (r) => ({
port: r.port,
protocol: r.protocol ?? protocol,
state: stateOf(r),
service: r.service,
banner: r.banner || null,
});
const open = results.filter((r) => stateOf(r) === "open");
const openFiltered = results.filter((r) => stateOf(r) === "open|filtered");
return {
host,
protocol,
ports,
scannedAt: new Date().toISOString(),
durationMs: elapsedMs,
totalScanned: results.length,
openCount: open.length,
open: open.map(toEntry),
openFilteredCount: openFiltered.length,
openFiltered: openFiltered.map(toEntry),
};
}
/**
* HTML-escape a value for safe interpolation into report markup.
*
* Banner and service text originate from the scanned host and are therefore
* untrusted: a hostile target could embed `<script>` or other markup in its
* banner. Escaping every dynamic value prevents that data from being treated
* as HTML when the report is opened in a browser.
*
* @param {unknown} value
* @returns {string}
*/
function escapeHtml(value) {
return String(value ?? "").replace(/[&<>"']/g, (ch) => {
switch (ch) {
case "&":
return "&";
case "<":
return "<";
case ">":
return ">";
case '"':
return """;
default:
return "'";
}
});
}
/** Map a port state to a CSS-class-safe slug (drops the `|` in open|filtered). */
function stateSlug(state) {
if (state === "open") return "open";
if (state === "open|filtered") return "filtered";
return "closed";
}
/**
* Render a scan as a standalone, self-contained HTML report.
*
* Reuses {@link buildJsonReport} for the underlying data so the HTML and JSON
* reports always agree. Every dynamic value is passed through
* {@link escapeHtml} because banners are attacker-controlled and must never be
* rendered as markup.
*
* @param {{host: string, protocol?: ("tcp"|"udp"), ports: string,
* results: ScanResult[], elapsedMs: number}} params
* @returns {string} a complete HTML document
*/
export function buildHtmlReport(params) {
const report = buildJsonReport(params);
const seconds = (report.durationMs / 1000).toFixed(1);
const renderRows = (entries) =>
entries
.map(
(e) => ` <tr>
<td class="port">${e.port}/${escapeHtml(e.protocol)}</td>
<td><span class="state state-${stateSlug(e.state)}">${escapeHtml(e.state)}</span></td>
<td>${escapeHtml(e.service ?? "unknown")}</td>
<td>${e.banner ? `<code>${escapeHtml(e.banner)}</code>` : '<span class="muted">—</span>'}</td>
</tr>`,
)
.join("\n");
const section = (title, entries) =>
entries.length === 0
? ""
: ` <h2>${escapeHtml(title)} <span class="count">${entries.length}</span></h2>
<table>
<thead>
<tr><th>Port</th><th>State</th><th>Service</th><th>Banner</th></tr>
</thead>
<tbody>
${renderRows(entries)}
</tbody>
</table>`;
const body =
report.open.length === 0 && report.openFiltered.length === 0
? ' <p class="empty">No open ports found.</p>'
: [
section("Open ports", report.open),
section("Open | filtered ports", report.openFiltered),
]
.filter(Boolean)
.join("\n");
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>NodeJS-PortScanner report — ${escapeHtml(report.host)}</title>
<style>
:root { color-scheme: light dark; }
* { box-sizing: border-box; }
body {
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
margin: 0; padding: 2rem 1rem; line-height: 1.5;
color: #1f2933; background: #eef1f5;
}
main { max-width: 60rem; margin: 0 auto; }
header h1 { margin: 0 0 .25rem; font-size: 1.6rem; }
.target { margin: 0; color: #52606d; }
.target strong { color: #1f2933; }
.meta { margin: 1.5rem 0; }
.meta dl {
display: grid; gap: 1px; margin: 0;
grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
border: 1px solid #d2d9e0; border-radius: .5rem; overflow: hidden;
}
.meta dl > div { background: #fff; padding: .75rem 1rem; }
.meta dt {
font-size: .7rem; text-transform: uppercase; letter-spacing: .04em;
color: #7b8794; margin-bottom: .15rem;
}
.meta dd { margin: 0; font-size: 1.1rem; font-weight: 600; }
h2 { font-size: 1.15rem; margin: 1.75rem 0 .75rem; }
h2 .count {
display: inline-block; min-width: 1.5rem; padding: 0 .4rem;
font-size: .8rem; text-align: center; border-radius: 1rem;
background: #cbd2d9; color: #1f2933; vertical-align: middle;
}
table {
width: 100%; border-collapse: collapse; background: #fff;
border-radius: .5rem; overflow: hidden; box-shadow: 0 1px 2px rgba(0,0,0,.06);
}
th, td { text-align: left; padding: .6rem .85rem; border-bottom: 1px solid #e4e7eb; vertical-align: top; }
th {
font-size: .7rem; text-transform: uppercase; letter-spacing: .04em;
color: #7b8794; background: #f5f7fa;
}
tr:last-child td { border-bottom: none; }
.port { font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 600; }
.state { display: inline-block; padding: .1rem .5rem; border-radius: 1rem; font-size: .8rem; font-weight: 600; }
.state-open { background: #c1f2d0; color: #0b6b2f; }
.state-filtered { background: #fce4b8; color: #8a5300; }
.state-closed { background: #e4e7eb; color: #52606d; }
code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: .85rem; word-break: break-all; }
.muted { color: #9aa5b1; }
.empty { padding: 2rem; text-align: center; color: #52606d; background: #fff; border-radius: .5rem; }
footer { margin-top: 2rem; font-size: .8rem; color: #7b8794; }
footer a { color: inherit; }
@media (prefers-color-scheme: dark) {
body { color: #e4e7eb; background: #1f2933; }
.target { color: #9aa5b1; }
.target strong { color: #f5f7fa; }
.meta dl { border-color: #3e4c59; }
.meta dl > div { background: #2a3744; }
table { background: #2a3744; box-shadow: none; }
th { background: #323f4b; color: #9aa5b1; }
th, td { border-color: #3e4c59; }
.empty { background: #2a3744; }
h2 .count { background: #3e4c59; color: #e4e7eb; }
}
</style>
</head>
<body>
<main>
<header>
<h1>🔍 NodeJS-PortScanner report</h1>
<p class="target"><strong>${escapeHtml(report.host)}</strong> · ${escapeHtml(report.protocol.toUpperCase())} · ports ${escapeHtml(report.ports)}</p>
</header>
<section class="meta">
<dl>
<div><dt>Scanned at</dt><dd>${escapeHtml(report.scannedAt)}</dd></div>
<div><dt>Duration</dt><dd>${seconds}s</dd></div>
<div><dt>Ports scanned</dt><dd>${report.totalScanned}</dd></div>
<div><dt>Open</dt><dd>${report.openCount}</dd></div>
<div><dt>Open | filtered</dt><dd>${report.openFilteredCount}</dd></div>
</dl>
</section>
<section>
${body}
</section>
<footer>
<p>Generated by <a href="https://github.com/zuedev/nodejs-portscanner">NodeJS-PortScanner</a>. Only scan systems you own or have explicit written permission to test.</p>
</footer>
</main>
</body>
</html>
`;
}
/**
* Choose a report format from an output filename's extension. Files ending in
* `.html` or `.htm` produce an HTML report; everything else is JSON.
*
* @param {string} outputPath
* @returns {("html"|"json")}
*/
export function reportFormatFor(outputPath) {
return /\.html?$/i.test(outputPath) ? "html" : "json";
}
/** The CLI help / usage text. */
export function helpText() {
return `NodeJS-PortScanner — a fast, lightweight TCP port scanner.
Usage:
node scanner.js --host <target> [options]
Options:
-h, --host <host> Target hostname or IP address (required)
-p, --ports <spec> Port range or list, e.g. 1-1024 or 80,443,8080
(default: ${DEFAULTS.ports})
-P, --protocol <tcp|udp> Transport protocol to scan (default: ${DEFAULTS.protocol})
-c, --concurrency <n> Max simultaneous connections (default: ${DEFAULTS.concurrency})
-t, --timeout <ms> Connection timeout in milliseconds (default: ${DEFAULTS.timeout})
-r, --rate <n> Max new probes started per second (default: unlimited)
-o, --output <file> Export results to a file; a .html (or .htm)
extension writes an HTML report, otherwise JSON
--help Show this help menu
Examples:
node scanner.js --host 192.168.1.1 --ports 1-1024
node scanner.js --host example.com --ports 22,80,443,8080
node scanner.js --host 10.0.0.5 --ports 1-65535 -c 200 -t 1500
node scanner.js --host 192.168.1.1 --ports 1-1024 --output results.json
node scanner.js --host 192.168.1.1 --ports 1-1024 --output report.html
node scanner.js --host 192.168.1.1 --protocol udp --ports 53,123,161
node scanner.js --host 192.168.1.1 --ports 1-1024 --rate 50
UDP ports that never reply are reported as "open|filtered", because UDP cannot
distinguish a silent-but-open service from a firewalled one.
Only scan systems you own or have explicit written permission to test.`;
}
// ---------------------------------------------------------------------------
// CLI entry point
// ---------------------------------------------------------------------------
/**
* Run the command-line interface. Output is injected so it can be captured in
* tests, and the function resolves to a process exit code rather than calling
* `process.exit` directly.
*
* @param {string[]} argv - arguments after `node scanner.js`
* @param {{log?: (msg: string) => void, error?: (msg: string) => void}} [io]
* @returns {Promise<number>} exit code
*/
export async function runCli(argv, io = {}) {
const log = io.log ?? console.log;
const errorLog = io.error ?? console.error;
let options;
try {
options = parseArgs(argv);
} catch (err) {
errorLog(`Error: ${err.message}`);
errorLog("Run with --help for usage.");
return 1;
}
if (options.help) {
log(helpText());
return 0;
}
if (!options.host) {