Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a Bash script that automatically authenticates with a service using my username and password. The credentials are currently stored as plain text within the script.

What precautions should I take to most securely store these credentials while still allowing the script access?

Points of clarification:

  • I understand that, if available, other methods of authentication should be used instead. I still want to know what to do in the case that password authentication is the only option.
  • Not storing the password at all is not an acceptable answer here. I am asking about the case in which the script must have unattended access to the password.
share|improve this question
2  
not store the passcode part of the credentials, perhaps? –  Thomas W. Jul 7 '11 at 19:04
    
Wouldn't it work to access GNOME Keyring or something similar? –  Firefeather Jul 14 '11 at 21:12
    
Perhaps if you could store it as a cryptographic hash (such as SHA-1), that would be nice. –  Anonymous Oct 25 '11 at 17:03
    
ROT13 would obscure it, but would NOT secure it in any way. –  Anonymous Oct 25 '11 at 17:04
    
@Anonymous If the password is stored as a cryptographic hash, how do you recommend retrieving it? –  ændrük Oct 25 '11 at 17:07

2 Answers 2

up vote 7 down vote accepted

What kind of service? Certain services have other methods to authenticate, e.g. SSH keys for SSH in conjunction with SSH agent.

I'd store the password separate from the script, and make sure that all path components have the correct permissions set. E.g., make sure that in the path /path/to/file, /, /path and /path/to are owned by a user you trust (root) and that these are not writable by someone who is not allowed to see your files. Finally, the recommended permissions for file is 600 or 400.

That file could look like this:

PASSWORD='something that you cannot remember'

In your script, use the below code to import the variable:

. /path/to/file

As for your script, make sure that it does not contain holes which may allow attackers to execute code in the script context (e.g. uncontrolled environment which may have an arbitrary $PATH variable set or invalid use of other files (e.g. sourcing a world-writable file).

As for the actual protection of your password, you can't. It must be available somehow to the other service. As an alternative, you can encrypt the file/ script containing the password using openssl or gpg so you need to enter a password before the credentials are unlocked. This is especially useful if your service's password is hard to remember.

share|improve this answer

Instead of hardcoding the password in the file, store the password in a separate file and protect the file (chmod 700 or chmod 500) so that only the authorised users can access it.

Retrieve the password with cat /dir/to/file/with/.password instead of reading the file and storing the content of it into a variable.

share|improve this answer
    
Some tools even have an option to read the password from a file directly for precisely this reason, such as gpg's --passphrase-file option. –  OrbWeaver Jul 13 '11 at 6:36
2  
...and make it a .dotfile (/dir/to/file/with/.password) –  knb Oct 25 '11 at 17:16
    
chmod 400 for files (read-only). Of course, you have to secure the leading directories (700 or 500, or even 100 if you are paranoid). Note that 400 (or 500 for directories) is sufficient, other modes (100) can be considered as security through obscurity, just like hiding the file by putting a dot in front of the name. –  Lekensteyn Feb 13 '13 at 20:49

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.