tpotce/bin/blackhole.sh

110 lines
3.2 KiB
Bash
Raw Normal View History

2022-02-02 22:51:54 +00:00
#!/bin/bash
# Run as root only.
myWHOAMI=$(whoami)
if [ "$myWHOAMI" != "root" ]
then
echo "### Need to run as root ..."
echo
2022-02-02 22:51:54 +00:00
exit
fi
# Disclaimer
if [ "$1" == "" ];
then
echo "### Warning!"
echo "### This script will download and add blackhole routes for known mass scanners in an attempt to decrease the chance of detection."
echo "### IPs are neither curated or verified, use at your own risk!"
echo "###"
echo "### As long as <blackhole.sh del> is not executed the routes will be re-added on T-Pot start through </opt/tpot/bin/updateip.sh>."
echo "### Check with <ip r> or <dps.sh> if blackhole is enabled."
2022-02-02 22:51:54 +00:00
echo
echo "Usage: blackhole.sh add (add blackhole routes)"
echo " blackhole.sh del (delete blackhole routes)"
echo
exit
fi
# QnD paths, files
2022-02-02 22:51:54 +00:00
mkdir -p /etc/blackhole
cd /etc/blackhole
myFILE="mass_scanner.txt"
myURL="https://raw.githubusercontent.com/stamparm/maltrail/master/trails/static/mass_scanner.txt"
2022-02-07 16:06:09 +00:00
myBASELINE="500"
# Alternatively, using less routes, but blocking complete /24 networks
#myFILE="mass_scanner_cidr.txt"
#myURL="https://raw.githubusercontent.com/stamparm/maltrail/master/trails/static/mass_scanner_cidr.txt"
2022-02-02 22:51:54 +00:00
# Calculate age of downloaded list, read IPs
if [ -f "$myFILE" ];
2022-02-02 22:51:54 +00:00
then
2022-02-03 23:32:34 +00:00
myNOW=$(date +%s)
myOLD=$(date +%s -r "$myFILE")
2022-02-03 23:32:34 +00:00
myDAYS=$(( (now-old) / (60*60*24) ))
echo "### Downloaded $myFILE list is $myDAYS days old."
myBLACKHOLE_IPS=$(grep -o -P "\b(?:\d{1,3}\.){3}\d{1,3}\b" "$myFILE" | sort -u)
2022-02-03 23:32:34 +00:00
fi
# Let's load ip list
if [[ ! -f "$myFILE" && "$1" == "add" || "$myDAYS" -gt 30 ]];
2022-02-03 23:32:34 +00:00
then
echo "### Downloading $myFILE list."
aria2c --allow-overwrite -s16 -x 16 "$myURL" && \
myBLACKHOLE_IPS=$(grep -o -P "\b(?:\d{1,3}\.){3}\d{1,3}\b" "$myFILE" | sort -u)
2022-02-02 22:51:54 +00:00
fi
2022-02-03 23:32:34 +00:00
myCOUNT=$(echo $myBLACKHOLE_IPS | wc -w)
2022-02-02 22:51:54 +00:00
# Let's extract mass scanner IPs
if [ "$myCOUNT" -lt "$myBASELINE" ] && [ "$1" == "add" ];
2022-02-03 23:32:34 +00:00
then
echo "### Something went wrong. Please check contents of /etc/blackhole/$myFILE."
2022-02-03 23:32:34 +00:00
echo "### Aborting."
echo
exit
elif [ "$(ip r | grep 'blackhole' -c)" -gt "$myBASELINE" ] && [ "$1" == "add" ];
2022-02-03 23:32:34 +00:00
then
echo "### Blackhole already enabled."
echo "### Aborting."
echo
exit
fi
2022-02-02 22:51:54 +00:00
# Let's add blackhole routes for all mass scanner IPs
if [ "$1" == "add" ];
then
2022-02-03 23:32:34 +00:00
echo
echo -n "Now adding $myCOUNT IPs to blackhole."
2022-02-02 22:51:54 +00:00
for i in $myBLACKHOLE_IPS;
do
ip route add blackhole "$i"
2022-02-03 23:32:34 +00:00
echo -n "."
2022-02-02 22:51:54 +00:00
done
2022-02-03 23:32:34 +00:00
echo
echo "Added $(ip r | grep "blackhole" -c) IPs to blackhole."
echo
echo "### Remember!"
2022-02-07 16:06:09 +00:00
echo "### As long as <blackhole.sh del> is not executed the routes will be re-added on T-Pot start through </opt/tpot/bin/updateip.sh>."
echo "### Check with <ip r> or <dps.sh> if blackhole is enabled."
2022-02-03 23:32:34 +00:00
echo
exit
2022-02-02 22:51:54 +00:00
fi
# Let's delete blackhole routes for all mass scanner IPs
if [ "$1" == "del" ] && [ "$myCOUNT" -gt "$myBASELINE" ];
2022-02-02 22:51:54 +00:00
then
2022-02-03 23:32:34 +00:00
echo
echo -n "Now deleting $myCOUNT IPs from blackhole."
2022-02-02 22:51:54 +00:00
for i in $myBLACKHOLE_IPS;
do
ip route del blackhole "$i"
2022-02-03 23:32:34 +00:00
echo -n "."
2022-02-02 22:51:54 +00:00
done
2022-02-03 23:32:34 +00:00
echo
echo "$(ip r | grep 'blackhole' -c) IPs remaining in blackhole."
echo
rm "$myFILE"
2022-02-03 23:32:34 +00:00
else
echo "### Blackhole already disabled."
echo
2022-02-02 22:51:54 +00:00
fi