diff --git a/de4dot.code/renamer/Renamer.cs b/de4dot.code/renamer/Renamer.cs index 0310f20a..e74fcd2f 100644 --- a/de4dot.code/renamer/Renamer.cs +++ b/de4dot.code/renamer/Renamer.cs @@ -315,6 +315,14 @@ namespace de4dot.code.renamer { refToDef.reference.Name = refToDef.definition.Name; foreach (var refToDef in module.FieldRefsToRename) 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(); } } diff --git a/de4dot.code/renamer/asmmodules/MemberRefFinder.cs b/de4dot.code/renamer/asmmodules/MemberRefFinder.cs index c94e8fd8..3bbe166d 100644 --- a/de4dot.code/renamer/asmmodules/MemberRefFinder.cs +++ b/de4dot.code/renamer/asmmodules/MemberRefFinder.cs @@ -44,6 +44,7 @@ namespace de4dot.code.renamer.asmmodules { public Dictionary pointerTypes = new Dictionary(); public Dictionary byReferenceTypes = new Dictionary(); public Dictionary sentinelTypes = new Dictionary(); + public Dictionary customAttributes = new Dictionary(); Stack memberRefStack; ModuleDefinition validModule; @@ -350,6 +351,7 @@ namespace de4dot.code.renamer.asmmodules { void addCustomAttribute(CustomAttribute attr) { if (attr == null) return; + customAttributes[attr] = true; pushMember(attr.Constructor); // 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) { - bool present; - if (eventDefinitions.TryGetValue(eventDefinition, out present)) + if (eventDefinitions.ContainsKey(eventDefinition)) return; eventDefinitions[eventDefinition] = true; addEventDefinition(eventDefinition); } void doFieldReference(FieldReference fieldReference) { - bool present; - if (fieldReferences.TryGetValue(fieldReference, out present)) + if (fieldReferences.ContainsKey(fieldReference)) return; fieldReferences[fieldReference] = true; addFieldReference(fieldReference); } void doFieldDefinition(FieldDefinition fieldDefinition) { - bool present; - if (fieldDefinitions.TryGetValue(fieldDefinition, out present)) + if (fieldDefinitions.ContainsKey(fieldDefinition)) return; fieldDefinitions[fieldDefinition] = true; addFieldDefinition(fieldDefinition); } void doMethodReference(MethodReference methodReference) { - bool present; - if (methodReferences.TryGetValue(methodReference, out present)) + if (methodReferences.ContainsKey(methodReference)) return; methodReferences[methodReference] = true; addMethodReference(methodReference); } void doMethodDefinition(MethodDefinition methodDefinition) { - bool present; - if (methodDefinitions.TryGetValue(methodDefinition, out present)) + if (methodDefinitions.ContainsKey(methodDefinition)) return; methodDefinitions[methodDefinition] = true; addMethodDefinition(methodDefinition); } void doGenericInstanceMethod(GenericInstanceMethod genericInstanceMethod) { - bool present; - if (genericInstanceMethods.TryGetValue(genericInstanceMethod, out present)) + if (genericInstanceMethods.ContainsKey(genericInstanceMethod)) return; genericInstanceMethods[genericInstanceMethod] = true; addGenericInstanceMethod(genericInstanceMethod); } void doPropertyDefinition(PropertyDefinition propertyDefinition) { - bool present; - if (propertyDefinitions.TryGetValue(propertyDefinition, out present)) + if (propertyDefinitions.ContainsKey(propertyDefinition)) return; propertyDefinitions[propertyDefinition] = true; addPropertyDefinition(propertyDefinition); } void doTypeReference(TypeReference typeReference) { - bool present; - if (typeReferences.TryGetValue(typeReference, out present)) + if (typeReferences.ContainsKey(typeReference)) return; typeReferences[typeReference] = true; addTypeReference(typeReference); } void doTypeDefinition(TypeDefinition typeDefinition) { - bool present; - if (typeDefinitions.TryGetValue(typeDefinition, out present)) + if (typeDefinitions.ContainsKey(typeDefinition)) return; typeDefinitions[typeDefinition] = true; addTypeDefinition(typeDefinition); } void doGenericParameter(GenericParameter genericParameter) { - bool present; - if (genericParameters.TryGetValue(genericParameter, out present)) + if (genericParameters.ContainsKey(genericParameter)) return; genericParameters[genericParameter] = true; addGenericParameter(genericParameter); } void doArrayType(ArrayType arrayType) { - bool present; - if (arrayTypes.TryGetValue(arrayType, out present)) + if (arrayTypes.ContainsKey(arrayType)) return; arrayTypes[arrayType] = true; addArrayType(arrayType); } void doFunctionPointerType(FunctionPointerType functionPointerType) { - bool present; - if (functionPointerTypes.TryGetValue(functionPointerType, out present)) + if (functionPointerTypes.ContainsKey(functionPointerType)) return; functionPointerTypes[functionPointerType] = true; addFunctionPointerType(functionPointerType); } void doGenericInstanceType(GenericInstanceType genericInstanceType) { - bool present; - if (genericInstanceTypes.TryGetValue(genericInstanceType, out present)) + if (genericInstanceTypes.ContainsKey(genericInstanceType)) return; genericInstanceTypes[genericInstanceType] = true; addGenericInstanceType(genericInstanceType); } void doOptionalModifierType(OptionalModifierType optionalModifierType) { - bool present; - if (optionalModifierTypes.TryGetValue(optionalModifierType, out present)) + if (optionalModifierTypes.ContainsKey(optionalModifierType)) return; optionalModifierTypes[optionalModifierType] = true; addOptionalModifierType(optionalModifierType); } void doRequiredModifierType(RequiredModifierType requiredModifierType) { - bool present; - if (requiredModifierTypes.TryGetValue(requiredModifierType, out present)) + if (requiredModifierTypes.ContainsKey(requiredModifierType)) return; requiredModifierTypes[requiredModifierType] = true; addRequiredModifierType(requiredModifierType); } void doPinnedType(PinnedType pinnedType) { - bool present; - if (pinnedTypes.TryGetValue(pinnedType, out present)) + if (pinnedTypes.ContainsKey(pinnedType)) return; pinnedTypes[pinnedType] = true; addPinnedType(pinnedType); } void doPointerType(PointerType pointerType) { - bool present; - if (pointerTypes.TryGetValue(pointerType, out present)) + if (pointerTypes.ContainsKey(pointerType)) return; pointerTypes[pointerType] = true; addPointerType(pointerType); } void doByReferenceType(ByReferenceType byReferenceType) { - bool present; - if (byReferenceTypes.TryGetValue(byReferenceType, out present)) + if (byReferenceTypes.ContainsKey(byReferenceType)) return; byReferenceTypes[byReferenceType] = true; addByReferenceType(byReferenceType); } void doSentinelType(SentinelType sentinelType) { - bool present; - if (sentinelTypes.TryGetValue(sentinelType, out present)) + if (sentinelTypes.ContainsKey(sentinelType)) return; sentinelTypes[sentinelType] = true; addSentinelType(sentinelType); diff --git a/de4dot.code/renamer/asmmodules/Module.cs b/de4dot.code/renamer/asmmodules/Module.cs index 7c136bbc..82664b3a 100644 --- a/de4dot.code/renamer/asmmodules/Module.cs +++ b/de4dot.code/renamer/asmmodules/Module.cs @@ -30,8 +30,21 @@ namespace de4dot.code.renamer.asmmodules { IList> typeRefsToRename = new List>(); IList> methodRefsToRename = new List>(); IList> fieldRefsToRename = new List>(); + List customAttributeFieldReferences = new List(); + List customAttributePropertyReferences = new List(); List 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 where R : MemberReference where D : R { public R reference; public D definition; @@ -53,6 +66,14 @@ namespace de4dot.code.renamer.asmmodules { get { return fieldRefsToRename; } } + public IEnumerable CustomAttributeFieldReferences { + get { return customAttributeFieldReferences; } + } + + public IEnumerable CustomAttributePropertyReferences { + get { return customAttributePropertyReferences; } + } + public IObfuscatedFile ObfuscatedFile { get { return obfuscatedFile; } } @@ -127,6 +148,60 @@ namespace de4dot.code.renamer.asmmodules { if (fieldDef != null) fieldRefsToRename.Add(new RefToDef(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() {