7-How To Set Up The Password Protect Web Directory in Apache
7-How To Set Up The Password Protect Web Directory in Apache
This article describes an easy way to password protect a web directory in Apache
using an .htaccess file.
Find the section that begins with the Directory "/var/www/html" and change the line
from AllowOverride none to AllowOverride AuthConfig
Page 1
Options Indexes FollowSymLinks
AllowOverride AuthConfig
Require all granted
This will ask you to supply and confirm a password for authuser1.
If you want to add another user, then leave out the -c argument
with htpasswd command.
You can see the user name and the encrypted password for each record by
running:
You need to grant permission to the “www-data” user to be able to read the
.htpasswd file.
Page 2
sudo chown www-data:www-data /etc/httpd/.htpasswd
sudo chmod 0660 /etc/httpd/.htpasswd
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
AuthName: This is content which displays on web page when prompted for user
name and password.
Save and close the file, and restart Apache to make these changes take effect.
Page 3
If you enter the correct user credentials, you will be allowed to access the content. If
you enter the wrong user credentials or hit “Cancel,” you will see
the Unauthorized error page.
Page 4
8- Setting Up Fail2ban to Protect
Apache from a DDOS Attack
Apache is one of the most widely used and popular web servers in the world, so it is
important to protect your website and users from Brute-force attacks. Fail2ban is an
open-source intrusion prevention software written in Python. Fail2Ban continuously
analyzes various services’ log files (like Apache, ssh, postfix …), and if it detects
malicious attacks, then it creates rules on the firewall to block hackers IP addresses
for a specified amount of time. Fail2Ban also informs a system admin with an email
of its activity.
Page 5
In this article I will explain how to install fail2ban and configure it to monitor your
Apache logs for malicious authentication failure attempts.
Requirements
Ubuntu server 14.04 with Apache installed
Installing Fail2Ban
First, make sure the Apache server is running and password authentication is
enabled.
[apache]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache2/*error.log
maxretry = 3
findtime = 600
ignoreip = 192.168.1.227
[apache-noscript]
Page 6
enabled = true
port = http,https
filter = apache-noscript
logpath = /var/log/apache2/*error.log
maxretry = 3
findtime = 600
ignoreip = 192.168.1.227
[apache-overflows]
enabled = true
port = http,https
filter = apache-overflows
logpath = /var/log/apache2/*error.log
maxretry = 2
findtime = 600
ignoreip = 192.168.1.227
[apache-badbots]
enabled = true
port = http,https
filter = apache-badbots
logpath = /var/log/apache2/*error.log
maxretry = 2
findtime = 600
ignoreip = 192.168.1.227
Save and close the file, then restart fail2ban for the changes to take effect.
You can verify the rules that were added by Fail2Ban in iptables using the following
command:
sudo iptables -L
Page 7
Note : You can find the details of each jail described below:
[apache-noscript] : this jail is used to block remote clients who are searching
for scripts on the website to execute.
[apache-noscript] : this jail is used to block remote clients who are searching
for scripts on website to execute.
Page 8
Note : You can find the details of each rule described below.
bantime : this option specifies the number of seconds that a remote host will be
blocked from the server.
maxretry : this option specifies the number of failed login attempts before
a remote host is blocked for the length of the ban time.
ignoreip : this option allows you to whitelist certain IP addresses from blocking.
Page 9
To see the status of a particular jail like apache, apache-badbots by running the
following commands:
Testing Fail2Ban
It is important to test your fail2ban whether it is working as expected or not. Now on
a remote machine, open your web browser and type the URL of your domain (or
your server’s IP address). When Apache prompts for authentication, give an
incorrect user name and password repeatedly. After you have reached the limit you
should be blocked and unable to access the site.
Page 10
Check the status with the fail2ban-client command:
You will see your IP address being blocked from the site.
Page 11
Conclusion
Now, you have enough knowledge to configure fail2ban. Using fail2ban is a good
and easy way to stop flooding (Brute-force attacks). It is also a good way to limit the
number of bad requests you receive on your Apache web server.
Page 12