FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Log all .htaccess/.htpasswd logins

htaccess htpasswd with phpA common request I get is how to view and/or log all usernames and passwords that clients use to access a website protected by htaccess basic authorization.

This article is BOSS and will show you how to fully take control of this aspect of security using php and .htaccess, I don't believe you will find how to do this anywhere else on the net.

Getting the password sent by the user

This is the php code you can use to show the decrypted password that was sent by the client.

$password=base64_decode(str_replace('Basic ','', $_SERVER['HTTP_AUTHORIZATION']));
echo $password;

.htaccess

# This points to your logging script, doesn't have to be php
ErrorDocument 401 /log-htpasswd.php

AuthName "Pass"
AuthUserFile /.htpasswd
AuthType Basic
Require valid-user

# Set REMOTE_USER env variable on 401 ErrorDocument
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^401$
RewriteRule .* - [E=REMOTE_USER:%{ENV:REDIRECT_REMOTE_USER}]

log-htpasswd.php


401 Authorization Required

Authorization Required

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.

'; exit; exit(); ?>

example log-htpasswd.log

just a list of usernames attempted
username1
tom
rcowen
askapache
dreamhost
dreamadmin

Debugging htaccess/htpasswd authorization

1. create a file called login.php in your /cgi-bin/ and copy to /


401 Authorization Required

Authorization Required

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.

'; if($_SERVER['REMOTE_ADDR'] !== '208.113.183.103') die(); echo '
';
$password=base64_decode(str_replace('Basic ','', $_SERVER['HTTP_AUTHORIZATION']));
echo $password;
print_r($_ENV);
print_r($_SERVER);
exit;
exit();
?>

Add this to your /web/user/domain.com/.htaccess

ErrorDocument 401 /logins.php

RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^401$ [OR]
RewriteCond %{REQUEST_URI} ^/.*login*.php$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},E=REMOTE_USER:%{ENV:REDIRECT_REMOTE_USER}]


AuthName "Protection"
AuthUserFile /web/user/.htpasswd
AuthGroupFile /dev/null
AuthType Basic
Require valid-user

Now goto your web browser and request http://site.com/cgi-bin/login.php and try entering the wrong password, hitting cancel, entering the correct password, etc.

Apache Source Code

    case HTTP_PROXY_AUTHENTICATION_REQUIRED:
    case HTTP_UNAUTHORIZED:
        return("

This server could not verify that youn" "are authorized to access the documentn" "requested. Either you supplied the wrongn" "credentials (e.g., bad password), or yourn" "browser doesn't understand how to supplyn" "the credentials required.

n");

More Info

Htaccess

 

 

Comments