r/PHPhelp • • Aug 19 '17

Code Review: Login form

Working to expand my PHP skills and move to more advanced development.

Here is an experiment I did with regards to a login form where instead of using a username in addition to a password, it is set up where the site will send a randomized authorization token upon successful matching of the email address $admin_email.

The code works as intended but wanted to know if the code has any flaws that would make it vulnerable on a production environment.

https://pastebin.com/uDnrnhTr

Thanks in advance!

7 Upvotes

4 comments sorted by

6

u/metavulp Aug 19 '17

So there is a few major issues in this code from the perspective of security - which since this is a login form, security is very important.

Based on your example - I am going to assume you only have one user that will ever log in, and that you are not using a database to handle users. If that's not the case and you plan to use a database there are some other considerations.

Let me see if I can work my way down your code...

$hash = hash('sha512', $_SERVER["PHP_SELF"] . date("h:i"));

This is the first issue and the main reason this login form is insecure. You need to generate a random token from a cryptographically secure pseudo-random number generator (like CSPRNG). This makes it so that people can't guess your token and bypass the whole thing. All i need to guess $hash is the url and the current time in 1 minute intervals.

Also: if you don't want to spend 2 minutes guessing the hash as an attacker, you could just look at the login form since you put the $hash right in the plain text of the form. '<input type="text" name="key" id="key" value="'. $hash .'">'

Let's skip ahead to where $hash is used, the creation of the login $token.

// Create token (Using IP, email & Hash)
$token = hash('sha512', $_SERVER["PHP_SELF"] . $admin_email . $hash);

Your comment implies you are using the IP, but $_SERVER["PHP_SELF"] returns the filename of the currently executing script not the ip of the client. We know the url, and $hash so now all we need is to guess your email and we can break in.

So how do you fix this. first, make a $token from something not guessable. Assume everything is stolen from this article and this article which you might want to take a look at.

// generate a random token
function generateToken($length = 20)
{
    return bin2hex(random_bytes($length));
}

my best guess at why you wanted to use $hash in the email login form was as a CSRF Token - I'll get back to you on that one.

$_SESSION['token'] = generateToken();

Since you do not store the $token in a session

Now its alot harder to guess. but you might want to do something about brute forcing. such as kill the token after too many attempts. You should also consider making the session expire or other things referenced here

1

u/dwbg Aug 19 '17

Thanks! Very helpful!!

2

u/lucasjkr Aug 20 '17

I would add a few other comments just in general.

There's no need to embed all that HTML code in your PHP functions. It will make the design much more complicated, having HTML interspersed with your functions.

Move all the html to your design/html/template files, whatever you want to call them. It's fine to echo variables into them, but that's all you want to do. The functions are logic not design, so your form design shouldn't be embedded in there.

Advancing beyond that, it looks like you're intending to add both the rest of your page html, and more functions into this single file. Don't. Have one (at least) file with your functions, and include that in the the rest of the pages on your site. Again, for separation and simplicity.

1

u/dwbg Aug 22 '17

Yeah I was thinking about that as well. Many Thanks!