The Web Squeeze: Encrypting Mysql Database Password - The Web Squeeze

Jump to content

Forum

Digg Del.ico.us Slashdot Technorati furl Reddit Facebook Fark Google Magnolia Wink Yahoo Netscape
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Encrypting Mysql Database Password

#1 User is offline   Monie Icon

  • Professional Squeeze
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,519
  • Joined: 13-February 08
  • Gender:Male
  • Location:Borneo

Posted 22 June 2009 - 07:12 PM

My question is HOW?

How do I HIDE/ENCRYPT my MySQL Database Password so that I am the only one in this world knows it and be able to access the database and do stuff inside?

If I do the method like below, someone else can dig the database password and gain access into my database.
Any way of solving issue like this?

Thanks.

<%
	'//1.Define Constant Value
	Const DB_SERVER = "localhost"
	Const DB_NAME = "database_name"
	Const DB_USER = "root"
	Const DB_PASS = "mysql_password"

	'//2. Database Connection
	Dim Conn, ConnectionString
	ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER="& DB_SERVER &"; DATABASE="& DB_NAME &"; OPTION=4; UID="& DB_USER &"; PASSWORD="& DB_PASS &";"
	Set Conn = Server.CreateObject("ADODB.Connection")
	Conn.Open(ConnectionString)
%>

Join me via Facebook | Twitter
0

#2 User is offline   MikeHopley Icon

  • Professional Squeeze
  • Icon
  • Group: Mentor
  • Posts: 1,391
  • Joined: 15-February 08
  • Gender:Male
  • Location:UK

Posted 23 June 2009 - 04:17 AM

If possible, put the mySQL connection details in one file only, and place that file somewhere above the level of the web root. This means the file will be completely inaccessible to anyone browsing over the web. Of course, if they knew your FTP login details, they could still access it (and everything else).

It's a good practice to put all your back-end files above the web root (PHP scripts, etc.). Only front-end stuff (HTML, CSS, javascript, images...) belongs at (or below) the level of the web root. Of course, your HTML documents may contain small amounts of PHP, and that's fine.

You then use includes to...er, include this file in your pages. To make this work, you need to know the full directory path on the server. For example, it might be something like: /home/yoursite/bin/mySQLconnect.php (if you placed the connection file in the bin directory).

Better yet, make a database object. You can then keep all the code for connecting to the database encapsulated in (and private to) this object, so that when you want to connect to the database, you just write something like this:

$db->connect();


This is the approach used by PHP frameworks (of course, the database object lives in a file above the web root).

You can also encrypt the database password. I'm a bit shaky on the theory here, but a simple method is to generate an md5 hash of your password. Instead of storing the raw password in your file, you store the hash. In your database connection function/method, you decode the hash.

Let's say your password was "my-weak-password". In PHP, for example, you would generate a hash by running md5("my-weak-password"). You then take the output (say, "f13jhvg0be8r32lfdgu9") and store it as your password ($db-password). In your connection function/method, instead of supplying the argument "$db-password", you supply the argument "base64_decode($db-password)".

You can make this encryption much more secure by adding a salt.
0

#3 User is offline   Monie Icon

  • Professional Squeeze
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,519
  • Joined: 13-February 08
  • Gender:Male
  • Location:Borneo

Posted 23 June 2009 - 04:41 AM

Hai Mike!

Thanks for the long info ^_^
Well, actually I am talking this locally. I mean, I have a localhost web based system in the company I am working.
All the IT Staff have access into every php page in the www root.

So, somewhere in that file is the page that store my MySQL database Password where I have defined the php variable.
What I want to do is to encrypt that password so that others don't have a clue what password to access into the MySQL database.

Is it possible?
Join me via Facebook | Twitter
0

#4 User is offline   MikeHopley Icon

  • Professional Squeeze
  • Icon
  • Group: Mentor
  • Posts: 1,391
  • Joined: 15-February 08
  • Gender:Male
  • Location:UK

Posted 23 June 2009 - 06:11 AM

View PostMonie, on Jun 23 2009, 10:41 AM, said:

Well, actually I am talking this locally. I mean, I have a localhost web based system in the company I am working.
All the IT Staff have access into every php page in the www root.


Do they have access to directories *above* the www root? If they don't have access, you can just put the file higher up.

Quote

So, somewhere in that file is the page that store my MySQL database Password where I have defined the php variable.
What I want to do is to encrypt that password so that others don't have a clue what password to access into the MySQL database.


Well, you can always encrypt the password, as I mentioned. However, if they have access to the entire source code, then I think they could just run the base64_decode function themselves to reverse the hash.

In principle, I think, you cannot properly encrypt something if they have access to the entire source code (including the values of all stored passwords, salts, and so on). Your only method of security is then "security through obscurity" -- making the source code so difficult to fathom that they fail to reverse-engineer it. ;)
0

#5 User is offline   Monie Icon

  • Professional Squeeze
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,519
  • Joined: 13-February 08
  • Gender:Male
  • Location:Borneo

Posted 23 June 2009 - 07:55 PM

Yes they do have full access to the entire source code.

LOL, I just thought that there is a direct tips for this issue :D
Anyhow... I'll see what I can do with your "security through obscurity" tips!
Thanks anyway....
Join me via Facebook | Twitter
0

#6 User is offline   christopher Icon

  • Rapid Squeezer
  • PipPipPipPip
  • Group: Members
  • Posts: 114
  • Joined: 15-February 08
  • Gender:Male
  • Location:Ottawa, Canada

Posted 10 July 2009 - 03:19 PM

Sorry, some corrections needed here:

View PostMikeHopley, on Jun 23 2009, 04:17 AM, said:

You can also encrypt the database password. I'm a bit shaky on the theory here, but a simple method is to generate an md5 hash of your password. Instead of storing the raw password in your file, you store the hash. In your database connection function/method, you decode the hash.

Let's say your password was "my-weak-password". In PHP, for example, you would generate a hash by running md5("my-weak-password"). You then take the output (say, "f13jhvg0be8r32lfdgu9") and store it as your password ($db-password). In your connection function/method, instead of supplying the argument "$db-password", you supply the argument "base64_decode($db-password)".

You can make this encryption much more secure by adding a salt.

A hash (such as MD5) is a one-way operation. It is impossible to undo a hash (I won't say decrypt, because hashing is NOT encrypting). Passwords are often stored hashed, but rather than undo-ing the hashing to check the password, the user supplied password is hashed using the same algorithm (e.g. MD5) and the hashed values are compared. Using a salt does make a hash more secure because it helps protect against bulk hash dictionary attacks.

Next, Base64 is neither hashing, nor encrypting. It's encoding. All the bits are lined up, and then taken 6 at a time. 6 bits mean 64 possibilities. Those 64 possibilities are each represented by 1 "safe"/printable character. Base64 is used to turn some thing binary that normally can't be safely represented in a string format (such as an image file) into a series of characters that can be safely represented in a string format. That's Base64 encoding. Base64 decoding does the exact reverse (i.e. from 6 bit possibilities, back to the 8 bit bytes).


Properly encrypting something requires a key. Some encryption methods use the same key to encrypt and decrypt, others use key pairs (one key encrypts, only the other can decrypt). But in either case, if you encrypt your password, you then have to worry about how to protect the key that decrypts the password. It becomes a never ending cycle.


As Mike says, your best bet is keeping the password in one location, above the web root and obscure it if you like.

This post has been edited by christopher: 10 July 2009 - 03:22 PM

Blog: annoyed.ca | Web Site Hosting: www.bluephyre.com
0

#7 User is offline   c010depunkk Icon

  • Squeeze Machine
  • Icon
  • Group: Advisors
  • Posts: 508
  • Joined: 14-February 08
  • Gender:Male
  • Location:Düsseldorf, Germany

Posted 13 July 2009 - 02:12 AM

if you're trying to obscure the password in your source code, then base64decode/encode would be one solution. just remember that because it's a decode/encode people can still get at the password if they are determined enough, but at least the password isn't stored as clear-text. This is more a method of "keeping honest people honest" ;)
an umcomfortably attractive blend of sort of perverted but suprisingly sweet.
i.met.janvt.com
0

#8 User is offline   Monie Icon

  • Professional Squeeze
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,519
  • Joined: 13-February 08
  • Gender:Male
  • Location:Borneo

Posted 13 July 2009 - 02:42 AM

Can you show me some example mate ^_^
Appreciate it, Thanks.
Join me via Facebook | Twitter
0

#9 User is offline   c010depunkk Icon

  • Squeeze Machine
  • Icon
  • Group: Advisors
  • Posts: 508
  • Joined: 14-February 08
  • Gender:Male
  • Location:Düsseldorf, Germany

Posted 14 July 2009 - 05:30 AM

make youself a simple php script:
<?php
echo(base64encode('your_password'));
?>

the output will be some random string like "ADssf(7234"
then instead of a clear-text password in your database connection you can write:
$password=base64decode('ADssf(7234');

an umcomfortably attractive blend of sort of perverted but suprisingly sweet.
i.met.janvt.com
0

#10 User is offline   Monie Icon

  • Professional Squeeze
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,519
  • Joined: 13-February 08
  • Gender:Male
  • Location:Borneo

Posted 14 July 2009 - 07:02 PM

Ahhh, great!
I might use that in my page ^_^
Thanks mate!
Join me via Facebook | Twitter
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic


Page 1 of 1
Trackbacks
Trackback URL Trackback Date Total Hits
No trackbacks were found
Page 1 of 1

Similar Topics
  Topic Started By Stats Last Post Info
New Replies Icon Photography Website - Client Password Access? 3twentysix Icon
  • 5 Replies
  • 2,135 Views
Locked Topic Icon Change Mysql Section Jason Icon
  • 4 Replies
  • 1,298 Views
New Replies Icon Learning Php/mysql thewal Icon
  • 3 Replies
  • 1,393 Views
Hot Topic (New) Icon Wierd Php / Mysql Error... JustinStudios Icon
  • 14 Replies
  • 2,203 Views
New Replies Icon Testing Database Skills Vanessa Icon
  • 6 Replies
  • 1,147 Views

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users