Git Product home page Git Product logo

tutorials-stringencryption's Introduction

Tutorial 1 - Strings Encryption

##Intro. String encryption is a protection method to convert Readable string to Unreadable String.

image

##Let's Start. First of all we have to Select the application that we want to Obfuscate it.

ModuleDef md = ModuleDefMD.Load(@"AppPath"); //replace AppPath with your Application Path you want to Obfuscate it.

Then we have to load the Types from the Module.

foreach(TypeDef type in md.Types)
{

}

after we loaded the Types from module, now we have to get the methods from the type.

foreach(MethodDef method in type.Methods)
{

}

After we loaded the Methods from type, now we need to get the Instructions for method and check if there's any String (String OpCode = Ldstr). But we should check if the method body isn't Null.

if (method.Body == null) continue;

Now we are going to get the instructions from method.

for(int i = 0; i < method.Body.Instructions.Count; i++)
{
                        
}

after getting the instructions from method, and check it if its null or no. now we have to check if the instruction is String

if (method.Body.Instructions[i].OpCode == OpCodes.Ldstr)
{
}

I will use Base64 for Encrypt the String and UTF8 for Encoding.

String oldString = method.Body.Instructions[i].Operand.ToString(); //Original String.
String newString = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(oldString)); //Encrypted String by Base64

Now we have to Inject the Decrypt Instructions

###Decrypt Instructions

image

To Dnlib Inject

method.Body.Instructions[i].OpCode = OpCodes.Nop; //Change the Opcode for the Original Instruction
method.Body.Instructions.Insert(i + 1, new Instruction(OpCodes.Call, md.Import(typeof(System.Text.Encoding).GetMethod("get_UTF8", new Type[] { })))); //get Method (get_UTF8) from Type (System.Text.Encoding).
method.Body.Instructions.Insert(i + 2, new Instruction(OpCodes.Ldstr, newString)); //add the Encrypted String
method.Body.Instructions.Insert(i + 3, new Instruction(OpCodes.Call, md.Import(typeof(System.Convert).GetMethod("FromBase64String", new Type[] { typeof(string) })))); //get Method (FromBase64String) from Type (System.Convert), and arguments for method we will get it using "new Type[] { typeof(string) }"
method.Body.Instructions.Insert(i + 4, new Instruction(OpCodes.Callvirt, md.Import(typeof(System.Text.Encoding).GetMethod("GetString", new Type[] { typeof(byte[]) }))));
i += 4; //skip the Instructions that we have just added.

Now we're almost done, now we only need to Write the obfuscated module.

md.Write("ObfuscatedApp.exe");

Done, Enjoy :)).

###the code should be like this

ModuleDef md = ModuleDefMD.Load(@"AppPath");
foreach(TypeDef type in md.Types)
{
    foreach (MethodDef method in type.Methods)
    {
    if (method.Body == null) continue;
        for(int i = 0; i < method.Body.Instructions.Count(); i++)
        {
            if (method.Body.Instructions[i].OpCode == OpCodes.Ldstr)
            {
                //Encoding.UTF8.GetString(Convert.FromBase64String(""));
                String oldString = method.Body.Instructions[i].Operand.ToString(); //Original String.
                String newString = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(oldString)); //Encrypted String by Base64
                method.Body.Instructions[i].OpCode = OpCodes.Nop; //Change the Opcode for the Original Instruction
                method.Body.Instructions.Insert(i + 1, new Instruction(OpCodes.Call, md.Import(typeof(System.Text.Encoding).GetMethod("get_UTF8", new Type[] { })))); //get Method (get_UTF8) from Type (System.Text.Encoding).
                method.Body.Instructions.Insert(i + 2, new Instruction(OpCodes.Ldstr, newString)); //add the Encrypted String
                method.Body.Instructions.Insert(i + 3, new Instruction(OpCodes.Call, md.Import(typeof(System.Convert).GetMethod("FromBase64String", new Type[] { typeof(string) })))); //get Method (FromBase64String) from Type (System.Convert), and arguments for method we will get it using "new Type[] { typeof(string) }"
                method.Body.Instructions.Insert(i + 4, new Instruction(OpCodes.Callvirt, md.Import(typeof(System.Text.Encoding).GetMethod("GetString", new Type[] { typeof(byte[]) }))));
                i += 4; //skip the Instructions that we have just added.
            }
        }
    }
}
md.Write("ObfuscatedApp.exe");

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.