#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,675
    Rep Power
    171

    If CRYPT_BLOWFISH not available


    I understand crypt is not the best choice (vs password combat or php 5.5's built in).

    Believe it or not I have a quesiton about crypt

    Would it be correct to use automatic hash this in case CRYPT_BLOWFISH is not available ( < 5.3 ),
    PHP Code:
    function generate_hash($password=NULL)
        {
            if(
    $password==NULL)
                {
                    return 
    false;    
                }
            else
                {
                    if(
    defined("CRYPT_BLOWFISH") && CRYPT_BLOWFISH)
                        {
                            
    $salt '$2y$11$' substr(md5(uniqid(rand(), true)), 022);
                            return 
    crypt($password$salt);
                        }
                    else
                        {
                            return 
    crypt($password);
                        }
                }
        } 
    PHP Code:
    ///Register 
    .........
    $stmt $connection->prepare("INSERT INTO website_test_members (name, email, password) VALUES (:name, :email, :password)");
    $stmt->execute(array(':name' => $name':email' => $email':password' => generate_hash($password)));
    ......... 
    (Before register I also need to add a part to make sure that generate_hash is not returning false.)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,008
    Rep Power
    376
    iirc password compat checks if it is available or not and then falls back on something else which i cant remember now.

    why not just use that and be done with all this fact finding missions
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    no, this is not secure. Not even "secure enough". Even the bcrypt part is messed up -- twice, actually (the randomness source and the encoding). Didn't I tell you last time that you must not fumble with low-level cryptography? Didn't I tell you that the sitepoint tutorial is sh*t? And didn't I say that you need to update your PHP installation in case you're below 5.3.7?

    Now we're back, and what you have is PHP 5.2 (or whatever) and a low-level function call copypastad from the sitepoint tutorial.

    What should I conclude from this except that you don't really care about our advice? What do you expect from us, anyway? Do you just want somebody to approve this so that you have peace of mind? Sorry, I won't. If you're not willing to follow basic security rules (like keeping your stuff up to date), you're simply not secure.
    Last edited by Jacques1; September 9th, 2013 at 08:08 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,183
    Rep Power
    9398
    Jacques, maybe I'm missing it, but what's wrong with coming up with a very random salt and bcrypting for >2000 iterations? Is it cryptographically secure? Maybe not, but are you saying we need to worry about an attacker having low-level, realtime access to the machine as these passwords are being generated?
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    First of all, this code throws away a large part of the salt by messing up the encoding: bcrypt supports a salt of 128 bits represented as a base64 encoded string of 22 characters (4 bits of the last base64 digit are being irgnored). The code uses 22 hex characters, which means each character only carries 4 bits. This reduces the salt to ~88 bits.

    The output of uniqid(), mt_rand() etc. is not "very random". There's a whole collection of remote attacks against PHP "randomness" allowing people to predict future salts. The salt does not have to be cryptographically secure. But it must not be predictable, and the chance of a collisions must be low. Proper sources would be something like openssl_random_pseudo_bytes($size) or mcrypt_create_iv($size, MCRYPT_DEV_URANDOM) or /dev/urandom.

    Stupid mistakes like this happen again and again and again. This is not just an unfortunate bug by some confused tutorial writer. I'm pretty sure he didn't even come up with the code himself. It's a mass phenomenon poisoning the "security" code of real-life application. And that's why we need to get the message out: Keep away from low-level functions unless you know exactly what you're doing. Security isn't the right domain for the usual "Let's copy some code we found on the Internet and fumble with it until we get the right output".

    I know this probably sounds terribly condescending. But I've seen so many people fail (including myself) that it's really the only sensible conclusion I could think of.

    Comments on this post

    • requinix agrees : true, but I'll still maintain that it's a lot of effort for lvery ittle real-world gain here
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,675
    Rep Power
    171
    Originally Posted by Jacques1
    What should I conclude from this except that you don't really care about our advice?
    Hi Jacques1 how are you?

    Running own business on limited budget can destroy your soul and shrinks your balls to the size of raisins.
    I don't see computers or code like you do. I didn't know what is an "email" until 2004. Am I in the wrong business? Probably yes but it's too late to get out I have too many transactions, too much money/people involved so I better learn this and enjoy it as well.

    Do I like my situation? Now I do. I am learning, making $ and getting better slowly at the same time. Advice I get from here I archive and go through over and over till it makes sense. One step at a time for me.

    $salt = '$2y$11$' . substr(md5(uniqid(rand(), true)), 0, 22); can take you a second to figure out but understanding whats behind md5 or uniqid or $2y$11$ in depth takes a little bit of time. While I am going through those I also need to keep the sites as secure as I can and the business going.

    Not long ago I used to store passwords just as user posted them. No MD5, no SH1, no nothing. I have more understanding (I better have after 2000 posts in Devshed, right?).

    Originally Posted by Jacques1
    What do you expect from us, anyway? Do you just want somebody to approve this so that you have peace of mind?
    No. I expect you to be cool & understanding as always and don't get frustrated. Every post you do makes a difference for a lot of people.
    I realised the only way to get more money is to have more value. Someone who knows PHP (including Zend, CI, Cake, OOP, etc) advanced SQL and work as a Uniqx server admin fellow can make a lot more cash than someone who knows only a few things on PHP.
    So my goal is to get really good at LAMP, hire you, fire you and hire paulh1983 because he has better social skills.

    Thanks

    Comments on this post

    • paulh1983 agrees : i agree that jacques help is appreciated and does make a difference

IMN logo majestic logo threadwatch logo seochat tools logo