<p>I'd like to bind a configuration file to my executable. I'd like to do this by storing an MD5 hash of the file inside the executable. This should keep anyone but the executable from modifying the file.</p>

<p>Essentially if someone modifies this file outside of the program the program should fail to load it again.</p>

<p>EDIT: The program processes credit card information so being able to change the configuration in any way could be a potential security risk. This software will be distributed to a large number of clients. Ideally client should have a configuration that is tied directly to the executable. This will hopefully keep a hacker from being able to get a fake configuration into place.</p>

<p>The configuration still needs to be editable though so compiling an individual copy for each customer is not an option.</p>

<hr>

<p>It's important that this be dynamic. So that I can tie the hash to the configuration file as the configuration changes.</p>

[{'answer_id': 35109, 'author': 'Joel Martinez', 'author_id': 3433, 'author_profile': 'https://Stackoverflow.com/users/3433', 'pm_score': -1, 'selected': False, 'text': '<p>just make a const string that holds the md5 hash and compile it into your app ... your app can then just refer to this const string when validating the configuration file</p>\n'}, {'answer_id': 35116, 'author': 'Jim McKeeth', 'author_id': 255, 'author_profile': 'https://Stackoverflow.com/users/255', 'pm_score': 5, 'selected': True, 'text': '<p>A better solution is to store the MD5 in the configuration file.  But instead of the MD5 being just of the configuration file, also include some secret "key" value, like a fixed guid, in the MD5.</p>\n\n<pre><code>write(MD5(SecretKey + ConfigFileText));\n</code></pre>\n\n<p>Then you simply remove that MD5 and rehash the file (including your secret key).  If the MD5\'s are the same, then no-one modified it.  This prevents someone from modifying it and re-applying the MD5 since they don\'t know your secret key.</p>\n\n<p>Keep in mind this is a fairly weak solution (as is the one you are suggesting) as they could easily track into your program to find the key or where the MD5 is stored.  </p>\n\n<p>A better solution would be to use a public key system and sign the configuration file.  Again that is weak since that would require the private key to be stored on their local machine.  Pretty much anything that is contained on their local PC can be bypassed with enough effort.</p>\n\n<p>If you REALLY want to store the information in your executable (which I would discourage) then you can just try appending it at the end of the EXE.  That is usually safe.  Modifying executable programs is <em>virus like</em> behavior and most operating system security will try to stop you too.  If your program is in the Program Files directory, and your configuration file is in the Application Data directory, and the user is logged in as a non-administrator (in XP or Vista), then you will be unable to update the EXE.</p>\n\n<p><strong>Update:</strong> I don\'t care if you are using Asymmetric encryption, RSA or Quantum cryptography, if you are storing your keys on the user\'s computer (which you <em>must</em> do unless you route it all through a web service) then the user can find your keys, even if it means inspecting the registers on the CPU at run time!  You are only buying yourself a moderate level of security, so stick with something that is simple.  To prevent modification the solution I suggested is the best.  To prevent reading then encrypt it, and if you are storing your key locally then use AES Rijndael.</p>\n\n<p><strong>Update:</strong>  The FixedGUID / SecretKey could alternatively be generated at install time and stored somewhere "secret" in the registry.  Or you could generate it every time you use it from hardware configuration.  Then you are getting more complicated.  How you want to do this to allow for moderate levels of hardware changes would be to take 6 different signatures, and hash your configuration file 6 times - once with each.  Combine each one with a 2nd secret value, like the GUID mentioned above (either global or generated at install).  Then when you check you verify each hash separately.  As long as they have 3 out of 6 (or whatever your tolerance is) then you accept it.  Next time you write it you hash it with the new hardware configuration.  This allows them to slowly swap out hardware over time and get a whole new system. . . Maybe that is a weakness.  It all comes down to your tolerance.  There are variations based on tighter tolerances.</p>\n\n<p><strong>UPDATE:</strong>  For a Credit Card system you might want to consider some real security.  You should retain the services of a <em>security and cryptography consultant</em>.  More information needs to be exchanged.  They need to analyze your specific needs and risks.  </p>\n\n<p>Also, if you want security with .NET you need to first start with a really good .NET obfuscator (<a href="http://www.google.com/search?hl=en&amp;q=.NET%20obfuscator&amp;aq=f&amp;oq=" rel="nofollow noreferrer">just Google it</a>).  A .NET assembly is way to easy to disassemble and get at the source code and read all your secrets.  Not to sound a like a broken record, but anything that depends on the security of your user\'s system is fundamentally flawed from the beginning.  </p>\n'}, {'answer_id': 35117, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 1, 'selected': False, 'text': "<p>Out of pure curiosity, what's your reasoning for never wanting to load the file if it's been changed?</p>\n\n<p>Why not just keep all of the configuration information compiled in the executable?  Why bother with an external file at all?</p>\n\n<p><strong><em>Edit</em></strong></p>\n\n<p>I just read your edit about this being a credit card info program.  That poses a very interesting challenge.</p>\n\n<p>I would think, for that level of security, some sort of pretty major encryption would be necessary but I don't know anything about handling that sort of thing in such a way that the cryptographic secrets can't just be extracted from the executable.</p>\n\n<p>Is authenticating against some sort of online source a possibility?</p>\n"}, {'answer_id': 35161, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 1, 'selected': False, 'text': '<p>I\'d suggest you use a <strong>Assymmetric Key Encryption</strong> to encrypt your configuration file, wherever they are stored, inside the executable or not.</p>\n<p>If I remember correctly, RSA is one the variants.</p>\n<p>For the explanation of it, see <a href="http://en.wikipedia.org/wiki/Public-key_cryptography" rel="nofollow noreferrer">Public-key cryptography</a> on Wikipedia</p>\n<p>Store the &quot;reading&quot; key in your executable and keep to yourself the &quot;writing&quot; key. So no one but you can modify the configuration.</p>\n<p>This has the advantages of:</p>\n<ul>\n<li>No-one can modify the configuration unless they have the &quot;writing&quot; key because any modification will <em>corrupt</em> it entirely, even if they know the &quot;reading&quot; key it would takes ages to compute the other key.</li>\n<li>Modification guarantee.</li>\n<li>It\'s not hard - there are plenty of libraries available these days. There\'re also a lot of key-generation programs that can generate really, really long keys.</li>\n</ul>\n<p>Do take some research on how to properly implement them though.</p>\n'}]
['https://Stackoverflow.com/questions/35103', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2191/']