Rename时不会使用dll存在的名称及mapping.xml中记录的映射过的名称

backup
walon 2025-04-16 18:05:21 +08:00
parent 6e8ef517cd
commit 4cb3b88d24
11 changed files with 326 additions and 183 deletions

View File

@ -15,5 +15,10 @@ namespace Obfuz
values.Add(value);
}
}
public static V GetValueOrDefault<K, V>(IDictionary<K, V> dic, K key)
{
return dic.TryGetValue(key, out V v) ? v : default(V);
}
}
}

View File

@ -15,6 +15,16 @@ namespace Obfuz
return Path.GetFileNameWithoutExtension(moduleName);
}
public static (string, string) SplitNamespaceAndName(string fullName)
{
int index = fullName.LastIndexOf('/');
if (index == -1)
{
int index2 = fullName.IndexOf('.');
return index2 >= 0 ? (fullName.Substring(0, index2), fullName.Substring(index2 + 1)) : ("", fullName);
}
return ("", fullName.Substring(index + 1));
}
public static TypeDef GetBaseTypeDef(TypeDef type)

View File

@ -4,11 +4,27 @@ namespace Obfuz.Rename
{
public interface INameMaker
{
void AddPreservedName(ModuleDefMD mod, string name);
void AddPreservedName(TypeDef typeDef, string name);
void AddPreservedNamespace(TypeDef typeDef, string name);
void AddPreservedName(MethodDef methodDef, string name);
void AddPreservedName(ParamDef paramDef, string name);
void AddPreservedName(FieldDef fieldDef, string name);
void AddPreservedName(PropertyDef propertyDef, string name);
void AddPreservedName(EventDef eventDef, string name);
string GetNewName(ModuleDefMD mod, string originalName);
string GetNewName(TypeDef typeDef, string originalName);
string GetNewNamespace(TypeDef typeDef, string originalNamespace);
string GetNewNamespace(TypeDef typeDef, string originalNamespace, bool reuse);
string GetNewName(MethodDef methodDef, string originalName);

View File

@ -0,0 +1,9 @@
namespace Obfuz.Rename
{
public interface INameScope
{
void AddPreservedName(string name);
string GetNewName(string originalName, bool reuse);
}
}

View File

@ -2,126 +2,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.Rename
{
public class NameScope
public class NameMaker : NameMakerBase
{
private readonly List<string> _wordSet;
private int _nextIndex;
private readonly Dictionary<string, string> _nameMap = new Dictionary<string, string>();
public NameScope(List<string> wordSet)
{
_wordSet = wordSet;
_nextIndex = 0;
}
private string CreateNewName()
{
var nameBuilder = new StringBuilder();
for (int i = _nextIndex++; ;)
{
nameBuilder.Append(_wordSet[i % _wordSet.Count]);
i = i / _wordSet.Count;
if (i == 0)
{
break;
}
}
return nameBuilder.ToString();
}
public string GetNewName()
{
return CreateNewName();
}
public string GetNewName0(string originalName)
{
if (_nameMap.TryGetValue(originalName, out var newName))
{
return newName;
}
newName = CreateNewName();
_nameMap[originalName] = newName;
return newName;
}
}
public class NameMaker : INameMaker
{
private readonly List<string> _wordSet;
private readonly Dictionary<object, NameScope> _nameScopes = new Dictionary<object, NameScope>();
private readonly object _namespaceScope = new object();
public NameMaker(List<string> wordSet)
{
_wordSet = wordSet;
}
private NameScope GetNameScope(object key)
protected override INameScope CreateNameScope()
{
if (!_nameScopes.TryGetValue(key, out var nameScope))
{
nameScope = new NameScope(_wordSet);
_nameScopes[key] = nameScope;
}
return nameScope;
}
public string GetNewName(ModuleDefMD mod, string originalName)
{
return GetDefaultNewName(this, originalName);
}
private string GetDefaultNewName(object scope, string originName)
{
return GetNameScope(scope).GetNewName();
}
public string GetNewNamespace(TypeDef typeDef, string originalNamespace)
{
if (string.IsNullOrEmpty(originalNamespace))
{
return string.Empty;
}
return GetNameScope(_namespaceScope).GetNewName0(originalNamespace);
}
public string GetNewName(TypeDef typeDef, string originalName)
{
return GetDefaultNewName(typeDef.Module, originalName);
}
public string GetNewName(MethodDef methodDef, string originalName)
{
return GetDefaultNewName(methodDef.DeclaringType, originalName);
}
public string GetNewName(ParamDef param, string originalName)
{
return "1";
}
public string GetNewName(FieldDef fieldDef, string originalName)
{
return GetDefaultNewName(fieldDef.DeclaringType, originalName);
}
public string GetNewName(PropertyDef propertyDef, string originalName)
{
return GetDefaultNewName(propertyDef.DeclaringType, originalName);
}
public string GetNewName(EventDef eventDef, string originalName)
{
return GetDefaultNewName(eventDef.DeclaringType, originalName);
return new NameScope(_wordSet);
}
}
}

View File

@ -0,0 +1,119 @@
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.Rename
{
public abstract class NameMakerBase : INameMaker
{
private readonly Dictionary<object, INameScope> _nameScopes = new Dictionary<object, INameScope>();
private readonly object _namespaceScope = new object();
protected abstract INameScope CreateNameScope();
protected INameScope GetNameScope(object key)
{
if (!_nameScopes.TryGetValue(key, out var nameScope))
{
nameScope = CreateNameScope();
_nameScopes[key] = nameScope;
}
return nameScope;
}
public void AddPreservedName(ModuleDefMD mod, string name)
{
GetNameScope(mod).AddPreservedName(name);
}
public void AddPreservedName(TypeDef typeDef, string name)
{
GetNameScope(typeDef.Module).AddPreservedName(name);
}
public void AddPreservedName(MethodDef methodDef, string name)
{
GetNameScope(methodDef.DeclaringType).AddPreservedName(name);
}
public void AddPreservedName(ParamDef paramDef, string name)
{
}
public void AddPreservedName(FieldDef fieldDef, string name)
{
GetNameScope(fieldDef.DeclaringType).AddPreservedName(name);
}
public void AddPreservedName(PropertyDef propertyDef, string name)
{
GetNameScope(propertyDef.DeclaringType).AddPreservedName(name);
}
public void AddPreservedName(EventDef eventDef, string name)
{
GetNameScope(eventDef.DeclaringType).AddPreservedName(name);
}
public void AddPreservedNamespace(TypeDef typeDef, string name)
{
GetNameScope(_namespaceScope).AddPreservedName(name);
}
public string GetNewName(ModuleDefMD mod, string originalName)
{
return GetDefaultNewName(this, originalName);
}
private string GetDefaultNewName(object scope, string originName)
{
return GetNameScope(scope).GetNewName(originName, false);
}
public string GetNewNamespace(TypeDef typeDef, string originalNamespace, bool reuse)
{
if (string.IsNullOrEmpty(originalNamespace))
{
return string.Empty;
}
return GetNameScope(_namespaceScope).GetNewName(originalNamespace, reuse);
}
public string GetNewName(TypeDef typeDef, string originalName)
{
return GetDefaultNewName(typeDef.Module, originalName);
}
public string GetNewName(MethodDef methodDef, string originalName)
{
return GetDefaultNewName(methodDef.DeclaringType, originalName);
}
public string GetNewName(ParamDef param, string originalName)
{
return "1";
}
public string GetNewName(FieldDef fieldDef, string originalName)
{
return GetDefaultNewName(fieldDef.DeclaringType, originalName);
}
public string GetNewName(PropertyDef propertyDef, string originalName)
{
return GetDefaultNewName(propertyDef.DeclaringType, originalName);
}
public string GetNewName(EventDef eventDef, string originalName)
{
return GetDefaultNewName(eventDef.DeclaringType, originalName);
}
}
}

View File

@ -0,0 +1,32 @@
using Microsoft.SqlServer.Server;
using System.Collections.Generic;
using System.Text;
namespace Obfuz.Rename
{
public class NameScope : NameScopeBase
{
private readonly List<string> _wordSet;
private int _nextIndex;
public NameScope(List<string> wordSet)
{
_wordSet = wordSet;
_nextIndex = 0;
}
protected override void BuildNewName(StringBuilder nameBuilder, string originalName)
{
for (int i = _nextIndex++; ;)
{
nameBuilder.Append(_wordSet[i % _wordSet.Count]);
i = i / _wordSet.Count;
if (i == 0)
{
break;
}
}
}
}
}

View File

@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.Text;
namespace Obfuz.Rename
{
public abstract class NameScopeBase : INameScope
{
private readonly Dictionary<string, string> _nameMap = new Dictionary<string, string>();
private readonly HashSet<string> _preservedNames = new HashSet<string>();
public void AddPreservedName(string name)
{
if (!string.IsNullOrEmpty(name))
{
_preservedNames.Add(name);
}
}
protected abstract void BuildNewName(StringBuilder nameBuilder, string originalName);
private string CreateNewName(string originalName)
{
var nameBuilder = new StringBuilder();
while (true)
{
nameBuilder.Clear();
BuildNewName(nameBuilder, originalName);
string newName = nameBuilder.ToString();
if (_preservedNames.Add(newName))
{
return newName;
}
}
}
public string GetNewName(string originalName, bool reuse)
{
if (!reuse)
{
return CreateNewName(originalName);
}
if (_nameMap.TryGetValue(originalName, out var newName))
{
return newName;
}
newName = CreateNewName(originalName);
_nameMap[originalName] = newName;
return newName;
}
}
}

View File

@ -101,16 +101,17 @@ namespace Obfuz
public RenameRecordMap(string mappingFile)
{
_mappingFile = mappingFile;
LoadXmlMappingFile(mappingFile);
}
public void Init(List<ObfuzAssemblyInfo> assemblies)
public void Init(List<ObfuzAssemblyInfo> assemblies, INameMaker nameMaker)
{
LoadXmlMappingFile(_mappingFile);
foreach (var ObfuzAssemblyInfo in assemblies)
{
ModuleDefMD mod = ObfuzAssemblyInfo.module;
string name = mod.Assembly.Name;
nameMaker.AddPreservedName(mod, name);
_modRenames.Add(mod, new RenameRecord
{
status = RenameStatus.NotRenamed,
@ -118,21 +119,48 @@ namespace Obfuz
oldName = name,
newName = null,
});
RenameMappingAssembly rma = _assemblies.GetValueOrDefault(name);
if (rma != null && rma.status == RenameStatus.Renamed)
{
nameMaker.AddPreservedName(mod, rma.newAssName);
}
foreach (TypeDef type in mod.GetTypes())
{
nameMaker.AddPreservedName(type, name);
nameMaker.AddPreservedNamespace(type, type.Namespace);
string fullTypeName = type.FullName;
RenameMappingType rmt = rma?.types.GetValueOrDefault(fullTypeName);
if (rmt != null)
{
var (newNamespace, newName) = MetaUtil.SplitNamespaceAndName(rmt.newFullName);
nameMaker.AddPreservedNamespace(type, newNamespace);
nameMaker.AddPreservedName(type, newName);
}
_typeRenames.Add(type, new RenameRecord
{
status = RenameStatus.NotRenamed,
signature = type.FullName,
oldName = type.FullName,
signature = fullTypeName,
oldName = fullTypeName,
newName = null,
});
foreach (MethodDef method in type.Methods)
{
nameMaker.AddPreservedName(method, method.Name);
string methodSig = TypeSigUtil.ComputeMethodDefSignature(method);
nameMaker.AddPreservedName(method, method.Name);
RenameMappingMethod rmm = rmt?.methods.GetValueOrDefault(methodSig);
if (rmm != null)
{
nameMaker.AddPreservedName(method, rmm.newName);
}
_methodRenames.Add(method, new RenameRecord
{
status = RenameStatus.NotRenamed,
signature = TypeSigUtil.ComputeMethodDefSignature(method),
signature = methodSig,
oldName = method.Name,
newName = null,
});
@ -152,30 +180,51 @@ namespace Obfuz
}
foreach (FieldDef field in type.Fields)
{
nameMaker.AddPreservedName(field, field.Name);
string fieldSig = TypeSigUtil.ComputeFieldDefSignature(field);
RenameMappingField rmf = rmt?.fields.GetValueOrDefault(fieldSig);
if (rmf != null)
{
nameMaker.AddPreservedName(field, rmf.newName);
}
_fieldRenames.Add(field, new RenameRecord
{
status = RenameStatus.NotRenamed,
signature = TypeSigUtil.ComputeFieldDefSignature(field),
signature = fieldSig,
oldName = field.Name,
newName = null,
});
}
foreach (PropertyDef property in type.Properties)
{
nameMaker.AddPreservedName(property, property.Name);
string propertySig = TypeSigUtil.ComputePropertyDefSignature(property);
RenameMappingProperty rmp = rmt?.properties.GetValueOrDefault(propertySig);
if (rmp != null)
{
nameMaker.AddPreservedName(property, rmp.newName);
}
_propertyRenames.Add(property, new RenameRecord
{
status = RenameStatus.NotRenamed,
signature = TypeSigUtil.ComputePropertyDefSignature(property),
signature = propertySig,
oldName = property.Name,
newName = null,
});
}
foreach (EventDef eventDef in type.Events)
{
nameMaker.AddPreservedName(eventDef, eventDef.Name);
string eventSig = TypeSigUtil.ComputeEventDefSignature(eventDef);
RenameMappingEvent rme = rmt?.events.GetValueOrDefault(eventSig);
if (rme != null)
{
nameMaker.AddPreservedName(eventDef, rme.newName);
}
_eventRenames.Add(eventDef, new RenameRecord
{
status = RenameStatus.NotRenamed,
signature = TypeSigUtil.ComputeEventDefSignature(eventDef),
signature = eventSig,
oldName = eventDef.Name,
newName = null,
});
@ -203,8 +252,6 @@ namespace Obfuz
}
}
private void LoadAssemblyMapping(XmlElement ele)
{
if (ele.Name != "assembly")

View File

@ -118,7 +118,7 @@ namespace Obfuz
public void Process()
{
_renameRecordMap.Init(_obfuzAssemblies);
_renameRecordMap.Init(_obfuzAssemblies, _nameMaker);
RenameModules();
RenameTypes();
RenameFields();
@ -236,7 +236,7 @@ namespace Obfuz
{
if (_renamePolicy.NeedRename(type))
{
Rename(type, _refTypeRefMetasMap.TryGetValue(type, out var typeDefMetas) ? typeDefMetas : null);
Rename(type, _refTypeRefMetasMap.GetValueOrDefault(type));
}
}
}
@ -342,7 +342,7 @@ namespace Obfuz
{
if (_renamePolicy.NeedRename(field))
{
Rename(field, refFieldMetasMap.TryGetValue(field, out var fieldMetas) ? fieldMetas : null);
Rename(field, refFieldMetasMap.GetValueOrDefault(field));
}
}
}
@ -410,7 +410,7 @@ namespace Obfuz
}
if (_renamePolicy.NeedRename(method))
{
Rename(method, refMethodMetasMap.TryGetValue(method, out var refMethodMetas) ? refMethodMetas : null);
Rename(method, refMethodMetasMap.GetValueOrDefault(method));
}
}
}
@ -443,7 +443,7 @@ namespace Obfuz
}
if (_renameRecordMap.TryGetRename(group, out var newName))
{
Rename(method, refMethodMetasMap.TryGetValue(method, out var refMethodMetas) ? refMethodMetas : null, newName);
Rename(method, refMethodMetasMap.GetValueOrDefault(method), newName);
}
else
{
@ -516,7 +516,7 @@ namespace Obfuz
{
if (_renamePolicy.NeedRename(property))
{
Rename(property, refPropertyMetasMap.TryGetValue(property, out var refPropertyMeta) ? refPropertyMeta : null);
Rename(property, refPropertyMetasMap.GetValueOrDefault(property));
}
}
}
@ -545,9 +545,10 @@ namespace Obfuz
private void Rename(ModuleDefMD mod)
{
string oldName = MetaUtil.GetModuleNameWithoutExt(mod.Name);
string oldName = mod.Assembly.Name;
string newName = _nameMaker.GetNewName(mod, oldName);
_renameRecordMap.AddRename(mod, newName);
mod.Assembly.Name = newName;
mod.Name = $"{newName}.dll";
//Debug.Log($"rename module. oldName:{oldName} newName:{newName}");
foreach (ObfuzAssemblyInfo ass in GetReferenceMeAssemblies(mod))
@ -556,6 +557,7 @@ namespace Obfuz
{
if (assRef.Name == oldName)
{
_renameRecordMap.AddRename(mod, newName);
assRef.Name = newName;
// Debug.Log($"rename assembly:{ass.name} ref oldName:{oldName} newName:{newName}");
}
@ -575,7 +577,7 @@ namespace Obfuz
}
else
{
newNamespace = _nameMaker.GetNewNamespace(type, oldNamespace);
newNamespace = _nameMaker.GetNewNamespace(type, oldNamespace, true);
type.Namespace = newNamespace;
}

View File

@ -1,71 +1,22 @@
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;
namespace Obfuz.Rename
{
public class TestNameMaker : INameMaker
public class TestNameMaker : NameMakerBase
{
private class TestNameScope : NameScopeBase
{
private int _nextIndex;
private readonly Dictionary<string, string> _nameMap = new Dictionary<string, string>();
private string GetDefaultNewName(string originName)
protected override void BuildNewName(StringBuilder nameBuilder, string originalName)
{
return $"{originName}>{_nextIndex++}";
nameBuilder.Append($"{originalName}>{_nextIndex++}");
}
}
public string GetNewName(ModuleDefMD mod, string originalName)
protected override INameScope CreateNameScope()
{
return GetDefaultNewName(originalName);
return new TestNameScope();
}
public string GetNewNamespace(TypeDef typeDef, string originalNamespace)
{
if (string.IsNullOrEmpty(originalNamespace))
{
return string.Empty;
}
if (_nameMap.TryGetValue(originalNamespace, out var newName))
{
return newName;
}
newName = GetDefaultNewName(originalNamespace);
_nameMap.Add(originalNamespace, newName);
return newName;
}
public string GetNewName(TypeDef typeDef, string originalName)
{
return GetDefaultNewName(originalName);
}
public string GetNewName(MethodDef methodDef, string originalName)
{
return GetDefaultNewName(originalName);
}
public string GetNewName(ParamDef param, 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);
}
}
}