I'm attempting to make an obfuscator and so far it's going well. Right now I'm trying to encode strings with base64
and then rot13
that just to make it an extra bit unreadable and hide my sooper sekret strings.
This is what I have for encoding the strings on the obfuscator:
ILProcessor processor = method.Body.GetILProcessor();
foreach (Instruction instruction in processor.Body.Instructions)
{
if (instruction.OpCode == OpCodes.Ldstr)
{
instruction.Operand = Enc.to64(Enc.Rot((String)instruction.Operand, 0x0D)); //0x0D = 13
}
}
So now what I need to do is inject my Enc
class into the root namespace and since the functions are static I'll be able to use them without creating an instance of the class.
Then what I'll need to do is wrap each string in two of the functions in Enc
to decode the string when the program is actually used, making
string lel = "x";
into
string lel = Enc.from64(Enc.Rot("encoded x", 0x0D));
I know how to do neither of these things, so there's where I need your help.
So to recap I need to know how to inject a class (with static functions) into the root namespace and then wrap all strings in the program with two functions from said class so that it's unreadable when reflected but decoded when used.
1. Inject a class into user’s assembly.
If I understand you correctly, you have an
Enc
class in a separate assembly, and you want to create copy of this class in a user’s assembly during the obfuscation. In that case, you could implement creating copy of class usingMono.Cecil
. You just open two assemblies:find the required class in your assembly:
and create a new class with same attributes and members of the user’s assembly:
There are third-party tools for this task: ILMerge, ILRepack, Mono.Merge. You can use one of them or write your own implementation based on them.
2. Inject a call of method into user’s assembly.
The
Mono.Cecil
contains everything you need to change an IL code of method body. You will need to parse the existing IL code of methods to find injection points. Parsing algorithm depends on your requirements for transformation code. You can look an example of a method call injection in my other answer.