views:

104

answers:

8

Hi guys, I am inserting data into a mySQL database, but I am inserting banking details so it is very sensitive, how can I secure it and protect against it getting into the wrong hands?

At the moment the code is still very basic, without any preventative measures in place,

mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$result = mysql_query("INSERT INTO table (id, name, surname) 
VALUES (NULL, '".$_SESSION['name']."', '".$_SESSION['surname']."' )")
or die(mysql_error());

Thanx in advance!

A: 

You need to prevent SQL injections. Use mysql_real_escape_string() around all your variables you're inserting. Better yet, use PDO and prepared statements. Even better, use PDO and stored procedures.

robdog
not "all your variables" but "all variables enclosed in quotes"
Col. Shrapnel
+3  A: 

You should really use Prepared Statements to secure yourself.

Your sample code is way too risky !

Sarfraz
Thanx, and yes I know it is very risky, which is why I am asking, as I said above, it is just to give you guys an idea of what I want to do.
+4  A: 

It appears that you are writing a PHP application (which I'll assume will be deployed to the web)

  • Use HTTPS for web communication
  • Do NOT use the default port for MySQL
  • Have a different user account (in MySQL) for each operation (each with different passwords) for example, you might have a mytable1_select account which can only perform selects on mytable1
  • Use random string generators for usernames and passwords. Although this will make the code harder to understand, this will make it harder for a malicous person to gain access because they will need to guess both the username and the password
  • Protect against SQL injection by escaping all user-inputted strings
Maz
+1  A: 

You should look for security in the following places:

  • Script on the website: No Inline SQL - use stored procedures or prepared statements
  • Data transfer from website to database: Use HTTPS
  • Data storage in the database: You can either encrypt some tables, or the entire database
  • Database and Key backups: If a backup falls into the wrong hands, it will be compromised. Backups should be kept secure.

Read this thread and it will teach you a lot about building websites

http://stackoverflow.com/questions/72394/what-should-a-developer-know-before-building-a-public-web-site

Raj More
+2  A: 

You'll need to encrypt the sensitive data before it goes into the database. Mcrypt is a common way of doing so within PHP.

Mcrypt manual on php.net

Ryan
A: 

These are just suggestions that I didn't see anybody else make.

  1. Since you're using $_SESSION, make sure that register_globals is off to avoid SQL injection.
  2. Secure your MySQL database. It shouldn't even be on the Internet. Only your web server should be able to access it.
Marcus Adams
+7  A: 

If you're seriously storing banking details please read as much as you possibly can in this area; consider hiring someone with a lot of expertise in the field.

Sensitive data's like landmines, you don't want to touch it unless you really know what you're doing.

Edit - I want to clarify I'm being deadly serious here, not flippant. If I were given a project like this I would raise a big "out of my depth" flag to protect myself and my company. IMO this is one of these projects where failure to provide adequate infrastructure and process could lead to a company-destroying compromise.

I could well be overreacting here, but I'm trying to offer genuine advice that I'd give to a friend or colleague who came to me with the same question.

Brian
Agreed, which is why I am asking about it, before I do anything.
Are you doing this for your job or your own project? If you are subject to banking regulations or credit card processing restrictions, you should check with your employers Governance, Risk, and Compliance group for secure coding guidelines.
Ben Walther
For my job, but before I even posted this on SO I put up a commotion about the banking details being so unsecure, so I've gotten them to reconsidder getting those details from the site as it is too big a risk! Thanx for all the advice guys!
A: 

whats the problem if you are connecting to localhost?

Android Noob