Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Constructor Creation [Mono.Cecil]

I've been having some issues with static constructors with my project. I need to add a static constructor to the type "" in order to call my resource decryption method.

Below in the gif you will see the issue I run into.

I will also include the code snippet. enter image description here

Code for creating cctor:

MethodDefinition method = new MethodDefinition(
    ".cctor",
    Mono.Cecil.MethodAttributes.Private
    | Mono.Cecil.MethodAttributes.Static
    | Mono.Cecil.MethodAttributes.HideBySig
    | Mono.Cecil.MethodAttributes.SpecialName
    | Mono.Cecil.MethodAttributes.RTSpecialName,
    mod.Import(typeof(void))
); 

I have also tried changing the attributes to the exact same as Yano's. It somehow never works. By "works" I mean detect it as a static constructor in DotNet Resolver.

Here's some more information about real outcome and expected result.

enter image description here

I do not have an ResolveEventHandler attached to my entrypoint. I have it attached to the application, which is being obfuscated and it is located in the "" type's static constructor which is executed before even the entrypoint is called.

Application resources have been encrypted with AES and are not recognised as valid resources by dotnet resolver or other decompilers. I am simply asking why the event is not being triggered as it should be triggered when a resource is invalid or missing. As you can see in the example a messagebox should pop up before the application is launched but it never does (string encryption encrypts the strings, so its a bit hard to see theres a string there).

Any help is appreciated.

like image 778
user2699298 Avatar asked Dec 22 '13 17:12

user2699298


1 Answers

use this :

void AddConstructor(TypeDefinition type, MethodReference baseEmptyConstructor)
{
    var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
    var method = new MethodDefinition(".ctor", methodAttributes, ModuleDefinition.TypeSystem.Void);
    method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
    method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, baseEmptyConstructor));
    method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
    type.Methods.Add(method);
}

you can also refer :

http://www.mono-project.com/Cecil:FAQ

like image 54
Imran Ali Khan Avatar answered Oct 03 '22 17:10

Imran Ali Khan