root_name, ext = os.path.splitext(apk_name) if ext != ".apk": print "error: not an APK file" sys.exit(2) a = apk.APK(args.filename) if args.output: outdir = args.output[0] else: outdir = os.path.dirname(args.filename) api_config = args.api db_path = os.path.join(working_dir, "androidlib") mo = monitor.APIMonitor(db_path, config=api_config) new_apk = os.path.join(outdir, root_name + "_new.apk") outdir = os.path.join(outdir, "apimonitor_out") if os.path.exists(outdir): shutil.rmtree(outdir) os.makedirs(outdir) dexpath = os.path.join(outdir, "origin.dex") smalidir = os.path.join(outdir, "origin_smali") new_dexpath = os.path.join(outdir, "new.dex") new_smalidir = os.path.join(outdir, "new_smali") min_version = int(a.get_min_sdk_version()) if a.get_target_sdk_version():
a = apk.APK(APK) dex_file = open(DEX, 'w') dex_file.write(a.get_dex()) dex_file.close() call(args=['baksmali', '-b', '-o', SMALI_DIR, DEX]) s = smali.SmaliTree(SMALI_DIR) api_list = [] perms = a.get_permissions() for p in perms: print p if API_BY_PERMISSION.has_key(p): for ml in API_BY_PERMISSION[p].values(): api_list.extend(ml) mo = monitor.APIMonitor(api_list) s = mo.inject(s) s.save(NEW_OUT) call(args=['smali', '-a', '7', '-o', NEW_DEX, NEW_OUT]) new_dex = open(NEW_DEX).read(); a.new_zip(filename=NEW_APK, deleted_files="(META-INF/.)", new_files = { "classes.dex" : new_dex } ) apk.sign_apk( NEW_APK, \ "/Users/kelwin/Dropbox/Backup/apkil", "apkil", "apkilapkil" )
db_path = os.path.join(working_dir, "androidlib") API_LIST = [ \ "Landroid/net/Uri;->parse(Ljava/lang/String;)", \ "Landroid/content/Intent;-><init>(Ljava/lang/String;)", \ "Landroid/content/ContextWrapper;->openFileOutput(Ljava/lang/String;I)", \ "Ljava/io/OutputStreamWriter;->write(Ljava/lang/String;)", \ "Lapkil/tests/APKIL;->openFileInput(Ljava/lang/String;)", "Ljava/io/BufferedReader;->readLine()Ljava/lang/String;", \ "Landroid/telephony/SmsManager;->sendTextMessage(\ Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;\ Landroid/app/PendingIntent;Landroid/app/PendingIntent;)" , \ "Landroid/content/pm/PackageManager;->getInstalledApplications(I)", ] mo = monitor.APIMonitor(db_path, API_LIST) API_CONFIG = "config/default_api_collection" mo = monitor.APIMonitor(db_path, config=API_CONFIG) s = mo.inject(s, min_version) s.save(NEW_OUT) call(args=[ 'java', '-jar', 'smali/smali.jar', '-a', str(min_version), '-o', NEW_DEX, NEW_OUT ]) new_dex = open(NEW_DEX).read() a.new_zip(filename=NEW_APK, deleted_files="(META-INF/.)",
#"Ljava/io/BufferedReader;->readLine()Ljava/lang/String;", \ # new intent ] TEMPLATE = "template/APIMonitor" EXPORT_FOLDER = "examples/APIMonitor/java" if os.path.exists(EXPORT_FOLDER): shutil.rmtree(EXPORT_FOLDER) shutil.copytree(TEMPLATE, EXPORT_FOLDER) m = monitor.APIMonitor(API_LIST) m.export(os.path.join(EXPORT_FOLDER, "src")) # sys.exit(0) call(args=["android", "update", "project", "--path", EXPORT_FOLDER]) call(args=["ant", "debug", "-buildfile", \ os.path.join(EXPORT_FOLDER, "build.xml")]) sys.exit(0) dex_file_path = os.path.join(EXPORT_FOLDER, "bin", "classes.dex") MONITOR_SMALI = "examples/APIMonitor/smali" call(args=['baksmali', '-b', '-o', MONITOR_SMALI, dex_file_path]) m_s = smali.SmaliTree(MONITOR_SMALI)