Skip to content

Instantly share code, notes, and snippets.

@geekman geekman/extract.py
Created Dec 24, 2014

Embed
What would you like to do?
mass-extract permissions & features from Android manifest
#!/usr/bin/env python
#
# extract permissions and features from Android manifest
# put this script into the androguard directory
#
import os
import sys
import csv
import traceback
from androguard.core.bytecodes import apk
def process_apk(filename):
results = {'_file': os.path.basename(filename)}
try:
a = apk.APK(filename)
features = a.get_elements('uses-feature', 'name')
perms = a.get_permissions()
# if uses-feature describes a GL version, there will be no name
if '' in features:
features.remove('')
results.update(dict(('p_' + p, 1) for p in perms))
results.update(dict(('f_' + f, 1) for f in features))
except:
traceback.print_exc()
results['_error'] = 1
return results
if __name__ == '__main__':
output_file = sys.argv[1]
assert not os.path.exists(output_file), 'output file %s already exists!' % output_file
files = []
for f in sys.argv[2:]:
if os.path.isfile(f):
files.append(f)
elif os.path.isdir(f):
files.extend(os.listdir(f))
results = []
columns = set()
for f in files:
print >> sys.stderr, 'processing %s...' % f
r = process_apk(f)
results.append(r)
columns.update(r.keys())
columns = sorted(list(columns))
# write data into output file
outf = open(output_file, 'wb')
outf_csv = csv.writer(outf)
outf_csv.writerow(columns)
for row in results:
outf_csv.writerow([row.get(k, '') for k in columns])
outf.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.