LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2006, 12:30 AM   #1
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
PHP security question: sessions vs cookies


I just want to know which is better to handle user authentication issues...

I currently use the default session handling (which is cleaner and is more portable). But should cookies be used explicitly to handle this?

Which is better? The session handling functions or the cookie functions?
 
Old 03-04-2006, 12:44 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
AFAIK, PHP *relies* on being able to write cookies in order to *implement* "sessions", doesn't it? Once you create a session, aren't you implicitly using cookies anyway , whether or not you explicitly read or write cookies in your own PHP code?

Last edited by paulsm4; 03-04-2006 at 12:45 AM.
 
Old 03-04-2006, 01:03 AM   #3
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
I know that. But there are cookie functions which allow me to set cookies explicitly.

Sessions work even if cookies are disabled, but uses the query string in the browser to keep track of the session vars. I read somewhere that this may be a security risk, but I cannot seem to find it.

Anyway, most users have cookies enabled. Which way would create portable and secure code?
 
Old 03-04-2006, 01:21 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
The security problem with encoding stuff in the URL is two-fold:

1. It's easily read (even more easily than the stuff in forms you'd have
to "view source" to see), revealing (possibly unwanted) details about
your web application's internals

2. It lends itself to cross-site scripting attacks (people can cut/paste
from the URL to manually access different parts of the web app, perhaps
subverting it).

Here's a link that gives a *much* better explanation than I'm doing (sorry! it's late ;-):
http://www.cgisecurity.com/articles/xss-faq.shtml

A great book covering an *extremely* wide range security issues is:

Security Warrior, Peikari & Chuvakin

'Hope that helps .. PSM
 
Old 03-04-2006, 09:18 AM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by Harishankar
I know that. But there are cookie functions which allow me to set cookies explicitly.

Sessions work even if cookies are disabled, but uses the query string in the browser to keep track of the session vars. I read somewhere that this may be a security risk, but I cannot seem to find it.

Anyway, most users have cookies enabled. Which way would create portable and secure code?
yes sessions will work without using cookies. the way i have sessions setup on my server i use mm to manage the sessions. this way they are never written to disk, even on the server. there is no use of any cookies at all..

sure there are some security concerns, but imo they are *at worst* balanced with cookies. the technique i use to preserve the session id is to pass the id using GET.. the requested page then checks the session id with a database that contains active sessions that allows only one session per userid.. the system checks the request against the current session, referrer url, ip address, and i had implemented a timestamp but threw it out due to being a bit of an overkill for my purposes. if anything does not match the user info in the db, the session is erased and the user is forced to login again.

while it may be possible to hijack a session using this method, it is extremely unlikely.

if you would want anymore info feel free.. i worked a while on this system and built it from scratch.. so i feel like i have a pretty good understanding of any security concerns with it..
 
Old 03-04-2006, 01:20 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Harishankar -

Everything Xhi is saying is correct. A couple of additional points:

1. "mm" is completely a server-side thing. It has nothing directly to do
with the "cookies vs URL" issue we were discussing:

http://us3.php.net/session
http://www.sitepoint.com/article/p3p-cookies-ie6/2

2. Here's a bit more on the "session hijacking" Xhi mentions:
Quote:
URL based session management has additional security risks compared to cookie based session management. Users may send a URL that contains an active session ID to their friends by email or users may save a URL that contains a session ID to their bookmarks and access your site with the same session ID always, for example.
<= Also from http://us3.php.net/session

3. PHP "global variables" is another potential security risk. Newer
versions of PHP all default to "register_globals = false" (in php.ini):
Quote:
If register_globals is disabled, only members of the global associative array $_SESSION can be registered as session variables. The restored session variables will only be available in the array $_SESSION.

Use of $_SESSION (or $HTTP_SESSION_VARS with PHP 4.0.6 or less) is recommended for improved security and code readability. With $_SESSION, there is no need to use the session_register(), session_unregister(), session_is_registered() functions. Session variables are accessible like any other variables.
<= Also from http://us3.php.net/session

4. To the extent you're using PHP "out of the box" (it sounds like Xhi has
done considerable work on his own infrastructure), cookies are arguably
the best, easiest, most reliable way to go.

IMHO .. PSM
 
Old 03-04-2006, 01:42 PM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
1. "mm" is completely a server-side thing. It has nothing directly to do with the "cookies vs URL" issue we were discussing:
true.. but i felt it is good information to throw in there if OP is going to be searching on how to setup a server to support sessions without cookies.. since it is one of only three ways to implement sessions tracking.. (there are only 3 that i know of anyhow)

Quote:
to the extent you're using PHP "out of the box" (it sounds like Xhi has done considerable work on his own infrastructure), cookies are arguably the best, easiest, most reliable way to go.
im not sure about most reliable, but cookies are definately the easiest way to go.. yes it was considerably more work.. if you choose the path i did, you can rest assured you will spend a few extra hours fine tuning php, as well as a database to track the sessions.. the verdict is still not in on my system as far as reliability under load goes..

good description on the session hijacking.. that is why i would recommend doing checks on ip address and referrer url along with the session id.. and of course you are correct on the global variables.. they should *never* be enabled..

my only reasons for using cookieless sessions were that i just do not like using cookies and wanted to see if i could implement a fully functional site without cookies.. i did and so i ran with it..
 
Old 03-04-2006, 11:02 PM   #8
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
I am creating a script where I cannot predict how the server set up will be like since I'll release it to the public.

My script is a simple CMS system (GPL license) with a small number of features to build a website. Actually I think at present I'll stick to the session management that is built in to PHP.

When I complete my script, I would be glad if experts could take the code, see what security flaws exist and so on.

I'm doing all the basic security things like converting form data into correct data type and converting to mysql query safe strings and such. I am also writing the code to be entire using $_SESSION vars and checking if they are set and so on.

That's what Open Source is for, isn't it? Learning something new all the time.

Last edited by vharishankar; 03-04-2006 at 11:06 PM.
 
Old 03-06-2006, 12:33 AM   #9
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
After reading a bit about sessions, I decided to use a cookie to encode a particular string and store it in the user's browser.

Unless the cookie is set with this value, the administrator session will not authenticate even if the session Id is correct.

Will this be of any value? Or is it weak?
 
Old 03-06-2006, 12:38 AM   #10
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
yes thats not a bad idea..

one thing i had done was everytime a user is registered they have a unique hash entered in a userhash db.. you could store a hash of a hash, or hash of part of a hash, etc in the cookie and test against the admin hashes in the db..
 
Old 03-06-2006, 01:19 AM   #11
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
I'm also using JavaScript to MD5 encrypt the password before it's submitted. Since this is a single user application (CMS with one user only) is that a good idea? Anyway, I guess most people have JavaScript enabled. Otherwise the password will not work.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Slack 10.1 setting up php with cookies caged Slackware 2 10-11-2005 12:27 AM
cookies in php RazorKnight Programming 1 10-08-2004 04:31 AM
PHP - Cookies, unserialize() dhbiker Programming 1 07-28-2004 02:18 PM
PHP > MySQL connection password security question Wibble Linux - Security 4 04-22-2004 03:19 PM
PHP and cookies. jacksmash Programming 13 04-02-2004 09:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration