Steven Lu
Steven Lu

Reputation: 43547

Connecting to MySQL database from PHP

Everything I find on the net tells me to provide the database user and password to connect to the database.

Being paranoid as usual, I don't like this because it means anybody that has the php source can now log into the database and screw with the data. Is this the only way to access the database?

context: http://www.cyberciti.biz/tips/facebook-source-code.html

Upvotes: 0

Views: 149

Answers (5)

Ken Downs
Ken Downs

Reputation: 4827

It's not the only way, but its the way most of the world (like 99.999%) does it, and as far as I can tell, 100% of web programmers.

Consider this: if the password is hardcoded into the source somewhere, why bother? Why not make the account password-less and just limit its access to the web server? Some might say, "oh noooooes!! Don't do that!" But again I ask, what's the difference? What security hole is opened that is not there anyway?

The real security issue is actually protecting yourself from SQL injection. That wide-open account makes you vulnerable if:

1) You have a glitch in code that can be exploited to do something a user is not supposed to do, or

2) They can trick your db server into executing code via SQL injection.

So SQL injection is your big bugaboo to protect against.

There are secondary protections as well. For instance, most people are paranoid about their "users" table that contains users and (unfortunately most of the time) plain text passwords, which are required if you are going to email a user their password.

You can put in a second level of protection on this table (in case they get past your SQL injection protection or find an exploit to make your program do something you thought it would not do) by locking it down so users cannot see it at all or write to it. Then you write two stored procedures, "addUser(username,password)" and "checkPassword(username,password)". This is an example of "security in depth" where you have multiple levels of security around the more sensitive data.

Upvotes: 1

zerkms
zerkms

Reputation: 255155

Yes, it is the only way. Also, as a paranoid you should know that knowledge of login and password is not enough, but you have to login from the right host (which is permitted to log in with this credentials).

Also, as a paranoid, why are you afraid of stealing your sources and not afraid of having vulnerabilities in your code that will allow a hacker to steal data even without login and password? In this case it is better for paranoid to never program at all.

Upvotes: 3

Michael Low
Michael Low

Reputation: 24506

You need to provide the connection information somehow, but you can mitigate the risk by setting up your MySQL user to have as limited privileges as possible. So grant read/insert access only where needed, and you can set the user host to only allow connections from 127.0.0.1 too.

There are also commercial obfuscation products like http://www.zend.com/en/products/guard/, which might be of interest. I'm not sure if they include string encryption or not though.

Upvotes: 0

madmik3
madmik3

Reputation: 6983

not a lot better but you an specify it in the php.ini http://www.php.net/manual/en/mysql.configuration.php#ini.mysql.default-password

Upvotes: 0

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74292

Since PHP will be parsed by the interpreter and only HTML will be output, you need not fear as there will be no way to get hold of the PHP source (guessing that you have set appropriate measures so that the PHP source cannot be downloaded).

Block all traffic to the MySQL server from outside. Only allow localhost to use it.

Upvotes: 1

Related Questions