PHP Web Host - Quality Web Hosting For All PHP Applications $35/month $250/year (Unlimited) - $25/month - 200,000 impressions - Your Ad Could be Here - Click For Details
  Login or Register
 • Home • Downloads • Your Account • Forums • 

View next topic
View previous topic


Google
 
Web RavenPHPScripts (This Site)
Post new topic   Reply to topic
Author Message
Dawg
Life Cycles Becoming CPU Cycles


Joined: Nov 07, 2003
Posts: 564

PostPosted: Sun Sep 16, 2007 9:12 pm Reply with quote Back to top

Greetings All,
I am using the include function in several apps and I just wanted to make sure I am doing it correctly and securely.

Code:

<?php
  $path = 'pages/';
  $extension = '.php';
 
  if ( preg_match("#^[a-z0-9_]+$#i",$page) ){
    $filename = $path.$page.$extension;
    include($filename);
  }
?>


Does this look right or is there a better way?

Dave
View user's profile Send private message
Dawg
Life Cycles Becoming CPU Cycles


Joined: Nov 07, 2003
Posts: 564

PostPosted: Mon Sep 17, 2007 5:55 am Reply with quote Back to top

I started playing around with this today and my first example I could not get to work....

Code:
<?php
$path="/full/path/to/script/";
if (getdomain($path) == 'yourdomain'){
     include($path.'somefile.php');
}
?>


I am going to try this one now....
View user's profile Send private message
Dawg
Life Cycles Becoming CPU Cycles


Joined: Nov 07, 2003
Posts: 564

PostPosted: Mon Sep 17, 2007 5:56 am Reply with quote Back to top

but that is not going to work with a remote include.....back to the drawing board...
View user's profile Send private message
montego
Site Admin


Joined: Aug 29, 2004
Posts: 7487
Location: Arizona

PostPosted: Mon Sep 17, 2007 6:14 am Reply with quote Back to top

The important thing to keep in mind is that you can use relative pathing, but it is relative to the original script that was requested via the browser. So, for example, if this is a nuke module, than the script which was requested was modules.php (unless include_path is being used - rare).

Another thing to keep in mind is variable scope. If you include a file within a function, for example, the variables and code become a part of that function's scope.
View user's profile Send private message Visit poster's website
montego
Site Admin


Joined: Aug 29, 2004
Posts: 7487
Location: Arizona

PostPosted: Mon Sep 17, 2007 6:15 am Reply with quote Back to top

Sorry, forgot to mention a few other things:

Some hosts will restrict you from being able to include a file that is outside your web account, so if that is what you were trying to do in the first example, what was the error?

Also, many hosts restrict remote includes...
View user's profile Send private message Visit poster's website
Dawg
Life Cycles Becoming CPU Cycles


Joined: Nov 07, 2003
Posts: 564

PostPosted: Mon Sep 17, 2007 6:29 am Reply with quote Back to top

montego,
Thank You for replying this really has me stumped!

I have the scripts working with a regular include. I am just trying to secure them againest injection.

Code:

<?php
  $path = 'http://www.mysever.com/path1/path2/';
  $extension = '.php';
 
  if ( preg_match("#^[a-z0-9_]+$#i",$page) ){
    $filename = $path.$page.$extension;
    include($filename);
  }
?>


I would have thought that would work....but it doesn't....

Hackers are such a PITA!

Dawg
View user's profile Send private message
Dawg
Life Cycles Becoming CPU Cycles


Joined: Nov 07, 2003
Posts: 564

PostPosted: Mon Sep 17, 2007 6:34 am Reply with quote Back to top

Would this stop them from injecting?

Code:


$myserver="http://www.mysever.com";
$path="/full/path/to/script/";
if ($myserver($path) == 'myserver'){
     include($myserver.$path.'somefile.php');
}
View user's profile Send private message
Dawg
Life Cycles Becoming CPU Cycles


Joined: Nov 07, 2003
Posts: 564

PostPosted: Mon Sep 17, 2007 8:04 am Reply with quote Back to top

Ok I am officialy STUCK!

I have an include that points at
Only registered users can see links on this board!
Get registered or login to the forums!


I want to secure this againest remote file attacks.

How do I do this?

Dawg
View user's profile Send private message
technocrat
Involved
Involved


Joined: Jul 07, 2005
Posts: 492

PostPosted: Mon Sep 17, 2007 9:04 am Reply with quote Back to top

Use base paths instead.

example:
Lets say that a file your including is in the same folder as this file. You would use
include_once(dirname(__FILE__).'/file.php');

If it's lets say
/html/blocks/file.php
And this file is
/html/modules/whatever/includer.php
include_once(dirname(dirname(dirname(__FILE__)))).'/blocks/file.php');

This should help you stop anything that is not alphanumeric or a _
if (preg_match("/[^\w_\-]/i",$file)) {
die('Invalid File Name');
}
View user's profile Send private message
Dawg
Life Cycles Becoming CPU Cycles


Joined: Nov 07, 2003
Posts: 564

PostPosted: Mon Sep 17, 2007 9:16 am Reply with quote Back to top

technocrat,

Thank You for the input but it is a remote file that I am including. I have a bunch of weather models that run on another server (my server....my models) and these includes bring in the files to be included in the site.

Problem one is getting rid of other sites that were using my stuff as their own....I fixed that. Now I am just trying to secure everything down tight to keep out the hackers.

Thank You again for your time and input cause I am STUCK!

Dawg
View user's profile Send private message
Gremmie
Former Moderator in Good Standing


Joined: Apr 06, 2006
Posts: 2401
Location: Iowa, USA

PostPosted: Mon Sep 17, 2007 9:56 am Reply with quote Back to top

Dawg wrote:

I have the scripts working with a regular include. I am just trying to secure them againest injection.

Code:

<?php
  $path = 'http://www.mysever.com/path1/path2/';
  $extension = '.php';
 
  if ( preg_match("#^[a-z0-9_]+$#i",$page) ){
    $filename = $path.$page.$extension;
    include($filename);
  }
?>


I would have thought that would work....but it doesn't....

Hackers are such a PITA!

Dawg


Your regular expression seems fine to me at first glance. If in doubt test it first before trying to use it in the include_once.

If instead you know in advance what "pages" you are going to call on the remote server you could forgo the regular expression and do something like:

Code:

$allowedPages = array('xyz', 'aaa', 'etc');
if (in_array($page, $allowedPages))
{
   include_once ...
}



Your host may not allow remote inclusion and maybe that is why it isn't working.
View user's profile Send private message
Dawg
Life Cycles Becoming CPU Cycles


Joined: Nov 07, 2003
Posts: 564

PostPosted: Mon Sep 17, 2007 10:53 am Reply with quote Back to top

Gremmie,
Thank You for your time. It works just fine with include...I just want to lock it down from the hackers.

Dave
View user's profile Send private message
Gremmie
Former Moderator in Good Standing


Joined: Apr 06, 2006
Posts: 2401
Location: Iowa, USA

PostPosted: Mon Sep 17, 2007 1:34 pm Reply with quote Back to top

Your code that I quoted looked fine. Why did you say it didn't work?
View user's profile Send private message
Display posts from previous:       
Post new topic   Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Forums ©
 

All logos and trademarks in this site are property of their respective owner.
The comments are property of their posters, all the rest © 2002-2008 by Raven
Proud to be listed at Lobo Links Web Directory

You can syndicate our news using the file xml

CSE HTML Validator Helped Clean up This Page! [Valid RSS] valid RSS 2.0 Valid robots.txt Stop Spam Harvesters, Join Project Honey Pot

Website engines core code is © copyright by PHP-Nuke but has been heavily patched and modified by myself and others.
PHP-Nuke is a free software released under the GNU/GPL.


:: fisubice phpbb2 style by Daz :: PHP-Nuke theme by www.nukemods.com ::

:: fisubice Theme Recoded To 100% W3C CSS & HTML 4.01 Transitional Compliance by Raven and 64bitguy ::

:: W3C CSS Compliance Validation :: W3C HTML 4.01 Transitional Compliance Validation ::

zerosum