943,855 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2259
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 8th, 2009
0

Login System

Expand Post »
Hi,
I wanted to post my login system I will use for an upcoming site for rating. I want to ensure a safe login, so please, if you know anything about this and see a security leak somewhere... Please post, any remarks are welcome.

How it works: the script generates a random number if the form hasn't been submitted yet. This number is being passed to the Javascript also. On submitting of the form the javascript creates a hash and empties the password field. The hash includes: IP + hashed password + random number. The server recreates this and destroys the session with the random number *. The two hashes are compared and a decision is made.
* I've been thinking of putting the random number in the database and an id in the session, then the random number is pulled of the database. However, I'm not so experienced in login systems and don't know what information can be corrupted.

NOTE: the script will also log who is currently logged in, I still have to code that part, but I couldn't wait to upload this here.
EDIT: I will include a script that will only allow for so much login attempts in a certain time span to exclude bots. Or shouldn't I?
php Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3. include_once("connect.php");
  4.  
  5. if(isset($_POST['logIn'])) {
  6. $RND = $_SESSION['RND'];
  7. session_destroy();
  8. $IP = $_SERVER['REMOTE_ADDR'];
  9.  
  10. $qGetUser = @mysql_query("SELECT * FROM users WHERE gebruikersnaam='".$_POST['username']."'");
  11. if(@mysql_num_rows($qGetUser) == 1) {
  12. $aGetUser = @mysql_fetch_assoc($qGetUser);
  13. $serverHash = sha1(($IP.$aGetUser['wachtwoord'].$RND));
  14.  
  15. if($serverHash == $_POST['hash']) {
  16. $msg = "NICE!";
  17. $type = "notification";
  18. }
  19. else {
  20. $msg = "fail :( serverHash: ".$serverHash." ; clientHash: ".$_POST['hash']." ; wachtw: ".$aGetUser['wachtwoord'];
  21. $type = "error";
  22. }
  23. }
  24. else {
  25. $msg = "De ingevoerde gebruikersnaam is ongeldig.";
  26. $type = "error";
  27. }
  28. }
  29. else {
  30. $_SESSION['RND'] = getRandomNumber();
  31. }
  32.  
  33. function getRandomNumber() {
  34. srand(time());
  35. return (rand()%1000001);
  36. }
  37. ?>
  38. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  39. <html>
  40. <head>
  41. <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
  42. <title>Vermeersch Constructie</title>
  43. <script type="text/javascript" src="MooTools_Functions.js"></script>
  44. <script type="text/javascript" src="MooTools_BackEnd.js"></script>
  45. <!--[if lt IE 7.]>
  46. <script defer type="text/javascript" src="pngfix.js"></script>
  47. <![endif]-->
  48. <link rel="stylesheet" href="style.css" type="text/css">
  49. <script type="text/javascript" src="sha1.js"></script>
  50. <script type="text/javascript">
  51. function hashIt() {
  52. var password = document.getElementById('password').value;
  53. var ip = document.getElementById('ip').value;
  54. var randomnumber = <?php echo $_SESSION['RND']; ?>;
  55.  
  56. document.getElementById('password').value = "";
  57. document.getElementById('hash').value = hex_sha1((ip + hex_sha1(password) + randomnumber));
  58. }
  59. </script>
  60. </head>
  61.  
  62. <body>
  63. <div class="header"></div>
  64. <div class="container">
  65. <?php
  66. if(!empty($msg)) {
  67. showMsg($msg, $type);
  68. $msg = null;
  69. $type = null;
  70. }
  71. ?>
  72. <form method="post" action="" onSubmit="hashIt();">
  73. <table>
  74. <tr>
  75. <td>Gebruikersnaam:</td><td><input type="text" name="username"></td>
  76. </tr>
  77. <tr>
  78. <td>Wachtwoord:</td><td><input type="password" id="password"></td>
  79. </tr>
  80. <tr>
  81. <td>&nbsp;</td><td style="text-align: right;"><input type="submit" name="logIn" value="Aanmelden"></td>
  82. </tr>
  83. </table>
  84. <input type="hidden" name="ip" id="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">
  85. <input type="hidden" name="hash" id="hash">
  86. </form>
  87. </div>
  88. <div class="footer"><div style="padding: 6px;">&copy; Debaere Brecht</div></div>
  89. </body>
  90. </html>

Thanks , please post comments on it
Last edited by brechtjah; Apr 8th, 2009 at 10:47 am.
Similar Threads
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Apr 8th, 2009
0

Re: Login System

Thats a really nice script. However what if the user has javascript turned off? Some of your validation is in javascrit therefore a login would be imossible.

Use <noscript> to let the user know their login wont be sucessfull unless they turn it on. Also, are you cleaning your strings from quotes and other characters? I cant really see if you are?

On the other hand, this is a really nice method. It works against CSRF and XXS therefore is really safe. I'm copying the code for personal use so thankyou

Does this help ?
Reputation Points: 12
Solved Threads: 16
Posting Whiz
Designer_101 is offline Offline
314 posts
since Jul 2007
Apr 8th, 2009
0

Re: Login System

Thats a really nice script. However what if the user has javascript turned off? Some of your validation is in javascrit therefore a login would be imossible.

Use <noscript> to let the user know their login wont be sucessfull unless they turn it on. Also, are you cleaning your strings from quotes and other characters? I cant really see if you are?

On the other hand, this is a really nice method. It works against CSRF and XXS therefore is really safe. I'm copying the code for personal use so thankyou

Does this help ?
Oh I should indeed implement htmlentities use, but...
What do you mean with CSRF and XXS? What are those things? Googling those things gives me other results than I need
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Apr 8th, 2009
0

Re: Login System

CSRF: Cross-site request forgery
XXS: Cross-site Scripting

As mentioned by Designer_101, I would suggest not using POST values directly into SQL queries (Or for anything really) They should be cleaned first. Use a preg_match on any values (such as usernames) where you know they will only contain certain characters, at a minimum you should have addslashes in there.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Apr 8th, 2009
0

Re: Login System

I am kinda stuck now on how I would make one user logged in over multiple pages, however leaving hackers out. I can't really use sessions because they can be hacked no?
Can I have some assist on this please, then I can complete the script and I'll paste it here

Thanks
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Apr 8th, 2009
0

Re: Login System

You will not make it 100% secure, no matter what you do.

But you can take some steps to secure your application further. Sessions are much more secure than cookies and are probably the best way to go for this, you can look at making it harder to 'hack':

Check the User Agent for each visit, while the user agent reported can be masked or changed by the user, checking it for each page load will stop some attempts, so if the user agent suddenly changes mid session, this will stop it:
php Syntax (Toggle Plain Text)
  1. <?php
  2. if(!isset($_SESSION['user_agent'])) {
  3. // Set the session value as the hash of the UA
  4. $_SESSION['user_agent'] = md5($_SERVER['HTTP_USER_AGENT']);
  5. } else {
  6. // Check that the session value matches the hash of the UA
  7. if($_SESSION['user_agent'] != md5($_SERVER['HTTP_USER_AGENT'])) {
  8. // Alert the user they have been logged out due to a UA change
  9. echo "The user agent data sent by your browser has changed unexpectedly, please login again.";
  10. session_destroy();
  11. exit(0);
  12. }
  13. }
  14. ?>
The same (or similar) could be done for other values such as the IP address, but since some users will be behind proxies, and the IP can change mid session, this may not be such a good idea.

Also, to make it harder for a malicious user, you can change the session key every page load:
php Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3. // We need to copy the old session data
  4. $previousSession = $_SESSION;
  5.  
  6. // Then re-create a new session
  7. session_destroy();
  8. session_start();
  9.  
  10. // And finally, reassign the session data
  11. $_SESSION = $previousSession;
  12. ?>
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Apr 8th, 2009
0

Re: Login System

Excuse me, I'm not so familiar with sessions. I don't know anything about session keys.
If I put the hash in a session and compare it to the hash in the database. Then it is possible for a hacker to pose himself like the user by somehow stealing the session, yes? How would I go to prevent this? Or isn't this possible?
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Apr 8th, 2009
0

Re: Login System

Seeing as all the session values are stored on the server, unlike cookies which are stored on the clients computer, they are much more secure anyway.

In theory if the malicious user got the session key they may be able to do some things, but this will be prevented to a certain extent by using the script above to change the session key every page load.

Also, make sure your logout button/link is easy to see, as by clicking this the session data should be deleted by the script meaning that it can no longer be accessed.
Last edited by Will Gresham; Apr 8th, 2009 at 4:31 pm.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Apr 8th, 2009
0

Re: Login System

Click to Expand / Collapse  Quote originally posted by xan ...
Seeing as all the session values are stored on the server, unlike cookies which are stored on the clients computer, they are much more secure anyway.

In theory if the malicious user got the session key they may be able to do some things, but this will be prevented to a certain extent by using the script above to change the session key every page load.

Also, make sure your logout button/link is easy to see, as by clicking this the session data should be deleted by the script meaning that it can no longer be accessed.
Isn't it possible to check if the hacker tries to inject a session id in the URI by using GET? Like this:
php Syntax (Toggle Plain Text)
  1. if($_GET['PHPSESSID'] != null) {
  2. // A hacker is trying to inject a session ID
  3. }

?
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Apr 8th, 2009
0

Re: Login System

Here is a good example of a secure login system:

http://www.daniweb.com/forums/thread183049.html

With proper implementation of sessions, the possibility of hacking them goes down drastically.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Need help to Delete all button for all checkboxes...
Next Thread in PHP Forum Timeline: timeout if page is inactive





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC