Is it Secret? Is it Safe?

by Jeff Starr on Wednesday, March 17, 2010 39 Responses

[ Enjoying the Evening ] Whenever I find myself working with PHP or messing around with server settings, I nearly always create a phpinfo.php file and place it in the root directory of whatever domain I happen to be working on. These types of informational files employ PHP’s handy phpinfo() function to display a concise summary of all of your server’s variables, which may then be referenced for debugging purposes, bragging rights, and so on.

While this sort of thing is normally okay, I frequently forget to remove the file and just leave it sitting there for the entire world to look at. This of course is a big “no-no” for site security, because the phpinfo.php file contains a hefty amount of information about my server, including stuff like:

  • The web server version
  • The IP address of the host
  • The version of the operating system
  • The root directory of the web server
  • Configuration information about the remote PHP installation
  • The username of the user who installed php and if they are a SUDO user

That, and tons more may be easily accessed quite easily by just about anyone looking for it. Of course, nefarious scum could then use this information to detect a vulnerability, exploit it, and feel better about their pathetic, wasted lives.

Remember to protect or remove any phpinfo.php or other sensitive files that you may have sitting around on your server.

So, wise readers, it is my advice to you (as well to myself) to remember to protect or remove any phpinfo.php or other sensitive files that you may have sitting around on your server. An information-disclosure attack may seem like a low priority affair, but if the attacker locates a vulnerability, you’re screwed.

How to protect your phpinfo and other sensitive files with htaccess

If you are constantly referring to the file and would rather not delete it, consider adding the following slice of HTAccess to keep it private for your IP only:

# protect phpinfo
<Files php-info.php>
	Order Deny,Allow
	Deny from all
	Allow from 123.456.789
</Files>

Edit this snippet to include your specific IP address, along with any other IPs that may require access. Just use additional Allow from 123.456.789 lines to do so.

Likewise, to protect other files, you can replace “php-info.php” with the name of the file, or use regular expressions to pattern-match specific file sets.

Remember, when it comes to sensitive data, take an old wizard’s advice:

Keep it secret. Keep it safe.

About the author

[ Jeff Starr ]

Jeff Starr is a web developer, graphic designer and content producer with over 10 years of experience and a passion for quality and detail. Jeff is co-author of the book Digging into WordPress and strives to help people be the best they can be on the Web. + Follow Jeff on Twitter and subscribe to Perishable Press for awesome web-design content delivered fresh.


39 Responses

Add a comment

[ Gravatar Icon ]

Joseph McCullough#1

Ouch, that seems like that could turn into a major problem. I haven’t had to deal with any of that, but thanks for the heads up.

Tell me if this would be a good idea, I’ve never done it before, just popped into my head

I include a docheading.php in all of my files that contains the doctype, charset, php constants, and a reference to my global css style sheet. If you do that or something similar, could you possibly tag this logic with it?

check if file phpinfo.php exists (using file_exists() )
If it exists
Mail to webmaster a message saying that phpinfo is still in the directory
AND BAD THINGS WILL ENSUE.

So you’ll get an email letting you know to delete it. You could also create a boolean value to make sure if a notification email has been sent it doesn’t continue to send every time a page loads.

Just a silly thought :)

[ Gravatar Icon ]

Connie#2

There is absolutely no need to call this file
phpinfo.php. You could as a weak step to protect, call it lkdfeorDasistidelIebederMATRosen.php, why not?

I am always frustrated

- to find these files at servers
- to find these files named phpinfo.php

no phantasy, no security!

[ Gravatar Icon ]

Bjørn Johansen#3

I totally agree with Connie here. It is absolutely no need to mess around with .htaccess.

If you want IP based security, you could just rewrite your phpinfo.php like this (Yes, it’s a one liner):

<?php if ($_SERVER['REMOTE_ADDR']=='123.456.789') phpinfo(); ?>

[ Gravatar Icon ]

Ben Everard#4

+1, why not write a post it and place it in the middle of your screen, then you won’t forget to delete it.

Equally like Connie said, name it super-cali-fragilistic-expiali-docious.php

Ben

[ Gravatar Icon ]

Frank Martin#5

I can’t understand why you’d ever create a separate file for this at all? Surely you wont need to reference this file enough to warrant keeping it there for the duration of a project.
Personally I’d use the same method as Bjørn then delete straight away, after all how long does it take to write that line of code into an existing file. Plus if it’s smack bang in the middle of your screen you can’t carry on working without deleting it..

[ Gravatar Icon ]

patriciomg#6

IMHO there’s no need to play with the .htaccess file for this. I see 3 alternatives:

1. Just ask to your sysadmin for the info you need. Done!

2. Create your script, upload it, load it, save the result in your pc, delete the script from your live system. done! The configuration in a live system does not change oft.

3. On virtual hosting there’s always a cpanel tool (or similar). Which means, there’s no need to create a phpinfo script.

I wonder why someone wants to have sensitive info in a live system.. Maybe I’m still sleepy, better I go for my coffee ration :)

regards.
Pat.

[ Gravatar Icon ]

Musa#7

Completely agree with Connie

[ Gravatar Icon ]

Bart Jacobs#8

To start, I never place a phpinfo file in the website root. I am always amused at the cleverness of web programmers and how they do their best to protect their work. I learn every single day.

Connie’s right, though, be creative with naming your files (especially temp files).

[ Gravatar Icon ]

Stephanie#9

I don’t know if you ever did a post about this (and apologies if you did). But I just started learning the tricks and ways of htacess. I’d be interested in learning more if you could compile some of the more common handy things that pros use now and again.

[ Gravatar Icon ]

Rod Homor#10

Another thing you could do is ONLY call and display the output functions IF a particular SUPER-GLOBAL variable is set in the query string. So, even if you forget to delete the file, you would be the only person to know WHAT URL value to use for that file / page to display the ‘sensitive’ data.

For example:

<?
if ( isset ( $_GET['super_secret_variable_name'] )) {
     phpinfo();
}

else {
     echo ( 'Are you forgetting something!!??!!' );
     exit;
}

[ Gravatar Icon ]

Adrian#11

After reading this post I decided to check the server on which this was posted.

http://perishablepress.com/php-info.php

[ Gravatar Icon ]

Jeff_drumgod#12

Hello, very nice your post. I created one on my site, based on their words, but translated into Portuguese - BR.
Congratulations on your blog.

URL: http://blog.webcres.com.br/como-proteger-o-seu-phpinfo-e-outros-arquivos-confidenciais-com-htaccess/

[ Gravatar Icon ]

Shadow Caster#13

I know I’m going to get blasted for this but shouldn’t it be the server admin’s job to make sure there are no vulnerabilities? And if the server gets hacked he will be a bit more proactive in defending it. From my experience any attacked websites/servers are brought back up within a few hours and don’t go down again for a long time afterwards.

[ Gravatar Icon ]

Chris Gray#14

Could this or a similar method be used to hide a directory from all but one IP in the same way?

[ Gravatar Icon ]

Jeff Starr#15

@Joseph McCullough: Great idea for when you need to keep the file on the server. Thanks for sharing.

@Connie: There are cases where it is useful to have such informational files on the server. In such cases, I think it is more convenient/useful to name the files something like, “phpinfo_whateveryouwant.php” than just naming them arbitrarily (which could get confusing and slow down development). I agree it’s best to simply remove them, but this is not always possible or practical. In such cases, htaccess is one way of locking them down.

@Bjørn Johansen: Another excellent protection method. Thanks!

@Ben Everard: As mentioned, it may be more useful to include a recognizable name along with the nonsense characters. But yeah, renaming is a simple way of protecting it. HTAccess works even better because the file is still blocked even if something happens to find it.

@Frank Martin: There are many reasons why people keep a copy of loose informational files on the server. Mostly convenience, but other reasons as well. In these cases, it is important to protect the files. As explained in the article, deleting such files is the ideal solution.

@patriciomg: Great ideas! Thanks for sharing them with us.

@Musa: Duly noted. Thanks for voting ;)

@Bart Jacobs: Creative naming with recognizable file names is good, but actually blocking such files is even better, in my experience.

@Stephanie: Check out my htaccess tag archive! :)

@Rod Homor: Another excellent technique — thank you!

@Adrian: Fixed, thanks for checking ;)

@Jeff_drumgod: Thank you — that’s awesome :)

@Shadow Caster: Perhaps, but there’s millions of different strategies and ways of doing things. Just trying to help those who can use it.

@Chris Gray: Yep, just use a wildcard * for the file name and place the htaccess file in the directory you would like to protect.

[ Gravatar Icon ]

Alfred Ayache#16

The phpinfo.php file is one of the first ones I include in a system I’m developing. It continues to be useful for the lifetime of the app, so I don’t want to remove it. But I hate dealing with Apache, so no .htaccess for me. Instead, I put it in my admin directory, and test to make sure the user requesting it is logged in as an admin.

In the event the system is broken and I can’t log in, then I create the file using some weird name, and make sure to delete it when I’m done.

Jeepers! I thought *everyone* did that!

[ Gravatar Icon ]

Francis#17

Every security guy will tell you that naming thing with complex name is not a security protection.

Instead of being lazy why don’t you delete the files after you done? It take less than 10 sec to create a phpinfo file!

There’s no point of messing around with login, .htaccess or complex $_GET params. Just do yourself a favour and be professional! Delete what you don’t need when you’re done.

I do not wish to see your code! If can’t delete a file after you done I imagine the mess in your code.

[ Gravatar Icon ]

Rod Homor#18

Francis, I only use the GET parameter just in the rare case that I forget to delete the file. Not as a consistent security measure.

Now that this point has been clarified….

I completely agree with you, Francis. Delete the file!!

[ Gravatar Icon ]

steve#19

Joseph - The problem is that you are not using a proper URL shortening service for your links. There are many choices, but http://www.skyzop.com seems to be the best one out there. You can shrink your URLs and also earn money from it. You can also create a blog, write reviews, and lots more. The other option is bit.ly, but they do not offer any type of payment for your links. The choice is yours - Hope this helps

[ Gravatar Icon ]

WP Tricks#20

I will do it… thanks for share easy tips

[ Gravatar Icon ]

keli#21

@Jeff: while I agree with you, you really don’t need a phpinfo to find out the hosts IP (in fact you’d be hard pressed to view the site, if your browser wouldn’t know that already). Webserver and PHP versions can often be nicked from any error page (though, of course that can and should be disabled as well. most often it isn’t)

@shadowcaster: yes you will be and you should be. And go ask your server admin for a slap on my behalf. As a sysadmin, only I know how often do I have to do cleanup after careless / ignorant / stupid developers. While a sysadmin can — and more often than a webdeveloper, actually will — harden the systems defenses, they simply can’t always patch every hole a developer leaves in a system and no it’s not their job either. Point in case (and this is a reaally harmless issue, compared to most) how does the server admin know whether you still need that phpinfo code or not? Should we arbitrarily delete / block access to every potentially dangerous file we might find?

Please, please consider really seriously, that every security should be multi-layered. Both the developer must take any measure s/he can to code safe and quality code and the server admin must also make sure that the rest of the system is kept as secure as circumstances allow. Ignorance of either part is just recipe for disaster. And even if “in your experience” every attacked site is back in working order in short time, that probably meant a lot of stressful work for a few people in the background, so that you can continue to remain ignorant about the reality.

[ Gravatar Icon ]

Steve#22

Excuse me for asking idiot questions. I want to perform some htaccess protections and need clarification. I’ve been engaged in an 8 week crash course to learn everything possible about operating the WP platform.

domain’s root HTAccess file.

Here is my install structure.

/
htdocs
wpblog

Is the htdocs where root htaccess resides or is that in wpblog? I’m assuming htdocs.

[ Gravatar Icon ]

Jeff Starr#23

Steve, you’ll want to place it in the web-accessible root directory, which in your case looks like htdocs. One way to check is to upload some random file from either directory and then see which one appears at domain.tld/random.file (as opposed to domain.tld/wpblog/random.file).

[ Gravatar Icon ]

Steve#24

Thanks Jeff,

The only code in my htdocs .htaccess was . .

Options -Indexes

I placed it below that. Correct?

Also I sent you an email about my wpblog .htaccess

Actually wpblog is not the real name of my Wordpress root folder but I’m turning into a security fanatic now. Not quite a black belt yet though by a long shot.

[ Gravatar Icon ]

Jeff Starr#25

Hi Steve, yes it should work fine there below the Options directive. I’ll reply to email as soon as I get to it (hopefully soon).

[ Gravatar Icon ]

Steve#26

Okay so my blogs root .htaccess is my master gatekeeper?

# END WordPress

# protect phpinfo

Order Deny,Allow
Deny from all
Allow from 123.456.789

Is this it?

[ Gravatar Icon ]

Jeff Starr#27

Hey Steve, I wouldn’t say that, but it can go far in protecting sensitive material such as phpinfo.php.

Also, the code you posted looks incorrect.. remember to wrap your code in <code> tags so that WordPress doesn’t gobble it..

[ Gravatar Icon ]

brett#28

couldn’t you also chmod the file so it wouldn’t be publicly available?

[ Gravatar Icon ]

keli#29

@brett: chmod won’t help with a phpinfo file as it only controls local access. Sure, you can deny it from apache, but then what’s the point, you might as well just delete it, or keep it outside the webroot. Locally there’s not much info in the phpinfo file …

[ Gravatar Icon ]

Eric#30

@connie et al: security through obscurity is NEVER a good idea.
Like removing the “Entrance” sign but keep the door unlocked…
It just delays the weakness being found.
Now you can argue wheather a “difficult to gues” file name is not equivalent to a password. It is not!
Anyone who manages to list the root directory has no bostacle anymore.

[ Gravatar Icon ]

Jeff Starr#31

Now you can argue wheather a “difficult to gues” file name is not equivalent to a password. It is not!

Technically, isn’t using a password also “security through obscurity”?

How is guessing a password any different than guessing a file name?

[ Gravatar Icon ]

Tanveer Malik#32

how can we protect multiple file, like if we want to protect index.php file at the same time as well?

[ Gravatar Icon ]

Jeff Starr#33

Many ways to protect multiple files with htaccess.. either using regular expression or multiple deny blocks (or both).

[ Gravatar Icon ]

James#34

Not sure why but I was getting internal server error msg when tried to use the code in htaccess!! Didn’t check but most probably I placed the code in between of some other htaccess codes …Anyway instead of htaccess I used the Rod Homor trick which works great :) … Thanks @Rod Homor

Trackbacks / Pingbacks
  1. Is it Secret? Is it Safe? | Webplus - web developer resource blog
  2. Is it Secret? Is it Safe? | Lively Web Tuts
  3. uberVU - social comments
  4. Secure your phpinfo.php files with .htaccess • Perishable Press « Netcrema – creme de la social news via digg + delicious + stumpleupon + reddit
  5. === popurls.com === popular today
Share your thoughts..

Read Comment Policy

Comment Rules: No spam. No profanity. Use your real name. You may use simple HTML tags for style. Wrap all code in <code> tags. Learn more.



Attention: Do NOT follow this link!