How to install BFD (Brute Force Detection) Published: Jan 21, 2004
  • Rating

    5/5

Apache's .htaccess is a great way to take control of your website by password protecting directories, creating custom error pages and controling user access.

Introduction

In this tutorial you will find out about the .htaccess file and the power it has to improve your website. Although .htaccess is only a file, it can change settings on the servers and allow you to do many different things, the most popular being able to have your own custom 404 error pages. .htaccess isn't difficult to use and is really just made up of a few simple instructions in a text file.

Will My Host Support It?

This is probably the hardest question to give a simple answer to. Many hosts support .htaccess but don't actually publicise it and many other hosts have the capability but do not allow their users to have a .htaccess file. As a general rule, if your server runs Unix or Linux, or any version of the Apache web server it will support .htaccess, although your host may not allow you to use it.

A good sign of whether your host allows .htaccess files is if they support password protection of folders. To do this they will need to offer .htaccess (although in a few cases they will offer password protection but not let you use .htaccess). The best thing to do if you are unsure is to either upload your own .htaccess file and see if it works or e-mail your web host and ask them.

What Can I Do?

You may be wondering what .htaccess can do, or you may have read about some of its uses but don't realise how many things you can actually do with it.

There is a huge range of things .htaccess can do including: password protecting folders, redirecting users automatically, custom error pages, changing your file extensions, banning users with certian IP addresses, only allowing users with certain IP addresses, stopping directory listings and using a different file as the index file.

Creating A .htaccess File

Creating a .htaccess file may cause you a few problems. Writing the file is easy, you just need enter the appropriate code into a text editor (like notepad). You may run into problems with saving the file. Because .htaccess is a strange file name (the file actually has no name but a 8 letter file extension) it may not be accepted on certain systems (e.g. Windows 3.1). With most operating systems, though, all you need to do is to save the file by entering the name as:

".htaccess"

(including the quotes). If this doesn't work, you will need to name it something else (e.g. htaccess.txt) and then upload it to the server. Once you have uploaded the file you can then rename it using an FTP program.

Warning

Before beginning using .htaccess, I should give you one warning. Although using .htaccess on your server is extremely unlikely to cause you any problems (if something is wrong it simply won't work), you should be wary if you are using the Microsoft FrontPage Extensions. The FrontPage extensions use the .htaccess file so you should not really edit it to add your own information. If you do want to (this is not recommended, but possible) you should download the .htaccess file from your server first (if it exists) and then add your code to the beginning.

Custom Error Pages

The first use of the .htaccess file which I will cover is custom error pages. These will allow you to have your own, personal error pages (for example when a file is not found) instead of using your host's error pages or having no page. This will make your site seem much more professional in the unlikely event of an error. It will also allow you to create scripts to notify you if there is an error (for example I use a PHP script on Free Webmaster Help to automatically e-mail me when a page is not found).

You can use custom error pages for any error as long as you know its number (like 404 for page not found) by adding the following to your .htaccess file:

ErrorDocument errornumber /file.html

For example if I had the file notfound.html in the root directory of my site and I wanted to use it for a 404 error I would use:

ErrorDocument 404 /notfound.html

If the file is not in the root directory of your site, you just need to put the path to it:

ErrorDocument 500 /errorpages/500.html

These are some of the most common errors:

401 - Authorization Required
400 - Bad request
403 - Forbidden
500 - Internal Server Error
404 - Wrong page

Then, all you need to do is to create a file to display when the error happens and upload it and the .htaccess file.

Part 2

In part 2 I will show you how to use some of the other .htaccess functions to improve your website.
[pagebreak title='.htaccess Commands']
Introduction

In the last part I introduced you to .htaccess and some of its useful features. In this part I will show you how to use the .htaccess file to implement some of these.

Stop A Directory Index From Being Shown

Sometimes, for one reason or another, you will have no index file in your directory. This will, of course, mean that if someone types the directory name into their browser, a full listing of all the files in that directory will be shown. This could be a security risk for your site.

To prevent against this (without creating lots of new 'index' files, you can enter a command into your .htaccess file to stop the directory list from being shown:

Options -Indexes

Deny/Allow Certian IP Addresses

In some situations, you may want to only allow people with specific IP addresses to access your site (for example, only allowing people using a particular ISP to get into a certian directory) or you may want to ban certian IP addresses (for example, keeping disruptive memembers out of your message boards). Of course, this will only work if you know the IP addresses you want to ban and, as most people on the internet now have a dynamic IP address, so this is not always the best way to limit usage.

You can block an IP address by using:

deny from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will block a whole range.

You can allow an IP address by using:

allow from 000.000.000.000

where 000.000.000.000 is the IP address. If you only specify 1 or 2 of the groups of numbers, you will allow a whole range.

If you want to deny everyone from accessing a directory, you can use:

deny from all

but this will still allow scripts to use the files in the directory.

Alternative Index Files

You may not always want to use index.htm or index.html as your index file for a directory, for example if you are using PHP files in your site, you may want index.php to be the index file for a directory. You are not limited to 'index' files though. Using .htaccess you can set foofoo.blah to be your index file if you want to!

Alternate index files are entered in a list. The server will work from left to right, checking to see if each file exists, if none of them exisit it will display a directory listing (unless, of course, you have turned this off).

DirectoryIndex index.php index.php3 messagebrd.pl index.html index.htm

Redirection

One of the most useful functions of the .htaccess file is to redirect requests to different files, either on the same server, or on a completely different web site. It can be extremely useful if you change the name of one of your files but allow users to still find it. Another use (which I find very useful) is to redirect to a longer URL, for example in my newsletters I can use a very short URL for my affiliate links. The following can be done to redirect a specific file:

Redirect /location/from/root/file.ext http://www.othersite.com/new/file/location.xyz

In this above example, a file in the root directory called oldfile.html would be entered as:

/oldfile.html

and a file in the old subdirectory would be entered as:

/old/oldfile.html

You can also redirect whole directoires of your site using the .htaccess file, for example if you had a directory called olddirectory on your site and you had set up the same files on a new site at: http://www.newsite.com/newdirectory/ you could redirect all the files in that directory without having to specify each one:

Redirect /olddirectory http://www.newsite.com/newdirectory

Then, any request to your site below /olddirectory will bee redirected to the new site, with the extra information in the URL added on, for example if someone typed in:

http://www.youroldsite.com/olddirecotry/oldfiles/images/image.gif

They would be redirected to:

http://www.newsite.com/newdirectory/oldfiles/images/image.gif

This can prove to be extremely powerful if used correctly.

Part 3

In part 3 I will cover a few other uses of the .htaccess file including password protection.
[pagebreak title='Password Protection']
Introduction

Although there are many uses of the .htaccess file, by far the most popular, and probably most useful, is being able to relaibly password protect directories on websites. Although JavaScript etc. can also be used to do this, only .htaccess has total security (as someone must know the password to get into the directory, there are no 'back doors')

The .htaccess File

Adding password protection to a directory using .htaccess takes two stages. The first part is to add the appropriate lines to your .htaccess file in the directory you would like to protect. Everything below this directory will be password protected:

AuthName "Section Name"
AuthType Basic
AuthUserFile /full/path/to/.htpasswd
Require valid-user

There are a few parts of this which you will need to change for your site. You should replace "Section Name" with the name of the part of the site you are protecting e.g. "Members Area".

The /full/parth/to/.htpasswd should be changed to reflect the full server path to the .htpasswd file (more on this later). If you do not know what the full path to your webspace is, contact your system administrator for details.

The .htpasswd File

Password protecting a directory takes a little more work than any of the other .htaccess functions because you must also create a file to contain the usernames and passwords which are allowed to access the site. These should be placed in a file which (by default) should be called .htpasswd. Like the .htaccess file, this is a file with no name and an 8 letter extension. This can be placed anywhere within you website (as the passwords are encrypted) but it is advisable to store it outside the web root so that it is impossible to access it from the web.

Entering Usernames And Passwords

Once you have created your .htpasswd file (you can do this in a standard text editor) you must enter the usernames and passwords to access the site. They should be entered as follows:

username:password

where the password is the encrypted format of the password. To encrypt the password you will either need to use one of the premade scripts available on the web or write your own. There is a good username/password service at the KxS site which will allow you to enter the user name and password and will output it in the correct format.

For multiple users, just add extra lines to your .htpasswd file in the same format as the first. There are even scripts available for free which will manage the .htpasswd file and will allow automatic adding/removing of users etc.

Accessing The Site

When you try to access a site which has been protected by .htaccess your browser will pop up a standard username/password dialog box. If you don't like this, there are certain scripts available which allow you to embed a username/password box in a website to do the authentication. You can also send the username and password (unencrypted) in the URL as follows:

http://username:[email protected]/directory/

Summary

.htaccess is one of the most useful files a webmaster can use. There are a wide variety of different uses for it which can save time and increase security on your website.

Thanks to freewebmasterhelp.com for providing this article.

 

 

  • Rating

    5/5

Related Articles

Comments (36)

  • Gravatar - Olly
    Olly 15:26, January 21, 2004
    What if your IP isnt static, (if you are talking about the ip which you use to connect, not your box ip) how would you get around this problem?
  • Gravatar - Mike
    Mike 02:14, January 23, 2004
    "What if your IP isnt static, (if you are talking about the ip which you use to connect, not your box ip) how would you get around this problem? "

    I was wondering the same thing, what if your IP isn't static.
  • Gravatar - matlex
    matlex 04:49, January 27, 2004
    I think its simple then.

    Add the whole IP range of your ISP.

    for example:
    If you ISP IP range
    12.234.345.0-12.234.345.255

    Add just this:
    12.234.345
  • Gravatar - bman
    bman 19:37, February 14, 2004
    any idears how to fix this ?
    tail -f /var/log/bfd_log
    Feb 14 20:35:00 xxxxxx BFD(8570): could not locate $FWFILE, aborting.
  • Gravatar - Alex
    Alex 15:03, March 16, 2004
    could not locate $FWFILE is because you didnt installed APF before
  • Gravatar - Ryan
    Ryan 23:48, July 20, 2004
    Q: what if 1, you dont have static ips, and 2, your isp has more then 2 b class ip blocks? as i dont want to add them all and open it upto all 3 1/4 million users they host.
  • Gravatar - pierre
    pierre 19:17, August 19, 2004
    how to install BFD can you write the command pls .<br />
    thank you
  • Gravatar - Nonel S. Pagdato
    Nonel S. Pagdato 09:12, October 4, 2004
    Hi, is there any BFD application for any flavor of microsoft server, hope to hear from you soon, thanks..
  • Gravatar - netcopan
    netcopan 16:58, December 19, 2004
    should we add it into cron? After I restart server it will still be there??
  • Gravatar - Steel Rat
    Steel Rat 22:05, January 12, 2005
    Also a line on how to have BFD startup automatically (unless it's tied to APF in that regard...
  • Gravatar - Alan
    Alan 22:39, January 21, 2005
    >Q: what if 1, you dont have static ips<br />
    <br />
    don't get your password wrong <br />
    <br />
    :)
  • Gravatar - Matrafox
    Matrafox 09:18, March 23, 2005
    >Q: what if 1, you dont have static ips<br />
    <br />
    Then if u type rong password and u are banned, u just have to reboot your machine to take another Ip, and just try again :)
  • Gravatar - xaver
    xaver 02:38, April 2, 2005
    "There is an included 'install.sh' script that installs all files to<br />
    '/usr/local/bfd/' and places a 8-minute cronjob in '/etc/cron.d/bfd'. The setup<br />
    is really as simple as that."
  • Gravatar - Chris
    Chris 00:03, July 22, 2005
    Thanks works perfect.
  • Gravatar - robbert
    robbert 16:00, August 8, 2005
    many thnx! works perfect!!!!
  • Gravatar - Mike
    Mike 00:37, September 6, 2005
    It would help ppl if you actually gave some good examples of what configurations are preferable on a cPanel or other server, all you are doing here is stating the contents on the RFX site or in a README.
  • Gravatar - yenren
    yenren 13:05, September 11, 2005
    do we have to restart bdf manually everytime we restarted server?
  • Gravatar - grabt
    grabt 10:58, October 22, 2005
    How to stop BFD
  • Gravatar - sam
    sam 11:44, January 18, 2006
    Its realy good!
  • Gravatar - Alexender
    Alexender 08:21, March 5, 2006
    grabt:<br />
    <br />
    to stop BFD , type<br />
    <br />
    /usr/local/sbin/bfd -s <br />
    <br />
    <br />
  • Gravatar - Luis
    Luis 02:49, March 15, 2006
    I actully managed to lock myself out of my server the other day after a few typing errors when trying to login... just went and turned my linksys router off, waited a few minutes and then reconnected... I had another IP and was able to enter again.
  • Gravatar - James Tervit
    James Tervit 19:07, April 17, 2006
    Installed BFD along with a few minor changes to my ports and it stopped failed authentications dead in its tracks..... very cool
  • Gravatar - Ramkriz
    Ramkriz 20:21, July 31, 2006
    BFD is not working with the default rules. Please give me some rules for BFD.<br />
    <br />
    Thanks.
  • Gravatar - Alan
    Alan 10:55, August 17, 2006
    does this bruteforce auto start with system reboot?
  • Gravatar - Jose
    Jose 06:52, September 14, 2006
    Q: Can i type in ignore hosts my dyndns host?<br />
    <br />
    Ex. abcdefg.dyndns.org<br />
    <br />
    Thanks for you teach
  • Gravatar - hello123
    hello123 05:51, October 25, 2006
    how to check whether BFD is started
  • Gravatar - Turkulerdiyari
    Turkulerdiyari 22:08, November 17, 2006
    I get this error :<br />
    could not locate $TLOGP, aborting.<br />
    <br />
    How can i fix this problem.
  • Gravatar - sss
    sss 11:51, January 16, 2007
    Really gud one
  • Gravatar - ePak
    ePak 22:09, February 1, 2007
    Q: Do I have to restart bfd everytime the server reboots?<br />
    <br />
    A: No. BFD is not a daemon. There is a cron that runs every 10 minutes that sweets the logs for "attacks." So basically, every 10 minutes, it runs on its own. Location of cron: /etc/cron.d/bfd<br />
    <br />
    <br />
    Q: How do I disable BFD?<br />
    <br />
    A: You can remove the cron from /etc/cron.d/bfd or you can use the uninstall script that came in the BFD tar.<br />
    <br />
    I like the script, but my question is, how many lines does it take to cause an IP block?
  • Gravatar - Radu
    Radu 15:55, February 23, 2007
    Has anybody try to install on suse ? Or
  • Gravatar - alex
    alex 22:39, March 18, 2007
    My bfd can't trace proftpd attack, why?<br />
    Can anyone help me?<br />
    Mar 18 22:23:47 ServerFTP proftpd[8035]: ServerFTP (82.191.154.xxx[82.191.154.xxx]) - USER anonymous: no such user found from 82.191.154.xxx [82.191.154.xxx] to 80.21.159.xxx:21
  • Gravatar - Riaan
    Riaan 21:52, September 24, 2007
    Hi there<br />
    <br />
    How can I change the rules so that bfd will block an IP after 2 attempt and what is the default attempts?
  • Gravatar - Tim
    Tim 14:10, October 13, 2007
    RE: Alexander<br />
    <br />
    /usr/local/sbin/bfd -s<br />
    <br />
    That does not STOP bfd, it runs it with standard output. BFD doesn't run as a service, but as cron job. So you'd have to remove the cron job as staed by ePak
  • Gravatar - Sabarish......
    Sabarish...... 21:44, January 20, 2008
    Hi Riaan,<br />
    <br />
    BFD is configured to block after 10 attempts. See the TRIG value inside the files on /usr/local/bfd/rules<br />
    <br />
    Let us know through mail, if u need any further assistance.
  • Gravatar - JT
    JT 18:29, September 26, 2008
    After restarting BFD, I got the following:<br />
    <br />
    /usr/local/sbin/bfd -s<br />
    BFD version 0.9 <[email protected]><br />
    Copyright (C) 1999-2004, R-fx Networks <[email protected]><br />
    Copyright (C) 2004, Ryan MacDonald <[email protected]><br />
    This program may be freely redistributed under the terms of the GNU GPL<br />
    <br />
    /usr/local/bfd/tlog: line 71: [: : integer expression expected<br />
    /usr/local/bfd/tlog: line 74: [: : integer expression expected<br />
    <br />
    What is the tlog bit mean? Please help...
  • Gravatar - dt
    dt 06:51, August 6, 2009
    I installed afp. ran it. see it in the log. then install bfd. ran it numerous times. nothing happens other than printing the copyright message, no matter what params I enter. i try failed logins from my other servers over and over and nothing happens. huge messages logfiles, doesn't seem to even try to scan trhem. bfd log file is completely empty. what is wrong? tried this on 2 servers, both didnt work

Add Your Thoughts

WebHostGear.com is a hosting directory, not a web host.

Copyright © 1998-2023 WebHostGear.com