LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 04-08-2005, 02:10 AM   #1
TigerOC
Senior Member
 
Registered: Jan 2003
Location: Devon, UK
Distribution: Debian Etc/kernel 2.6.18-4K7
Posts: 2,380

Rep: Reputation: 49
Security risks of php based mysql queries


I am just getting started with dynamic website programming and was reading up methods of querying a mysql database. The php query routine requires the connection to be established with the user name and password. Since this php query is present in the internet folder and is world readable surely this is very high risk? Is there something I am missing or are there methods of linking the query from the public directory to a non public directory that will obviate this.

edit: I have just read this thread;

http://www.linuxquestions.org/questi...hreadid=310768

and this is exactly what I am talking about. Someone has obtained the mysql access details from the script and penetrated the database. What is to stop anyone using the same type of agent off the net penetrating the database in the same way.

Last edited by TigerOC; 04-08-2005 at 02:21 AM.
 
Old 04-08-2005, 03:42 AM   #2
TigerOC
Senior Member
 
Registered: Jan 2003
Location: Devon, UK
Distribution: Debian Etc/kernel 2.6.18-4K7
Posts: 2,380

Original Poster
Rep: Reputation: 49
I am going to answer my own question. If I am wrong could someone correct me.
The answer seems to lie in defining a user on the database and further defining the rights of the user on the database.
Reference article. .

Quoting from this article; the user can be defined as having read rights only and only to this database;

mysql> GRANT SELECT ON database_name.* TO utahep@localhost IDENTIFIED BY ‘utahep’;

This means that you created a user ‘utahep’ who can connect with a password ‘utahep’ from the localhost having only read privileges (means selective privileges) on the database which is ‘database_name’.
 
Old 04-08-2005, 03:27 PM   #3
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 47
See my reply to the thread in your first post ...

But yes, you should exercise due diligence when setting up your database users too.
 
Old 04-10-2005, 01:40 AM   #4
DaHammer
Member
 
Registered: Oct 2003
Location: Planet Earth
Distribution: Slackware, LFS
Posts: 561

Rep: Reputation: 30
Seems your other thread has been removed, not that I'm surprised due to the war going on there. Anyway, as suggested in that thread, you can use file permissions to control which local users can access the sensitive file also. If I understood you correctly. You stated that you had to allow the file to be world readable so that the Apache webserver could access the file, and as a consequence to doing that you are also allowing every local user (ie users whom have shell access to the server) to also access the file. Which is how your database was compromised. Correct? If so, then there is a way to prevent the users from accessing the file while still allowing the webserver to access it, using file permissions. To do so, you'll just need to know what group Apache runs as. To explain further, consider this file:
Code:
----------  1 bob users 5 Apr 10 01:11 test.txt
Here test.txt has 000 for it's permissions. Nobody, not even bob can read it. Of course since bob owns it, he can change the permissions, delete it, or whatever. But that's beside the point.

Code:
-rw-------  1 bob users 5 Apr 10 01:11 test.txt
Here test.txt has 600 for it's permissions. Now bob can read/write to it, but nobody else can.

Now, suppose Apache runs as group "nogroup" and I want to allow Apache to access it without allowing everyone else to. I just change the group to "nogroup".
Code:
-rw-r-----  1 bob nogroup 5 Apr 10 01:11 test.txt
Here test.txt has 640 permissions and belongs to the group "nogroup". Since Apache runs under that group, Apache can now access the file, as can bob. But no other local users can because they neither belong to the group "nogroup" nor are they bob. At least the other users "shouldn't" belong to that group. Now there is a catch. As a non-root user whom also doesn't belong to the group "nogroup" you can not change the file to that group. hehe... Kind of a catch 22. Anyway, you should be able to get whoever has root access on the server to change it for you, I'd think. Especially considering you've already been compromised.

Also don't take the above the wrong way. I merely set out to explain the process and not to question your skill level.

Something else you may want to look into, in addition to the read-only control user for the database, is http authentication. That is if you need your users to have a higher level of control over the database vs just read access. Personally, I've never implemented it from scratch, but I do know that it's possible. And example program that can be setup to use it is phpMyAdmin. It can also be setup to use cookies. With http authentication you'd be prompted for a username/password by Apache when you logged onto the website (or loaded the php page, whatever the case might be) and then that username and password is used to connect to the server instead of the one in your php file. The only downside is the extra hassle your users would have to go through to get logged in. And if you could put the website behind SSL, even better. Anyway, you could probably sift through phpMyAdmin's source and figure out how they do it and implement that on your server. Good luck.

Last edited by DaHammer; 04-10-2005 at 01:42 AM.
 
Old 04-10-2005, 02:30 AM   #5
TigerOC
Senior Member
 
Registered: Jan 2003
Location: Devon, UK
Distribution: Debian Etc/kernel 2.6.18-4K7
Posts: 2,380

Original Poster
Rep: Reputation: 49
DaHammer I have done quite a bit of reading of various basic php/mysql documents available on the net and the php part of any html page is not readable from outside. This was the part I could not get to grips with. The general recommendations in these documents also strongly advise setting up specific users for given databases and not to have root capability. Thanks for your thoughts and input.
 
Old 04-10-2005, 07:30 AM   #6
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 47
Don't forget:

Code:
chown -R <userid>:<apache's group> /path/to/your/DocumentRoot
find /path/to/your/DocumentRoot -type f | xargs chmod 640
find /path/to/your/DocumentRoot -type d | xargs chmod 750
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what are the security risks using 'passwd' in shell scripts? MisterESauce Linux - Security 5 04-10-2005 01:48 PM
MYSQL paranoia security prohibits PHP to use database ? Dark Carnival Debian 1 01-08-2005 10:33 AM
SQL queries per page (PHP, MySQL) Silent1 Programming 2 06-27-2004 11:15 PM
PHP > MySQL connection password security question Wibble Linux - Security 4 04-22-2004 03:19 PM
samba/apache machine security risks fatman Linux - Networking 1 01-16-2004 09:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 09:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration