def copy_smali_class_files(dir_path): subdirs = os.listdir(dir_path) for subdir in subdirs: # Check that we are in the correct android dir (there is always a support dir) if (subdir == 'android'): if (os.path.exists(os.path.join(dir_path, subdir, 'support'))): # We copy the folder containing our .smali files helper.copy(os.path.join('smali', 'ares'), os.path.join(dir_path, subdir, 'ares')) return copy_smali_class_files(os.path.dirname(dir_path))
def compile(): print 'Compiling ' + globals.getsrc() lang = settings.langexts[globals.getext()] print 'Language detected: ' + lang if not helper.copy(os.path.join(globals.working, globals.getsrc()), globals.sandboxdir): print 'Source file not found in working directory' return False compcommand = settings.compcommands[lang] if compcommand is None: print 'No compilation necessary' return True else: compcommand = pattern.convert(compcommand) print 'Compilation command: ' + compcommand compresult = helper.runproc(compcommand, globals.sandboxdir, timelim=settings.compilelimit) if compresult[0] is None: # Time limit exceeded print 'Compile time limit exceeded ({:.2f} seconds)'.format( settings.compilelimit) return False message = compresult[3].strip() if compresult[0] != 0: # Runtime error helper.printdesc(message, 'Compile errors', settings.smalldivider, False) return False print 'Successful compile ({:.2f} seconds)'.format(compresult[1]) if len(message) > 0: # Check for warnings helper.printdesc(message, 'Compile warnings', settings.smalldivider, False) return True
def compile (): print 'Compiling ' + globals.getsrc() lang = settings.langexts[globals.getext()] print 'Language detected: ' + lang if not helper.copy(os.path.join(globals.working, globals.getsrc()), globals.sandboxdir): print 'Source file not found in working directory' return False compcommand = settings.compcommands[lang] if compcommand is None: print 'No compilation necessary' return True else: compcommand = pattern.convert(compcommand) print 'Compilation command: ' + compcommand compresult = helper.runproc(compcommand, globals.sandboxdir, timelim=settings.compilelimit) if compresult[0] is None: # Time limit exceeded print 'Compile time limit exceeded ({:.2f} seconds)'.format(settings.compilelimit) return False message = compresult[3].strip() if compresult[0] != 0: # Runtime error helper.printdesc(message, 'Compile errors', settings.smalldivider, False) return False print 'Successful compile ({:.2f} seconds)'.format(compresult[1]) if len(message) > 0: # Check for warnings helper.printdesc(message, 'Compile warnings', settings.smalldivider, False) return True
def run (): if not globals.commands['comp'](): return False print settings.meddivider stdio = globals.getmode() == 'stdio' print 'Executing problem {} in {} I/O mode'.format(globals.getprob(), globals.getmode()) files = os.listdir(globals.probdir) cases = [] for file in files: if file.endswith('.in'): cases.append(file[:-3]) print '{} test inputs found'.format(len(cases)) sandboxin = os.path.join(globals.sandboxdir, pattern.convert(settings.inputfile)) sandboxout = os.path.join(globals.sandboxdir, pattern.convert(settings.outputfile)) execcommand = pattern.convert(settings.execcommands[settings.langexts[globals.getext()]]) langlim = settings.timeconstraints[settings.langexts[globals.getext()]] outcomes = collections.OrderedDict([ ('Correct', 0), ('Runtime Error', 0), ('Time Limit Exceeded', 0), ('Presentation Error', 0), ('Wrong Answer', 0), ('No Output Produced', 0), ('No Answer File', 0) ]) print 'Time limit per input: {:.2f} seconds'.format(langlim) for case in cases: print settings.meddivider print 'Test input ' + case helper.copy(os.path.join(globals.probdir, case + '.in'), sandboxin) input = helper.read(sandboxin) execresult = helper.runproc(execcommand, globals.sandboxdir, stdin=(input if stdio else None), timelim=langlim) output = execresult[2] if stdio else helper.read(sandboxout) ans = helper.read(os.path.join(globals.probdir, case + '.ans')) wronganswer = False if execresult[0] is None: # Time limit exceeded print 'Execution time limit exceeded' outcomes['Time Limit Exceeded'] += 1 else: print 'Execution time: {:.2f} seconds'.format(execresult[1]) if execresult[0] != 0: print 'Runtime error: exit code ' + repr(execresult[0]) outcomes['Runtime Error'] += 1 elif output is None or len(output) == 0: print 'Program did not produce output' outcomes['No Output Produced'] += 1 elif ans is None: print 'No answer file provided' outcomes['No Answer File'] += 1 elif ans == output: print 'Correct' outcomes['Correct'] += 1 elif ''.join(ans.split()) == ''.join(output.split()): print 'Presentation Error (extra/missing whitespace)' outcomes['Presentation Error'] += 1 wronganswer = True else: print 'Wrong Answer' outcomes['Wrong Answer'] += 1 wronganswer = True if settings.printinput: helper.printdesc(input, 'Input', settings.smalldivider) if settings.printoutput: helper.printdesc(output, 'Output', settings.smalldivider) if wronganswer and settings.printexpected: helper.printdesc(ans, 'Expected Output', settings.smalldivider) if settings.printstdout and not stdio: helper.printdesc(execresult[2], 'Stdout', settings.smalldivider) if settings.printstderr: helper.printdesc(execresult[3], 'Stderr', settings.smalldivider) helper.write(os.path.join(globals.probdir, case + '.out'), output) if not stdio: helper.write(os.path.join(globals.probdir, case + '.stdout'), execresult[2]) helper.write(os.path.join(globals.probdir, case + '.stderr'), execresult[3]) if settings.printsummary: print settings.meddivider print 'Results Summary' for outcome in outcomes: print '{:2d} | {:<20}'.format(outcomes[outcome], outcome) print 'Total Score: {:.1f}'.format((100.0 * outcomes['Correct']) / len(cases)); return True
def copy(self, localDirectoryPath, remoteDirectoryPath, download=False): download = '' if not download else '--download' return hp.copy([ self.megaPath + 'megacopy', '--config', self.configPath, '--local', localDirectoryPath, '--remote', remoteDirectoryPath, download ])
def main(): args = parseArgs() if (args.graph): graphic_interface.main_menu() elif (args.app and args.code_to_inject): app = os.path.abspath(args.app) if (os.path.isfile(app) and app.endswith('.apk')): inject_user_input = args.code_to_inject code_to_inject = check_inject_arg(inject_user_input) head, tail = os.path.split(app) tail = os.path.splitext(tail)[0] if (code_to_inject): if (not os.getenv('JAVA_HOME') or (os.getenv('JAVA_HOME') not in os.getenv('PATH'))): nenv = os.environ.copy() nenv['JAVA_HOME'] = 'C:/Program Files/Java/jdk1.8.0_45' nenv['PATH'] += ';C:/Program Files/Java/jdk1.8.0_45/bin' print('\n::INFO Disassembly {0}...\n'.format(tail)) sp = subprocess.Popen([ 'java', '-jar', os.path.join('modules', 'baksmali.jar'), '-p', app, '-o', os.path.join('smali', tail)], shell=True, env=nenv) sp.wait() if (args.target): # For the target to be ./smali/skandiabanken-smali-mod/se just enter --target se target = os.path.join('smali', tail + '-mod', args.target) helper.copy(os.path.join('smali', tail ), os.path.join('smali', tail + '-mod')) if ((os.path.exists(target)) and (os.path.isdir(target) \ or (os.path.isfile(target) and target.endswith('.smali')))): if (os.path.isdir(target)): treat_dir(target, code_to_inject) else: treat_file(target, code_to_inject) else: print('\n::WARN {0} does not exist/is not valid, please try again... \n' .format(target)) sys.exit(0) else: target = os.path.join('smali', tail + '-mod') helper.copy(os.path.join('smali', tail), target) treat_dir(target, code_to_inject) print('::INFO Assembly {0} into classes.dex...'.format(os.path.join('smali', tail + '-mod'))) sp1 = subprocess.Popen(['java', '-jar', os.path.join('modules', 'smali.jar'), os.path.join('smali', tail + '-mod'), '-o', os.path.join('apps', 'classes.dex')], shell=True, env=nenv) sp1.wait() # We create an app-mod folder and erase its certificates zfile = zipfile.ZipFile(app).extractall(os.path.join('apps', tail + '-mod')) for root, subdirs, files in os.walk(os.path.join('apps', tail + '-mod', 'META-INF')): for file in files: file_path = os.path.join('apps', tail + '-mod', 'META-INF', file) if (file != 'MANIFEST.MF' and ('.RSA' in file or '.SF' in file)): os.remove(os.path.abspath(file_path)) # We move the new classes.dex to the app-mod folder helper.move(os.path.join('apps' ,'classes.dex'), os.path.join('apps', tail + '-mod', 'classes.dex')) # We create the app-mod.zip, rename it to app-mod.apk and remove the app-mod folder shutil.make_archive(os.path.join('apps', tail + '-mod'), 'zip', os.path.join('apps', tail + '-mod')) helper.move(os.path.join('apps', tail + '-mod.zip'), os.path.join('apps', tail + '-mod.apk')) shutil.rmtree(os.path.join('apps', tail + '-mod')) # We sign the app-mod.apk with our certificate print('::INFO Signing {0}... \n'.format(tail + '-mod.apk')) sp1 = subprocess.Popen(['jarsigner', '-verbose', '-sigalg', 'SHA1withRSA', '-digestalg', 'SHA1', '-keystore', './modules/my-release-key.keystore', '-storepass', 'password', os.path.join('apps', tail + '-mod.apk'), 'alias_name'], shell=True, stdout=subprocess.DEVNULL, env=nenv) sp1.wait() else: print('\n::WARN Incorrect usage [-i]: smali-code-injector -h for help \n') sys.exit(0) else: print('::WARN {0} does not exist/is not valid, please try again... \n' .format(app)) sys.exit(0) elif (args.target and args.code_to_inject): target = args.target if ((os.path.exists(target)) and (os.path.isdir(target) or \ (os.path.isfile(target) and target.endswith('.smali')))): inject_user_input = args.code_to_inject code_to_inject = check_inject_arg(inject_user_input) if (code_to_inject): if (os.path.isdir(target)): treat_dir(target, code_to_inject) else: treat_file(target, code_to_inject) else: print('Incorrect usage [-i]: smali-code-injector -h for help\n') sys.exit(0) else: print('\n::WARN {0} does not exist/is not valid, please try again... \n' .format(target)) sys.exit(0) elif (args.target and args.check): target = args.target if ((os.path.exists(target)) and (os.path.isdir(target) or (os.path.isfile(target) and target.endswith('.smali')))): if (os.path.isdir(target)): print('\n::INFO Creation of metadata...') my_metadata = metadata.create_metadata(target) metadata.check_dir(os.path.basename(target), my_metadata) else: print('\n::INFO Creation of metadata...') file_metadata = metadata.create_file_metadata(target) metadata.check_file(os.path.basename(target), file_metadata) else: print('::WARN Incorrect usage: smali-code-injector -h for help \n') sys.exit(0)