Linux Apache2 Webserver

Him Her

VIP Member
VIP Member
Joined
Dec 23, 2011
Messages
8,145
Reaction score
6,435
Location
North Yorkshire
I don't really get involved professionally any more, just host sites for a couple of mates and a couple for my own amusement. Still, it's irritating to get thousands of probes every day probably generated by script kiddies.

Apache2 isn't really optimised out of the box so I had to tune it a bit to stop the scanner from gobbling up all the memory and thrashing the swap file then I added geoIP to the awstats installation. Awstats provides website statistics like number of hits and where they came from in terms of IP or domain if it can be resolved. GeoIP does regional lookups on IP providing country of origin and city.

Both of these are included in most of the recent Ubuntu/Debian distributions so you can easily install them using apt-get. Be aware though that the directory structure in these distributions is a little different from the norm. For example, the GeoIP database will end up in /usr/share/GeoIP NOT /var/lib as some tutorials will suggest.

Once installed you can run this command:

geoiplookup 8.8.8.8

in a terminal session and get results like that below:

GeoIP Country Edition: US, United States
GeoIP City Edition, Rev 1: US, CA, Mountain View, 94043, 37.419201, -122.057404, 807, 650
GeoIP City Edition, Rev 0: US, CA, Mountain View, 94043, 37.419201, -122.057404
GeoIP ASNum Edition: AS15169 Google Inc.

However, even better is to build it into your Apache configuration and block countries using the .htaccess file!

Add these lines to your apache2.conf file:

# Block countries (.htaccess)
GeoIPEnable on
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
#

Ensure the geoip module is enabled in Apache and .htaccess usage is not blocked and restart Apache. Then you can add a .htaccess file to the root of the website(s) you want to protect:

# Block Countries
SetEnvIf GEOIP_COUNTRY_CODE GB AllowCountry #Allow GB
SetEnvIf GEOIP_COUNTRY_CODE UK AllowCountry #Allow UK
SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry #Allow US
#
Deny from all # Everything else is blocked
Allow from env=AllowCountry # Except the list above
Allow from 10.100. # And my internal network
# End Block Countries

A quick check of my Apache error log file reveals the culprits...

[04:53:10] [error] [client 37.140.141.23] client denied by server configuration: Russia
[05:11:03] [error] [client 119.63.196.92] client denied by server configuration: Japan
[05:17:04] [error] [client 123.125.71.28] client denied by server configuration: China
[05:17:17] [error] [client 220.181.108.123] client denied by server configuration: China
[05:36:06] [error] [client 46.4.38.67] client denied by server configuration: Germany
[05:47:29] [error] [client 180.76.5.196] client denied by server configuration: China
[05:51:37] [error] [client 37.140.141.23] client denied by server configuration: Russia
[06:17:12] [error] [client 123.125.71.35] client denied by server configuration: China
[06:17:24] [error] [client 220.181.108.118] client denied by server configuration: China
[06:19:49] [error] [client 37.140.141.23] client denied by server configuration: Russia
[06:47:27] [error] [client 37.140.141.23] client denied by server configuration: Russia
[07:04:19] [error] [client 178.137.129.128] client denied by server configuration: Russia
[07:04:19] [error] [client 178.137.129.128] client denied by server configuration: Russia
[07:18:17] [error] [client 37.140.141.23] client denied by server configuration: Russia
[07:21:52] [error] [client 123.125.71.24] client denied by server configuration: China
[07:22:08] [error] [client 220.181.108.99] client denied by server configuration: China
[07:34:10] [error] [client 180.76.5.149] client denied by server configuration: China

Note: the log file above has been modified by hand to show the countries but .htaccess is clearly doing its job!
 
Could you utilise IPTables as well. When I was researching SSH tunnel for my RPi I came across IPTables as a way of preventing unwanted connections... didnt go into too much detail but suggested it was security related.
 
Could you utilise IPTables as well. When I was researching SSH tunnel for my RPi I came across IPTables as a way of preventing unwanted connections... didnt go into too much detail but suggested it was security related.

Short answer is yes. It's more cryptic but less cpu time. I'm using a hardware firewall so it wasn't an option at the time but rationalisation may change my view!
 
Back
Top