Permalink
Browse files

Allowed running apimonitor from arbitrary directory

  • Loading branch information...
1 parent a16f5d2 commit 3b224ae8cfe9243bbb5af4920e23309d49fbf600 @kelwin committed Aug 26, 2012
Showing with 31 additions and 12 deletions.
  1. +15 −7 apimonitor.py
  2. +9 −3 apkil/monitor.py
  3. +7 −2 test.py
View
@@ -25,7 +25,9 @@
from apkil import smali, monitor, logger
from subprocess import call
-working_dir=sys.path[0]
+working_dir = sys.path[0]
+default_api = os.path.join(working_dir, "config", "default_api_collection")
+
parser = argparse.ArgumentParser(description=\
'Repackage apk to monitor arbitrary APIs.')
parser.add_argument('-o, --output', metavar='dirpath', type=str, nargs=1,
@@ -35,7 +37,7 @@
help='target API level for instrumentation',
dest='level')
parser.add_argument('-a, --api', metavar='apilist', type=str,
- default="{}/config/default_api_collection".format(working_dir),
+ default=default_api,
help='config file of API list',
dest='api')
parser.add_argument('-v, --version', action='version',
@@ -58,7 +60,9 @@
outdir = os.path.dirname(args.filename)
api_config = args.api
-mo = monitor.APIMonitor(config=api_config)
+
+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")
@@ -89,20 +93,24 @@
dex_file.write(a.get_dex())
dex_file.close()
-call(args=['java', '-jar', '{}/smali/baksmali.jar'.format(working_dir),
+smali_jar = os.path.join(working_dir, "smali", "smali.jar")
+baksmali_jar = os.path.join(working_dir, "smali", "baksmali.jar")
+cert_path = os.path.join(working_dir, "config", "apkil.cert")
+
+call(args=['java', '-jar', baksmali_jar,
'-b', '-o', smalidir, dexpath])
s = smali.SmaliTree(level, smalidir)
s = mo.inject(s, level)
s.save(new_smalidir)
-call(args=['java', '-jar', '{}/smali/smali.jar'.format(working_dir),
+call(args=['java', '-jar', smali_jar,
'-a', str(level), '-o', new_dexpath, new_smalidir])
new_dex = open(new_dexpath).read();
a.new_zip(filename=new_apk,
deleted_files="(META-INF/.)", new_files = {
"classes.dex" : new_dex } )
-apk.sign_apk(new_apk, \
-"config/apkil.cert", "apkil", "apkilapkil" )
+apk.sign_apk(new_apk, cert_path, "apkil", "apkilapkil" )
+print "NEW APK: %s" % new_apk
View
@@ -122,7 +122,8 @@
class APIMonitor(object):
- def __init__(self, entries=[], config=""):
+ def __init__(self, db_dir, entries=[], config=""):
+ self.db_dir = ""
self.entries = []
self.method_descs = []
self.config = ""
@@ -134,6 +135,7 @@ def __init__(self, entries=[], config=""):
self.helper = ClassNode(buf=DEFAULT_HELPER)
self.android_api = None
+ self.db_dir = db_dir
self.entries = entries
if (not entries) and config:
if os.path.isfile(config):
@@ -160,11 +162,15 @@ def __repr__(self):
return '\n'.join(self.method_descs)
def load_api(self, level):
+ if level > 16:
+ level = 16
+ elif level < 3:
+ level = 3
self.android_api = AndroidAPI()
- data_path = os.path.join("androidlib", "android-%d.db" % level)
+ data_path = os.path.join(self.db_dir, "android-%d.db" % level)
while not os.path.exists(data_path):
level += 1
- data_path = os.path.join("androidlib", "android-%d.db" % level)
+ data_path = os.path.join(self.db_dir, "android-%d.db" % level)
self.android_api.load(data_path)
return level
View
@@ -2,11 +2,14 @@
# -*- coding: utf-8 -*-
import sys
+import os
import time
from androguard.core.bytecodes import apk
from apkil import smali, monitor, logger
from subprocess import call
+working_dir = sys.path[0]
+
APK = "examples/APKILTests.apk"
DEX = "examples/APKILTests.dex"
SMALI_DIR = "examples/APKILTests"
@@ -26,6 +29,8 @@
call(args=['java', '-jar', 'smali/baksmali.jar', '-b', '-o', SMALI_DIR, DEX])
s = smali.SmaliTree(min_version, SMALI_DIR)
+db_path = os.path.join(working_dir, "androidlib")
+
API_LIST = [ \
"Landroid/net/Uri;->parse(Ljava/lang/String;)", \
"Landroid/content/Intent;-><init>(Ljava/lang/String;)", \
@@ -38,10 +43,10 @@
Landroid/app/PendingIntent;Landroid/app/PendingIntent;)", \
"Landroid/content/pm/PackageManager;->getInstalledApplications(I)",
]
-mo = monitor.APIMonitor(API_LIST)
+mo = monitor.APIMonitor(db_path, API_LIST)
API_CONFIG = "config/default_api_collection"
-mo = monitor.APIMonitor(config=API_CONFIG)
+mo = monitor.APIMonitor(db_path, config=API_CONFIG)
s = mo.inject(s, min_version)
s.save(NEW_OUT)

0 comments on commit 3b224ae

Please sign in to comment.