Skip to content

Instantly share code, notes, and snippets.

@mraspor
Last active February 1, 2019 19:48
Show Gist options
  • Save mraspor/fb415178e4adb74d0eede888af5dcf14 to your computer and use it in GitHub Desktop.
Save mraspor/fb415178e4adb74d0eede888af5dcf14 to your computer and use it in GitHub Desktop.
APF firewall import blocked IPs from maxmind geoip database
#!/bin/bash
#
# URL of the Maxmind geoip database
#
filename="GeoLite2-Country-CSV"
filesuffix=".zip"
url="https://geolite.maxmind.com/download/geoip/database/$filename$filesuffix"
#
# Countries to block
#
countries=(1668284 # taiwan
1814991 # china
1149361 # afghanistan
298795 # turkey
3469034 # brazil
2017370 # russia
1835841 # south korea
1873107 # north korea
)
#
# Build grep command
#
grep="grep"
for i in ${countries[@]}; do
grep="$grep -e $i"
done
#
# Retrieve geoip database file
#
wget "$url"
#
# Unzip to stout and grep for blocked countries, filter only first column/IPs
# Place this file (blocked_ips.sh) in the /etc/apf directory and call if from cron once a week/month
#
unzip -p "$filename$filesuffix" "$filename"_*/GeoLite2-Country-Blocks-IPv4.csv | $grep | awk -F ',' '{ print $1 }' > deny_hosts.rules
#
# Delete the geoip zip file
#
rm -fr "$filename$filesuffix"
#
# Restart apf firewall
#
apf --restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment