Take the 2-minute tour ×
Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

I have a cronned perl script that connects to our database and does various kinds of lookups and integrity checks. The original script was written by someone long ago. My job is to make some changes to it. But I really don't like staring at the username="foo", password="bar" parameters hardcoded in there for accessing the database.

There's got to be a more secure way of doing this. All I could think of to do for now is to comment out the cron job, delete the line in the script that had the password, and start brainstorming about how to make this more secure. But meanwhile the things the script does have to be done by hand.

Any ideas?

PS: No, "foo" and "bar" are not our actual database credentials.

share|improve this question

5 Answers 5

up vote 10 down vote accepted

The standard way is to put the credentials into a config file, and attempt to protect the config file from being more readable than the perl file. This offers a moderate increase in security; for example, the code may be in source control and accessible to developers, the config file wouldn't be. The code needs to be in the web server's cgi root, and possibly downloadable under certain misconfigurations, and the config file needn't be.

The ambitious way is to reversibly encrypt the credentials and put them into a config file. Of course, anything reversibly encrypted can be decrypted. The BladeLogic application did this, and it was trivial (<1 day) for me to de-compile their Java enough to find out the function to decrypt credentials and use it to decrypt them to my satisfaction. Not a mark against them; that's just the name of the reversibly encrypted game.

Another option is to use OS-based authorization in concert with strictly limited database restrictions. For example, limit the database client user's access to a set of stored procedures to limit the potential for abuse, and allow that user to access the database without a password. This doesn't work if you're doing client-server over the network, which limits how often it's useful. Also, people tend to look more askance at "passwordless" OS-user access than they do at writing the password down willy-nilly. It is not completely logical, but there are standards that say all database users must have passwords, so that's that.

share|improve this answer
    
Great answer. Thank you. One bit of good news is that there is no webserver involved in any of this. I like the idea of a combination of your first two methods: config file + encrypting the password. They are more specific versions of some fuzzy ideas I was thinking about. –  Luke Sheppard Sep 20 '12 at 22:20
    
Given that we don't have very many database user accounts to manage, and given that my script only needs to run three different queries, I'm thinking that I'll create a database user that can only run three stored procedures, as you recommended. But I'll require that user to log in with a password. That, in concert with the config file and reversible encryption, will let me sleep at night. I much prefer layers of security, especially in systems that run automatically, so might get forgotten over time. Thank you again. –  Luke Sheppard Oct 21 '12 at 19:21

As others have already said, put the credentials in a separate file which the script will load. Risks of having the password in the script include its being exposed to shoulder surfing, its being committed in revision control systems or configuration management systems and distributed far beyond what it should be, accidentally copying to another location when reusing part of the code, etc.

The credentials file should have the minimum permissions necessary. It should possibly not be included or treated differently by configuration management systems.

If available, look into using an operating system feature that limits the access to the credential file further than permissions. For example, AppArmor or SELinux under Linux can restrict the access to the credential file to the one script that needs it. This only protects against an attacker who cannot take control of the legitimate script, so make sure the legitimate script is not owned by the user who runs it (this is a general recommendation: don't have executable programs owned by the user that runs them).

share|improve this answer
    
This sounds like a good idea: "this is a general recommendation: don't have executable programs owned by the user that runs them", but I'm not clear on why it is a good idea. What attacks does this prevent? –  Luke Sheppard Sep 21 '12 at 21:40
1  
@LukeSheppard This is especially a warning against setuid programs on unix: if you can compromise a setuid program then you can write to the program executable and turn it into a trojan to infect the users who run the program. Example: the Debian policy is to make games that write a system-wide high score file setgid, not setuid, so that at most a security hole allows the user to cheat. But more generally, it's a way to turn a vulnerability that allows writing to a file into one that allows arbitrary code execution. –  Gilles Sep 21 '12 at 21:42

I like putting configs in ~/config/appname and having a generic config for my local mysql (or any other) connection. If you don't want to give your home a X permission and run the app as a different user have a standard config folder someplace. I do this so i don't accidentally package or give any passwords or usernames to other programmers. I dont think its a big deal but my boss is a security freak and its a good habit especially when publishing anything to the web.

It'd be wise to write a standard oneliner or twoliner for your config files and copy/paste them into any new scripts you write

share|improve this answer

The consensus seems to be:

  1. The password should be in a separate file, not part of version control, and protected with permissions more restrictive than that of the script.
  2. Encrypting the stored password is a waste of time since the script needs to be able to decrypt the password anyway.

But I'm thinking of adding a third control on top of those: Time Based (temporal) Access Control might help out a bit here too. Limiting the amount of time that the cleartext password is available to lower privileged users would add another (albeit small) layer of access control between the password and any local attacker.

My idea is to have root own the password file, set to mode 0400. And then have a root cronjob that does a chmod 0404 on the password file just before the lower-privileged cronjob that runs the script. At a specified interval later (hopefully after the script is done running), a second root cronjob does a chmod 0400 on the password file. This all assumes that the script only needs the password for some period of time less than always. Your thoughts on this scheme?

share|improve this answer
    
By the way, I can't find a URL pointing to any general, authoritative reference on the web for Time Based Access Control. Cisco routers still support it, and it used to be used in large dial-in modem pools for corporate and government systems. But I think it is generally a phenomenon of 20th century mainframe security systems. I think that the concept back then was something like, "Legitimate users would never dial in to the mainframe outside of normal business hours". But I don't think I've ever seen it used for controlling intra-system file access. –  Luke Sheppard Sep 21 '12 at 1:42
1  
Time-based access control would only help in rather contrived circumstances (e.g. if the attacker can only read files and not execute arbitrary commands, and the vulnerability is against a scheduled task that runs at a time the credentials file is inaccessible, and the attacker cannot delay the task). Adding this complexity for no real gain in security would be counterproductive. –  Gilles Sep 21 '12 at 10:13
1  
It provides a limited amount of additional security in exchange for excessive complexity. I won't go so far as @Gilles and say it's counterproductive, but it is clearly a makeshift bandaid that will end up biting you on the day the script can't read the file (you need to run it by hand, for example). What you really want is some form of MAC; for example, a tool that only permits your script to read that file and not other programs. Cisco Security Agent used to offer this sort of functionality, but is now defunct. I would love to find a good product that picked up that mantle. –  gowenfawr Sep 21 '12 at 11:26
    
Thanks gowenfawr. Excellent point about the added complexity. I've always believed that security controls are less effective as system complexity increases. –  Luke Sheppard Sep 21 '12 at 21:43

I often pass the password to the script in an environment variable that way the script need not have access to the secure location that contains the password.

PASSWORD=`cat passwd_file`  perl_script.pl

then the script reads the password

my $password = $ENV{'PASSWORD'}

bonus points if the perl script drops privileges

share|improve this answer
    
As long as /proc/<pid>/environ (or equivalent) is not readable by anyone else (it usually isn't) –  symcbean Sep 20 '12 at 23:40
    
This sounds OK, but I don't see how it is any different than just storing the password in a text file separate from the main script, and having the script read the password that way. In your method, the owner of both scripts is the same or at least has the same level of permissions on the two files, right? –  Luke Sheppard Sep 21 '12 at 1:31
    
There's one nice difference here worth noting. This separates the user / group the script runs as from the user / group ownership of the password. For instance, root might be the only one who should have access to the password, but the script needs to run with the same permissions as the web app you're running on the machine. –  Stephen Touset Sep 21 '12 at 1:53
    
OK. Then if root is the only user that can access the password file, then this line (that you typed) must be run by root, right? <code>PASSWORD=cat passwd_file perl_script.pl</code> But running the script as root is a bad idea. So I'm missing something here. –  Luke Sheppard Sep 21 '12 at 21:59
    
It's better to put it in a user environment variable over a file as someone will invariably check that file into source control. –  Neil McGuigan Oct 26 '13 at 8:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.