예제 #1
0
 public TypeInfo(MTypeDef typeDef, MemberInfos memberInfos)
     : base(typeDef)
 {
     this.Type = typeDef;
     this._memberInfos = memberInfos;
     OldNamespace = typeDef.TypeDef.Namespace.String;
 }
예제 #2
0
 public void AddInterface(MTypeDef ifaceDef, ITypeDefOrRef iface)
 {
     if (ifaceDef == null || iface == null)
     {
         return;
     }
     interfaces.Add(new TypeInfo(iface, ifaceDef));
 }
예제 #3
0
 public void AddBaseType(MTypeDef baseDef, ITypeDefOrRef baseRef)
 {
     if (baseDef == null || baseRef == null)
     {
         return;
     }
     baseType = new TypeInfo(baseRef, baseDef);
 }
예제 #4
0
		public MMethodDef(MethodDef methodDef, MTypeDef owner, int index)
			: base(methodDef, owner, index) {
			genericParams = MGenericParamDef.CreateGenericParamDefList(MethodDef.GenericParameters);
			visibleBaseIndex = methodDef.MethodSig != null && methodDef.MethodSig.HasThis ? 1 : 0;
			for (int i = 0; i < methodDef.Parameters.Count; i++) {
				var param = methodDef.Parameters[i];
				if (param.IsNormalMethodParameter)
					visibleParamCount++;
				paramDefs.Add(new MParamDef(param, i));
			}
			returnParamDef = new MParamDef(methodDef.Parameters.ReturnParameter, -1);
		}
예제 #5
0
 public MMethodDef(MethodDef methodDef, MTypeDef owner, int index)
     : base(methodDef, owner, index)
 {
     genericParams    = MGenericParamDef.CreateGenericParamDefList(MethodDef.GenericParameters);
     visibleBaseIndex = methodDef.MethodSig != null && methodDef.MethodSig.HasThis ? 1 : 0;
     for (int i = 0; i < methodDef.Parameters.Count; i++)
     {
         var param = methodDef.Parameters[i];
         if (param.IsNormalMethodParameter)
         {
             visibleParamCount++;
         }
         paramDefs.Add(new MParamDef(param, i));
     }
     returnParamDef = new MParamDef(methodDef.Parameters.ReturnParameter, -1);
 }
예제 #6
0
        public void FindAllMemberRefs(ref int typeIndex)
        {
            memberRefFinder = new MemberRefFinder();
            memberRefFinder.FindAll(ModuleDefMD);
            allMethods = new List <MethodDef>(memberRefFinder.MethodDefs.Keys);

            var allTypesList = new List <MTypeDef>();

            foreach (var type in memberRefFinder.TypeDefs.Keys)
            {
                var typeDef = new MTypeDef(type, this, typeIndex++);
                types.Add(typeDef);
                allTypesList.Add(typeDef);
                typeDef.AddMembers();
            }

            var allTypesCopy = new List <MTypeDef>(allTypesList);
            var typeToIndex  = new Dictionary <TypeDef, int>();

            for (int i = 0; i < allTypesList.Count; i++)
            {
                typeToIndex[allTypesList[i].TypeDef] = i;
            }
            foreach (var typeDef in allTypesList)
            {
                if (typeDef.TypeDef.NestedTypes == null)
                {
                    continue;
                }
                foreach (var nestedTypeDef2 in typeDef.TypeDef.NestedTypes)
                {
                    int index         = typeToIndex[nestedTypeDef2];
                    var nestedTypeDef = allTypesCopy[index];
                    allTypesCopy[index] = null;
                    if (nestedTypeDef == null)                          // Impossible
                    {
                        throw new ApplicationException("Nested type belongs to two or more types");
                    }
                    typeDef.Add(nestedTypeDef);
                    nestedTypeDef.NestingType = typeDef;
                }
            }
        }
예제 #7
0
		public bool Check(MTypeDef type) {
			if (results.ContainsKey(type))
				return results[type];

			bool val;
			if (classNames.ContainsKey(type.TypeDef.FullName))
				val = true;
			else if (type.baseType == null) {
				if (type.TypeDef.BaseType != null)
					val = classNames.ContainsKey(type.TypeDef.BaseType.FullName);
				else
					val = false;
			}
			else
				val = Check(type.baseType.typeDef);

			results[type] = val;
			return val;
		}
예제 #8
0
        static MPropertyDef FindProperty(MTypeDef typeDef, UTF8String name, TypeSig propType)
        {
            while (typeDef != null)
            {
                foreach (var propDef in typeDef.AllProperties)
                {
                    if (propDef.PropertyDef.Name != name)
                    {
                        continue;
                    }
                    if (new SigComparer().Equals(propDef.PropertyDef.PropertySig.GetRetType(), propType))
                    {
                        return(propDef);
                    }
                }

                if (typeDef.baseType == null)
                {
                    break;
                }
                typeDef = typeDef.baseType.typeDef;
            }
            return(null);
        }
예제 #9
0
        static MFieldDef FindField(MTypeDef typeDef, UTF8String name, TypeSig fieldType)
        {
            while (typeDef != null)
            {
                foreach (var fieldDef in typeDef.AllFields)
                {
                    if (fieldDef.FieldDef.Name != name)
                    {
                        continue;
                    }
                    if (new SigComparer().Equals(fieldDef.FieldDef.FieldSig.GetFieldType(), fieldType))
                    {
                        return(fieldDef);
                    }
                }

                if (typeDef.baseType == null)
                {
                    break;
                }
                typeDef = typeDef.baseType.typeDef;
            }
            return(null);
        }
예제 #10
0
		string FindWindowsFormsClassName(MTypeDef type) {
			foreach (var methodDef in type.AllMethods) {
				if (methodDef.MethodDef.Body == null)
					continue;
				if (methodDef.MethodDef.IsStatic || methodDef.MethodDef.IsVirtual)
					continue;
				var instructions = methodDef.MethodDef.Body.Instructions;
				for (int i = 2; i < instructions.Count; i++) {
					var call = instructions[i];
					if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt)
						continue;
					if (!IsWindowsFormsSetNameMethod(call.Operand as IMethod))
						continue;

					var ldstr = instructions[i - 1];
					if (ldstr.OpCode.Code != Code.Ldstr)
						continue;
					var className = ldstr.Operand as string;
					if (className == null)
						continue;

					if (instructions[i - 2].GetParameterIndex() != 0)
						continue;

					FindInitializeComponentMethod(type, methodDef);
					return className;
				}
			}
			return null;
		}
예제 #11
0
 public void add(MTypeDef t)
 {
     types.add(t);
 }
예제 #12
0
		void FixClsTypeNames(MTypeDef nesting, MTypeDef nested) {
			int nestingCount = nesting == null ? 0 : nesting.GenericParams.Count;
			int arity = nested.GenericParams.Count - nestingCount;
			var nestedInfo = memberInfos.Type(nested);
			if (nestedInfo.renamed && arity > 0)
				nestedInfo.newName += "`" + arity;
			foreach (var nestedType in nested.NestedTypes)
				FixClsTypeNames(nested, nestedType);
		}
예제 #13
0
		void Rename(MTypeDef type) {
			var typeDef = type.TypeDef;
			var info = memberInfos.Type(type);

			if (isVerbose)
				Logger.v("Type: {0} ({1:X8})", Utils.RemoveNewlines(typeDef.FullName), typeDef.MDToken.ToUInt32());
			Logger.Instance.Indent();

			renameGenericParams2(type.GenericParams);

			if (RenameTypes && info.GotNewName()) {
				var old = typeDef.Name;
				typeDef.Name = info.newName;
				if (isVerbose)
					Logger.v("Name: {0} => {1}", Utils.RemoveNewlines(old), Utils.RemoveNewlines(typeDef.Name));
			}

			if (RenameNamespaces && info.newNamespace != null) {
				var old = typeDef.Namespace;
				typeDef.Namespace = info.newNamespace;
				if (isVerbose)
					Logger.v("Namespace: {0} => {1}", Utils.RemoveNewlines(old), Utils.RemoveNewlines(typeDef.Namespace));
			}

			Logger.Instance.DeIndent();
		}
예제 #14
0
			void Merge(MTypeDef type) {
				if (visited.ContainsKey(type))
					return;
				visited[type] = true;

				TypeInfo info;
				if (!memberInfos.TryGetType(type, out info))
					return;

				if (type.baseType != null)
					Merge(type.baseType.typeDef);
				foreach (var ifaceInfo in type.interfaces)
					Merge(ifaceInfo.typeDef);

				if (type.baseType != null)
					Merge(info, type.baseType.typeDef);
				foreach (var ifaceInfo in type.interfaces)
					Merge(info, ifaceInfo.typeDef);
			}
예제 #15
0
			void Prepare(MTypeDef type) {
				if (prepareMethodCalled.ContainsKey(type))
					return;
				prepareMethodCalled[type] = true;

				foreach (var ifaceInfo in type.interfaces)
					Prepare(ifaceInfo.typeDef);
				if (type.baseType != null)
					Prepare(type.baseType.typeDef);

				TypeInfo info;
				if (memberInfos.TryGetType(type, out info))
					func(info);
			}
예제 #16
0
		public bool Check(MTypeDef type) {
			return Check(type, 0);
		}
예제 #17
0
		public void AddBaseType(MTypeDef baseDef, ITypeDefOrRef baseRef) {
			if (baseDef == null || baseRef == null)
				return;
			baseType = new TypeInfo(baseRef, baseDef);
		}
예제 #18
0
		public TypeInfo(TypeInfo other, GenericInstSig git) {
			this.typeRef = GenericArgsSubstitutor.Create(other.typeRef, git);
			this.typeDef = other.typeDef;
		}
예제 #19
0
		public bool TryGetType(MTypeDef t, out TypeInfo info) {
			return allTypeInfos.TryGetValue(t, out info);
		}
예제 #20
0
		public TypeInfo Type(MTypeDef t) {
			return allTypeInfos[t];
		}
예제 #21
0
		public bool IsWinFormsClass(MTypeDef type) {
			return checkWinFormsClass.Check(type);
		}
예제 #22
0
 public MEventDef(EventDef eventDef, MTypeDef owner, int index)
     : base(eventDef, owner, index)
 {
 }
예제 #23
0
파일: EventDef.cs 프로젝트: GodLesZ/de4dot
		public MEventDef(EventDef eventDef, MTypeDef owner, int index)
			: base(eventDef, owner, index) {
		}
예제 #24
0
		void FindInitializeComponentMethod(MTypeDef type, MMethodDef possibleInitMethod) {
			foreach (var methodDef in type.AllMethods) {
				if (methodDef.MethodDef.Name != ".ctor")
					continue;
				if (methodDef.MethodDef.Body == null)
					continue;
				foreach (var instr in methodDef.MethodDef.Body.Instructions) {
					if (instr.OpCode.Code != Code.Call && instr.OpCode.Code != Code.Callvirt)
						continue;
					if (!MethodEqualityComparer.CompareDeclaringTypes.Equals(possibleInitMethod.MethodDef, instr.Operand as IMethod))
						continue;

					memberInfos.Method(possibleInitMethod).suggestedName = "InitializeComponent";
					return;
				}
			}
		}
예제 #25
0
		public MFieldDef(FieldDef fieldDef, MTypeDef owner, int index)
			: base(fieldDef, owner, index) {
		}
예제 #26
0
		protected Ref(IMemberRef memberRef, MTypeDef owner, int index) {
			this.memberRef = memberRef;
			Owner = owner;
			Index = index;
		}
예제 #27
0
		MEventDef CreateEvent(MTypeDef ownerType, string name, TypeSig eventType) {
			if (string.IsNullOrEmpty(name) || eventType == null || eventType.ElementType == ElementType.Void)
				return null;
			var newEvent = ownerType.Module.ModuleDefMD.UpdateRowId(new EventDefUser(name, eventType.ToTypeDefOrRef(), 0));
			var eventDef = ownerType.FindAny(newEvent);
			if (eventDef != null)
				return eventDef;

			eventDef = ownerType.Create(newEvent);
			memberInfos.Add(eventDef);
			if (isVerbose)
				Logger.v("Restoring event: {0}", Utils.RemoveNewlines(newEvent));
			return eventDef;
		}
예제 #28
0
 public TypeInfo(ITypeDefOrRef typeRef, MTypeDef typeDef)
 {
     this.typeRef = typeRef;
     this.typeDef = typeDef;
 }
예제 #29
0
			void Visit(MTypeDef type) {
				if (visited.ContainsKey(type))
					return;
				visited[type] = true;

				if (type.baseType != null)
					Visit(type.baseType.typeDef);
				foreach (var ifaceInfo in type.interfaces)
					Visit(ifaceInfo.typeDef);

				TypeInfo info;
				if (!memberInfos.TryGetType(type, out info))
					return;

				foreach (var method in type.AllMethodsSorted) {
					MethodNameGroup group;
					if (!methodToGroup.TryGetValue(method, out group))
						continue;
					foreach (var m in group.Methods)
						methodToGroup.Remove(m);
					func(group);
				}
			}
예제 #30
0
		public void AddInterface(MTypeDef ifaceDef, ITypeDefOrRef iface) {
			if (ifaceDef == null || iface == null)
				return;
			interfaces.Add(new TypeInfo(iface, ifaceDef));
		}
예제 #31
0
			void Merge(TypeInfo info, MTypeDef other) {
				TypeInfo otherInfo;
				if (!memberInfos.TryGetType(other, out otherInfo))
					return;

				if ((flags & MergeStateFlags.Methods) != MergeStateFlags.None)
					info.variableNameState.MergeMethods(otherInfo.variableNameState);
				if ((flags & MergeStateFlags.Properties) != MergeStateFlags.None)
					info.variableNameState.MergeProperties(otherInfo.variableNameState);
				if ((flags & MergeStateFlags.Events) != MergeStateFlags.None)
					info.variableNameState.MergeEvents(otherInfo.variableNameState);
			}
예제 #32
0
 public void Add(MTypeDef t)
 {
     types.Add(t);
 }
예제 #33
0
		void RenameMembers(MTypeDef type) {
			var info = memberInfos.Type(type);

			if (isVerbose)
				Logger.v("Type: {0}", Utils.RemoveNewlines(info.type.TypeDef.FullName));
			Logger.Instance.Indent();

			RenameFields2(info);
			RenameProperties2(info);
			RenameEvents2(info);
			RenameMethods2(info);

			Logger.Instance.DeIndent();
		}
예제 #34
0
		public TypeInfo(ITypeDefOrRef typeRef, MTypeDef typeDef) {
			this.typeRef = typeRef;
			this.typeDef = typeDef;
		}
예제 #35
0
		MPropertyDef CreateProperty(MTypeDef ownerType, string name, TypeSig propType, MethodDef getter, MethodDef setter) {
			if (string.IsNullOrEmpty(name) || propType.ElementType == ElementType.Void)
				return null;
			var newSig = CreatePropertySig(getter, propType, true) ?? CreatePropertySig(setter, propType, false);
			if (newSig == null)
				return null;
			var newProp = ownerType.Module.ModuleDefMD.UpdateRowId(new PropertyDefUser(name, newSig, 0));
			newProp.GetMethod = getter;
			newProp.SetMethod = setter;
			var propDef = ownerType.FindAny(newProp);
			if (propDef != null)
				return propDef;

			propDef = ownerType.Create(newProp);
			memberInfos.Add(propDef);
			if (isVerbose)
				Logger.v("Restoring property: {0}", Utils.RemoveNewlines(newProp));
			return propDef;
		}
예제 #36
0
		public void Add(MTypeDef t) {
			types.Add(t);
		}
예제 #37
0
		public MPropertyDef(PropertyDef propertyDef, MTypeDef owner, int index)
			: base(propertyDef, owner, index) {
		}
예제 #38
0
 public MFieldDef(FieldDef fieldDef, MTypeDef owner, int index)
     : base(fieldDef, owner, index)
 {
 }
예제 #39
0
 public TypeInfo(TypeInfo other, GenericInstSig git)
 {
     this.typeRef = GenericArgsSubstitutor.Create(other.typeRef, git);
     this.typeDef = other.typeDef;
 }
예제 #40
0
 protected Ref(IMemberRef memberRef, MTypeDef owner, int index)
 {
     this.memberRef = memberRef;
     Owner          = owner;
     Index          = index;
 }
예제 #41
0
 public MPropertyDef(PropertyDef propertyDef, MTypeDef owner, int index)
     : base(propertyDef, owner, index)
 {
 }
예제 #42
0
		void MergeState(MTypeDef other) {
			if (other == null)
				return;
			TypeInfo otherInfo;
			if (!memberInfos.TryGetType(other, out otherInfo))
				return;
			variableNameState.Merge(otherInfo.variableNameState);
		}
예제 #43
0
        public MTypeDef ResolveOther(ITypeDefOrRef type)
        {
            if (type == null)
            {
                return(null);
            }
            type = type.ScopeType;
            if (type == null)
            {
                return(null);
            }

            MTypeDef typeDef;

            if (typeToTypeDefDict.TryGetValue(type, out typeDef))
            {
                return(typeDef);
            }

            var typeDef2 = deobfuscatorContext.ResolveType(type);

            if (typeDef2 == null)
            {
                typeToTypeDefDict.TryGetSimilarValue(type, out typeDef);
                typeToTypeDefDict[type] = typeDef;
                return(typeDef);
            }

            if (typeToTypeDefDict.TryGetValue(typeDef2, out typeDef))
            {
                typeToTypeDefDict[type] = typeDef;
                return(typeDef);
            }

            typeToTypeDefDict[type]     = null;         // In case of a circular reference
            typeToTypeDefDict[typeDef2] = null;

            typeDef = new MTypeDef(typeDef2, null, 0);
            typeDef.AddMembers();
            foreach (var iface in typeDef.TypeDef.Interfaces)
            {
                var ifaceDef = ResolveOther(iface.Interface);
                if (ifaceDef == null)
                {
                    continue;
                }
                typeDef.AddInterface(ifaceDef, iface.Interface);
            }
            var baseDef = ResolveOther(typeDef.TypeDef.BaseType);

            if (baseDef != null)
            {
                typeDef.AddBaseType(baseDef, typeDef.TypeDef.BaseType);
            }

            typeToTypeDefDict[type] = typeDef;
            if (type != typeDef2)
            {
                typeToTypeDefDict[typeDef2] = typeDef;
            }
            return(typeDef);
        }
예제 #44
0
 public void add(MTypeDef t)
 {
     types.add(t);
 }