Windows Static Analysis
Overview
Windows static analysis is entered through mobsf/…/windows.py. staticanalyzer_windows(request, checksum, api=False) is the web and REST entry point: mobsf/…/urls.py routes /static_analyzer_windows/<checksum>/ to it, and mobsf/…/api_static_analysis.py calls the same function for uploaded files whose scan type is in settings.WINDOWS_EXTS (appx, msix, appxbundle, msixbundle). The view validates that the checksum is an MD5, verifies the upload exists in RecentScansDB, rejects unsupported file types, and then either reuses an existing StaticAnalyzerWindows row or runs a fresh scan when there is no row or rescan=1/re_scan=1 is set. Web requests render static_analysis/windows_binary_analysis.html; API requests return the same context dictionary.
analyze_package() in mobsf/…/windows.py builds the persisted analysis. It hashes the uploaded package with hash_gen(), extracts the archive with the shared unzip() helper into settings.UPLD_DIR/<md5>, and keeps a shared uncompressed-byte budget so an outer bundle and its selected inner package are capped together. For appxbundle and msixbundle, _select_bundle_package() parses AppxMetadata/AppxBundleManifest.xml, keeps only application packages, prefers architectures in x64, arm64, x86, neutral, arm order, rejects traversal/links with _safe_child(), then extracts the selected .appx/.msix into bundle_package after _reset_dir() removes anything previously planted there. The file list stored in analysis['files'] comes from the extracted archive entries and excludes directory entries.
Manifest and package-signature analysis live in mobsf/…/manifest.py. parse_manifest(root / 'AppxManifest.xml') parses XML with defusedxml, rejects pipes/links and files over MAX_MANIFEST_BYTES, and returns a fixed manifest shape even when parsing fails. It extracts identity, publisher, version, architecture, display metadata, minimum OS version, build metadata, application executable paths, capabilities, package dependencies, and security findings. Findings include full-trust applications, cleartext ApplicationContentUriRules, remote content with WinRT access, known sensitive extension categories such as protocol handlers, full-trust processes, COM servers, services, firewall rules, loader search path overrides, and debug runtime dependencies. analyze_signature(app_dir, manifest['publisher']) inspects the outer package's AppxSignature.p7x, strips the optional PKCX prefix, parses PKCS#7 signer information with asn1crypto/cryptography, and reports signer, issuer, expiry, and digest algorithm. Its findings cover unsigned packages, unparsable signatures, signer/publisher DN mismatches, self-signed certificates, weak hash algorithms, and expired certificates. analyze_package() merges manifest findings, signature findings, and _package_file_findings() output for packaged .pdb files into manifest_analysis.
PE hardening and string extraction are driven by _analyze_binaries() in mobsf/…/windows.py. _iter_pe_files() walks the extracted app root, only considers known PE suffixes (.exe, .dll, .sys, .ocx, .cpl, .scr, .pyd, .node), skips pipes/links, validates safe relative paths, ignores files larger than MAX_PE_SIZE, and checks the MZ magic before analysis. Main executables declared by the manifest are sorted first, and the scan is capped at MAX_PE_FILES. Each candidate is analyzed by PEChecksec in mobsf/…/pe.py, which parses the binary through lief.PE.parse under settings.BINARY_ANALYSIS_TIMEOUT. The result records binary name, architecture, PE32/PE32+ format, EXE/DLL type, runtime (Native, .NET (mixed mode), or .NET (IL only)), and checks for ASLR, high-entropy VA, DEP/NX, Control Flow Guard, SafeSEH, /GS stack cookies, RWX sections, Authenticode digest validity, and Microsoft SDL banned imports. IL-only .NET assemblies return Not Applicable for native-only checks instead of false failures.
mobsf/…/strings.py provides bounded printable string extraction for the PE pass. extract_strings() reads at most MAX_READ_BYTES from each file in 1 MiB chunks, matches ASCII and UTF-16LE printable runs of at least six characters, keeps overlap so chunk boundaries do not split strings, truncates individual strings to MAX_STRING_LEN, and returns unique sorted values up to the per-file cap passed by _analyze_binaries(). analyze_package() also enforces MAX_STRINGS_TOTAL across files and persists only the first MAX_STORED_STRINGS. Extracted strings are passed to the shared url_n_email_extract() helper for per-file URL/email tables, to get_secrets() for hardcoded-secret findings, to MalwareDomainCheck().scan() for domain reputation, and to Trackers.Trackers(...).get_trackers_domains_or_deps() for tracker attribution.
Persistence and report context conversion are centralized in mobsf/…/db_interaction.py. get_context_from_analysis() converts the in-memory result into the template/API schema, using _display_name() to fall back from unresolved ms-resource: display names to the package name and stripping findings out of the package_signature object before display. save_or_update() writes the context into StaticAnalyzerWindows fields and updates RecentScansDB.APP_NAME, PACKAGE_NAME, and VERSION_NAME; failures are logged into scan status rather than raised. get_context_from_db_entry() reverses the stored TextField values with python_list()/python_dict() and appends get_scan_logs() so cached reports and freshly generated reports expose the same keys. The model fields backing this schema are in mobsf/…/models.py on StaticAnalyzerWindows, including manifest metadata, FILES, STRINGS, PE_ANALYSIS, MANIFEST_ANALYSIS, CAPABILITIES, PACKAGE_SIGNATURE, URLS, EMAILS, DOMAINS, SECRETS, and TRACKERS.
The web report at mobsf/…/windows_binary_analysis.html consumes that context directly. It presents package/app/build metadata, scan actions, package signature details, manifest findings, capability rows, PE hardening rows, malware lookup links, optional VirusTotal results, domain malware checks, extracted URLs and emails, trackers, possible hardcoded secrets, stored strings, and the extracted file list. The PE table expects the PEChecksec.checksec() key names and renders each check via static_analysis/windows_pe_cell.html; older rows with binary_analysis/binary_warnings still display a legacy table with a prompt to rescan.
Coupling
- If the Windows scan result schema changes in mobsf/…/windows.py, update both conversion paths in mobsf/…/db_interaction.py and the
StaticAnalyzerWindowsfields in mobsf/…/models.py; cached reports are reconstructed from DB text fields and missing keys will break mobsf/…/windows_binary_analysis.html or API consumers. - If
PEChecksec.checksec()in mobsf/…/pe.py adds, removes, or renames a check key, update the PE table in mobsf/…/windows_binary_analysis.html; the template addresses keys such asaslr,high_entropy_va,dep,cfg,safe_seh,stack_cookie,rwx_sections,code_signature, andbanned_importsdirectly. - If manifest parsing changes how executable paths are recorded in mobsf/…/manifest.py, verify
_iter_pe_files()sorting in mobsf/…/windows.py; manifest executables are used only to prioritize binaries, so path normalization drift silently changes which files fit underMAX_PE_FILESfirst. - If bundle extraction behavior changes in mobsf/…/windows.py, verify the shared
unzip()contract in mobsf/…/shared_func.py; bundles rely on a sharedsize_budgetacross outer and inner archives and on the no-OS-fallback path to keep decompression limits enforceable. - If capability or manifest-finding severities in mobsf/…/manifest.py change, verify the badge mapping in mobsf/…/windows_binary_analysis.html; the template has explicit handling for
high,warning,secure, and otherwise treats values as informational. - If supported Windows extensions change in mobsf/…/settings.py, verify the entry-point checks in mobsf/…/windows.py and API dispatch in mobsf/…/api_static_analysis.py; both must agree or uploads may be accepted but never routed to the Windows analyzer.
Risks
- mobsf/…/windows.py limits PE analysis to
MAX_PE_FILESafter sorting manifest executables first. If a malicious package contains many decoy PE files and important binaries are not listed in AppxManifest.xml, those later binaries are skipped, so PE hardening findings, strings, URLs, secrets, malware domains, and trackers can be incomplete. - mobsf/…/strings.py reads only the first
MAX_READ_BYTESfrom each binary and mobsf/…/windows.py stops collecting new strings afterMAX_STRINGS_TOTALand stores onlyMAX_STORED_STRINGS. If indicators are embedded beyond those caps, the report will omit the corresponding URL, email, secret, domain, and tracker evidence. - mobsf/…/manifest.py treats an unreadable, overlarge, linked, or invalid AppxManifest.xml as
empty_manifest(). That keeps the scan alive, but it also means package identity, capabilities, executable prioritization, and manifest findings are absent rather than surfaced as a dedicated manifest-parse failure. - mobsf/…/manifest.py verifies the package signature metadata and compares signer DN to the manifest publisher, but it does not validate certificate trust chains or timestamp semantics. A report can mark a package as signed while the signer is untrusted outside the local package context.