Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Secure credential storage in python

The attack

One possible threat model, in the context of credential storage, is an attacker which has the ability to :

  • inspect any (user) process memory
  • read local (user) files

AFAIK, the consensus on this type of attack is that it's impossible to prevent (since the credentials must be stored in memory for the program to actually use them), but there's a couple of techniques to mitigate it:

  • minimize the amount of time the sensitive data is stored in memory
  • overwrite the memory as soon as the data is not needed anymore
  • mangle the data in memory, keep moving it, and other security through obscurity measures

Python in particular

The first technique is easy enough to implement, possibly through a keyring (hopefully kernel space storage)

The second one is not achievable at all without writing a C module, to the best of my knowledge (but I'd love to be proved wrong here, or to have a list of existing modules)

The third one is tricky.

In particular, python being a language with very powerful introspection and reflection capabilities, it's difficult to prevent access to the credentials to anyone which can execute python code in the interpreter process.

There seems to be a consensus that there's no way to enforce private attributes and that attempts at it will at best annoy other programmers who are using your code.

The question

Taking all this into consideration, how does one securely store authentication credentials using python? What are the best practices? 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?

like image 278
loopbackbee Avatar asked Jan 31 '13 22:01

loopbackbee


2 Answers

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.

like image 101
Andrew Alcock Avatar answered Oct 24 '22 16:10

Andrew Alcock


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:

  • Github
  • Vault Website
  • Use Cases
like image 40
toast38coza Avatar answered Oct 24 '22 16:10

toast38coza