Hotfix: Handle Docker <-> ADB connectivity internally (#2219)

* host.docker.internal transilation for localhost

* Replace urlparse with re

* version bump

* update ascii art
This commit is contained in:
Ajin Abraham
2023-07-25 13:19:06 -07:00
committed by GitHub
parent 1fde1cfa6d
commit 218954d3e7
7 changed files with 41 additions and 17 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ LABEL \
# Environment vars
ENV DEBIAN_FRONTEND="noninteractive" \
ANALYZER_IDENTIFIER="" \
MOBSF_PLATFORM="docker" \
JDK_FILE="openjdk-16.0.1_linux-x64_bin.tar.gz" \
JDK_FILE_ARM="openjdk-16.0.1_linux-aarch64_bin.tar.gz" \
WKH_FILE="wkhtmltox_0.12.6-1.focal_amd64.deb" \
+1 -1
View File
@@ -1,5 +1,5 @@
# Mobile Security Framework (MobSF)
Version: v3.6 beta
Version: v3.7 beta
![](https://cloud.githubusercontent.com/assets/4301109/20019521/cc61f7fc-a2f2-11e6-95f3-407030d9fdde.png)
@@ -129,8 +129,6 @@ def dynamic_analyzer(request, checksum, api=False):
try:
identifier = get_device()
except Exception:
pass
if not identifier:
return print_n_send_error_response(
request, get_android_dm_exception_msg(), api)
@@ -303,8 +301,6 @@ def trigger_static_analysis(request, checksum):
try:
identifier = get_device()
except Exception:
pass
if not identifier:
err = 'Cannot connect to Android Runtime'
return print_n_send_error_response(request, err)
env = Environment(identifier)
@@ -74,7 +74,7 @@ class Environment:
logger.info('Connecting to Android %s', self.identifier)
self.run_subprocess_verify_output([get_adb(),
'connect',
self.identifier], 0)
self.identifier])
def connect_n_mount(self):
"""Test ADB Connection."""
@@ -18,6 +18,7 @@ from mobsf.DynamicAnalyzer.views.android.environment import (
)
from mobsf.MobSF.utils import (
cmd_injection_check,
docker_translate_localhost,
get_adb,
get_device,
is_md5,
@@ -92,6 +93,7 @@ def mobsfy(request, api=False):
'message': 'Command Injection Detected',
}
return send_response(data, api)
identifier = docker_translate_localhost(identifier)
create_env = Environment(identifier)
if not create_env.connect_n_mount():
data = {'status': 'failed', 'message': msg}
+6 -6
View File
@@ -10,13 +10,13 @@ from mobsf.install.windows.setup import windows_config_local
logger = logging.getLogger(__name__)
VERSION = '3.6.9'
VERSION = '3.7.0'
BANNER = """
__ __ _ ____ _____ _____ __
| \/ | ___ | |__/ ___|| ___|_ _|___ / / /_
| |\/| |/ _ \| '_ \___ \| |_ \ \ / / |_ \| '_ \
| | | | (_) | |_) |__) | _| \ V / ___) | (_) |
|_| |_|\___/|_.__/____/|_| \_/ |____(_)___/
__ __ _ ____ _____ _____ _____
| \/ | ___ | |__/ ___|| ___|_ _|___ /|___ |
| |\/| |/ _ \| '_ \___ \| |_ \ \ / / |_ \ / /
| | | | (_) | |_) |__) | _| \ V / ___) | / /
|_| |_|\___/|_.__/____/|_| \_/ |____(_)_/
""" # noqa: W291
# ASCII Font: Standard
+30 -4
View File
@@ -321,18 +321,44 @@ def find_process_by(name):
return proc
def docker_translate_localhost(identifier):
"""Convert localhost to host.docker.internal."""
if not identifier:
return identifier
if not os.getenv('MOBSF_PLATFORM') == 'docker':
return identifier
try:
identifier = identifier.strip()
docker_internal = 'host.docker.internal:'
if re.match(r'^emulator-\d{4}$', identifier):
adb_port = int(identifier.split('emulator-')[1]) + 1
# ADB port is console port + 1
return f'{docker_internal}{adb_port}'
m = re.match(r'^(localhost|127\.0\.0\.1):\d{1,5}$', identifier)
if m:
adb_port = int(identifier.split(m.group(1))[1].replace(':', ''))
return f'{docker_internal}{adb_port}'
return identifier
except Exception:
logger.exception('Failed to convert device '
'identifier for docker connectivity')
return identifier
def get_device():
"""Get Device."""
if os.getenv('ANALYZER_IDENTIFIER'):
return os.getenv('ANALYZER_IDENTIFIER')
if settings.ANALYZER_IDENTIFIER:
return settings.ANALYZER_IDENTIFIER
return docker_translate_localhost(
os.getenv('ANALYZER_IDENTIFIER'))
elif settings.ANALYZER_IDENTIFIER:
return docker_translate_localhost(
settings.ANALYZER_IDENTIFIER)
else:
dev_id = ''
out = subprocess.check_output([get_adb(), 'devices']).splitlines()
if len(out) > 2:
dev_id = out[1].decode('utf-8').split('\t')[0]
return dev_id
return docker_translate_localhost(dev_id)
logger.error(get_android_dm_exception_msg())