mirror of
https://github.com/MobSF/Mobile-Security-Framework-MobSF.git
synced 2026-10-12 02:06:59 +00:00
a8254bcbb8
* fix: mitmdump process leaked and respawned on every dynamic analysis run mobsf_agents_setup() called create_ca() unconditionally on every dynamic analysis session, spawning a new detached mitmdump process even when the CA cert already existed. The spawned process was also never stopped, so it stayed running indefinitely after the session ended. create_ca() now waits for the CA cert file to appear and terminates mitmdump once it's done, and the redundant unconditional call in mobsf_agents_setup() is removed in favor of the existing get_ca_file() check (already used by install_mobsf_ca()). Co-authored-by: alanhasn * reap mitmdump after CA generation Co-authored-by: Ajin Abraham <[email protected]> * Update environment.py * Fix comment formatting in mobsf_agents_setup Removed period from comment about installing MITM RootCA. --------- Co-authored-by: Ajin Abraham <[email protected]> Co-authored-by: Cursor Agent <[email protected]>
122 lines
3.3 KiB
Python
122 lines
3.3 KiB
Python
import logging
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import time
|
|
|
|
import requests
|
|
|
|
from django.conf import settings
|
|
|
|
from mobsf.MobSF.utils import upstream_proxy
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def stop_httptools(url):
|
|
"""Kill httptools."""
|
|
# httptools is a local subprocess. Never use the request Host header for
|
|
# this server-side call; ALLOWED_HOSTS may be configured broadly.
|
|
local_url = f'http://127.0.0.1:{settings.PROXY_PORT}'
|
|
# Invoke HTTPtools UI Kill Request
|
|
try:
|
|
requests.get(f'{local_url}/kill', timeout=5)
|
|
logger.info('Killing httptools UI')
|
|
except Exception:
|
|
pass
|
|
|
|
# Invoke HTTPtools Proxy Kill Request
|
|
try:
|
|
http_proxy = local_url
|
|
headers = {'httptools': 'kill'}
|
|
proxy_kill_url = 'http://127.0.0.1'
|
|
requests.get(
|
|
proxy_kill_url,
|
|
timeout=5,
|
|
headers=headers,
|
|
proxies={'http': http_proxy})
|
|
logger.info('Killing httptools Proxy')
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def start_proxy(port, project):
|
|
"""Start HTTPtools in Proxy Mode."""
|
|
argz = ['httptools',
|
|
'-m', 'capture',
|
|
'-p', str(port), '-n', project]
|
|
proxies, _ = upstream_proxy('http')
|
|
if proxies['http']:
|
|
argz.extend(['-u', proxies['http']])
|
|
fnull = open(os.devnull, 'w')
|
|
subprocess.Popen(argz, stdout=fnull, stderr=subprocess.STDOUT)
|
|
|
|
|
|
def start_httptools_ui(port):
|
|
"""Start Server UI."""
|
|
subprocess.Popen(['httptools',
|
|
'-m', 'server', '-p', str(port)])
|
|
time.sleep(3)
|
|
|
|
|
|
def _mitm_ca_file():
|
|
"""Path to the mitmproxy generated CA cert."""
|
|
from mitmproxy import options
|
|
ca_dir = Path(options.CONF_DIR).expanduser()
|
|
return ca_dir / 'mitmproxy-ca-cert.pem'
|
|
|
|
|
|
def create_ca():
|
|
"""Generate CA on first run."""
|
|
ca_file = _mitm_ca_file()
|
|
if ca_file.exists():
|
|
return
|
|
|
|
argz = ['mitmdump', '-n']
|
|
proc = subprocess.Popen(argz,
|
|
stdin=subprocess.DEVNULL,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
close_fds=True)
|
|
try:
|
|
# mitmdump is only needed here to generate the CA cert. Stop it
|
|
# once the cert file appears instead of leaving it running as a
|
|
# leaked background process.
|
|
for _ in range(30):
|
|
if ca_file.exists() or proc.poll() is not None:
|
|
break
|
|
time.sleep(0.5)
|
|
if not ca_file.exists():
|
|
logger.warning('mitmproxy root CA generation failed.')
|
|
finally:
|
|
if proc.poll() is None:
|
|
proc.terminate()
|
|
try:
|
|
proc.wait(timeout=5)
|
|
except subprocess.TimeoutExpired:
|
|
proc.kill()
|
|
proc.wait()
|
|
|
|
|
|
def get_ca_file():
|
|
"""Get CA Dir."""
|
|
ca_file = _mitm_ca_file()
|
|
if not ca_file.exists():
|
|
create_ca()
|
|
return ca_file.as_posix()
|
|
|
|
|
|
def get_traffic(package):
|
|
web = Path.home() / '.httptools' / 'flows' / f'{package}.flow.txt'
|
|
if web.is_file():
|
|
return web.read_text('utf-8', 'ignore')
|
|
return ''
|
|
|
|
|
|
def get_http_tools_url(req):
|
|
"""Get httptools URL from request."""
|
|
scheme = req.scheme
|
|
ip = req.get_host().split(':')[0]
|
|
port = settings.PROXY_PORT
|
|
return f'{scheme}://{ip}:{str(port)}'
|