• Stars
    star
    304
  • Rank 137,274 (Top 3 %)
  • Language
    C
  • Created almost 4 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A guide for emulating macOS arm64e on an x86-based host.

macos-arm64-emulation

Use the following guide to download and configure all of the necessary tools and files for emulating the macOS arm64e kernel. The guide begins in this project's root directory, ie. the same directory as this README file.

Install decompression tools

Get, patch, and build xar:

git clone https://github.com/mackyle/xar.git
cd xar/xar
sed -i 's/OpenSSL_add_all_ciphers/OPENSSL_init_crypto/g' configure.ac
cat > ext2.patch << EOF
--- ./lib/ext2.c
+++ ./lib/ext2.c
@@ -140,8 +140,10 @@
    if(! (flags & ~EXT2_NOCOMPR_FL) )
        x_addprop(f, "NoCompBlock");
 #endif
+#ifdef EXT2_ECOMPR_FL
    if(! (flags & ~EXT2_ECOMPR_FL) )
        x_addprop(f, "CompError");
+#endif
    if(! (flags & ~EXT2_BTREE_FL) )
        x_addprop(f, "BTree");
    if(! (flags & ~EXT2_INDEX_FL) )
@@ -229,8 +231,10 @@
    if( e2prop_get(f, "NoCompBlock", (char **)&tmp) == 0 )
        flags |= EXT2_NOCOMPR_FL ;
 #endif
+#ifdef EXT2_ECOMPR_FL
    if( e2prop_get(f, "CompError", (char **)&tmp) == 0 )
        flags |= EXT2_ECOMPR_FL ;
+#endif
    if( e2prop_get(f, "BTree", (char **)&tmp) == 0 )
        flags |= EXT2_BTREE_FL ;
    if( e2prop_get(f, "HashIndexed", (char **)&tmp) == 0 )
EOF
git apply --ignore-whitespace ext2.patch
./autogen.sh
make
cd ../..
XAR=./xar/xar/src/xar

Get and build lzfse:

git clone https://github.com/lzfse/lzfse.git
cd lzfse
make
cd ..
LZFSE=./lzfse/build/bin/lzfse

Getting the files

Fetch the installer package (NOTE: this is a very large ~12GB file):

wget http://swcdn.apple.com/content/downloads/00/55/001-86606-A_9SF1TL01U7/5duug9lar1gypwunjfl96dza0upa854qgg/InstallAssistant.pkg

(UPDATE: Unfortunately, Apple has removed the above link and it is no longer valid. Click here to download the files and skip to the Building QEMU section.)

Extract the kernel binaries:

cd ../../
$XAR -xf InstallAssistant.pkg SharedSupport.dmg
7z e SharedSupport.dmg 5.hfs
rm SharedSupport.dmg ._SharedSupport.dmg
7z e -so 5.hfs "Shared Support/SFR/com_apple_MobileAsset_SFRSoftwareUpdate/aabc1798a59cc185ea5a87bfd4dec012f4b7feb1.zip" > sfr.zip
7z e -so 5.hfs "Shared Support/com_apple_MobileAsset_MacSoftwareUpdate/6c799f422b6d995ccc7f3fb669fe3246fd9f61aa.zip" > mac.zip
rm 5.hfs
7z e sfr.zip AssetData/usr/standalone/update/ramdisk/arm64eSURamDisk.dmg
7z e sfr.zip AssetData/boot/Firmware/all_flash/DeviceTree.j273aap.im4p
7z e sfr.zip AssetData/boot/kernelcache.release.j273

Decode the kernel binaries:

git clone https://github.com/alephsecurity/xnu-qemu-arm64-tools.git
SCRIPTS=xnu-qemu-arm64-tools/bootstrap_scripts
python $SCRIPTS/asn1kerneldecode.py kernelcache.release.j273 kernelcache.release.j273.asn1decoded
python $SCRIPTS/asn1rdskdecode.py arm64eSURamDisk.dmg arm64eSURamDisk.dmg.asn1decoded
python $SCRIPTS/asn1dtredecode.py DeviceTree.j273aap.im4p DeviceTree.j273aap.im4p.asn1decoded
$LZFSE -decode -i kernelcache.release.j273.asn1decoded -o kernelcache.release.j273.out
$LZFSE -decode -i DeviceTree.j273aap.im4p.asn1decoded -o DeviceTree.j273aap.im4p.out
cp arm64eSURamDisk.dmg.asn1decoded arm64eSURamDisk.dmg.out

Patching the Device Tree

Build dtetool and patch the device tree file:

cd dtetool
./build.sh
./dtetool ../DeviceTree.j273aap.im4p.out -d dtediff_20C69 -o ../DeviceTree.j273aap.im4p.out.patched
cd ..

Expanding the ramdisk in macOS

This step can only be done on a macOS system. Copy the ramdisk onto a macOS system and expand it:

hdiutil resize -size 1.5G -imagekey diskimage-class=CRawDiskImage arm64eSURamDisk.dmg.out

Getting the user binaries

Copy the expanded ramdisk back onto the Linux system and extract the user binaries:

7z e mac.zip AssetData/Restore/022-10310-098.dmg
rm mac.zip
7z e 022-10310-098.dmg "3 - Apple_APFS"
rm 022-10310-098.dmg

Mount the filesystem and ramdisk to two new directories:

mkdir apfs ramdisk
apfs-fuse -o allow_other "3 - Apple_APFS" apfs
sudo mount -t hfsplus -o force,rw arm64eSURamDisk.dmg.out ramdisk

Transfer the user binaries to ramdisk:

sudo cp -rn apfs/root/bin/* ramdisk/bin/
sudo cp -rn apfs/root/sbin/* ramdisk/sbin/
sudo cp -rn apfs/root/usr/bin/* ramdisk/usr/bin/
sudo cp -rn apfs/root/usr/sbin/* ramdisk/usr/sbin/

Remove all existing launchd profiles and create a profile for bash:

sudo rm -rf ramdisk/System/Library/LaunchDaemons/*
cat > com.apple.bash.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>com.apple.bash</string>
        <key>Umask</key>
        <integer>0</integer>
        <key>RunAtLoad</key>
        <true/>
        <key>ProgramArguments</key>
        <array>
                <string>/bin/bash</string>
        </array>
        <key>StandardInPath</key>
        <string>/dev/console</string>
        <key>StandardOutPath</key>
        <string>/dev/console</string>
        <key>StandardErrorPath</key>
        <string>/dev/console</string>
        <key>POSIXSpawnType</key>
        <string>Interactive</string>
        <key>EnablePressuredExit</key>
        <false/>
        <key>UserName</key>
        <string>root</string>
</dict>
</plist>
EOF 
sudo cp com.apple.bash.plist ramdisk/System/Library/LaunchDaemons/

Unmount the disk images:

sudo umount apfs ramdisk

Building QEMU

Download, extract, and patch the QEMU 5.1.0 source:

wget https://download.qemu.org/qemu-5.1.0.tar.xz
tar xf qemu-5.1.0.tar.xz
mv qemu-5.1.0 xnu-qemu-arm64-5.1.0
git apply xnu-qemu-arm64-5.1.0.diff

Configure and build the source:

cd xnu-qemu-arm64-5.1.0
./configure --target-list=aarch64-softmmu --disable-capstone --disable-pie --disable-slirp
make -j6
cd ..

Modify the -j6 option according to the number of cores on your CPU times 1.5.

Start the emulator

Start the emulator with the following script:

./xnu-qemu-arm64-5.1.0/aarch64-softmmu/qemu-system-aarch64 \
-M macos11-j273-a12z,\
kernel-filename=kernelcache.release.j273.out,\
dtb-filename=DeviceTree.j273aap.im4p.out.patched,\
ramdisk-filename=arm64eSURamDisk.dmg.out,\
kern-cmd-args="kextlog=0xfff cpus=1 rd=md0 serial=2 -noprogress",\
xnu-ramfb=off \
-cpu max \
-m 6G \
-serial mon:stdio \
-nographic \

More Repositories

1

CyBot

Open Source Threat Intelligence Chat Bot
Python
313
star
2

IntroductionToMachineLearningForSecurityPros

Example code for our book Introduction to Artificial Intelligence for Security Professionals
Python
150
star
3

SMBTrap

Tools developed to test the Redirect to SMB issue
Python
83
star
4

mitmcanary

Tool/service to detect Man in the Middle attacks with Canary Requests
Python
55
star
5

winapi-deobfuscation

Towards Generic Deobfuscation of Windows API Calls
Python
50
star
6

MarkovObfuscate

Use Markov Chains to obfuscate data as other data
Python
50
star
7

Ablation

Ablation is a tool for augmenting static analysis by extracting information at runtime, and importing it into IDA. It can resolve virtual calls, reveal interesting code, exclude heavily traversed regions, identify untested or undocumented features, visually diff samples, or perform root cause analysis simply by running samples. My favourite however is the virtual call resolution with fully interactive x-refs. It's simple, elegant, and disassembled C++ reads like C! It helps me time and time again.
C++
45
star
8

NMAP-Cluster

Clustering NMAP XML results to help make sense of large scan results.
JavaScript
33
star
9

PyPackerDetect

A malware dataset curation tool which helps identify packed samples.
Python
29
star
10

GetNETGUIDs

Extract GUIDs from .NET assemblies
Python
21
star
11

python-cyapi

This Library provides python bindings to interact with the Cylance API.
Python
20
star
12

IDPanel

Identify botnet panels with Ensembled Decision Trees
Python
18
star
13

rogers

Python command-line tool that uses nearest neighbor search methods for malware similarity analysis
Python
16
star
14

GeneralizedConvolutionalNeuralNets

Generalized convolutional neural network algorithm for use with point cloud data with arbitrary spatial features.
Python
7
star
15

improving-malware-detection-accuracy-by-extracting-icon-information

Code for the paper "Improving Malware Detection Accuracy by Extracting Icon Information"
Jupyter Notebook
6
star
16

GoogleAuthDotNet

A dot net based Google Authenticator Client - suitable for use in a windows environment
C#
4
star
17

REcon2016

BBS-Era Exploitation for Fun and Anachronism
C#
4
star
18

lazy-stochastic-principal-component-analysis

Code for the paper "Lazy stochastic principal component analysis"
Python
4
star
19

perturbed-sequence-model

TeX
3
star
20

Prangster

Black-Box Assessment of Pseudorandom Algorithms
C#
3
star
21

IOCs

1
star