private void Obfuscate(BuildMethod method)
        {
            if (!method.ObfuscateStrings)
            {
                return;
            }

            if (!MethodBody.IsValid(method))
            {
                return;
            }

            var methodBody = MethodBody.Load(method);

            if (!HasLoadStringInstruction(methodBody))
            {
                return;
            }

            var ilBody = ILBody.Load(methodBody);

            Obfuscate(ilBody);
            ilBody.CalculateMaxStackSize(method);

            methodBody = ilBody.Build();
            methodBody.Build(method);
        }
Example #2
0
        private void GenerateGlobalCCtor(BuildMethod proxyMethod)
        {
            var ownerType = _module.Types.GetOrCreateGlobal();

            var method = ownerType.Methods.GetOrCreateStaticConstructor();

            MethodBody methodBody;

            if (MethodBody.IsValid(method))
            {
                methodBody = MethodBody.Load(method);
            }
            else
            {
                methodBody = new MethodBody();
                methodBody.Instructions.Add(new Instruction(OpCodes.Ret));
            }

            if (methodBody.MaxStackSize < 2)
            {
                methodBody.MaxStackSize = 2;
            }

            methodBody.Instructions.Insert(0, new Instruction(OpCodes.Call, proxyMethod.ToReference(proxyMethod.Module)));
            methodBody.ShiftRightExceptionHandlerOffsets(1);
            methodBody.Build(method);
        }
 private bool IsMainTypeInitMethod(BuildMethod method)
 {
     return
         (method.IsStaticConstructor() ||
          method.Name == "InitializeControlFlow" ||
          method.Name == "InitializeMethodPointers");
 }
        private void ObfuscateControlFlowInitMethod(BuildMethod method)
        {
            if (!MethodBody.IsValid(method))
            {
                return;
            }

            var methodBody = MethodBody.Load(method);

            var ilBody = ILBody.Load(methodBody);

            // Obfuscate
            var obfuscator = new ControlFlowObfuscator(ilBody, method);

            obfuscator.DoNotUseFieldNumber = true;
            obfuscator.Obfuscate();

            // Calculate max stack size
            ilBody.CalculateMaxStackSize(method);

            // Save
            methodBody = ilBody.Build();

            methodBody.Build(method);
        }
        private void Build(BuildMethod method)
        {
            var cryptoMethod = method.CreateILCrypto();

            var type = method.GetOwnerType();

            var invokeTypeInfo = GetInvokeType(type.GenericParameters.Count);

            var invokeType = invokeTypeInfo.Instance;

            cryptoMethod.MethodID = invokeType.MethodCount++;

            // Delegate type
            var delegateGenericArguments = new List <TypeSignature>();
            var delegateType             = _delegateTypeBuilder.Build(method, delegateGenericArguments);

            if (delegateGenericArguments.Count > 0)
            {
                cryptoMethod.DelegateGenericArguments = delegateGenericArguments.ToArray();
            }

            // Invoke method
            var invokeGenericArguments = new List <TypeSignature>();
            var invokeMethod           = BuildInvokeMethod(method, invokeTypeInfo, invokeGenericArguments);

            invokeMethod.DelegateType = delegateType;

            cryptoMethod.InvokeMethod = invokeMethod;

            if (invokeGenericArguments.Count > 0)
            {
                cryptoMethod.InvokeGenericArguments = invokeGenericArguments.ToArray();
            }
        }
        public static void Obfuscate(BuildMethod method, bool ignoreEncryptIL)
        {
            if (!method.ObfuscateControlFlow)
            {
                return;
            }

            if (ignoreEncryptIL && method.EncryptIL)
            {
                return;
            }

            if (!MethodBody.IsValid(method))
            {
                return;
            }

            var methodBody = MethodBody.Load(method);

            var ilBody = ILBody.Load(methodBody);

            // Obfuscate
            var obfuscator = new ControlFlowObfuscator(ilBody, method);

            obfuscator.Obfuscate();

            // Save
            methodBody = ilBody.Build();

            methodBody.Build(method);
        }
		private bool Build(ref MethodReference methodRef, BuildMethod method)
		{
			bool changed = false;

			// Name
			string name;
			if (method.NameChanged)
			{
				name = method.NewName;
				changed = true;
			}
			else
			{
				name = method.Name;
			}

			// Owner
			var owner = methodRef.Owner;
			changed |= Build(ref owner);

			// Call site
			var callSite = methodRef.CallSite;
			changed |= Build(ref callSite);

			if (!changed)
				return false;

			methodRef = new MethodReference(name, owner, callSite);
			return true;
		}
 private void UnstripAndEnqueue(BuildMethod method)
 {
     if (method.Strip)
     {
         method.Strip = false;
         Enqueue(method);
     }
 }
 private void Enqueue(BuildMethod method)
 {
     if (!method.StripProcessed)
     {
         method.StripProcessed = true;
         _queue.Enqueue(method);
     }
 }
        private InvokeMethodInfo BuildInvokeMethod(BuildMethod method, List <TypeSignature> genericArguments)
        {
            var invokeMethodInfo = new InvokeMethodInfo();

            int argumentCount = method.Parameters.Count + 1;

            if (method.HasThis)
            {
                argumentCount++;
            }

            var arguments      = new TypeSignature[argumentCount];
            var parameterFlags = new int[argumentCount];

            // Arguments
            {
                int index = 0;
                if (method.HasThis)
                {
                    arguments[index++] = new GenericParameterType(true, 0);
                    genericArguments.Add(TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly));
                }

                var parameters = method.Parameters;

                for (int i = 0; i < parameters.Count; i++)
                {
                    var parameter = parameters[i];
                    arguments[index]      = BuildType(parameter.Type, genericArguments);
                    parameterFlags[index] = GetParameterFlags(parameter);
                    index++;
                }

                // index : [mscorlib]System.Int32
                arguments[index++] = TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly);
            }

            var returnType = BuildType(method.ReturnType.Type, genericArguments);

            invokeMethodInfo.CallSite =
                new CallSite(
                    false,
                    false,
                    MethodCallingConvention.Default,
                    returnType,
                    arguments,
                    -1,
                    genericArguments.Count);

            invokeMethodInfo.ParameterFlags = parameterFlags;

            invokeMethodInfo.GenericParameterCount = genericArguments.Count;

            return(invokeMethodInfo);
        }
        private DelegateTypeInfo BuildInfo(BuildMethod method, List <TypeSignature> genericArguments)
        {
            var typeInfo = new DelegateTypeInfo();

            int argumentCount = method.Parameters.Count;

            if (method.HasThis)
            {
                argumentCount++;
            }

            var arguments      = new TypeSignature[argumentCount];
            var parameterFlags = new int[argumentCount];

            if (argumentCount > 0)
            {
                int index = 0;
                if (method.HasThis)
                {
                    arguments[index++] = new GenericParameterType(false, 0);
                    genericArguments.Add(TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly));
                }

                var parameters = method.Parameters;

                for (int i = 0; i < parameters.Count; i++)
                {
                    var parameter = parameters[i];
                    arguments[index]      = BuildType(parameter.Type, genericArguments);
                    parameterFlags[index] = GetParameterFlags(parameter);
                    index++;
                }
            }

            var returnType = BuildType(method.ReturnType.Type, genericArguments);

            typeInfo.InvokeCallSite =
                new CallSite(
                    true,
                    false,
                    MethodCallingConvention.Default,
                    returnType,
                    arguments,
                    -1,
                    0);

            typeInfo.InvokeParameterFlags = parameterFlags;

            typeInfo.GenericParameterCount = genericArguments.Count;

            return(typeInfo);
        }
Example #12
0
        private void Analyze(BuildMethod method)
        {
            var attr = CA.BindableAttribute.FindFirst(method.CustomAttributes);

            if (attr != null && attr.Bindable)
            {
                if (!_renameBindableMembers)
                {
                    method.Rename = false;
                }

                method.Strip = false;
            }
        }
Example #13
0
        private void Analyze(BuildMethod method, bool serializable)
        {
            LoadSerializableAttributes(ref serializable, method.CustomAttributes);

            if (serializable)
            {
                if (!_renameSerializableMembers)
                {
                    method.Rename = false;
                }

                method.Strip = false;
            }
        }
        private void Analyze(BuildMethod method)
        {
            if (!MethodBody.IsValid(method))
            {
                return;
            }

            var body = MethodBody.Load(method);

            var instructions = body.Instructions;

            for (int i = 0; i < instructions.Count; i++)
            {
                var instruction = instructions[i];
                var opCode      = instruction.OpCode;
                if (opCode == OpCodes.Newobj)
                {
                    var calledMethodSig = instruction.Value as MethodReference;
                    if (calledMethodSig != null)
                    {
                        if (IsResourceManagerCtorWithType(calledMethodSig))
                        {
                            var typeSig = FindPrevLdtoken(instructions, i - 1) as TypeSignature;
                            if (typeSig != null)
                            {
                                Unmark(typeSig);
                            }
                        }
                        else if (IsComponentResourceManagerCtorWithType(calledMethodSig))
                        {
                            var typeSig = FindPrevLdtoken(instructions, i - 1) as TypeSignature;
                            if (typeSig != null)
                            {
                                Unmark(typeSig);
                            }
                        }
                        else if (IsBitmapCtorWithType(calledMethodSig))
                        {
                            var typeSig = FindPrevLdtoken(instructions, i - 1) as TypeSignature;
                            if (typeSig != null)
                            {
                                Unmark(typeSig);
                            }
                        }
                    }
                }
            }
        }
        private static void Unmark(BuildMethod method)
        {
            if (method.Strip)
            {
                method.DevirtualizeMethod        = false;
                method.StripObfuscationAttribute = false;

                if (!method.StripEncrypted)
                {
                    method.Rename               = false;
                    method.NameChanged          = false;
                    method.EncryptIL            = false;
                    method.ObfuscateControlFlow = false;
                    method.ObfuscateStrings     = false;
                }
            }
        }
        public static void Build(BuildMethod method)
        {
            if (!MethodBody.IsValid(method))
            {
                return;
            }

            var methodBody = MethodBody.Load(method);

            if (methodBody == null)
            {
                return;
            }

            var builder = new Builder(method);

            bool changed      = false;
            var  instructions = methodBody.Instructions;

            for (int i = 0; i < instructions.Count; i++)
            {
                var instruction = instructions[i];
                if (instruction.OpCode != OpCodes.Call && instruction.OpCode != OpCodes.Callvirt)
                {
                    continue;
                }

                var calledMethodSig = instruction.Value as MethodSignature;
                if (calledMethodSig == null)
                {
                    continue;
                }

                if (builder.Build(ref calledMethodSig))
                {
                    instructions[i] = new Instruction(instruction.OpCode, calledMethodSig);
                    changed         = true;
                }
            }

            if (changed)
            {
                methodBody.Build(method);
            }
        }
        private void Map(BuildMethod method)
        {
            if (!MethodBody.IsValid(method))
            {
                return;
            }

            var methodBody = MethodBody.Load(method);

            var ilBody = ILBody.Load(methodBody);

            if (Map(ilBody))
            {
                ilBody.CalculateMaxStackSize(method);
                methodBody = ilBody.Build();
                methodBody.Build(method);
            }
        }
        private void UnstripAndEnqueueEncrypted(BuildMethod method)
        {
            if (!method.Strip || method.StripEncrypted)
            {
                return;
            }

            if (method.EncryptIL && !method.IsVirtual)
            {
                method.StripEncrypted = true;
            }
            else
            {
                method.Strip = false;
            }

            Enqueue(method);
        }
        private bool CanStrip(BuildMethod method)
        {
            if (method.IsVirtual)
            {
                return(false);
            }

            if (method.IsConstructor())
            {
                return(false);
            }

            if (method.CodeType != MethodCodeTypeFlags.CIL)
            {
                return(false);
            }

            return(true);
        }
        public ControlFlowObfuscator(ILBody body, BuildMethod method)
        {
            if (body == null)
            {
                throw new ArgumentNullException("body");
            }

            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            _body   = body;
            _method = method;

            var module = (BuildModule)method.Module;

            _mainType = module.MainType;
            _random   = module.RandomGenerator;
        }
Example #21
0
        public static void Change(BuildMethod method)
        {
            if (method.NameChanged)
            {
                method.Name = method.NewName;
            }

            if (method.Rename || method.IsConstructor())
            {
                foreach (var parameter in method.Parameters)
                {
                    parameter.Name = null;
                }

                foreach (var genericParameter in method.GenericParameters)
                {
                    genericParameter.Name = null;
                }
            }
        }
        private void Process(BuildMethod method)
        {
            UnstripAndEnqueue((BuildType)method.GetOwnerType());

            // Process references
            var module = method.Module;

            Process(method.ReturnType, module);
            Process(method.Parameters, module);
            Process(method.GenericParameters, module);
            Process(method.Overrides, module);
            Process(method.CustomAttributes, module);
            Process(method.SecurityAttributes, module);

            if (MethodBody.IsValid(method))
            {
                var body = MethodBody.Load(method);
                Process(body, module);
            }
        }
        public DelegateType Build(BuildMethod method, List <TypeSignature> genericArguments)
        {
            var typeInfo = BuildInfo(method, genericArguments);

            int index;

            if (_types.TryAdd(typeInfo, out index))
            {
                var delegateType = _module.DelegateTypes.Add();
                delegateType.GenericParameterCount = typeInfo.GenericParameterCount;
                delegateType.InvokeParameterFlags  = typeInfo.InvokeParameterFlags;
                delegateType.InvokeCallSite        = typeInfo.InvokeCallSite;
                typeInfo.Instance = delegateType;

                return(delegateType);
            }
            else
            {
                return(_types[index].Instance);
            }
        }
Example #24
0
        private static void DevirtualizeMethod(BuildMethod method)
        {
            if (!method.DevirtualizeMethod)
            {
                return;
            }

            if (!method.IsVirtual)
            {
                return;
            }

            var ownerType = method.GetOwnerType();

            if (ownerType.IsInterface)
            {
                return;
            }

            method.IsVirtual = false;
            method.IsNewSlot = false;
        }
        private ILCryptoInvokeMethod BuildInvokeMethod(BuildMethod method, InvokeTypeInfo invokeTypeInfo, List <TypeSignature> genericArguments)
        {
            var invokeMethodInfo = BuildInvokeMethod(method, genericArguments);

            int index;

            if (invokeTypeInfo.Methods.TryAdd(invokeMethodInfo, out index))
            {
                var invokeMethod = invokeTypeInfo.Instance.InvokeMethods.Add();
                invokeMethod.GenericParameterCount = invokeMethodInfo.GenericParameterCount;
                invokeMethod.ParameterFlags        = invokeMethodInfo.ParameterFlags;
                invokeMethod.CallSite     = invokeMethodInfo.CallSite;
                invokeMethod.OwnerType    = invokeTypeInfo.Instance;
                invokeMethodInfo.Instance = invokeMethod;

                return(invokeMethod);
            }
            else
            {
                return(invokeTypeInfo.Methods[index].Instance);
            }
        }
        private void ObfuscateControlFlow(BuildMethod method)
        {
            if (!MethodBody.IsValid(method))
            {
                return;
            }

            var methodBody = MethodBody.Load(method);

            var ilBody = ILBody.Load(methodBody);

            // Call proxy
            if (_callProxyBuilder != null)
            {
                _callProxyBuilder.Build(ilBody);
            }

            // Function pointers
            if (_functionPointerBuilder != null)
            {
                _functionPointerBuilder.Build(ilBody);
            }

            // Obfuscate
            var obfuscator = new ControlFlowObfuscator(ilBody, method);

            obfuscator.Obfuscate();

            // Calculate max stack size
            ilBody.CalculateMaxStackSize(method);

            // Save
            methodBody = ilBody.Build();

            methodBody.Build(method);
        }
Example #27
0
        private static void DevirtualizeCalls(BuildMethod method)
        {
            if (!MethodBody.IsValid(method))
            {
                return;
            }

            var methodBody = MethodBody.Load(method);

            if (methodBody == null)
            {
                return;
            }

            bool changed      = false;
            var  instructions = methodBody.Instructions;

            for (int i = 0; i < instructions.Count; i++)
            {
                var instruction = instructions[i];

                var opCode = instruction.OpCode;
                if (opCode.Type == OpCodeType.Prefix)
                {
                    i++;
                    continue;
                }

                if (opCode != OpCodes.Callvirt)
                {
                    continue;
                }

                var calledMethodSig = instruction.Value as MethodSignature;
                if (calledMethodSig == null)
                {
                    continue;
                }

                var calledMethod = calledMethodSig.Resolve(method.Module, true);
                if (calledMethod == null)
                {
                    continue;
                }

                var calledDeclaringMethod = calledMethod.DeclaringMethod as BuildMethod;
                if (calledDeclaringMethod == null)
                {
                    continue;
                }

                if (!calledDeclaringMethod.DevirtualizeMethod)
                {
                    continue;
                }

                instructions[i] = new Instruction(OpCodes.Call, instruction.Value);
                changed         = true;
            }

            if (changed)
            {
                methodBody.Build(method);
            }
        }
Example #28
0
 public static void Devirtualize(BuildMethod method)
 {
     DevirtualizeMethod(method);
     DevirtualizeCalls(method);
 }
 public Builder(BuildMethod owner)
 {
     _owner           = owner;
     _module          = owner.Module;
     _assemblyManager = owner.AssemblyManager;
 }