[TUT] Login systeem MySQL extra beveiliging

Gestart doorMounssif15 reacties72 weergaven

16 berichten · pagina 1 van 2

Mounssif

Lid · Topicstarter

Berichten
1.284
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Credits naar Erik-Jan
Hieronder staat een eenvoudig ogend login systeem met php en MySQL.

Er is echter wat onzichtbare extra beveiliging toegevoegd ten opzichte van wat normaal op de verschillende sites wordt gevonden.

Heeft er nog iemand ideeën om het geheel (nog) veiliger te maken en heeft iemand suggesties?

Features:

- controle van browser, ip en poort en bij ieder bezoek aan een pagina en wisselende sessie-id en wisselende random code (tegen session hijacking)
- (beperkte) bescherming tegen SQL-injection en file injection
- keuze uit sessiegegevens in MySQL-database of in cookie
- toegang op verschillende niveaus
- bescherming tegen verzenden van plain password
- gecodeerde gegevens in database, dus brute force nodig om username/password en andere gegevens achterhalen
- check voor https

Potentiële nadelen

- Vrij zwaar bij veel gebruikers
- Over the top

Getest met

- Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
- MySQL client version: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $
_______________________________________________________________________________
Totale scripts:

"¢ database.php
"¢ index.php
"¢ index2.php
"¢ sessions.php
"¢ sha1.js
"¢ database.sql
______________

Database.php

[COLOR= #0000BB]<?php[/COLOR]

    [URL="http://www.php.net/mysql_connect"]mysql_connect[/URL] [COLOR= #007700]([/COLOR][COLOR= #DD0000]"localhost"[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]"login"[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]"login"[/COLOR][COLOR= #007700])[/COLOR] or [URL="http://www.php.net/exit"]exit[/URL][COLOR= #007700]();[/COLOR] 
    [URL="http://www.php.net/mysql_select_db"]mysql_select_db[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"login"[/COLOR][COLOR= #007700])[/COLOR] or [URL="http://www.php.net/exit"]exit[/URL][COLOR= #007700]();[/COLOR] 

?>


Index.php

[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/error_reporting"]error_reporting[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #FF8000]/* OPTIONAL: HANDLE SECTIONS THROUGH MYSQL + CHECK IF HTTPS IS USED */[/COLOR]

    [URL="http://www.php.net/include"]include[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"sessions.php"[/COLOR][COLOR= #007700]);[/COLOR]
    
    [COLOR= #FF8000]//check if HTTPS IS USED
    //if ($_SERVER[SERVER_PORT] != 443) { header(Location:https://www.abc.com/login/); exit; } 

/* OPTIONAL: CLEAN UP LOGIN TABLE */
    
    //connect to database (not required if MySQL-sessions are used)[/COLOR]
    [URL="http://www.php.net/require_once"]require_once[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"database.php"[/COLOR][COLOR= #007700]);[/COLOR]
    
    [COLOR= #FF8000]//remove session_id which are no longer used from the login table[/COLOR]
    [COLOR= #0000BB]$sql[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]" DELETE FROM `login` WHERE `session_id` NOT IN (SELECT `id` FROM `sessions`) "[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$sql[/COLOR][COLOR= #007700])[/COLOR] or [URL="http://www.php.net/die"]die[/URL][COLOR= #007700]();[/COLOR]

[COLOR= #FF8000]/* OPTIONAL: PROTECTION AGAINST CODE INJECTION */
    
    //list of valid pages[/COLOR]
    [COLOR= #0000BB]$pages[/COLOR][COLOR= #007700]=[/COLOR][URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"/login/index.php"[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]"/login/index.php?task=logout"[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]( ![/COLOR][URL="http://www.php.net/in_array"]in_array[/URL][COLOR= #007700]([/COLOR]htmlspecialchars[COLOR= #007700]([/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]REQUEST_URI[/COLOR][COLOR= #007700]]),[/COLOR][COLOR= #0000BB]$pages[/COLOR][COLOR= #007700])) {[/COLOR]
        
    [COLOR= #FF8000]//if the case reload clean page    [/COLOR]
    [URL="http://www.php.net/header"]header[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"Location:index.php"[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/exit"]exit[/URL][COLOR= #007700];

    }[/COLOR]
        
[COLOR= #FF8000]/* LOGOUT SECTION */[/COLOR]

[URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]task[/COLOR][COLOR= #007700]])[/COLOR] AND [URL="http://www.php.net/ctype_alnum"]ctype_alnum[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]task[/COLOR][COLOR= #007700]])[/COLOR] AND [COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]task[/COLOR][COLOR= #007700]] ==[/COLOR] [COLOR= #DD0000]logout[/COLOR][COLOR= #007700])
{[/COLOR]
    [COLOR= #FF8000]//start the session to be closed[/COLOR]
    [URL="http://www.php.net/session_start"]session_start[/URL][COLOR= #007700]();[/COLOR]
    
    [COLOR= #FF8000]//destroy the session[/COLOR]
    [URL="http://www.php.net/session_destroy"]session_destroy[/URL][COLOR= #007700]();
}[/COLOR]

[COLOR= #FF8000]/* START NEW SESSION */[/COLOR]

    [URL="http://www.php.net/session_start"]session_start[/URL][COLOR= #007700]();[/COLOR]
    [URL="http://www.php.net/session_regenerate_id"]session_regenerate_id[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$delete_old_session[/COLOR] [COLOR= #007700]=[/COLOR] TRUE[COLOR= #007700]);[/COLOR]

[COLOR= #FF8000]// VERIFY POSTED USERNAME AND PASSWORD USING CHALLENGE[/COLOR]

[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR] [URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]login[/COLOR][COLOR= #007700]]))
{[/COLOR]
    [COLOR= #0000BB]$username[/COLOR][COLOR= #007700]=[/COLOR][URL="http://www.php.net/sha1"]sha1[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/mysql_real_escape_string"]mysql_real_escape_string[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]username[/COLOR][COLOR= #007700]]));[/COLOR]

    [COLOR= #FF8000]//create password including the challenge submitted[/COLOR]
    [COLOR= #0000BB]$response[/COLOR][COLOR= #007700]=[/COLOR][URL="http://www.php.net/mysql_real_escape_string"]mysql_real_escape_string[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]response[/COLOR][COLOR= #007700]]);[/COLOR]
    
    [COLOR= #FF8000]//check if username and password are valid[/COLOR]
    [COLOR= #0000BB]$query[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]" SELECT `level`,`password` FROM `users` WHERE `user` = "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$username[/COLOR][COLOR= #007700].[/COLOR][COLOR= #DD0000]""[/COLOR][COLOR= #007700];[/COLOR]
    [COLOR= #0000BB]$result[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"$query"[/COLOR][COLOR= #007700])[/COLOR] or [URL="http://www.php.net/die"]die[/URL][COLOR= #007700]();[/COLOR]
    [COLOR= #0000BB]$row[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$result[/COLOR][COLOR= #007700]);[/COLOR]
    
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR] [URL="http://www.php.net/mysql_num_rows"]mysql_num_rows[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$result[/COLOR][COLOR= #007700]) ==[/COLOR] [COLOR= #0000BB]1[/COLOR] AND [URL="http://www.php.net/sha1"]sha1[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$username[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]password[/COLOR][COLOR= #007700]].[/COLOR]htmlspecialchars[COLOR= #007700]([/COLOR][COLOR= #0000BB]$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]challenge[/COLOR][COLOR= #007700]])) ==[/COLOR] [COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]response[/COLOR][COLOR= #007700]] )
    {[/COLOR]

    [COLOR= #FF8000]//clean username, password, respons and empty challenge session for security reasons[/COLOR]
    [COLOR= #0000BB]$username[/COLOR][COLOR= #007700]=[/COLOR][COLOR= #DD0000][/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]password[/COLOR][COLOR= #007700]]=[/COLOR][COLOR= #DD0000][/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$response[/COLOR][COLOR= #007700]=[/COLOR][COLOR= #DD0000][/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]challenge[/COLOR][COLOR= #007700]]=[/COLOR][COLOR= #DD0000][/COLOR][COLOR= #007700];[/COLOR]

    [COLOR= #FF8000]//write system details in session[/COLOR]
    [COLOR= #0000BB]$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]system[/COLOR][COLOR= #007700]]=[/COLOR][URL="http://www.php.net/sha1"]sha1[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]HTTP_USER_AGENT[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]REMOTE_ADDR[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]SERVER_PORT[/COLOR][COLOR= #007700]]);[/COLOR]
    
    [COLOR= #FF8000]//write random code and system details in database[/COLOR]
    [COLOR= #0000BB]$code[/COLOR][COLOR= #007700]=[/COLOR][URL="http://www.php.net/sha1"]sha1[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/rand"]rand[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]1000[/COLOR][COLOR= #007700],[/COLOR][COLOR= #0000BB]10000[/COLOR][COLOR= #007700]));[/COLOR]
    [COLOR= #0000BB]$sql[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]"INSERT `login` SET `session_id` = "[/COLOR][COLOR= #007700].[/COLOR][URL="http://www.php.net/session_id"]session_id[/URL][COLOR= #007700]().[/COLOR][COLOR= #DD0000]" , `code` = "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$code[/COLOR][COLOR= #007700].[/COLOR][COLOR= #DD0000]" , `system` = "[/COLOR][COLOR= #007700];[/COLOR]
    [COLOR= #0000BB]$sql[/COLOR][COLOR= #007700].=[/COLOR] [COLOR= #DD0000]"SHA1("[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]HTTP_USER_AGENT[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]REMOTE_ADDR[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]SERVER_PORT[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]") , "[/COLOR][COLOR= #007700];[/COLOR]
    [COLOR= #0000BB]$sql[/COLOR][COLOR= #007700].=[/COLOR] [COLOR= #DD0000]" `level` = "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]level[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]" "[/COLOR][COLOR= #007700];[/COLOR]
    [COLOR= #0000BB]$result[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"$sql"[/COLOR][COLOR= #007700])[/COLOR] or [URL="http://www.php.net/die"]die[/URL][COLOR= #007700]();[/COLOR]
    
    [COLOR= #FF8000]//write random code in session[/COLOR]
    [COLOR= #0000BB]$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]code[/COLOR][COLOR= #007700]]=[/COLOR][COLOR= #0000BB]$code[/COLOR][COLOR= #007700];[/COLOR]

    [COLOR= #FF8000]//go to next page    [/COLOR]
    [URL="http://www.php.net/header"]header[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"Location:index2.php"[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/exit"]exit[/URL][COLOR= #007700];
    
}[/COLOR]

[COLOR= #FF8000]/* IF LOGIN IS NOT SUCCESSFUL */[/COLOR]
    
[URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR] [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<br />Wrong username/password."[/COLOR][COLOR= #007700]; }

}[/COLOR]


[COLOR= #FF8000]// CREATE CHALLENGE FOR PREVENTING SENDING PLAIN PASSWORD OVER HTTP CONNECTION[/COLOR]
    [COLOR= #0000BB]$challenge[/COLOR][COLOR= #007700]=[/COLOR][URL="http://www.php.net/sha1"]sha1[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/rand"]rand[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]1000[/COLOR][COLOR= #007700],[/COLOR][COLOR= #0000BB]10000[/COLOR][COLOR= #007700]));[/COLOR]
    [COLOR= #0000BB]$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]challenge[/COLOR][COLOR= #007700]]=[/COLOR][COLOR= #0000BB]$challenge[/COLOR][COLOR= #007700];[/COLOR]

?>

<script type="text/javascript" src="sha1.js"></script>

<script type="text/javascript">
<!--
function createResponse()
{
document.getElementById(response).value = hex_sha1 ( hex_sha1 ( document.getElementById(username).value ) + hex_sha1 (document.getElementById(password).value ) + document.getElementById(challenge).value ) ;
document.getElementById(password).value = "";
document.getElementById(challenge).value = "";
return true;
}
//-->
</script>

<form action="index.php" method="post" name="login" onsubmit="return createResponse();">
<fieldset>
<legend>Log In</legend>
    <label>Username</label>
<input type="text" name="username" id="username"><br />
    <label>Password</label>
<input type="password" name="password" id="password"><br />
<input type="hidden" name="challenge" id="challenge" value="[COLOR= #0000BB]<?php[/COLOR] [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$challenge[/COLOR][COLOR= #007700];[/COLOR] ?>">
<input type="hidden" name="response" id="response" value="">
<input name= "login" type="submit">
</fieldset>
</form>


Index2.php

[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/error_reporting"]error_reporting[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #FF8000]/* OPTIONAL: HANDLE SECTIONS THROUGH MYSQL */[/COLOR]

    [URL="http://www.php.net/include"]include[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"sessions.php"[/COLOR][COLOR= #007700]);[/COLOR]
    
[COLOR= #FF8000]/* SESSION START */[/COLOR]

    [URL="http://www.php.net/session_start"]session_start[/URL][COLOR= #007700]();[/COLOR]

[COLOR= #FF8000]/* CHECKS */

    //connect to database[/COLOR]
    [URL="http://www.php.net/require_once"]require_once[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"database.php"[/COLOR][COLOR= #007700]);[/COLOR]

    [COLOR= #FF8000]// check same brower, IP address and port and random code in both database and session[/COLOR]
    [COLOR= #0000BB]$query[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]" SELECT `level` FROM `login` WHERE `session_id` = "[/COLOR][COLOR= #007700].[/COLOR][URL="http://www.php.net/session_id"]session_id[/URL][COLOR= #007700]().[/COLOR][COLOR= #DD0000]" AND "[/COLOR][COLOR= #007700];[/COLOR]
    [COLOR= #0000BB]$query[/COLOR][COLOR= #007700].=[/COLOR] [COLOR= #DD0000]"`system` = SHA1("[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]HTTP_USER_AGENT[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]REMOTE_ADDR[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]SERVER_PORT[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]") AND "[/COLOR][COLOR= #007700];[/COLOR]
    [COLOR= #0000BB]$query[/COLOR][COLOR= #007700].=[/COLOR] [COLOR= #DD0000]"`code` = "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]code[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]" AND "[/COLOR][COLOR= #007700];[/COLOR]
    [COLOR= #0000BB]$query[/COLOR][COLOR= #007700].=[/COLOR] [COLOR= #DD0000]"`system` = "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]system[/COLOR][COLOR= #007700]].[/COLOR][COLOR= #DD0000]""[/COLOR][COLOR= #007700];[/COLOR]
    [COLOR= #0000BB]$result[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"$query"[/COLOR][COLOR= #007700])[/COLOR] or [URL="http://www.php.net/die"]die[/URL][COLOR= #007700]();[/COLOR]

[COLOR= #FF8000]/* IF CHECKS SUCCESSFUL -> GENERATE NEW RANDOM CODE AND GENERATE NEW SESSION ID */[/COLOR]

[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR] [URL="http://www.php.net/mysql_num_rows"]mysql_num_rows[/URL][COLOR= #007700]([/COLOR] [COLOR= #0000BB]$result[/COLOR] [COLOR= #007700]) ==[/COLOR] [COLOR= #0000BB]1[/COLOR] [COLOR= #007700]) 
{[/COLOR]
    [COLOR= #FF8000]//put level in a variable[/COLOR]
    [COLOR= #0000BB]$row[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$result[/COLOR][COLOR= #007700]);[/COLOR]
    [COLOR= #0000BB]$level[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]level[/COLOR][COLOR= #007700]];[/COLOR]
    
    [COLOR= #FF8000]//generate new random code[/COLOR]
    [COLOR= #0000BB]$code[/COLOR][COLOR= #007700]=[/COLOR][URL="http://www.php.net/sha1"]sha1[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/rand"]rand[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]1000[/COLOR][COLOR= #007700],[/COLOR][COLOR= #0000BB]10000[/COLOR][COLOR= #007700]));[/COLOR]
    
    [COLOR= #FF8000]//obtain old session id[/COLOR]
    [COLOR= #0000BB]$session_id_old[/COLOR][COLOR= #007700]=[/COLOR][URL="http://www.php.net/session_id"]session_id[/URL][COLOR= #007700]();[/COLOR]
    
    [COLOR= #FF8000]//generate new session id and remove old session[/COLOR]
    [URL="http://www.php.net/session_regenerate_id"]session_regenerate_id[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$delete_old_session[/COLOR] [COLOR= #007700]=[/COLOR] TRUE[COLOR= #007700]);[/COLOR]
    
    [COLOR= #FF8000]//obtain new session id[/COLOR]
    [COLOR= #0000BB]$session_id_new[/COLOR][COLOR= #007700]=[/COLOR][URL="http://www.php.net/session_id"]session_id[/URL][COLOR= #007700]();[/COLOR]
    
    [COLOR= #FF8000]//update table with random code and assing new session id[/COLOR]
    [COLOR= #0000BB]$sql[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]"UPDATE `login` SET `code` = "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$code[/COLOR][COLOR= #007700].[/COLOR][COLOR= #DD0000]" , `session_id` = "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$session_id_new[/COLOR][COLOR= #007700].[/COLOR][COLOR= #DD0000]" WHERE `session_id` = "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$session_id_old[/COLOR][COLOR= #007700].[/COLOR][COLOR= #DD0000]""[/COLOR][COLOR= #007700];[/COLOR]
    [COLOR= #0000BB]$result[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"$sql"[/COLOR][COLOR= #007700])[/COLOR] or [URL="http://www.php.net/die"]die[/URL][COLOR= #007700]();[/COLOR]
    
    [COLOR= #FF8000]//write new random code in session[/COLOR]
    [COLOR= #0000BB]$_SESSION[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]code[/COLOR][COLOR= #007700]]=[/COLOR][COLOR= #0000BB]$code[/COLOR][COLOR= #007700];[/COLOR]

[COLOR= #FF8000]/* IF CHECKS OK */[/COLOR]    
    
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Logged in!<br />"[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Level: "[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$level[/COLOR][COLOR= #007700].[/COLOR][COLOR= #DD0000]"<br />"[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"<a href="index.php?task=logout">Log out</a>"[/COLOR][COLOR= #007700];

}[/COLOR]

[COLOR= #FF8000]/* IF CHECKS NOT OK */[/COLOR]    

[URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]

    [COLOR= #FF8000]//destroy session[/COLOR]
    [URL="http://www.php.net/session_destroy"]session_destroy[/URL][COLOR= #007700]();[/COLOR]
    
    [COLOR= #FF8000]//go back to login page[/COLOR]
    [URL="http://www.php.net/header"]header[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"Location:index.php"[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/exit"]exit[/URL][COLOR= #007700];

}[/COLOR]

?>


sessions.php

[COLOR= #0000BB]<?php[/COLOR]

    [COLOR= #FF8000]//connect to database[/COLOR]
    [URL="http://www.php.net/require_once"]require_once[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]"database.php"[/COLOR][COLOR= #007700]);[/COLOR]

    [COLOR= #FF8000]//hash session id through sha1[/COLOR]
    [URL="http://www.php.net/ini_set"]ini_set[/URL] [COLOR= #007700]([/COLOR][COLOR= #DD0000]session.hash_function[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]1[/COLOR][COLOR= #007700]);[/COLOR]
    
    [COLOR= #FF8000]//never double sessions[/COLOR]
    [URL="http://www.php.net/ini_set"]ini_set[/URL] [COLOR= #007700]([/COLOR][COLOR= #DD0000]session.gc_probability[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]100[/COLOR][COLOR= #007700]);[/COLOR]
    [URL="http://www.php.net/ini_set"]ini_set[/URL] [COLOR= #007700]([/COLOR][COLOR= #DD0000]session.gc_divisor[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]100[/COLOR][COLOR= #007700]);[/COLOR]
    
    [COLOR= #FF8000]//session only for 5 minutes without refresh[/COLOR]
    [URL="http://www.php.net/ini_set"]ini_set[/URL] [COLOR= #007700]([/COLOR][COLOR= #DD0000]session.gc_maxlifetime[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]300[/COLOR][COLOR= #007700]);[/COLOR]
    
    [COLOR= #FF8000]//OPTIONAL referer check
    //ini_set (session.referer_check,https://www.abc.com/login/);[/COLOR]

    
[COLOR= #007700]function[/COLOR] _open[COLOR= #007700]() { }

function[/COLOR] _close[COLOR= #007700]() { }

function[/COLOR] _read[COLOR= #007700]([/COLOR][COLOR= #0000BB]$id[/COLOR][COLOR= #007700])
{[/COLOR]
    [COLOR= #0000BB]$sql[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]"SELECT `data` FROM `sessions` WHERE `id` = $id"[/COLOR][COLOR= #007700];[/COLOR]
        [URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$result[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$sql[/COLOR][COLOR= #007700]))
        {[/COLOR]
            [URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR][URL="http://www.php.net/mysql_num_rows"]mysql_num_rows[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$result[/COLOR][COLOR= #007700]))
            {[/COLOR]
                [COLOR= #0000BB]$record[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$result[/COLOR][COLOR= #007700]);[/COLOR]
                [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$record[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]data[/COLOR][COLOR= #007700]];
            }
        }[/COLOR]
[URL="http://www.php.net/return"]return[/URL][COLOR= #007700];
}

function[/COLOR] _write[COLOR= #007700]([/COLOR][COLOR= #0000BB]$id[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$data[/COLOR][COLOR= #007700])
{[/COLOR]
    [COLOR= #0000BB]$access[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/time"]time[/URL][COLOR= #007700]();[/COLOR]
    [COLOR= #0000BB]$sql[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]"REPLACE INTO `sessions` VALUES ($id, $access, $data, CURRENT_TIMESTAMP)"[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$sql[/COLOR][COLOR= #007700])[/COLOR] or [URL="http://www.php.net/die"]die[/URL][COLOR= #007700]();
}

function[/COLOR] _destroy[COLOR= #007700]([/COLOR][COLOR= #0000BB]$id[/COLOR][COLOR= #007700])
{[/COLOR]
    [COLOR= #0000BB]$sql[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]"DELETE FROM `sessions` WHERE `id` = $id "[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$sql[/COLOR][COLOR= #007700])[/COLOR] or [URL="http://www.php.net/die"]die[/URL][COLOR= #007700]();
}

function[/COLOR] _clean[COLOR= #007700]([/COLOR][COLOR= #0000BB]$max[/COLOR][COLOR= #007700])
{[/COLOR]
    [COLOR= #0000BB]$old[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/time"]time[/URL][COLOR= #007700]() -[/COLOR] [COLOR= #0000BB]$max[/COLOR][COLOR= #007700];[/COLOR]
    [COLOR= #0000BB]$sql[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]"DELETE FROM `sessions` WHERE `access` < $old"[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$sql[/COLOR][COLOR= #007700])[/COLOR] or [URL="http://www.php.net/die"]die[/URL][COLOR= #007700]();
}[/COLOR]

[URL="http://www.php.net/session_set_save_handler"]session_set_save_handler[/URL] [COLOR= #007700]([/COLOR][COLOR= #DD0000]_open[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]_close[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]_read[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]_write[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]_destroy[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]_clean[/COLOR][COLOR= #007700]);[/COLOR]

?>


sha1.js

/*
 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
 * in FIPS PUB 180-1
 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for details.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions youll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}

/*
 * Perform a simple self-test to see if the VM is working
 */
function sha1_vm_test()
{
  return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
}

/*
 * Calculate the SHA-1 of an array of big-endian words, and a bit length
 */
function core_sha1(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << (24 - len % 32);
  x[((len + 64 >> 9) << 4) + 15] = len;

  var w = Array(80);
  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;
  var e = -1009589776;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;
    var olde = e;

    for(var j = 0; j < 80; j++)
    {
      if(j < 16) w[j] = x[i + j];
      else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
      var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
                       safe_add(safe_add(e, w[j]), sha1_kt(j)));
      e = d;
      d = c;
      c = rol(b, 30);
      b = a;
      a = t;
    }

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
    e = safe_add(e, olde);
  }
  return Array(a, b, c, d, e);

}

/*
 * Perform the appropriate triplet combination function for the current
 * iteration
 */
function sha1_ft(t, b, c, d)
{
  if(t < 20) return (b & c) | ((~b) & d);
  if(t < 40) return b ^ c ^ d;
  if(t < 60) return (b & c) | (b & d) | (c & d);
  return b ^ c ^ d;
}

/*
 * Determine the appropriate additive constant for the current iteration
 */
function sha1_kt(t)
{
  return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :
         (t < 60) ? -1894007588 : -899497514;
}

/*
 * Calculate the HMAC-SHA1 of a key and some data
 */
function core_hmac_sha1(key, data)
{
  var bkey = str2binb(key);
  if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);

  var ipad = Array(16), opad = Array(16);
  for(var i = 0; i < 16; i++)
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
  return core_sha1(opad.concat(hash), 512 + 160);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Convert an 8-bit or 16-bit string to an array of big-endian words
 * In 8-bit function, characters >255 have their hi-byte silently ignored.
 */
function str2binb(str)
{
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
  return bin;
}

/*
 * Convert an array of big-endian words to a string
 */
function binb2str(bin)
{
  var str = "";
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < bin.length * 32; i += chrsz)
    str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
  return str;
}

/*
 * Convert an array of big-endian words to a hex string.
 */
function binb2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8  )) & 0xF);
  }
  return str;
}

/*
 * Convert an array of big-endian words to a base-64 string
 */
function binb2b64(binarray)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i += 3)
  {
    var triplet = (((binarray[i   >> 2] >> 8 * (3 -  i   %4)) & 0xFF) << 16)
                | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
                |  ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
    for(var j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
      else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
    }
  }
  return str;
}


Database.sql

CREATE TABLE `login` (
  `id` smallint(6) NOT NULL AUTO_INCREMENT,
  `session_id` varchar(50) NOT NULL,
  `code` varchar(40) NOT NULL,
  `system` varchar(40) NOT NULL,
  `level` smallint(6) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;


CREATE TABLE `sessions` (
  `id` varchar(40) NOT NULL,
  `access` int(10) unsigned DEFAULT NULL,
  `data` text,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE `users` (
  `id` smallint(6) NOT NULL AUTO_INCREMENT,
  `user` varchar(40) NOT NULL,
  `password` varchar(40) NOT NULL,
  `level` smallint(6) NOT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

INSERT INTO `users` (`id`, `user`, `password`, `level`) VALUES
(4, SHA1(user), SHA1(password), 1),
(7, SHA1(moderator), SHA1(password), 2),
(8, SHA1(admin), SHA1(password), 3);


Vragen? PM mij maar of post het hier onder!
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Zelf gemaakt?
0
Berichten
1.871
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nielsie schreef:
Zelf gemaakt?
Blackbullet schreef:
Credits naar Erik-Jan
Wie is Erik-Jan?
0
Berichten
2.609
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
n.v.m
0
Berichten
15.446
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nielsjojo schreef:
Eerdere citaten tonen
Nielsie schreef:
Zelf gemaakt?
Eerdere citaten tonen
Blackbullet schreef:
Credits naar Erik-Jan
Wie is Erik-Jan?
Jenner op one2xs smile
0
Berichten
8.649
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Dit is toch geen tutorail? Eerder een script die je wilt delen.
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Wil je a.u.b. die lange kut codes in een spoiler zetten? Zo blijft het wat overzichtelijk en hoef je niet 3 uur te scrollen..
En áls dit een tutorial is, waarom plaats je het dan in webwereld?
Of is het verplaatst hierheen?
0
Berichten
1.544
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Marjolein schreef:
Eerdere citaten tonen
Nielsjojo schreef:
Eerdere citaten tonen
Nielsie schreef:
Zelf gemaakt?
Eerdere citaten tonen
Blackbullet schreef:
Credits naar Erik-Jan
Wie is Erik-Jan?
Jenner op one2xs smile
Ik ben Jan Erik yummie (Meer mensen maken die fout hoor), maar dit script is niet van mij. wink
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Xjessex schreef:
Wil je a.u.b. die lange kut codes in een spoiler zetten? Zo blijft het wat overzichtelijk en hoef je niet 3 uur te scrollen..
En áls dit een tutorial is, waarom plaats je het dan in webwereld?
Of is het verplaatst hierheen?
Verplaatst
0
Berichten
398
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
door Erik-Jan op 13-06-2011 23:00.

Staat er op http://www.pfz.nl/scripts/449-login-systeem-mysql-extra-beveiliging-sessions-in/

En hier in het postje staat er:

Quote:
Credits naar Erik-Jan

Dus, hij vermeld er wel bij, dat het gemaakt is door Erik-Jan.
Toesteming heeft hij niet nodig dus. want naamsvermelding van eigenaar staat er bij.
0
Berichten
1.871
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Xthibo schreef:
door Erik-Jan op 13-06-2011 23:00.

Staat er op http://www.pfz.nl/scripts/449-login-systeem-mysql-extra-beveiliging-sessions-in/

En hier in het postje staat er:
Quote:
Credits naar Erik-Jan

Dus, hij vermeld er wel bij, dat het gemaakt is door Erik-Jan.
Toesteming heeft hij niet nodig dus. want naamsvermelding van eigenaar staat er bij.

Met naamsvermelding is het ook niet zomaar toegestaan?
0
Berichten
398
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Dan zou hij het best de link plaatsen van de site, waar hij het vandaan haalt.
Dan is dit dan ook weer opgelost wink
0
Berichten
7.681
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Je weet dat het niet heel veilig is he....

Aangezien je de encryptie met Javascript doe, wat je in de broncode kan zien. smile
0
Berichten
10.421
Reputatie
4
Punten
0,00
Lid sinds
Laatst gezien
Freestyle schreef:
Je weet dat het niet heel veilig is he....

Aangezien je de encryptie met Javascript doe, wat je in de broncode kan zien. smile
Verdiep je eens in wat een challenge precies doet....
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren