SPB Git

Repository Public

client.py 4.2 KB 115 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project          : QYVORA7# File             : security/audit/mobsf/client.py8# Module           : Security9# Repository       : https://git.spboucher.ai/qyvora10# Project Website  : Not specified11# Created          : 2026-12-0412# Last Modified    : 2026-12-0413#14# Author           : Simon-Pierre Boucher15# Position         : Professor of Finance16# Institution      : Université du Québec en Outaouais (UQO)17# Organization     : SP Boucher18# Website          : https://spboucher.ai19# Git              : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development      : dev@spboucher.ai23# Support          : support@spboucher.ai24# Security/Admin   : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License          : None specified (no license file in this repository)33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""MobSF REST client executed INSIDE the MobSF container (`docker exec -i <container> python3 -`).4243The container has no network: this script talks to MobSF on the container's own loopback44interface, uploads the .ipa that was copied into the container, starts the static scan, prints45the JSON report on stdout and deletes the scan. It uses `requests`, which the MobSF image ships.4647Environment: MOBSF_API_KEY (set when the container was started), QY_IPA (path inside the container),48QY_ACTION = wait | scan.49"""5051import json52import os53import sys54import time5556import requests5758BASE = "http://127.0.0.1:8000"59KEY = os.environ["MOBSF_API_KEY"]60HEADERS = {"Authorization": KEY}616263def fail(step, r):64    print(json.dumps({"qy_error": f"{step}: HTTP {r.status_code}: {r.text[:500]}"}))65    sys.exit(2)666768def wait(timeout):69    deadline = time.time() + timeout70    last = ""71    while time.time() < deadline:72        try:73            r = requests.get(BASE + "/api/v1/scans", headers=HEADERS, timeout=5)74            if r.status_code == 200:75                print(json.dumps({"ready": True}))76                return77            last = f"HTTP {r.status_code}"78        except requests.RequestException as e:  # server still starting79            last = type(e).__name__80        time.sleep(3)81    print(json.dumps({"qy_error": "MobSF did not answer in time: " + last}))82    sys.exit(2)838485def scan(path, timeout):86    with open(path, "rb") as f:87        r = requests.post(BASE + "/api/v1/upload", headers=HEADERS,88                          files={"file": (os.path.basename(path), f, "application/octet-stream")}, timeout=300)89    if r.status_code != 200:90        fail("upload", r)91    up = r.json()92    r = requests.post(BASE + "/api/v1/scan", headers=HEADERS, data={"hash": up["hash"]}, timeout=timeout)93    if r.status_code != 200:94        fail("scan", r)95    r = requests.post(BASE + "/api/v1/report_json", headers=HEADERS, data={"hash": up["hash"]}, timeout=300)96    if r.status_code != 200:97        fail("report_json", r)98    report = r.json()99    scorecard = None100    r = requests.post(BASE + "/api/v1/scorecard", headers=HEADERS, data={"hash": up["hash"]}, timeout=300)101    if r.status_code == 200:102        scorecard = r.json()103    r = requests.post(BASE + "/api/v1/delete_scan", headers=HEADERS, data={"hash": up["hash"]}, timeout=300)104    deleted = r.status_code == 200105    print(json.dumps({"upload": up, "report": report, "scorecard": scorecard, "scan_deleted": deleted}))106107108if __name__ == "__main__":109    action = os.environ.get("QY_ACTION", "scan")110    limit = int(os.environ.get("QY_TIMEOUT", "1800"))111    if action == "wait":112        wait(limit)113    else:114        scan(os.environ["QY_IPA"], limit)115
Files main
Menu

Theme