views:

3208

answers:

11

I have got a python script which is creating an ODBC connection. The ODBC connection is generated with a connection string. In this connection string I have to include the username and password for this connection.

Is there an easy way to obscure this password in the file (just that nobody can read the password when I'm editing the file) ?

A: 

Try ROT13

Thorsten79
A: 

There are several ROT13 utilities written in Python on the 'Net -- just google for them. ROT13 encode the string offline, copy it into the source, decode at point of transmission.

But this is really weak protection...

Kevin Little
ROT13 utilities? Isn't 'your string'.encode('rot13') enough?
ΤΖΩΤΖΙΟΥ
+26  A: 

Base64 encoding is in the standard library and will do to stop shoulder surfers:

>>> import base64
>>> print base64.b64encode("password")
cGFzc3dvcmQ=
>>> print base64.b64decode("cGFzc3dvcmQ=")
password
Dave Webb
I agree. The base64 encoded password looks much more mysterious.
Ed Haber
But doesn't help the fact that the script must be readable by the user running it and the password must not.
Martin Beckett
You don't encode the entire script though. Just the password inside the script.
Mark Biek
worked very well - thank you.
bernhardrusch
+1  A: 

Your operating system probably provides facilities for encrypting data securely. For instance, on Windows there is DPAPI (data protection API). Why not ask the user for their credentials the first time you run then squirrel them away encrypted for subsequent runs?

Jamie Eisenhart
+1  A: 

How about importing the username and password from a file external to the script? That way even if someone got hold of the script, they wouldn't automatically get the password.

Douglas F Shearer
+11  A: 

Douglas F Shearer's is the generally approved solution in Unix when you need to specify a password for a remote login.
You add a --password-from-file option to specify the path and read plaintext from a file.
The file can then be in the user's own area protected by the operating system. It also allows different users to automatically pick up their own own file.

For passwords that the user of the script isn't allowed to know - you can run the script with elavated permission and have the password file owned by that root/admin user.

Martin Beckett
+6  A: 

The best solution, assuming the username and password can't be given at runtime by the user, is probably a separate source file containing only variable initialization for the username and password that is imported into your main code. This file would only need editing when the credentials change. Otherwise, if you're only worried about shoulder surfers with average memories, base 64 encoding is probably the easiest solution. ROT13 is just too easy to decode manually, isn't case sensitive and retains too much meaning in it's encrypted state. Encode your password and user id outside the python script. Have he script decode at runtime for use.

Giving scripts credentials for automated tasks is always a risky proposal. Your script should have its own credentials and the account it uses should have no access other than exactly what is necessary. At least the password should be long and rather random.

tduehr
Very nice answer - thank you. For the small scripts I'm writing (which are maintenance scripts anyway - the BASE64 encoding will suffice)
bernhardrusch
A: 

I made a web utility here to do the base64 encoding method. (For whatever that method is worth)

Greg
+3  A: 

This is a pretty common problem. Typically the best you can do is to either

A) create some kind of ceasar cipher function to encode/decode (just not rot13) or B) the preferred method is to use an encryption key, within reach of your program, encode/decode the password. In which you can use file protection to protect access the key. Along those lines if your app runs as a service/daemon (like a webserver) you can put your key into a password protected keystore with the password input as part of the service startup. It'll take an admin to restart your app, but you will have really good pretection for your configuration passwords.

+1  A: 

base64 is the way to go for your simple needs. There is no need to import anything:

>>> 'your string'.encode('base64')
'eW91ciBzdHJpbmc=\n'
>>> _.decode('base64')
'your string'
ΤΖΩΤΖΙΟΥ
No, Thats just silly.
FlySwat
What exactly is silly?! The whole reply, or the not-importing part?
ΤΖΩΤΖΙΟΥ
Base64 only adds the illusion of security.
FlySwat
Jonathan, it seems as if you didn't read the question. It's about _obscurity_ (and a very temporary one), not _security_, so I don't understand why you consider my answer not helpful.
ΤΖΩΤΖΙΟΥ
I didn't know you could do this instead of having to use the base64 module. And there are a lot of encodings too like zlib too... fun :)
Kiv
A: 

Place the configuration information in a encrypted config file. Query this info in your code using an key. Place this key in a separate file per environment, and don't store it with your code.

FlySwat