PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 28 votes, 4.54 average. Display Modes
 
Unread Dev Shed Forums Sponsor:
  #241  
Old March 9th, 2007, 02:35 PM
XPertMailer XPertMailer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 17 XPertMailer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 21 m 17 sec
Reputation Power: 0
Send a message via Yahoo to XPertMailer
GET & POST reverse magic quotes:
PHP Code:
if (get_magic_quotes_gpc()) {
    if (isset(
$_GET)) $_GET array_map('stripslashes'$_GET);
    if (isset(
$_POST)) $_POST array_map('stripslashes'$_POST);


Reply With Quote
  #242  
Old March 10th, 2007, 02:04 AM
buggedcom buggedcom is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: london
Posts: 62 buggedcom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 32 m 37 sec
Reputation Power: 6
Send a message via AIM to buggedcom Send a message via Yahoo to buggedcom
To protect your includes from being accessed via the browser why not just put them in a .htaccess protected directory. that way the scripts can still include them and the browser can't see them.

Reply With Quote
  #243  
Old March 10th, 2007, 07:47 AM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,382 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 11 h 1 m 51 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
Quote:
Originally Posted by buggedcom
To protect your includes from being accessed via the browser why not just put them in a .htaccess protected directory. that way the scripts can still include them and the browser can't see them.


I'm sure that was mentioned, but it's not an option for everyone. It's hard to require that if you're the designer of a program, too. Not everyone runs Apache, either. But yes, that is one good method.

---John Holmes...

Reply With Quote
  #244  
Old March 11th, 2007, 12:32 PM
XPertMailer XPertMailer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 17 XPertMailer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 21 m 17 sec
Reputation Power: 0
Send a message via Yahoo to XPertMailer
Tip: use "mysql_real_escape_string()" for SQL statement and not "addslashes()".

Secure GET & POST values:

PHP Code:
<?php

$_GETS 
array_map('mysql_real_escape_string'$_GET);
$_POSTS array_map('mysql_real_escape_string'$_POST);

mysql_query("SELECT * FROM `table` WHERE ".
"`get1` = '".$_GETS['key1']."' AND ".
"`get2` = '".$_GETS['key2']."' AND ".
"`post1` = '".$_POSTS['key1']."' AND ".
"`post2` = '".$_POSTS['key2']."'");

// ... etc ...

?>

and then you dont have to worry about forgetting to do this.

Reply With Quote
  #245  
Old March 11th, 2007, 02:15 PM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,382 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 11 h 1 m 51 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
Quote:
array_map('stripslashes', $_GET)

Quote:
array_map('mysql_real_escape_string', $_GET)

Personal preference here, but I don't think applying functions blindly to the entire $_GET and $_POST arrays is good for efficiency and scalability.

IF I've already validated a string and I KNOW the value is going into a query, then I apply mysql_real_escape_string() to that value only.

Also, I only need to run stripslashes on a value if it's already validated to a string and I KNOW I'm going to turn around and use that value elsewhere.

---John Holmes...

Reply With Quote
  #246  
Old March 11th, 2007, 06:28 PM
Hammer65's Avatar
Hammer65 Hammer65 is online now
Web Developer/Musician
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Nov 2004
Location: Lincoln Nebraska
Posts: 2,075 Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level)Hammer65 User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 8 h 41 m 30 sec
Reputation Power: 590
Send a message via AIM to Hammer65
I don't mind doing that with the entire array, if the majority of values are destined for the database. If only 2 or 3 values out of say 20 in a form are not destined for the db it seems to make sense to me to do that. Better to use the pre-written function for that than typing out 17 calls to mysql_real_escape_string, just because not all request varaibles (get or post) are meant for the database.

My db abstraction has it's own escaping method to use with db values and prepared statements, so for inserts and updates I rarely escape out side of the db abstraction, but I have the option to do so If need be.
__________________
"Strange women lying in ponds distributing swords is no basis for a system of government. Supreme executive power derives from a mandate from the masses, not from some farcical aquatic ceremony! Well, but you can't expect to wield supreme executive power just 'cause some watery tart threw a sword at you! I mean, if I went 'round saying I was an emperor just because some moistened bint had lobbed a scimitar at me, they'd put me away!"

Last edited by Hammer65 : March 12th, 2007 at 08:40 AM.

Reply With Quote
  #247  
Old March 26th, 2007, 03:16 PM
mfrank410 mfrank410 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Location: Ontario, Canada
Posts: 16 mfrank410 User rank is Sergeant Major (2000 - 5000 Reputation Level)mfrank410 User rank is Sergeant Major (2000 - 5000 Reputation Level)mfrank410 User rank is Sergeant Major (2000 - 5000 Reputation Level)mfrank410 User rank is Sergeant Major (2000 - 5000 Reputation Level)mfrank410 User rank is Sergeant Major (2000 - 5000 Reputation Level)mfrank410 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 h 53 m 47 sec
Reputation Power: 0
PHP Session Hijacking

Greetings,

Great information and and resources for the newbie PHP developer. I have noticed one security issue that has not been addressed. PHP Session Hijacking.

Session ID hijacking can be a problem with PHP Websites. The PHP session tracking component uses a unique ID for each user's session, but if this ID is known to another user, that person can hijack the user's session and see information that should be confidential. Session ID hijacking cannot completely be prevented; you should know the risks so you can mitigate them.

For instance, even after a user has been validated and assigned a session ID, you should revalidate that user when he or she performs any highly sensitive actions, such as resetting passwords. Never allow a session-validated user to enter a new password without also entering their old password, for example. You should also avoid displaying truly sensitive data, such as credit card numbers, to a user who has only been validated by session ID.

A user who creates a new session by logging in should be assigned a fresh session ID using the session_regenerate_id function. A hijacking user will try to set his session ID prior to login; this can be prevented if you regenerate the ID at login.

If your site is handling critical information such as credit card numbers, always use an SSL secured connection. This will help reduce session hijacking vulnerabilities since the session ID cannot be sniffed and easily hijacked.

If your site is run on a shared Web server, be aware that any session variables can easily be viewed by any other users on the same server. Mitigate this vulnerability by storing all sensitive data in a database record that's keyed to the session ID rather than as a session variable. If you must store a password in a session variable (and I stress again that it's best just to avoid this), do not store the password in clear text; use the sha1() (PHP 4.3+) or md5() function to store the hash of the password instead.

PHP Code:
if ($_SESSION['password'] == $userpass) {
 
// do sensitive things here



The above code is not secure, since the password is stored in plain text in a session variable. Instead, use code more like this:

PHP Code:
if ($_SESSION['sha1password'] == sha1($userpass)) {
 
// do sensitive things here



The SHA-1 algorithm is not without its flaws, and further advances in computing power are making it possible to generate what are known as collisions (different strings with the same SHA-1 sum). Yet the above technique is still vastly superior to storing passwords in clear text. Use MD5 if you must -- since it's superior to a clear text-saved password -- but keep in mind that recent developments have made it possible to generate MD5 collisions in less than an hour on standard PC hardware. Ideally, one should use a function that implements SHA-256; such a function does not currently ship with PHP and must be found separately.

Another solution to help prevent Session Hijacking is to handle sessions with custom handlers. This allows you to store the session information in a database (encrypted form of course). A little trick to help validate sessions is to assign the user agent to the session entry in the database. When you verify a users session id, you should also compare the user agent.

In conclusion, you cannot prevent session hijacking completely, but you can take measure to minimize the damage and information that can be stolen.

Smart programming and proper security implementations.

Cheers!
Comments on this post
SimonGreenhill agrees: good post.
J_Tree agrees: ++ See example code below!
ryon420 agrees!
b3n agrees!

Reply With Quote
  #248  
Old March 28th, 2007, 09:46 AM
J_Tree's Avatar
J_Tree J_Tree is offline
Rocking my php-ness
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2004
Location: Boston, MA
Posts: 1,968 J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 15 h 11 m 47 sec
Reputation Power: 145
Send a message via ICQ to J_Tree Send a message via AIM to J_Tree Send a message via MSN to J_Tree Send a message via Yahoo to J_Tree
I have some code I use as part of a session class to create / use 1 time session IDs that regenerate on each request. Here is the code put together into one easy to use function:

PHP Code:
/*
 Starts a session, regenerates an id and destroys the old session to 
 help prevent session hijacking
*/
function safer_session_start()
{
    
// Start the session
    
session_start();

    
// Assign current contents to a temp var
    
$s $_SESSION;

    
// Get rid of $_SESSION
    
$_SESSION null;
    unset(
$_SESSION);

    
// Get current session id
    
$old_id session_id();

    
// Make a new session id
    
session_regenerate_id();

    
// Get the new session id
    
$new_id session_id();

    
// Tell php we want to use the old session
    
session_id($old_id);

    
// Buh bye old session!!! Ohh NOES!!
    
session_destroy();

    
// Tell php it's back to the new session!
    
session_id($new_id);

    
// Start it up proper!
    
session_start();

    
// Put back our session var(s)
    
$_SESSION $s;

__________________
My new WebComic http://www.jjsunshines.com/
The Geek Shall Inherit the Earth

It is NOT ok to IM me with questions unless I told you it was ok via PM

Last edited by J_Tree : March 28th, 2007 at 09:47 AM. Reason: funny spacing

Reply With Quote
  #249  
Old March 28th, 2007, 10:06 AM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,382 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 11 h 1 m 51 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
Quote:
Originally Posted by J_Tree
I have some code I use as part of a session class to create / use 1 time session IDs that regenerate on each request. Here is the code put together into one easy to use function
WTH would you go through all of that instead of just calling session_regenerate_id() by itself? Kind of seems like a waste. :shrug:
Comments on this post
J_Tree agrees: I understand your skepicism but see my next post

Reply With Quote
  #250  
Old March 28th, 2007, 02:03 PM
J_Tree's Avatar
J_Tree J_Tree is offline
Rocking my php-ness
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2004
Location: Boston, MA
Posts: 1,968 J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level)J_Tree User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 15 h 11 m 47 sec
Reputation Power: 145
Send a message via ICQ to J_Tree Send a message via AIM to J_Tree Send a message via MSN to J_Tree Send a message via Yahoo to J_Tree
Well . . .

In my testing using php 4, session_regenerate_id() doesn't seem to remove old session data, it just starts a new session with a new id. This means that if someone were to capture my previous ID they could have access to my session data even though I was using a new copy. Who cares who is using which copy if my credit card or social security number is stored in there. The method I posted above explicitly clears my old session data assigned with my previous ID and moves it to my new session. For all intents and purposes, it creates a 1 time 1 use Session ID.

The manual for this function makes it seem as if all that is needed is session_regenerate_id(), but real life testing shows differently.

EDIT: Reading the manual now shows an optional argument for this function as of php 5.1 that seems to address the issue my function solves. So I guess if you have php < 5.1, My function would be a replacement.

Last edited by J_Tree : March 28th, 2007 at 02:06 PM.

Reply With Quote