IMethod GetNewForArray(object token) { Mono.Cecil.ModuleDefinition module = null; string typename = null; if (token is Mono.Cecil.TypeDefinition) { Mono.Cecil.TypeDefinition _def = (token as Mono.Cecil.TypeDefinition); module = _def.Module; typename = _def.FullName; } else if (token is Mono.Cecil.TypeReference) { Mono.Cecil.TypeReference _ref = (token as Mono.Cecil.TypeReference); module = _ref.Module; typename = _ref.FullName; } else { throw new NotImplementedException(); } ICLRType _Itype = GetType(typename, module); typename += "[]"; //var _type = context.environment.GetType(typename, type.Module); var _type = GetType(typename, module); MethodParamList tlist = MethodParamList.MakeList_OneParam_Int(environment); var m = _type.GetMethod(".ctor", tlist); return(m); }
public ModuleMetadataInfo(Module module, Mono.Cecil.ModuleDefinition cecilModule) { this.Module = module; this.CecilModule = cecilModule; typeRefLoader = new CecilLoader(); typeRefLoader.SetCurrentModule(cecilModule); }
IMethod GetMethod(object token) { Mono.Cecil.ModuleDefinition module = null; string methodname = null; string typename = null; MethodParamList genlist = null; MethodParamList list = null; if (token is Mono.Cecil.MethodReference) { Mono.Cecil.MethodReference _ref = (token as Mono.Cecil.MethodReference); module = _ref.Module; methodname = _ref.Name; typename = _ref.DeclaringType.FullName; list = new MethodParamList(environment, _ref); if (_ref.IsGenericInstance) { Mono.Cecil.GenericInstanceMethod gmethod = _ref as Mono.Cecil.GenericInstanceMethod; genlist = new MethodParamList(environment, gmethod); } } else if (token is Mono.Cecil.MethodDefinition) { Mono.Cecil.MethodDefinition _def = token as Mono.Cecil.MethodDefinition; module = _def.Module; methodname = _def.Name; typename = _def.DeclaringType.FullName; list = new MethodParamList(environment, _def); if (_def.IsGenericInstance) { throw new NotImplementedException(); //Mono.Cecil.GenericInstanceMethod gmethod = _def as Mono.Cecil.GenericInstanceMethod; //genlist = new MethodParamList(environment, gmethod); } } else { throw new NotImplementedException(); } var typesys = GetType(typename, module); if (typesys == null) { throw new Exception("type can't find:" + typename); } IMethod _method = null; if (genlist != null) { _method = typesys.GetMethodT(methodname, genlist, list); } else { _method = typesys.GetMethod(methodname, list); } return(_method); }
public void LoadModule(System.IO.Stream dllStream, System.IO.Stream pdbStream) { this.module = Mono.Cecil.ModuleDefinition.ReadModule(dllStream); #if WITHPDB if (pdbStream != null) { var debugInfoLoader = new Mono.Cecil.Pdb.PdbReaderProvider(); module.ReadSymbols(debugInfoLoader.GetSymbolReader(module, pdbStream)); } #endif if (module.HasAssemblyReferences) { foreach (var ar in module.AssemblyReferences) { if (moduleref.Contains(ar.Name) == false) { moduleref.Add(ar.Name); } if (moduleref.Contains(ar.FullName) == false) { moduleref.Add(ar.FullName); } } } //mapModule[module.Name] = module; if (module.HasTypes) { foreach (var t in module.Types) { mapType[t.FullName] = new ILType(this, t); } } }
/// <summary> /// Takes a Flame method and converts it to a Cecil method reference. /// For this to work, <paramref name="field"/> cannot reference /// non-Cecil types or methods. /// </summary> /// <param name="field"> /// The method to convert to a method reference. /// </param> /// <returns> /// A method reference. /// </returns> public static Mono.Cecil.MethodReference ImportReference( this Mono.Cecil.ModuleDefinition module, IMethod method) { if (method is ClrMethodDefinition) { var def = ((ClrMethodDefinition)method).Definition; return(module == null ? def : module.ImportReference(def)); } else if (method is IndirectMethodSpecialization) { var specialization = (IndirectMethodSpecialization)method; return(CloneMethodWithDeclaringType( module.ImportReference(specialization.Declaration), module.ImportReference(specialization.ParentType))); } else if (method is DirectMethodSpecialization) { var specialization = (DirectMethodSpecialization)method; var genInst = new Mono.Cecil.GenericInstanceMethod( module.ImportReference(specialization.Declaration)); foreach (var item in specialization.GenericArguments) { genInst.GenericArguments.Add(module.ImportReference(item)); } return(genInst); } else { throw new NotSupportedException($"Cannot import ill-understood method '{method.FullName}'."); } }
public static Mono.Cecil.MethodDefinition ConvertToMonoCecilMethodDefinition(System.Reflection.MethodBase mi) { // Get assembly name which encloses code for kernel. String kernel_assembly_file_name = mi.DeclaringType.Assembly.Location; // Get directory containing the assembly. String full_path = Path.GetFullPath(kernel_assembly_file_name); full_path = Path.GetDirectoryName(full_path); String kernel_full_name = null; // Get full name of kernel, including normalization because they cannot be compared directly with Mono.Cecil names. if (mi as System.Reflection.MethodInfo != null) { System.Reflection.MethodInfo mik = mi as System.Reflection.MethodInfo; kernel_full_name = string.Format("{0} {1}.{2}({3})", mik.ReturnType.FullName, Campy.Utils.Utility.RemoveGenericParameters(mi.ReflectedType), mi.Name, string.Join(",", mi.GetParameters().Select(o => string.Format("{0}", o.ParameterType)).ToArray())); } else { kernel_full_name = string.Format("{0}.{1}({2})", Campy.Utils.Utility.RemoveGenericParameters(mi.ReflectedType), mi.Name, string.Join(",", mi.GetParameters().Select(o => string.Format("{0}", o.ParameterType)).ToArray())); } kernel_full_name = Campy.Utils.Utility.NormalizeSystemReflectionName(kernel_full_name); // Decompile entire module. Mono.Cecil.ModuleDefinition md = Mono.Cecil.ModuleDefinition.ReadModule(kernel_assembly_file_name); // Examine all types, and all methods of types in order to find the lambda in Mono.Cecil. List <Type> types = new List <Type>(); StackQueue <Mono.Cecil.TypeDefinition> type_definitions = new StackQueue <Mono.Cecil.TypeDefinition>(); StackQueue <Mono.Cecil.TypeDefinition> type_definitions_closure = new StackQueue <Mono.Cecil.TypeDefinition>(); foreach (Mono.Cecil.TypeDefinition td in md.Types) { type_definitions.Push(td); } while (type_definitions.Count > 0) { Mono.Cecil.TypeDefinition ty = type_definitions.Pop(); type_definitions_closure.Push(ty); foreach (Mono.Cecil.TypeDefinition ntd in ty.NestedTypes) { type_definitions.Push(ntd); } } foreach (Mono.Cecil.TypeDefinition td in type_definitions_closure) { foreach (Mono.Cecil.MethodDefinition md2 in td.Methods) { String md2_name = Campy.Utils.Utility.NormalizeMonoCecilName(md2.FullName); if (md2_name.Contains(kernel_full_name)) { return(md2); } } } return(null); }
public static EntityHandle DecompileType(ModuleDefinition module, string typeName) { // Searches for the class / type var typeDefinition = module.Types.FirstOrDefault(type => type.Name == typeName); // Creates EntityHandle of typeDefinition var handle = MetadataTokens.EntityHandle(typeDefinition.MetadataToken.ToInt32()); // Returns EntityHandle of Type. return(handle); }
public static Mono.Cecil.ModuleDefinition GetMonoCecilModuleDefinition(System.Delegate del) { System.Reflection.MethodInfo mi = del.Method; // Get assembly name which encloses code for kernel. String kernel_assembly_file_name = mi.DeclaringType.Assembly.Location; // Decompile entire module. Mono.Cecil.ModuleDefinition md = Mono.Cecil.ModuleDefinition.ReadModule(kernel_assembly_file_name); return(md); }
ICLRType GetType(string fullname, Mono.Cecil.ModuleDefinition module) { var type = environment.GetType(fullname, module); ICLRType_Sharp stype = type as ICLRType_Sharp; if (stype != null && stype.NeedCCtor) { //执行.cctor stype.InvokeCCtor(this); } return(type); }
protected MonoSymbolFile(string filename, Mono.Cecil.ModuleDefinition module) : this(filename) { // Check that the MDB file matches the module, if we have been // passed a module. if (module == null) { return; } CheckGuidMatch(module.Mvid, filename, module.FullyQualifiedName); }
public void LoadModule(System.IO.Stream dllStream, string dir, System.IO.Stream pdbStream) { Mono.Cecil.DefaultAssemblyResolver resolver = new Mono.Cecil.DefaultAssemblyResolver(); Mono.Cecil.ReaderParameters p = new Mono.Cecil.ReaderParameters(); p.AssemblyResolver = resolver; resolver.AddSearchDirectory(dir); this.module = Mono.Cecil.ModuleDefinition.ReadModule(dllStream, p); #if WITHPDB if (pdbStream != null) { var debugInfoLoader = new Mono.Cecil.Pdb.PdbReaderProvider(); module.ReadSymbols(debugInfoLoader.GetSymbolReader(module, pdbStream)); } #endif if (module.HasAssemblyReferences) { foreach (var ar in module.AssemblyReferences) { if (moduleref.Contains(ar.Name) == false) { moduleref.Add(ar.Name); } if (moduleref.Contains(ar.FullName) == false) { moduleref.Add(ar.FullName); } } } //mapModule[module.Name] = module; if (module.HasTypes) { foreach (var t in module.Types) { if (t.FullName.Contains(".My."))//vb 系统类不要 { continue; } mapType[t.FullName] = new ILType(this, t); if (t.HasNestedTypes) { foreach (var nt in t.NestedTypes) { mapType[nt.FullName] = new ILType(this, nt); } } } } }
public static EntityHandle DecompileField(ModuleDefinition module, string typeName, string fieldName) { // Searches for the class / type var typeDefinition = module.Types.FirstOrDefault(type => type.Name == typeName); // Searches for the field var fieldDefinition = typeDefinition.Fields.FirstOrDefault(field => field.Name == fieldName); // Creates EntityHandle of fieldDefinition var handle = MetadataTokens.EntityHandle(fieldDefinition.MetadataToken.ToInt32()); // Returns EntityHandle of Field. return(handle); }
IMethod GetNewForArray(object token) { IMethod __method = null; if (methodCache.TryGetValue(token.GetHashCode(), out __method)) { return(__method); } Mono.Cecil.ModuleDefinition module = null; string typename = null; if (token is Mono.Cecil.TypeDefinition) { Mono.Cecil.TypeDefinition _def = (token as Mono.Cecil.TypeDefinition); module = _def.Module; typename = _def.FullName; } else if (token is Mono.Cecil.TypeReference) { Mono.Cecil.TypeReference _ref = (token as Mono.Cecil.TypeReference); module = _ref.Module; typename = _ref.FullName; } else { throw new NotImplementedException(); } ICLRType _type = null; ICLRType _Itype = GetType(typename); if (_Itype is ICLRType_Sharp) { _type = environment.GetType(typeof(CLRSharp.CLRSharp_Instance[])); } else { typename += "[]"; //var _type = context.environment.GetType(typename, type.Module); _type = GetType(typename); } MethodParamList tlist = MethodParamList.const_OneParam_Int(environment); var m = _type.GetMethod(".ctor", tlist); methodCache[token.GetHashCode()] = m; return(m); }
public static EntityHandle DecompileMethod(ModuleDefinition module, string typeName, string methodName, params Type[] typeList) { // Searches for the class / type var typeDefinition = module.Types.FirstOrDefault(type => type.Name == typeName); // Searches for the method var methodDefinition = typeDefinition.Methods.FirstOrDefault(x => x.Name == methodName && typeList.SequenceEqual(x.Parameters.Select(para => Type.GetType(para.ParameterType.FullName)))); // Creates EntityHandle of methodDefinition var handle = MetadataTokens.EntityHandle(methodDefinition.MetadataToken.ToInt32()); // Returns EntityHandle of Method. return(handle); }
public void LoadModule(System.IO.Stream dllStream, System.IO.Stream pdbStream) { this.module = Mono.Cecil.ModuleDefinition.ReadModule(dllStream); if (pdbStream != null) { var debugInfoLoader = new Mono.Cecil.Pdb.PdbReaderProvider(); module.ReadSymbols(debugInfoLoader.GetSymbolReader(module, pdbStream)); } if (module.HasAssemblyReferences) { foreach (var ar in module.AssemblyReferences) { if (moduleref.Contains(ar.Name) == false) { moduleref.Add(ar.Name); } if (moduleref.Contains(ar.FullName) == false) { moduleref.Add(ar.FullName); } } } //mapModule[module.Name] = module; if (module.HasTypes) { foreach (var t in module.Types) { if (t.FullName.Contains(".My."))//vb skip the system class { continue; } mapType[t.FullName] = new ILType(this, t, logger); if (t.HasNestedTypes) { foreach (var nt in t.NestedTypes) { mapType[nt.FullName] = new ILType(this, nt, logger); } } } } }
public static Mono.Cecil.TypeDefinition ConvertToMonoCecilTypeDefinition(Type ty) { // Get assembly name which encloses code for kernel. String kernel_assembly_file_name = ty.Assembly.Location; // Get directory containing the assembly. String full_path = Path.GetFullPath(kernel_assembly_file_name); full_path = Path.GetDirectoryName(full_path); // Decompile entire module. Mono.Cecil.ModuleDefinition md = Mono.Cecil.ModuleDefinition.ReadModule(kernel_assembly_file_name); // Examine all types, and all methods of types in order to find the lambda in Mono.Cecil. List <Type> types = new List <Type>(); StackQueue <Mono.Cecil.TypeDefinition> type_definitions = new StackQueue <Mono.Cecil.TypeDefinition>(); StackQueue <Mono.Cecil.TypeDefinition> type_definitions_closure = new StackQueue <Mono.Cecil.TypeDefinition>(); foreach (Mono.Cecil.TypeDefinition td in md.Types) { type_definitions.Push(td); } while (type_definitions.Count > 0) { Mono.Cecil.TypeDefinition td = type_definitions.Pop(); if (Campy.Utils.Utility.IsSimilarType(ty, td)) { return(td); } type_definitions_closure.Push(td); foreach (Mono.Cecil.TypeDefinition ntd in td.NestedTypes) { type_definitions.Push(ntd); } } foreach (Mono.Cecil.TypeDefinition td in type_definitions_closure) { if (Campy.Utils.Utility.IsSimilarType(ty, td)) { return(td); } } return(null); }
public bool decrypt(PeImage peImage, Mono.Cecil.ModuleDefinition module, CliSecureRtType csRtType, ref DumpedMethods dumpedMethods) { this.peImage = peImage; this.csRtType = csRtType; this.module = module; switch (decrypt2(ref dumpedMethods)) { case DecryptResult.Decrypted: return(true); case DecryptResult.NotEncrypted: return(false); case DecryptResult.Error: Log.w("Using dynamic method decryption"); byte[] moduleCctorBytes = getModuleCctorBytes(csRtType); dumpedMethods = de4dot.code.deobfuscators.MethodsDecrypter.decrypt(module.FullyQualifiedName, moduleCctorBytes); return(true); default: throw new ApplicationException("Invalid DecryptResult"); } }
ICLRType GetType(object token) { Mono.Cecil.ModuleDefinition module = null; string typename = null; if (token is Mono.Cecil.TypeDefinition) { Mono.Cecil.TypeDefinition _def = (token as Mono.Cecil.TypeDefinition); module = _def.Module; typename = _def.FullName; } else if (token is Mono.Cecil.TypeReference) { Mono.Cecil.TypeReference _ref = (token as Mono.Cecil.TypeReference); module = _ref.Module; typename = _ref.FullName; } else { throw new NotImplementedException(); } return(GetType(typename, module)); }
/// <summary> /// Takes a Flame field and converts it to a Cecil field reference. /// For this to work, <paramref name="field"/> cannot reference /// non-Cecil types or methods. /// </summary> /// <param name="field"> /// The field to convert to a field reference. /// </param> /// <returns> /// A field reference. /// </returns> public static Mono.Cecil.FieldReference ImportReference( this Mono.Cecil.ModuleDefinition module, IField field) { if (field is ClrFieldDefinition) { var def = ((ClrFieldDefinition)field).Definition; return(module == null ? def : module.ImportReference(def)); } else if (field is IndirectFieldSpecialization) { var specialization = (IndirectFieldSpecialization)field; var declarationRef = module.ImportReference(specialization.Declaration); var typeRef = module.ImportReference(specialization.ParentType); return(new Mono.Cecil.FieldReference( declarationRef.Name, module.ImportReference(declarationRef.FieldType, typeRef), typeRef)); } else { throw new NotSupportedException($"Cannot import ill-understood field '{field.FullName}'."); } }
public Mono.Cecil.Cil.ISymbolWriter GetSymbolWriter(Mono.Cecil.ModuleDefinition module, string fileName) { throw null; }
public Mono.Cecil.Cil.ISymbolWriter GetSymbolWriter(Mono.Cecil.ModuleDefinition module, System.IO.Stream symbolStream) { throw null; }
protected override MemberRenamingServices.MemberRenamingData GetMemberRenamingData(Mono.Cecil.ModuleDefinition module, Languages.ILanguage language) { return(new WinRTRenamingService(language, this.renameInvalidMembers).GetMemberRenamingData(module)); }
public MethodsEnumerator(Mono.Cecil.ModuleDefinition module) { _module = module; }
public static IEnumerable <Mono.Cecil.MethodDefinition> GetMethods(Mono.Cecil.ModuleDefinition module) { return(new MethodsEnumerator(module)); }
public static IEnumerable <Mono.Cecil.TypeDefinition> GetTypes(Mono.Cecil.ModuleDefinition module) { return(new TypesEnumerator(module)); }
public TypesEnumerator(Mono.Cecil.ModuleDefinition module) { _module = module; }
public static MonoSymbolFile ReadSymbolFile(Mono.Cecil.ModuleDefinition module) { return(ReadSymbolFile(module, module.FullyQualifiedName)); }
public bool decrypt(PeImage peImage, Mono.Cecil.ModuleDefinition module, CliSecureRtType csRtType, ref DumpedMethods dumpedMethods) { this.peImage = peImage; this.csRtType = csRtType; this.module = module; switch (decrypt2(ref dumpedMethods)) { case DecryptResult.Decrypted: return true; case DecryptResult.NotEncrypted: return false; case DecryptResult.Error: Log.w("Using dynamic method decryption"); byte[] moduleCctorBytes = getModuleCctorBytes(csRtType); dumpedMethods = de4dot.code.deobfuscators.MethodsDecrypter.decrypt(module.FullyQualifiedName, moduleCctorBytes); return true; default: throw new ApplicationException("Invalid DecryptResult"); } }
protected MonoSymbolFile(string filename, Mono.Cecil.ModuleDefinition module) : this(filename) { CheckGuidMatch(module.Mvid, filename, module.FullyQualifiedName); }
public static MonoSymbolFile ReadSymbolFile(Mono.Cecil.ModuleDefinition module, string filename) { string name = filename + ".mdb"; return(new MonoSymbolFile(name, module)); }
//得到类型的时候应该得到模块内Type或者真实Type //一个统一的Type,然后根据具体情况调用两边 public ICLRType GetType(string fullname, Mono.Cecil.ModuleDefinition module) { ICLRType type = null; bool b = mapType.TryGetValue(fullname, out type); if (!b) { List <ICLRType> subTypes = new List <ICLRType>(); if (fullname.Contains("<>"))//匿名类型 { string[] subts = fullname.Split('/'); ICLRType ft = GetType(subts[0], module); for (int i = 1; i < subts.Length; i++) { ft = ft.GetNestType(this, subts[i]); } return(ft); } string fullnameT = fullname.Replace('/', '+'); if (fullnameT.Contains("<")) { string outname = ""; int depth = 0; int lastsplitpos = 0; for (int i = 0; i < fullname.Length; i++) { string checkname = null; if (fullname[i] == '/') { } else if (fullname[i] == '<') { if (i != 0) { depth++; } if (depth == 1)// { lastsplitpos = i; outname += "["; continue; } } else if (fullname[i] == '>') { if (depth == 1) { checkname = fullnameT.Substring(lastsplitpos + 1, i - lastsplitpos - 1); var subtype = GetType(checkname, module); subTypes.Add(subtype); if (subtype is ICLRType_Sharp) { subtype = GetType(typeof(CLRSharp_Instance)); } outname += "[" + subtype.FullNameWithAssembly + "]"; lastsplitpos = i; } //if(depth>0) depth--; if (depth == 0) { outname += "]"; continue; } else if (depth < 0) { depth = 0; } } else if (fullname[i] == ',') { if (depth == 1) { checkname = fullnameT.Substring(lastsplitpos + 1, i - lastsplitpos - 1); var subtype = GetType(checkname, module); subTypes.Add(subtype); if (subtype is ICLRType_Sharp) { subtype = GetType(typeof(CLRSharp_Instance)); } outname += "[" + subtype.FullNameWithAssembly + "],"; lastsplitpos = i; } } if (depth == 0) { outname += fullnameT[i]; } } fullnameT = outname; // fullnameT = fullnameT.Replace('<', '['); //fullnameT = fullnameT.Replace('>', ']'); } System.Type t = System.Type.GetType(fullnameT); if (t == null && module != null && module.HasAssemblyReferences) { foreach (var rm in module.AssemblyReferences) { t = System.Type.GetType(fullnameT + "," + rm.Name); if (t != null) { fullnameT = fullnameT + "," + rm.Name; break; } } } if (t != null) { type = new Type_Common_System(this, t, fullnameT, subTypes.ToArray()); } mapType[fullname] = type; } return(type); }