Jump to content

Recommended Posts

Hi...

 

Have speant some time away from programming, so i need some updates! :)

I have written a small application for my company and an other company. Suddenly the security part smacked me in the head. This is the first time i actually must have a login to protect info. Nothing very important, but we dont want averyone lurking around being able to read everything.

 

Anyhow. When i first started to do some security on my own sites I did it like this:

<?php
session_start()
if (isset($_GET['logout'])) {
$_SESSION['login'] = false;
unset($_SESSION["user"]);
unset($_SESSION["status"]);
unset($_SESSION["name"]);
}
if($_SESSION['login'] == true && isset($_SESSION['user']) && isset($_SESSION['status'])){

//My Page with secret stuff

} else {
echo "Log in bastard";
}
?>

 

is this a good enugh way for keeping people out or is this old shit that is renewed?

There is offcource MD5 encryption on the passwords in the database, and the login script lookes like this:

 

<?php
session_start();
if($_GET['task'] == "login") {

$usr = !empty($_POST ['username']) ? $_POST['username'] : '';
$pass = !empty($_POST ['password']) ? $_POST['password'] : '';

$md5_pass = md5($pass);
include("admin/conn_us.php");
$query = "SELECT * FROM ${dbtable} WHERE brukernavn = '".mysql_real_escape_string($usr)."'";

if($result = mysql_query($query)) {
if(mysql_num_rows($result) <> 1) {
die("<p><span class='red'>Feil Brukernavn,".
"</p><p>Kontakt systemadministrator.</span></p>");
}


if($row = mysql_fetch_array($result)) {
if($row['passord'] == $md5_pass){
session_start();
$usr = $row['brukernavn'];
$name = $row['navn'];

$_SESSION["login"] = true;
$_SESSION["user"] = $usr;
$_SESSION["name"] = $name;

header("Location: http://bahrawy.net/dlight/ballroom/index.php");
exit;
} else {
echo "<span class='red'>Du har tastet feil passord</span>";

}}}} else {
echo '<p>
<form name="form1" method="post" action="login.php?task=login">
Brukernavn:<br>
<input type="text" name="username" id="username">
<br>
Passord:
<br>
<input type="password" name="password" id="password">
<br>
<input type="submit" name="submit" id="submit" value="Logg Inn">
</form>';

}
?>

 

Thanks for any inputt! :D

Link to comment
https://forums.phpfreaks.com/topic/269598-php-security/
Share on other sites

I'm going to go through this in ordered list fashion, so please bear with me.

 

1. MD5 is not encryption, but rather it's a hash algorithm. That may seem like a small nit to pick, but there's an important distinction between the two - something that's encrypted can be decrypted. A hash is a one-way transformation. Once something is hashed, you can't dehash it to get the original value.

 

MD5 itself is not a good hash to use for password security. It's insecure, and should really only be used to create a checksum. Passwords need to be protected with a nice, slow algorithm that can generate a lot of entropy. They also, at the very least, need to be created with a salt value.

 

The easiest way to do this is to use phpass. It's a library specifically made to protect passwords.

See: http://www.openwall.com/phpass/

Tutorial: http://www.openwall.com/articles/PHP-Users-Passwords

 

2. Don't use the old mysql_* functions. They're soft deprecated. Instead, use either MySQLi or PDO, and be sure to use their prepared statement functionality. Prepared statements escape your queries (among other things).

 

3. Be sure to validate incoming data. Example, if you expect an integer, check if it's actually an integer. Conversely, if you're not expecting to receive one, don't accept one.

 

That should cover the basics.

Link to comment
https://forums.phpfreaks.com/topic/269598-php-security/#findComment-1385882
Share on other sites

Good advice from Kevin.

 

I'll one up him, and just advise you to use PDO with the mysql driver.

 

With session_start() you do it once at the logical top of your script.

 

You should not have code that does:

 

session_start().... some code if (true) session_start() as your current code does.

 

You really want to generalize your login check into a class or function so you can simply do:

 

if (!loggedIn()) {
  header('Location: login.php');
  exit;
} 

Link to comment
https://forums.phpfreaks.com/topic/269598-php-security/#findComment-1385897
Share on other sites

Thanks for the answers guys!

Just a couple of follow ups.

 

OK, so by upgrading my outdated code with PDO(), validating my income data and add some salt to the MD5 hash my site is secure with the code below:

 

if(is_int($_SESSION['login']) == 1 && isset($_SESSION['user']) && isset($_SESSION['name'])){

//secret stuff... HTML output of a table from mysql.

}

 

or should there be some other check and validation that the user is the user.

I would like to keep away from cookies and alike.

 

jup, the double session() was just a mistake of updating my code while developing... But thanks for the tip! :)

Link to comment
https://forums.phpfreaks.com/topic/269598-php-security/#findComment-1386089
Share on other sites

Thanks for the answers guys!

Just a couple of follow ups.

 

OK, so by upgrading my outdated code with PDO(), validating my income data and add some salt to the MD5 hash my site is secure with the code below:

 

if(is_int($_SESSION['login']) == 1 && isset($_SESSION['user']) && isset($_SESSION['name'])){

//secret stuff... HTML output of a table from mysql.

}

 

or should there be some other check and validation that the user is the user.

I would like to keep away from cookies and alike.

 

jup, the double session() was just a mistake of updating my code while developing... But thanks for the tip! :)

 

Again, don't use MD5. If you use phpass (which you really, really should do), you'll be using a different hashing algorithm altogether.

 

Staying away from cookies is pretty easy. Don't read from $_REQUEST or $_COOKIE.

 

For your sessions, you may want to look into session_regenerate_id if you have your users accessing different kinds of hidden info in different areas.

Link to comment
https://forums.phpfreaks.com/topic/269598-php-security/#findComment-1386098
Share on other sites

Generally, yes it's a perfectly acceptable practice.

 

IMO it depends how much overhead that would cause though. If most of those functions are only needed on say one page, then I would have a separate file for just those, and include any functions needed globally from your main include.

 

Hope that makes sense.

Link to comment
https://forums.phpfreaks.com/topic/269598-php-security/#findComment-1386118
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.