1

Topic: ADD Anti-Brutus-Force on Login.php

Hello all ,

I Woud make a anti-brutus-force for ma login !

I Have put a captcha but ....

I have make a script but he can't do :s

Can you say me the error ?

<?php

/***********************************************************************



  Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)



  This file is part of PunBB.



  PunBB is free software; you can redistribute it and/or modify it

  under the terms of the GNU General Public License as published

  by the Free Software Foundation; either version 2 of the License,

  or (at your option) any later version.



  PunBB is distributed in the hope that it will be useful, but

  WITHOUT ANY WARRANTY; without even the implied warranty of

  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

  GNU General Public License for more details.



  You should have received a copy of the GNU General Public License

  along with this program; if not, write to the Free Software

  Foundation, Inc., 59 Temple Place, Suite 330, Boston,

  MA  02111-1307  USA



************************************************************************/


$user = gethostbyaddr(getenv('REMOTE_ADDR'));

$temps = time();
$temps1 = $temps - 60;//Ici $temps1 vaut $temps - 61 donc l'heure actuelle moins 1 minute (60 secondes). C'est le temps pendant lequel les erreurs de mots de passe peuvent être effectuées.


if (isset($_GET['action']))

    define('PUN_QUIET_VISIT', 1);





$captcha_questions = array(
    'Combien font deux plus deux ?' => 4,
    'Combien font quatre moins un ?' => 3,
    'Combien font trois plus deux ?' => 5,
    'Combien font deux fois quatre ?' => 8,
    'Combien font cinq moins deux ?' => 3,
    'Combien font six plus trois ?' => 9,
    'Combien font sept moins un ?' => 6,
    'Combien font huit fois deux ?' => 16,
    'Combien font six fois deux ?' => 12,
    'Combien font neuf moins sept ?' => 2,
    'Combien font 1 + 3' => 4,
    'Combien font 1 - 1' => 0,
    'Combien font 2 + 6' => 8,
    'Combien font 2 - 1' => 1,
    'Combien font 3 + 6' => 9,
    'Combien font 3 - 2' => 1,
    'Combien font 4 + 8' => 12,
    'Combien font 4 - 2' => 2,
    'Combien font 5 + 3' => 8,
    'Combien font 5 - 2' => 3,
    'Combien font 6 + 4' => 10,
    'Combien font 6 - 3' => 3,
    'Combien font 7 + 6' => 13,
    'Combien font 7 - 3' => 4,
    'Combien font 8 + 7' => 15,
    'Combien font 8 - 3' => 5,
    'Combien font 9 + 9' => 18,
    'Combien font 9 - 2' => 7,
    'Combien font 10 + 4' => 14,
    'Combien font 10 - 4' => 6,
    'Combien font Un plus trois' => 4,
    'Combien font Un moins un' => 0,
    'Combien font Deux plus six' => 8,
    'Combien font Deux moins un' => 1,
    'Combien font Trois plus six' => 9,
    'Combien font Trois moins deux' => 1,
    'Combien font Quatre plus huit' => 12,
    'Combien font Quatre moins deux' => 2,
    'Combien font Cinq plus trois' => 8,
    'Combien font Cinc moins deux' => 3,
    'Combien font Six plus quatre' => 10,
    'Combien font Six moins trois' => 3,
    'Combien font Sept plus six' => 13,
    'Combien font Sept moins quatre' => 4,
    'Combien font Huit plus sept' => 15,
    'Combien font Huit moins trois' => 5,
    'Combien font Neuf plus neuf' => 18,
    'Combien font Neuf moins deux' => 7,
    'Combien font Dix plus quatre' => 14,
    'Combien font Dix moins quatre' => 6,
    'Un CD-Rom est carre ou rond ?' => rond,
    'Une tomate est rouge ou bleue ?' => rouge,
    'Une pierre est molle ou dure ?' => dure,
    'Le feu est chaud ou froid ?' => chaud,
    'Un citron est noir ou jaune ?' => jaune,
    'Un train roule ou vole ?' => roule,
    'Un ocean est petit ou grand ?' => grand,
    'Un glacier est froid ou chaud ?' => froid,
    'Un bateau vole ou flote ?' => flote,
    'Une montagne est haute ou basse ?' => haute,
    'Quel liquide contient un stylo-plume ?' => encre,
    'Quelle est la couleur centrale du drapeaux de la France ?' => blanc,
    'Quelle ville est la capitale de la France ?' => paris,
    'Quel liquide contient une bouteille de bordeaux ?' => vin,
    'Quel fruit produit un pommier ? (au singulier)' => pomme,
    'Quel produit fabrique principalement un boulanger ?' => pain,
    'Quel animal de compagnie miaule ?' => chat,
    'Quelle est la monnaie actuelle de la France ?' => euro,
    'Quel est l\'acronyme de "Train a Grande Vitesse" ?' => tgv,
    'Quel est le premier jour de la semaine ?' => lundi

);

$captcha_index = rand(0,count($captcha_questions)-1);


define('PUN_ROOT', './');
require PUN_ROOT.'include/common.php';




// Load the login.php language file

require PUN_ROOT.'lang/'.$pun_user['language'].'/login.php';



$action = isset($_GET['action']) ? $_GET['action'] : null;



if (isset($_POST['form_sent']) && $action == 'in')

{

    $form_username = trim($_POST['req_username']);

    $form_password = trim($_POST['req_password']);



    $username_sql = ($db_type == 'mysql' || $db_type == 'mysqli') ? 'username=\''.$db->escape($form_username).'\'' : 'LOWER(username)=LOWER(\''.$db->escape($form_username).'\')';

 // Secu brutus by AsTr0 AsTr0(Part 1/3)
$logintime = time() - 10; // 10 seconds delais.
$SQLoginCheck = "SELECT * FROM ".$db->prefix."iptrylog WHERE ip='".getremoteaddress()."' and lastry>='".$logintime."'";
$check = mysql_num_rows(mysql_query($SQLoginCheck));
if($check > 0) error('Mon message d erreur','login.php', '', '');
// End of part 1 of patch, see below

    $result = $db->query('SELECT id, group_id, password, save_pass FROM '.$db->prefix.'users WHERE '.$username_sql) or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());

    list($user_id, $group_id, $db_password_hash, $save_pass) = $db->fetch_row($result);



    $authorized = false;



    if (!empty($db_password_hash))

    {

    $p_question = trim($_POST['captcha_q']);
    $p_answer = trim($_POST['captcha']);
    
    $questions_arry = array();
    foreach ($captcha_questions as $k => $v)
        $questions_arry[md5($k)] = $v;
    
    if (empty($questions_arry[$p_question]) || $questions_arry[$p_question] != $p_answer)
        message('Vous vous êtes trompé à la question ou vous n\'etes peut-être pas un humain ...');
        
        $sha1_in_db = (strlen($db_password_hash) == 40) ? true : false;

        $sha1_available = (function_exists('sha1') || function_exists('mhash')) ? true : false;



        $form_password_hash = pun_hash($form_password);    // This could result in either an SHA-1 or an MD5 hash (depends on $sha1_available)



        if ($sha1_in_db && $sha1_available && $db_password_hash == $form_password_hash)

            $authorized = true;

    // Secu brutus by AsTr0(Part 2/3)

$db->query('DELETE FROM '.$db->prefix.'iptrylog WHERE ip=\''.getremoteaddress().'\'');

    // End of Part 2. 
        else if (!$sha1_in_db && $db_password_hash == md5($form_password))

        {

            if ($sha1_available)    // There's an MD5 hash in the database, but SHA1 hashing is available, so we update the DB

                $db->query('UPDATE '.$db->prefix.'users SET password=\''.$form_password_hash.'\' WHERE id='.$user_id) or error('Unable to update user password', __FILE__, __LINE__, $db->error());

        }

    }



    if (!$authorized)

    {

    // Secu brutus by AsTr0 (Part 3/3)
                $db->query('INSERT INTO '.$db->prefix.'iptrylog (ip, lastry) VALUES(\''.getremoteaddress().'\', \''.$logintime+10.'\')');
    
        message($lang_login['Wrong user/pass'].' <a href="login.php?action=forget">'.$lang_login['Forgotten pass'].'</a>');





    // Update the status if this is the first time the user logged in

    if ($group_id == PUN_UNVERIFIED)

        $db->query('UPDATE '.$db->prefix.'users SET group_id='.$pun_config['o_default_user_group'].' WHERE id='.$user_id) or error('Unable to update user status', __FILE__, __LINE__, $db->error());



    // Remove this users guest entry from the online list

    $db->query('DELETE FROM '.$db->prefix.'online WHERE ident=\''.$db->escape(get_remote_address()).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $db->error());



    $expire = ($save_pass == '1') ? time() + 31536000 : 0;

    pun_setcookie($user_id, $form_password_hash, $expire);



    redirect(htmlspecialchars($_POST['redirect_url']), $lang_login['Login redirect']);

}





else if ($action == 'out')

{

    if ($pun_user['is_guest'] || !isset($_GET['id']) || $_GET['id'] != $pun_user['id'])

    {

        header('Location: index.php');

        exit;

    }



    // Remove user from "users online" list.

    $db->query('DELETE FROM '.$db->prefix.'online WHERE user_id='.$pun_user['id']) or error('Unable to delete from online list', __FILE__, __LINE__, $db->error());



    // Update last_visit (make sure there's something to update it with)

    if (isset($pun_user['logged']))

        $db->query('UPDATE '.$db->prefix.'users SET last_visit='.$pun_user['logged'].', read_topics=NULL WHERE id='.$pun_user['id']) or error('Unable to update user visit data', __FILE__, __LINE__, $db->error());



    pun_setcookie(1, random_pass(8), time() + 31536000);



    redirect('index.php', $lang_login['Logout redirect']);

}





else if ($action == 'forget' || $action == 'forget_2')

{

    if (!$pun_user['is_guest'])

        header('Location: index.php');



    if (isset($_POST['form_sent']))

    {

        require PUN_ROOT.'include/email.php';



        // Validate the email-address

        $email = strtolower(trim($_POST['req_email']));

        if (!is_valid_email($email))

            message($lang_common['Invalid e-mail']);



        $result = $db->query('SELECT id, username FROM '.$db->prefix.'users WHERE email=\''.$db->escape($email).'\'') or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());



        if ($db->num_rows($result))

        {

            // Load the "activate password" template

            $mail_tpl = trim(file_get_contents(PUN_ROOT.'lang/'.$pun_user['language'].'/mail_templates/activate_password.tpl'));



            // The first row contains the subject

            $first_crlf = strpos($mail_tpl, "\n");

            $mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));

            $mail_message = trim(substr($mail_tpl, $first_crlf));



            // Do the generic replacements first (they apply to all e-mails sent out here)

            $mail_message = str_replace('<base_url>', $pun_config['o_base_url'].'/', $mail_message);

            $mail_message = str_replace('<board_mailer>', $pun_config['o_board_title'].' '.$lang_common['Mailer'], $mail_message);



            // Loop through users we found

            while ($cur_hit = $db->fetch_assoc($result))

            {

                // Generate a new password and a new password activation code

                $new_password = random_pass(8);

                $new_password_key = random_pass(8);



                $db->query('UPDATE '.$db->prefix.'users SET activate_string=\''.pun_hash($new_password).'\', activate_key=\''.$new_password_key.'\' WHERE id='.$cur_hit['id']) or error('Unable to update activation data', __FILE__, __LINE__, $db->error());



                // Do the user specific replacements to the template

                $cur_mail_message = str_replace('<username>', $cur_hit['username'], $mail_message);

                $cur_mail_message = str_replace('<activation_url>', $pun_config['o_base_url'].'/profile.php?id='.$cur_hit['id'].'&action=change_pass&key='.$new_password_key, $cur_mail_message);

                $cur_mail_message = str_replace('<new_password>', $new_password, $cur_mail_message);



                pun_mail($email, $mail_subject, $cur_mail_message);

            }



            message($lang_login['Forget mail'].' <a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>.');

        }

        else

            message($lang_login['No e-mail match'].' '.htmlspecialchars($email).'.');

    }





    $page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_login['Request pass'];

    $required_fields = array('req_email' => $lang_common['E-mail']);

    $focus_element = array('request_pass', 'req_email');

    require PUN_ROOT.'header.php';



?>

<div class="blockform">

    <h2><span><?php echo $lang_login['Request pass'] ?></span></h2>

    <div class="box">
    
            

        <form id="request_pass" method="post" action="login.php?action=forget_2" onsubmit="this.request_pass.disabled=true;if(process_form(this)){return true;}else{this.request_pass.disabled=false;return false;}">

            <div class="inform">
            
                <fieldset>
                    <legend><?php echo $lang_login['Request pass legend'] ?></legend>

                    <div class="infldset">
<strong>Pour des raisons de sécurité, certains navigateurs (comme celui d'AOL ou Internet Exploerer) ne permettent pas l'utilisation du site Légal-Pirate.</br>Si vous ne parvenez pas à vous logger, il faudra utiliser un navigateur plus courant tel que <a href="http://www.getfirefox.com">Mozilla Firefox</a>.</br> Merci de votre compréhension.</strong>

                        <input type="hidden" name="form_sent" value="1" />

                        <input id="req_email" type="text" name="req_email" size="50" maxlength="50" />

                        <p><?php echo $lang_login['Request pass info'] ?></p>

                    </div>

                </fieldset>

            </div>

            <p><input type="submit" name="request_pass" value="<?php echo $lang_common['Submit'] ?>" /><a href="javascript:history.go(-1)"><?php echo $lang_common['Go back'] ?></a></p>

        </form>

    </div>

</div>

<?php



    require PUN_ROOT.'footer.php';

}





if (!$pun_user['is_guest'])

    header('Location: index.php');



// Try to determine if the data in HTTP_REFERER is valid (if not, we redirect to index.php after login)

$redirect_url = (isset($_SERVER['HTTP_REFERER']) && preg_match('#^'.preg_quote($pun_config['o_base_url']).'/(.*?)\.php#i', $_SERVER['HTTP_REFERER'])) ? htmlspecialchars($_SERVER['HTTP_REFERER']) : 'index.php';



$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_common['Login'];

$required_fields = array('req_username' => $lang_common['Username'], 'req_password' => $lang_common['Password']);

$focus_element = array('login', 'req_username');

require PUN_ROOT.'header.php';



?>

<div class="blockform">

    <h2><span><?php echo $lang_common['Login'] ?></span></h2>

    <div class="box">

        <form id="login" method="post" action="login.php?action=in" onsubmit="return process_form(this)">

            <div class="inform">
        
<center>
<strong>Pour des raisons de sécurité, certains navigateurs (comme celui d'AOL ou Internet Explorer) ne permettent pas l'utilisation du site Légal-Pirate.</br>Si vous ne parvenez pas à vous logger, il faudra utiliser un navigateur plus courant tel que <a href="http://www.getfirefox.com">Mozilla Firefox</a>.</br> Merci de votre compréhension.</strong>
</center>
                <fieldset>
<?php             
$question = array_keys($captcha_questions);
$qencoded = md5($question[$captcha_index]);
?>
                    <legend><?php echo $lang_login['Login legend'] ?></legend>

                        <div class="infldset">

                            <input type="hidden" name="form_sent" value="1" />

                            <input type="hidden" name="redirect_url" value="<?php echo $redirect_url ?>" />

                            <label class="conl"><strong><?php echo $lang_common['Username'] ?></strong><br /><input type="text" name="req_username" size="20" maxlength="25" tabindex="1" /><br /></label>

                            <label class="conl"><strong><?php echo $lang_common['Password'] ?></strong><br /><input type="password" name="req_password" size="15" maxlength="16" tabindex="2" /><br /></label>

                            <label class="conl"><strong><?php echo $question[$captcha_index] ?></strong><br /><input name="captcha" id="captcha" type="text" size="10" maxlength="10" /><input name="captcha_q" value="<?php echo $qencoded ?>" type="hidden" /><br /></label>

                            <p class="clearb"><?php echo $lang_login['Login info'] ?></p>

                            <p><a href="register.php" tabindex="4"><?php echo $lang_login['Not registered'] ?></a>  

                            <a href="login.php?action=forget" tabindex="5"><?php echo $lang_login['Forgotten pass'] ?></a></p>

                        </div>

                </fieldset>

            </div>


            
            <p><input type="submit" name="login" value="<?php echo $lang_common['Login'] ?>" tabindex="3" /></p>

        </form>

    </div>

</div>

<?php



require PUN_ROOT.'footer.php';

Thanx [ And sorry I'm French big_smile]