Skip to content

How to Collect Suspicious Mac Executables Safely

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.

Published on 4 min read

TL;DR. Start from what runs by itself: the launchd jobs, login items and background tasks, then the hidden folders they point to. Copy the binaries with cp -p or ditto, so that the quarantine attribute and the download metadata come along: they are evidence too. Keep the original paths. Treat .app bundles as folders, mount disk images read-only, and expand installer packages without running their scripts. Never open a sample "to see what it does": static analysis tells you most of it without the risk.

Before you start

  • Write down where everything was. The path a binary was found at is half its story: ~/Library/Application Support/.updater/updater says something the file alone does not.
  • Record the metadata before touching anything: stat -x file for the times, xattr -l file for the extended attributes, codesign -dvvv file for the signature. Reading a signature runs nothing.
  • Expect the protections to react. XProtect, XProtect Remediator or an EDR may quarantine or delete a known sample while you copy it. Either work from a read-only image of the disk, or coordinate with whoever runs the EDR.

Where to look

Most Mac malware needs to start again after a reboot, so persistence is the best place to begin.

LocationWhat runs from it
~/Library/LaunchAgents, /Library/LaunchAgentsPer-user jobs, started at login
/Library/LaunchDaemonsSystem-wide jobs, run as root at boot
Login items and background itemsListed by sudo sfltool dumpbtm on macOS 13 and later, with the developer of each
crontab -l, /usr/lib/cron/tabsOld-style scheduled jobs, still honoured
~/.zshrc, ~/.zprofile, ~/.bash_profileCommands run by every new shell

A launchd property list names its program in Program or ProgramArguments. Follow it to the binary:

for p in ~/Library/LaunchAgents/*.plist /Library/LaunchAgents/*.plist /Library/LaunchDaemons/*.plist; do
  prog=$(/usr/libexec/PlistBuddy -c 'Print :Program' "$p" 2>/dev/null ||
         /usr/libexec/PlistBuddy -c 'Print :ProgramArguments:0' "$p" 2>/dev/null)
  echo "$p -> $prog"
done

Then look where malware likes to put files, because users can write there and rarely look:

  • hidden folders under ~/Library/Application Support/;
  • /Users/Shared;
  • /tmp and /private/var/tmp;
  • the per-user temporary folder ($TMPDIR, under /private/var/folders);
  • ~/Downloads;
  • any mounted disk image.

A program whose name imitates Apple's (com.apple.softwareupdate.agent, .mdworker) but which lives in a user folder deserves a closer look.

Copy without running, keep the metadata

macOS's cp copies extended attributes, and -p also keeps the times and permissions. ditto keeps everything, ACLs and resource forks included. Both keep:

  • com.apple.quarantine: when the file was downloaded and by which application;
  • com.apple.metadata:kMDItemWhereFroms: the download URL and the referring page;
  • on recent systems, com.apple.provenance.

To collect every Mach-O file changed recently under the usual writable places, with their paths kept:

DST="$HOME/triage/macho"
sudo find /Users /Library /private/tmp /private/var/tmp /Applications \
  -xdev -type f -mtime -14 -size -200M \( -perm -u+x -o -name '*.dylib' \) 2>/dev/null |
while IFS= read -r f; do
  file -b "$f" | grep -q 'Mach-O' || continue
  mkdir -p "$DST$(dirname "$f")" && cp -p "$f" "$DST$f"
done

file reads a few bytes to recognise a Mach-O file, so nothing is executed. Hash what you collected right away (shasum -a 256), so that later copies can be checked against it.

Bundles, disk images and installer packages

  • An .app is a folder. Copy it whole with ditto. The executable is in Contents/MacOS, but the frameworks, helpers, plug-ins and Contents/Info.plist beside it matter too. So does _CodeSignature/CodeResources, which seals the bundle's other files.
  • A .dmg is a container. Mount it without Finder showing it, and read-only: hdiutil attach -nobrowse -readonly sample.dmg. Mounting runs nothing (macOS has no autorun). Then copy out what is inside.
  • A .pkg is an installer archive. pkgutil --expand-full sample.pkg out/ unpacks it without installing it and without running its scripts. Read Scripts/preinstall and postinstall as text: in malicious packages they are often the real payload.
  • App Store apps are FairPlay-encrypted. Their code cannot be read statically. The load command LC_ENCRYPTION_INFO_64 with cryptid 1 says so.

Packaging for another machine

Pack everything into one ZIP with ditto, which keeps symbolic links and stores the extended attributes inside the archive:

ditto -c -k --keepParent "$HOME/triage/macho" "$HOME/triage/macho.zip"

The convention for sharing malware is a ZIP encrypted with the password infected. It stops mail filters and antivirus on the way from eating the file, and stops anyone from opening it by accident. Mach-O Parser opens such ZIPs directly when the password is infected, malware or virus.

What not to do

  • Don't open it. Not by double-clicking it, not with open, not from Terminal "just with --help". Some samples act on any launch.
  • Don't remove the quarantine attribute (xattr -d com.apple.quarantine). It is evidence, and it is what makes Gatekeeper check the file. Malware strips it from its own files for that reason; the updater sample even carries the command as a string.
  • Don't re-sign the file to make it run elsewhere. That replaces the signature, the cdhash and the evidence that came with them.
  • Don't trust the name. A signing identifier or bundle ID starting with com.apple. proves nothing on an ad-hoc signature.

Next step

Drop the folder or the ZIP onto Mach-O Parser. It reads every file in the browser, keeps the folder layout in the list, and opens universal binaries, bundles and embedded payloads. Nothing is uploaded and nothing is executed. The walkthrough in analyze a Mach-O file in your browser shows what comes back.

Frequently asked questions

Is it safe to copy a malicious app on a Mac?

Copying does not execute anything: cp, ditto and a Finder copy only move bytes. Code runs when the program is opened, by double-clicking it, running open or launching it from Terminal, even with --help. Copy, hash and analyse; never start it to see what it does.

Should I remove the quarantine attribute before analysis?

No. com.apple.quarantine records when the file was downloaded and by which application, and it is what makes Gatekeeper check the file before it first runs. Removing it destroys evidence and a safety net at once. Malware removes it for exactly that reason.

How do I zip an .app without breaking it?

Use ditto -c -k --keepParent App.app App.zip, which is what Finder's Compress does. It keeps the bundle's symbolic links, permissions and extended attributes. A plain zip -r follows symbolic links (frameworks are full of them) and drops the extended attributes.

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.
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.
The Mach-O format for incident responders: header and flags, universal binaries, load commands, segments, imports, __LINKEDIT and what matters in triage.