In het kader van transparantie en voor geïnteresseerden met een eigen forum, het script "TOR Hammer":
Code: Selecteer alles
#!/bin/bash
#author: Proos
#info: https://wo2forum.nl/viewtopic.php?f=16&t=43598
tor_Debug=2
#0 off, 1 summary, 2 full
tor_DebugFile=/home/proos/scripts/wo2forum/tor.debug
tor_DebugFileOverwrite=1
# 0 add to file, 1 replace/truncate file
# configuration
torcnf_DBUser=/home/proos/scripts/wo2forum/tor.db
torcnf_DBName=wo2forum
torcnf_DBTable=3_banlist
torcnf_TTL=86400
torcnf_Reason=TOR-IP
torcnf_ReasonPublic="I.v.m. spam vanaf het TOR-netwerk is toegang via TOR momenteel niet toegestaan. Maak een directe verbinding of probeer het later nog een keer: we verversen dagelijks de IP-lijst. Test je verbinding hier: check.torproject.org"
torcnf_Tempfile=/home/proos/scripts/wo2forum/tor.tmp
# populate the MySQL parameters
tor_DB="--defaults-extra-file=$torcnf_DBUser $torcnf_DBName --skip-column-names"
# counters
torcnt_New=0
torcnt_Old=0
torcnt_Updated=0
# get the current Unixtime
tor_Now=`date +%s`
# get the current humanreadable time
tor_NowLog=`date`
# get the current time for timing calculation
tor_Timer1="` date +%s.%N `"
# add the configured validity period to get the expiration Unix time
tor_Expire=$(($tor_Now + $torcnf_TTL))
# Sart up log file, clean when set to overwrite
if [[ $tor_Debug > 0 ]]; then
if [[ $tor_DebugFileOverwrite = 0 ]]; then echo "--TOR hammer start at $tor_NowLog" >> $tor_DebugFile; fi
if [[ $tor_DebugFileOverwrite = 1 ]]; then echo "--TOR hammer start at $tor_NowLog" > $tor_DebugFile; fi
fi
# get number of items in db before modifications
if [[ $tor_Debug > 0 ]]; then
torcnt_TStart=$(echo "SELECT COUNT(ban_id) FROM $torcnf_DBTable WHERE ban_reason = '$torcnf_Reason'" | mysql $tor_DB)
fi
if [[ $tor_Debug > 1 ]]; then echo "Downloading IP list" >> $tor_DebugFile; fi
# Get IP list of TOR exit nodes
wget -t 60 --tries 2 -q https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=1.0.0.1 -O -|sed '/^#/d' | while read tor_IP
# For each of the received IPs we perform the next steps
do
if [ -z $tor_IP ]; then echo "Could not obtain IP list. Exiting" & exit 1; fi
if [[ $tor_IP =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
# do nothing
tor_IPnothing=1
else
if [[ $tor_Debug = 1 ]]; then echo "Downloaded an invalid IP: $tor_IP" >> $tor_DebugFile; fi
if [[ $tor_Debug = 2 ]]; then echo "Skipping invalid IP: $tor_IP" >> $tor_DebugFile; fi
continue
fi
# In debug mode echo the IP
if [[ $tor_Debug > 1 ]]; then echo -n "$tor_IP" >> $tor_DebugFile; fi
# Check if we can find record with this IP and our own label
tor_ID=$(echo "SELECT ban_id FROM $torcnf_DBTable WHERE ban_ip = '$tor_IP'" | mysql $tor_DB)
if [[ -z $tor_ID ]]; then # there is no ID, it is new
let torcnt_New=$torcnt_New+1
if [[ $tor_Debug > 1 ]]; then echo -n " new IP" >> $tor_DebugFile; fi
# Add it to the database
tor_Add=$(echo "INSERT INTO $torcnf_DBTable (ban_id, ban_userid, ban_ip, ban_email, ban_start, ban_end, ban_exclude, ban_reason, ban_give_reason) VALUES (NULL, '0', '$tor_IP', '', '$tor_Now', '$tor_Expire', '0', '$torcnf_Reason', '$torcnf_ReasonPublic');" | mysql $tor_DB)
if [[ $tor_Debug > 1 ]]; then
tor_NewID=$(echo "SELECT ban_id FROM $torcnf_DBTable WHERE ban_ip = '$tor_IP'" | mysql $tor_DB)
echo ", added (ID $tor_NewID)" >> $tor_DebugFile
fi
else # The IP was registered earlier
let torcnt_Old=$torcnt_Old+1
if [[ $tor_Debug > 1 ]]; then echo -n " found (ID $tor_ID)" >> $tor_DebugFile; fi
# Check if it is ours
tor_Check=$(echo "SELECT * FROM $torcnf_DBTable WHERE ban_id = '$tor_ID' AND ban_reason = '$torcnf_Reason'" | mysql $tor_DB)
if [[ -z $tor_Check ]]; then # it is not ours, leave it
if [[ $tor_Debug > 1 ]]; then echo ", unmanaged entry, skipping update" >> $tor_DebugFile; fi
else # it is ours, update the expiration
tor_Update=$(echo "UPDATE $torcnf_DBTable SET ban_end = '$tor_Expire' WHERE $torcnf_DBTable.ban_id = $tor_ID;" | mysql $tor_DB)
if [[ $tor_Debug > 1 ]]; then echo ", set expiration to $tor_Expire" >> $tor_DebugFile; fi
let torcnt_Updated=$torcnt_Updated+1
fi
fi
# write variables to temp file because we cant communicate subshell to shell
echo torcnt_New=$torcnt_New > $torcnf_Tempfile
echo torcnt_Old=$torcnt_Old >> $torcnf_Tempfile
echo torcnt_Updated=$torcnt_Updated >> $torcnf_Tempfile
# End of the loop per IP
done
# clean stale records
torcnt_Delete=$(echo "SELECT COUNT(ban_id) FROM $torcnf_DBTable WHERE ban_reason = '$torcnf_Reason' AND ban_end BETWEEN 1 AND $tor_Now" | mysql $tor_DB)
tor_Delete=$(echo "DELETE FROM $torcnf_DBTable WHERE ban_reason = '$torcnf_Reason' AND ban_end BETWEEN 1 AND $tor_Now" | mysql $tor_DB)
# get number of items in db before modifications
if [[ $tor_Debug > 0 ]]; then
torcnt_TEnd=$(echo "SELECT COUNT(ban_id) FROM $torcnf_DBTable WHERE ban_reason = '$torcnf_Reason'" | mysql $tor_DB)
fi
if [[ $tor_Debug > 0 ]]; then
if [ -f "$torcnf_Tempfile" ]; then
. $torcnf_Tempfile
echo "$torcnt_TStart items in DB (at start)" >> $tor_DebugFile
echo "$torcnt_New added" >> $tor_DebugFile
torcnt_Ignored=$(($torcnt_Old-$torcnt_Updated))
echo "$torcnt_Ignored ignored" >> $tor_DebugFile
echo "$torcnt_Updated renewed">> $tor_DebugFile
torcnt_Total=$(($torcnt_New+$torcnt_Updated))
echo "$torcnt_Total active" >> $tor_DebugFile
torcnt_Stale=$(($torcnt_TEnd-$torcnt_Old-$torcnt_New))
echo "$torcnt_Stale stale" >> $tor_DebugFile
echo "$torcnt_Delete removed" >> $tor_DebugFile
echo "$torcnt_TEnd items in DB (at end)" >> $tor_DebugFile
tor_Timer2="` date +%s.%N `"
tor_TimerTotal=`echo "$tor_Timer2 - $tor_Timer1" | bc | awk -F"." '{print $1"."substr($2,1,3)}'`
echo "Done (in $tor_TimerTotal seconds)" >> $tor_DebugFile
echo >> $tor_DebugFile
else
# no data
echo "No data downloaded" >> $tor_DebugFile
fi
fi
# clean up temp file
if [ -f "$torcnf_Tempfile" ]; then
rm $torcnf_Tempfile
fi
Database configuratie in "tor.db", dient te bevatten user, password en host.