支持重命名Module Name
parent
907f6a9dfa
commit
177896026e
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
public static class FileUtil
|
||||
{
|
||||
public static void RecreateDir(string dir)
|
||||
{
|
||||
if (Directory.Exists(dir))
|
||||
{
|
||||
Directory.Delete(dir, true);
|
||||
}
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
public static class MetaUtil
|
||||
{
|
||||
public static string GetModuleNameWithoutExt(string moduleName)
|
||||
{
|
||||
return Path.GetFileNameWithoutExtension(moduleName);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,12 @@
|
|||
using dnlib.DotNet;
|
||||
using Obfuz.Rename;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Obfuz
|
||||
{
|
||||
|
@ -14,7 +17,7 @@ namespace Obfuz
|
|||
public class Options
|
||||
{
|
||||
public List<string> AssemblySearchDirs;
|
||||
public List<string> ObfusAssemblyNames;
|
||||
public List<string> ObfuscatedAssemblyNames;
|
||||
public string outputDir;
|
||||
}
|
||||
|
||||
|
@ -23,21 +26,27 @@ namespace Obfuz
|
|||
|
||||
private readonly List<ObfuzAssemblyInfo> _obfuzAssemblies = new List<ObfuzAssemblyInfo>();
|
||||
|
||||
private readonly IRenamePolicy _renamePolicy;
|
||||
private readonly INameMaker _nameMaker;
|
||||
|
||||
public Obfuscator(Options options)
|
||||
{
|
||||
_options = options;
|
||||
_assemblyCache = new AssemblyCache(new PathAssemblyResolver(options.AssemblySearchDirs.ToArray()));
|
||||
_renamePolicy = new RenamePolicy();
|
||||
_nameMaker = new NameMaker();
|
||||
}
|
||||
|
||||
public void DoIt()
|
||||
{
|
||||
LoadAssemblies();
|
||||
Rename();
|
||||
Save();
|
||||
}
|
||||
|
||||
private void LoadAssemblies()
|
||||
{
|
||||
foreach (string assName in _options.ObfusAssemblyNames)
|
||||
foreach (string assName in _options.ObfuscatedAssemblyNames)
|
||||
{
|
||||
ModuleDefMD mod = _assemblyCache.LoadModule(assName);
|
||||
var obfuzAsm = new ObfuzAssemblyInfo
|
||||
|
@ -46,6 +55,7 @@ namespace Obfuz
|
|||
module = mod,
|
||||
referenceMeAssemblies = new List<ObfuzAssemblyInfo>(),
|
||||
};
|
||||
obfuzAsm.referenceMeAssemblies.Add(obfuzAsm);
|
||||
_obfuzAssemblies.Add(obfuzAsm);
|
||||
}
|
||||
|
||||
|
@ -69,9 +79,23 @@ namespace Obfuz
|
|||
var ctx = new ObfuscatorContext
|
||||
{
|
||||
assemblies = _obfuzAssemblies,
|
||||
renamePolicy = _renamePolicy,
|
||||
nameMaker = _nameMaker,
|
||||
};
|
||||
var sr = new SymbolRename(ctx);
|
||||
sr.Process();
|
||||
}
|
||||
|
||||
private void Save()
|
||||
{
|
||||
string outputDir = _options.outputDir;
|
||||
FileUtil.RecreateDir(outputDir);
|
||||
foreach (var ass in _obfuzAssemblies)
|
||||
{
|
||||
string outputFile = $"{outputDir}/{ass.module.Name}";
|
||||
ass.module.Write(outputFile);
|
||||
Debug.Log($"save module. oldName:{ass.name} newName:{ass.module.Name} output:{outputFile}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using dnlib.DotNet;
|
||||
using Obfuz.Rename;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
@ -19,5 +20,9 @@ namespace Obfuz
|
|||
public class ObfuscatorContext
|
||||
{
|
||||
public List<ObfuzAssemblyInfo> assemblies;
|
||||
|
||||
public IRenamePolicy renamePolicy;
|
||||
|
||||
public INameMaker nameMaker;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ namespace Obfuz
|
|||
string assPath = Path.Combine(path, assemblyName + ".dll");
|
||||
if (File.Exists(assPath))
|
||||
{
|
||||
Debug.Log($"resolve {assemblyName} at {assPath}");
|
||||
//Debug.Log($"resolve {assemblyName} at {assPath}");
|
||||
return assPath;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
using dnlib.DotNet;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Obfuz.Rename
|
||||
{
|
||||
public interface INameMaker
|
||||
{
|
||||
string GetNewName(ModuleDefMD mod, string originalName);
|
||||
|
||||
string GetNewName(TypeDef typeDef, string originalName);
|
||||
|
||||
string GetNewName(MethodDef methodDef, string originalName);
|
||||
|
||||
string GetNewName(FieldDef fieldDef, string originalName);
|
||||
|
||||
string GetNewName(PropertyDef propertyDef, string originalName);
|
||||
|
||||
string GetNewName(EventDef eventDef, string originalName);
|
||||
}
|
||||
|
||||
public class NameMaker : INameMaker
|
||||
{
|
||||
private string GetDefaultNewName(string originName)
|
||||
{
|
||||
return originName + "_generated_obfuz";
|
||||
}
|
||||
|
||||
public string GetNewName(ModuleDefMD mod, string originalName)
|
||||
{
|
||||
return GetDefaultNewName(originalName);
|
||||
}
|
||||
|
||||
public string GetNewName(TypeDef typeDef, string originalName)
|
||||
{
|
||||
return GetDefaultNewName(originalName);
|
||||
}
|
||||
|
||||
public string GetNewName(MethodDef methodDef, string originalName)
|
||||
{
|
||||
return GetDefaultNewName(originalName);
|
||||
}
|
||||
|
||||
public string GetNewName(FieldDef fieldDef, string originalName)
|
||||
{
|
||||
return GetDefaultNewName(originalName);
|
||||
}
|
||||
|
||||
public string GetNewName(PropertyDef propertyDef, string originalName)
|
||||
{
|
||||
return GetDefaultNewName(originalName);
|
||||
}
|
||||
|
||||
public string GetNewName(EventDef eventDef, string originalName)
|
||||
{
|
||||
return GetDefaultNewName(originalName);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,94 +9,49 @@ namespace Obfuz.Rename
|
|||
{
|
||||
public interface IRenamePolicy
|
||||
{
|
||||
bool NeedKeepName(ModuleDefMD mod);
|
||||
bool NeedRename(ModuleDefMD mod);
|
||||
|
||||
bool NeedKeepName(TypeDef typeDef);
|
||||
bool NeedRename(TypeDef typeDef);
|
||||
|
||||
bool NeedKeepName(MethodDef methodDef);
|
||||
bool NeedRename(MethodDef methodDef);
|
||||
|
||||
bool NeedKeepName(FieldDef fieldDef);
|
||||
|
||||
bool NeedKeepName(PropertyDef propertyDef);
|
||||
|
||||
bool NeedKeepName(EventDef eventDef);
|
||||
|
||||
|
||||
string GetNewName(ModuleDefMD mod, string originalName);
|
||||
|
||||
string GetNewName(TypeDef typeDef, string originalName);
|
||||
|
||||
string GetNewName(MethodDef methodDef, string originalName);
|
||||
|
||||
string GetNewName(FieldDef fieldDef, string originalName);
|
||||
|
||||
string GetNewName(PropertyDef propertyDef, string originalName);
|
||||
|
||||
string GetNewName(EventDef eventDef, string originalName);
|
||||
bool NeedRename(FieldDef fieldDef);
|
||||
|
||||
bool NeedRename(PropertyDef propertyDef);
|
||||
|
||||
bool NeedRename(EventDef eventDef);
|
||||
}
|
||||
|
||||
public class RenamePolicy : IRenamePolicy
|
||||
{
|
||||
public bool NeedKeepName(ModuleDefMD mod)
|
||||
public bool NeedRename(ModuleDefMD mod)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
public bool NeedKeepName(TypeDef typeDef)
|
||||
{
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool NeedKeepName(MethodDef methodDef)
|
||||
public bool NeedRename(TypeDef typeDef)
|
||||
{
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool NeedKeepName(FieldDef fieldDef)
|
||||
public bool NeedRename(MethodDef methodDef)
|
||||
{
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool NeedKeepName(PropertyDef propertyDef)
|
||||
public bool NeedRename(FieldDef fieldDef)
|
||||
{
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool NeedKeepName(EventDef eventDef)
|
||||
public bool NeedRename(PropertyDef propertyDef)
|
||||
{
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public string GetNewName(ModuleDefMD mod, string originalName)
|
||||
public bool NeedRename(EventDef eventDef)
|
||||
{
|
||||
return originalName + "_obfuz_generated__";
|
||||
}
|
||||
|
||||
public string GetNewName(TypeDef typeDef, string originalName)
|
||||
{
|
||||
return originalName + "_obfuz_generated__";
|
||||
}
|
||||
|
||||
public string GetNewName(MethodDef methodDef, string originalName)
|
||||
{
|
||||
return originalName + "_obfuz_generated__";
|
||||
}
|
||||
|
||||
public string GetNewName(FieldDef fieldDef, string originalName)
|
||||
{
|
||||
return originalName + "_obfuz_generated__";
|
||||
}
|
||||
|
||||
public string GetNewName(PropertyDef propertyDef, string originalName)
|
||||
{
|
||||
return originalName + "_obfuz_generated__";
|
||||
}
|
||||
|
||||
public string GetNewName(EventDef eventDef, string originalName)
|
||||
{
|
||||
return originalName + "_obfuz_generated__";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
using dnlib.DotNet;
|
||||
using Obfuz.Rename;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Obfuz
|
||||
|
@ -19,8 +22,103 @@ namespace Obfuz
|
|||
}
|
||||
|
||||
public void Process()
|
||||
{
|
||||
foreach (ObfuzAssemblyInfo ass in _ctx.assemblies)
|
||||
{
|
||||
if (_renamePolicy.NeedRename(ass.module))
|
||||
{
|
||||
Rename(ass.module);
|
||||
}
|
||||
foreach (TypeDef type in ass.module.GetTypes())
|
||||
{
|
||||
if (type.FullName != "<Module>" && _renamePolicy.NeedRename(type))
|
||||
{
|
||||
Rename(type);
|
||||
}
|
||||
foreach (FieldDef field in type.Fields)
|
||||
{
|
||||
if (_renamePolicy.NeedRename(field))
|
||||
{
|
||||
Rename(field);
|
||||
}
|
||||
}
|
||||
foreach (MethodDef method in type.Methods)
|
||||
{
|
||||
if (_renamePolicy.NeedRename(method))
|
||||
{
|
||||
Rename(method);
|
||||
foreach (Parameter param in method.Parameters)
|
||||
{
|
||||
Rename(param.ParamDef);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (EventDef eventDef in type.Events)
|
||||
{
|
||||
if (_renamePolicy.NeedRename(eventDef))
|
||||
{
|
||||
Rename(eventDef);
|
||||
}
|
||||
}
|
||||
foreach (PropertyDef property in type.Properties)
|
||||
{
|
||||
if (_renamePolicy.NeedRename(property))
|
||||
{
|
||||
Rename(property);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<ObfuzAssemblyInfo> GetReferenceMeAssemblies(ModuleDefMD mod)
|
||||
{
|
||||
return _ctx.assemblies.Find(ass => ass.module == mod).referenceMeAssemblies;
|
||||
}
|
||||
|
||||
private void Rename(ModuleDefMD mod)
|
||||
{
|
||||
string oldName = MetaUtil.GetModuleNameWithoutExt(mod.Name);
|
||||
string newName = _ctx.nameMaker.GetNewName(mod, oldName);
|
||||
mod.Name = $"{newName}.dll";
|
||||
Debug.Log($"rename module. oldName:{oldName} newName:{newName}");
|
||||
foreach (ObfuzAssemblyInfo ass in GetReferenceMeAssemblies(mod))
|
||||
{
|
||||
foreach (AssemblyRef assRef in ass.module.GetAssemblyRefs())
|
||||
{
|
||||
if (assRef.Name == oldName)
|
||||
{
|
||||
assRef.Name = newName;
|
||||
Debug.Log($"rename assembly:{ass.name} ref oldName:{oldName} newName:{newName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Rename(TypeDef type)
|
||||
{
|
||||
}
|
||||
|
||||
private void Rename(FieldDef field)
|
||||
{
|
||||
}
|
||||
|
||||
private void Rename(MethodDef method)
|
||||
{
|
||||
}
|
||||
|
||||
private void Rename(ParamDef param)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Rename(EventDef eventDef)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Rename(PropertyDef property)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue