To enhance the port scanner by providing information about the common uses of well-known ports, you can create a lookup table that maps port numbers to their typical services or use cases. Here is how you can integrate this feature into your existing script:
First, create a portable JSON-like object that includes some common ports and their associated services:
const portDescriptions = {
21: 'FTP (File Transfer Protocol)',
22: 'SSH (Secure Shell)',
23: 'Telnet (Teletype Network)',
25: 'SMTP (Simple Mail Transfer Protocol)',
53: 'DNS (Domain Name System)',
80: 'HTTP (Hypertext Transfer Protocol)',
110: 'POP3 (Post Office Protocol v3)',
143: 'IMAP (Internet Message Access Protocol)',
443: 'HTTPS (HTTP Secure)',
3389:'RDP (Remote Desktop Protocol)',
// Add more ports and descriptions as needed
};
Then, update the script to use this object when reporting open ports:
const net = require('net');
const fs = require('fs');
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
const readline = require('readline');
const chalk = require('chalk');
const ProgressBar = require('progress');
const portDescriptions = {
21: 'FTP (File Transfer Protocol)',
22: 'SSH (Secure Shell)',
23: 'Telnet (Teletype Network)',
25: 'SMTP (Simple Mail Transfer Protocol)',
53: 'DNS (Domain Name System)',
80: 'HTTP (Hypertext Transfer Protocol)',
110: 'POP3 (Post Office Protocol v3)',
143: 'IMAP (Internet Message Access Protocol)',
443: 'HTTPS (HTTP Secure)',
3389:'RDP (Remote Desktop Protocol)',
// You can expand this list with additional ports of interest
};
const maxPorts = 65535;
const defaultConcurrency = 200;
if (isMainThread) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the IP address to scan: ', (inputIp) => {
const host = inputIp.trim();
rl.close();
if (!net.isIP(host)) {
console.log(chalk.red('Invalid IP address.'));
return;
}
const concurrency = defaultConcurrency;
let startPort = 1;
let workerCount = 0;
const totalPorts = maxPorts;
let completedPorts = 0;
const bar = new ProgressBar(':bar :percent :etas', {
total: totalPorts,
width: 40,
complete: chalk.green('=')
});
for (let i = 0; i < concurrency; i++) {
const endPort = Math.min(startPort + Math.floor(maxPorts / concurrency) - 1, maxPorts);
const worker = new Worker(__filename, { workerData: { host, startPort, endPort } });
workerCount++;
worker.on('message', (message) => {
if (message.open) {
const portDescription = portDescriptions[message.port] || 'Unknown Service';
const logMessage = `Port ${message.port} is open (${portDescription})`;
fs.appendFile('open_ports.txt', logMessage + '\n', (err) => {
if (err) throw err;
console.log(chalk.green(logMessage));
});
}
});
worker.on('exit', () => {
workerCount--;
if (workerCount === 0) {
console.log(chalk.blue('Scan completed.'));
}
});
startPort = endPort + 1;
}
const updateProgress = () => {
completedPorts++;
bar.tick();
if (completedPorts < totalPorts) {
setImmediate(updateProgress);
}
}
updateProgress();
});
} else {
const { host, startPort, endPort } = workerData;
const scanPort = (port) => {
return new Promise((resolve) => {
const socket = new net.Socket();
socket.setTimeout(150);
socket.on('connect', () => {
parentPort.postMessage({ open: true, port });
socket.destroy();
resolve();
});
socket.on('timeout', () => {
socket.destroy();
resolve();
});
socket.on('error', () => {
socket.destroy();
resolve();
});
socket.connect(port, host);
});
};
const scanRange = async () => {
for (let port = startPort; port <= endPort; port++) {
await scanPort(port);
}
parentPort.close();
};
scanRange();
}
Explanation:
- Port Descriptions: A
portDescriptions object is used to map known port numbers to their common uses. You can expand this list with more ports and descriptions as needed.
- Whenever an open port is found, its description (if available) is included in the console output, enhancing the informational value of the scan results.
Running the Script:
-
Save the revised script to a file, such as port_scanner_with_info.js.
-
Execute the script as before and enter the IP address when prompted:
node port_scanner_with_info.js
This version of the script provides insights into what each open port is commonly used for, which can be very helpful during network assessments.