Compare commits

...

4 Commits

Author SHA1 Message Date
sceox
97bd5b000a run with least privilege, support pf
* run the service as a low privilege user and use sudo/doas to add
      the block rules
    * add support for pf firewall
    * write documentation for the above
    * add abort function for handling error logging and exit cleanup
2020-09-06 15:20:12 -07:00
sceox
49d1f7524d log ufw errors to log file 2020-09-06 11:26:45 -07:00
sceox
4312d75e27 log date and ip on failure 2020-09-06 11:23:37 -07:00
sceox
20bbc771b6 only create cache if it does not exist 2020-09-06 11:21:40 -07:00
3 changed files with 70 additions and 22 deletions

View File

@ -7,9 +7,35 @@ To deploy:
* modify the configuration variables in the scripts
* copy the scripts to `/usr/local/bin`
* create the user
useradd -s /sbin/nologin _forumspam
* if using `pf`:
* create appropriate permissions for the file:
install -m 640 -o _forumspam -g wheel /dev/null /etc/pf-forumspam.txt
* add the following to `/etc/pf.conf`, preferably high up in the ruleset:
table <forumspam> persist file "/etc/pf-forumspam.txt"
block in quick on egress from <forumspam>
block out quick on egress to <forumspam>
* set up sudo or doas:
* for doas:
* add the following to `/etc/doas.conf`:
permit nopass _forumspam cmd pfctl args -nf /etc/pf.conf
permit nopass _forumspam cmd pfctl args -t forumspam -T replace -f /etc/pf-forumspam.txt
* for sudo:
* add to your sudo config file:
`Cmnd_Alias FIREWALL = /usr/sbin/ufw, /sbin/iptables`
and `_forumspam ALL = NOPASSWD: FIREWALL`
* register `q2a_usercheck.sh` as a systemd unit or rc script
TODO:
* also check against [botscout](http://botscout.com/api.htm)
* add support for pf
* add support for iptables
* give the scripts more sensible names

View File

@ -2,31 +2,53 @@
# API reference: https://www.stopforumspam.com/usage
# user configuration area - change as needed
ostype="bsd" # allowed options are bsd, linux
logf=/var/log/forumspam.log
limit=0 # number of violations allowed before blocking the ip
case "$ostype" in
bsd)
getroot="doas"
fw_cmd="pfctl"
;;
linux)
getroot="sudo"
fw_cmd="ufw"
;;
esac
abort() {
echo `date` 'error:' $* >> $logf
rm $repl
exit 1
}
block_ip() {
case "$fw_cmd" in
ufw)
if [ "$getroot" ufw deny from $ip >/dev/null 2>>$logf ]; then
echo `date` blocked ip $ip with freq $freq >> $logf
else
abort "failed to block ip $ip"
fi
;;
pfctl)
"$getroot" pfctl -nf /etc/pf.conf || abort "pf config check failed" # Ensure proposed changes are valid before reloading table
"$getroot" pfctl -t forumspam -T replace -f /etc/pf-forumspam.txt
;;
esac
}
# we ignore all arguments except the first
ip=$1
repl=`mktemp`
logf=/var/log/forumspam.log
curl 'http://api.stopforumspam.org/api?ip='$ip 2>/dev/null > $repl
if ! grep -q '<response success="true">' $repl; then
echo error failed to query stopforumspam api >> $logf
rm $repl
exit 1
fi
if ! grep -q '<appears>yes</appears>' $repl; then
rm $repl
exit 0
abort 'error failed to query stopforumspam api'
fi
freq=`grep '<frequency>' $repl | sed 's/<frequency>//; s!</frequency>!!'`
if [ "$freq" -ge 1 ]; then
if ufw deny from $ip >/dev/null; then
echo `date` blocked ip $ip with freq $freq >> $logf
rm $repl
exit 0
else
echo error failed to add ufw rule >> $logf
rm $repl
exit 2
fi
if grep -q '<appears>yes</appears>' $repl && [ "$freq" -gt "$limit" ]; then
block_ip "$ip"
fi
rm $repl

View File

@ -3,7 +3,7 @@
logf='/var/log/nginx/access.log'
cache='/tmp/forumspam_cache'
install -m 600 /dev/null $cache
test -f $cache || install -m 600 /dev/null $cache
tail -F $logf | while read line ; do
case $line in (*"POST"*)