Skip to content

Commit

Permalink
A massive restructuring. If someone wants to get the old vesion, get …
Browse files Browse the repository at this point in the history
…to 3 commits before this.
  • Loading branch information
Rahul Chatterjee committed May 10, 2018
1 parent 1d4d3fb commit 654e824
Show file tree
Hide file tree
Showing 22 changed files with 3,302 additions and 97 deletions.
Binary file added a.db
Binary file not shown.
1 change: 1 addition & 0 deletions blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def store_str(st):

def app_title_and_flag(apps):
_td = apps.merge(APP_FLAGS, on='appId', how="left").set_index('appId')
_td['rawflags'] = ''
_td['flags'] = (_td['store'].apply(store_str) + '-' + _td['flag']).fillna('').apply(lambda x: [x] if x else [])
# print(apps, flagged_apps)
spy_regex_app = _td.index.map(_regex_blacklist).values | _td.title.fillna('').apply(_regex_blacklist).values
Expand Down
15 changes: 11 additions & 4 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path
import os
import shlex
from sys import platform

DEV_SUPPRTED = ['android', 'ios'] # 'windows', 'mobileos', later

Expand All @@ -14,7 +15,7 @@

# ---------------------------------------------------------
DEBUG = True
TEST = False
TEST = True


TEST_APP_LIST = 'static_data/android.test.apps_list'
Expand Down Expand Up @@ -44,18 +45,24 @@ def set_test_mode(test):


THISDIR = os.path.dirname(os.path.abspath(__file__))
ANDROID_HOME = os.getenv('ANDROID_HOME', os.path.join(THISDIR, './static_data'))
ADB_PATH = shlex.quote(os.path.join(ANDROID_HOME, 'adb'))
STATIC_DATA = os.path.join(THISDIR, 'static_data')
ANDROID_HOME = os.getenv('ANDROID_HOME', STATIC_DATA)
PLATFORM = 'darwin' if platform == 'darwin' else 'linux' if platform.startswith('linux') \
else 'win32' if platform == 'win32' else None
ADB_PATH = shlex.quote(os.path.join(ANDROID_HOME, 'adb-' + PLATFORM))
# MOBILEDEVICE_PATH = 'mobiledevice'
# MOBILEDEVICE_PATH = os.path.join(THISDIR, "mdf") #'python2 -m MobileDevice'
MOBILEDEVICE_PATH = shlex.quote(os.path.join(THISDIR, "static_data/ios-deploy")) #'python2 -m MobileDevice'
MOBILEDEVICE_PATH = shlex.quote(os.path.join(STATIC_DATA, "/ios-deploy-" + PLATFORM))

DUMP_DIR = os.path.join(THISDIR, 'phone_dumps')

ERROR_LOG = []

APPROVED_INSTALLERS = {'com.android.vending', }

REPORT_PATH = os.path.join(THISDIR, 'reports')
if not os.path.exists(REPORT_PATH):
os.mkdir(REPORT_PATH)

def add_to_error(*args):
global ERROR_LOG
Expand Down
153 changes: 149 additions & 4 deletions db.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,164 @@
import sqlite3
import config
from flask import g
from datetime import datetime as dt
import config
import os
import pandas as pd

DATABASE = config.SQL_DB_PATH.replace('sqlite:///', '')


def today():
db = get_db()
t = dt.now()
today = t.strftime('%Y%m%d:%H')
return today


def new_client_id():
last_client_id = query_db(
'select max(clientid) as cid from scan_res '\
'where time > datetime("now", "localtime", "-1 day")',
one=True
)['cid']
d, t = today(), 0
print("new_client_id >>>> {}".format(last_client_id))
if last_client_id:
d, t = last_client_id.split('_')
return '{}_{:03d}'.format(d, int(t)+1)


def make_dicts(cursor, row):
return dict((cursor.description[idx][0], value)
for idx, value in enumerate(row))

DATABASE = config.SQL_DB_PATH


def get_db():
db = getattr(g, '_database', None)
if db is None:
print("Creating new db connection {}".format(DATABASE))
db = g._database = sqlite3.connect(DATABASE)
db.row_factory = make_dicts
return db

def init_db(app, force=False):
with app.app_context():
db = get_db()
if force or not os.path.exists(DATABASE):
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()

def init_db(app):

def insert(query, args):
db = get_db()
cur = db.execute(query, args)
lrowid = cur.lastrowid
cur.close()
db.commit()
return lrowid


def insert_many(query, argss):
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
cur = db.executemany(query, argss)
lrowid = cur.lastrowid
cur.close()
db.commit()
return lrowid


def query_db(query, args=(), one=False):
cur = get_db().execute(query, args)
rv = cur.fetchall()
lrowid = cur.lastrowid
cur.close()
return (rv[0] if rv else None) if one else rv


def save_note(scanid, note):
insert("update scan_res set note=? where id=?",
args=(note, scanid))
return True


def create_scan(clientid, serial, device):
"""
@scanr must have following fields.
"""
return insert(
"insert into scan_res "\
"(clientid, serial, device) "\
"values (?, ?, ?)",
args=(clientid, serial, device),
)


def update_appinfo(scanid, appid, remark, action):
return insert("update app_info set "\
"remark=?, action=? where scanid=? and appid=?",
args=(remark, action, scanid, appid),
)


def update_app_deleteinfo(scanid, appid, remark):
return insert("update app_info set "\
"remark=? here scanid=? and appid=?",
args=(remark, action, scanid, appid),
)


def update_mul_appinfo(args):
return insert_many("update app_info set "\
"remark=? where scanid=? and appid=?",
args
)


def create_appinfo(scanid, appid, flags, remark='', action='<new>'):
"""
@scanr must have following fields.
"""
return insert(
"insert into app_info (scanid, appid, flags, remark, action_taken) "
"values (?,?,?,?,?)",
args=(scanid, appid, flags, remark, action)
)

def create_mult_appinfo(args):
"""
"""
return insert_many("insert into app_info (scanid, appid, flags, remark, action_taken) values (?,?,?,?,?)",
args)



def get_device_from_db(scanid):
d = query_db('select device from scan_res where id=?', args=(scanid,), one=True)
if d:
return d['device']
else:
return ''

def get_serial_from_db(scanid):
d = query_db('select serial from scan_res where id=?', args=(scanid,), one=True)
if d:
return d['serial']
else:
return ''

def create_report(clientid):
"""
Creates a report for a clientid
"""
reportf = os.path.join(config.REPORT_PATH, clientid + '.csv')
d = pd.DataFrame(query_db("select * from scan_res inner join app_info on "
"scan_res.id=app_info.scanid where scan_res.clientid=?",
args=(clientid,)))
d.to_csv(reportf, index=None)
return d

Empty file added logs/.dumy
Empty file.
15 changes: 6 additions & 9 deletions phone_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import re
import shlex

db = dataset.connect(config.SQL_DB_PATH)


class AppScan(object):
device_type = ''
Expand Down Expand Up @@ -102,17 +100,17 @@ def find_spyapps(self, serialno):
r['class_'] = r.flags.apply(blacklist.assign_class)
r['score'] = r.flags.apply(blacklist.score)
r['title'] = r.title.str.encode('ascii', errors='ignore').str.decode('ascii')
r['flags'] = r.flags.apply(blacklist.flag_str)
r['html_flags'] = r.flags.apply(blacklist.flag_str)
r.sort_values(by=['score', 'appId'], ascending=[False, True], inplace=True, na_position='last')
r.set_index('appId', inplace=True)
return r[['title', 'flags', 'score', 'class_']]
return r[['title', 'flags', 'score', 'class_', 'html_flags']]

def flag_apps(self, serialno):
installed_apps = self.get_apps(serialno)
app_flags = blacklist.flag_apps(installed_apps)
return app_flags

def uninstall(self, serialno, appid):
def uninstall(self, serial, appid):
pass

def run_command(self, cmd, **kwargs):
Expand All @@ -129,7 +127,6 @@ def run_command(self, cmd, **kwargs):
def save(self, table, **kwargs):
try:
tab = db.get_table(table)
kwargs['time'] = datetime.now()
kwargs['device'] = kwargs.get('device', self.device_type)
tab.insert(kwargs)
db.commit()
Expand Down Expand Up @@ -215,7 +212,7 @@ def devices_info(self):
# f.write(p.stdout.read())
# print("Dump success! Written to={}".format(outfname))

def uninstall(self, appid, serialno):
def uninstall(self, serial, appid):
cmd = '{cli} -s {serial} uninstall {appid!r}'
s = self.catch_err(self.run_command(cmd, serial=shlex.quote(serialno),
appid=shlex.quote(appid)),
Expand Down Expand Up @@ -260,7 +257,7 @@ def devices(self):
print(s)
return [l.strip() for l in s.split('\n') if l.strip()]

def uninstall(self, appid, serialno):
def uninstall(self, serial, appid):
cmd = '{cli} -i {serial} --uninstall_only --bundle_id {appid!r}'
s = self.catch_err(self.run_command(cmd, serial=serialno, appid=appid),
cmd=cmd, msg="Could not uninstall")
Expand All @@ -282,7 +279,7 @@ def devices(self):
def get_system_apps(self, serialno):
return self.get_apps(serialno)[:10]

def uninstall(self, appid, serialno):
def uninstall(self, serial, appid):
return True


Loading

0 comments on commit 654e824

Please sign in to comment.