Example #1
0
        IEnumerable <FieldDef> GetPossibleFields(TypeDef type)
        {
            var typeToFields = new TypeDefDict <List <FieldDef> >();

            foreach (var field in type.Fields)
            {
                if (field.Attributes != FieldAttributes.Private)
                {
                    continue;
                }
                var fieldType = DotNetUtils.GetType(module, field.FieldSig.GetFieldType());
                if (fieldType == null)
                {
                    continue;
                }
                if (!CheckBaseType(fieldType))
                {
                    continue;
                }
                var list = typeToFields.Find(fieldType);
                if (list == null)
                {
                    typeToFields.Add(fieldType, list = new List <FieldDef>());
                }
                list.Add(field);
            }

            foreach (var list in typeToFields.GetValues())
            {
                if (list.Count == 1)
                {
                    yield return(list[0]);
                }
            }
        }
Example #2
0
 public bool Exists(IMethod method)
 {
     if (method == null)
     {
         return(false);
     }
     if (method.DeclaringType != null && types.Find(method.DeclaringType))
     {
         return(true);
     }
     return(methods.Find(method));
 }
Example #3
0
        FieldDef GetNewField(IField structField, IField oldFieldRef)
        {
            var fieldsDict = typeToFieldsDict.Find(structField.DeclaringType);

            if (fieldsDict == null)
            {
                throw new ApplicationException("Could not find structField declaringType");
            }
            var newField = fieldsDict.Find(oldFieldRef);

            if (newField == null)
            {
                throw new ApplicationException("Could not find new field");
            }
            return(newField);
        }
        public void Deobfuscate(Blocks blocks)
        {
            var instrsToRemove = new List <int>();

            foreach (var block in blocks.MethodBlocks.GetAllBlocks())
            {
                instrsToRemove.Clear();
                var instrs = block.Instructions;
                for (int i = 0; i < instrs.Count; i++)
                {
                    var           instr = instrs[i];
                    int           indexToRemove;
                    ITypeDefOrRef type;
                    Local         local = null;

                    if (instr.OpCode.Code == Code.Newobj)
                    {
                        if (i + 1 >= instrs.Count)
                        {
                            continue;
                        }
                        var ctor = instr.Operand as IMethod;
                        if (ctor == null || ctor.DeclaringType == null)
                        {
                            continue;
                        }
                        if (ctor.Name != ".ctor")
                        {
                            continue;
                        }

                        var next = instrs[i + 1];
                        if (!next.IsStloc() && !next.IsLeave() && next.OpCode.Code != Code.Pop)
                        {
                            continue;
                        }

                        indexToRemove = i;
                        type          = ctor.DeclaringType;
                        if (next.IsStloc())
                        {
                            local = Instr.GetLocalVar(blocks.Locals, next);
                        }
                    }
                    else if (instr.OpCode.Code == Code.Ldfld)
                    {
                        if (i == 0)
                        {
                            continue;
                        }
                        var ldloc = instrs[i - 1];
                        if (!ldloc.IsLdloc())
                        {
                            continue;
                        }

                        var field = instr.Operand as IField;
                        if (field == null || field.DeclaringType == null)
                        {
                            continue;
                        }

                        indexToRemove = i;
                        type          = field.DeclaringType;
                        local         = Instr.GetLocalVar(blocks.Locals, ldloc);
                    }
                    else
                    {
                        continue;
                    }

                    if (type == null)
                    {
                        continue;
                    }
                    var info = typeToInfo.Find(type);
                    if (info == null)
                    {
                        continue;
                    }

                    info.referenced = true;
                    instrsToRemove.Add(indexToRemove);
                    if (local != null)
                    {
                        local.Type = info.localType;
                    }
                }
                if (instrsToRemove.Count > 0)
                {
                    block.Remove(instrsToRemove);
                }
            }
        }
Example #5
0
		IEnumerable<FieldDef> GetPossibleFields(TypeDef type) {
			var typeToFields = new TypeDefDict<List<FieldDef>>();
			foreach (var field in type.Fields) {
				if (field.Attributes != FieldAttributes.Private)
					continue;
				var fieldType = DotNetUtils.GetType(module, field.FieldSig.GetFieldType());
				if (fieldType == null)
					continue;
				if (!CheckBaseType(fieldType))
					continue;
				var list = typeToFields.Find(fieldType);
				if (list == null)
					typeToFields.Add(fieldType, list = new List<FieldDef>());
				list.Add(field);
			}

			foreach (var list in typeToFields.GetValues()) {
				if (list.Count == 1)
					yield return list[0];
			}
		}
Example #6
0
 public MType GetType(IType typeRef)
 {
     return(typeRefToType.Find(typeRef));
 }
 public bool CheckCanInline(MethodDef method)
 {
     return(methodsTypes.Find(method.DeclaringType));
 }
Example #8
0
 public MType GetType(IType typeRef) => typeRefToType.Find(typeRef);