pax_global_header00006660000000000000000000000064132445356430014523gustar00rootroot0000000000000052 comment=8d8608e42c478b3baf1f6d9f1a154f632de153d0 de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/000077500000000000000000000000001324453564300177135ustar00rootroot00000000000000de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/.editorconfig000066400000000000000000000103671324453564300223770ustar00rootroot00000000000000root = true [*] indent_size = 4 indent_style = tab insert_final_newline = true tab_width = 4 #end_of_line = [*.json] [App.config] [*.yml] indent_size = 2 indent_style = space [*.{proj,csproj,vbproj,props,targets,resx,vsixmanifest}] indent_size = 2 indent_style = space [app.manifest] indent_size = 2 indent_style = space [*.xml] [*.xaml] indent_style = space [*.{cs,vb}] dotnet_sort_system_directives_first = true dotnet_style_qualification_for_field = false:suggestion dotnet_style_qualification_for_property = false:suggestion dotnet_style_qualification_for_method = false:suggestion dotnet_style_qualification_for_event = false:suggestion dotnet_style_predefined_type_for_locals_parameters_members = true:none dotnet_style_predefined_type_for_member_access = true:none dotnet_style_object_initializer = true:suggestion dotnet_style_collection_initializer = true:suggestion dotnet_style_coalesce_expression = true:suggestion dotnet_style_null_propagation = true:suggestion dotnet_style_explicit_tuple_names = true:suggestion dotnet_separate_import_directive_groups = false dotnet_style_prefer_is_null_check_over_reference_equality_method = false:suggestion dotnet_style_require_accessibility_modifiers = never:info [*.cs] csharp_style_throw_expression = true:suggestion csharp_style_inlined_variable_declaration = true:suggestion csharp_style_var_for_built_in_types = false:none csharp_style_var_when_type_is_apparent = true:suggestion csharp_style_var_elsewhere = true:suggestion csharp_style_conditional_delegate_call = true:suggestion csharp_style_pattern_matching_over_as_with_null_check = true:suggestion csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion csharp_style_expression_bodied_constructors = true:suggestion csharp_style_expression_bodied_methods = true:suggestion csharp_style_expression_bodied_operators = true:suggestion csharp_style_expression_bodied_properties = true:suggestion csharp_style_expression_bodied_indexers = true:suggestion csharp_style_expression_bodied_accessors = true:suggestion csharp_prefer_braces = false csharp_space_between_method_declaration_name_and_open_parenthesis = false csharp_space_between_method_declaration_parameter_list_parentheses = false csharp_space_between_method_declaration_empty_parameter_list_parentheses = false csharp_space_between_method_call_name_and_opening_parenthesis = false csharp_space_between_method_call_parameter_list_parentheses = false csharp_space_between_method_call_empty_parameter_list_parentheses = false csharp_space_after_keywords_in_control_flow_statements = true csharp_space_between_parentheses = csharp_space_after_cast = false csharp_space_around_declaration_statements = false csharp_space_before_open_square_brackets = false csharp_space_between_empty_square_brackets = false csharp_space_between_square_brackets = false csharp_space_after_colon_in_inheritance_clause = true csharp_space_after_comma = true csharp_space_after_dot = false csharp_space_after_semicolon_in_for_statement = true csharp_space_before_colon_in_inheritance_clause = true csharp_space_before_comma = false csharp_space_before_dot = false csharp_space_before_semicolon_in_for_statement = false csharp_space_around_binary_operators = before_and_after csharp_indent_braces = false csharp_indent_block_contents = true csharp_indent_switch_labels = false csharp_indent_case_contents = true csharp_indent_labels = flush_left csharp_preserve_single_line_blocks = true csharp_preserve_single_line_statements = true csharp_new_line_before_open_brace = none csharp_new_line_before_else = true csharp_new_line_before_catch = true csharp_new_line_before_finally = true csharp_new_line_before_members_in_object_initializers = true csharp_new_line_before_members_in_anonymous_types = true csharp_new_line_between_query_expression_clauses = true csharp_indent_case_contents_when_block = false csharp_prefer_inferred_anonymous_type_member_names = true:suggestion csharp_prefer_inferred_tuple_names = true:suggestion csharp_prefer_simple_default_expression = true:suggestion #csharp_preferred_modifier_order = csharp_style_pattern_local_over_anonymous_function = true:suggestion [*.vb] visual_basic_prefer_inferred_anonymous_type_member_names = true:suggestion visual_basic_prefer_inferred_tuple_names = true:suggestion #visual_basic_preferred_modifier_order = de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/.gitignore000066400000000000000000000001321324453564300216770ustar00rootroot00000000000000*~ */obj/ *.csproj.user *.sdf *.opensdf *.suo /Debug/ /Release/ .vs/ *.tmp_proj de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/.gitmodules000066400000000000000000000001141324453564300220640ustar00rootroot00000000000000[submodule "dnlib"] path = dnlib url = https://github.com/0xd4d/dnlib.git de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/000077500000000000000000000000001324453564300222645ustar00rootroot00000000000000de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/AssemblyData.csproj000066400000000000000000000106121324453564300260570ustar00rootroot00000000000000 Debug AnyCPU 8.0.30703 2.0 {FBD84077-9D35-41FE-89DF-8D79EFE0B595} Library Properties AssemblyData AssemblyData v2.0 512 true ..\de4dot.snk true full false ..\Debug\bin\ DEBUG;TRACE prompt 4 AnyCPU pdbonly true ..\Release\bin\ TRACE prompt 4 true AnyCPU {045B96F2-AF80-4C4C-8D27-E38635AC705E} de4dot.blocks {5C93C5E2-196F-4877-BF65-96FEBFCEFCA1} de4dot.mdecrypt {FDFC1237-143F-4919-8318-4926901F4639} dnlib de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/AssemblyResolver.cs000066400000000000000000000077451324453564300261310ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Reflection; using System.Collections.Generic; using System.IO; using System.Xml; namespace AssemblyData { class AssemblyResolver { Dictionary assemblies = new Dictionary(StringComparer.Ordinal); Dictionary assemblySearchPathsDict = new Dictionary(StringComparer.OrdinalIgnoreCase); List assemblySearchPaths = new List(); public AssemblyResolver() { AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve; } void AddAssemblySearchPath(string path) { if (assemblySearchPathsDict.ContainsKey(path)) return; assemblySearchPathsDict[path] = true; assemblySearchPaths.Add(path); } Assembly Get(string assemblyFullName) { var asmName = new AssemblyName(assemblyFullName); Assembly assembly; if (assemblies.TryGetValue(asmName.FullName, out assembly)) return assembly; if (assemblies.TryGetValue(asmName.Name, out assembly)) return assembly; return null; } static string[] assemblyExtensions = new string[] { ".dll", ".exe" }; Assembly AssemblyResolve(object sender, ResolveEventArgs args) { var assembly = Get(args.Name); if (assembly != null) return assembly; var asmName = new AssemblyName(args.Name); foreach (var path in assemblySearchPaths) { foreach (var ext in assemblyExtensions) { try { var filename = Path.Combine(path, asmName.Name + ext); if (!new FileInfo(filename).Exists) continue; AddConfigFile(filename + ".config"); return AddAssembly(Assembly.LoadFile(filename)); } catch (IOException) { } catch (BadImageFormatException) { } catch (ArgumentException) { } catch (NotSupportedException) { } catch (UnauthorizedAccessException) { } catch (System.Security.SecurityException) { } } } return null; } public Assembly Load(string filename) { AddConfigFile(filename + ".config"); return AddAssembly(LoadFile(filename)); } Assembly LoadFile(string filename) { try { return Assembly.LoadFrom(filename); } catch (FileLoadException) { // Here if eg. strong name signature validation failed and possibly other errors return Assembly.Load(File.ReadAllBytes(filename)); } } Assembly AddAssembly(Assembly assembly) { var asmName = assembly.GetName(); assemblies[asmName.FullName] = assembly; assemblies[asmName.Name] = assembly; return assembly; } void AddConfigFile(string configFilename) { var dirName = Utils.GetDirName(Utils.GetFullPath(configFilename)); AddAssemblySearchPath(dirName); try { using (var xmlStream = new FileStream(configFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) { var doc = new XmlDocument(); doc.Load(XmlReader.Create(xmlStream)); foreach (var tmp in doc.GetElementsByTagName("probing")) { var probingElem = tmp as XmlElement; if (probingElem == null) continue; var privatePath = probingElem.GetAttribute("privatePath"); if (string.IsNullOrEmpty(privatePath)) continue; foreach (var path in privatePath.Split(';')) AddAssemblySearchPath(Path.Combine(dirName, path)); } } } catch (IOException) { } catch (XmlException) { } } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/AssemblyServer.cs000066400000000000000000000033501324453564300255620ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Collections; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Ipc; using AssemblyData; namespace AssemblyServer { public static class Start { public static int Main(string[] args) { if (args.Length != 3) Environment.Exit(1); var serviceType = (AssemblyServiceType)int.Parse(args[0]); var channelName = args[1]; var uri = args[2]; var service = (AssemblyService)AssemblyService.Create(serviceType); StartServer(service, channelName, uri); service.WaitExit(); return 0; } static void StartServer(AssemblyService service, string name, string uri) { var props = new Hashtable(); props["portName"] = name; var provider = new BinaryServerFormatterSinkProvider(); provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; var channel = new IpcServerChannel(props, provider); ChannelServices.RegisterChannel(channel, false); RemotingServices.Marshal(service, uri); } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/AssemblyService.cs000066400000000000000000000052031324453564300257130ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Reflection; using System.Threading; namespace AssemblyData { public abstract class AssemblyService : MarshalByRefObject, IAssemblyService { ManualResetEvent exitEvent = new ManualResetEvent(false); protected Assembly assembly = null; AssemblyResolver assemblyResolver = new AssemblyResolver(); public static AssemblyService Create(AssemblyServiceType serviceType) { switch (serviceType) { case AssemblyServiceType.StringDecrypter: return new StringDecrypterService(); case AssemblyServiceType.MethodDecrypter: return new MethodDecrypterService(); case AssemblyServiceType.Generic: return new GenericService(); default: throw new ArgumentException("Invalid assembly service type"); } } public static Type GetType(AssemblyServiceType serviceType) { switch (serviceType) { case AssemblyServiceType.StringDecrypter: return typeof(StringDecrypterService); case AssemblyServiceType.MethodDecrypter: return typeof(MethodDecrypterService); case AssemblyServiceType.Generic: return typeof(GenericService); default: throw new ArgumentException("Invalid assembly service type"); } } public void DoNothing() { } public virtual void Exit() { exitEvent.Set(); } public void WaitExit() { exitEvent.WaitOne(); } public override object InitializeLifetimeService() { return null; } protected void CheckAssembly() { if (assembly == null) throw new ApplicationException("LoadAssembly() hasn't been called yet."); } protected void LoadAssemblyInternal(string filename) { if (assembly != null) throw new ApplicationException("Only one assembly can be explicitly loaded"); try { assembly = assemblyResolver.Load(filename); } catch (BadImageFormatException ex) { throw new ApplicationException(string.Format("Could not load assembly {0}. Maybe it's 32-bit or 64-bit only?", filename), ex); } } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/DelegateStringDecrypter.cs000066400000000000000000000036531324453564300274050ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Reflection; using System.Reflection.Emit; using System.Collections.Generic; namespace AssemblyData { class DelegateStringDecrypter : IStringDecrypter { delegate string DecryptString(object[] args); List stringDecryptMethods = new List(); public int DefineStringDecrypter(MethodInfo method) { stringDecryptMethods.Add(BuildDynamicMethod(method)); return stringDecryptMethods.Count - 1; } public object[] DecryptStrings(int stringDecrypterMethod, object[] args, MethodBase caller) { if (stringDecrypterMethod > stringDecryptMethods.Count) throw new ApplicationException("Invalid string decrypter method"); var rv = new object[args.Length]; var stringDecrypter = stringDecryptMethods[stringDecrypterMethod]; for (int i = 0; i < args.Length; i++) rv[i] = stringDecrypter((object[])args[i]); return rv; } DecryptString BuildDynamicMethod(MethodInfo method) { var dm = new DynamicMethod("", typeof(string), new Type[] { typeof(object[]) }, typeof(DelegateStringDecrypter), true); Utils.AddCallStringDecrypterMethodInstructions(method, dm.GetILGenerator()); return (DecryptString)dm.CreateDelegate(typeof(DecryptString)); } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/EmuStringDecrypter.cs000066400000000000000000000037201324453564300264140ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Collections.Generic; using System.Reflection; using AssemblyData.methodsrewriter; namespace AssemblyData { class EmuStringDecrypter : IStringDecrypter { List decryptInfos = new List(); MethodsRewriter methodsRewriter = new MethodsRewriter(); class DecryptInfo { public MethodInfo method; public RewrittenMethod decryptString; public DecryptInfo(MethodInfo method) { this.method = method; } } public int DefineStringDecrypter(MethodInfo method) { decryptInfos.Add(new DecryptInfo(method)); return decryptInfos.Count - 1; } public object[] DecryptStrings(int stringDecrypterMethod, object[] args, MethodBase caller) { var decryptInfo = decryptInfos[stringDecrypterMethod]; if (decryptInfo.decryptString == null) decryptInfo.decryptString = CreateDecryptString(decryptInfo.method); methodsRewriter.SetCaller(decryptInfo.decryptString, caller); var result = new object[args.Length]; for (int i = 0; i < args.Length; i++) result[i] = decryptInfo.decryptString((object[])args[i]); return result; } RewrittenMethod CreateDecryptString(MethodInfo method) { methodsRewriter.CreateMethod(method); return methodsRewriter.CreateDelegate(method); } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/GenericService.cs000066400000000000000000000046341324453564300255170ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Reflection; namespace AssemblyData { class GenericService : AssemblyService, IGenericService { IUserGenericService userGenericService; public override void Exit() { if (userGenericService != null) userGenericService.Dispose(); userGenericService = null; base.Exit(); } public void LoadUserService(Type createServiceType, object createMethodArgs) { var createServiceMethod = GetCreateUserServiceMethod(createServiceType); userGenericService = createServiceMethod.Invoke(null, null) as IUserGenericService; if (userGenericService == null) throw new ApplicationException("create-service-method failed to create user service"); } MethodInfo GetCreateUserServiceMethod(Type createServiceType) { if (createServiceType == null) throw new ApplicationException(string.Format("Create-service-type is null")); foreach (var method in createServiceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) { if (method.GetCustomAttributes(typeof(CreateUserGenericServiceAttribute), false).Length > 0) return method; } throw new ApplicationException(string.Format("Failed to find create-service-method. Type token: Type: {0}", createServiceType)); } void CheckUserService() { if (userGenericService == null) throw new ApplicationException("LoadUserService() hasn't been called yet."); } public void LoadAssembly(string filename) { CheckUserService(); LoadAssemblyInternal(filename); userGenericService.AssemblyLoaded(assembly); } public object SendMessage(int msg, object[] args) { CheckUserService(); return userGenericService.HandleMessage(msg, args); } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/IAssemblyService.cs000066400000000000000000000016221324453564300260250ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ namespace AssemblyData { public enum AssemblyServiceType { StringDecrypter, MethodDecrypter, Generic, } public interface IAssemblyService { void DoNothing(); void Exit(); } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/IGenericService.cs000066400000000000000000000020271324453564300256220ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; namespace AssemblyData { public class CreateUserGenericServiceAttribute : Attribute { } public interface IGenericService : IAssemblyService { void LoadUserService(Type createServiceType, object createMethodArgs); void LoadAssembly(string filename); object SendMessage(int msg, object[] args); } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/IMethodDecrypterService.cs000066400000000000000000000020101324453564300273400ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using de4dot.blocks; using de4dot.mdecrypt; namespace AssemblyData { public interface IMethodDecrypterService : IAssemblyService { void InstallCompileMethod(DecryptMethodsInfo decryptMethodsInfo); void LoadObfuscator(string filename); bool CanDecryptMethods(); DumpedMethods DecryptMethods(); } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/IStringDecrypter.cs000066400000000000000000000016621324453564300260610ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System.Reflection; namespace AssemblyData { interface IStringDecrypter { int DefineStringDecrypter(MethodInfo method); object[] DecryptStrings(int stringDecrypterMethod, object[] args, MethodBase caller); } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/IStringDecrypterService.cs000066400000000000000000000021221324453564300273720ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ namespace AssemblyData { public enum StringDecrypterType { Delegate, Emulate, } public interface IStringDecrypterService : IAssemblyService { void LoadAssembly(string filename); void SetStringDecrypterType(StringDecrypterType type); int DefineStringDecrypter(int methodToken); object[] DecryptStrings(int stringDecrypterMethod, object[] args, int callerToken); } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/IUserGenericService.cs000066400000000000000000000016521324453564300264640ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Reflection; namespace AssemblyData { public interface IUserGenericService : IDisposable { void AssemblyLoaded(Assembly assembly); object HandleMessage(int msg, object[] args); } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/MethodDecrypterService.cs000066400000000000000000000033551324453564300272440ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using de4dot.blocks; using de4dot.mdecrypt; namespace AssemblyData { class MethodDecrypterService : AssemblyService, IMethodDecrypterService { bool installCompileMethodCalled = false; public void InstallCompileMethod(DecryptMethodsInfo decryptMethodsInfo) { if (installCompileMethodCalled) throw new ApplicationException("installCompileMethod() has already been called"); installCompileMethodCalled = true; DynamicMethodsDecrypter.Instance.DecryptMethodsInfo = decryptMethodsInfo; DynamicMethodsDecrypter.Instance.InstallCompileMethod(); } public void LoadObfuscator(string filename) { LoadAssemblyInternal(filename); DynamicMethodsDecrypter.Instance.Module = assembly.ManifestModule; DynamicMethodsDecrypter.Instance.LoadObfuscator(); } public bool CanDecryptMethods() { CheckAssembly(); return DynamicMethodsDecrypter.Instance.CanDecryptMethods(); } public DumpedMethods DecryptMethods() { CheckAssembly(); return DynamicMethodsDecrypter.Instance.DecryptMethods(); } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/Properties/000077500000000000000000000000001324453564300244205ustar00rootroot00000000000000de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/Properties/AssemblyInfo.cs000066400000000000000000000024151324453564300273440ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System.Reflection; using System.Runtime.InteropServices; [assembly: AssemblyTitle("AssemblyData")] [assembly: AssemblyDescription(".NET Assembly data for use by client and server")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("AssemblyData")] [assembly: AssemblyCopyright("Copyright (C) 2011-2015 de4dot@gmail.com")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: AssemblyVersion("3.1.41592.3405")] [assembly: AssemblyFileVersion("3.1.41592.3405")] de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/SimpleData.cs000066400000000000000000000033471324453564300246450ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Text; namespace AssemblyData { // This class will make sure no data in the string is destroyed by serialization [Serializable] class MyString { short[] data; public MyString() { } public MyString(string s) { if (s == null) data = null; else { data = new short[s.Length]; for (int i = 0; i < s.Length; i++) data[i] = (short)s[i]; } } public override string ToString() { if (data == null) return null; var sb = new StringBuilder(data.Length); foreach (var c in data) sb.Append((char)c); return sb.ToString(); } } public static class SimpleData { public static object[] Pack(object[] args) { for (int i = 0; i < args.Length; i++) { var s = args[i] as string; if (s != null) args[i] = new MyString(s); } return args; } public static object[] Unpack(object[] args) { for (int i = 0; i < args.Length; i++) { var s = args[i] as MyString; if (s != null) args[i] = s.ToString(); } return args; } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/StringDecrypterService.cs000066400000000000000000000055451324453564300272750ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Reflection; namespace AssemblyData { class StringDecrypterService : AssemblyService, IStringDecrypterService { IStringDecrypter stringDecrypter = null; void CheckStringDecrypter() { if (stringDecrypter == null) throw new ApplicationException("SetStringDecrypterType() hasn't been called yet."); } public void LoadAssembly(string filename) { LoadAssemblyInternal(filename); } public void SetStringDecrypterType(StringDecrypterType type) { if (stringDecrypter != null) throw new ApplicationException("StringDecrypterType already set"); switch (type) { case StringDecrypterType.Delegate: stringDecrypter = new DelegateStringDecrypter(); break; case StringDecrypterType.Emulate: stringDecrypter = new EmuStringDecrypter(); break; default: throw new ApplicationException(string.Format("Unknown StringDecrypterType {0}", type)); } } public int DefineStringDecrypter(int methodToken) { CheckStringDecrypter(); var methodInfo = FindMethod(methodToken); if (methodInfo == null) throw new ApplicationException(string.Format("Could not find method {0:X8}", methodToken)); if (methodInfo.ReturnType != typeof(string) && methodInfo.ReturnType != typeof(object)) throw new ApplicationException(string.Format("Method return type must be string or object: {0}", methodInfo)); return stringDecrypter.DefineStringDecrypter(methodInfo); } public object[] DecryptStrings(int stringDecrypterMethod, object[] args, int callerToken) { CheckStringDecrypter(); var caller = GetCaller(callerToken); foreach (var arg in args) SimpleData.Unpack((object[])arg); return SimpleData.Pack(stringDecrypter.DecryptStrings(stringDecrypterMethod, args, caller)); } MethodBase GetCaller(int callerToken) { try { return assembly.GetModules()[0].ResolveMethod(callerToken); } catch { return null; } } MethodInfo FindMethod(int methodToken) { CheckAssembly(); foreach (var module in assembly.GetModules()) { var method = module.ResolveMethod(methodToken) as MethodInfo; if (method != null) return method; } return null; } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/Utils.cs000066400000000000000000000300511324453564300237120ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.IO; using System.Reflection; using System.Reflection.Emit; using System.Text; namespace AssemblyData { internal delegate void Action(); internal delegate void Action(T1 arg1); internal delegate void Action(T1 arg1, T2 arg2); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, T17 arg17); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, T17 arg17, T18 arg18); internal delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, T17 arg17, T18 arg18, T19 arg19); internal delegate TResult Func(); internal delegate TResult Func(T1 arg1); internal delegate TResult Func(T1 arg1, T2 arg2); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, T17 arg17); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, T17 arg17, T18 arg18); internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, T17 arg17, T18 arg18, T19 arg19); static class Utils { static Random random = new Random(); public static uint GetRandomUint() { return (uint)(random.NextDouble() * uint.MaxValue); } public static Type GetDelegateType(Type returnType, Type[] args) { Type[] types; if (returnType == typeof(void)) { types = args; switch (types.Length) { case 0: return typeof(Action).MakeGenericType(types); case 1: return typeof(Action<>).MakeGenericType(types); case 2: return typeof(Action<,>).MakeGenericType(types); case 3: return typeof(Action<,,>).MakeGenericType(types); case 4: return typeof(Action<,,,>).MakeGenericType(types); case 5: return typeof(Action<,,,,>).MakeGenericType(types); case 6: return typeof(Action<,,,,,>).MakeGenericType(types); case 7: return typeof(Action<,,,,,,>).MakeGenericType(types); case 8: return typeof(Action<,,,,,,,>).MakeGenericType(types); case 9: return typeof(Action<,,,,,,,,>).MakeGenericType(types); case 10: return typeof(Action<,,,,,,,,,>).MakeGenericType(types); case 11: return typeof(Action<,,,,,,,,,,>).MakeGenericType(types); case 12: return typeof(Action<,,,,,,,,,,,>).MakeGenericType(types); case 13: return typeof(Action<,,,,,,,,,,,,>).MakeGenericType(types); case 14: return typeof(Action<,,,,,,,,,,,,,>).MakeGenericType(types); case 15: return typeof(Action<,,,,,,,,,,,,,,>).MakeGenericType(types); case 16: return typeof(Action<,,,,,,,,,,,,,,,>).MakeGenericType(types); case 17: return typeof(Action<,,,,,,,,,,,,,,,,>).MakeGenericType(types); case 18: return typeof(Action<,,,,,,,,,,,,,,,,,>).MakeGenericType(types); case 19: return typeof(Action<,,,,,,,,,,,,,,,,,,>).MakeGenericType(types); default: throw new ApplicationException(string.Format("Too many delegate type arguments: {0}", types.Length)); } } else { types = new Type[args.Length + 1]; Array.Copy(args, types, args.Length); types[types.Length - 1] = returnType; switch (types.Length) { case 1: return typeof(Func<>).MakeGenericType(types); case 2: return typeof(Func<,>).MakeGenericType(types); case 3: return typeof(Func<,,>).MakeGenericType(types); case 4: return typeof(Func<,,,>).MakeGenericType(types); case 5: return typeof(Func<,,,,>).MakeGenericType(types); case 6: return typeof(Func<,,,,,>).MakeGenericType(types); case 7: return typeof(Func<,,,,,,>).MakeGenericType(types); case 8: return typeof(Func<,,,,,,,>).MakeGenericType(types); case 9: return typeof(Func<,,,,,,,,>).MakeGenericType(types); case 10: return typeof(Func<,,,,,,,,,>).MakeGenericType(types); case 11: return typeof(Func<,,,,,,,,,,>).MakeGenericType(types); case 12: return typeof(Func<,,,,,,,,,,,>).MakeGenericType(types); case 13: return typeof(Func<,,,,,,,,,,,,>).MakeGenericType(types); case 14: return typeof(Func<,,,,,,,,,,,,,>).MakeGenericType(types); case 15: return typeof(Func<,,,,,,,,,,,,,,>).MakeGenericType(types); case 16: return typeof(Func<,,,,,,,,,,,,,,,>).MakeGenericType(types); case 17: return typeof(Func<,,,,,,,,,,,,,,,,>).MakeGenericType(types); case 18: return typeof(Func<,,,,,,,,,,,,,,,,,>).MakeGenericType(types); case 19: return typeof(Func<,,,,,,,,,,,,,,,,,,>).MakeGenericType(types); case 20: return typeof(Func<,,,,,,,,,,,,,,,,,,,>).MakeGenericType(types); default: throw new ApplicationException(string.Format("Too many delegate type arguments: {0}", types.Length)); } } } public static string RandomName(int min, int max) { int numChars = random.Next(min, max + 1); var sb = new StringBuilder(numChars); int numLower = 0; for (int i = 0; i < numChars; i++) { if (numLower == 0) sb.Append((char)((int)'A' + random.Next(26))); else sb.Append((char)((int)'a' + random.Next(26))); if (numLower == 0) { numLower = random.Next(1, 5); } else { numLower--; } } return sb.ToString(); } public static void AddCallStringDecrypterMethodInstructions(MethodInfo method, ILGenerator ilg) { var args = method.GetParameters(); for (int i = 0; i < args.Length; i++) { var arg = args[i].ParameterType; ilg.Emit(OpCodes.Ldarg_0); ilg.Emit(OpCodes.Ldc_I4, i); ilg.Emit(OpCodes.Ldelem_Ref); if (arg.IsValueType) ilg.Emit(OpCodes.Unbox_Any, arg); else ilg.Emit(OpCodes.Castclass, arg); } ilg.Emit(OpCodes.Call, method); ilg.Emit(OpCodes.Ret); } public static string GetFullPath(string path) { try { return Path.GetFullPath(path); } catch (Exception) { return path; } } public static string GetDirName(string name) { return Path.GetDirectoryName(name); } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/methodsrewriter/000077500000000000000000000000001324453564300255135ustar00rootroot00000000000000de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/methodsrewriter/AssemblyResolver.cs000066400000000000000000000101751324453564300313470ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Collections.Generic; using System.Reflection; using dnlib.DotNet; using de4dot.blocks; namespace AssemblyData.methodsrewriter { class MGenericParameter { } class AssemblyResolver { Dictionary> types = new Dictionary>(StringComparer.Ordinal); List globalMethods; List globalFields; Assembly assembly; public AssemblyResolver(string asmName) { assembly = Assembly.Load(new AssemblyName(asmName)); InitTypes(); } void InitTypes() { foreach (var type in assembly.GetTypes()) { string key = (type.Namespace ?? "") + "." + type.Name; List list; if (!types.TryGetValue(key, out list)) types[key] = list = new List(); list.Add(new TypeResolver(type)); } } TypeResolver GetTypeResolver(ITypeDefOrRef typeRef) { if (typeRef == null) return null; var scopeType = typeRef.ScopeType; var key = scopeType.Namespace + "." + scopeType.TypeName; List list; if (!types.TryGetValue(key, out list)) return null; if (scopeType is TypeDef) { foreach (var resolver in list) { if (resolver.type.MetadataToken == scopeType.MDToken.Raw) return resolver; } } foreach (var resolver in list) { if (ResolverUtils.CompareTypes(resolver.type, scopeType)) return resolver; } return null; } public FieldInfo Resolve(IField fieldRef) { var resolver = GetTypeResolver(fieldRef.DeclaringType); if (resolver != null) return resolver.Resolve(fieldRef); return ResolveGlobalField(fieldRef); } FieldInfo ResolveGlobalField(IField fieldRef) { InitGlobalFields(); foreach (var globalField in globalFields) { if (ResolverUtils.CompareFields(globalField, fieldRef)) return globalField; } return null; } void InitGlobalFields() { if (globalFields != null) return; globalFields = new List(); var flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance; foreach (var module in assembly.GetModules(true)) { foreach (var method in module.GetFields(flags)) globalFields.Add(method); } } public MethodBase Resolve(IMethod methodRef) { var resolver = GetTypeResolver(methodRef.DeclaringType); if (resolver != null) return resolver.Resolve(methodRef); return ResolveGlobalMethod(methodRef); } MethodBase ResolveGlobalMethod(IMethod methodRef) { InitGlobalMethods(); foreach (var globalMethod in globalMethods) { if (ResolverUtils.CompareMethods(globalMethod, methodRef)) return globalMethod; } return null; } void InitGlobalMethods() { if (globalMethods != null) return; globalMethods = new List(); var flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance; foreach (var module in assembly.GetModules(true)) { foreach (var method in module.GetMethods(flags)) globalMethods.Add(method); } } public Type Resolve(ITypeDefOrRef typeRef) { var resolver = GetTypeResolver(typeRef); if (resolver != null) return resolver.type; var ts = typeRef as TypeSpec; if (ts != null && ts.TypeSig is GenericSig) return typeof(MGenericParameter); return null; } public override string ToString() { return assembly.ToString(); } } } de4dot-8d8608e42c478b3baf1f6d9f1a154f632de153d0/AssemblyData/methodsrewriter/CodeGenerator.cs000066400000000000000000000245341324453564300305730ustar00rootroot00000000000000/* Copyright (C) 2011-2015 de4dot@gmail.com This file is part of de4dot. de4dot is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. de4dot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with de4dot. If not, see . */ using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Reflection.Emit; using dnlib.DotNet.Emit; using dnlib.DotNet; using de4dot.blocks; using OpCode = dnlib.DotNet.Emit.OpCode; using OpCodes = dnlib.DotNet.Emit.OpCodes; using OperandType = dnlib.DotNet.Emit.OperandType; using ROpCode = System.Reflection.Emit.OpCode; using ROpCodes = System.Reflection.Emit.OpCodes; namespace AssemblyData.methodsrewriter { class CodeGenerator { static Dictionary dnlibToReflection = new Dictionary(); static CodeGenerator() { var refDict = new Dictionary(0x100); foreach (var f in typeof(ROpCodes).GetFields(BindingFlags.Static | BindingFlags.Public)) { if (f.FieldType != typeof(ROpCode)) continue; var ropcode = (ROpCode)f.GetValue(null); refDict[ropcode.Value] = ropcode; } foreach (var f in typeof(OpCodes).GetFields(BindingFlags.Static | BindingFlags.Public)) { if (f.FieldType != typeof(OpCode)) continue; var opcode = (OpCode)f.GetValue(null); ROpCode ropcode; if (!refDict.TryGetValue(opcode.Value, out ropcode)) continue; dnlibToReflection[opcode] = ropcode; } } IMethodsRewriter methodsRewriter; string methodName; IList allInstructions; IList allExceptionHandlers; ILGenerator ilg; Type methodReturnType; Type[] methodParameters; Type delegateType; MMethod methodInfo; LocalBuilder tempObjLocal; LocalBuilder tempObjArrayLocal; int thisArgIndex; List locals; List