Skip to content

Instantly share code, notes, and snippets.

@dillera
Created June 12, 2024 02:00
Show Gist options
  • Save dillera/ec29b3c6133be5c78fdb42163133cd1a to your computer and use it in GitHub Desktop.
Save dillera/ec29b3c6133be5c78fdb42163133cd1a to your computer and use it in GitHub Desktop.
BASH command to save a node script that will listen on port 6677 for IP address from a SubText BBS
cat <<'EOF' > /usr/local/bin/udp_firewall.js
const dgram = require('dgram');
const { exec } = require('child_process');
const PORT = 6677;
const server = dgram.createSocket('udp4');
server.on('listening', () => {
const address = server.address();
console.log(`UDP server listening on ${address.address}:${address.port}`);
});
server.on('message', (msg, rinfo) => {
const ipAddress = msg.toString().trim();
console.log(`Received IP address: ${ipAddress}`);
// Ban the IP address using iptables
const commands = [
`sudo iptables -A INPUT -s ${ipAddress} -j DROP`,
`sudo iptables -A OUTPUT -d ${ipAddress} -j DROP`,
`sudo iptables -A FORWARD -s ${ipAddress} -j DROP`,
`sudo iptables -A FORWARD -d ${ipAddress} -j DROP`
];
commands.forEach((command) => {
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${command}`);
console.error(`Error: ${error.message}`);
return;
}
console.log(`Command executed: ${command}`);
});
});
// Save the new iptables rules using netfilter-persistent
exec('sudo netfilter-persistent save', (error, stdout, stderr) => {
if (error) {
console.error('Error saving iptables rules using netfilter-persistent');
console.error(`Error: ${error.message}`);
return;
}
console.log('New iptables rules saved using netfilter-persistent');
});
});
server.on('error', (err) => {
console.error(`UDP server error: ${err.message}`);
server.close();
});
server.bind(PORT);
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment