summaryrefslogtreecommitdiff
path: root/inc/mod
diff options
context:
space:
mode:
authorMichael Foster <savetheinternet@tinyboard.org>2013-07-24 11:15:55 -0400
committerMichael Foster <savetheinternet@tinyboard.org>2013-07-24 11:15:55 -0400
commit31f657e550ab8b1ea00953cc3a26f4661ee4458c (patch)
tree2a04233caaacc672b0ead54ad8e1ce43649dc18f /inc/mod
parent46edec0f2d8c31b55e36e160c7d9bac8bf4d3aa4 (diff)
Long overdue: Salted password hashes
Diffstat (limited to 'inc/mod')
-rw-r--r--inc/mod/auth.php34
-rw-r--r--inc/mod/pages.php24
2 files changed, 39 insertions, 19 deletions
diff --git a/inc/mod/auth.php b/inc/mod/auth.php
index 173190ab..1bc27dff 100644
--- a/inc/mod/auth.php
+++ b/inc/mod/auth.php
@@ -29,6 +29,11 @@ function mkhash($username, $password, $salt = false) {
return $hash;
}
+function generate_salt() {
+ mt_srand(microtime(true) * 100000 + memory_get_usage(true));
+ return md5(uniqid(mt_rand(), true));
+}
+
function login($username, $password, $makehash=true) {
global $mod;
@@ -37,20 +42,23 @@ function login($username, $password, $makehash=true) {
$password = sha1($password);
}
- $query = prepare("SELECT `id`,`type`,`boards` FROM `mods` WHERE `username` = :username AND `password` = :password LIMIT 1");
+ $query = prepare("SELECT `id`, `type`, `boards`, `password`, `salt` FROM `mods` WHERE `username` = :username");
$query->bindValue(':username', $username);
- $query->bindValue(':password', $password);
$query->execute() or error(db_error($query));
- if ($user = $query->fetch()) {
- return $mod = array(
- 'id' => $user['id'],
- 'type' => $user['type'],
- 'username' => $username,
- 'hash' => mkhash($username, $password),
- 'boards' => explode(',', $user['boards'])
- );
- } else return false;
+ if ($user = $query->fetch(PDO::FETCH_ASSOC)) {
+ if ($user['password'] === hash('sha256', $user['salt'] . $password)) {
+ return $mod = array(
+ 'id' => $user['id'],
+ 'type' => $user['type'],
+ 'username' => $username,
+ 'hash' => mkhash($username, $user['password']),
+ 'boards' => explode(',', $user['boards'])
+ );
+ }
+ }
+
+ return false;
}
function setCookies() {
@@ -104,10 +112,10 @@ if (isset($_COOKIE[$config['cookies']['mod']])) {
exit;
}
- $query = prepare("SELECT `id`, `type`, `boards`, `password` FROM `mods` WHERE `username` = :username LIMIT 1");
+ $query = prepare("SELECT `id`, `type`, `boards`, `password` FROM `mods` WHERE `username` = :username");
$query->bindValue(':username', $cookie[0]);
$query->execute() or error(db_error($query));
- $user = $query->fetch();
+ $user = $query->fetch(PDO::FETCH_ASSOC);
// validate password hash
if ($cookie[1] !== mkhash($cookie[0], $user['password'], $cookie[2])) {
diff --git a/inc/mod/pages.php b/inc/mod/pages.php
index cddecad0..1ed9a94b 100644
--- a/inc/mod/pages.php
+++ b/inc/mod/pages.php
@@ -1407,9 +1407,13 @@ function mod_user($uid) {
}
if ($_POST['password'] != '') {
- $query = prepare('UPDATE `mods` SET `password` = SHA1(:password) WHERE `id` = :id');
+ $salt = generate_salt();
+ $password = hash('sha256', $salt . sha1($_POST['password']));
+
+ $query = prepare('UPDATE `mods` SET `password` = :password, `salt` = :salt WHERE `id` = :id');
$query->bindValue(':id', $uid);
- $query->bindValue(':password', $_POST['password']);
+ $query->bindValue(':password', $password);
+ $query->bindValue(':salt', $salt);
$query->execute() or error(db_error($query));
modLog('Changed password for ' . utf8tohtml($_POST['username']) . ' <small>(#' . $user['id'] . ')</small>');
@@ -1430,9 +1434,13 @@ function mod_user($uid) {
if (hasPermission($config['mod']['change_password']) && $uid == $mod['id'] && isset($_POST['password'])) {
if ($_POST['password'] != '') {
- $query = prepare('UPDATE `mods` SET `password` = SHA1(:password) WHERE `id` = :id');
+ $salt = generate_salt();
+ $password = hash('sha256', $salt . sha1($_POST['password']));
+
+ $query = prepare('UPDATE `mods` SET `password` = :password, `salt` = :salt WHERE `id` = :id');
$query->bindValue(':id', $uid);
- $query->bindValue(':password', $_POST['password']);
+ $query->bindValue(':password', $password);
+ $query->bindValue(':salt', $salt);
$query->execute() or error(db_error($query));
modLog('Changed own password');
@@ -1494,9 +1502,13 @@ function mod_user_new() {
if ($_POST['type'] !== JANITOR && $_POST['type'] !== MOD && $_POST['type'] !== ADMIN)
error(sprintf($config['error']['invalidfield'], 'type'));
- $query = prepare('INSERT INTO `mods` VALUES (NULL, :username, SHA1(:password), :type, :boards)');
+ $salt = generate_salt();
+ $password = hash('sha256', $salt . sha1($_POST['password']));
+
+ $query = prepare('INSERT INTO `mods` VALUES (NULL, :username, :password, :salt, :type, :boards)');
$query->bindValue(':username', $_POST['username']);
- $query->bindValue(':password', $_POST['password']);
+ $query->bindValue(':password', $password);
+ $query->bindValue(':salt', $salt);
$query->bindValue(':type', $_POST['type']);
$query->bindValue(':boards', implode(',', $boards));
$query->execute() or error(db_error($query));