Fast kernel-level IP blocking via IPSet for KeyHelp servers, including a live status dashboard widget for blocked IPs, traffic volume, and intercepted packets.
Note
If you do not want the status widget in the KeyHelp dashboard, complete Step 1 and jump straight to Step 4 (Fail2Ban & IPSet).
Important
Important Notes from the Scripts
- Requirements:
- OS & Memory: Ubuntu or Debian with
systemdand kernelipset(hash:net) support, at least 1 GB RAM recommended. - Tools & Access: Root privileges, outbound internet access, and the
ipset,iptables, andwgetpackages installed. - KeyHelp: Optional – only needed for KeyHelp-specific integration and status files (see screenshot above).
- OS & Memory: Ubuntu or Debian with
- Blocklists & Filtering:
loadlist.shfetchesabuseipdb-s100-3d.ipv4(borestad) andfirehol_level1.netset(FireHOL) and filters out loopback (127.*) and private (172.16.0.0/12) ranges. - Kernel IPSet & 7-Day Timeout:
loadlist.shcreatesf2b-iplistblock(hash:net) withmaxelem 1000000andtimeout 604800(7 days) directly in the kernel without needing Fail2Ban. If anf2b-iplistblockset without a timeout already exists, runipset destroy f2b-iplistblockonce beforehand. - Firewall & DHCP: The script automatically adds the
INPUTDROPrule forf2b-iplistblockand allows DHCP traffic (UDP67/68) so lease renewals are not blocked. - Persistent Stats:
live_status.shstores cumulative and daily counters in/root/banip/(.persistent_total,.daily_bytes_start,f2b_status.txt) and copiesf2b_status.txtto/home/keyhelp/www/keyhelp/f2b_status.txtwithchmod 644.
mkdir -p /root/banip
cd /root/banip
wget -O live_status.sh https://github.com/NicoMSH-plugins/keyhelp-kernel-ipset-firewall/releases/download/1.3.0/live_status.sh
wget -O loadlist.sh https://github.com/NicoMSH-plugins/keyhelp-kernel-ipset-firewall/releases/download/1.3.0/loadlist.sh
chmod +x /root/banip/live_status.sh /root/banip/loadlist.shCreate the service file (/etc/systemd/system/f2b-status.service):
nano /etc/systemd/system/f2b-status.servicePaste the following content and save the file (Ctrl+O, Enter, Ctrl+X):
[Unit]
Description=Updates the Fail2Ban IPSet status for monitoring
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/bin/bash /root/banip/live_status.sh
[Install]
WantedBy=multi-user.targetCreate the timer file (/etc/systemd/system/f2b-status.timer):
nano /etc/systemd/system/f2b-status.timerPaste the following content and save the file (Ctrl+O, Enter, Ctrl+X):
[Unit]
Description=Runs the f2b-status script every 1 minute
[Timer]
OnBootSec=30s
OnUnitActiveSec=1min
Unit=f2b-status.service
[Install]
WantedBy=timers.targetReload systemd and enable the timer:
systemctl daemon-reload
systemctl enable --now f2b-status.timer
systemctl start f2b-status.service- Log in to KeyHelp as an administrator.
- Navigate to Configuration → White Label and click the pencil icon next to All Languages.
- Locate the Additional JavaScript field, paste the following code, and save:
Show / Expand JavaScript for the Dashboard Box
// ====================================================
// 🛡️ ABANDONED/LIVE ABUSEIPDB & KERNEL IPSET STATUS BOX
// ====================================================
if ('admin_dashboard' === (typeof getUrlQueryParameterValue === 'function' ? getUrlQueryParameterValue('page') : new URLSearchParams(window.location.search).get('page'))) {
(function startAbuseBox() {
if (!window.jQuery) {
setTimeout(startAbuseBox, 50);
return;
}
window.jQuery(function($) {
if ($('#abuseipdb-status-card').length) return;
function isLightThemeActive() {
const targets = [document.body, document.documentElement];
for (const el of targets) {
if (!el) continue;
const bg = window.getComputedStyle(el).backgroundColor;
const match = bg && bg.match(/rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/i);
if (match) {
const r = Number(match[1]), g = Number(match[2]), b = Number(match[3]);
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
if (luminance > 0.62) return true;
if (luminance <= 0.62 && (r + g + b) > 0) return false;
}
}
return false;
}
let $portTable = $('table').filter(function() {
let htmlText = $(this).text();
return htmlText.includes('FTP') && htmlText.includes('SSH');
}).first();
if (!$portTable.length) {
$portTable = $('table').filter(function() {
return $(this).find('.fa-network-wired, .fa-server, .fa-desktop').length > 0;
}).first();
}
let $portCard = $portTable.closest('.card, .box');
let htmlLang = $('html').attr('lang') || '';
let isGerman = htmlLang.toLowerCase().startsWith('de');
let txtLoading = isGerman ? 'Lade Live-Daten...' : 'Loading live data...';
let $abuseBox = $(`
<div class="card mt-5 mb-5" id="abuseipdb-status-card" style="margin-top: 1.5rem !important; margin-bottom: 1.5rem !important;">
<header class="card-header">
<span class="card-header-icon">
<i class="fas fa-shield-alt" style="color: #3273dc;"></i>
</span>
<p class="card-header-title">AbuseIPDB & Kernel-Firewall (IPSet)</p>
</header>
<div class="card-content">
<div id="abuseipdb-status-content" class="columns is-multiline is-mobile">
<div class="column is-12 has-text-grey">${txtLoading}</div>
</div>
</div>
</div>
`);
if ($portCard.length) {
$portCard.before($abuseBox);
} else {
let $fallbackCard = $('.app-content .card, #content .card').last();
if ($fallbackCard.length) {
$fallbackCard.after($abuseBox);
}
}
let txt = isGerman ? {
ips: "Gesperrte IPs",
total: "Geblockter Datenverkehr Gesamt",
daily: "Geblockter Datenverkehr Heute",
packets: "Abgefangene / Blockierte Pakete",
update: "Letztes Update",
parseErr: "Status-Datei konnte nicht geladen werden."
} : {
ips: "Blocked IPs",
total: "Blocked Traffic Total",
daily: "Blocked Traffic Today",
packets: "Intercepted / Blocked Packets",
update: "Last Update",
parseErr: "Could not load status file."
};
function updateAbuseStatus() {
$.get('f2b_status.txt?_=' + Date.now(), function(rawText) {
let parts = rawText.split('|');
let ips = '0';
let total = '0 Bytes';
let daily = '0 Bytes';
let packets = '0';
let upd = '-';
try {
if (parts.length >= 4) {
ips = parts[0].replace(/[^0-9]/g, '');
total = parts[1].replace('Gesamt:', '').trim();
let dailyPart = parts[2];
if (dailyPart && dailyPart.includes(':')) {
daily = dailyPart.split(':')[1].replace('(Pakete', '').trim();
}
let packetMatch = rawText.match(/Pakete:\s*([0-9]+)/);
if (packetMatch) {
packets = packetMatch[1];
}
let updMatch = rawText.match(/Upd:\s*(.*)/);
if (updMatch) {
upd = updMatch[1].trim();
}
} else {
ips = rawText;
}
} catch (e) {
console.error("Fehler beim Parsen des Status:", e);
}
const light = isLightThemeActive();
const cBlue = light ? '#1d4ed8' : '#3273dc';
const cGreen = light ? '#15803d' : '#48c774';
const cYellow = light ? '#b45309' : '#ffdd57';
const cRed = light ? '#dc2626' : '#ff3860';
const bgAlpha = light ? '0.08' : '0.05';
let html = `
<div class="column is-12-mobile is-4-tablet">
<div class="p-5" style="background: rgba(50, 115, 220, ${bgAlpha}); border-radius: 6px; border-left: 4px solid ${cBlue};">
<div class="is-size-7 has-text-grey text-uppercase"><i class="fas fa-ban mr-1"></i> ${txt.ips}</div>
<div class="is-size-4 has-text-weight-bold mt-2" style="color: ${cBlue}; padding-left: 15px !important;">${ips}</div>
</div>
</div>
<div class="column is-12-mobile is-4-tablet">
<div class="p-5" style="background: rgba(72, 199, 116, ${bgAlpha}); border-radius: 6px; border-left: 4px solid ${cGreen};">
<div class="is-size-7 has-text-grey text-uppercase"><i class="fas fa-database mr-1"></i> ${txt.total}</div>
<div class="is-size-4 has-text-weight-bold mt-2" style="color: ${cGreen}; padding-left: 15px !important;">${total}</div>
</div>
</div>
<div class="column is-12-mobile is-4-tablet">
<div class="p-5" style="background: rgba(217, 119, 6, ${bgAlpha}); border-radius: 6px; border-left: 4px solid ${cYellow};">
<div class="is-size-7 has-text-grey text-uppercase"><i class="fas fa-chart-line mr-1"></i> ${txt.daily}</div>
<div class="is-size-4 has-text-weight-bold mt-2" style="color: ${cYellow}; padding-left: 15px !important;">${daily}</div>
</div>
</div>
<div class="column is-12">
<div class="p-5" style="background: rgba(255, 56, 96, ${bgAlpha}); border-radius: 6px; border-left: 4px solid ${cRed};">
<div class="is-size-7 has-text-grey text-uppercase"><i class="fas fa-box mr-1"></i> ${txt.packets}</div>
<div class="is-size-4 has-text-weight-bold mt-2" style="color: ${cRed}; padding-left: 15px !important;">${packets}</div>
</div>
</div>
<div class="column is-12 has-text-right pt-0 pb-0">
<span class="is-size-7 has-text-grey"><i class="far fa-clock mr-1"></i> ${txt.update}: ${upd}</span>
</div>
`;
$('#abuseipdb-status-content').html(html);
}).fail(function() {
$('#abuseipdb-status-content').html(`<div class="column is-12 has-text-danger">${txt.parseErr}</div>`);
});
}
updateAbuseStatus();
setInterval(updateAbuseStatus, 60000);
});
})();
}Since /root/banip/loadlist.sh creates and manages the kernel IPSet f2b-iplistblock (hash:net, maxelem 1000000, timeout 604800 = 7 days) as well as the iptables rules directly, no Fail2Ban filter or jail is required. To ensure the blocklist is loaded automatically after a server reboot, create a systemd oneshot service.
Create the service file:
nano /etc/systemd/system/loadlist-boot.servicePaste the following content and save the file (Ctrl+O, Enter, Ctrl+X):
[Unit]
Description=Load IP Blocklist at Boot
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/bash /root/banip/loadlist.sh
RemainAfterExit=true
[Install]
WantedBy=multi-user.targetReload systemd, enable the service at boot, and verify its status:
systemctl daemon-reload
systemctl enable loadlist-boot.service
systemctl start loadlist-boot.service
systemctl status loadlist-boot.service --no-pagerTip
Optional check to verify that the kernel IPSet is active with maxelem 1000000 and timeout 604800:
ipset list f2b-iplistblock | head -n 10- Log in to KeyHelp as an administrator, go to Configuration → Scheduled Tasks, and click Add Task.
- Fill in the fields as follows and click Save:
- Enabled: Yes
- Description:
IP Blocklist - Task Type: Execute command
- Command:
/root/banip/loadlist.sh - Schedule: Daily (choose your preferred time)
- Owner:
-- System (root) --
Either trigger the newly created scheduled task in KeyHelp by clicking the blue play icon on the right, or run it directly in your terminal:
bash /root/banip/loadlist.shEverything will now run automatically. Once the blocklists have been fetched and imported, the blocked IPs, packet counts, and traffic stats will appear on your KeyHelp dashboard. Enjoy!
Show / Expand Changelog History
- Removed Fail2Ban Requirement: Replaced the Fail2Ban filter and jail setup (
[iplistblock]) with direct kernel IPSet management vialoadlist.sh(hash:net,maxelem 1000000,timeout 604800for automatic 7-day expiration). - Added Boot Service (
loadlist-boot.service): Added documentation and setup instructions for thesystemdoneshot service to automatically load the IP blocklist into the kernel on server reboot.
- Light & Dark Mode Support: Added automatic theme detection based on background luminance so the dashboard widget renders with clear contrast in both Light and Dark mode.
- Updated Traffic Labels: Clarified traffic labels to explicitly reflect blocked traffic (
Blocked Traffic Total/Blocked Traffic Today).