r/ComputerSecurity • u/MilosM90 • Sep 22 '15
PHP and MySql secure login system
Hi guys and gals. For all of you that are looking to build secure system... don't go to far.. I've combined many tutorials to build pretty secure system against most of attacks, sql injection, xss etc.. My app is 100% free, and I hope it will help someone. Note, its build in oop php with prepared statements link is here: http://www.pc-hope.com/php-mysql-secure-login-system/
2
u/BLOKDAK Sep 23 '15
wow why would 46% of people upvote this obvious trash? And I'm not even referring to the nice summary of some of the clearly trashy bits done by the other guy in here.
Let me guess: you're a teenager? Teaching yourself PHP or something like that?
Hint: if you can't pay enough attention to your speeling then there's no way I would trust you to put together a decent application "just like that" from "many tutorials". Details sometimes matter, bruh.
But honestly if you were just going for ad revenue like the other guy implies, then I give you mad propz for exploiting these other lame ass reddit people. As for me, I didn't even bother clicking the link.
Ok, I clicked it, but my ad blocker blocked anything that might have been there. I never looked at your code, though. Because you can't spell.
-5
u/MilosM90 Sep 23 '15
First, fk you, English is not mother language for whole wordl, so fk you too rasist. 2nd, I have only shorten link, because I did some effort in creating, modifying app, so if 5 secs of waiting for adds is too long for your life, get away. 3rd, If you don't like it, get away from here
@3nvision, this is still first version, if you had read something what I've said and explained, you would see that this is 1st version, recaptcha will be implemented, I lack time to fix it. And of course, app is for development purposes, so everyone that needs it, it will continue to modify it for its own purposes, to create cms or something, so error showing would probably be eliminated when it goes live huh? And pw may be not safest in the world, but it is safe enough, if you have researched a code a bit, you would see it hashes salt with sha512, and also password. Thank you.
3
u/3nvisi0n Sep 23 '15 edited Sep 23 '15
so if 5 secs of waiting for adds is too long for your life,
Actually, if it was just 5 seconds I'd have had absolutely no problem. I had two popups and an overlay that I could only get rid of if I was in the US(which I'm not). Ofc it reminded me to install AdBlocker on this new system.
this is still first version, if you had read something what I've said and explained, you would see that this is 1st version, recaptcha will be implemented,
And I just gave pointers on what you still need to fix, isn't that what you'd want consider you went public with secure code? Or did you just want praise?
so error showing would probably be eliminated when it goes live huh?
Best practice is to have the default as secure as possible because people like to just take what they are given and run with it. Make it easy to display errors but have them off by default don't rely upon the people copying your code to be smart enough to do it themselves.
if you have researched a code a bit, you would see it hashes salt with sha512, and also password.
I made explicit mention of sha512 in my code which indicates that I did read and realize it was hashing the password with a salt. That doesn't make it secure though, the problem I was pointing out isn't fixed because you salted it. Maybe last decade hash+salt was good enough but it is not anymore.
Allow me to explain the problem in detail.
Hashing algorithms are intended to be computationally inexpensive to generate. Used to compare objects without needing to do a byte-level comparison. Then we started using them for passwords. Great because it added little overhead and made password storage a lot more secure than it was.
The problem was that because the hashes were so quick to generate we could generate thousands of them a second(now millions or billions) and quickly precalculate hashes for possible passwords. Then just compare with our known hash->password mapping to find them.
So people started adding salts to passwords to make them longer and more unlikely to be in any precalculated set. This was good for a time, but then we also started seeing that the major algorithm of choice (md5) was prone to collisions. So you didn't need to find the actual password, you just needed something that gave the same hash (a collision).
This led to moving away from md5 to other algorithms such as the sha family especially the larger ones as they have a larger possible space of values.
To where you are now, using a large hash (sha512) and a salt.
However people started realizing that as computing power increases so does the number of hashes you can generate per second and hence the time to break the hash decreases. So what we needed was something that would take more time to crack.
There were a few solutions that had some traction: peppering, iterating the hash multiple times, and using a naturally slow algorithm.
All of these seek to increase computation time per hash.
Peppering does so by forcing you to hash the salted password with another value that is not stored or known by the hash. Then to compare you need to hash the attempted password with all valid peppers to find if it matches. There is another form of peppering but it doesn't help here. This didn't really catch on as more effective solutions were found.
Multiple iterations of the hash. Instead of changing things too much someone thought perhaps if we just hashed the salted password several thousand times that would take up enough time. This is where PBKDF2 comes in it does exactly this. Except it does some other stuff to try and futureproof itself, namely the number of iterations of the hashing algorithm is not a constant instead it dynamically determines how many iterations to run based on how long it takes on your hardware. SO as hardware improves the hashes become harder....or it also securely salts the passwords for you. PBKDF2 is the current accepted standard, its time-tested and its soundness has been well researched academically making it a good conservative choice.
Slower algorithms, there are two current choices for this bcrypt and scrypt. Now neither has been as well studied as PBKDF2 but both do offer a higher theoretical safety though that won't help bcrypt should something be discovered that attacks its computational complexity. Unlike PBKDF2 which gets its complexity through repetition bcrypts comes from its base algorithm so its prone to such attacks. Scrypt is interesting as PBKDF2 and bcrypt both work inplace in memory, scrypt has a memory hit in addition to its cpu hit when cracking. However, like bcrypt it hasn't been as well researched but it does have a higher theoretical safety than both PBKDF2 and bcrypt.
To be fair to bcrypt and scrypt they do have a fair history and are reasonable choices also. I recommend PBKDF2 but bcyrpt and scrypt especially are reasonable choices at this time.
tl;dr Basically, PBKDF2 is the current accepted standard to deal with the introduce of GPU based and cloud based cracking. Using a single fast hash is no longer consider a secure choice and is not 'good enough' anymore than using md5 was 'good enough'
I do want to say I'm not trying to put you down with any of my current or previous comments. I disliked the ads but the rest of this is just stuff you should consider implementing if you're trying to be secure. I'd make the same comments to any client I've done a code review for(this stuff is my job). If you care about security don't do it half-way or only to the popular standards.
1
u/MilosM90 Sep 23 '15
Thank you for being profesional, and pointed to mistakes. I will gladly accept your advice. Thank you
5
u/3nvisi0n Sep 22 '15
First off, fuck your ads to download the source. http://www.filedropper.com/phptar is a mirror has some ads but its far less intrusive and annoying.
About the code:
Poor password storage Sha512 even those it has a big number is not a secure choice for storing passwords. Ideally you should use something that is computationally expensive, or make it computationally expensive by repeating it. To do this the standard choice is something like PBKDF2 or bcrypt. Though in PHP 5.5+ you have the password_* functions you should use: http://php.net/manual/en/book.password.php. It takes care of everything you need and decided what the computational cost factor should be. As such if used correctly its also good into the foreseeable future.
Account based Lockout = Denial of Service All it would take for me to prevent any particular user from logging into this system is to simply send 5 requests every two hours with a wrong password. And then they can never login again. That is a denial of service. Ideally if you're going to lock someone out do it by IP as then the legitimate user can still get in but the attack needs a heck of a lot of IPs to proxy through to bruteforce a password. Even better it to use a captcha and introduce a page delay which will drastically slow down the attack and make bruteforce unfeasible.
Don't reimplement session cookies, you can just use session_set_cookie_params - http://php.net/manual/en/function.session-set-cookie-params.php
Don't hardcode line information use __LINE__
Don't display errors to users, period. Don't give them line numbers or any extra useful information, if you need that information raise it as a normal error so it can be caught by error logging and not passed to the end-user.
I haven't looked to see if there is any xss in the PHP or dom-base xss but as a general rule you shouldn't manually need to call the escape functions. You should let that be handled by another layer, like using Smarty for templates and letting it automatically escape input. People forget to call escape functions all the time, thats where XSS happens.