Rename custom attribute fields and properties
This commit is contained in:
parent
c3c92ebfaa
commit
77f1f2de67
|
@ -315,6 +315,14 @@ namespace de4dot.code.renamer {
|
||||||
refToDef.reference.Name = refToDef.definition.Name;
|
refToDef.reference.Name = refToDef.definition.Name;
|
||||||
foreach (var refToDef in module.FieldRefsToRename)
|
foreach (var refToDef in module.FieldRefsToRename)
|
||||||
refToDef.reference.Name = refToDef.definition.Name;
|
refToDef.reference.Name = refToDef.definition.Name;
|
||||||
|
foreach (var info in module.CustomAttributeFieldReferences) {
|
||||||
|
var field = info.cattr.Fields[info.index];
|
||||||
|
info.cattr.Fields[info.index] = new CustomAttributeNamedArgument(info.reference.Name, field.Argument);
|
||||||
|
}
|
||||||
|
foreach (var info in module.CustomAttributePropertyReferences) {
|
||||||
|
var prop = info.cattr.Properties[info.index];
|
||||||
|
info.cattr.Properties[info.index] = new CustomAttributeNamedArgument(info.reference.Name, prop.Argument);
|
||||||
|
}
|
||||||
Log.deIndent();
|
Log.deIndent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,7 @@ namespace de4dot.code.renamer.asmmodules {
|
||||||
public Dictionary<PointerType, bool> pointerTypes = new Dictionary<PointerType, bool>();
|
public Dictionary<PointerType, bool> pointerTypes = new Dictionary<PointerType, bool>();
|
||||||
public Dictionary<ByReferenceType, bool> byReferenceTypes = new Dictionary<ByReferenceType, bool>();
|
public Dictionary<ByReferenceType, bool> byReferenceTypes = new Dictionary<ByReferenceType, bool>();
|
||||||
public Dictionary<SentinelType, bool> sentinelTypes = new Dictionary<SentinelType, bool>();
|
public Dictionary<SentinelType, bool> sentinelTypes = new Dictionary<SentinelType, bool>();
|
||||||
|
public Dictionary<CustomAttribute, bool> customAttributes = new Dictionary<CustomAttribute, bool>();
|
||||||
|
|
||||||
Stack<MemberReference> memberRefStack;
|
Stack<MemberReference> memberRefStack;
|
||||||
ModuleDefinition validModule;
|
ModuleDefinition validModule;
|
||||||
|
@ -350,6 +351,7 @@ namespace de4dot.code.renamer.asmmodules {
|
||||||
void addCustomAttribute(CustomAttribute attr) {
|
void addCustomAttribute(CustomAttribute attr) {
|
||||||
if (attr == null)
|
if (attr == null)
|
||||||
return;
|
return;
|
||||||
|
customAttributes[attr] = true;
|
||||||
pushMember(attr.Constructor);
|
pushMember(attr.Constructor);
|
||||||
|
|
||||||
// Some obfuscators don't rename custom ctor arguments to the new name, causing
|
// Some obfuscators don't rename custom ctor arguments to the new name, causing
|
||||||
|
@ -572,134 +574,115 @@ namespace de4dot.code.renamer.asmmodules {
|
||||||
}
|
}
|
||||||
|
|
||||||
void doEventDefinition(EventDefinition eventDefinition) {
|
void doEventDefinition(EventDefinition eventDefinition) {
|
||||||
bool present;
|
if (eventDefinitions.ContainsKey(eventDefinition))
|
||||||
if (eventDefinitions.TryGetValue(eventDefinition, out present))
|
|
||||||
return;
|
return;
|
||||||
eventDefinitions[eventDefinition] = true;
|
eventDefinitions[eventDefinition] = true;
|
||||||
addEventDefinition(eventDefinition);
|
addEventDefinition(eventDefinition);
|
||||||
}
|
}
|
||||||
void doFieldReference(FieldReference fieldReference) {
|
void doFieldReference(FieldReference fieldReference) {
|
||||||
bool present;
|
if (fieldReferences.ContainsKey(fieldReference))
|
||||||
if (fieldReferences.TryGetValue(fieldReference, out present))
|
|
||||||
return;
|
return;
|
||||||
fieldReferences[fieldReference] = true;
|
fieldReferences[fieldReference] = true;
|
||||||
addFieldReference(fieldReference);
|
addFieldReference(fieldReference);
|
||||||
}
|
}
|
||||||
void doFieldDefinition(FieldDefinition fieldDefinition) {
|
void doFieldDefinition(FieldDefinition fieldDefinition) {
|
||||||
bool present;
|
if (fieldDefinitions.ContainsKey(fieldDefinition))
|
||||||
if (fieldDefinitions.TryGetValue(fieldDefinition, out present))
|
|
||||||
return;
|
return;
|
||||||
fieldDefinitions[fieldDefinition] = true;
|
fieldDefinitions[fieldDefinition] = true;
|
||||||
addFieldDefinition(fieldDefinition);
|
addFieldDefinition(fieldDefinition);
|
||||||
}
|
}
|
||||||
void doMethodReference(MethodReference methodReference) {
|
void doMethodReference(MethodReference methodReference) {
|
||||||
bool present;
|
if (methodReferences.ContainsKey(methodReference))
|
||||||
if (methodReferences.TryGetValue(methodReference, out present))
|
|
||||||
return;
|
return;
|
||||||
methodReferences[methodReference] = true;
|
methodReferences[methodReference] = true;
|
||||||
addMethodReference(methodReference);
|
addMethodReference(methodReference);
|
||||||
}
|
}
|
||||||
void doMethodDefinition(MethodDefinition methodDefinition) {
|
void doMethodDefinition(MethodDefinition methodDefinition) {
|
||||||
bool present;
|
if (methodDefinitions.ContainsKey(methodDefinition))
|
||||||
if (methodDefinitions.TryGetValue(methodDefinition, out present))
|
|
||||||
return;
|
return;
|
||||||
methodDefinitions[methodDefinition] = true;
|
methodDefinitions[methodDefinition] = true;
|
||||||
addMethodDefinition(methodDefinition);
|
addMethodDefinition(methodDefinition);
|
||||||
}
|
}
|
||||||
void doGenericInstanceMethod(GenericInstanceMethod genericInstanceMethod) {
|
void doGenericInstanceMethod(GenericInstanceMethod genericInstanceMethod) {
|
||||||
bool present;
|
if (genericInstanceMethods.ContainsKey(genericInstanceMethod))
|
||||||
if (genericInstanceMethods.TryGetValue(genericInstanceMethod, out present))
|
|
||||||
return;
|
return;
|
||||||
genericInstanceMethods[genericInstanceMethod] = true;
|
genericInstanceMethods[genericInstanceMethod] = true;
|
||||||
addGenericInstanceMethod(genericInstanceMethod);
|
addGenericInstanceMethod(genericInstanceMethod);
|
||||||
}
|
}
|
||||||
void doPropertyDefinition(PropertyDefinition propertyDefinition) {
|
void doPropertyDefinition(PropertyDefinition propertyDefinition) {
|
||||||
bool present;
|
if (propertyDefinitions.ContainsKey(propertyDefinition))
|
||||||
if (propertyDefinitions.TryGetValue(propertyDefinition, out present))
|
|
||||||
return;
|
return;
|
||||||
propertyDefinitions[propertyDefinition] = true;
|
propertyDefinitions[propertyDefinition] = true;
|
||||||
addPropertyDefinition(propertyDefinition);
|
addPropertyDefinition(propertyDefinition);
|
||||||
}
|
}
|
||||||
void doTypeReference(TypeReference typeReference) {
|
void doTypeReference(TypeReference typeReference) {
|
||||||
bool present;
|
if (typeReferences.ContainsKey(typeReference))
|
||||||
if (typeReferences.TryGetValue(typeReference, out present))
|
|
||||||
return;
|
return;
|
||||||
typeReferences[typeReference] = true;
|
typeReferences[typeReference] = true;
|
||||||
addTypeReference(typeReference);
|
addTypeReference(typeReference);
|
||||||
}
|
}
|
||||||
void doTypeDefinition(TypeDefinition typeDefinition) {
|
void doTypeDefinition(TypeDefinition typeDefinition) {
|
||||||
bool present;
|
if (typeDefinitions.ContainsKey(typeDefinition))
|
||||||
if (typeDefinitions.TryGetValue(typeDefinition, out present))
|
|
||||||
return;
|
return;
|
||||||
typeDefinitions[typeDefinition] = true;
|
typeDefinitions[typeDefinition] = true;
|
||||||
addTypeDefinition(typeDefinition);
|
addTypeDefinition(typeDefinition);
|
||||||
}
|
}
|
||||||
void doGenericParameter(GenericParameter genericParameter) {
|
void doGenericParameter(GenericParameter genericParameter) {
|
||||||
bool present;
|
if (genericParameters.ContainsKey(genericParameter))
|
||||||
if (genericParameters.TryGetValue(genericParameter, out present))
|
|
||||||
return;
|
return;
|
||||||
genericParameters[genericParameter] = true;
|
genericParameters[genericParameter] = true;
|
||||||
addGenericParameter(genericParameter);
|
addGenericParameter(genericParameter);
|
||||||
}
|
}
|
||||||
void doArrayType(ArrayType arrayType) {
|
void doArrayType(ArrayType arrayType) {
|
||||||
bool present;
|
if (arrayTypes.ContainsKey(arrayType))
|
||||||
if (arrayTypes.TryGetValue(arrayType, out present))
|
|
||||||
return;
|
return;
|
||||||
arrayTypes[arrayType] = true;
|
arrayTypes[arrayType] = true;
|
||||||
addArrayType(arrayType);
|
addArrayType(arrayType);
|
||||||
}
|
}
|
||||||
void doFunctionPointerType(FunctionPointerType functionPointerType) {
|
void doFunctionPointerType(FunctionPointerType functionPointerType) {
|
||||||
bool present;
|
if (functionPointerTypes.ContainsKey(functionPointerType))
|
||||||
if (functionPointerTypes.TryGetValue(functionPointerType, out present))
|
|
||||||
return;
|
return;
|
||||||
functionPointerTypes[functionPointerType] = true;
|
functionPointerTypes[functionPointerType] = true;
|
||||||
addFunctionPointerType(functionPointerType);
|
addFunctionPointerType(functionPointerType);
|
||||||
}
|
}
|
||||||
void doGenericInstanceType(GenericInstanceType genericInstanceType) {
|
void doGenericInstanceType(GenericInstanceType genericInstanceType) {
|
||||||
bool present;
|
if (genericInstanceTypes.ContainsKey(genericInstanceType))
|
||||||
if (genericInstanceTypes.TryGetValue(genericInstanceType, out present))
|
|
||||||
return;
|
return;
|
||||||
genericInstanceTypes[genericInstanceType] = true;
|
genericInstanceTypes[genericInstanceType] = true;
|
||||||
addGenericInstanceType(genericInstanceType);
|
addGenericInstanceType(genericInstanceType);
|
||||||
}
|
}
|
||||||
void doOptionalModifierType(OptionalModifierType optionalModifierType) {
|
void doOptionalModifierType(OptionalModifierType optionalModifierType) {
|
||||||
bool present;
|
if (optionalModifierTypes.ContainsKey(optionalModifierType))
|
||||||
if (optionalModifierTypes.TryGetValue(optionalModifierType, out present))
|
|
||||||
return;
|
return;
|
||||||
optionalModifierTypes[optionalModifierType] = true;
|
optionalModifierTypes[optionalModifierType] = true;
|
||||||
addOptionalModifierType(optionalModifierType);
|
addOptionalModifierType(optionalModifierType);
|
||||||
}
|
}
|
||||||
void doRequiredModifierType(RequiredModifierType requiredModifierType) {
|
void doRequiredModifierType(RequiredModifierType requiredModifierType) {
|
||||||
bool present;
|
if (requiredModifierTypes.ContainsKey(requiredModifierType))
|
||||||
if (requiredModifierTypes.TryGetValue(requiredModifierType, out present))
|
|
||||||
return;
|
return;
|
||||||
requiredModifierTypes[requiredModifierType] = true;
|
requiredModifierTypes[requiredModifierType] = true;
|
||||||
addRequiredModifierType(requiredModifierType);
|
addRequiredModifierType(requiredModifierType);
|
||||||
}
|
}
|
||||||
void doPinnedType(PinnedType pinnedType) {
|
void doPinnedType(PinnedType pinnedType) {
|
||||||
bool present;
|
if (pinnedTypes.ContainsKey(pinnedType))
|
||||||
if (pinnedTypes.TryGetValue(pinnedType, out present))
|
|
||||||
return;
|
return;
|
||||||
pinnedTypes[pinnedType] = true;
|
pinnedTypes[pinnedType] = true;
|
||||||
addPinnedType(pinnedType);
|
addPinnedType(pinnedType);
|
||||||
}
|
}
|
||||||
void doPointerType(PointerType pointerType) {
|
void doPointerType(PointerType pointerType) {
|
||||||
bool present;
|
if (pointerTypes.ContainsKey(pointerType))
|
||||||
if (pointerTypes.TryGetValue(pointerType, out present))
|
|
||||||
return;
|
return;
|
||||||
pointerTypes[pointerType] = true;
|
pointerTypes[pointerType] = true;
|
||||||
addPointerType(pointerType);
|
addPointerType(pointerType);
|
||||||
}
|
}
|
||||||
void doByReferenceType(ByReferenceType byReferenceType) {
|
void doByReferenceType(ByReferenceType byReferenceType) {
|
||||||
bool present;
|
if (byReferenceTypes.ContainsKey(byReferenceType))
|
||||||
if (byReferenceTypes.TryGetValue(byReferenceType, out present))
|
|
||||||
return;
|
return;
|
||||||
byReferenceTypes[byReferenceType] = true;
|
byReferenceTypes[byReferenceType] = true;
|
||||||
addByReferenceType(byReferenceType);
|
addByReferenceType(byReferenceType);
|
||||||
}
|
}
|
||||||
void doSentinelType(SentinelType sentinelType) {
|
void doSentinelType(SentinelType sentinelType) {
|
||||||
bool present;
|
if (sentinelTypes.ContainsKey(sentinelType))
|
||||||
if (sentinelTypes.TryGetValue(sentinelType, out present))
|
|
||||||
return;
|
return;
|
||||||
sentinelTypes[sentinelType] = true;
|
sentinelTypes[sentinelType] = true;
|
||||||
addSentinelType(sentinelType);
|
addSentinelType(sentinelType);
|
||||||
|
|
|
@ -30,8 +30,21 @@ namespace de4dot.code.renamer.asmmodules {
|
||||||
IList<RefToDef<TypeReference, TypeDefinition>> typeRefsToRename = new List<RefToDef<TypeReference, TypeDefinition>>();
|
IList<RefToDef<TypeReference, TypeDefinition>> typeRefsToRename = new List<RefToDef<TypeReference, TypeDefinition>>();
|
||||||
IList<RefToDef<MethodReference, MethodDefinition>> methodRefsToRename = new List<RefToDef<MethodReference, MethodDefinition>>();
|
IList<RefToDef<MethodReference, MethodDefinition>> methodRefsToRename = new List<RefToDef<MethodReference, MethodDefinition>>();
|
||||||
IList<RefToDef<FieldReference, FieldDefinition>> fieldRefsToRename = new List<RefToDef<FieldReference, FieldDefinition>>();
|
IList<RefToDef<FieldReference, FieldDefinition>> fieldRefsToRename = new List<RefToDef<FieldReference, FieldDefinition>>();
|
||||||
|
List<CustomAttributeReference> customAttributeFieldReferences = new List<CustomAttributeReference>();
|
||||||
|
List<CustomAttributeReference> customAttributePropertyReferences = new List<CustomAttributeReference>();
|
||||||
List<MethodDefinition> allMethods;
|
List<MethodDefinition> allMethods;
|
||||||
|
|
||||||
|
public class CustomAttributeReference {
|
||||||
|
public CustomAttribute cattr;
|
||||||
|
public int index;
|
||||||
|
public MemberReference reference;
|
||||||
|
public CustomAttributeReference(CustomAttribute cattr, int index, MemberReference reference) {
|
||||||
|
this.cattr = cattr;
|
||||||
|
this.index = index;
|
||||||
|
this.reference = reference;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class RefToDef<R, D> where R : MemberReference where D : R {
|
public class RefToDef<R, D> where R : MemberReference where D : R {
|
||||||
public R reference;
|
public R reference;
|
||||||
public D definition;
|
public D definition;
|
||||||
|
@ -53,6 +66,14 @@ namespace de4dot.code.renamer.asmmodules {
|
||||||
get { return fieldRefsToRename; }
|
get { return fieldRefsToRename; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerable<CustomAttributeReference> CustomAttributeFieldReferences {
|
||||||
|
get { return customAttributeFieldReferences; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<CustomAttributeReference> CustomAttributePropertyReferences {
|
||||||
|
get { return customAttributePropertyReferences; }
|
||||||
|
}
|
||||||
|
|
||||||
public IObfuscatedFile ObfuscatedFile {
|
public IObfuscatedFile ObfuscatedFile {
|
||||||
get { return obfuscatedFile; }
|
get { return obfuscatedFile; }
|
||||||
}
|
}
|
||||||
|
@ -127,6 +148,60 @@ namespace de4dot.code.renamer.asmmodules {
|
||||||
if (fieldDef != null)
|
if (fieldDef != null)
|
||||||
fieldRefsToRename.Add(new RefToDef<FieldReference, FieldDefinition>(fieldRef, fieldDef.FieldDefinition));
|
fieldRefsToRename.Add(new RefToDef<FieldReference, FieldDefinition>(fieldRef, fieldDef.FieldDefinition));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach (var cattr in memberRefFinder.customAttributes.Keys) {
|
||||||
|
try {
|
||||||
|
var typeDef = resolver.resolve(cattr.AttributeType);
|
||||||
|
if (typeDef == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (int i = 0; i < cattr.Fields.Count; i++) {
|
||||||
|
var field = cattr.Fields[i];
|
||||||
|
var fieldDef = findFieldByName(typeDef, field.Name);
|
||||||
|
if (fieldDef == null) {
|
||||||
|
Log.w("Could not find field {0} in attribute {1} ({2:X8})",
|
||||||
|
Utils.toCsharpString(field.Name),
|
||||||
|
Utils.toCsharpString(typeDef.TypeDefinition.Name),
|
||||||
|
typeDef.TypeDefinition.MetadataToken.ToInt32());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
customAttributeFieldReferences.Add(new CustomAttributeReference(cattr, i, fieldDef.FieldDefinition));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < cattr.Properties.Count; i++) {
|
||||||
|
var prop = cattr.Properties[i];
|
||||||
|
var propDef = findPropertyByName(typeDef, prop.Name);
|
||||||
|
if (propDef == null) {
|
||||||
|
Log.w("Could not find property {0} in attribute {1} ({2:X8})",
|
||||||
|
Utils.toCsharpString(prop.Name),
|
||||||
|
Utils.toCsharpString(typeDef.TypeDefinition.Name),
|
||||||
|
typeDef.TypeDefinition.MetadataToken.ToInt32());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
customAttributePropertyReferences.Add(new CustomAttributeReference(cattr, i, propDef.PropertyDefinition));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static FieldDef findFieldByName(TypeDef typeDef, string name) {
|
||||||
|
foreach (var fieldDef in typeDef.AllFields) {
|
||||||
|
if (fieldDef.FieldDefinition.Name == name)
|
||||||
|
return fieldDef;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PropertyDef findPropertyByName(TypeDef typeDef, string name) {
|
||||||
|
foreach (var propDef in typeDef.AllProperties) {
|
||||||
|
if (propDef.PropertyDefinition.Name == name)
|
||||||
|
return propDef;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onTypesRenamed() {
|
public void onTypesRenamed() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user