Yes, it's possible, and it work!
Make sure you are legally entitled to use an mmdb ASN database.
- Get a legal to ASN to IP use database. Check if the below connection is offered according to rights:
GitHub: P3TERX/GeoLite.mmdb: MaxMind's GeoIP2 GeoLite2 Country, City, and ASN databases
And place it in a directory as:
/usr/share/GeoIP/GeoLite2-ASN.mmdb
And also download the country database:
# curl -Lo /usr/share/GeoIP/GeoLite2-Country.mmdb https://git.io/GeoLite2-Country.mmdb
Before installing mod_maxminddb do the following:
- Install the libmaxminddb library. This can be installed via PPA in Ubuntu.
sudo add-apt-repository ppa:maxmind/ppa
sudo apt update
sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin
- Install the dev package for your apache version, in my case, this was done like below:
# sudo apt install apache2-dev
- Installing MaxMind with Apache:
# curl -Lo /usr/share/GeoIP/mod_maxminddb-1.2.0.tar.gz https://github.com/maxmind/mod_maxminddb/releases/download/1.2.0/mod_maxminddb-1.2.0.tar.gz
# tar -xvzf /usr/share/GeoIP/mod_maxminddb-1.2.0.tar.gz -C ~/
# cd ~/mod_maxminddb-1.2.0
Note. ~ is short for the home directory.
After going to the directory the ModMaxminddb was downloaded to, activate it:
# ./configure
# sudo make install
- Set .htaccess rule to block certain ASN (ISPs):
<IfModule mod_maxminddb.c>
MaxMindDBEnable On
MaxMindDBSetNotes On
MaxMindDBFile CITY_DB /usr/share/GeoIP/GeoLite2-City.mmdb
MaxMindDBFile ASN_DB /usr/share/GeoIP/GeoLite2-ASN.mmdb
MaxMindDBEnv COUNTRY_CODE CITY_DB/country/iso_code
MaxMindDBEnv CONTINENT_CODE CITY_DB/continent/code
MaxMindDBEnv MM_ASN ASN_DB/autonomous_system_number
MaxMindDBEnv MM_ASORG ASN_DB/autonomous_system_organization
MaxMindDBNetworkEnv ASN_DB ASN_DB_NETWORK
SetEnvIf MM_ASN ^(31708|9930|26496|21501|12876|38731|41564|399471|51852) BlockAsn
SetEnvIf Remote_Addr ^(66\.249\.) !BlockAsn
SetEnvIf REQUEST_URI ^(.*/\.well\-known/acme\-challenge/.*) !BlockAsn
order allow,deny
allow from all
Deny from env=BlockAsn <env=SecondBlockName> <env=ThirdBlockName>
</IfModule>
Test via curl on SSH CLI:
curl -I --header "X-Forwarded-For: 52.47.88.71" "https://currenge.com/" --referer "https://google.com/" --user-agent "hsts-preload-bot"
curl -I --header "X-Forwarded-For: 3.249.149.161" "https://currenge.com/" --referer "https://google.com/" --user-agent "Hola"
curl -I --header "X-Forwarded-For: 23.128.248.18" "https://currenge.com/" --referer "https://google.com/" --user-agent "Hola"
curl -I --header "X-Forwarded-For: 34.76.75.95" "https://currenge.com/" --referer "https://google.com/" --user-agent "Duplex on the web"
curl -I --header "X-Forwarded-For: 34.76.75.45" "https://currenge.com/" --referer "https://google.com/" --user-agent "yadadd"
curl -I --header "X-Forwarded-For: 34.76.75.25" "https://currenge.com/" --referer "https://google.com/" --user-agent "balbla"
curl -I --header "X-Forwarded-For: 104.167.241.0" "https://currenge.com/" --referer "https://google.com/" --user-agent "bing"
curl -I --header "X-Forwarded-For: 104.167.241.0" "https://currenge.com/" --referer "https://google.com/" --user-agent "Bella"
curl -I --header "X-Forwarded-For: 34.76.78.159" "https://currenge.com/" --referer "https://google.com/" --user-agent "Duplex on the web"
curl -I --header "X-Forwarded-For: 104.167.241.20" "https://currenge.com/" --referer "https://google.com/" --user-agent "Bingbot"