To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 12-05-2003, 09:41 AM   #1
seby
Senior Member
 
Join Date: Nov 2002
Posts: 476
is this secure?

Greetings,
I'm looking for some advice on the below code.
I would like to know how dependable does the login code look and if anyone has anything that would improve it in a security stand-point..

I know nothing is 100% secure, but would like to know if anyone sees any flaws.

1) username,userid,password is fetched form the database, if no user is found give the user a userid of 0.

2) if user is not found proceed to let the user login.

3) if user is found then send them to index.php.

code:

PHP Code:
error_reporting(E_ALL &~E_NOTICE);
require(
'./global.php');

// check if user already has a cookie
$fetchuserinfo = $data->query(
    
"SELECT userid
     FROM admins
     WHERE userid = '"
.$_COOKIE['gameuid']."'");
$userinfo = $data->fetch_array($fetchuserinfo);

if(
mysql_num_rows($fetchuserinfo) == 0)
{
    
$userinfo['userid'] = 0;
}

if (
$userinfo['userid'] != 0)
{
    
// if user is already logged in then send them to index.php
    
header('Location: index.php');
    exit;
}

if (
$_POST['submit'])
{
    
$validate = $data->query("SELECT userid,username,password
    FROM `admins` WHERE `username` = '"
.
    
addslashes(htmlspecialchars($_POST['username'])) . "'");

    
// check if username entered is valid
    
if (mysql_num_rows($validate) == 0)
    {
        echo
'Username does not exist';
        exit;
    }
    else
    {
        
$user = $data->fetch_array($validate);

        
// check if password matches
        
if(md5($_POST['password']) != $user['password'])
        {
            echo
'Sorry, your password is incorrect.';
            exit;
        }
        else
        {
            
setcookie('gameuser', $user['username'], time() + (3600 * 24 * 7));
            
setcookie('gameuid', $user['userid'], time() + (3600 * 24 * 7));
            
setcookie('gamepass', md5($user['password']), time() + (3600 * 24 * 7));

            
// user is now logged in and sent to index.php
            
header('Location: index.php');
            exit;
        }
    }
}
else
{
    
// login form template code
    
eval("echo(\"" . template("loginpage") . "\");");
}
__________________
Counter-Strike Planet

Last edited by BuzzLY; 12-09-2003 at 03:09 PM.
seby is offline   Reply With Quote
Old 12-05-2003, 10:06 AM   #2
Thatsmej
Junior Member
 
Join Date: Nov 2003
Posts: 25
PHP Code:
$fetchuserinfo = $data->query("SELECT userid FROM admins
WHERE userid = '"
.$_COOKIE['gameuid']."'");
test if you can add a quote in here ( " )

if sow you should add an addslashes here..

i believe it dependse on the server config..

Last edited by BuzzLY; 12-09-2003 at 03:07 PM.
Thatsmej is offline   Reply With Quote
Old 12-05-2003, 10:22 AM   #3
AstroTeg
Senior Member
 
Join Date: Dec 2003
Location: Cleveland Ohio
Posts: 1,657
Thumbs up on the md5 hashing.

The only thing I noticed (and its a little too early in the morning for me so it might not be the only thing). Your query:

PHP Code:
$fetchuserinfo = $data->query("SELECT userid FROM admins
WHERE userid = '"
.$_COOKIE['gameuid']."'");
In theory, a user could create a cookie which looked similar to the cookie your code looks for, but with a user ID chosen by the user instead of one assigned by the site. To make it a little more bullet proof, include the password and the user name in the above select statement. That way if the user tries doctoring some of the cookie data, they'll have to get the user name, password hash, and the user ID just right. This should make it more difficult for the user to just guess at user IDs.

Otherwise, it looks pretty good from where I'm at...

Last edited by BuzzLY; 12-09-2003 at 03:08 PM.
AstroTeg is offline   Reply With Quote
Old 12-05-2003, 11:21 AM   #4
Shrike
Not Yet Involved
 
Shrike's Avatar
 
Join Date: Oct 2003
Location: The Eighth, Sursamen
Posts: 2,254
Use PHP's session support if you really need some tighter security, that way the only data stored on the client is the randomly generated PHPSESSID.

To prevent brute-force hacking, give each user 3 attempts at logging in before locking their account in some way.

Bear in mind that if you allow people to select their own username and password, you could effectively give access details away during the registration process, if you print messages such as 'this username is inuse'

Also giving a user cookie a 1 week lifetime is not very secure (in the case of shared PC's

my2c
__________________
The Hundredth Idiot
Shrike is offline   Reply With Quote
Old 12-05-2003, 12:57 PM   #5
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Quote:
Originally posted by Shrike
To prevent brute-force hacking, give each user 3 attempts at logging in before locking their account in some way.
And then, because I don't want you posting here any more, I'll try and log in as you three times and so lock you out
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 12-05-2003, 01:37 PM   #6
Shrike
Not Yet Involved
 
Shrike's Avatar
 
Join Date: Oct 2003
Location: The Eighth, Sursamen
Posts: 2,254
Thats the price you pay for security

Also only works if you know my username. On a forum ok, but on for e.g. an online bank account, not so likely ;p
__________________
The Hundredth Idiot

Last edited by Shrike; 12-05-2003 at 01:39 PM.
Shrike is offline   Reply With Quote
Old 12-05-2003, 01:47 PM   #7
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Quote:
Originally posted by Shrike
Thats the price you pay for security
Can you say "Denial of service"?
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 12-05-2003, 01:52 PM   #8
Shrike
Not Yet Involved
 
Shrike's Avatar
 
Join Date: Oct 2003
Location: The Eighth, Sursamen
Posts: 2,254
Lets just force people to pass an IQ test before being allowed to buy a computer
__________________
The Hundredth Idiot
Shrike is offline   Reply With Quote
Old 12-05-2003, 02:43 PM   #9
procoder
Freelance Programmer
 
Join Date: Dec 2002
Posts: 102
Why are you storing the password in a cookie? that is very bad.
procoder is offline   Reply With Quote
Old 12-05-2003, 03:42 PM   #10
DeadFly
<? phpinfo(); ?>
 
DeadFly's Avatar
 
Join Date: May 2003
Location: Canada
Posts: 147
Quote:
Originally posted by procoder
Why are you storing the password in a cookie? that is very bad.
Agreed.
I usually just store the UserID and UserName in cookies, and nothing more. If I need a password from them (for admin purposes), I make a prompt for it.
DeadFly is offline   Reply With Quote
Old 12-05-2003, 07:00 PM   #11
agoe
Junior Member
 
Join Date: Jun 2003
Location: Norway
Posts: 28
he's storing passwords the md5-way : hard to decrypt.

to comment the script:
i would use sessions
i would put both userid/username and password in the same query so you get "wrong username or password" instead of "wrong username" or "wrong password": saves time and server capacity, if only a little bit. plus the security-issues mentioned above.
agoe is offline   Reply With Quote
Old 12-05-2003, 09:47 PM   #12
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Quote:
Originally posted by agoe
he's storing passwords the md5-way : hard to decrypt.
Doesn't matter. I just steal the cookie.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 12-05-2003, 09:50 PM   #13
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Quote:
Originally posted by Shrike
Lets just force people to pass an IQ test before being allowed to buy a computer
Been there
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 12-07-2003, 02:01 AM   #14
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
You're relying on cookies and post data. You cannot achieve security while relying on cookies and post data. All you can do is make it harder to hack. Prevent sql insertions and you're okay.

BTW: do not store any version of the password in a cookie. What if someone hacks md5 tomorrow? Besides MD5 isn't all that secure. It's only 128 bits long which means you only really get 64 bits of security from it based on collision attacks. For security you should be using SHA-256.
drawmack is offline   Reply With Quote
Old 12-07-2003, 12:39 PM   #15
Merve
black sheep with red wool
 
Merve's Avatar
 
Join Date: Jul 2003
Location: North of the 49th parallel
Posts: 2,579
Don't store the password in a cookie; just match it against the password in the DB...no need to store it any form cookie.
__________________
"A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

The Three C's
Merve is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 03:05 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.