Created
March 20, 2023 13:28
-
-
Save noname1007/09972d24c4bf9d3f5ffa1e9bbdb62e40 to your computer and use it in GitHub Desktop.
Blocks Shodan, Zoomeye, Censys IPs From Scanning Your Servers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ ! -x "/usr/bin/whois" ]; then | |
echo "whois must be installed at /usr/bin/whois" | |
exit 2 | |
elif [ $# -ne 1 ]; then | |
echo "Usage $0 [ASN #]" | |
exit 1 | |
elif ! [ $1 -eq $1 ] 2> /dev/null; then | |
echo "ASN must be a number" | |
exit 1 | |
fi | |
setname="as$1" | |
echo "create $setname hash:net family inet hashsize 1024 maxelem 2048" > "$setname.ipset" | |
/usr/bin/whois -h whois.radb.net -i origin AS$1 | /bin/grep -Eo '([0-9.]+){4}/[0-9]+' >> "$setname.ipset" | |
lines=`wc -l < $setname.ipset` | |
if (($lines < 2)); then | |
echo "ASN $1 not found or no routes found" | |
rm -f "$setname.ipset" | |
exit | |
elif (($lines > 2049)); then | |
echo "More than 2048 records found, adjust maxelem for set" | |
fi | |
iptables -I INPUT -m set --match-set "$setname" src -j DROP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment