Skip to content

Capabilities and Stack Strings in Mac Malware

How Mach-O Parser reads arm64 and x86_64 code: function discovery, named calls, capability rules mapped to ATT&CK, PT_DENY_ATTACH and stack strings.

Published on 6 min read

TL;DR. Imports and strings say what a binary contains. The code says how those pieces fit together. Mach-O Parser disassembles arm64 and x86_64 slices in the browser:

  • it finds every function, even in stripped binaries;
  • it names each call after the import, Objective-C selector or string it reaches;
  • it matches every function against capability rules mapped to MITRE ATT&CK and the Malware Behavior Catalog;
  • it rebuilds strings written onto the stack.

Nothing is executed. Each match names the function where the evidence sits, so the next step is one click away in the disassembly.

Why imports and strings are not enough

Static triage of a Mach-O file starts with its imports and strings, and they go a long way. Three habits of Mac malware get past them:

  • Objective-C hides the calls. Every method call goes through one import, objc_msgSend. What is actually called is the selector, a string passed alongside: writeToFile:atomically:, dataTaskWithURL:. Since Xcode 14, arm64 binaries call per-selector stubs named objc_msgSend$selector instead, which helps, but only if something reads them.
  • Dynamic resolution. dlopen and dlsym fetch functions by name at run time, so the import table shows only those two calls.
  • Text that never exists as a string. Commands, paths and URLs built on the stack, or decoded from XOR-ed bytes, are invisible to strings.

Finding the functions

A disassembly is only as good as its function list. The analysis collects start addresses from:

  • LC_FUNCTION_STARTS, a compact list of every function the linker saw;
  • the symbol table, the exports and the entry point;
  • the implementations of Objective-C methods.

Stripped binaries sometimes drop the function-start list too. The analysis then scans the code for call instructions (BL on arm64, CALL on x86) and treats every target as a function. On a large app like Google Chrome's framework, that recovers over a hundred thousand functions.

Each function is then walked once. Every call is named:

  • through the stubs and the GOT, after the imported symbol;
  • through the chained fixups, after the bound symbol;
  • through objc_msgSend$… stubs or selector references, after the selector.

Every address the code loads is resolved to a C string, a CFString, a selector or a class where possible. The Code tab shows the same names in its listing, so evidence in the analysis and instructions in the listing line up.

Capability rules

Each function produces a set of features: the APIs it calls, the selectors it sends, the classes it uses, the strings and constants it references, the system calls it makes, and characteristics such as loops and XOR instructions. Rules combine features. A few of the macOS rules:

CapabilityEvidence the rule looks forATT&CK
Prevent debugger attachptrace with constant 31 (PT_DENY_ATTACH), or system call 26 directlyT1622
Check for a debugger via sysctlsysctl with the KERN_PROC (14) query and the P_TRACED flag (0x800)T1622
Check for a debugger via exception portstask_get_exception_portsT1622
Check for a virtual machinekern.hv_vmm_present, "VMware", "VirtualBox", hardware-model queriesT1497.001
Invoke a system call directlysvc #0x80 (arm64) or syscall with 0x2000000 + n (x86_64)T1106
Read keychain itemsSecItemCopyMatching, security find-generic-passwordT1555.001
Capture keystrokesCGEventTapCreate, HID input callbacks, global event monitorsT1056.001
Capture the screenCGWindowListCreateImage, ScreenCaptureKit classes, screencaptureT1113
Load a Mach-O image from memoryNSCreateObjectFileImageFromMemory, NSLinkModuleT1620
Transform data with XOR in a loopA loop containing XOR of two different registers or with memory—
Contain crypto constantsThe AES S-box, the ChaCha / Salsa20 constant, SHA-256, MD5 or CRC32 tables—

There are 64 rules in all. They cover persistence (LaunchAgents, login items, cron, shell start-up files) and credential access (keychain, browser stores, fake password dialogs). They also cover collection, discovery, command-and-control, Gatekeeper and quarantine tampering, privilege escalation and process injection.

A function match means the evidence sits in one function: the call to ptrace and the constant 31 in the same body. A whole-file match means the imports or strings are present, but no single walked function ties them together. That is common when the code is tiny, or when the strings are only passed around as data. The five sample files only print a greeting, so all their matches are whole-file: their infostealer strings are inert data that no code uses.

Anti-debugging, a worked example

PT_DENY_ATTACH is the oldest anti-debugging trick on macOS, and it shows why rules need more than the import table.

  • The plain version imports ptrace and calls it with 31. Both features are in one function, so the rule matches at function level.
  • The evasive version never imports ptrace. It loads the system-call number 26 into x16 and executes svc #0x80 on arm64, or puts 0x2000000 + 26 in rax and executes syscall on x86_64. The analysis tracks the register and reports both "invoke a system call directly" and the debugger rule.

The same register tracking resolves sysctl queries. A function that builds the query {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()} and then tests bit 11 of the result is checking whether a debugger is attached.

Stack strings

A stack string is text written into a function's stack frame with immediate values: four or eight bytes per store instruction on x86_64, assembled in registers first on arm64. The analysis follows the stores, rebuilds the buffer and keeps the results that look like text: at least six printable characters, at least four distinct ones, including a letter.

Not every stack string is hidden on purpose. Compilers initialise short local strings the same way, so the Code analysis tab hides results that look like data and offers to show them. Found stack strings also feed the IOC list, so a URL or path rebuilt this way is extracted like any other. Each result links to its function in the Code tab.

What decoding cannot do

Following instructions without running them has limits:

  • Strings decrypted by a loop at run time stay encrypted, though the loop itself often matches the XOR rule.
  • Packed code (UPX) and FairPlay-encrypted code have to be unpacked or decrypted first.
  • Very large binaries are analysed up to a budget of three million instructions. The listing itself always covers everything.

The best-known open-source tools for this job, capa and FLOSS, were built for Windows PE files first. A browser-based analyser for the Mach-O side means a Mac sample gets the same first look, from any machine, without leaving it.

From a match to an answer

  1. Open Code analysis (or run it for the whole batch) and read the capabilities grouped by namespace.
  2. Prefer function matches: they name an address. Click it to open the function in Code.
  3. In the listing, follow calls and references: every import, selector and string is named.
  4. Export the ATT&CK Navigator layer: it combines what the file declares (entitlements, libraries, strings) with what the code does.

The indicators that come out of this, stack strings included, flow into the IOC exports: see from a Mach-O file to IOCs.

Frequently asked questions

What is a stack string?

Text that a function writes into its own stack frame a few bytes at a time, with immediate values in store instructions, before using it. The text never appears whole in the file, so a strings tool misses it. A disassembler that follows those stores can put it back together.

What is PT_DENY_ATTACH?

Request 31 of ptrace on macOS. A process that calls ptrace(PT_DENY_ATTACH, 0, 0, 0) can no longer be attached by a debugger, and if one is already attached the process exits. Malware uses it, often through a raw system call so that ptrace never appears among its imports.

Does the code analysis run the program?

No. It decodes instructions and follows them statically: it never executes, maps or emulates the code. Strings that are only decrypted at run time stay hidden, and that is the trade-off for analysing an unknown file safely in a browser.

Related articles

A walkthrough of Mach-O Parser on five harmless samples: triage, signatures, entitlements, a dropper's payload, compare, YARA and exports. Nothing uploaded.
Where Mac malware keeps its binaries, how to copy them without running them and without losing the quarantine attribute, and how to package an .app, a .dmg or a .pkg for analysis on another machine.
How a Mach-O is code-signed, which codesign --verify checks work offline, and what ad-hoc, linker-signed and Developer ID signatures really mean.