Skip to content

Instantly share code, notes, and snippets.

@klmitchell2
Created March 29, 2020 16:21
Show Gist options
  • Save klmitchell2/98c8bd1b088668c680a04d92d9effb12 to your computer and use it in GitHub Desktop.
Save klmitchell2/98c8bd1b088668c680a04d92d9effb12 to your computer and use it in GitHub Desktop.
Test function that injects a simple script into a running process via Frida-swift
func testFullCycle() {
let expectation = self.expectation(description: "Got message from script")
class TestDelegate : ScriptDelegate {
let expectation: XCTestExpectation
var messages = [Any]()
init(expectation: XCTestExpectation) {
self.expectation = expectation
}
func scriptDestroyed(_: Script) {
print("destroyed")
}
func script(_: Script, didReceiveMessage message: Any, withData data: Data?) {
print("didReceiveMessage")
messages.append(message)
if messages.count == 2 {
expectation.fulfill()
}
}
}
let delegate = TestDelegate(expectation: expectation)
let manager = DeviceManager()
var script: Script? = nil
manager.enumerateDevices { result in
let devices = try! result()
let localDevice = devices.filter { $0.kind == Device.Kind.usb }.first!
localDevice.spawn("com.amazon.Amazon") { (result) in
let pid = try! result()
localDevice.attach(pid) { result in
localDevice.resume(pid)
let session = try! result()
session.createScript("console.log(\"hello\"); send(1337);", name: "test") { result in
let s = try! result()
s.delegate = delegate
s.load() { result in
_ = try! result()
print("Script loaded")
}
script = s
}
}
}
}
self.waitForExpectations(timeout: 5.0, handler: nil)
print("Done with script \(script.debugDescription), messages: \(delegate.messages)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment