Added new password system to strengthen passwords against brute force attacks.

This commit is contained in:
GRMrGecko 2013-06-09 14:08:10 -05:00
parent 255c090a5f
commit a3be9ae994
4 changed files with 56 additions and 5 deletions

View File

@ -42,7 +42,7 @@ if (isset($_MGM['user']) && $_MGM['user']['level']==1 && $_MGM['path'][1]=="user
$epassword = $result['password']; $epassword = $result['password'];
if (!empty($password)) { if (!empty($password)) {
$salt = substr(sha1(rand()),0,12); $salt = substr(sha1(rand()),0,12);
$epassword = $salt.hash("sha512", $salt.hash("sha512", $password)); $epassword = $salt.hashPassword($password,hex2bin($salt));
} }
if ($level=="") if ($level=="")
$level = $result['level']; $level = $result['level'];

View File

@ -24,7 +24,7 @@ if (isset($_REQUEST['login'])) {
$error = "Invalid login credentials."; $error = "Invalid login credentials.";
} else { } else {
$salt = substr($user['password'], 0, 12); $salt = substr($user['password'], 0, 12);
$epassword = $salt.hash("sha512", $salt.hash("sha512", $password)); $epassword = $salt.hashPassword($password,hex2bin($salt));
if ($epassword!=$user['password']) { if ($epassword!=$user['password']) {
$error = "Invalid login credentials."; $error = "Invalid login credentials.";
} else { } else {

View File

@ -16,7 +16,7 @@
$result = databaseQuery("SELECT * FROM settings WHERE name='db_version'"); $result = databaseQuery("SELECT * FROM settings WHERE name='db_version'");
if ($result==NULL) { if ($result==NULL) {
databaseQuery("CREATE TABLE settings (name TEXT, value TEXT)"); databaseQuery("CREATE TABLE settings (name TEXT, value TEXT)");
databaseQuery("INSERT INTO settings (name, value) VALUES ('db_version',%d)", $_MGM['version']); databaseQuery("INSERT INTO settings (name, value) VALUES ('db_version',%s)", $_MGM['version']);
databaseQuery("CREATE VIRTUAL TABLE images USING fts3(user_id INTEGER, hash TEXT, extension TEXT, name TEXT, file_size INTEGER, width INTEGER, height INTEGER, thumb_file_size INTEGER, thumb_width INTEGER, thumb_height INTEGER, tags TEXT, external_data TEXT, ocr TEXT, time INTEGER)"); databaseQuery("CREATE VIRTUAL TABLE images USING fts3(user_id INTEGER, hash TEXT, extension TEXT, name TEXT, file_size INTEGER, width INTEGER, height INTEGER, thumb_file_size INTEGER, thumb_width INTEGER, thumb_height INTEGER, tags TEXT, external_data TEXT, ocr TEXT, time INTEGER)");
databaseQuery("CREATE TABLE users (docid INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT, password TEXT, time INTEGER, level INTEGER)"); databaseQuery("CREATE TABLE users (docid INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT, password TEXT, time INTEGER, level INTEGER)");
@ -31,6 +31,49 @@ if ($result==NULL) {
<? <?
require_once("footer.php"); require_once("footer.php");
exit(); exit();
} else {
$info = databaseFetchAssoc($result);
if ($info['value']<=1) {
if (isset($_REQUEST['update_user'])) {
$email = (isset($_REQUEST['email']) ? trim($_REQUEST['email']) : "");
$password = (isset($_REQUEST['password']) ? trim($_REQUEST['password']) : "");
$result = databaseQuery("SELECT * FROM users WHERE email=%s AND level!=0", $email);
$user = databaseFetchAssoc($result);
if ($user==NULL) {
echo "Invalid login credentials.";
} else if ($user['level']!=1) {
echo "Account is not an administrator account.";
} else {
$salt = substr($user['password'], 0, 12);
$epassword = $salt.hash("sha512", $salt.hash("sha512", $password));
echo $epassword;
if ($epassword!=$user['password']) {
echo "Invalid login credentials.";
} else {
$epassword = $salt.hashPassword($password,hex2bin($salt));
databaseQuery("UPDATE users SET password=%s WHERE email=%s", $epassword, $email);
databaseQuery("UPDATE settings SET value=%s WHERE name='db_version'", $_MGM['version']);
header("location: ".generateURL());
exit();
}
}
}
require_once("header.php");
?>
<h1>Admin Password Update</h1>
<p>Passwords hash system has been changed. Please enter an administrator email and password to update your account. You would be required to update any other account's password once you have completed this step as their account will not be able to function.</p>
<form action="<?=generateURL()?>" method="POST">
<input type="hidden" name="update_user" value="true" />
<input type="text" placeholder="Email" name="email" /><br />
<input type="password" placeholder="Password" name="password" /><br />
<input type="submit" value="Fix Password" class="btn" />
</form>
<?
require_once("footer.php");
exit();
}
} }
if (isset($_REQUEST['create_user'])) { if (isset($_REQUEST['create_user'])) {
@ -41,7 +84,7 @@ if (isset($_REQUEST['create_user'])) {
$count = databaseFetchAssoc($result); $count = databaseFetchAssoc($result);
if ($count['count']==0 && !empty($email) && !empty($password)) { if ($count['count']==0 && !empty($email) && !empty($password)) {
$salt = substr(sha1(rand()),0,12); $salt = substr(sha1(rand()),0,12);
$epassword = $salt.hash("sha512", $salt.hash("sha512", $password)); $epassword = $salt.hashPassword($password,hex2bin($salt));
databaseQuery("INSERT INTO users (email, password, time, level) VALUES (%s,%s,%d,1)", $email, $epassword, $_MGM['time']); databaseQuery("INSERT INTO users (email, password, time, level) VALUES (%s,%s,%d,1)", $email, $epassword, $_MGM['time']);
setcookie("{$_MGM['CookiePrefix']}user_email", $email, $_MGM['time']+31536000, $_MGM['CookiePath'], $_MGM['CookieDomain']); setcookie("{$_MGM['CookiePrefix']}user_email", $email, $_MGM['time']+31536000, $_MGM['CookiePath'], $_MGM['CookieDomain']);
setcookie("{$_MGM['CookiePrefix']}user_password", hash("sha512", $epassword.$_MGM['time']), $_MGM['time']+31536000, $_MGM['CookiePath'], $_MGM['CookieDomain']); setcookie("{$_MGM['CookiePrefix']}user_password", hash("sha512", $epassword.$_MGM['time']), $_MGM['time']+31536000, $_MGM['CookiePath'], $_MGM['CookieDomain']);

View File

@ -17,7 +17,7 @@
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT); error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT);
$_MGM = array(); $_MGM = array();
$_MGM['version'] = "1"; $_MGM['version'] = "2";
$_MGM['DBType'] = "SQLITE"; // MYSQL, POSTGRESQL, SQLITE. $_MGM['DBType'] = "SQLITE"; // MYSQL, POSTGRESQL, SQLITE.
$_MGM['DBPersistent'] = NO; $_MGM['DBPersistent'] = NO;
$_MGM['DBHost'] = "localhost"; $_MGM['DBHost'] = "localhost";
@ -72,6 +72,14 @@ function generateURL($path) {
return "http".($_MGM['ssl'] ? "s" : "")."://".$_MGM['domain'].(((!$_MGM['ssl'] && $_MGM['port']==80) || ($_MGM['ssl'] && $_MGM['port']==443)) ? "" : ":{$_MGM['port']}").$_MGM['installPath'].$path; return "http".($_MGM['ssl'] ? "s" : "")."://".$_MGM['domain'].(((!$_MGM['ssl'] && $_MGM['port']==80) || ($_MGM['ssl'] && $_MGM['port']==443)) ? "" : ":{$_MGM['port']}").$_MGM['installPath'].$path;
} }
function hashPassword($password, $salt) {
$hashed = hash("sha512", $salt.$password);
for ($i=0; $i<10000; $i++) {
$hashed = hash("sha512", $salt.hex2bin($hashed));
}
return $hashed;
}
connectToDatabase(); connectToDatabase();
if (file_exists("code/setup.php")) { if (file_exists("code/setup.php")) {