By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,272 Members | 1,403 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,272 IT Pros & Developers. It's quick & easy.

Flat file security

P: n/a
Hi all,

I'm sure this is a popular question that comes up every few months
here. Indeed, I've looked at some of the past postings, but I would
like to ask things differently.

Basically, I'm using a flat file to storing data. I have to do this
because mySQL is not installed on my web server, and I am not the root
user. The amount of data is so small, that it isn't worth a full-blown
database anyway. However, while the data is nothing valuable
(generally e-mail addresses), I would like to make it as secure as
possible. Both from robots and from other users.

I found this useful posting in comp.lang.php (some parts cut) dating
from 2002:

-----
1. Put the file containing userdata _outside_ your webdirectory.

or

2. Use a robots.txt to tell robots to not read the data.
Save the file to root on your web as robots.txt, and (as an example)
with
the following content:

User-Agent: *
Disallow: /directory_containing_a_lot_of_email_adresses_and
_other_juicy_user_stuff

or

3. Wrap the data in an auth of some sort (may be difficult if you use
that
data for the auth....;-)
-----

I can do #1 and I was wondering if that is sufficient. As the non-root
user, I guess I cannot do #2... Can I also move the php scripts that
write the flat files outside my web directory? Or is that not
necessary?

Also, as the host is a Unix machine, what permissions are suggested for
the following? Of course, I only want the web server and me to be able
to read and write to them. I'm thought about the permissions and have
inserted them below.

1) directory of the php scripts that writes the flat files
-rwx---r-x

2) the php scripts that writes the flat files
-rwx---r-x

3) the directory of the flat files
-rwx---rwx

4) the flat files themselves
-rwx---rw-

Is this possible? Can I do better?

I'm also new to php... I've hard-coded the paths to the flat files
inside my php files, as one must, I guess. Is there a way for people
to see the source of the php files so that they can extract the hard
coded paths?

Thank you!

Ray

Oct 3 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
ra******@gmail.com wrote:
I can do #1 and I was wondering if that is sufficient. As the non-root
user, I guess I cannot do #2... Can I also move the php scripts that
write the flat files outside my web directory? Or is that not
necessary?
My pick would also be option #1. Moving the php scripts outside the
webdirectory is not only not necessary, but also impossible if you still
want to execute them from the web.
1) directory of the php scripts that writes the flat files
-rwx---r-x

2) the php scripts that writes the flat files
-rwx---r-x

3) the directory of the flat files
-rwx---rwx

4) the flat files themselves
-rwx---rw-

Is this possible? Can I do better?
I'm not sure why you leave all the group permissions empty and why other
(world) do get permissions. If the webserver user is the owner of the
flat files directory, you can change that to -rwx------
Same goes for the flat files themselves.
I'm also new to php... I've hard-coded the paths to the flat files
inside my php files, as one must, I guess. Is there a way for people
to see the source of the php files so that they can extract the hard
coded paths?


No, not as long as PHP works on the webserver, because the script gets
interpreted by the webserver and only the output of the scripts is being
sent to the client (webbrowser).

--
http://www.phpforums.nl
Oct 3 '05 #2

P: n/a
ra******@gmail.com wrote:
Hi all,

I'm sure this is a popular question that comes up every few months
here. Indeed, I've looked at some of the past postings, but I would
like to ask things differently.

Basically, I'm using a flat file to storing data. I have to do this
because mySQL is not installed on my web server, and I am not the root
user. The amount of data is so small, that it isn't worth a full-blown
database anyway. However, while the data is nothing valuable
(generally e-mail addresses), I would like to make it as secure as
possible. Both from robots and from other users.

I found this useful posting in comp.lang.php (some parts cut) dating
from 2002:

-----
1. Put the file containing userdata _outside_ your webdirectory.

or

2. Use a robots.txt to tell robots to not read the data.
Save the file to root on your web as robots.txt, and (as an example)
with
the following content:

User-Agent: *
Disallow: /directory_containing_a_lot_of_email_adresses_and
_other_juicy_user_stuff

or

3. Wrap the data in an auth of some sort (may be difficult if you use
that
data for the auth....;-)
-----

I can do #1 and I was wondering if that is sufficient.
No, the only advantage working outside webroot is that a simple request to
the right place will not be answered by the webserver.
But you can also do so by other means.
As the non-root user, I guess I cannot do #2...
Yes you can.
you can place a robots.txt file just as you can place any other text file.
Can I also move the php scripts that write the flat files outside my web directory? Or is that not
necessary?
Yes, can be done.
Pay attention to permissions however. :-)
If you do not, you can end up with files that are readable to the world,
that is 'everybody' who has access to your system.
When using shared hosting, that is everybody else on the same system.

Also, as the host is a Unix machine, what permissions are suggested for
the following? Of course, I only want the web server and me to be able
to read and write to them. I'm thought about the permissions and have
inserted them below.

1) directory of the php scripts that writes the flat files
-rwx---r-x

2) the php scripts that writes the flat files
-rwx---r-x

3) the directory of the flat files
-rwx---rwx

4) the flat files themselves
-rwx---rw-

Is this possible? Can I do better?
Yes you can.
Suppose I am on the same machine:
- I can see directory 3)
- I can browse the content of directory 3)
- I can read/modify file in directory 3)


I'm also new to php... I've hard-coded the paths to the flat files
inside my php files, as one must, I guess. Is there a way for people
to see the source of the php files so that they can extract the hard
coded paths?
Sometimes.
Your php scripts have permission -rwx---r-x, so if I am on the same machine,
I can possible read your PHP files.

Some time ago I discussed a similar problem with macbri.
Here is a link:
http://groups.google.nl/group/comp.l...398dedf888542a

Maybe that helps setting up something a lot more secure.
It involves denying directorylistings combined with a very long strange name
for a directory.

I hope it helps.
Good luck.

Regards,
Erwin Moller

Thank you!

Ray


Oct 3 '05 #3

P: n/a
Ray
Peter van Schie wrote:
ra******@gmail.com wrote:
I can do #1 and I was wondering if that is sufficient. As the non-root
user, I guess I cannot do #2... Can I also move the php scripts that My pick would also be option #1. Moving the php scripts outside the
webdirectory is not only not necessary, but also impossible if you still
want to execute them from the web.


Ah, I see. I didn't know it would not be possible to run php scripts
if they are outside the web directory. Thanks!
1) directory of the php scripts that writes the flat files
-rwx---r-x

2) the php scripts that writes the flat files
-rwx---r-x

3) the directory of the flat files
-rwx---rwx

4) the flat files themselves
-rwx---rw-

I'm not sure why you leave all the group permissions empty and why other
(world) do get permissions. If the webserver user is the owner of the
flat files directory, you can change that to -rwx------
Same goes for the flat files themselves.


Hmmm...I noticed the files are made by the user www-data. I guess I
didn't know what privileges it had. I enabled the world permissions
for the directories and files for it...

I also tried chown'ing the file so that www-data owns it. I also tried
to create a group so that only I and www-data are in it. Neither seems
to work as I'm not the root user.

But, say I could make files owned by www-data and give it -rwx------.
How could I read them?
No, not as long as PHP works on the webserver, because the script gets
interpreted by the webserver and only the output of the scripts is being
sent to the client (webbrowser).


Since php is server-side code, this is what I thought; but I also was
worried that there is some way to get around it. Thanks for confirming
to me that there isn't.

Ray

Oct 3 '05 #4

P: n/a
Ray
Hi Erwin,

Erwin Moller wrote:
....
I can do #1 and I was wondering if that is sufficient. No, the only advantage working outside webroot is that a simple request to
the right place will not be answered by the webserver.
But you can also do so by other means.


I am tempted to ask you what you mean by "other means", but on second
thought, the less people who know, the better. Good enough for me to
know that it isn't perfect security.
As the non-root
user, I guess I cannot do #2... Yes you can.
you can place a robots.txt file just as you can place any other text file.


Ah! I didn't know that...thank you!
Yes, can be done.
Pay attention to permissions however. :-)
If you do not, you can end up with files that are readable to the world,
that is 'everybody' who has access to your system.
When using shared hosting, that is everybody else on the same system.
Ah! I see...
Yes you can.
Suppose I am on the same machine:
- I can see directory 3)
- I can browse the content of directory 3)
- I can read/modify file in directory 3)
I said this in my previous reply, but I guess my main problem is that I
don't understand www-data -- the account which runs the web server and
thus, creates these files.

I tried changing ownership to it and creating a group with only me and
it, but neither worked because I'm not the root user. Perhaps I am
doing something wrong, but I am having problems chown'ing my own
files... Or, maybe the sysadmin has disallowed its use? Likewise for
creating a new group.
Some time ago I discussed a similar problem with macbri.
Here is a link:
http://groups.google.nl/group/comp.l...398dedf888542a

Maybe that helps setting up something a lot more secure.
It involves denying directorylistings combined with a very long strange name
for a directory.


Thanks! I'm going through it now and it looks long and detailed.
Thank you for the original posting...I'll make sure the extensive
typing you did is used at least one more time by me. :)

Ray

Oct 3 '05 #5

P: n/a
On 3 Oct 2005 04:02:57 -0700, "Ray" <ra******@gmail.com> wrote:
Peter van Schie wrote:
ra******@gmail.com wrote:
> I can do #1 and I was wondering if that is sufficient. As the non-root
> user, I guess I cannot do #2... Can I also move the php scripts that My pick would also be option #1. Moving the php scripts outside the
webdirectory is not only not necessary, but also impossible if you still
want to execute them from the web.


Ah, I see. I didn't know it would not be possible to run php scripts
if they are outside the web directory. Thanks!


Well, actually, you can. There are, of course, multiple ways of doing
so.
> 1) directory of the php scripts that writes the flat files
> -rwx---r-x
>
> 2) the php scripts that writes the flat files
> -rwx---r-x
>
> 3) the directory of the flat files
> -rwx---rwx
>
> 4) the flat files themselves
> -rwx---rw- I'm not sure why you leave all the group permissions empty and why other
(world) do get permissions. If the webserver user is the owner of the
flat files directory, you can change that to -rwx------
Same goes for the flat files themselves.


Hmmm...I noticed the files are made by the user www-data. I guess I
didn't know what privileges it had. I enabled the world permissions
for the directories and files for it...


Bad idea.
I also tried chown'ing the file so that www-data owns it. I also tried
to create a group so that only I and www-data are in it. Neither seems
to work as I'm not the root user.
Yep. You need root access to change the owner on a good Unix.
But, say I could make files owned by www-data and give it -rwx------.
How could I read them?
No, not as long as PHP works on the webserver, because the script gets
interpreted by the webserver and only the output of the scripts is being
sent to the client (webbrowser).

Unless, of course, you've got a bug in your php code. Any simple
mistake could lead to opening your source to the world.
Since php is server-side code, this is what I thought; but I also was
worried that there is some way to get around it. Thanks for confirming
to me that there isn't.

Ray


--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
DataBasix | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ۳
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Oct 3 '05 #6

P: n/a
On 3 Oct 2005 04:16:21 -0700, "Ray" <ra******@gmail.com> wrote:
Hi Erwin,

Erwin Moller wrote:
...
> I can do #1 and I was wondering if that is sufficient.

No, the only advantage working outside webroot is that a simple request to
the right place will not be answered by the webserver.
But you can also do so by other means.


I am tempted to ask you what you mean by "other means", but on second
thought, the less people who know, the better. Good enough for me to
know that it isn't perfect security.


Security by obscurity isn't a good idea. Ignoring security risks is
worse.
--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
DataBasix | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ۳
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Oct 3 '05 #7

P: n/a
Ray wrote:

Hi Ray,

[snip]
But, say I could make files owned by www-data and give it -rwx------.
How could I read them?


You mean how to read the file from PHP? Just use fopen, fread and
fclose. Or even easier: file_get_contents.

Peter.
--
http://www.phpforums.nl
Oct 3 '05 #8

P: n/a
Ray wrote:
Hi Erwin,

Erwin Moller wrote:
...
> I can do #1 and I was wondering if that is sufficient. No, the only advantage working outside webroot is that a simple request
to the right place will not be answered by the webserver.
But you can also do so by other means.


I am tempted to ask you what you mean by "other means", but on second
thought, the less people who know, the better. Good enough for me to
know that it isn't perfect security.


Hi,

No secret here. :-)
On a *nix filesystem every directory and file has permissions.
If that file is in or outside public_html (or whatever your directory is
called), is of no concern when it comes to the other users of the system.
When you are the only user of the server, you probably don't have to worry
too much, but when you know you are in a shared hosting environment AND you
use rw for the 'world'/thrid argument, alarmbells should ring. :-)

As the non-root
> user, I guess I cannot do #2... Yes you can.
you can place a robots.txt file just as you can place any other text
file.


Ah! I didn't know that...thank you!


Here is more:
http://www.searchengineworld.com/rob...s_tutorial.htm

It is actually a very simple straightforward system.
But be aware that robots.txt only works when the spider is friendly and
cares about your suggestions.
The first email-harvester written by Mr. Spam and Mr. Scriptboy would not
even bother to read robots.txt.
So never rely on it when it comes to protecting your data.
It will only work for the Good Guys, like Google.

Yes, can be done.
Pay attention to permissions however. :-)
If you do not, you can end up with files that are readable to the world,
that is 'everybody' who has access to your system.
When using shared hosting, that is everybody else on the same system.
Ah! I see...
Yes you can.
Suppose I am on the same machine:
- I can see directory 3)
- I can browse the content of directory 3)
- I can read/modify file in directory 3)


I said this in my previous reply, but I guess my main problem is that I
don't understand www-data -- the account which runs the web server and
thus, creates these files.


Well, www-data is just the name of a user.
It is typically the name of a user that runs Apache.
Remember that every process on *nix is owned by a user.
So is PHP. PHP is executed by Apache, so PHP runs as user Apache.
(Things might be more complicated actually, but this is the idea.)

A few years ago www-data had another name: 'Apache' or 'nobody'.
It is just what the serveradmin decided to name it.
I think nowadays www-data is used everywhere.
(When you are unlucky enough to hit a W$ machine, the user will often be
named IUSR_machinename. But W$ doesn't have permissions like *nix has.)

I tried changing ownership to it and creating a group with only me and
it, but neither worked because I'm not the root user. Perhaps I am
doing something wrong, but I am having problems chown'ing my own
files... Or, maybe the sysadmin has disallowed its use? Likewise for
creating a new group.
True.
You don't want normal users changing ownership of files, do you?
;-)
That would make hacking/cracking too easy to give any satisfaction. :P

So: chown is a command executed by root.
Some time ago I discussed a similar problem with macbri.
Here is a link:
http://groups.google.nl/group/comp.l...398dedf888542a
Maybe that helps setting up something a lot more secure.
It involves denying directorylistings combined with a very long strange
name for a directory.
Thanks! I'm going through it now and it looks long and detailed.
Thank you for the original posting...I'll make sure the extensive
typing you did is used at least one more time by me. :)


Hehe, thanks.
When that trick was first explained to me by a guy, I decided to share it at
least 1 time with somebody else. I did twice now. ;-)

One last tip: Once you set up something you think is reasonable secure, try
to break it yourself by using another account on the same machine, if that
is possible. It is a good way of testing what the rwx means on directories
and such, which is quite confusing the first time you use them (for me).

Good luck.

Regards,
Erwin Moller

Ray


Oct 3 '05 #9

P: n/a
Ray
Gary L. Burnore wrote:
On 3 Oct 2005 04:16:21 -0700, "Ray" <ra******@gmail.com> wrote:
I am tempted to ask you what you mean by "other means", but on second
thought, the less people who know, the better. Good enough for me to
know that it isn't perfect security.


Security by obscurity isn't a good idea. Ignoring security risks is
worse.


Absolutely true...by "good enough for me", I was referring to
preventing my brain from going on overload. :) Ignoring security
risks IS bad, but let me slowly catch up instead of learning all in one
go. Progress at a snail's pace is still progress... :(

Ray

Oct 4 '05 #10

P: n/a
Ray
Hi Peter,

Peter van Schie wrote:
Ray wrote:
But, say I could make files owned by www-data and give it -rwx------.
How could I read them?


You mean how to read the file from PHP? Just use fopen, fread and
fclose. Or even easier: file_get_contents.


Ummm, no I don't mean that. I mean if the files are owned by one user
(www-data) how can another user (me in this case: ray) read them?
Wouldn't I'd get into a situation where I have files owned by someone
else sitting in my directory which I cannot read, write, or delete?
Ok...not true...I suppose I could extend what Erwin suggested and
create a php script that does these things which would then be executed
by www-data. But, from the Unix prompt, I can't do anything to them?

Something is wrong with my understanding, right?

Ray

Oct 4 '05 #11

P: n/a
Ray
Hi Erwin,

Erwin Moller wrote:
Here is more:
http://www.searchengineworld.com/rob...s_tutorial.htm
Thank you for finding it!
It is actually a very simple straightforward system.
But be aware that robots.txt only works when the spider is friendly and
cares about your suggestions.
I see. Well, if the best I can do is add many small locks instead of
one big one, I can settle with that. Thanks for the warning!
I said this in my previous reply, but I guess my main problem is that I
don't understand www-data -- the account which runs the web server and
thus, creates these files.

Well, www-data is just the name of a user.
It is typically the name of a user that runs Apache.
Remember that every process on *nix is owned by a user.


I see. Perhaps that is what confused me and why I did -------rw- in
the first place (which seems to have caused an uproar in this thread :)
). At first, I thought www-data was some special user...like root.
After playing around a bit and realizing that public web pages have to
have -------r--, I suspected that it is just a normal user.

So, my problem was that I wanted www-data to create files which I can
also read. If I own the files, one solution is to create a group and
add www-data and me to it...but I'm not the sysadmin and I cannot do a
groupadd. If www-data owns the files, then won't I have problems
reading it?
From your previous post in August, you suggested creating files owned by www-data within my directory. It feels strange to me, but perhaps I
don't understand how permissions cascade. Unix permissions is simple
enough: user, group, other/world...but when you put them within each
other is something that I still don't have a grasp of.
True.
You don't want normal users changing ownership of files, do you?
;-)
That would make hacking/cracking too easy to give any satisfaction. :P

So: chown is a command executed by root.
Well, before I tried, I thought I would get into the problem of giving
ownership to a file and later, not being able to get it back... But,
when I tried it, I was thinking I won't learn unless I do...I did and
it didn't let me. :)
Thanks! I'm going through it now and it looks long and detailed.
Thank you for the original posting...I'll make sure the extensive
typing you did is used at least one more time by me. :)

Hehe, thanks.
When that trick was first explained to me by a guy, I decided to share it at
least 1 time with somebody else. I did twice now. ;-)


Well, I'll make sure to pass it on. A "good" chain mail! :)

mySQL is great, but not everyone is a sysadmin and sometimes, you have
to make do with what you have. And, I guess it is overkill for what
I'm doing.
One last tip: Once you set up something you think is reasonable secure, try
to break it yourself by using another account on the same machine, if that
is possible. It is a good way of testing what the rwx means on directories
and such, which is quite confusing the first time you use them (for me).


Well, on that machine, I'm not the sysadmin, so I don't have the
luxury. I am a sysadmin of my machine, but it's behind a firewall.
Nevertheless, I can install a web server and give it a try...the test
won't be as good as the actual test, but maybe good enough.

Thanks for your help!

Ray

Oct 4 '05 #12

P: n/a
Ray wrote:
<snip>
Something is wrong with my understanding, right?

Ray


Hi Ray,

Possibly.
But don't worry: Nobody was born with knowledge of unix filepermissions. ;-)

Here are a few links I googled up that seem to make sense:

http://www.dartmouth.edu/~rc/help/faq/permissions.html
http://catcode.com/teachmod/
http://www.ee.surrey.ac.uk/Teaching/Unix/unix5.html

Jus a friendly word of advise:
I want to encourage you to play around with filepermissions on your OWN
system, where you have root-acces so you can change groups and such.
Just create 2 users: Ray and Erwin.
Open for both a terminal.

Let Ray create a file with certain permissions, check with what permissions
Erwin can read them.
Put Ray and Eriwn in a group, check again. etc.

This is all reasonably straightforward.

Then start playing with right on directories. This is where the fun starts.
:-)
Can Erwin list the content of a directory made by Ray?
Which setteings on the directory make this happen?
When can Erwin create a file in a directory owned by Ray?
When can Erwin delete/modify a file in a directory owned by Ray?
When can Erwin SEE a file in a directory owned by Ray?
Etc.

Really: It is WELL WORTH your time to play around for 1 day or so.
(I wished I did before publishing my first Perl-scripts may years ago. :P)
If you understand how it works, you KNOW what you are doing in your shared
hosting environment where you do not have rootaccess.
You know what www-data can and cannot do.
You know what malicious users on the same machine can do and cannot do.
Be aware that on most shared host environments, everybody can mimic
www-data, just by writing a script in php. That script then runs as
www-data.
Good luck.

Regards,
Erwin Moller

Oct 5 '05 #13

P: n/a
Ray
Hi Erwin,

Erwin Moller wrote:
Ray wrote:
<snip>
Something is wrong with my understanding, right? Hi Ray,

Possibly.
But don't worry: Nobody was born with knowledge of unix filepermissions. ;-)


haha...funny how some people look like they were born with such
knowledge. ;)
Here are a few links I googled up that seem to make sense:
Thank you for googling! So much out there...hard to find things that
are correct and understandable to someone like me.
Jus a friendly word of advise:
I want to encourage you to play around with filepermissions on your OWN
system, where you have root-acces so you can change groups and such.


I will and thank you so much for all your help. I've set up the web
page as per your previous post months ago and I think I can sleep at
night. :) What I've set up isn't perfect perhaps, but it's good and
anything else I've done wrong, I can learn slowly. There is only so
much that can be crammed into my brain at one time. :)

Thanks for your patient explanations. It was very much appreciated!

Ray

Oct 6 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.