Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to securely store database connection details

In an application that needs to open a database connection, the username/password details must be sent to the database. What is the most secure way of storing, and using, this data?

like image 363
Bobby Jack Avatar asked May 05 '09 09:05

Bobby Jack


People also ask

How do you store database connection strings securely?

The best way to secure the database connection string is to encrypt the value within the configuration file. The application would then load the encrypted value from the config file, decrypt the value, and then use the decrypted value as the connection string to connect to the database.

Where should you store the connection string information?

Connection strings in configuration files are typically stored inside the <connectionStrings> element in the app. config for a Windows application, or the web. config file for an ASP.NET application.


3 Answers

The exact method depends on the environment, but in general, you store the credentials in a location which is only readable by the user that your application is running as. For example on Windows you would store the credentials in the registry in a location protected by an ACL so that only that user could read it. Optionally, you could use the DPAPI to encrypt the data so it was further protected. In Unix, you would store it in a file that was protected with chmod (and optionally encrypted) so that only the app could read it.

like image 93
1800 INFORMATION Avatar answered Oct 29 '22 10:10

1800 INFORMATION


That depends on the database you're using. For Microsoft SQL Server you either encrypt the database connection string in the configuration or you use integrated security, where you connect to the database using the identity of the application you're connecting from.

like image 24
Ronald Wildenberg Avatar answered Oct 29 '22 09:10

Ronald Wildenberg


Excellent question.
It's an issue with which we've grappled - and come up with a variety of approaches.

The first answer is to go with 1800 INFORMATION's suggestion:

put it in an area only readable by the userid running your application.

I don't think you'll get a better all-round solution than this.

Other methods we've toyed with (and rejected):

  • Save it in an encrypted file
    • this only works if the attacker can't get to your code to see how the encryption works, so not so good most of the time.
  • Save it in the database and require a human to log on to start the application
    • this works, as long as you are in a position to have a real person start up the application all the time
  • Rely on built-in security devices, such as those in .NET (see rwwilden's answer).
    • this is a good solution if you are, e.g. a Microsoft shop.
like image 33
AJ. Avatar answered Oct 29 '22 09:10

AJ.