3

So, i don't know if i was completely clear in my question but what I'm trying to do is a simple concept: i want to while compiling a c# windows forms application a variable X assume a value i wont know. But EVERY time i execute the application without compiling it again, this value is the same (but you don't know). So what I'm asking is if exist the possibility to compile something that you don't know which value will be. The application will use this same variable with the same value each time, but every time you compile the application again, this variable would change.

I'm trying to implement a security algorithm that will be different for each distribution of my application, but i want to do this in a automatic way without changing manual paramters for each release of my application.

A simple example of what i mean:

  • (while compiling) i = rand().
  • (when executed the app) i = ? (some value you will never know, but will be the same every time you execute the application)
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 1
    Commenters did not read the full question... "every time you compile the application again, this variable would change" – Ben Voigt Jan 31 '15 at 23:53
  • I don't think there's any way built into the normal C# compiler. However, none of this will do you any good if the recipient can simply decompile the application, so surely you are running an obfuscator as a post-compile step. And a post-compile step is the place to insert this magic value, either during obfuscation or immediately before. – Ben Voigt Jan 31 '15 at 23:56
  • 1
    You can utilize T4 templates for source code which will generate new source code file with new random number in it. – abatishchev Jan 31 '15 at 23:57
  • hum, could i use t4 template to execute a method that i made to generate random value then? – Matheus Garbelini Jan 31 '15 at 23:59
  • @abatishchev making T4 to run compile-time is probably more overhead than just creating a task (unless it is already used in the project) - https://msdn.microsoft.com/en-us/library/ee847423.aspx. – Alexei Levenkov Feb 01 '15 at 00:04

2 Answers2

5

The C# compiler emits a unique (random) ModuleVersionId (Guid) in every build. This differs even when building from the exact same source.

You may be able to use that as a build token.

As Enigmativity kindly mentioned, you can get to that value using:

this.GetType().Assembly.ManifestModule.ModuleVersionId
oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
  • 1
    I was about to post exactly the same answer. It can be found like this: `this.GetType().Assembly.ManifestModule.ModuleVersionId`. – Enigmativity Feb 01 '15 at 00:27
  • oh my godddd, this was exactly what i need, i can use this as a seed to a function that i know will perform the same exactly operation each time i open the application, but i wont know the seed. Thank you so much xD i'm very happy now xD xD xD , thanks oɔɯǝɹ and @Enigmativity – Matheus Garbelini Feb 02 '15 at 01:08
0

You can generate source or resource at build time and include that value as part of such source.

Links: Displaying the build date shows how to create resource at build time, you can also check out MSBuild task reference to build more integrated solution.

Note that protection of such kind is very easy to circumvent...

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • hum interesting, i was think in running a method that will generate random RSA keys, every time i rebuild the application will generate a different key and save in a vector. – Matheus Garbelini Jan 31 '15 at 23:58