Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Web Development
You last visited: Today at 14:15

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Website] PHP Ideas: Cookies and Security Checks

Discussion on [Website] PHP Ideas: Cookies and Security Checks within the Web Development forum part of the Coders Den category.

View Poll Results: Rate the helpfulness of this post: 5 being the most help
1 1 100.00%
2 0 0%
3 0 0%
4 0 0%
5 0 0%
Voters: 1. You may not vote on this poll

Reply
 
Old   #1
 
xBlackPlagu3x's Avatar
 
elite*gold: 0
Join Date: Jan 2011
Posts: 286
Received Thanks: 71
[Website] PHP Ideas: Cookies and Security Checks

Hello, E*PVP community! I hopefully have a relatively useful PHP guide/release for you here today.

After all the PHP I've done with websites over the last couple of years, I've never really learned more than I have on the last project I had begun working on, which was a PHP, tick-based MMORPG (a tick-based MMORPG is a MMORPG that allows the user do do RPGish actions, but the server part of it is refreshed every time the user either loads the page, or every set time or so. For instance, the leaderboards might be set to update every 15 minutes).

One thing that really had me thinking about this, is how I handle registration and cookies. Cookies used wrong are very dangerous to a user's security. Here's some PHP code that basically says
Code:
if ($COOKIE['adminlevel'] == 4)
{
    // Execute code here
}
And let's say a user's admin level was not 4, but maybe 1 being a basic member. Using a Cookie editor, they could easily change that number to 4, and get by the adminlevel check.

What I've come up with, while yes, it might be slightly inefficient since you could use Sessions instead, but in cases where you need to use Cookies, or you just want to be safe, I've come up with this:

When a user registers, it takes their username and password and Sha1 hashes them both.

Afterwards, it creates a "secretkey" variable that combines both of those new hashed values and creates a Sha512 hash out of it generating quite a long secret key and then stores it in the database.

Why this is useful is because when I write my PHP code, I force every page that let's a user perform some sort of action to check the secretkey and then see if the user's cookies match the value in the database. If it doesn't, it gives them a javascript alert and notifies me via email that they've changed their cookies.

You might be thinking "Okay, well they could change their secretkey too."
Well no, because since the secretkey is generated using algorithms, every key is unique. They'd have to know a user's username and password both and hash them in order to get the new values, and then hash both of those values to get the new value.

Some might say there's an easier way, but this way has worked for me, and if you need to use Cookies and don't have any security knowledge then this would definitely be a start.

EDIT:

Sorry this is a big wall of text, and over time I'll re-word my explanations and make this post more beautiful, but here it is for now. If you'd like the PHP file, it can be downloaded as an attachment.

- xSherufanir/xBlackPlagu3x; Please rate the helpfulness of this. ^^
Attached Files
File Type: rar xSherufanir's_registeraction.php_file.rar (1.5 KB, 2 views)
xBlackPlagu3x is offline  
Thanks
1 User
Old 07/04/2012, 07:35   #2
 
Zeroxelli's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 1,769
Received Thanks: 1,143
Hmm, one suggestion would be: include the permission type (1, 4, or whatever) in the hash. So if they did change the permission type, the hash wouldn't match anymore. Also, you don't need to keep the hash in the cookies, as you can simply regenerate it every time you load something important given the username, password, and permission type. Though, instead of storing the username and password in the cookies, store their hash (and maybe salt it with a number unique to that user, maybe the entry number of their account entry in the database.)

Also, people who will say that they use sessions because not all browsers support cookies: Sessions are cookies. The data is stored on the server side, but there is still a cookie (the session id number) stored in the browser. This can be worked around by simply including the SID in the url, i.e. mypage.com/index.php?sid=YOURSIDHERE

Edit: Keeping in mind, that sessions can be even more insecure. Obtaining someone's session id on a website that doesn't check IP/etc will instantly allow you to become logged in as them.

Anywho, thanks for the release.
Zeroxelli is offline  
Old 07/04/2012, 07:55   #3
 
xBlackPlagu3x's Avatar
 
elite*gold: 0
Join Date: Jan 2011
Posts: 286
Received Thanks: 71
Quote:
Originally Posted by Zeroxelli View Post
Hmm, one suggestion would be: include the permission type (1, 4, or whatever) in the hash. So if they did change the permission type, the hash wouldn't match anymore. Also, you don't need to keep the hash in the cookies, as you can simply regenerate it every time you load something important given the username, password, and permission type. Though, instead of storing the username and password in the cookies, store their hash (and maybe salt it with a number unique to that user, maybe the entry number of their account entry in the database.)

Also, people who will say that they use sessions because not all browsers support cookies: Sessions are cookies. The data is stored on the server side, but there is still a cookie (the session id number) stored in the browser. This can be worked around by simply including the SID in the url, i.e. mypage.com/index.php?sid=YOURSIDHERE

Edit: Keeping in mind, that sessions can be even more insecure. Obtaining someone's session id on a website that doesn't check IP/etc will instantly allow you to become logged in as them.

Anywho, thanks for the release.
Holy cow, you just totally simplified about 40+ lines of PHP code I've made. >.< Thanks for the idea, if it's cool, can I update my post with what you said? I'll include that you said this of course.
xBlackPlagu3x is offline  
Old 07/04/2012, 07:56   #4
 
Zeroxelli's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 1,769
Received Thanks: 1,143
Quote:
Originally Posted by xBlackPlagu3x View Post
Holy cow, you just totally simplified about 40+ lines of PHP code I've made. >.< Thanks for the idea, if it's cool, can I update my post with what you said? I'll include that you said this of course.
Ah, sorry about that, I think. xD Kinda meant that for people reading this, but hey, whoever it helps. Go ahead, I don't mind. Whenever you find something useful from me, feel free to use it.

Edit: Here, I wrote this up real quick:

PHP Code:
<?php

    $session 
= array( // Example, you'd probably be using $_SESSION or $_COOKIES
        
'Username' => 'mrTestUser924'
        
'Password' => 'superTopSecretStuff'
        
'Salt' => 0x15); // = 21 - Maybe they were the 21st entry in the database?
    
    
function MakeHash($username$password$hashSalt)
    {
        
$userHash hash('SHA256'$username $hashSalt);
        
$passHash hash('SHA256'$password $hashSalt);
        
$hashData hash('SHA512'$userHash $passHash $hashSalt);
        return 
$hashData;
    }
    
    
$myHash MakeHash($session['Username'], $session['Password'], $session['Salt']);
    echo 
"Username: {$session['Username']} - Password: {$session['Password']} - Salt: {$session['Salt']} - Hash: {$myHash}\n";
?>
Zeroxelli is offline  
Reply


Similar Threads Similar Threads
[Help] SQL security & website php scripts
08/30/2013 - Shaiya Private Server - 8 Replies
Hello all, i've run into a bit of a snag and was hoping the good people here on epvp could give me a clue... I've searched for this but unable to find anything clear. For security in my SQL Server Configuration Manager under TCP/IP I've disabled external IP's from accessing the database, (Listen all = no, All IPs other than 127.0.0.1 switched to no) Problem is, the scripts on my website such as registration, online players etc can't communicate with the DB either, which some are prudent to...
[Release]Make a new website! Any ideas!?
06/15/2010 - CO2 PServer Guides & Releases - 15 Replies
Hello, I gonna make a conquer website, do you got any ideas ? The register page and Unstuck tool is for v5017 LOTF for other sources ask me! Progress: * Register page 50% * Unstuck tool 100% * Status Checker 100% * Top Rankings 20%
Cabal security checks
05/25/2009 - Cabal Online - 3 Replies
Anyone knows how to bypass the security checks? If not how do you guys bot while afking without being dced by the security checks. Please help me thanks:)
[IDEAS] Creating HastaLavistaCo WebSite
02/13/2009 - CO2 Private Server - 13 Replies
hi im pete, currently i am making a ip site for hastalavistaco... yes i am doing it from scratch.. already got the index.php down. next im doing the register page basically this post is for everyone to post ideas on what i should add to it.. currently this is what it looks like.. ill update the picture after i add major things to it and if i make register page ill update it and make both pictures smaller.. http://i497.photobucket.com/albums/rr340/peteninj a/preview-2.jpg
Good website ideas...
06/08/2007 - Main - 2 Replies
Hi everyone. I wanna make a website, made a couple so far. But i need a good idea for a website. A good subject... I have Photoshop CS2 so i can make templates myself, i only need a good idea... So, anyone knows a good idea?



All times are GMT +1. The time now is 14:15.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.