Example #1
0
        void AnalyzeMethod(ConfuserContext context, INameService service, MethodDef method)
        {
            var dpRegInstrs        = new List <Tuple <bool, Instruction> >();
            var routedEvtRegInstrs = new List <Instruction>();

            for (int i = 0; i < method.Body.Instructions.Count; i++)
            {
                Instruction instr = method.Body.Instructions[i];
                if ((instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt))
                {
                    var regMethod = (IMethod)instr.Operand;

                    if (regMethod.DeclaringType.FullName == "System.Windows.DependencyProperty" &&
                        regMethod.Name.String.StartsWith("Register"))
                    {
                        dpRegInstrs.Add(Tuple.Create(regMethod.Name.String.StartsWith("RegisterAttached"), instr));
                    }
                    else if (regMethod.DeclaringType.FullName == "System.Windows.EventManager" &&
                             regMethod.Name.String == "RegisterRoutedEvent")
                    {
                        routedEvtRegInstrs.Add(instr);
                    }
                }
                else if (instr.OpCode.Code == Code.Newobj)
                {
                    var methodRef = (IMethod)instr.Operand;

                    if (methodRef.DeclaringType.FullName == "System.Windows.Data.PropertyGroupDescription" &&
                        methodRef.Name == ".ctor" && i - 1 >= 0 && method.Body.Instructions[i - 1].OpCode.Code == Code.Ldstr)
                    {
                        foreach (var property in analyzer.LookupProperty((string)method.Body.Instructions[i - 1].Operand))
                        {
                            service.SetCanRename(property, false);
                        }
                    }
                }
                else if (instr.OpCode == OpCodes.Ldstr)
                {
                    var operand = ((string)instr.Operand).ToUpperInvariant();
                    if (operand.EndsWith(".BAML") || operand.EndsWith(".XAML"))
                    {
                        var match     = UriPattern.Match(operand);
                        var refModule = method.Module;
                        if (match.Success)
                        {
                            var resourceAssemblyName = match.Groups[1].Success ? match.Groups[1].Value : string.Empty;
                            // Check if the expression contains a resource name (group 1)
                            // If it does, check if it is this assembly.
                            if (!string.IsNullOrWhiteSpace(resourceAssemblyName) &&
                                !resourceAssemblyName.Equals(method.Module.Assembly.Name.String, StringComparison.OrdinalIgnoreCase))
                            {
                                // Let's see if we can find this assembly.
                                refModule = context.Modules.FirstOrDefault(m =>
                                                                           resourceAssemblyName.Equals(m.Assembly.Name.String,
                                                                                                       StringComparison.OrdinalIgnoreCase));

                                if (refModule == null)
                                {
                                    // This resource points to an assembly that is not part of the obfuscation.
                                    // Leave it alone!
                                    return;
                                }
                            }
                            operand = match.Groups[2].Value;
                        }
                        else if (operand.Contains("/"))
                        {
                            context.Logger.WarnFormat("Fail to extract XAML name from '{0}'.", instr.Operand);
                        }

                        var reference = new BAMLStringReference(refModule, instr);
                        operand = WebUtility.UrlDecode(operand.TrimStart('/'));
                        var baml = operand.Substring(0, operand.Length - 5) + ".BAML";
                        var xaml = operand.Substring(0, operand.Length - 5) + ".XAML";
                        bamlRefs.AddListEntry(baml, reference);
                        bamlRefs.AddListEntry(xaml, reference);
                    }
                }
            }

            if (dpRegInstrs.Count == 0)
            {
                return;
            }

            var         traceSrv = context.Registry.GetService <ITraceService>();
            MethodTrace trace    = traceSrv.Trace(method);

            bool erred = false;

            foreach (var instrInfo in dpRegInstrs)
            {
                int[] args = trace.TraceArguments(instrInfo.Item2);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }
                Instruction ldstr = method.Body.Instructions[args[0]];
                if (ldstr.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }

                var     name     = (string)ldstr.Operand;
                TypeDef declType = method.DeclaringType;
                bool    found    = false;
                if (instrInfo.Item1)                 // Attached DP
                {
                    MethodDef accessor;
                    if ((accessor = declType.FindMethod("Get" + name)) != null && accessor.IsStatic)
                    {
                        service.SetCanRename(accessor, false);
                        found = true;
                    }
                    if ((accessor = declType.FindMethod("Set" + name)) != null && accessor.IsStatic)
                    {
                        service.SetCanRename(accessor, false);
                        found = true;
                    }
                }

                // Normal DP
                // Find CLR property for attached DP as well, because it seems attached DP can be use as normal DP as well.
                PropertyDef property = null;
                if ((property = declType.FindProperty(name)) != null)
                {
                    service.SetCanRename(property, false);

                    found = true;
                    if (property.GetMethod != null)
                    {
                        service.SetCanRename(property.GetMethod, false);
                    }

                    if (property.SetMethod != null)
                    {
                        service.SetCanRename(property.SetMethod, false);
                    }

                    if (property.HasOtherMethods)
                    {
                        foreach (MethodDef accessor in property.OtherMethods)
                        {
                            service.SetCanRename(accessor, false);
                        }
                    }
                }
                if (!found)
                {
                    if (instrInfo.Item1)
                    {
                        context.Logger.WarnFormat("Failed to find the accessors of attached dependency property '{0}' in type '{1}'.",
                                                  name, declType.FullName);
                    }
                    else
                    {
                        context.Logger.WarnFormat("Failed to find the CLR property of normal dependency property '{0}' in type '{1}'.",
                                                  name, declType.FullName);
                    }
                }
            }

            erred = false;
            foreach (Instruction instr in routedEvtRegInstrs)
            {
                int[] args = trace.TraceArguments(instr);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }
                Instruction ldstr = method.Body.Instructions[args[0]];
                if (ldstr.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }

                var     name     = (string)ldstr.Operand;
                TypeDef declType = method.DeclaringType;

                EventDef eventDef = null;
                if ((eventDef = declType.FindEvent(name)) == null)
                {
                    context.Logger.WarnFormat("Failed to find the CLR event of routed event '{0}' in type '{1}'.",
                                              name, declType.FullName);
                    continue;
                }
                service.SetCanRename(eventDef, false);

                if (eventDef.AddMethod != null)
                {
                    service.SetCanRename(eventDef.AddMethod, false);
                }

                if (eventDef.RemoveMethod != null)
                {
                    service.SetCanRename(eventDef.RemoveMethod, false);
                }

                if (eventDef.InvokeMethod != null)
                {
                    service.SetCanRename(eventDef.InvokeMethod, false);
                }

                if (eventDef.HasOtherMethods)
                {
                    foreach (MethodDef accessor in eventDef.OtherMethods)
                    {
                        service.SetCanRename(accessor, false);
                    }
                }
            }
        }
        private void AnalyzeMethod(ConfuserContext context, INameService service, MethodDef method)
        {
            var dpRegInstrs        = new List <Tuple <bool, Instruction> >();
            var routedEvtRegInstrs = new List <Instruction>();

            for (var i = 0; i < method.Body.Instructions.Count; i++)
            {
                var instr = method.Body.Instructions[i];
                if (instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt)
                {
                    var regMethod = (IMethod)instr.Operand;

                    if (regMethod.DeclaringType.FullName == "System.Windows.DependencyProperty" &&
                        regMethod.Name.String.StartsWith("Register"))
                    {
                        dpRegInstrs.Add(Tuple.Create(regMethod.Name.String.StartsWith("RegisterAttached"), instr));
                    }
                    else if (regMethod.DeclaringType.FullName == "System.Windows.EventManager" &&
                             regMethod.Name.String == "RegisterRoutedEvent")
                    {
                        routedEvtRegInstrs.Add(instr);
                    }
                }
                else if (instr.OpCode.Code == Code.Newobj)
                {
                    var methodRef = (IMethod)instr.Operand;

                    if (methodRef.DeclaringType.FullName == "System.Windows.Data.PropertyGroupDescription" &&
                        methodRef.Name == ".ctor" && i - 1 >= 0 && method.Body.Instructions[i - 1].OpCode.Code == Code.Ldstr)
                    {
                        foreach (var property in analyzer.LookupProperty((string)method.Body.Instructions[i - 1].Operand))
                        {
                            service.SetCanRename(property, false);
                        }
                    }
                }
                else if (instr.OpCode == OpCodes.Ldstr)
                {
                    var operand = ((string)instr.Operand).ToUpperInvariant();
                    if (operand.EndsWith(".BAML") || operand.EndsWith(".XAML"))
                    {
                        var match = UriPattern.Match(operand);
                        if (match.Success)
                        {
                            operand = match.Groups[1].Value;
                        }
                        else if (operand.Contains("/"))
                        {
                            context.Logger.WarnFormat("Fail to extract XAML name from '{0}'.", instr.Operand);
                        }

                        var reference = new BAMLStringReference(instr);
                        operand = operand.TrimStart('/');
                        var baml = operand.Substring(0, operand.Length - 5) + ".BAML";
                        var xaml = operand.Substring(0, operand.Length - 5) + ".XAML";
                        bamlRefs.AddListEntry(baml, reference);
                        bamlRefs.AddListEntry(xaml, reference);
                    }
                }
            }

            if (dpRegInstrs.Count == 0)
            {
                return;
            }

            var traceSrv = context.Registry.GetService <ITraceService>();
            var trace    = traceSrv.Trace(method);

            var erred = false;

            foreach (var instrInfo in dpRegInstrs)
            {
                var args = trace.TraceArguments(instrInfo.Item2);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }
                var ldstr = method.Body.Instructions[args[0]];
                if (ldstr.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }

                var name     = (string)ldstr.Operand;
                var declType = method.DeclaringType;
                var found    = false;
                if (instrInfo.Item1) // Attached DP
                {
                    MethodDef accessor;
                    if ((accessor = declType.FindMethod("Get" + name)) != null && accessor.IsStatic)
                    {
                        service.SetCanRename(accessor, false);
                        found = true;
                    }
                    if ((accessor = declType.FindMethod("Set" + name)) != null && accessor.IsStatic)
                    {
                        service.SetCanRename(accessor, false);
                        found = true;
                    }
                }

                // Normal DP
                // Find CLR property for attached DP as well, because it seems attached DP can be use as normal DP as well.
                PropertyDef property = null;
                if ((property = declType.FindProperty(name)) != null)
                {
                    service.SetCanRename(property, false);

                    found = true;
                    if (property.GetMethod != null)
                    {
                        service.SetCanRename(property.GetMethod, false);
                    }

                    if (property.SetMethod != null)
                    {
                        service.SetCanRename(property.SetMethod, false);
                    }

                    if (property.HasOtherMethods)
                    {
                        foreach (var accessor in property.OtherMethods)
                        {
                            service.SetCanRename(accessor, false);
                        }
                    }
                }
                if (!found)
                {
                    if (instrInfo.Item1)
                    {
                        context.Logger.WarnFormat("Failed to find the accessors of attached dependency property '{0}' in type '{1}'.",
                                                  name, declType.FullName);
                    }
                    else
                    {
                        context.Logger.WarnFormat("Failed to find the CLR property of normal dependency property '{0}' in type '{1}'.",
                                                  name, declType.FullName);
                    }
                }
            }

            erred = false;
            foreach (var instr in routedEvtRegInstrs)
            {
                var args = trace.TraceArguments(instr);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }
                var ldstr = method.Body.Instructions[args[0]];
                if (ldstr.OpCode.Code != Code.Ldstr)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", method.FullName);
                    }
                    erred = true;
                    continue;
                }

                var name     = (string)ldstr.Operand;
                var declType = method.DeclaringType;

                EventDef eventDef = null;
                if ((eventDef = declType.FindEvent(name)) == null)
                {
                    context.Logger.WarnFormat("Failed to find the CLR event of routed event '{0}' in type '{1}'.",
                                              name, declType.FullName);
                    continue;
                }
                service.SetCanRename(eventDef, false);

                if (eventDef.AddMethod != null)
                {
                    service.SetCanRename(eventDef.AddMethod, false);
                }

                if (eventDef.RemoveMethod != null)
                {
                    service.SetCanRename(eventDef.RemoveMethod, false);
                }

                if (eventDef.InvokeMethod != null)
                {
                    service.SetCanRename(eventDef.InvokeMethod, false);
                }

                if (eventDef.HasOtherMethods)
                {
                    foreach (var accessor in eventDef.OtherMethods)
                    {
                        service.SetCanRename(accessor, false);
                    }
                }
            }
        }
        // Token: 0x06000044 RID: 68 RVA: 0x00004ED4 File Offset: 0x000030D4
        private void AnalyzeMethod(ConfuserContext context, INameService service, MethodDef method)
        {
            List <Tuple <bool, Instruction> > dpRegInstrs = new List <Tuple <bool, Instruction> >();
            List <Instruction> routedEvtRegInstrs         = new List <Instruction>();

            foreach (Instruction instr in method.Body.Instructions)
            {
                if (instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt)
                {
                    IMethod regMethod = (IMethod)instr.Operand;
                    if (regMethod.DeclaringType.FullName == "System.Windows.DependencyProperty" && regMethod.Name.String.StartsWith("Register"))
                    {
                        dpRegInstrs.Add(Tuple.Create <bool, Instruction>(regMethod.Name.String.StartsWith("RegisterAttached"), instr));
                    }
                    else if (regMethod.DeclaringType.FullName == "System.Windows.EventManager" && regMethod.Name.String == "RegisterRoutedEvent")
                    {
                        routedEvtRegInstrs.Add(instr);
                    }
                }
                else if (instr.OpCode == OpCodes.Ldstr)
                {
                    string operand = ((string)instr.Operand).ToUpperInvariant();
                    if (operand.EndsWith(".BAML") || operand.EndsWith(".XAML"))
                    {
                        Match match = WPFAnalyzer.UriPattern.Match(operand);
                        if (match.Success)
                        {
                            operand = match.Groups[1].Value;
                        }
                        BAMLStringReference reference = new BAMLStringReference(instr);
                        operand = operand.TrimStart(new char[]
                        {
                            '/'
                        });
                        string baml = operand.Substring(0, operand.Length - 5) + ".BAML";
                        string xaml = operand.Substring(0, operand.Length - 5) + ".XAML";
                        this.bamlRefs.AddListEntry(baml, reference);
                        this.bamlRefs.AddListEntry(xaml, reference);
                    }
                }
            }
            if (dpRegInstrs.Count == 0)
            {
                return;
            }
            ITraceService traceSrv = context.Registry.GetService <ITraceService>();
            MethodTrace   trace    = traceSrv.Trace(method);
            bool          erred    = false;

            foreach (Tuple <bool, Instruction> instrInfo in dpRegInstrs)
            {
                int[] args = trace.TraceArguments(instrInfo.Item2);
                if (args == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", new object[]
                        {
                            method.FullName
                        });
                    }
                    erred = true;
                }
                else
                {
                    Instruction ldstr = method.Body.Instructions[args[0]];
                    if (ldstr.OpCode.Code != Code.Ldstr)
                    {
                        if (!erred)
                        {
                            context.Logger.WarnFormat("Failed to extract dependency property name in '{0}'.", new object[]
                            {
                                method.FullName
                            });
                        }
                        erred = true;
                    }
                    else
                    {
                        string  name     = (string)ldstr.Operand;
                        TypeDef declType = method.DeclaringType;
                        bool    found    = false;
                        if (instrInfo.Item1)
                        {
                            MethodDef accessor;
                            if ((accessor = declType.FindMethod("Get" + name)) != null && accessor.IsStatic)
                            {
                                service.SetCanRename(accessor, false);
                                found = true;
                            }
                            if ((accessor = declType.FindMethod("Set" + name)) != null && accessor.IsStatic)
                            {
                                service.SetCanRename(accessor, false);
                                found = true;
                            }
                        }
                        PropertyDef property;
                        if ((property = declType.FindProperty(name)) != null)
                        {
                            service.SetCanRename(property, false);
                            found = true;
                            if (property.GetMethod != null)
                            {
                                service.SetCanRename(property.GetMethod, false);
                            }
                            if (property.SetMethod != null)
                            {
                                service.SetCanRename(property.SetMethod, false);
                            }
                            if (property.HasOtherMethods)
                            {
                                foreach (MethodDef accessor2 in property.OtherMethods)
                                {
                                    service.SetCanRename(accessor2, false);
                                }
                            }
                        }
                        if (!found)
                        {
                            if (instrInfo.Item1)
                            {
                                context.Logger.WarnFormat("Failed to find the accessors of attached dependency property '{0}' in type '{1}'.", new object[]
                                {
                                    name,
                                    declType.FullName
                                });
                            }
                            else
                            {
                                context.Logger.WarnFormat("Failed to find the CLR property of normal dependency property '{0}' in type '{1}'.", new object[]
                                {
                                    name,
                                    declType.FullName
                                });
                            }
                        }
                    }
                }
            }
            erred = false;
            foreach (Instruction instr2 in routedEvtRegInstrs)
            {
                int[] args2 = trace.TraceArguments(instr2);
                if (args2 == null)
                {
                    if (!erred)
                    {
                        context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", new object[]
                        {
                            method.FullName
                        });
                    }
                    erred = true;
                }
                else
                {
                    Instruction ldstr2 = method.Body.Instructions[args2[0]];
                    if (ldstr2.OpCode.Code != Code.Ldstr)
                    {
                        if (!erred)
                        {
                            context.Logger.WarnFormat("Failed to extract routed event name in '{0}'.", new object[]
                            {
                                method.FullName
                            });
                        }
                        erred = true;
                    }
                    else
                    {
                        string   name2     = (string)ldstr2.Operand;
                        TypeDef  declType2 = method.DeclaringType;
                        EventDef eventDef;
                        if ((eventDef = declType2.FindEvent(name2)) == null)
                        {
                            context.Logger.WarnFormat("Failed to find the CLR event of routed event '{0}' in type '{1}'.", new object[]
                            {
                                name2,
                                declType2.FullName
                            });
                        }
                        else
                        {
                            service.SetCanRename(eventDef, false);
                            if (eventDef.AddMethod != null)
                            {
                                service.SetCanRename(eventDef.AddMethod, false);
                            }
                            if (eventDef.RemoveMethod != null)
                            {
                                service.SetCanRename(eventDef.RemoveMethod, false);
                            }
                            if (eventDef.InvokeMethod != null)
                            {
                                service.SetCanRename(eventDef.InvokeMethod, false);
                            }
                            if (eventDef.HasOtherMethods)
                            {
                                foreach (MethodDef accessor3 in eventDef.OtherMethods)
                                {
                                    service.SetCanRename(accessor3, false);
                                }
                            }
                        }
                    }
                }
            }
        }