def append_cls_name(self, cls, append_str): p = re.compile("^.*\/([\w$-]+);$") # cls_name has package path cls_name = cls.getSignature(True) if cls_name.find(append_str) == -1: s = re.search(p, cls_name) if not s: return False simple_new_name = s.group(1) + '_' + append_str actCntx = ActionContext(self.dex, Actions.RENAME, cls.getItemId(), cls.getAddress()) actData = ActionRenameData() actData.setNewName(simple_new_name) if self.dex.prepareExecution(actCntx, actData): try: bRlt = self.dex.executeAction(actCntx, actData) if (not bRlt): print(u'executeAction fail!') return False else: return True except Exception, e: return self.jeb.renameClass(cls_name, simple_new_name) else: return False
def getItemOriginalName(self, item): actCntx = ActionContext(self.focusUnit, Actions.RENAME, item.getItemId(), item.getSignature()) actData = ActionRenameData() originalName = "" if self.focusUnit.prepareExecution(actCntx, actData): try: originalName = actData.getOriginalName() assert (item.getName() == actData.getCurrentName()) except Exception as e: print(e) return originalName
def getItemOriginalName(self, viewName, itemId, viewAddress): actCntx = ActionContext(self.focusUnit, Actions.RENAME, itemId, viewAddress) actData = ActionRenameData() originalName = "" if self.focusUnit.prepareExecution(actCntx, actData): try: originalName = actData.getOriginalName() assert (viewName == actData.getCurrentName()) except Exception as e: print(e) return originalName
def renameClass(self, unit, clz, sname): ctx = ActionContext(unit, Actions.RENAME, clz.getItemId(), clz.getAddress()) data = ActionRenameData() data.setNewName(sname) if not unit.prepareExecution(ctx, data): return False try: return unit.executeAction(ctx, data) except Exception as e: print(Exception, e) return False
def rename_class(self, unit, originClazz, sourceName, isBackup): actCtx = ActionContext(unit, Actions.RENAME, originClazz.getItemId(), originClazz.getAddress()) actData = ActionRenameData() actData.setNewName(sourceName) if unit.prepareExecution(actCtx, actData): try: result = unit.executeAction(actCtx, actData) if not result: print('rename to %s failed!' % sourceName) except Exception, e: print(Exception, e)
def rename(self, obj, new_name): actCntx = ActionContext(self.unit, Actions.RENAME, obj.getItemId(), obj.getAddress()) actData = ActionRenameData() actData.setNewName(new_name) if self.unit.prepareExecution(actCntx, actData): try: res = self.unit.executeAction(actCntx, actData) if not res: print(u'rename failed [new_name %s]' % new_name) return False else: return True except Exception,e: print(e) return False
def commenceRename(self, originName, newName, isClass): if isClass == 0: clz = self.targetUnit.getClass(originName) elif isClass == 1: clz = self.targetUnit.getField(originName) else: clz = self.targetUnit.getMethod(originName) actCntx = ActionContext(self.targetUnit, Actions.RENAME, clz.getItemId(), clz.getAddress()) actData = ActionRenameData() actData.setNewName(newName) if (self.targetUnit.prepareExecution(actCntx, actData)): # 执行重命名动作 try: bRlt = self.targetUnit.executeAction(actCntx, actData) if (not bRlt): print(u'executeAction fail!') except Exception, e: print Exception, ":", e
def commenceRename(self, codeUnit, ClassName, newName): clz = codeUnit.getClass(ClassName) #clz = codeUnit.getMethod(ClassName) print("[+] Change class name from [ %s ] to [ %s ] " % (ClassName, newName)) #print(clz) actCntx = ActionContext(codeUnit, Actions.RENAME, clz.getItemId(), clz.getAddress()) actData = ActionRenameData() actData.setNewName(newName) if (codeUnit.prepareExecution(actCntx, actData)): try: bRlt = codeUnit.executeAction(actCntx, actData) if (not bRlt): print(u'executeAction fail!') except Exception, e: print Exception, ":", e
def rename(self, unit, origin, source, flag): actCtx = ActionContext(unit, Actions.RENAME, origin.getItemId(), origin.getAddress()) actData = ActionRenameData() if flag == 'field': newName = 'm%s_%d' % (source, self.count) self.count += 1 else: newName = '%s___%s' % (source, origin.getName(True)) actData.setNewName(newName) if unit.prepareExecution(actCtx, actData): try: result = unit.executeAction(actCtx, actData) if result: print('rename to %s success!' % newName) else: print('rename to %s failed!' % newName) except Exception, e: print(Exception, e)
def rename_class(self, unit, originClazz, sourceName): actCtx = ActionContext(unit, Actions.RENAME, originClazz.getItemId(), originClazz.getAddress()) actData = ActionRenameData() actData.setNewName(sourceName) if unit.prepareExecution(actCtx, actData): try: originalName = actData.getOriginalName() if len( originalName ) > 10: # Skip for general cases: already have meaningful class name return sourceName result = unit.executeAction(actCtx, actData) if result: print('rename %s to %s success!' % (originalName, originClazz.getAddress())) else: print('rename to %s failed!' % sourceName) except Exception, e: print(Exception, e)
def renameItembySig(self, codeType, signature, newName): x = None for cu in self.codeUnits: if codeType == "Field": x = cu.getField(signature) elif codeType == "Method": x = cu.getMethod(signature) elif codeType == "Class": x = cu.getClass(signature) else: raise Exception("execute renameItembySig with unknown codeType!") if x: break if not x: return itemId = x.getItemId() itemAddress = x.getAddress() itemSig = x.getSignature() actCntx = ActionContext(cu, Actions.RENAME, itemId, itemAddress) actData = ActionRenameData() if cu.prepareExecution(actCntx, actData): try: originalName = actData.getOriginalName() getCurrentName = actData.getCurrentName() if newName == getCurrentName: return newName actData.setNewName(newName) bRlt = cu.executeAction(actCntx, actData) if not bRlt: print(u'Failure Action %s' % itemAddress) else: pass except Exception as e: print(e) return newName
def RenameItem(self, unit, itemId, itemAddress, prefix=""): actCntx = ActionContext(unit, Actions.RENAME, itemId, itemAddress) actData = ActionRenameData() if unit.prepareExecution(actCntx, actData): try: getCurrentName = actData.getCurrentName() if "_" in getCurrentName: return getCurrentName originalName = actData.getOriginalName() if prefix: prefix += "_" newName = prefix + self.methodNameTransform(originalName) if not newName or newName == getCurrentName: return getCurrentName actData.setNewName(newName) bRlt = unit.executeAction(actCntx, actData) except Exception as e: print(e)