This is a discussion on "Reliable and secure php templating system" within the PHP Forum section. This forum, and the thread "Reliable and secure php templating system are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Reliable and secure php templating system
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
Reliable and secure php templating system
Hello,
My first post in the php forum as I am no expert on php, I come from a graphical environment. I have been using a simple template system written in php to design smaller websites but one was hacked into and when asked by the host if I am running any scripts, the only thing I could think of was the php script for the template:
The host replied that this script would allow remote inclusion of files onto the site and therefore the script should be removed. And there goes my template system... Now, is this really vulnerable? I followed an article by A List Apart when creating it (http://www.alistapart.com/articles/phpcms/). If the code is no good as it stands, would there be any workarounds to get this snippet safer? Many thanks in advance, J |
|
|
|
#2
|
|||
|
|||
|
Re: Reliable and secure php templating system
Can I just ask what this script is supposed to do?
|
|
#3
|
||||
|
||||
|
Re: Reliable and secure php templating system
Okay so the code is pretty dangerous because I can just specify any file I want to access on your site and PHP will server it up. This can give access to passwords whatever else you have on your site.
It would be much safer to specify an array of allowed files for your templating system
By storing all the files you want access to in the $pages array, anything bar the index of the array will default to home. so http://www.example.com/index.php?page=other will include $pages['other'] but http://www.example.com/index.php?page=haxedYourSite will only include $pages['home'] Cheers,
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
|
#4
|
|||
|
|||
|
Re: Reliable and secure php templating system
Hello, many thanks for replying so quickly.
AdRock, what this script does is calling html files from a template (a php file). The idea is to have one master page so to speak which contains the design of the site, and the code is placed on that page so that when you open a page of the type, say, template.php?page=yourpage, yourpage is insterted into the template. Yourpage is a plain html file with your page specific content. Rakuli, thanks for that. However, then you must specify *all* the files that *can* be accessed, which can get pretty tedious if you have 100 pages or more to include. No? |
|
#5
|
|||
|
|||
|
Re: Reliable and secure php templating system
I wrote a tutorial about this for next month's newsletter. Should be available for reading in the next week or so...
Last Blog Entry: 10 Reasons Why My Laptop Is Better Than Your Girlfriend (Dec 15th, 2007)
|
|
#6
|
|||
|
|||
|
Re: Reliable and secure php templating system
Then I'd better pay attention!
|
|
#7
|
|||
|
|||
|
Re: Reliable and secure php templating system
Quote:
This a small version of what I think you are trying to do This is where you want the content to change
and how you would use the links
|
|
#8
|
||||
|
||||
|
Re: Reliable and secure php templating system
Yes it will be a large array but just including any file sent in the query string is asking to be haxed.
If you created a uniform naming structure for your files and directories you could simplify it somewhat. EG.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Last edited by Rakuli; Oct 24th, 2007 at 13:24. |
|
#9
|
|||
|
|||
|
Re: Reliable and secure php templating system
AdRock, that is basically what I have been doing, yes, but with the specific code I posted above and with links looking like this:
/template.php?page=somepage /template.php?dir=somedirectory&page=somepage Rakuli, I will give your alternative some thought. Someone suggested this rewrite to me - what do you think of it?
Last edited by HitByLife; Oct 24th, 2007 at 13:41. Reason: fix error |
|
#10
|
||||
|
||||
|
Re: Reliable and secure php templating system
That last solution would be safer but still gives access to any html file on your site which I guess is less of a problem.
Probably better to use paths though instead of URL's. EG $file = 'path/to/tfile'; etc to avoid someone sending some encoded URL characters to cause a loop in your script.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
|
#11
|
|||
|
|||
|
Re: Reliable and secure php templating system
Rakuli, thanks.
Most of the sites I have under development at the moment have no sensitive data stored, which currently goes for the site that got hacked into as well. What the hacker did there was to place a massmailer script and later on he used the site for phishing (not sure it was the same hacker). The result for me was a suspension of the service. Luckily it was not a clients site, but a personal site. Now, will the above script stop the hacker from placing files on my server? |
|
#12
|
|||
|
|||
|
Re: Reliable and secure php templating system
AdRock, can I ask something? I notice you use 'break' in your code - what's that for?
|
|
#13
|
||||
|
||||
|
Re: Reliable and secure php templating system
If that is all your script is doing with the query string then a hacker should not be able to place a file on your site with it.
PS - the switch statement is like an if else swicth ($condition) { case 'this1' : do this break; // don't do any more with this1 case 'this2' : do this case 'this3' : do this ( with thi2 && this3) break; // no more with this 2 & 3 default : do this cause there was no match } It's a structure from C, faster and more compact than a big if else statement.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Last edited by Rakuli; Oct 24th, 2007 at 14:36. |
|
#14
|
|||
|
|||
|
Re: Reliable and secure php templating system
The break gets out of the switch statement because a condition has been met
|
|
#15
|
|||
|
|||
|
Re: Reliable and secure php templating system
Like I said earlier, if you can wait till the next CC Newsletter comes out, I have a beautiful, elegant, dynamic solution to exactly this problem.
But you'll have to wait a few days, I'm not allowed to give away the goodies ahead of time
Last Blog Entry: 10 Reasons Why My Laptop Is Better Than Your Girlfriend (Dec 15th, 2007)
|
|
#16
|
|||
|