Skip to content

Instantly share code, notes, and snippets.

@Ekultek
Created August 9, 2018 21:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Ekultek/b8cf855b4e1547d8d1f440f91aa87a91 to your computer and use it in GitHub Desktop.
Save Ekultek/b8cf855b4e1547d8d1f440f91aa87a91 to your computer and use it in GitHub Desktop.
A script to download bad IP addresses and deny them with UFW
#!/bin/bash
TMP_DIR="/tmp"
URL_LINKS=$"http://www.blocklist.de/lists/ssh.txt
http://www.blocklist.de/lists/apache.txt
http://www.blocklist.de/lists/asterisk.txt
http://www.blocklist.de/lists/bots.txt
http://www.blocklist.de/lists/courierimap.txt
http://www.blocklist.de/lists/courierpop3.txt
http://www.blocklist.de/lists/email.txt
http://www.blocklist.de/lists/ftp.txt
http://www.blocklist.de/lists/imap.txt
http://www.blocklist.de/lists/ircbot.txt
http://www.blocklist.de/lists/pop3.txt
http://www.blocklist.de/lists/postfix.txt
http://www.blocklist.de/lists/proftpd.txt
http://www.blocklist.de/lists/sip.txt
http://www.ciarmy.com/list/ci-badguys.txt
http://charles.the-haleys.org/ssh_dico_attack_hdeny_format.php/hostsdeny.txt
http://www.nothink.org/blacklist/blacklist_ssh_day.txt
http://malc0de.com/bl/IP_Blacklist.txt
http://www.nothink.org/blacklist/blacklist_malware_dns.txt
http://www.nothink.org/blacklist/blacklist_malware_http.txt
http://www.nothink.org/blacklist/blacklist_malware_irc.txt"
function denyHost() {
sudo ufw deny from $1;
}
function getLists() {
if [[ ! -d "${BACKUP_DIR}" ]]; then
mkdir -p "${BACKUP_DIR}";
fi;
for line in echo ${URL_LINKS}; do
filename=$(python -c 'import string; import random; acc=string.ascii_letters; d=["".join([random.choice(acc) for _ in range(1)]) for _ in range(7)]; print("".join(d))')
writeTo="${TMP_DIR}/${filename}.deny";
touch $writeTo;
curl -o "${writeTo}" $line;
while read item; do
denyHost $item;
done < "${writeTo}";
done;
}
getLists;
@Gawrion
Copy link

Gawrion commented Aug 2, 2022

It's taking so much time to add one rule (one rule per second).
The way U do this must be more efficient.
Maybe editing user.rules and reloading the ufw?

@Ekultek
Copy link
Author

Ekultek commented Aug 2, 2022

However you want to do it.

@Gawrion
Copy link

Gawrion commented Aug 4, 2022

now i dont have much time, but maybe in the future i will fork something more efficient taking your code as a base ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment