Secure credential storage in python Secure credential storage in python python python

Secure credential storage in python


There are two very different reasons why you might store authentication credentials:

  1. To authenticate your user: For example, you only allow the user access to the services after the user authenticates to your program
  2. To authenticate the program with another program or service: For example, the user starts your program which then accesses the user's email over the Internet using IMAP.

In the first case, you should never store the password (or an encrypted version of the password). Instead, you should hash the password with a high-quality salt and ensure that the hashing algorithm you use is computationally expensive (to prevent dictionary attacks) such as PBKDF2 or bcrypt. See Salted Password Hashing - Doing it Right for many more details. If you follow this approach, even if the hacker retrieves the salted, slow-hashed token, they can't do very much with it.

In the second case, there are a number of things done to make secret discovery harder (as you outline in your question), such as:

  • Keeping secrets encrypted until needed, decrypting on demand, then re-encrypting immediately after
  • Using address space randomization so each time the application runs, the keys are stored at a different address
  • Using the OS keystores
  • Using a "hard" language such as C/C++ rather than a VM-based, introspective language such as Java or Python

Such approaches are certainly better than nothing, but a skilled hacker will break it sooner or later.

Tokens

From a theoretical perspective, authentication is the act of proving that the person challenged is who they say they are. Traditionally, this is achieved with a shared secret (the password), but there are other ways to prove yourself, including:

  • Out-of-band authentication. For example, where I live, when I try to log into my internet bank, I receive a one-time password (OTP) as a SMS on my phone. In this method, I prove I am by virtue of owning a specific telephone number
  • Security token: To log in to a service, I have to press a button on my token to get a OTP which I then use as my password.
  • Other devices:

    • SmartCard, in particular as used by the US DoD where it is called the CAC. Python has a module called pyscard to interface to this
    • NFC device

And a more complete list here

The commonality between all these approaches is that the end-user controls these devices and the secrets never actually leave the token/card/phone, and certainly are never stored in your program. This makes them much more secure.

Session stealing

However (there is always a however):

Let us suppose you manage to secure the login so the hacker cannot access the security tokens. Now your application is happily interacting with the secured service. Unfortunately, if the hacker can run arbitrary executables on your computer, the hacker can hijack your session for example by injecting additional commands into your valid use of the service. In other words, while you have protected the password, it's entirely irrelevant because the hacker still gains access to the 'secured' resource.

This is a very real threat, as the multiple cross-site scripting attacks have shows (one example is U.S. Bank and Bank of America Websites Vulnerable, but there are countless more).

Secure proxy

As discussed above, there is a fundamental issue in keeping the credentials of an account on a third-party service or system so that the application can log onto it, especially if the only log-on approach is a username and password.

One way to partially mitigate this by delegating the communication to the service to a secure proxy, and develop a secure sign-on approach between the application and proxy. In this approach

  • The application uses a PKI scheme or two-factor authentication to sign onto the secure proxy
  • The user adds security credentials to the third-party system to the secure proxy. The credentials are never stored in the application
  • Later, when the application needs to access the third-party system, it sends a request to the proxy. The proxy logs on using the security credentials and makes the request, returning results to the application.

The disadvantages to this approach are:

  • The user may not want to trust the secure proxy with the storage of the credentials
  • The user may not trust the secure proxy with the data flowing through it to the third-party application
  • The application owner has additional infrastructure and hosting costs for running the proxy

Some answers

So, on to specific answers:

How does one securely store authentication credentials using python?

  • If storing a password for the application to authenticate the user, use a PBKDF2 algorithm, such as https://www.dlitz.net/software/python-pbkdf2/
  • If storing a password/security token to access another service, then there is no absolutely secure way.
  • However, consider switching authentication strategies to, for example the smartcard, using, eg, pyscard. You can use smartcards to both authenticate a user to the application, and also securely authenticate the application to another service with X.509 certs.

Can something be done about the language "everything is public" philosophy? I know "we're all consenting adults here", but should we be forced to choose between sharing our passwords with an attacker and using another language?

IMHO there is nothing wrong with writing a specific module in Python that does it's damnedest to hide the secret information, making it a right bugger for others to reuse (annoying other programmers is its purpose). You could even code large portions in C and link to it. However, don't do this for other modules for obvious reasons.

Ultimately, though, if the hacker has control over the computer, there is no privacy on the computer at all. Theoretical worst-case is that your program is running in a VM, and the hacker has complete access to all memory on the computer, including the BIOS and graphics card, and can step your application though authentication to discover its secrets.

Given no absolute privacy, the rest is just obfuscation, and the level of protection is simply how hard it is obfuscated vs. how much a skilled hacker wants the information. And we all know how that ends, even for custom hardware and billion-dollar products.

Using Python keyring

While this will quite securely manage the key with respect to other applications, all Python applications share access to the tokens. This is not in the slightest bit secure to the type of attack you are worried about.


I'm no expert in this field and am really just looking to solve the same problem that you are, but it looks like something like Hashicorp's Vault might be able to help out quite nicely.

In particular WRT to the problem of storing credentials for 3rd part services. e.g.:

In the modern world of API-driven everything, many systems also support programmatic creation of access credentials. Vault takes advantage of this support through a feature called dynamic secrets: secrets that are generated on-demand, and also support automatic revocation.

For Vault 0.1, Vault supports dynamically generating AWS, SQL, and Consul credentials.

More links: