Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project : QYVORA7# File : security/audit/mobsf/selftest.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"""Self-test of the MobSF and binary-check runners that needs no build and no container.4243 python3 security/audit/mobsf/selftest.py4445It checks the paths that must never report success by mistake:46 * `--no-mobsf` gives NOT RUN;47 * a missing policy digest gives NOT RUN;48 * an unreachable Docker host gives NOT RUN with the reason (requires an existing build; skipped otherwise);49 * the normaliser lists a MobSF warning as a finding and applies the reviewed triage with its justification;50 * the recovery-phrase detector flags a phrase and ignores an alphabetical slice of the word list.51"""5253from __future__ import annotations5455import sys56from pathlib import Path5758sys.path.insert(0, str(Path(__file__).resolve().parents[1]))59from lib.common import NOT_RUN, Context # noqa: E40260from runners import binary_checks, ipa_build, mobsf # noqa: E4026162failures = []636465def check(name: str, ok: bool) -> None:66 print(("ok " if ok else "FAIL ") + name)67 if not ok:68 failures.append(name)697071r = mobsf.run(Context(options={"no_mobsf": True}))72check("--no-mobsf is NOT RUN", r["status"] == NOT_RUN and "--no-mobsf" in r["reason"] and not r["findings"])7374original = mobsf.load_json75mobsf.load_json = lambda *a, **k: {"image": "x"}76r = mobsf.run(Context())77check("policy without digest is NOT RUN", r["status"] == NOT_RUN and "digest" in r["reason"])78mobsf.load_json = original7980if (ipa_build.APP / "Qyvora").exists() and (ipa_build.load_json(ipa_build.MARKER, {}) or {}).get("state") == ipa_build.tree_state():81 mobsf.load_json = lambda *a, **k: {**original(*a, **k), "mode": "ssh", "host": "qyvora-no-such-host.invalid"}82 r = mobsf.run(Context())83 check("unreachable Docker host is NOT RUN with the reason", r["status"] == NOT_RUN and "not reachable" in r["reason"])84 mobsf.load_json = original85else:86 print("skip unreachable-host test (no current build in the scratch directory)")8788report = {"version": "test", "macho_analysis": {"name": "Qyvora",89 "pie": {"has_pie": False, "severity": "high", "description": "no PIE"},90 "rpath": {"has_rpath": True, "severity": "warning", "description": "rpath"},91 "nx": {"has_nx": True, "severity": "info", "description": "nx"}},92 "binary_analysis": {"findings": {"Weak hash": {"detailed_desc": "MD5", "severity": "high", "cwe": "CWE-327", "masvs": "MSTG-CRYPTO-4"}},93 "summary": {}}}94triage = {"macho/rpath": {"status": "accepted-risk", "severity": "info", "justification": "reviewed"}}95found, metrics = mobsf.normalise(report, {"high": "high", "warning": "medium", "info": "info"}, triage)96by_rule = {f["rule"]: f for f in found}97check("MobSF high item becomes an open high finding", by_rule["macho/pie"]["severity"] == "high" and by_rule["macho/pie"]["status"] == "open")98check("MobSF binary finding keeps CWE and MASVS", by_rule["binary/Weak hash"]["cwe"] == "CWE-327" and by_rule["binary/Weak hash"]["severity"] == "high")99check("triaged item keeps its justification and MobSF severity", by_rule["macho/rpath"]["status"] == "accepted-risk"100 and by_rule["macho/rpath"]["triage"] == "reviewed" and by_rule["macho/rpath"]["mobsf_severity"] == "medium")101check("informational item is counted, not listed", "macho/nx" not in by_rule and metrics["items_by_mobsf_severity"]["info"] == 1)102103words = binary_checks.bip39_words()104check("BIP-39 word list read from the sources (2048 words)", len(words) == 2048)105phrase = "legal winner thank year wave sausage worth useful legal winner thank yellow"106slice_ = "catch category cattle caught cause caution cave ceiling celery cement census century"107check("recovery-phrase detector flags a phrase", binary_checks.find_mnemonics([phrase], words) == [phrase])108check("recovery-phrase detector ignores a slice of the word list", binary_checks.find_mnemonics([slice_], words) == [])109check("WIF pattern matches a compressed-key WIF", bool(binary_checks.SECRET_PATTERNS["Bitcoin WIF private key"].search(110 "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn")))111check("extended private key pattern matches", bool(binary_checks.SECRET_PATTERNS["BIP-32 extended private key"].search(112 "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi")))113114sys.exit(1 if failures else 0)115