Question

MySQL - Best strategy to keep generic password secret

Asked by: elsamman

Is there a commonly accepted way for a web based program to manage a password that it needs to access a mySQL database on behalf of multiple users?
One could always keep it in a file but in a suexec world the program that uses the password will always run under the user's identity and so keeping the password in a file that is readable by everyone is not really a good idea.  I could keep it in the source of the program but that is not very good either.  Yes I could encrypt the password but then I have get into a whole area that has to be done very carefully if it is to be secure.  Besides if anyone really wanted it bad enough they could hack the program and extract the keys.  OK call me paranoid.

....Sam

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-03-08 at 10:28:23ID20911267
Tags

hack

,

keys

,

mysql

Topic

Linux Network Security

Participating Experts
4
Points
500
Comments
22

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. SUEXEC
    hi, how to setup SUEXEC in apache/linux??? please give simple steps. thanks
  2. suexec
    i try to configure apache with suexec, i use apache_1.3.14: [root@ns1 apache_1.3.14]# ./configure \ > --enable-suexec \ > --enable-shared=max \ > --enable-module=most \ > --with-port=10001 \ > --suexec-caller=www \ > --suexec-docroot=/home/sites \ > --su...
  3. Need guidance/clarification regarding Apache restart error…
    I am running Redhat Enterprise 3 and using the latest iteration of Webmin. I just added the first virtual domain to Apache (something I have done many times with Redhat 7.3) using Webmin's Apache module. When I apply the changes by restarting Apache the website becomes live...
  4. SuEXEC and CGI-files everywhere
    Hello, experts! I have some troubles with SuEXEC. I want to make possible execution of CGI-files not only in /cgi-bin/ folder. If I called in browser http://domain.com.ua/cgi-bin/script.cgi - all works file, but when I calling http://domain.com.ua/other_script.cgi result is P...
  5. MySQL
    What is the easy way to learn MySQL and is there a book that one can recommend? Can it run on a windows XP machine? I will like to learn everything about MySQL but not sure where to start?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: RedimidoPosted on 2004-03-08 at 15:16:40ID: 10545411

the file where you should store the password must be only readable by root, with 600 permissions. your suid program will run as root and then  that program will access the file to read the password.

this way final user is not able to read the file, while the program once running can.

 

by: elsammanPosted on 2004-03-08 at 16:06:35ID: 10545725

suexec will not allow a program to run as root.  There is a way to setup an alias ID to have suexec run as root (thanks to ahoffman for that) but I really don't want to have anything to do with root.

I know that a lot of programs have the same requirement as I and I know that they do not run as root.  Everything from eCommerce apps to discussion group software access a mySQL database and supply and ID and password.

 

by: jleviePosted on 2004-03-08 at 19:52:57ID: 10546953

The usual method is to create one or more MySQL users that have the most limited access to the database possible consistant with the needs of the web application. Those user names/passwords will have to be in the Perl, Php, etc., code that comprises the application, so that code needs to be prected to the maximum extent possible.

In the ideal case only SysAdmins actually have login rights on the web server and protection of the web source files is correspondingly easy. If users must be able to manage web sites, meaning uploading page code to the server those users can be limited to FTP where the FTP server doesn't use Linux account information for access and chroot's the FTP user to their site directory. The page code associated with other sites are in site directorys and thus inaccessible.

 

by: ahoffmannPosted on 2004-03-09 at 12:51:53ID: 10554523

as said, there is (currently) no programatik way to hide the password needed by a program.
This problem has been discussed several times, all with no (useful) avail.
Protect your file containing the password, that's it.
Best is 400 for root.

If you access MySQL via a webserver, then it's most likely possible to use the password while the webserver starts (and makes a permanent connection), at least apache with mod_perl, fastcgi, php, tt2, etc. can do it. This way you even hide the password from application programmers.

 

by: ahoffmannPosted on 2004-03-09 at 13:00:58ID: 10554601

well, another way I sometimes use, is as follows:
  1. setup MySQL to listen on localhost:port only
  2. in mysql.db use a very restrictive entry with IP and username and db, but empty password
  3. setup ~user/.ssh/authorized keys with a valid key
  4. connect to the host (with mysql) with ssh and local port forwarding
      ssh user@remote -i /path/ro/secret.key -L4711:localhost:4242 sleep 1000
  5. connect to mysql without password:
     mysql -P 4242 -h localhost -u user <your-query.sql

This is more of security-by-obscurity 'cause you still need a passwordless ssh-key, to circumvent this you
may use ssh-agent (once at startup)
At least you database is as secure as the system which it runs on *and* the ssh-key (if passwordless).
Even if the passwordless ssh-key gets stolen, someone needs to find out where it is used.

 

by: elsammanPosted on 2004-03-09 at 13:18:49ID: 10554773

I should have been more clear in that the target environment is a bog standard web host.

For applications that run as root, protecting the password from all but root is pretty easy as ahoffmann points out.  Changing the mysql server listen on the localhost port only is not always an option.  Hiding the ID and password in your code is what I suspect most people do.  The problem there is that once someone finds out the password your application is compromised.   You really need to create a random password and put this in a file.  You can then protect the file from being read by other users on the system.  To me this is not great but it sounds like this question has come up before with no standard answer.  I did search but could not find any threads on this.

I will give this one a few more days.

 

by: ahoffmannPosted on 2004-03-09 at 13:30:50ID: 10554895

> ..  listen on the localhost port only is not always an option
no warries.
Let it listen on more than one IP, but protect the localhost  as I said.
If the webserver and database server share the same system, it's even worse.
In such cases I use the vserver patch for the kernel, and run vservers on the same hardware: one vserver for one application (web, database). Some people may arg that this is what M$ does (to make money:), but it's much better, each such vserver only runs init + sshd + application, nothing more. A dozent vservers on a standard hardware are no problem ;-)
This also protects you that one root-compromised system compromises another one.

Using random passwords programatically makes things complicated, and if something fails you also may be lost in nowhere. Just keep in mind ...

 

by: alain_tesioPosted on 2004-03-10 at 06:18:42ID: 10560797

I don't see why people suggested so complex things. The server based program is something like a php script or a cgi ?

The usual method is just to put the password in a configuration file readable only by the process running it like the apache user, outside the web server document root, and make sure you can't do more than needed if you have this password.. This is what almost every script does, like phpmyadmin.

Don't run programs as root, don't mess with roost suid programs for that, if the user can execute a suid program to get the password  there is no difference than reading the password file directly, except you may have a big securiity hole if you don't know what you're doing with the suid stuff.

Use suexec to run a program as root just to protect your mysql password is really stupid. At least, if you do this, read the password at startup and give up root privileges as soon as possible. But no serious program do this, some programs like apache and bind start as root to bind to ports<1024 because it's an OS limitation.

If you have a program running on client machines which connect directly to the database, either each user is a  mysql user and he enters his password when connecting or it's stored somewhere locally.
But I think you have a database design problem in this case.

If there is a single mysql public user for all, there is no ultimate way to hide/encrypt it in the program anyway. You cannot trust machines you don't control. If someone has the program and wants to find the password, assume he'll  find it.

 

by: ahoffmannPosted on 2004-03-10 at 06:49:25ID: 10561054

> The usual method is just to put the password in a configuration file  ..
> .. make sure you can't do more than needed if you have this password..
hmm, remember this is a Security TA, and the question was "how to do it better"
We all agree, somehow, that this is undoable.
I also prefer KISS - keep it stupid simple - but passwords used programaticaly seem to brake this paradigm ..

 

by: alain_tesioPosted on 2004-03-10 at 07:07:15ID: 10561243

What does it break ?

Some examples for common softs as packaged in debian :

For PHPMyAdmin, I have the password in /etc/phpmyadmin
For roundup, it's in $TRACKER_HOME/config.py
For cacti, in /etc/cacti
For mnogosearch , in etc/indexer.conf

It's probably the same for any webmail with a sql backend, or j2ee programs, this is just the usual way to do it.

You have to think at how the password could be compromised: an intruder needs the apache (or anything else running the process) privileges, and at this point you have bigger problems anyway. And whatever the method to get the password is, since your process needs to get it, the intruder can get it too once he has apache privileges.

 

by: elsammanPosted on 2004-03-10 at 07:57:22ID: 10561764

Alain,

What I don't see is how this could work with SUEXEC with virtual domains since the application that needs to get at those password files would be running as a real user rather than the user specified for Apache.

 

by: alain_tesioPosted on 2004-03-10 at 08:15:09ID: 10561939

I thought that suexec was just a suggestion from you, if your process (which language ?) is running with each user identity, you can just put these users in a group who's allowed to read the password file.

This doesn't change the problem, if the program can read a file or whatever to get the password the user can do it too.

If you don't want users to have a direct connection to the database, you can use an intermediate layer. It's more or less easy depending of your language.
For example in python you have pyro which is an object broker really simple to use, you request a distant connection object (initialized by another single multithreaded background process) and use it as if it was created locally.

It looks like this, similar to rmi in java :

import Pyro.core
Pyro.core.initClient()
connection=Pyro.core.getProxyForURI("localhost:7777/connection")
connection.execute("select something from some where")

 

by: ahoffmannPosted on 2004-03-10 at 09:20:18ID: 10562683

> .. this is just the usual way to do it.
that's the defect.
If the password can only be read by the server (nobody, wwwrun orwhatever running httpd), a broken application does not compromise the databse security.
Just think of XSS, path enumeration, etc. which might be possible in the application (and PHP applications are far, far, very far away from being secure, unfortunately).

We may discuss if the application or the OS is more likely infected by whatever.
We don't need to talk about getting root-access somehow, then anything is lost. But if the application is seperated from the web-server (httpd), then it's one more level to break.

Yes, my suggestions are something like a proxy.
It's like you go to the bank getting a withdrawal: you cannot go to the vault yourself, even the building is save and protected.

 

by: elsammanPosted on 2004-03-10 at 10:03:44ID: 10563204

I love a good and lively discussion and there have been some interesting ideas that have come forth.  Let me refer back to my requirements and then clarify them a bit more.

1) mySQL
2) A single database for multiple users
3) SUEXEC

As I have mentioned before I target a web-hosting environment where I cannot make too many demands on the host.  Web hosts typically use SUEXEC to keep user's away from each other.  As long as you have one database per virtual domain / unix user, a password in a file for each user with 600 is grand.  I think this is the case with a lot of applications.  

In my case I want to have one single database for all domains / users.  This means SUEXEC will run the application under the user identity associated with each virtual domain.  Therfore the password file would have to be readable by everyone.  I need SUEXEC because I have to manipulate the user’s files.  Even if I did not, once SUEXEC is enabled it forces all CGI to run under a user’s identity rather than that of the web server.  I have no choice as to whether the web host runs SUEXEC.

I have solved this problem for the project at hand by piping my request to a daemon running as root.  The daemon can suid to the user to manipulate files.   I did this mainly for performance but it looks like I would have had to do it anyway for security reasons.  While all of this is fresh in my mind I am fishing for a simpler solution that I might use in other similar projects.

 

by: ahoffmannPosted on 2004-03-10 at 10:31:23ID: 10563462

who needs to care about the passwords used for the database: the human user infront of the browser, or the application accessing the database?

 

by: elsammanPosted on 2004-03-10 at 10:43:37ID: 10563587

The application accessing the database.

 

by: alain_tesioPosted on 2004-03-10 at 11:34:28ID: 10564117

>  The application accessing the database.

Good question because I didn't get it finally: what's the web server doing if the final user uses this application ? What language do you use for the application ?

 

by: elsammanPosted on 2004-03-10 at 11:53:37ID: 10564319

> what's the web server doing if the final user uses this application ?

Sorry Alain, I am not understanding your question.

The application is a CGI script written in Perl.  It authenticates the user (using FTP/POP),  fools around with the mySQL database and reads / writes files into the user's public_html directory.  It is a site builder tool.

 

by: ahoffmannPosted on 2004-03-10 at 14:31:20ID: 10565884

if the application is the master (knowing all passwords), why having a password for each user?
One password for the application seems to be enough.
Your cgi, language doesn't matter, then can access the database, and calls a suexec'd script/pogram to store the files in the user's public_html directory.

 

by: elsammanPosted on 2004-03-10 at 15:02:02ID: 10566116

The application does not know any user passwords.  Each user of the application is a valid unix user. The user is prompted for a unix password.  This ID/password collected from the user is authenticated via POP.  Having authenticated the user, the application feels confident to access the user's files.   After all it is running under the identity of the user.  With suexec you have no choice in the matter.  Your script will run under a real user's identity.

The problem is that there is no way to keep the database password secret since the script will be running a number of different identities.

 

by: ahoffmannPosted on 2004-03-10 at 15:57:12ID: 10566486

> .. user is prompted for a unix password.
Is this done via POP?

Anyway, how about following idea:
  1. cgi prompts for password ina HTML form (returned via SSL)
  2. cgi authentificates against OS (/etc/passwd or whatever)
  3. cgi SUEXECs with valid user
  4. cgi (now with effective uid of user) authentificates with same password against mysql

password is now known by the (human) user only, but used in clear text in the cgi, so you need to prtect the cgi (500 etc.)

'caue MySQL has it's own authentification schema, you can connect the databse in step 2., then use this connection in step 4. (see mysql's --socket option)

 

by: elsammanPosted on 2004-03-12 at 04:38:39ID: 10579937

>  2. cgi authentificates against OS (/etc/passwd or whatever)

Only root can authenticate against /etc/passwd since almost everyone runs with shadow passwords

>  3. cgi SUEXECs with valid user

You might want to take a look at the SUEXEC docs.  It is not an API call and therefore a step that a CGI takes.  When mod_suexec is running all CGIs run on behalf of a user

>  4. cgi (now with effective uid of user) authenticates with same password against mysql

As I said I need to have one database for multiple user's.  If I gave everyone an ID and password against the database then anyone would be able to get at the database.

I think it is time to wrap this up.  I am going to award the points to alain because he actually set things straight in the first post and pretty much said that there was no way to do what I wanted to do.  I think the rest of the dialog simply confirmed that.  Also the object broker technique while not as automated in Perl is essentially what I ended up doing (daemon)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...