mconslau
New Member
- Messages
- 6
- Reaction score
- 0
- Points
- 1
-An introduction into making your sites secure-
Written by Joshcamas
Written by Joshcamas
Hello all!
I've been learning quite a bit about php and security this past week, and wanted to share my knowledge. Keep in mind that this is not a tutorial on how to make a login system and such, and not how to code it, but just introduces how hackers do their shiz.
Security is a pain. This day in age you can download a 1.5 billion-word dictionary for free, simply letting you press a button to get into systems. But there are ways to make their job much harder, and here are a few of these ways.
1: Hash your passwords
When facebook saves their passwords to their database, they hash it before hand. A hash is when you get a password and turn it into gobbilygook:
'password123' --> '77d656f673ff58e3f8b0adb904b5046ab8a120ae736ba3b60f'
The thing is, you can't turn the hash back into the password!
77d656f673ff58e3f8b0adb904b5046ab8a120ae736ba3b60f' --X--> 'password123'
So how would you be able to check if the user's password they typed in the login-screen is right? You'd hash the input, then compare!
'entered password'--> '77d656f673ff58e3f8b0adb904b5046ab8a120ae736ba3b60f'
'real password' --> '77d656f673ff58e3f8b0adb904b5046ab8a120ae736ba3b60f'
'77d656f673ff58e3f8b0adb904b5046ab8a120ae736ba3b60f' = 77d656f673ff58e3f8b0adb904b5046ab8a120ae736ba3b60f'!!!! The password is correct!!
2: Salt your passwords
There is a problem though. If the hacker got the hash ('77d656f673ff58e3f8b0adb904b5046ab8a120ae736ba3b60f' in this case), they could guess a password:
'guessed password (s3cr3t)'--> '13d653f6as2dhj2233230aa4b5046asdh28d20ae736bahejk237'
'real password' --> '77d656f673ff58e3f8b0adb904b5046ab8a120ae736ba3b60f'
'77d656f673ff58e3f8b0adb904b5046ab8a120ae736ba3b60f' X= 13d653f6as2dhj2233230aa4b5046asdh28d20ae736bahejk237'! WRONG PASSWORD
This is fine, unless the real password sucks, like 'password123'. The hacker knows a lot of people do this, and would guess this, and get in! (Note: He can try unlimited times because he has the hash on his own computer, so locking the login after a certain number of tries would not work!)
But there is a way to fix this! Using salts!
A salt is basically a random string. This lovely string is added to the password before it's hashed to make it more hard to guess:
salt = 'hk13298sdk1230f8s09df8'
'password123.salt --> ''asdhkj1237jk12kd789q343f8b0adb904b5046abdhk123yif'
That means that the hacker wouldn't know the salt, and would have to guess 'password123.hk13298sdk1230f8s09df8' which is much harder to guess than simply password123!!!
3: Stop MySql Injections
If you're using a Mysql database for saving anything, you'll need to know about Injections.
MySql works by sending something to the database. Example:
"SELECT * FROM users WHERE username = '$name'"
Now this is fine and dandy as long as $name is something like "joshcamas" or "justinbieber4life".
But what if it was... "'; DELETE FROM users WHERE 1 or username = '"!? What would happen?
Oh, nothing. Nothing except Delete the entire users table!!!!! Now thats a slight problem.
Dis bad! How fix? Its a quite easy fix, surprisingly enough. There are two ways to do it.
1). You simply replace the values "[\'\")(;|`,<>]" with "". That would turn that ";" into "" in this case.
2). You run $data = mysql_real_escape_string(trim($data), $connection). This would turn ";" into "\;", which would make MySql understand that its not part of the command.
You can use whatever you please, but each one works a little differently.2). You run $data = mysql_real_escape_string(trim($data), $connection). This would turn ";" into "\;", which would make MySql understand that its not part of the command.
4: Stop Cross-site scripting attacks
A XSS attack is kinda like a mysql injection - sending bad stuff for the server to run. If I remember correctly, this actually happened to MySpace once!
So lets take the example from myspace, shall we? Say a hacker has an account. This lovely account allows you to have a status. Yippy! Thats fine and dandy when the status is "Just killed myself" or "I hate grandpas". But what if... it was javascript?
This happened when a guy added some javascript to his status - basically he made the code automatically add the person looking at his status 1) friend him, and 2) turn their status into the malicious code. Oh no! In just a few hours, millions of people had this terrible status! (Note: I could be wrong about the setting - its possible that this never happened to MySpace, but this could happen to you)
So how to stop it? Right before you save the data, you can run it through strip_tags(), which removes all those yucky "<"'s and ">"'s and such. That would make the code stop working. You can then run it through htmlentities(), which turns special characters into what they should be. (Just like mysql_real_escape_string).
5: Never trust the client: Do everything on the server
Say you're building a register page, and want some fancy javascript that says "You need at least one symbol" when the user's password doesn't have a symbol. This is fine, and I would say you're on the right path. But you can't stop there. Javascript is run on the client's side, and that means the user can edit said javascript to do his bidding. If he simply made it so it didn't give the warning, *boom*. He just made a password without a symbol.
To fix this you need to remember to do everything on the server, which would mean in PHP. In other words, do the checks server-side as well! If you're checking to see if someone did the captcha correctly in javascript, do it in php as well! Remember this, and remember this well: The client is in the hands of the enemy.
I hope this post at least introduced the crazyness of making sure your code doesn't let nasty hackers into it
Last edited: