Skip to content

Commit 1ca0336

Browse files
committed
Address issues in #2490
1 parent fff1223 commit 1ca0336

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

mobsf/MobSF/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
'releases/latest')
139139
FRIDA_SERVER = 'https://api.github.com/repos/frida/frida/releases/tags/'
140140
GOOGLE = 'https://www.google.com'
141+
PLAYSTORE = 'https://play.google.com'
141142
BAIDU = 'https://www.baidu.com/'
142143
APKPURE = 'https://m.apkpure.com/android/{}/download?from=details'
143144
APKTADA = 'https://apktada.com/download-apk/'

mobsf/MobSF/tools_download.py

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import tempfile
55
import zipfile
66
import platform
7+
import os
8+
import ssl
79
from pathlib import Path
810
from urllib.request import (
11+
HTTPSHandler,
912
ProxyHandler,
1013
Request,
1114
build_opener,
@@ -19,11 +22,77 @@
1922
logger = logging.getLogger(__name__)
2023

2124

25+
def standalone_upstream_proxy():
26+
"""Set upstream Proxy for urllib - standalone."""
27+
upstream_proxy_enabled = bool(os.getenv('MOBSF_UPSTREAM_PROXY_ENABLED', ''))
28+
29+
if upstream_proxy_enabled:
30+
upstream_proxy_username = os.getenv('MOBSF_UPSTREAM_PROXY_USERNAME', '')
31+
upstream_proxy_password = os.getenv('MOBSF_UPSTREAM_PROXY_PASSWORD', '')
32+
upstream_proxy_type = os.getenv('MOBSF_UPSTREAM_PROXY_TYPE', 'http')
33+
upstream_proxy_ip = os.getenv('MOBSF_UPSTREAM_PROXY_IP', '127.0.0.1')
34+
upstream_proxy_port = int(os.getenv('MOBSF_UPSTREAM_PROXY_PORT', '3128'))
35+
36+
# Handle Docker proxy IP translation
37+
if os.getenv('MOBSF_PLATFORM') == 'docker':
38+
if (upstream_proxy_ip and upstream_proxy_ip.strip() in
39+
('127.0.0.1', 'localhost')):
40+
upstream_proxy_ip = 'host.docker.internal'
41+
42+
if not upstream_proxy_username:
43+
proxy_port = str(upstream_proxy_port)
44+
proxy_host = f'{upstream_proxy_type}://{upstream_proxy_ip}:{proxy_port}'
45+
else:
46+
proxy_port = str(upstream_proxy_port)
47+
proxy_host = (f'{upstream_proxy_type}://{upstream_proxy_username}:'
48+
f'{upstream_proxy_password}@{upstream_proxy_ip}:'
49+
f'{proxy_port}')
50+
51+
# For urllib, we need to set both http and https proxies
52+
proxies = {
53+
'http': proxy_host,
54+
'https': proxy_host,
55+
}
56+
else:
57+
proxies = {}
58+
59+
upstream_proxy_ssl_verify = os.getenv('MOBSF_UPSTREAM_PROXY_SSL_VERIFY', '1')
60+
verify = upstream_proxy_ssl_verify in ('1', '"1"')
61+
return proxies, verify
62+
63+
2264
def download_file(url, file_path):
2365
req = Request(url)
66+
67+
# Check for system proxies first (http_proxy, https_proxy env vars)
2468
system_proxies = getproxies()
25-
proxy_handler = ProxyHandler(system_proxies)
26-
opener = build_opener(proxy_handler)
69+
70+
if system_proxies:
71+
proxies = system_proxies
72+
verify = True # Default to verify for system proxies
73+
logger.info('Using system proxies: %s (SSL verify: %s)', proxies, verify)
74+
else:
75+
# Check if MobSF upstream proxy is explicitly configured
76+
upstream_proxy_enabled = bool(os.getenv('MOBSF_UPSTREAM_PROXY_ENABLED', ''))
77+
78+
if upstream_proxy_enabled:
79+
proxies, verify = standalone_upstream_proxy()
80+
logger.info('Using MobSF upstream proxies: %s (SSL verify: %s)',
81+
proxies, verify)
82+
else:
83+
# No proxy configuration - use direct connection
84+
proxies = {}
85+
verify = True
86+
87+
proxy_handler = ProxyHandler(proxies)
88+
89+
if verify:
90+
ssl_context = ssl.create_default_context()
91+
else:
92+
ssl_context = ssl._create_unverified_context()
93+
94+
https_handler = HTTPSHandler(context=ssl_context)
95+
opener = build_opener(proxy_handler, https_handler)
2796

2897
with opener.open(req) as response:
2998
if response.status == 200:

mobsf/StaticAnalyzer/views/android/playstore.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ def get_app_details(app_dic, man_data):
2525
'description': 'Failed to identify the package name',
2626
}
2727
try:
28+
# Check if Google Play Store is reachable
29+
# The library can cause timeout issue behind proxy
30+
try:
31+
proxies, verify = upstream_proxy('https')
32+
requests.get(settings.PLAYSTORE,
33+
timeout=5,
34+
proxies=proxies,
35+
verify=verify)
36+
except Exception:
37+
logger.warning('Google Play Store is not reachable.'
38+
' Skipping Play Store lookup.')
39+
return
40+
2841
if man_data.get('packagename'):
2942
package_id = man_data['packagename']
3043
elif app_dic.get('apk_features', {}).get('package'):

0 commit comments

Comments
 (0)