public void ChangeConstantFieldParent(int FieldNumber, int ParentNumber) { ConstantPoolMethodInfo FieldRef = (ConstantPoolMethodInfo)FConstantPool.Item(FieldNumber); FieldRef.ParentClass.References--; FieldRef.SetParent((ushort)ParentNumber, FConstantPool); }
public void ChangeConstantFieldType(int FieldNumber, string OldParentName, string NewParentName) { // takes an index into the constantpool // simple changes the name of a method/field in the constant pool // always create new name // TODO: check this! ConstantPoolMethodInfo FieldRef = (ConstantPoolMethodInfo)FConstantPool.Item(FieldNumber); string OldName = FieldRef.NameAndType.Descriptor; string NewName = Common.FixDescriptor(FieldRef.NameAndType.Descriptor, OldParentName, NewParentName); if (OldName == NewName) return; ConstantUtf8Info NewTypeString = new ConstantUtf8Info(NewName); ushort NewTypeIndex = FConstantPool.Add(NewTypeString); FieldRef.NameAndType.SetType(NewTypeIndex, FConstantPool); }
public void ChangeConstantFieldName(int FieldNumber, string NewName) { // takes an index into the constantpool // simple changes the name of a method/field in the constant pool // always create new name // TODO: check this! ConstantPoolMethodInfo FieldRef = (ConstantPoolMethodInfo)FConstantPool.Item(FieldNumber); ConstantUtf8Info NewNameString = new ConstantUtf8Info(NewName); ushort NewNameIndex = FConstantPool.Add(NewNameString); // we have to make a new one ! FieldRef.NameAndType.References--; // add a new string constant to the pool ConstantNameAndTypeInfo NewNaT = new ConstantNameAndTypeInfo(NewNameIndex, FieldRef.NameAndType.TypeIndex, FConstantPool); ushort NewIndex = FConstantPool.Add(NewNaT); // set the method its new name FieldRef.SetNameAndType(NewIndex, FConstantPool); FieldRef.NameAndType.References = 1; }
/// <summary> /// This function runs over a class, fixing up any references from a deobfuscated file. /// </summary> /// <param name="Index">This is the index of the ClassFile to have its references updated</param> /// <param name="ChangeList">This is a list of before/after values from a previously deobfuscated file</param> private void FixReferencePass1(int Index, ArrayList ChangeList, ArrayList OwnerChangeList) { /* the first pass does the following: * - replaces the Super Class name (if it needs replacing) * - replaces any constant method/field names (if they need replacing) * - replaces the class field names (if needed) * it does NOT change the original class name */ TClassFile ClassFile = (TClassFile)FClassFiles[Index]; if (ClassFile == null) return; // - ChangeList[0] is always a string, which is the parent name of the deobfuscated class // - ChangeList[1] is always the deobfuscated (new) class name... yes i know this is lame :P string OldParentName = (string)ChangeList[0]; string NewParentName = (string)ChangeList[1]; // check the Super class name if it needs renaming if (ClassFile.SuperClassName == OldParentName) { ClassFile.ChangeSuperClassName(NewParentName); } // loop through the constant pool for field/method references // check the parent of each, and if the parent is the class we have // just modified, try and match it to one of the changes // in the changearray for (int i = 0; i < ClassFile.ConstantPool.MaxItems(); i++) { if (ClassFile.ConstantPool.Item(i) is ConstantPoolMethodInfo) { ConstantPoolMethodInfo ci = (ConstantPoolMethodInfo)ClassFile.ConstantPool.Item(i); // check its parent if (ci.ParentClass.Name == OldParentName || ci.ParentClass.Name == NewParentName) { // check the descriptor // - for fields this is the field type // - for methods this is the parameter list // if parents are the same, check the name and descriptor // against the list of originals for (int j = 2; j < ChangeList.Count; j++) { if ((ChangeList[j] is TMethodChangeRecord) && (ci is ConstantMethodrefInfo || ci is ConstantInterfaceMethodrefInfo)) { if (ci is ConstantInterfaceMethodrefInfo) { // handle interface references differently TMethodChangeRecord mcr = (TMethodChangeRecord)ChangeList[j]; // if found update it to the overridden version if (mcr.OriginalMethod.Name.Value == ci.NameAndType.Name && mcr.OriginalMethod.Descriptor == ci.NameAndType.Descriptor) { // find the overridden version for (int k = 2; k < OwnerChangeList.Count; k++) { if (OwnerChangeList[k] is TMethodChangeRecord) { TMethodChangeRecord mcr2 = (TMethodChangeRecord)OwnerChangeList[k]; if (mcr2.OriginalMethod.Name.Value == mcr.OriginalMethod.Name.Value && mcr2.OriginalMethod.Descriptor == mcr.OriginalMethod.Descriptor) { ClassFile.ChangeConstantFieldName(i, mcr2.NewMethod.Name.Value); break; } } } } } else { TMethodChangeRecord mcr = (TMethodChangeRecord)ChangeList[j]; // if found update it to the new version... if (mcr.OriginalMethod.Name.Value == ci.NameAndType.Name && mcr.OriginalMethod.Descriptor == ci.NameAndType.Descriptor) { ClassFile.ChangeConstantFieldName(i, mcr.NewMethod.Name.Value); break; } } } else if ((ChangeList[j] is TFieldChangeRecord) && (ci is ConstantFieldrefInfo)) { TFieldChangeRecord fcr = (TFieldChangeRecord)ChangeList[j]; // if found update it to the new version... if (fcr.OriginalField.Name.Value == ci.NameAndType.Name && fcr.OriginalField.Descriptor == ci.NameAndType.Descriptor) { ClassFile.ChangeConstantFieldName(i, fcr.NewField.Name.Value); break; } } } } } } // also loop through the Fields array to change all the Types for (int i = 0; i < ClassFile.Fields.MaxItems(); i++) { ClassFile.ChangeFieldType(i, OldParentName, NewParentName); } // do the same for methods (fix the parameter list) for (int i = 0; i < ClassFile.Methods.MaxItems(); i++) { ClassFile.ChangeMethodParam(i, OldParentName, NewParentName); } // and the same for all the interfaces for (int i = 0; i < ClassFile.Interfaces.Items.Count; i++) { if (ClassFile.Interfaces.Item(i).Name == OldParentName) ClassFile.ChangeInterfaceName(i, NewParentName); } }