Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid having the database password stored in plaintext in sourcecode?

In the web-application I'm developing I currently use a naive solution when connecting to the database:

Connection c = DriverManager.getConnection("url", "username", "password"); 

This is pretty unsafe. If an attacker gains access to the sourcecode he also gains access to the database itself. How can my web-application connect to the database without storing the database-password in plaintext in the sourcecode?

like image 747
runaros Avatar asked Sep 03 '08 23:09

runaros


People also ask

What is one method that can be used to avoid putting plaintext database usernames and passwords into your code?

A good approach is to not even store the password at all. Instead one should use so-called salt and hashing and only store the hash in a database.

Why should we never store password as plain text?

Anyone can read it. If you store a password in clear, readable text, anyone who has (un)authorized access to your account or device can read it. And if that person is a hacker who has just broken into the database, your sensitive data now belongs to him.

What method should be used to pass credentials in to source code?

You should encrypt your credentials before saving the file, and additionally, you can apply a second encryption to the file itself (2-layer encryption to the credentials, and 1-layer to other file contents). Note that each of the two encryption processes mentioned above can be multiple-layered themselves.

Why passwords in database are stored in encrypted format?

Encrypted passwordsSince the attacker knows his password in plain text/encrypted form, he can guess the logic of the encryption and try to reverse it. If he succeeds, all passwords will be retrieved as quickly as they were in plain text, regardless of the algorithm's complexity.


1 Answers

You can store the connection string in Web.config or App.config file and encrypt the section that holds it. Here's a very good article I used in a previous project to encrypt the connection string:

http://www.ondotnet.com/pub/a/dotnet/2005/02/15/encryptingconnstring.html

like image 55
Julio César Avatar answered Sep 23 '22 00:09

Julio César