Merge pull request #1043 from MobSF/win_temp

Windows QA
This commit is contained in:
superpoussin22
2019-08-08 08:01:59 +02:00
committed by GitHub
6 changed files with 88 additions and 67 deletions
File diff suppressed because one or more lines are too long
+17 -25
View File
@@ -5,16 +5,14 @@ import json
import logging
import os
import re
import shutil
import subprocess
import tempfile
from collections import namedtuple
import requests
from django.conf import settings
from MobSF.utils import is_file_exists, sha256, upstream_proxy
from MobSF.utils import (is_file_exists,
is_internet_available,
update_local_db)
logger = logging.getLogger(__name__)
@@ -36,22 +34,18 @@ class Trackers:
def _update_tracker_db(self):
"""Update Trackers DB."""
try:
proxies, verify = upstream_proxy('http')
except Exception:
logger.exception('[ERROR] Setting upstream proxy')
try:
exodus_url = settings.TRACKERS_DB_URL
res = requests.get(exodus_url, timeout=3, proxies=proxies,
allow_redirects=True, verify=verify)
data = res.content
tmp_dwd = tempfile.NamedTemporaryFile()
tmp_dwd.write(data)
tmp_dwd.seek(0)
if not is_internet_available():
logger.warning('No Internet Connection. '
'Skipping Trackers Database Update.')
return
resp = update_local_db('Trackers',
settings.TRACKERS_DB_URL,
self.tracker_db)
# Check1: SHA256 Change
if sha256(tmp_dwd.name) != sha256(self.tracker_db):
if resp:
# DB needs update
# Check2: DB Syntax Changed
data = json.loads(tmp_dwd.read().decode('utf-8', 'ignore'))
data = json.loads(resp.decode('utf-8', 'ignore'))
is_db_format_good = False
if 'trackers' in data:
if '1' in data['trackers']:
@@ -59,19 +53,17 @@ class Trackers:
is_db_format_good = True
if is_db_format_good:
# DB Format is not changed. Let's update DB
logger.info('Updating Tracker Database....')
shutil.copyfile(tmp_dwd.name, self.tracker_db)
logger.info('Updating Trackers Database....')
with open(self.tracker_db, 'wb') as wfp:
wfp.write(resp)
else:
logger.info('Tracker Database format from '
logger.info('Trackers Database format from '
'reports.exodus-privacy.eu.org has changed.'
' Database is not updated. '
'Please report to: https://github.com/MobSF/'
'Mobile-Security-Framework-MobSF/issues')
else:
logger.info('Tracker Database is up-to-date.')
tmp_dwd.close()
except Exception:
logger.exception('[ERROR] Tracker DB Update')
logger.exception('[ERROR] Trackers DB Update')
def _compile_signatures(self):
"""
+11 -28
View File
@@ -4,17 +4,12 @@ import io
import logging
import os
import re
import shutil
import tempfile
from urllib.parse import urlparse
import requests
from django.conf import settings
from MobSF.utils import (is_internet_available,
sha256,
upstream_proxy)
update_local_db)
logger = logging.getLogger(__name__)
@@ -26,36 +21,24 @@ MALWARE_DB_DIR = TOOLS_DIR = os.path.join(
def update_malware_db():
"""Check for update in malware DB."""
try:
proxies, verify = upstream_proxy('http')
except Exception:
logger.exception('[ERROR] Setting upstream proxy')
try:
url = settings.MALWARE_DB_URL
response = requests.get(url, timeout=3, proxies=proxies, verify=verify)
data = response.content
tmp_dwd = tempfile.NamedTemporaryFile()
tmp_dwd.write(data)
mal_db = os.path.join(MALWARE_DB_DIR, 'malwaredomainlist')
tmp_dwd.seek(0)
# Check1: SHA256 Change
if sha256(tmp_dwd.name) != sha256(mal_db):
resp = update_local_db('Malware', settings.MALWARE_DB_URL, mal_db)
if (resp):
# DB needs update
# Check2: DB Syntax Changed
line = tmp_dwd.readline().decode('utf-8', 'ignore')
line = resp.decode('utf-8', 'ignore').split('\n')[0]
lst = line.split('",')
if len(lst) == 10:
# DB Format is not changed. Let's update DB
logger.info('Updating Malware Database....')
shutil.copyfile(tmp_dwd.name, mal_db)
with open(mal_db, 'wb') as wfp:
wfp.write(resp)
else:
logger.info('Malware Database format from '
'malwaredomainlist.com has changed.'
' Database is not updated. '
'Please report to: https://github.com/'
'MobSF/Mobile-Security-Framework-MobSF/issues')
else:
logger.info('Malware Database is up-to-date.')
tmp_dwd.close()
except Exception:
logger.exception('[ERROR] Malware DB Update')
@@ -66,14 +49,14 @@ def malware_check(urllist):
if not settings.DOMAIN_MALWARE_SCAN:
logger.info('Domain Malware Check disabled in settings')
return result
domainlist = get_domains(urllist)
if not domainlist:
return result
if is_internet_available():
update_malware_db()
else:
logger.warning(
'No Internet Connection. Skipping Malware Database Update.')
logger.warning('No Internet Connection. '
'Skipping Malware Database Update.')
domainlist = get_domains(urllist)
if not domainlist:
return result
mal_db = os.path.join(MALWARE_DB_DIR, 'malwaredomainlist')
with io.open(mal_db,
mode='r',
+41 -1
View File
@@ -390,7 +390,17 @@ def sha256(file_path):
while buf:
hasher.update(buf)
buf = afile.read(blocksize)
return (hasher.hexdigest())
return hasher.hexdigest()
def sha256_object(file_obj):
blocksize = 65536
hasher = hashlib.sha256()
buf = file_obj.read(blocksize)
while buf:
hasher.update(buf)
buf = file_obj.read(blocksize)
return hasher.hexdigest()
def gen_sha256_hash(msg):
@@ -519,3 +529,33 @@ def first_run(secret_file, base_dir, mobsf_home):
# Windows Setup
windows_config_local(mobsf_home)
return secret_key
def update_local_db(db_name, url, local_file):
"""Update Local DBs."""
update = None
try:
proxies, verify = upstream_proxy('http')
except Exception:
logger.exception('[ERROR] Setting upstream proxy')
try:
response = requests.get(url,
timeout=3,
proxies=proxies,
verify=verify)
resp = response.content
inmemoryfile = io.BytesIO(resp)
# Check1: SHA256 Change
if sha256_object(inmemoryfile) != sha256(local_file):
# Hash Changed
logger.info('%s Database is outdated!', db_name)
update = resp
else:
logger.info('%s Database is up-to-date', db_name)
return update
except Exception:
logger.exception('[ERROR] %s DB Update', db_name)
return update
finally:
if inmemoryfile:
inmemoryfile.truncate(0)
+5 -3
View File
@@ -232,15 +232,17 @@ def api_test():
files = [{'file': 'opensecurity/helloworld/MainActivity.java',
'type': 'apk',
'hash': '3a552566097a8de588b8184b059b0158'},
{'file': 'helloworld.app/Info.plist',
'type': 'ipa',
'hash': '6c23c2970551be15f32bbab0b5db0c71'},
{'file': 'opensecurity/webviewignoressl/MainActivity.java',
'type': 'studio',
'hash': '52c50ae824e329ba8b5b7a0f523efffe'},
{'file': 'DamnVulnerableIOSApp/AppDelegate.m',
'type': 'ios',
'hash': '57bb5be0ea44a755ada4a93885c3825e'}]
if platform.system() in ['Darwin', 'Linux']:
files.append({
'file': 'helloworld.app/Info.plist',
'type': 'ipa',
'hash': '6c23c2970551be15f32bbab0b5db0c71'})
for sfile in files:
resp = http_client.post(
'/api/v1/view_source', sfile, HTTP_AUTHORIZATION=auth)
+13 -9
View File
@@ -394,26 +394,27 @@ def binskim(name, bin_an_dic, run_local=False, app_dir=None):
def parse_binskim(bin_an_dic, output):
"""Parse output to results and warnings."""
current_run = output['runs'][0]
if 'results' in current_run:
rules = output['runs'][0]['rules']
rules = output['runs'][0]['resources']['rules']
for res in current_run['results']:
if res['level'] != 'pass':
if len(res['message']['arguments']) > 2:
info = (res['message']['arguments'][1] + ', '
+ res['message']['arguments'][2])
else:
info = ''
result = {
'rule_id': res['ruleId'],
'status': 'Insecure',
'desc': rules[res['ruleId']]['shortDescription'],
'info': info,
'desc': rules[res['ruleId']]['fullDescription']['text'],
}
if len(res['formattedRuleMessage']['arguments']) > 2:
result['info'] = res[
'formattedRuleMessage']['arguments'][2]
else:
result['info'] = ''
else:
result = {
'rule_id': res['ruleId'],
'status': 'Secure',
'desc': rules[res['ruleId']]['shortDescription'],
'info': '',
'desc': rules[res['ruleId']]['fullDescription']['text'],
}
bin_an_dic['results'].append(result)
else:
@@ -422,6 +423,7 @@ def parse_binskim(bin_an_dic, output):
warning = {
'rule_id': 'No Binskim-Results',
'status': 'Info',
'info': '',
'desc': 'No results from Binskim.',
}
bin_an_dic['warnings'].append(warning)
@@ -431,6 +433,7 @@ def parse_binskim(bin_an_dic, output):
warning = {
'rule_id': warn['ruleId'],
'status': 'Info',
'info': '',
'desc': warn['message'],
}
bin_an_dic['warnings'].append(warning)
@@ -528,6 +531,7 @@ def binscope(name, bin_an_dic, run_local=False, app_dir=None):
result = {
'rule_id': item.find('issueType').text,
'status': status,
'info': '',
'desc': desc,
}
bin_an_dic['results'].append(result)