2024-07-24 16:40:40 +02:00
|
|
|
import { createServer } from 'node:http';
|
2024-07-26 16:08:05 +02:00
|
|
|
import { setTimeout } from 'node:timers';
|
2024-07-24 16:40:40 +02:00
|
|
|
|
2025-09-27 10:38:03 +02:00
|
|
|
const msg = 'Relax, take it easy! For there is nothing that we can do.';
|
2026-01-17 08:45:23 +01:00
|
|
|
const minDelayMs = 3000;
|
|
|
|
|
const maxDelayMs = 6000;
|
|
|
|
|
const delayDiff = maxDelayMs - minDelayMs;
|
|
|
|
|
const randomDelay = () => Math.floor(Math.random() * delayDiff + minDelayMs);
|
|
|
|
|
const ipNextReportDateMap = new Map();
|
2024-07-25 23:04:36 +02:00
|
|
|
|
2024-07-24 16:40:40 +02:00
|
|
|
const server = createServer((req, res) => {
|
2026-01-15 09:34:17 +01:00
|
|
|
const connOpenDate = new Date();
|
|
|
|
|
const endpoint = `${req.method} ${req.url}`;
|
2024-07-24 19:19:39 +02:00
|
|
|
|
2026-01-15 09:41:02 +01:00
|
|
|
if (endpoint === 'GET /health') {
|
|
|
|
|
res.statusCode = 200;
|
|
|
|
|
return res.end('OK\n');
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 08:45:23 +01:00
|
|
|
const ip = req.headers['x-forwarded-for'];
|
|
|
|
|
const userAgent = req.headers['user-agent'];
|
|
|
|
|
const host = req.headers['x-forwarded-host'];
|
|
|
|
|
|
2026-01-15 09:34:17 +01:00
|
|
|
console.log(
|
2026-01-17 08:45:23 +01:00
|
|
|
`${ip} (${userAgent}) targeted ${host} on ${endpoint}`
|
2026-01-15 09:34:17 +01:00
|
|
|
);
|
2024-07-24 16:40:40 +02:00
|
|
|
|
2025-11-28 13:22:12 +01:00
|
|
|
let charIdx = 0;
|
|
|
|
|
|
2024-07-26 16:08:05 +02:00
|
|
|
const hang = () => {
|
|
|
|
|
if (res.closed) return;
|
2024-07-30 17:37:51 +02:00
|
|
|
else if (charIdx === msg.length) res.end('\n');
|
|
|
|
|
else res.write(msg[charIdx++]);
|
2024-07-26 16:08:05 +02:00
|
|
|
|
|
|
|
|
setTimeout(hang, randomDelay());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
hang();
|
2024-07-24 16:40:40 +02:00
|
|
|
|
2026-01-17 08:45:23 +01:00
|
|
|
res.once('close', async () => {
|
2026-01-15 09:34:17 +01:00
|
|
|
const connCloseDate = new Date();
|
2026-01-17 08:45:23 +01:00
|
|
|
const diffText = new Date(connCloseDate - connOpenDate)
|
|
|
|
|
.toISOString()
|
|
|
|
|
.substring(14, 19);
|
2024-07-30 17:37:51 +02:00
|
|
|
|
2026-01-17 08:45:23 +01:00
|
|
|
const nextIpReportDate = ipNextReportDateMap.get(ip) || connCloseDate;
|
2026-01-15 09:34:17 +01:00
|
|
|
const hangResult =
|
|
|
|
|
charIdx === msg.length ? 'received the message' : 'aborted connection';
|
2024-07-24 19:15:07 +02:00
|
|
|
|
2026-01-17 08:45:23 +01:00
|
|
|
console.log(`${ip} ${hangResult} after ${diffText}`);
|
|
|
|
|
|
|
|
|
|
if (connCloseDate < nextIpReportDate) return;
|
|
|
|
|
|
|
|
|
|
const queryParams = new URLSearchParams();
|
|
|
|
|
|
|
|
|
|
queryParams.append('ip', ip);
|
2026-01-17 10:15:24 +01:00
|
|
|
queryParams.append('categories', '15,21');
|
2026-01-17 08:45:23 +01:00
|
|
|
queryParams.append('timestamp', connOpenDate.toISOString());
|
|
|
|
|
queryParams.append(
|
|
|
|
|
'comment',
|
|
|
|
|
`Vulnerability scanner detected!\nUser-Agent: ${userAgent}\nEndpoint: ${endpoint}`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const abuseIpDbRes = await fetch(
|
|
|
|
|
`https://api.abuseipdb.com/api/v2/report?${queryParams.toString()}`,
|
|
|
|
|
{
|
2026-01-17 09:23:15 +01:00
|
|
|
method: 'POST',
|
2026-01-17 08:45:23 +01:00
|
|
|
headers: {
|
|
|
|
|
'Accept': 'application/json',
|
|
|
|
|
'Key': process.env.ABUSEIPDB_API_KEY,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (abuseIpDbRes.ok) {
|
|
|
|
|
const reportDate = new Date();
|
|
|
|
|
|
|
|
|
|
console.log(`${ip} has been reported!`);
|
|
|
|
|
ipNextReportDateMap.set(
|
|
|
|
|
ip,
|
|
|
|
|
new Date(
|
|
|
|
|
reportDate.getFullYear(),
|
|
|
|
|
reportDate.getMonth(),
|
|
|
|
|
reportDate.getDate() + 1,
|
|
|
|
|
reportDate.getHours(),
|
|
|
|
|
reportDate.getMinutes(),
|
|
|
|
|
reportDate.getSeconds()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
console.error(
|
|
|
|
|
`Failed to report ${ip}: ${abuseIpDbRes.status} ${abuseIpDbRes.statusText}`
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-01-15 09:34:17 +01:00
|
|
|
});
|
2024-07-24 16:40:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.listen(3000);
|
2026-01-15 09:34:17 +01:00
|
|
|
|
2026-01-17 08:45:23 +01:00
|
|
|
process.on('SIGTERM', () => server.close());
|