Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

login.php - I can login with my password capitalized?

I can log in with info such as Ben:test however I can also log in with Ben:TEST. I have posted my login.php below. Why can people log in with their password in caps?

Code
<?php
    session_start();
    // Starting Session
    $error='';
    // Variable To Store Error Message
    
    if (isset($_POST['submit'])) {
        
        if (empty($_POST['username']) || empty($_POST['password'])) {
            $error = "Username or Password empty";
            echo $error;
            //include('login_forms.html');
            //header("location: login_retry.php?error=" . urlencode($error));
            header("location: /ss/error/");
        } else {
            // Define $username and $password
            $username=$_POST['username'];
            //Check
            $password=$_POST['password'];
            // Establishing Connection with Server by passing server_name, user_id and password as a parameter
            $connection = mysql_connect("localhost", "root", "");
            // To protect MySQL injection for Security purpose
            $username = stripslashes($username);
            $password = stripslashes($password);
            $username = mysql_real_escape_string($username);
            $password = mysql_real_escape_string($password);
            // Selecting Database
            $db = mysql_select_db("test", $connection);
            // SQL query to fetch information of registerd users and finds user match.
            $query = mysql_query("select * from users where password='$password' AND username='$username'", $connection);
            $rows = mysql_num_rows($query);
            
            if ($rows == 1) {
                $_SESSION['login_user']=$username;
                // Initializing Session
                #header("location: /ss/login/profile.php"); // Redirecting To Other Page
                header("location: /ss/user/");
                // Redirecting To Other Page
            } else {
                $error = "Username or Password is invalid";
                echo $error;
                //header("location: login_retry.php?error=" . urlencode($error));
                header("location: /ss/error/");
            }

            mysql_close($connection);
            // Closing Connection
        }

    }

    ?>


I also tried removing:

Code
            $username = stripslashes($username);
            $password = stripslashes($password);
            $username = mysql_real_escape_string($username);
            $password = mysql_real_escape_string($password);

That has no effect on it.


EDIT: I fixed it using BINARY in my query. Why does this work?

The edit I made:

Code
$query = mysql_query("select * from users where BINARY password = BINARY '$password' AND BINARY username= BINARY '$username'", $connection);
1) for user logins - password saving you MUST use hashing. STRONGLY NOT RECOMMENDED TO NOT USE IT

2) STOP using mysql, it's bad practice as it's deprecated and in php 7 removed.
USE: mysqli OR PDO instead of this.

3) DON'T use nonbinary string in BINARY assigned tables please educate yourself further by the following document refference: http://dev.mysql.com/doc/refman/5.7/en/b...inary.html

4) Use prepared statements and leave the escaping bullshit like that alone.
Side note: Dude you really shouldn't be using the deprecated mysql_* API. Switching to mysqli_* is very easy and in most cases just requires changing mysql_ to mysqli_
(Nov 24, 2015 03:39 PM)ItsUzi Wrote:  Side note: Dude you really shouldn't be using the deprecated mysql_* API. Switching to mysqli_* is very easy and in most cases just requires changing mysql_ to mysqli_

just changing mysql_ to mysqli_.
Doesn't make a lot of sense.
Changing mysql_ with mysqli_ and prepared statements would.
Which is number 4 in my post:
4) Use prepared statements and leave the escaping bullshit like that alone.
Being able to login with your password case insensitive is a bad idea. I'm sure if you hashed your password with a good hashing algorithm that you wouldn't get this error.
(Nov 24, 2015 03:43 PM)+Haze Wrote:  Being able to login with your password case insensitive is a bad idea. I'm sure if you hashed your password with a good hashing algorithm that you wouldn't get this error.

That you didn't comment upon his "binary" usage. Haze I expected more from you than tackeling one of the many issues :P😭
You don't appear to be comparing the users data with the stored data. try doing something like the following.
(And like stated above, you should use MySQLi prepared statements or PDO but this will give you a quick idea of checking stuff... Oh, use hashing too. google for "php password_hash()" and "php password_verify()" to get started on some hashing)

$SQL = mysqli_query($con," SELECT password FROM users WHERE username='$_POST['username']' ");
$result = mysqli_fetch_assoc($SQL);

if($result['password'] === $_POST['password']){
// log the user in
}else{
// invalid username/password combination
}
(Nov 24, 2015 03:46 PM)hellomen Wrote:  That you didn't comment upon his "binary" usage. Haze I expected more from you than tackeling one of the many issues :P😭

😏😏
Yes there are many things wrong with his code, but I thought I would be more useful if I got straight to the point and helped op solve his problem.


(Nov 24, 2015 03:54 PM)Nosymbols Wrote:  You don't appear to be comparing the users data with the stored data. try doing something like the following.
(And like stated above, you should use MySQLi prepared statements or PDO but this will give you a quick idea of checking stuff... Oh, use hashing too. google for "php password_hash()" and "php password_verify()" to get started on some hashing)

$SQL = mysqli_query($con," SELECT password FROM users WHERE username='$_POST['username']' ");
$result = mysqli_fetch_assoc($SQL);

if($result['password'] === $_POST['password']){
// log the user in
}else{
// invalid username/password combination
}

You suggest prepared statements yet you don't use them? And you suggest password hashing yet you don't make use of it result in your demonstration?
(Nov 24, 2015 04:00 PM)+Haze Wrote:  😏😏
Yes there are many things wrong with his code, but I thought I would be more useful if I got straight to the point and helped op solve his problem.



You suggest prepared statements yet you don't use them? And you suggest password hashing yet you don't make use of it result in your demonstration?

OP asked a question, I provided an answer with an example relevant to his/her skill level. Nothing wrong with that is there?
(Nov 24, 2015 04:09 PM)Nosymbols Wrote:  OP asked a question, I provided an answer with an example relevant to his/her skill level. Nothing wrong with that is there?

Yeah there's nothing wrong with teaching people bad programming habits. Sorry I almost forgot that.
(Nov 24, 2015 03:43 PM)+Haze Wrote:  Being able to login with your password case insensitive is a bad idea. I'm sure if you hashed your password with a good hashing algorithm that you wouldn't get this error.

Difference between hashing and encrypting :p
(Nov 24, 2015 05:09 PM)ItsUzi Wrote:  Difference between hashing and encrypting :p

No.

You can still compare them case-sensitively when encrypting instead of hashing.
BTC: 1PQFRwoyRUTCWMC7anLdVxTp4t22MVoos2
(Nov 24, 2015 04:09 PM)Nosymbols Wrote:  OP asked a question, I provided an answer with an example relevant to his/her skill level. Nothing wrong with that is there?

No, you didn't really provide an answer, since your answer won't work (the script part). First of all the query itself is messed up (from the perspective of PHP syntax), also the query is vulnerable to SQLi. And yes, you mention prepared statements, yet you don't utilize it... why not? Also you mention the password_ functions and your script is ready to use it, but you don't use it. To mention something positive: you didn't query all the columns, instead only the relevant one, which is resource-wise more efficient than just be lazy and use the "SELECT * FROM..." version. 👍

PS: HF has [php] and [code] tags.
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
(Nov 24, 2015 10:30 PM)Scy7e Wrote:  No.

You can still compare them case-sensitively when encrypting instead of hashing.

You're mistaken sir, I'm talking about the difference between a hashing algorithm like SHA, MD5 etc vs an encryption algorithm like BCrypt (Blowfish), AES etc. Using hashing algorithms for password management would be absurd.
(Nov 25, 2015 04:39 AM)ItsUzi Wrote:  You're mistaken sir, I'm talking about the difference between a hashing algorithm like SHA, MD5 etc vs an encryption algorithm like BCrypt (Blowfish), AES etc. Using hashing algorithms for password management would be absurd.
PLZ: http://php.net/manual/en/function.crypt.php
'nuffsaid..
Because the method using is called crypt doesn't mention it isn't a hashing algorithm as you said...
crypt() is a ONE WAY STRING HASHING METHOD [one way encryption, it's also called]
bcrypt AKA Blowfish Crypt AKA CRYPT_BLOWFISH: Blowfish hashing with a salt as follows: "$2a$", "$2x$" or "$2y$", a two digit cost parameter, "$", and 22 characters from the alphabet "./0-9A-Za-z".

Hmmm...

Reason of hashing algorithm are used for passwords:
Passwords shall be limited into known by the end-users, not by anyone else managing a password.
When using the word "Password" You're mentioning to "A WORD PASSing access to a private area".
In other words, no one beyond the end-users shall know what password they use.
On that point we come accross the ONE-WAY encryption A.K.A hashing.
We want a generally "slow" encryption method algorithm whom can only encrypt but not decrypt. A.K.A Hashing.
This method we make sure, no one can actually leak the actual passwords without bruteforcing or rainbow tabling.
BCrypt has a cost range which slows the hashing speed down, basical wise then.
The higher the cost is, the slower your hash would be returned.

Now what if we saved the passwords with a simple encryption in the database?
-LOOK AN SQLI! DUMPED USERS TABLE
-DECRYPTING PASSWORDS WIN
This is what we want to make harder thus we use a password hashing method such as blowfish.

-LOOK AN SQLI~ DUMPED USERS TABLE
-CRAP ITS HASHED WITH HIGH COST BLOWFISH
-FUCK IT TAKES AGES TO BRUTEFORCE THESE PASSWORDS I AM OUT

This is a really basic example of what may happen, ofcourse if people dump your database they won't give up till they have all or just a few passwords but still, it's a basic point of view.

BCRYPT saves the hash as: prefix-cost-postfix-salt-weird_crap [this is how most people look against it]
So what if we take the hash from the DB.
take the prefix-cost-postfix from the hash
then take the salt from the hash and hash it again with the method, but now it would return the exact same hash if it's correct...
This is how pw_verify in the password_* works.
It looks to the hash.
Takes out the prefix-cost-postfix
takes out the salt.
hash the password.
Returns false upon failure, true upon success.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by Dark AngelsApr 22, 2017 06:56 AM 4 338 Apr 22, 2017 06:56 AM
Last Post: taz3r
[PHP] by Pl0x_Dec 8, 2016 03:55 AM 11 293 Dec 8, 2016 03:55 AM
Last Post: Pl0x_
[PHP] by magiC*Jun 22, 2013 07:09 AM 2 239 Jun 22, 2013 07:09 AM
Last Post: 0x42