943,777 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2259
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Apr 8th, 2009
0

Re: Login System

I don't mean to sound rude, but I just want this question answered about blocking session ID stealing.
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

Well, to be honest when your asking if somethings secure it either is or it isnt. Theres no in the middle.

As another user said, there are however ways in which you can make your website 'safer'. Depending on the trafic of your website you should increase the amount of security you add to your code.
For example, I run a low traffic website for a sports team, and the only security is that of protecting against SQL injections (by clearing all inputed data).

To continue, it is therefore your choice wether or not you need this much security but daniweb provides answer, and the answers above are perfectly in context and should be appreciated. Sorry if it sounds blunt but people spend time writing posts to help others, not for the fun of it.

In an earlier comment you said you didnt understant CSRF.
In this context it would be validating a selfmade html form on a victims website.
In others words, creating a form with the same names as those of the website your hacking and then sending it to the website to be validated. Its a very sneakly thing and I suggest you look into it, google will help you there.

Hope all this helps
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

Click to Expand / Collapse  Quote originally posted by brechtjah ...
I don't mean to sound rude, but I just want this question answered about blocking session ID stealing.
The code I posted earlier would help prevent this. It will changed the session ID every single time the user clicks on a new link, so even if the malicious user somehow gets the session ID, it will likely have changed by the time they try to do something.

Here is the code again:
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 9th, 2009
0

Re: Login System

Click to Expand / Collapse  Quote originally posted by xan ...
The code I posted earlier would help prevent this. It will changed the session ID every single time the user clicks on a new link, so even if the malicious user somehow gets the session ID, it will likely have changed by the time they try to do something.

Here is the code again:
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. ?>
instead of all that, why not use session_regenerate_id()?

thats what i use.
Last edited by kkeith29; Apr 9th, 2009 at 2:06 am.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Apr 9th, 2009
0

Re: Login System

Ok, I will rewrite the whole code and upload it here once ready.
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Apr 10th, 2009
0

Re: Login System

session_regenerate_id() would be better in this case.
Reputation Points: 12
Solved Threads: 16
Posting Whiz
Designer_101 is offline Offline
314 posts
since Jul 2007
Apr 10th, 2009
0

Re: Login System

i agree with designer_101
Reputation Points: 16
Solved Threads: 48
Posting Whiz
BzzBee is offline Offline
327 posts
since Apr 2009
Apr 10th, 2009
0

Re: Login System

Ok this is the code I have now, I haven't fully checked it yet so there could be some errors in it, but I doubt this. What I'm more interested in is are the holes in the code for a user to hack it. Well... if it's quite secure, or if there are improvements to be made.
It works the same as before, with some changes. The function renew() should be called on every page load as suggested here.

login_BackEnd.php
php Syntax (Toggle Plain Text)
  1. <?php
  2. /*
  3. * Error Codes
  4. * 0: Success
  5. * 1: User does not exist in DB
  6. * 2: User is already logged in
  7. * 3: Hash in form is not equal to server side created hash
  8. * 4: An error occured while creating the session in the DB
  9. */
  10. session_start();
  11. srand(time());
  12.  
  13. if(!isset($_SESSION['RND'])) {
  14. $_SESSION['RND'] = sha1(rand()%1000001);
  15. }
  16.  
  17. if(!isset($_SESSION['authINF1'], $_SESSION['authINF2'], $_SESSION['authINF3'])) {
  18. $_SESSION['authINF1'] = sha1($_SERVER['HTTP_USER_AGENT']);
  19. $_SESSION['authINF2'] = sha1($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  20. $_SESSION['authINF3'] = sha1($_SERVER['REMOTE_ADDR']);
  21. }
  22.  
  23. function authenticate() {
  24. if(!isset($_SESSION['conSession'], $_SESSION['chSession'])) {
  25. // Get information from form
  26. $username = htmlentities($_POST['username'], ENT_QUOTES);
  27. $hash = htmlentities($_POST['hash'], ENT_QUOTES);
  28.  
  29. // Create salt for hash
  30. $salt = htmlentities($_SESSION['RND'], ENT_QUOTES);
  31. $_SESSION['RND'] = sha1(rand()%1000001);
  32. $salt .= htmlentities($_SERVER['REMOTE_ADDR'], ENT_QUOTES);
  33. $salt .= htmlentities($_SERVER['HTTP_USER_AGENT'], ENT_QUOTES);
  34.  
  35. $qGetUser = @mysql_query("SELECT * FROM users WHERE gebruikersnaam='".$username."'");
  36. if(@mysql_num_rows($qGetUser) == 1) {
  37.  
  38. // The user exists in the DB
  39. $aGetUser = @mysql_fetch_assoc($qGetUser);
  40. $qGetSession = @mysql_query("SELECT * FROM sessions WHERE gebruikersnaam='".$username."'");
  41. if(@mysql_num_rows($qGetSession) == 0) {
  42.  
  43. // The user is not logged in yet
  44. $serverSideHash = sha1($aGetUser['wachtwoord'].$salt);
  45. if($serverSideHash == $hash) {
  46.  
  47. // The submitted hash and the server side created one are equal
  48. $chSession = sha1(rand()%1000001);
  49. if(@mysql_query("INSERT INTO sessions(gebruikersnaam, conSessie, chSessie, sessieTijd) VALUES('".$username."', '".$serverSideHash."', '".$chSession."', ".time().")")) {
  50.  
  51. // The session has been created
  52. $_SESSION['conSession'] = $serverSideHash;
  53. $_SESSION['chSession'] = $chSession;
  54. $err = 0;
  55. }
  56. else {
  57. $err = 4;
  58. }
  59. }
  60. else {
  61. $err = 3;
  62. }
  63. }
  64. else {
  65. $err = 2;
  66. }
  67. }
  68. else {
  69. $err = 1;
  70. }
  71. }
  72. return $err;
  73. }
  74.  
  75. function renew() {
  76. deleteOldSessions();
  77. session_regenerate_id(TRUE);
  78. $conSession = htmlentities($_SESSION['conSession'], ENT_QUOTES);
  79. $chSession = htmlentities($_SESSION['chSession'], ENT_QUOTES);
  80. $qGetSession = @mysql_query("SELECT * FROM sessions WHERE conSessie='".$conSession."' AND chSessie='".$chSession."'");
  81.  
  82. if(@mysql_num_rows($qGetSession) == 1) {
  83. $aGetSession = @mysql_fetch_assoc($qGetSession);
  84. if($chSession == $aGetSession['chSessie']) {
  85. $chSession = sha1((rand()%1000001).$chSession);
  86. $_SESSION['chSession'] = htmlentities($chSession, ENT_QUOTES);
  87. @mysql_query("UPDATE sessions SET chSessie='".$chSession."', sessieTijd=".time()."");
  88. }
  89. }
  90. }
  91.  
  92. function destroy() {
  93. $conSession = htmlentities($_SESSION['conSession'], ENT_QUOTES);
  94. $chSession = htmlentities($_SESSION['chSession'], ENT_QUOTES);
  95. $qGetSession = @mysql_query("DELETE FROM sessions WHERE conSessie='".$conSession."' AND chSessie='".$chSession."'");
  96. session_unset();
  97. session_destroy();
  98. }
  99.  
  100. function deleteOldSessions() {
  101. $inactivityTime = 60*5;
  102. $expirationTime = time() - $inactivityTime;
  103.  
  104. if(isset($_SESSION['conSession']) && isset($_SESSION['chSession'])) {
  105. $conSession = htmlentities($_SESSION['conSession'], ENT_QUOTES);
  106. $chSession = htmlentities($_SESSION['chSession'], ENT_QUOTES);
  107. $qGetSession = @mysql_query("SELECT * FROM sessions WHERE conSessie='".$conSession."' AND chSessie='".$chSession."' AND sessieTijd<".$expirationTime."");
  108. $aGetSession = @mysql_fetch_assoc($qGetSession);
  109. if(@mysql_num_rows($qGetSession) == 1) {
  110. destroy();
  111. }
  112. }
  113. @mysql_query("DELETE FROM sessions WHERE sessieTijd<".$expirationTime."");
  114. }
  115. ?>

login.php
php Syntax (Toggle Plain Text)
  1. <?php
  2. include_once("login_BackEnd.php");
  3. include_once("connect.php");
  4. deleteOldSessions();
  5.  
  6. if(isset($_POST['logIn'])) {
  7. $response = authenticate();
  8. switch($response) {
  9. case 0:
  10. $msg = "Succes";
  11. $type = "notification";
  12. break;
  13. default:
  14. $msg = $response;
  15. $type = "information";
  16. break;
  17. }
  18. }
  19.  
  20. if(isset($_SESSION['conSession'], $_SESSION['chSession'])) {
  21. if((sha1($_SERVER['HTTP_USER_AGENT']) == $_SESSION['authINF1']) && (sha1($_SERVER['HTTP_ACCEPT_LANGUAGE']) == $_SESSION['authINF2']) && (sha1($_SERVER['REMOTE_ADDR']) == $_SESSION['authINF3'])) {
  22. $conSession = htmlentities($_SESSION['conSession'], ENT_QUOTES);
  23. $chSession = htmlentities($_SESSION['chSession'], ENT_QUOTES);
  24. $qGetSession = @mysql_query("SELECT * FROM sessions WHERE conSessie='".$conSession."' AND chSessie='".$chSession."'");
  25. $aGetSession = @mysql_fetch_assoc($qGetSession);
  26. if(@mysql_num_rows($qGetSession) == 1) {
  27. $msg = "U bent ingelogd als ".$aGetSession['gebruikersnaam'];
  28. $type = "notification";
  29. renew();
  30. }
  31. }
  32. }
  33. ?>
  34. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  35. <html>
  36. <head>
  37. <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
  38. <title>Vermeersch Constructie</title>
  39. <script type="text/javascript" src="MooTools_Functions.js"></script>
  40. <script type="text/javascript" src="MooTools_BackEnd.js"></script>
  41. <!--[if lt IE 7.]>
  42. <script defer type="text/javascript" src="pngfix.js"></script>
  43. <![endif]-->
  44. <link rel="stylesheet" href="style.css" type="text/css">
  45. <script type="text/javascript" src="sha1.js"></script>
  46. <script type="text/javascript">
  47. function hashIt() {
  48. var salt = "<?php echo htmlentities($_SESSION['RND'], ENT_QUOTES); ?>";
  49. salt += "<?php echo htmlentities($_SERVER['REMOTE_ADDR'], ENT_QUOTES); ?>";
  50. salt += "<?php echo htmlentities($_SERVER['HTTP_USER_AGENT'], ENT_QUOTES); ?>";
  51. pass = document.getElementById('password').value;
  52.  
  53. document.getElementById('password').value = "";
  54. document.getElementById('hash').value = hex_sha1(hex_sha1(pass)+salt);
  55. }
  56. </script>
  57. </head>
  58.  
  59. <body>
  60. <div class="header"></div>
  61. <div class="container">
  62. <?php
  63. if(!empty($msg)) {
  64. showMsg($msg, $type);
  65. $msg = null;
  66. $type = null;
  67. }
  68. ?>
  69. <form method="post" action="" onSubmit="hashIt();">
  70. <table>
  71. <tr>
  72. <td>Gebruikersnaam:</td><td><input type="text" name="username"></td>
  73. </tr>
  74. <tr>
  75. <td>Wachtwoord:</td><td><input type="password" id="password"></td>
  76. </tr>
  77. <tr>
  78. <td>&nbsp;</td><td style="text-align: right;"><input type="submit" name="logIn" value="Aanmelden"></td>
  79. </tr>
  80. </table>
  81. <input type="hidden" name="hash" id="hash">
  82. </form>
  83. </div>
  84. <div class="footer"><div style="padding: 6px;">&copy; Debaere Brecht</div></div>
  85. </body>
  86. </html>
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Apr 10th, 2009
0

Re: Login System

why are you using javascript at all in the login process? I mean, only use it for basic validation not hashing. That in itself is a security flaw because attackers can see how you are encrypting a password, which helps them to crack it.

What if a user has javascript turned off? Then what. You should have php handle everything.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Apr 10th, 2009
0

Re: Login System

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
why are you using javascript at all in the login process? I mean, only use it for basic validation not hashing. That in itself is a security flaw because attackers can see how you are encrypting a password, which helps them to crack it.

What if a user has javascript turned off? Then what. You should have php handle everything.
I agree JavaScript is more efficient (for the users) but PHP should always be your focus as it is loaded before the hacker gets the page.

Always make sure with important scripts that you make sure that the refering URL is the the page you wish it to come from.
Reputation Points: 31
Solved Threads: 27
Unverified User
Josh Connerty is offline Offline
342 posts
since Apr 2009

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