Ticket #2394: 2394-salt.patch

File 2394-salt.patch, 5.3 KB (added by pishmishy, 5 years ago)

Draft of salted passwords

  • wp-login.php

     
    183183 
    184184        // Generate something random for a password... md5'ing current time with a rand salt 
    185185        $new_pass = substr( md5( uniqid( microtime() ) ), 0, 7); 
     186        // By default we now create a salted password. 
     187        $salt = password_salt(); 
     188        $new_pass = $salt.md5($salt.$new_pass); 
    186189        $wpdb->query("UPDATE $wpdb->users SET user_pass = MD5('$new_pass'), user_activation_key = '' WHERE user_login = '$user->user_login'"); 
    187190        wp_cache_delete($user->ID, 'users'); 
    188191        wp_cache_delete($user->user_login, 'userlogins'); 
  • wp-includes/pluggable.php

     
    322322                $error = __('<strong>ERROR</strong>: Invalid username.'); 
    323323                return false; 
    324324        } else { 
    325                 // If the password is already_md5, it has been double hashed. 
    326                 // Otherwise, it is plain text. 
     325                if (strlen($login->user_pass) > 32 && !$already_md5){ 
     326                        // If the password is longer than a plain MD5 hash 
     327                        // then presume that it's a salted password hash. 
     328                        $hash = substr($login->user_pass,strlen($login->user_pass)-32,32); 
     329                        $salt = substr($login->user_pass,0,strlen($login->user_pass)-32); 
     330                        if (md5($salt . $password) == $hash) { 
     331                                return true; 
     332                        } else { 
     333                                $error = __('<strong>ERROR</strong>: Incorrect Password.'); 
     334                                $pwd = ''; 
     335                                return false; 
     336                        } 
     337                } elseif (strlen($login->user_pass) > 32 && $already_md5) { 
     338                        // If the password is already_md5, it has been double hashed. 
     339                        // Otherwise, it is plain text. 
     340                        $hash = substr($login->user_pass,strlen($login->user_pass)-32,32); 
     341                        $salt = substr($login->user_pass,0,strlen($login->user_pass)-32); 
     342                        $test = md5($hash); 
     343                        return ($test == $password); 
     344                }        
     345 
    327346                if ( ($already_md5 && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) { 
    328347                        return true; 
    329348                } else { 
     
    426445 
    427446if ( !function_exists('wp_setcookie') ) : 
    428447function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '', $remember = false) { 
    429         if ( !$already_md5 ) 
    430                 $password = md5( md5($password) ); // Double hash the password in the cookie. 
     448        $user = get_userdatabylogin($username); 
    431449 
     450        if ( !$already_md5) { 
     451                if (strlen($user->user_pass)>32) { // Assume that the password is salted  
     452                                                   // extract the hash and hash again. 
     453                        $hash = substr($user->user_pass,strlen($user->user_pass)-32,32); 
     454                        $password = md5($hash); 
     455                } else { // Unsalted passwords 
     456                        $password = md5( md5($password) ); // Double hash the password in the cookie. 
     457                } 
     458        } 
     459 
    432460        if ( empty($home) ) 
    433461                $cookiepath = COOKIEPATH; 
    434462        else 
  • wp-includes/registration.php

     
    3737} 
    3838 
    3939/** 
     40 * Generates a short random string for salting user passwords. This can  
     41 * be replaced with any function that returns a string less than 32  
     42 * characters in length (limitation is database field size) 
     43 */ 
     44function password_salt(){ 
     45        for($i=0;$i<4;$i++){ 
     46                $x = rand(0,61); 
     47                if (0 <= $x && $x < 10 ) $salt = $salt . chr(48+$x); 
     48                if (10 <= $x && $x < 36) $salt = $salt . chr(55+$x); 
     49                if (36 <= $x && $x < 62) $salt = $salt . chr(61+$x); 
     50        } 
     51        return $salt; 
     52} 
     53 
     54/** 
    4055 * Insert an user into the database. 
    4156 * @global object $wpdb WordPress database layer. 
    4257 * @param array $userdata An array of user data. 
     
    5469        } else { 
    5570                $update = false; 
    5671                // Password is not hashed when creating new user. 
    57                 $user_pass = md5($user_pass); 
     72                // By default we now create a salted password. 
     73                $salt = password_salt(); 
     74                $user_pass = $salt.md5($salt.$user_pass); 
    5875        } 
    5976 
    6077        $user_login = sanitize_user($user_login, true); 
     
    163180        // If password is changing, hash it now. 
    164181        if ( ! empty($userdata['user_pass']) ) { 
    165182                $plaintext_pass = $userdata['user_pass']; 
    166                 $userdata['user_pass'] = md5($userdata['user_pass']); 
     183                // By default we now create a salted password. 
     184                $salt = password_salt(); 
     185                $userdata['user_pass'] = $salt.md5("$salt".$userdata['user_pass']); 
    167186        } 
    168187 
    169188        // Merge old and new fields with new fields overwriting old ones. 
     
    214233        return wp_create_user($username, $password, $email); 
    215234} 
    216235 
    217 ?> 
    218  No newline at end of file 
     236?> 
  • wp-includes/user.php

     
    2121        } else { 
    2222                $userdata = $cache_userdata[$user_login]; 
    2323        } 
    24         return (md5($user_pass) == $userdata->user_pass); 
     24 
     25        $login = get_userdatabylogin($username); 
     26        if (strlen($login->user_pass) > 32) { // If new style salted passwords are being used 
     27                        $hash = substr($login->user_pass,strlen($login->user_pass)-32,32); 
     28                        $salt = substr($login->user_pass,0,strlen($login->user_pass)-32); 
     29                        return (md5($salt . $user_pass) == $hash); 
     30        } else { // Revert to unsalted passwords 
     31                return (md5($user_pass) == $userdata->user_pass); 
     32        } 
    2533} 
    2634 
    2735//