How to make php webpage to access the file in /root directory?
I will forget about the security implications of doing this and will go down to business:
If you have done ls -l /var/www
and ls -l /root
you would have noticed that both has different permissions:
$ ls -l /root/cr.txt
total 2
-rw-r----- 1 root root 0 Jul 9 01:28 cr.txt
$ ls -l /var/www
total 2
-rw-r--r-- 1 www-data www-data 0 Jul 9 01:28 somefile
The /root
is only readable for root
, while /var/www
is readable by www-data
user. Now, if you check apache process you will notice that it's running using the www-data
user.
$ ps aux | grep apache www-data 5133 0.0 0.2 6512 1208 ? R+ 10:04 0:00 apache
Now, you are trying to make apache running with the www-data
user read the file. You can take three courses of action:
Move the file to
/var/www
and change it's permissions sowww-data
users can read it.mv /root/cr.txt /var/www/ chown www-data:www-data /var/www/cr.txt
This is the preferable method.
Create a symlink to the file in the
/var/www
directory:ln -s /root/cr.txt /var/www/
This won't ensure that your file is being read, in some cases.
This is dangerous and should not be done! Add the
www-data
user to theroot
group, or change the file ownership so it could be read bywww-data
users:chown :www-data /root/cr.txt ## Or sudo adduser www-data root
This shouldn't be done if you don't understand the risks!
Related videos on Youtube
acekapila
Offensive Security Certified Professional(OSCP) seeking a challenge roles and opportunities in the area of Information Security community. Dedicated towards learning and providing information security solutions. Contact for best in class information security solutions.
Updated on September 18, 2022Comments
-
acekapila over 1 year
I am using Linux 12.04,apache and php is installed on it.I want to access a text file in /root/ folder.I am really confused with the permissions.The php script i m using
<?php $file = fopen("/root/cr.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "<br>"; } fclose($file); ?>
This script is able to access the file /var/www folder but not able to access /root/ip.txt file. Please help and explain step to step possible.
-
karlingen over 10 yearsDouble-check your code. You are saying /root/ip.txt in the comment but in the code you are writing /root/cr.txt
-
Braiam over 10 yearsEdit your post and include the output of
ls -l /root/cr.txt
-
-
acekapila over 10 yearsi have added the www-data to root ,but still it's not working.The script is not able to get content of /root/cr.txt