Reputation: 313
I have recently been tasked with coming up with a solution for providing renaming functions, as well as various other obfuscation pre-compile at runtime. I believe using Roslyn is the way to go, but please provide any insight you may have.
The ultimate goal is as follows: Allow end user to select various options that are then generated into a text version of assembly at runtime. We then use Roslyn to generate the .exe. I was curious if it possible to obfuscate at runtime, before the EXE is even generated. This way I can rename vars, etc.
Upvotes: 2
Views: 1187
Reputation: 95354
You can use any tool that can reliably transform C# source code.
Roslyn is one but in a funny way; you can modify the program and produce object code. That should work.
Other Program Transformation Systems (PTS) can do this by modifying the source code. A PTS reads source code, builds compiler data structures (e.g., ASTs), lets you modify the ASTs, and then can regenerate source code from the modified AST. That way you can see the obfuscated code; you can always compile it later with the C# compiler. A good PTS will let you write code transformations in terms of the syntax of the targeted language in a form like this:
if you see *this pattern*, replace it by *that pattern*
expressed below as
rule <name> <patternvariables> "thispattern" -> "thatpattern";
Using a PTS, you can arguably make arbitrary changes to the source code, including function and variable renaming, code flow scrambling and data flow scrambling. For instance, you might use this rule to add confusion:
rule scramble_if_then(c: condition, b: block): statement -> statement
" if (\c) \b " -> "int temp = \c?4:3;
while (temp>3) {\b; temp--; }";
This rule is a bit simple/silly but I think it makes the point that you can write readable source code transformations. If you have many such rules, it will scramble the code a lot, especially if your rules do sophisticated transformations.
We use our DMS Software Reengineering Toolkit to implement name-scrambling obfuscators, including one for C#.
Upvotes: 5