🔧 AI Nachrichten Debian is Voting on Whether to Allow AI-Assisted Contributions(23.08.2026 um 09:34 Uhr)
🔧 AI Nachrichten The Linux Kernel Is Approaching 2,000 CVEs Per Release(29.08.2026 um 20:00 Uhr)
⚠️ Malware / Trojaner / VirenCitrix Adds a Linux-Powered Escape Hatch For Compromised Windows PCs(30.08.2026 um 17:34 Uhr)
🔧 ProgrammierungZaku 26.0 beta - Local-first, open-source API client(11.09.2026 um 22:38 Uhr)
🔧 Programmierung[$] Stabilizing Rust's never type(08.09.2026 um 15:34 Uhr)
🕵️ SicherheitslückenForgejo 16.0.4 and 15.0.8 address critical security vulnerability(10.09.2026 um 22:05 Uhr)
🔧 AI Nachrichten Debian is Voting on Whether to Allow AI-Assisted Contributions(23.08.2026 um 09:34 Uhr)
🔧 AI Nachrichten The Linux Kernel Is Approaching 2,000 CVEs Per Release(29.08.2026 um 20:00 Uhr)
⚠️ Malware / Trojaner / VirenCitrix Adds a Linux-Powered Escape Hatch For Compromised Windows PCs(30.08.2026 um 17:34 Uhr)
🔧 ProgrammierungZaku 26.0 beta - Local-first, open-source API client(11.09.2026 um 22:38 Uhr)
🔧 Programmierung[$] Stabilizing Rust's never type(08.09.2026 um 15:34 Uhr)
🕵️ SicherheitslückenForgejo 16.0.4 and 15.0.8 address critical security vulnerability(10.09.2026 um 22:05 Uhr)

🔧 Programmierung 🕛 vor 3 Monaten 4 Min Lesezeit
0

How to Build a Secure Serverless Port Scanner in Node.js (and Prevent SSRF)

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Every network engineer and systems developer needs to verify connection ports. Whether you're debugging why a remote database connection is failing, checking if an SSH daemon is running, or auditing active firewall rules, programmatically checking TCP ports is a core developer task.



However, writing a port scanner in Node.js comes with a massive, critical security risk: Server-Side Request Forgery (SSRF).



If you allow users to pass a host parameter directly into a network socket connection, an attacker can input localhost or local IPs (like 127.0.0.1 or 192.168.1.1) to map and scan your own server's internal networks, databases, and microservices.



Here is how to build a high-performance TCP port scanner in Node.js that runs in a serverless environment and is fully hardened against SSRF attacks.









1. The Core Port Scanner logic (TCP & Banner Grabbing)



We use Node's built-in net module to attempt quick TCP socket connections.



If a connection succeeds, we wait up to 250ms to read any greeting bytes sent by the server. This allows our scanner to perform banner grabbing—allowing us to extract software versions (like SSH-2.0-OpenSSH_8.9p1) directly.




CODE
import net from 'net';

function checkPort(ipAddress, port, timeout = 1000) {
return new Promise((resolve) => {
const socket = new net.Socket();
let status = 'closed';
let banner = null;
let completed = false;

socket.setTimeout(timeout);

socket.once('connect', () => {
status = 'open';
// Wait briefly for greeting banner (e.g. SSH/SMTP welcome banners)
socket.setTimeout(250);
});

socket.on('data', (data) => {
banner = data.toString('utf8').trim().substring(0, 128);
cleanup();
});

socket.on('timeout', () => {
status = status === 'open' ? 'open' : 'filtered';
cleanup();
});

socket.on('error', () => {
status = 'closed';
cleanup();
});

function cleanup() {
if (completed) return;
completed = true;
try { socket.destroy(); } catch (e) {}
resolve({ port, status, banner });
}

try {
socket.connect(port, ipAddress);
} catch (err) {
status = 'closed';
cleanup();
}
});
}









2. Preventing SSRF: Hardening the DNS Lookup



Before you let Node connect to any hostname (like google.com or an IP address), you must:




  1. Resolve the hostname to an IP address.

  2. Check if the resolved IP belongs to a private, loopback, or link-local subnet.

  3. Terminate the request if it points to a private subnet.



Here is the subnet validation check:

import dns from 'dns';




CODE

// Check if IPv4 matches loopback, private subnets (Class A/B/C), or link-local
function isPrivateIPv4(ip) {
const parts = ip.split('.').map(Number);
if (parts.length !== 4 || parts.some(isNaN)) return true;

return (
parts[0] === 127 || // Loopback (127.0.0.0/8)
parts[0] === 10 || // Private Class A (10.0.0.0/8)
(parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) || // Private Class B
(parts[0] === 192 && parts[1] === 168) || // Private Class C (192.168.0.0/16)
(parts[0] === 169 && parts[1] === 254) || // Link-local (169.254.0.0/16)
parts[0] === 0 // Local broadcast
);
}

// Check if IPv6 points to loopback, link-local, or unique local ranges
function isPrivateIPv6(ip) {
const normalized = ip.toLowerCase().trim();
return (
normalized === '::1' ||
normalized === '::' ||
normalized.startsWith('fe80:') || // Link-local
normalized.startsWith('fc00:') || // Unique local
normalized.startsWith('fd00:')
);
}






Now, integrate this check into your routing logic:




CODE

async function secureScanHandler(host, portsToScan = [80, 443]) {
// 1. Resolve host to target IP
let targetIp;
try {
const lookup = await dns.promises.lookup(host);
targetIp = lookup.address;
} catch (err) {
throw new Error(`Could not resolve host: ${err.message}`);
}

// 2. Enforce SSRF blocklist
const isIPv6 = targetIp.includes(':');
const isPrivate = isIPv6 ? isPrivateIPv6(targetIp) : isPrivateIPv4(targetIp);

if (isPrivate) {
throw new Error('Access denied: Scanning internal network targets is prohibited.');
}

// 3. Scan ports in parallel
const results = await Promise.all(
portsToScan.map(port => checkPort(targetIp, port))
);

return { host, ip: targetIp, results };
}









3. Best Practices for Production Scanners





  1. Scan Limits: Serverless runtimes (like Vercel or AWS Lambda) have strict execution timeouts (typically 10-15s on free plans). Restrict developers to scanning a maximum of 20 ports concurrently to ensure your handler finishes quickly.


  2. Timeout Boundaries: Keep the connection timeout boundary low (e.g., 1000ms). Firewalls that drop packages silently (resulting in a filtered status) will cause sockets to hang until they hit this threshold.






Try it Live



If you don't want to build, host, and maintain your own scraping servers and IP subnets lists, I have deployed a fully hardened version of this tool.



You can try out, test code snippets, and call this service with a free sandbox tier (up to 100 queries a month) directly at the Port Scanner & Network Diagnostics API on RapidAPI.



How are you managing network diagnostic validations in your deployment flows? Let me know in the comments below!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Debian is Voting on Whether to Allow AI-Assisted Contributions
1 Quelle
The Linux Kernel Is Approaching 2,000 CVEs Per Release
1 Quelle
Citrix Adds a Linux-Powered Escape Hatch For Compromised Windows PCs
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to Build a Secure Serverless Port Scanner in Node.js (and Prevent SSRF)

Thematisch verwandte Begriffe: Build, Secure, Serverless, Port · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...