Skip to content

Instantly share code, notes, and snippets.

@woohooyeah
Created September 10, 2017 16:39
Show Gist options
  • Select an option

  • Save woohooyeah/28f2f400cacc4c53339c8e233d258b65 to your computer and use it in GitHub Desktop.

Select an option

Save woohooyeah/28f2f400cacc4c53339c8e233d258b65 to your computer and use it in GitHub Desktop.
## insert-spammers-into-iptables.sh v0.1
## Dorian Harmans <[email protected]>
##
#!/bin/bash
if [ -e /root/spammers.txt ]; then
/sbin/iptables-save > /root/iptables.rules && cp /root/iptables.rules /root/iptables.rules-old
else
echo -e "Failed!\nReason: /root/spammers.txt not found"
exit 1
fi
if [ -e /root/iptables.rules ]; then
grep -E "(-A INPUT -s | -j DROP)" /root/iptables.rules | awk '{ print $4 }' > /root/ip-blocklist-old.txt
grep -vE "(-A INPUT -s | -j DROP)" /root/iptables.rules > /root/iptables.rules-clean
else
echo -e "Failed!\nReason: /root/iptables.rules not found"
exit 1
fi
if [ -e /root/iptables.rules-clean ]; then
/sbin/iptables-restore < /root/iptables.rules-clean
else
echo -e "Failed!\nReason: /root/iptables.rules-clean not found"
exit 1
fi
if [ -e /root/spammers.txt ]; then
for line in $(cat /root/spammers.txt); do /sbin/iptables -A INPUT -s $line/32 -j DROP; done
else
echo -e "Failed!\nReason: /root/spammers.txt not found"
exit 1
fi
if [ -e /root/iptables.rules ]; then
/sbin/iptables-save > /etc/iptables.rules && /sbin/iptables-save > /root/iptables.rules-new
grep -E "(-A INPUT -s | -j DROP)" /root/iptables.rules-new | awk '{ print $4 }' > /root/ip-blocklist-new.txt
else
echo -e "Failed!\nReason: /root/iptables.rules-new not found"
exit 1
fi
if [ -e /root/ip-blocklist-old.txt ] && [ -e /root/ip-blocklist-new.txt ]; then
diff -Naur /root/ip-blocklist-old.txt /root/ip-blocklist-new.txt
else
echo -e "Failed!\nReason: /root/ip-blocklist-{old,new} not found"
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment