views:

228

answers:

4

I'm managing the MySQL database from PHP scripts. the communication between server and client is secured via SSL. I store user account data which is sensitive.

Is there a way to encrypt this data when entered into the DB? What is the best way to protect this sensitive data?

EDIT: I’m using a CRON job for updating data which relies on this password to login the user account. So I need a way to hash this password and to be able to get the original password for my CRON job tasks.

What’s the best way to achieve it?

Thanks

+1  A: 

There are encryption functions in MySQL,

http://dev.mysql.com/doc/refman/5.1/en/encryption-functions.html

For example, you can use aes_encrypt() to store data in encrypted form.

However, there are some weakness in these functions,

  1. It doesn't support IV or block-chaining. The same text always encrypts into the same ciphertext so it's not suitable for sensitive data like passwords.

  2. There is no key information so it's very hard to rotate keys.

ZZ Coder
Don't use aes_encrypt()! You should read my post.
Rook
+1  A: 

There is too litte info for a really useful answer but here are the common cases:

If you store passwords where you should check if the user input is correct, but you dont need the original data itself, you can store a hash (SHA1/SHA2). Lookup all practices for hashing to do this right.

If you need to read back the original data, you can use encryption functions. A good choice for a symmetric encryption is AES. However, the problem here becomes key management. Where do you store your key that is used for encryption/decryption? This is a very common problem and depending on the context tehre are different solutions.

Henri
Whats the best option to store the keys?On a different server?
embedded
On a TPC - Trusted Platform Module. THis is a hardware chip that can not be read out.
TomTom
I think TPC is a broader "device" as what is means here. I think you should google for a cryptographic hardware module. This does all crypto on the machine inside the module without ever exposing the key. Of course, you should make sure that accessing this think is hard. In keymangement it is always moving the problem, not really solving it, unfortunately.
Henri
A: 

There are some basic ways to do it (unless you provide some more thorough information on exactly what you want to accomplish).

  • Disable remote access.
  • Monitor the MySQL access log.
  • Require the use of strong and secure passwords from client end.
  • Sanitize ALL your user input, before storing into database.
  • Configure your php for security. (register_globals, safe_mode etc etc)
  • Use mysql inbuilt encryption functions, as pointed out. http://dev.mysql.com/doc/refman/5.1/en/encryption-functions.html

If you are protecting against wanna-be hackers this should be OK. Again I need you to specify in a bit more detail.

You also have to consider the level of security you need against the speed of the algorithm.

Russell Dias
The DB stores usernames and passwords which I don't want to store as plain text.so if someone manages to access my DB he could not take advantage of the info.what is the best option to achieve it?
embedded
Do not store the passwords. THat simple. Storing the passwords is gross neglect - if you end up in court, I just proove you stored the passwords and you lost. Accepted professional practice is never to store the passwords, either clear text, or encoded. Use hashes of the passwords and use a hash comparison.
TomTom
what do you mean by hash comparison?I need to use this passwords to fetch data from the accounts.
embedded
Are you saying they're passwords for **other** systems - i.e. you're storing someone's Twitter password or something?
ceejayoz
Yes.For monitoring users accounts.
embedded
Develop a basic API.
Russell Dias
What do you mean by basic API?what should it do?
embedded
+4  A: 

Seriously, DON'T USE MySQL's aes_encrypt() It is the the most insecure method of using a block cipher. It is using ECB mode, and I can give a simple example demonstration why this is a serious mistake.

Plain text message:

alt text

The same message encrypted with ECB mode (doesn't matter what cipher you use): alt text

The EXACT same message using CBC mode (again, it doesn't matter what cipher you use): alt text

There are even more reasons not to use mysql's aes_encrypt, most notably every single query you send will also have the aes key that you use. If the database is compromised the attacker will enable logging and just get your aes key and decrypt the entire database.

So what should you use? I like this class for the time being. Its using CBC mode with a String2Key function and an IV. You can use the primary key as your IV, each message must have a unique IV. Its okay if the attacker knows the IV, and if they are sequential, so long as the block cipher implementation is secure. Reuse of an IV made WEP much less secure.

Rook
+1 for the pictures, even though I have no idea what is going on.
Lotus Notes