View Full Version : Resolved create file outside home directory
ggalan
01-03-2012, 12:27 AM
how can i append my vhost list, which is outside of my home directory
from the diagram below, is it possible from myfile.php to affect httpd.conf using fopen?
root
|-var
| |-www
| |-html
| |-myfile.php
|
|-etc
|-httpd
|-conf
|-httpd.conf
$Handle = fopen("httpd.conf", "a");
$Data = "some string";
fwrite($Handle, $Data);
fclose($Handle);
djr33
01-03-2012, 04:45 AM
1. httpd.conf is not a string. It should give a warning (which might not be shown) and may default to that value as a string, but don't rely on it. You should be using quotes around it.
2. You can get to the higher level file in either of the following ways:
../../../../ect/httpd/conf/httpd.conf (relative)
/etc/httpd/conf/httpd.conf (absolute)
3. You should NOT be modifying your server's configuration from PHP. A well-designed server should not allow this (file permissions), but even if it does, this is incredibly dangerous.
On a more technical note, I doubt it would work. I expect that you'd need to restart the server (or at least reset something) for the new configuration to take effect, so you couldn't actually do this using PHP unless you also restarted the server at the same time.
At the very least, this should absolutely only be on very strongly protected admin pages in case you need to do something important to your server (for example, block all requests while you try to stop a hacker).
4. Also, if this is a serious piece of code, what you're doing would probably completely crash the server. You would need to parse the httpd.conf file then modify a certain part of the file in a valid way. Just adding some text to the end (or replacing the whole file with random text) is a great way to cause severe problems. Of course you could technically do this with PHP (in terms of editing a file), but it would need to be a lot more complicated than what you've shown here in terms of setting up the content for the file. The easiest way could be to design a template for the file then insert some values using PHP into it. I'd also strongly recommend replacing the entire file rather than just adding some content to the end-- as I said, that probably won't work. But in order to do that, you'd first need to get the current values and/or generate a full httpd.conf settings string.
ggalan
01-03-2012, 05:30 AM
appending vhost to a conf file doesnt seem like such a big deal
apachectl graceful will reload the file
also this can be an included file from the main conf
djr33
01-03-2012, 06:54 AM
Technically, there's nothing wrong with it. I still think it may be a little awkward to get it running again (you have to reset the server somehow, right?), but generally it should be fine.
But as I said, the real issue is that if you make a mistake (or if someone manages to hack your PHP and take control of what is added), this can easily break everything. If there's a typo, the server may stop working until you fix it manually.
This is something like robots doing heart surgery. If it works, it's a great idea. But a typo is very, very bad.
ggalan
01-03-2012, 03:42 PM
good point, i guess i will need regex to tightly control how this file gets made
ive seen lots of automation like this, didnt think much of it at the time
ggalan
01-03-2012, 05:28 PM
but getting back to my question, i cant seem to create files outside of the html directory using php. i guess i would need bash script or something lower level to manipulate the entire process
henda
01-03-2012, 05:34 PM
i cant seem to create files outside of the html directory using php.
That's more than likely down to your directory permissions. Chmod the required directory to give your script read/write/execute access.
ggalan
01-03-2012, 05:48 PM
i have the www directory as
sudo chmod 777 /var/www/
but cant make anything outside of html
henda
01-03-2012, 05:58 PM
Yes that should allow you to write to /var/www/
say you want to write to any subfolder in /var/www/ you would need to add a -R to your chmod commad to give it recursive permissions e.g
sudo chmod 777-R /var/www/
If you're wanting to write to say /var/someotherfolder/ you need to make sure that directory too has the appropriate directive access by chmoding it.
Be warned when changing directive permissions in that you don't affect the security of your application.
ggalan
01-03-2012, 06:28 PM
is it possible to create a directory called "conf.x" inside httpd
and give conf.x 777
while httpd has 755
and write into conf.x
?
re: it seems to work
henda
01-03-2012, 06:49 PM
I don't see why not. Just be careful in what you do and who you give access to these scripts.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.