Reply
Ok.. how to check a string has allowed begining... help
Old 09-21-2007, 07:54 PM Ok.. how to check a string has allowed begining... help
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 5,888
Name: Dan
Location: Swindon
Ok for a file editor thing i got i have it get the location of the file im editing/viewing vie get file so it unsafe as could potentionally view and edit any file on system.

i have a defined thing which has the allowed path so like for example on my testing server thats M:/server/xampp/htdocs/calm/ i have this defined and is PHP_FILE_TREE_PATH

i made a really bad and sleepy attempt with this :P

PHP Code:
$file_path "/".PHP_FILE_TREE_PATH."^/";
if(
preg_match($file_path$file_edit$matches))
{
if(
$matches 1)
{
readfile($file_edit);
}
}
else{ echo 
'Hack attempt detected'; }
#edit file_edit isset 
So basically my idea being i want to check that the first part of the path matched the allowed, which mean sthey are only accessing a file above the allowed dir Got me?

So how do i do this?
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
When You Register, These Ads Go Away!
     
Old 09-22-2007, 01:06 AM Re: Ok.. how to check a string has allowed begining... help
Extreme Talker

Posts: 223
Location: United States
Regular expressions aren't the fastest things in the world, so it's best to avoid them if possible. I think this might be such a case here where you don't need them, but it depends how you set things up.

PHP Code:
if (strpos($file_editPHP_FILE_TREE_PATH) === 0)){ // if PHP_FILE_TREE_PATH is at the beginning of $file_edit...
  
readfile($file_edit);
}else{
  echo 
'Hack attempt detected';

Some notes: if you are using PHP 5, you can replace strpos with stripos (case insensitive). If not, you will want to do a strtolower() before you execute strpos().

On a side note, I actually don't like this way of validating which files can be edited. It just doesn't seem very secure. What if someone enters "/a/valid/directory/../../private_directory/file.php"? I don't know what would happen there. I'd personally prefer to use a database table to list specific files. It would likely be more secure because the users could only choose files listed in the table, and you could add more functionality, such as group file permissions.
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.

Last edited by frost : 09-22-2007 at 01:08 AM.
frost is offline
Reply With Quote
View Public Profile
 
Old 09-22-2007, 08:55 AM Re: Ok.. how to check a string has allowed begining... help
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 5,888
Name: Dan
Location: Swindon
Thanks, i guess its better than non, and i will probably end up developing this further in the futrue.

i will test its security as well so i know it weaknesss.
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-22-2007, 08:59 AM Re: Ok.. how to check a string has allowed begining... help
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 5,888
Name: Dan
Location: Swindon
Just tested it and yes it has the exact weakness you said if i directly try to access anything before the allowed folder it shows hack attempt iff i do allowed/../notalowed it shows the file.

How ever i am not amazingly worried as this would be intended for a CMS were it would be in a protected area, and only if they also have access to change CMOD settings could the edit so all they could really do was look.. unless the file is cmodded so its writeable.

But thanks, this is a Ok temp thing.

Ok i just had a thought i would like to throw at you.

As you can probably tell i would like to get a CMS project together, would you be interested in developing the file manager?
should be a fairly basic thing, just something where users can edit their files (NOT CONTENT) basically so they could modify a template or a script or what have you. basically something to view some of the files they might like to hack a bit.

Dan
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)

Last edited by dansgalaxy : 09-22-2007 at 09:02 AM.
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-22-2007, 09:59 AM Re: Ok.. how to check a string has allowed begining... help
Novice Talker

Posts: 9
u really did this?
zaidi is offline
Reply With Quote
View Public Profile
 
Old 09-22-2007, 10:32 AM Re: Ok.. how to check a string has allowed begining... help
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 5,888
Name: Dan
Location: Swindon
What are you refering to me doing and ill tell you if i did it :?
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-24-2007, 09:08 PM Re: Ok.. how to check a string has allowed begining... help
mgraphic's Avatar
Truth Seeker

Posts: 2,285
Name: Keith Marshall
Location: West Hartford, CT
Dan, I noticed one thing that might cause some trouble:

$file_path = "/".PHP_FILE_TREE_PATH."^/";

The carrot (^) is to mark the beginning of a string. To mark the end of a string use ($)

$file_path = "/".PHP_FILE_TREE_PATH."$/";
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 09-25-2007, 07:57 AM Re: Ok.. how to check a string has allowed begining... help
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 5,888
Name: Dan
Location: Swindon
yea i forgot

im using frosts snippet at the moment even tho it isnt fully secure.
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to Ok.. how to check a string has allowed begining... help
 

Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.36142 seconds with 13 queries