Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Encrypt Your Web.config, Please

, 16 Jul 2009 CC (ASA 2.5)
Rate this:
Please Sign up or sign in to vote.
Are you encrypting your web.config files? It's not that hard to do, but in this post, I share a program I wrote to make it as easy as point and click. Now, you can remotely encrypt and decrypt your web.config files without needing to use a command prompt or look up IIS information!

Introduction

If you follow me on Twitter, you may notice me talk about #BadVendor from time to time. Actually, they were recently upgraded to #EpicFailVendor when I discovered they weren’t cleaning strings before passing them into SQL queries. Needless to say, everyday has been a little more shocking than the next.

For the most part, all of these systems are things I can’t make changes to — either it’s compiled code, or I just don’t have the authority to go in and make the fixes, but there is something that I can do — encrypt their web.config files.

Making Encrypting Easier

Encrypting normally involves logging onto the server in question, locating a few mildly obscure pieces of information, and then running aspnet_regiis. It’s not that hard but it isn’t point and click easy as well.

I wanted to make it easier to update these files without needing to locate all the information each time, so I wrote a little application to make the whole process a bit easier. The utility uses credentials you supply to log into your servers via WMI and locate the required information and then encrypt your files without needing to pull up a command prompt.

I’m not really a WinForms guy and WMI is certainly not my specialty, but this program came together pretty quickly and seems to be fairly decent. It’s certainly not bug free and could use a round of refactoring to make it nicer, so any feedback is appreciated.

How It Works

The first step is to provide your credentials to the server you want to log into. If you choose to run the tool on the actual server itself, then you can leave all those fields blank (since WMI won’t use them for local connections anyways). If you aren’t an admin for that server or at least have an account with some elevated credentials, then this may not work for you.

nkript.screen1

Once you successfully connect to the server, a list of the sites on the server will be loaded along with any virtual directories (since they could potentially contain a web.config file). At this point, you can simply browse around and find the web.config you’re wanting to encrypt.

nkript.screen2

It’s worth noting that if there aren’t any web.config (that name specifically) found inside the directory then it won’t be listed. If you happened to have something named web.temp.config then it won’t show up on this list.

At this point, the program is going to do a little painful WMI magic and connect out to your server and load the web.config file into the view. The config file will be parsed and all the root nodes will be listed as available to be encrypted.

nkript.screen3

There are apparently some rules about what can or cannot be encrypted, so if the actual aspnet_regiis call fails, you’ll just end up with the same file as before, but you don’t get an explicit message as to why (still trying to find out how I can access messages like that in a semi-reliable WMI fashion).

There isn’t much configuration for this application. The default settings are used to perform the encryption and decryption of the web.config files, so if you are wanting to add some features on, you are more than welcome to add them in. I’d love to hear about your changes so I can add them to this version.

2gmLEwHP/vZ/NYLeiTattLFnylc= *

It’s not hard to encrypt your web.config files and keep your sensitive information safe. The command line tool aspnet_regiis offers a lot of great functions to further protect your data. Hopefully, this tool allows you to get your work done even faster (that thing up there is SHA1.ComputeHash(”*I <3 Encryption") as Base64 string).

Now, if you’ll excuse me, I need to share this tool with #EpicFailVendor. I dunno about the rest of you, but enough is enough! I’ve had it with these monkey fighting vendors not encrypting their Monday to Friday web.configs!

Mandatory disclaimer: This program is certified as ‘Works On My Machine’ – The author makes no warranties about how it might behave in your environment (but most likely, you have nothing to worry about).

Downloads

Use these links for the latest versions:

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License

Share

About the Author

webdev_hb

United States United States
No Biography provided

Comments and Discussions

 
QuestionWhat to put in the domain PinmemberIdanShechter17-Feb-13 14:59 
GeneralMy vote of 5 Pinmemberhypermellow6-Mar-12 2:31 
GeneralMy vote of 5 Pinmemberjfriedman3-Mar-12 11:48 
Generallogin.aspx PinmemberAjay Kale New27-Sep-10 1:05 
Generalautomatic redirection to login.aspx PinmemberAjay Kale New10-Sep-10 5:21 
Generali agree encrypt the file PinmemberDonsw21-Aug-09 18:04 
Generalthe Namespace used here support only Win 2003 Server ? [modified] Pinmembervrajaraman27-Jul-09 21:36 
GeneralRe: the Namespace used here support only Win 2003 Server ? Pinmemberwebdev_hb28-Jul-09 4:44 
GeneralMy vote of 1 PinmemberGary Noter21-Jul-09 7:51 
GeneralRe: My vote of 1 Pinmemberwebdev_hb21-Jul-09 8:20 
QuestionHow about encrypting using RSA? Pinmembervrajaraman20-Jul-09 17:39 
AnswerRe: How about encrypting using RSA? Pinmemberwebdev_hb20-Jul-09 18:05 
GeneralRe: How about encrypting using RSA? Pinmembervrajaraman20-Jul-09 18:30 
GeneralRe: How about encrypting using RSA? Pinmemberwebdev_hb20-Jul-09 18:37 
GeneralRe: How about encrypting using RSA? Pinmembervrajaraman20-Jul-09 18:41 
GeneralRe: How about encrypting using RSA? Pinmemberwebdev_hb20-Jul-09 19:35 
QuestionCouldn't read the file PinmemberGregSawin20-Jul-09 10:11 
AnswerRe: Couldn't read the file Pinmemberwebdev_hb20-Jul-09 10:21 
GeneralRe: Couldn't read the file PinmemberGregSawin20-Jul-09 10:34 
GeneralRe: Couldn't read the file Pinmemberwebdev_hb20-Jul-09 11:02 
GeneralRe: Couldn't read the file PinmemberGregSawin20-Jul-09 12:06 
GeneralRe: Couldn't read the file Pinmemberwebdev_hb20-Jul-09 12:14 
GeneralMy vote of 1 PinmemberTawani Anyangwe17-Jul-09 3:17 
GeneralRe: My vote of 1 [modified] Pinmemberwebdev_hb17-Jul-09 7:09 
GeneralRe: My vote of 1 PinmemberGavin Harriss21-Jul-09 1:02 
GeneralRe: My vote of 1 PinmemberXmen W.K.21-Jul-09 20:24 
GeneralRe: My vote of 1 Pinmemberwebdev_hb21-Jul-09 20:51 
GeneralRe: My vote of 1 PinmemberXmen W.K.21-Jul-09 20:59 
GeneralRe: My vote of 1 PinmemberIssa Qandil21-Jul-09 21:08 
GeneralRe: My vote of 1 Pinmemberwebdev_hb28-Jul-09 4:55 
JokeRe: My vote of 1 PinmemberGary Noter28-Jul-09 6:46 
GeneralRe: My vote of 1 PinmemberXmen W.K.28-Jul-09 7:48 
GeneralRe: My vote of 1 PinmemberSky Sanders16-Aug-09 11:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 16 Jul 2009
Article Copyright 2009 by webdev_hb
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid