def test_colorize_methods(): """Test colorize convenience methods.""" assert Color.black("TEST").value_colors == "\033[30mTEST\033[39m" assert Color.bgblack("TEST").value_colors == "\033[40mTEST\033[49m" assert Color.red("TEST").value_colors == "\033[31mTEST\033[39m" assert Color.bgred("TEST").value_colors == "\033[41mTEST\033[49m" assert Color.green("TEST").value_colors == "\033[32mTEST\033[39m" assert Color.bggreen("TEST").value_colors == "\033[42mTEST\033[49m" assert Color.yellow("TEST").value_colors == "\033[33mTEST\033[39m" assert Color.bgyellow("TEST").value_colors == "\033[43mTEST\033[49m" assert Color.blue("TEST").value_colors == "\033[34mTEST\033[39m" assert Color.bgblue("TEST").value_colors == "\033[44mTEST\033[49m" assert Color.magenta("TEST").value_colors == "\033[35mTEST\033[39m" assert Color.bgmagenta("TEST").value_colors == "\033[45mTEST\033[49m" assert Color.cyan("TEST").value_colors == "\033[36mTEST\033[39m" assert Color.bgcyan("TEST").value_colors == "\033[46mTEST\033[49m" assert Color.white("TEST").value_colors == "\033[37mTEST\033[39m" assert Color.bgwhite("TEST").value_colors == "\033[47mTEST\033[49m" assert Color.black("this is a test.", auto=True) == Color("{autoblack}this is a test.{/autoblack}") assert Color.black("this is a test.") == Color("{black}this is a test.{/black}") assert Color.bgblack("this is a test.", auto=True) == Color("{autobgblack}this is a test.{/autobgblack}") assert Color.bgblack("this is a test.") == Color("{bgblack}this is a test.{/bgblack}") assert Color.red("this is a test.", auto=True) == Color("{autored}this is a test.{/autored}") assert Color.red("this is a test.") == Color("{red}this is a test.{/red}") assert Color.bgred("this is a test.", auto=True) == Color("{autobgred}this is a test.{/autobgred}") assert Color.bgred("this is a test.") == Color("{bgred}this is a test.{/bgred}") assert Color.green("this is a test.", auto=True) == Color("{autogreen}this is a test.{/autogreen}") assert Color.green("this is a test.") == Color("{green}this is a test.{/green}") assert Color.bggreen("this is a test.", auto=True) == Color("{autobggreen}this is a test.{/autobggreen}") assert Color.bggreen("this is a test.") == Color("{bggreen}this is a test.{/bggreen}") assert Color.yellow("this is a test.", auto=True) == Color("{autoyellow}this is a test.{/autoyellow}") assert Color.yellow("this is a test.") == Color("{yellow}this is a test.{/yellow}") assert Color.bgyellow("this is a test.", auto=True) == Color("{autobgyellow}this is a test.{/autobgyellow}") assert Color.bgyellow("this is a test.") == Color("{bgyellow}this is a test.{/bgyellow}") assert Color.blue("this is a test.", auto=True) == Color("{autoblue}this is a test.{/autoblue}") assert Color.blue("this is a test.") == Color("{blue}this is a test.{/blue}") assert Color.bgblue("this is a test.", auto=True) == Color("{autobgblue}this is a test.{/autobgblue}") assert Color.bgblue("this is a test.") == Color("{bgblue}this is a test.{/bgblue}") assert Color.magenta("this is a test.", auto=True) == Color("{automagenta}this is a test.{/automagenta}") assert Color.magenta("this is a test.") == Color("{magenta}this is a test.{/magenta}") assert Color.bgmagenta("this is a test.", auto=True) == Color("{autobgmagenta}this is a test.{/autobgmagenta}") assert Color.bgmagenta("this is a test.") == Color("{bgmagenta}this is a test.{/bgmagenta}") assert Color.cyan("this is a test.", auto=True) == Color("{autocyan}this is a test.{/autocyan}") assert Color.cyan("this is a test.") == Color("{cyan}this is a test.{/cyan}") assert Color.bgcyan("this is a test.", auto=True) == Color("{autobgcyan}this is a test.{/autobgcyan}") assert Color.bgcyan("this is a test.") == Color("{bgcyan}this is a test.{/bgcyan}") assert Color.white("this is a test.", auto=True) == Color("{autowhite}this is a test.{/autowhite}") assert Color.white("this is a test.") == Color("{white}this is a test.{/white}") assert Color.bgwhite("this is a test.", auto=True) == Color("{autobgwhite}this is a test.{/autobgwhite}") assert Color.bgwhite("this is a test.") == Color("{bgwhite}this is a test.{/bgwhite}")
def test_colorize_methods(): """Test colorize convenience methods.""" assert Color.black('TEST').value_colors == '\033[30mTEST\033[39m' assert Color.bgblack('TEST').value_colors == '\033[40mTEST\033[49m' assert Color.red('TEST').value_colors == '\033[31mTEST\033[39m' assert Color.bgred('TEST').value_colors == '\033[41mTEST\033[49m' assert Color.green('TEST').value_colors == '\033[32mTEST\033[39m' assert Color.bggreen('TEST').value_colors == '\033[42mTEST\033[49m' assert Color.yellow('TEST').value_colors == '\033[33mTEST\033[39m' assert Color.bgyellow('TEST').value_colors == '\033[43mTEST\033[49m' assert Color.blue('TEST').value_colors == '\033[34mTEST\033[39m' assert Color.bgblue('TEST').value_colors == '\033[44mTEST\033[49m' assert Color.magenta('TEST').value_colors == '\033[35mTEST\033[39m' assert Color.bgmagenta('TEST').value_colors == '\033[45mTEST\033[49m' assert Color.cyan('TEST').value_colors == '\033[36mTEST\033[39m' assert Color.bgcyan('TEST').value_colors == '\033[46mTEST\033[49m' assert Color.white('TEST').value_colors == '\033[37mTEST\033[39m' assert Color.bgwhite('TEST').value_colors == '\033[47mTEST\033[49m' assert Color.black( 'this is a test.', auto=True) == Color('{autoblack}this is a test.{/autoblack}') assert Color.black('this is a test.') == Color( '{black}this is a test.{/black}') assert Color.bgblack( 'this is a test.', auto=True) == Color('{autobgblack}this is a test.{/autobgblack}') assert Color.bgblack('this is a test.') == Color( '{bgblack}this is a test.{/bgblack}') assert Color.red('this is a test.', auto=True) == Color('{autored}this is a test.{/autored}') assert Color.red('this is a test.') == Color('{red}this is a test.{/red}') assert Color.bgred( 'this is a test.', auto=True) == Color('{autobgred}this is a test.{/autobgred}') assert Color.bgred('this is a test.') == Color( '{bgred}this is a test.{/bgred}') assert Color.green( 'this is a test.', auto=True) == Color('{autogreen}this is a test.{/autogreen}') assert Color.green('this is a test.') == Color( '{green}this is a test.{/green}') assert Color.bggreen( 'this is a test.', auto=True) == Color('{autobggreen}this is a test.{/autobggreen}') assert Color.bggreen('this is a test.') == Color( '{bggreen}this is a test.{/bggreen}') assert Color.yellow( 'this is a test.', auto=True) == Color('{autoyellow}this is a test.{/autoyellow}') assert Color.yellow('this is a test.') == Color( '{yellow}this is a test.{/yellow}') assert Color.bgyellow( 'this is a test.', auto=True) == Color('{autobgyellow}this is a test.{/autobgyellow}') assert Color.bgyellow('this is a test.') == Color( '{bgyellow}this is a test.{/bgyellow}') assert Color.blue( 'this is a test.', auto=True) == Color('{autoblue}this is a test.{/autoblue}') assert Color.blue('this is a test.') == Color( '{blue}this is a test.{/blue}') assert Color.bgblue( 'this is a test.', auto=True) == Color('{autobgblue}this is a test.{/autobgblue}') assert Color.bgblue('this is a test.') == Color( '{bgblue}this is a test.{/bgblue}') assert Color.magenta( 'this is a test.', auto=True) == Color('{automagenta}this is a test.{/automagenta}') assert Color.magenta('this is a test.') == Color( '{magenta}this is a test.{/magenta}') assert Color.bgmagenta( 'this is a test.', auto=True) == Color('{autobgmagenta}this is a test.{/autobgmagenta}') assert Color.bgmagenta('this is a test.') == Color( '{bgmagenta}this is a test.{/bgmagenta}') assert Color.cyan( 'this is a test.', auto=True) == Color('{autocyan}this is a test.{/autocyan}') assert Color.cyan('this is a test.') == Color( '{cyan}this is a test.{/cyan}') assert Color.bgcyan( 'this is a test.', auto=True) == Color('{autobgcyan}this is a test.{/autobgcyan}') assert Color.bgcyan('this is a test.') == Color( '{bgcyan}this is a test.{/bgcyan}') assert Color.white( 'this is a test.', auto=True) == Color('{autowhite}this is a test.{/autowhite}') assert Color.white('this is a test.') == Color( '{white}this is a test.{/white}') assert Color.bgwhite( 'this is a test.', auto=True) == Color('{autobgwhite}this is a test.{/autobgwhite}') assert Color.bgwhite('this is a test.') == Color( '{bgwhite}this is a test.{/bgwhite}')
def _process_mtd(self, mtd): if DEBUG_MODE: print('\n', '+' * 100) print('Starting to decode ...') print(Color.green(mtd)) # 如果存在数组 array_data_content = [] arr_res = self.arr_data_ptn.search(mtd.get_body()) if arr_res: array_data_content = re.split(r'\n\s', arr_res.group()) lines = re.split(r'\n\s*', mtd.get_body()) old_body = lines.copy() # 存放原始方法体 new_body = [] # 存放解密后的方法体 snippet = [] # 存放smali代码,用于模拟执行 args = {} # 存放方法参数,用于smaliemu执行 index = -1 # 用于计数 xget_opcodes = {'iget', 'iget-object', 'sget', 'sget-object'} block_args = {'first': {}} # 保存所有分支的执行结果 last_block_key = 'first' # 上一个分支-关键字 this_block_key = 'first' # 当前分支,默认第一分支 keys = ['first'] # 默认执行第一分支 for line in lines: index += 1 if not line: continue new_body.append(line) # 解密结果,直接放后面即可 if DEBUG_MODE: print(Color.blue(line)) parts = line.split() opcode = parts[0] # smali 代码分块执行 # 命中下述关键字,则表示当前分支结束 # 并根据上一个分支的情况,判断之前的分支是否可用 if 'if-' in opcode: if DEBUG_MODE: print('>' * 10, opcode) print('this_block_key', this_block_key) print('last_block_key', last_block_key) print('block_args', block_args) # 存在两种情况 # 1. 当前代码片段(if语句之前的代码),还没执行;全部执行一次 # 2. 当前代码片段,已经执行了一部分,因为解密;从执行后的地方开始执行 pre_args = {} if this_block_key in last_block_key: for key in reversed(keys): if this_block_key not in key: pre_args = block_args[key].copy() break else: pre_args = block_args[last_block_key].copy() if this_block_key in block_args: pre_args.update(block_args[this_block_key]) snippet.extend(array_data_content) self.emu.call(snippet, args=pre_args, cv=True, thrown=False) block_args[this_block_key] = self.emu.vm.variables snippet.clear() last_block_key = this_block_key this_block_key = 'if' + parts[-1] # 表示接下来跑的代码块是这个语句的 keys.append(this_block_key) if DEBUG_MODE: print('block_args - 运行后', block_args) continue elif 'goto' in opcode: # 跳转语句,直接跳过 continue elif opcode.startswith(':cond_')\ or opcode.startswith(':try_start')\ or opcode.startswith('.catch_'): if DEBUG_MODE: print('>' * 10, opcode) print('this_block_key', this_block_key) print('last_block_key', last_block_key) print('block_args', block_args) # 存在两种情况 # 1. 当前代码片段,还没执行;全部执行一次 # 2. 当前代码片段,已经执行了一部分,因为解密;从执行后的地方开始执行 pre_args = block_args[last_block_key].copy() if this_block_key in block_args: pre_args.update(block_args[this_block_key]) snippet.extend(array_data_content) self.emu.call(snippet, args=pre_args, cv=True, thrown=False) block_args[this_block_key] = self.emu.vm.variables snippet.clear() last_block_key = this_block_key this_block_key = opcode # 表示接下来跑的代码块是这个语句的 keys.append(this_block_key) if DEBUG_MODE: print('block_args - 运行后', block_args) continue elif opcode.startswith(':try_start'): pass elif '.catch_' in opcode: # 前面代码当成一块处理 continue snippet.append(line) is_static = True if opcode == 'invoke-static': result = self.process_invoke_static_statement(line) if result: cname, mname, ptypes, rtype, rnames = result else: continue # elif opcode == 'invoke-virtual': # TODO 实例方法,目前只考虑无参实例化。 # result = self.process_invoke_static_statement(line) # if result: # cname, mname, ptypes, rtype, rnames = result # print(result) # # 判断类的构造函数是否为<init>()V # clz = self.smalidir.get_method( # java2smali(cname), '<init>()V') # if not clz: # continue # is_static = False # else: # continue elif opcode in xget_opcodes: self.process_xget_statement(line) continue elif 'Ljava/lang/String;-><init>([B)V' in line: if 'move-result-object' in snippet[0]: snippet = snippet[1:] self.emu.call(snippet, args=args, cv=True, thrown=False) if not self.emu.vm.result: continue # 如果有结果,则替换 vx, _ = SmaliLine.parse(line) new_line = 'const-string {}, "{}"'.format( vx, self.emu.vm.result) del new_body[-1] new_body.append(new_line) self.make_changes = True mtd.set_modified(True) snippet.clear() continue else: continue # 模拟执行,获取解密参数 del snippet[-1] snippet.extend(array_data_content) try: snippet = self.process_if_statement(snippet) if DEBUG_MODE: print(Color.red('开始处理解密参数 {}'.format(line))) for l in snippet: print(Color.red(l)) print('args', args) print(block_args) print(keys) print(this_block_key) print('-' * 80) pre_args = block_args[last_block_key].copy() args.update(pre_args) if this_block_key in block_args: args.update(block_args[this_block_key]) args.update(self.fields) self.emu.call(snippet, args=args, cv=True, thrown=False) registers = self.emu.vm.variables block_args[this_block_key] = registers if DEBUG_MODE: print(snippet) print('args:', args) print('smali执行后,寄存器内容', registers) if registers: for k, v in registers.items(): if v is None: continue args[k] = v registers = args except TIMEOUT_EXCEPTION: snippet.clear() continue print(Color.red('->')) obj_flag = False if len(ptypes) == 1 and ptypes[0][0] == 'L' and ptypes != [ 'Ljava/lang/String;' ]: # 单独处理参数为对象的情况 obj_flag = True snippet.clear() # 已经执行过的代码,不再执行 if not registers and not obj_flag: continue print(Color.red('->>')) # 从寄存器中获取对应的参数 # 参数获取 "arguments": ["I:198", "I:115", "I:26"]} arguments = [] # args = {} # the parameter of smali method if not obj_flag: ridx = -1 for item in ptypes: ridx += 1 rname = rnames[ridx] if rname not in registers: break value = registers[rnames[ridx]] argument = self.convert_args(item, value) if argument is None: break arguments.append(argument) else: arguments.append('Object:' + smali2java(ptypes[0])) if DEBUG_MODE: print(Color.red('->>')) print('参数类型', ptypes) print('参数值', arguments) if len(arguments) != len(ptypes): print(Color.red('->> 参数对不上')) continue json_item = self.get_json_item(cname, mname, arguments) print(json_item) # print('生成json item') # {id}_{rtn_name} 让这个唯一化,便于替换 old_content = '# %s' % json_item['id'] # 如果 move_result_obj 操作存在的话,解密后一起替换 find = self.move_result_obj_ptn.search(lines[index + 1]) print(Color.red('->> not fount')) # 必须要找到返回值操作,用于更新寄存器 if not find: print('找不到返回寄存器') continue print(Color.red('->>>')) rtn_name = find.groups()[0] # 为了避免 '# abc_v10' 替换成 '# abc_v1' old_content = old_content + '_' + rtn_name + 'X' self.append_json_item(json_item, mtd, old_content, rtn_name) result = self.get_result(rtype) if DEBUG_MODE: print("解密结果", result) self.json_list.clear() if result: new_body = new_body[:-1] else: continue if not args: args = {} if rtype == 'Ljava/lang/String;': result = list(result.values())[0][0] # 更新寄存器 args[rtn_name] = result # 更新代码 new_line = 'const-string {}, "{}"'.format(rtn_name, result) new_body.append(new_line) self.make_changes = True mtd.set_modified(True) elif rtype.startswith('['): args[rtn_name] = result # 把结果保存到当前分支 else: print("返回值并非字符串,也不是B/C数组") if args: block_args[this_block_key].update(args) # 把结果保存到当前分支 if DEBUG_MODE: print(block_args) print('*' * 100) # print('last_block_key', last_block_key) # print('this_block_key', this_block_key) # # pre_args = block_args[last_block_key].copy() # if this_block_key in block_args: # pre_args.update(block_args[this_block_key]) # block_args[this_block_key] = mtd.set_body('\n'.join(new_body))