Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store 'sensitive' data in MySQL DB

How should 'sensitive' data be stored in MySQL Database?

1) Should I focus more on the security of the MySQL database and store the data as plain text?

  • I found a step by step tutorial on how to make a MySQL database more secure:
  • http://www.symantec.com/connect/articles/securing-mysql-step-step

2) Should I encrypt the data?

  • If yes, then how should the encryption be done?
    1. Using MySQL aes_encrypt/aes_decrypt?
    2. Using PHP AES functions/algorithm for encrypting/decrypting data?
  • How should the data be stored in MySQL?
    1. BLOB
    2. BINARY
    3. VARBINARY

In my case the 'sensitive' data are payments done by individuals.

Thanks

like image 893
Catalin MUNTEANU Avatar asked May 23 '12 10:05

Catalin MUNTEANU


2 Answers

It's a mixture of both. Two existing answers (at the time I wrote this https://stackoverflow.com/a/10718397/1015483 and https://stackoverflow.com/a/10718459/1015483) are valid - you need to look at about 5 methods of possible attack that I can think of

  • They get access to your DB server; so yes, secure that baby as much as is reasonable (Matt's answer)
  • Stand alone data hijacking (someone gets to your database data somehow else, could be a backup, could be they guess a password, could be MITM if you transfer data from one place to another). For this, you do encypt your data. You also may do a CSV dump for some reason and e-mail to someone. Whoops. But it happens. So encrypt (vlzvt's answer)

But three elements not mentioned:

  • They could gain access to your web server (if different from your DB server). If they have access to the webserver, all bets are off as they have your password, encyption keys the lot. So you need to make that even more secure than the DB server. (Matt might have meant that above - but just make it clear)
  • Similar to above, but not to be forgotten, is if someone gets access to phpMyAdmin or your management consule. Don't use plain text auth or config stored passwords for access.
  • Finally there's your application itself (and the hardest to lock down). You need to prevent against SQL injections that may reveal data. Encrypting the data would stop minimise problems if someone did gain access through an untrapped query - so for this, encryption is the solution.

For part 2 of your question:

Using MySQL encrypt/decrypt functions will stop someone who has access to the raw data, but not MITM or SQL injection or even CSV dumps taken for transport.

So, IMO (and it's only my opinion and the way I've done it) is to encrypt with PHP and sned the encrypted data over the wire, as that stops all methods of trapping the data, and a CSV dump will be "scrambled".

If you do that, you may as well use the varbinary / blob types as it stops you accidentally trying to read/edit in phpMyAdmin. Plus potentially saves a few bytes nominally (although this depends on indexes and other stuff - so that alone is not a winning argument).


And now the down side: searching and sorting. Anything you index or search on, if encrypted, will only match the entire, exact, case sensitive string padded to the correct length (normally a search will be case insensitive, and you can do part searches with LIKE). And if you want to ORDER BY then you need the original strings. So bear than in mind when designing the structure.

Hope that helps.

like image 73
Robbie Avatar answered Oct 15 '22 23:10

Robbie


What's the worst possible scenario if an attacker gets access to the plain text data? Given that you have to decrypt data in order to make it useful and you therefore need the encryption key to be somewhere accessible too, any attacker who can get to the DB will likely be able to get to the key as well, unless this is for archiving rather than e.g. a live website. I'd focus on the DB server security, unless you're carting HDDs around full of data which might get lost, but it really depends on why you need to encrypt it.

like image 3
Matt Gibson Avatar answered Oct 15 '22 22:10

Matt Gibson