Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Securing a password in source code?

I have a password in my code which is needed to connect to a sftp server. Whats the best way to "obfuscate" or hide it in the code?

Thanks

like image 414
grady Avatar asked Nov 11 '10 14:11

grady


People also ask

What method should be used to pass credentials in source code?

You should encrypt your credentials before saving the file, and additionally, you can apply a second encryption to the file itself (2-layer encryption to the credentials, and 1-layer to other file contents). Note that each of the two encryption processes mentioned above can be multiple-layered themselves.

What is a hardcoded password?

Hardcoded Passwords, also often referred to as Embedded Credentials, are plain text passwords or other secrets in source code. Password hardcoding refers to the practice of embedding plain text (non-encrypted) passwords and other secrets (SSH Keys, DevOps secrets, etc.) into the source code.

Can you protect source code?

Implementing network security solutions such as firewalls, Virtual Private Networks (VPN), anti-virus, and anti-malware software count as basic protection. These solutions safeguard your source code from external exploits of hackers and ensure secure data sharing between employees and data sources.


2 Answers

Don't store you password in your source code, store it in a protected section within you App.Config (or Web.Config).

See Encrypting Configuration File Sections Using Protected Configuration section in this Microsoft Doc

This works by encrypting the encryption keys using built-in Windows stuff, locked to the MAC address and various other undocumented things.

This will even work if you are using more than one server:

... if you are planning to use the same encrypted configuration file on multiple servers, such as a Web farm, only the RsaProtectedConfigurationProvider enables you to export the encryption keys used to encrypt the data and import them on another server.

Using this, if someone wanted to get your password, they would have to first break the Windows security on your server (not impossible, but harder than looking into your IL for the password by far).

like image 153
badbod99 Avatar answered Sep 25 '22 09:09

badbod99


I actually consider using the "protected sections" feature in App.Config or Web.Config to be LESS secure than storing the password in your code.

Anyone with server access can decrypt that section of the config just as quick as you encrypted it by running the decrypt command described in the article everyone keeps quoting:

aspnet_regiis -pd "connectionStrings" -app "/SampleApplication" 

https://msdn.microsoft.com/en-us/library/zhhddkxy.aspx#Anchor_1

So this feature of ASP.Net only adds security in the case that a hacker somehow had access to your web.config but not your entire server (happened in 2010 as @djteller mentioned in the oracle padding attack comment). But if they do have server access, you're exposed in one cmd call. They don't even have to install ildasm.exe.

However, storing actual passwords in your code is a maintenance nightmare. So one thing I've seen done is storing an encrypted password in your web.config and storing the encryption key in your code. This accomplishes the goal of hiding passwords from casual browsing while still being maintainable.

In this case a hacker has to at least decompile your code, find your key, and then figure out what encryption algorithm you're using. Not impossible, but certainly harder than running "aspnet_regiis -pd...".

Meanwhile I am also looking for better answers to this six year old question...

like image 36
Will Avatar answered Sep 22 '22 09:09

Will