In Frida how can I call a Swift.NativeFunction that takes a Swift String?

  Kiến thức lập trình

I have this Swift code:

@objc
public class Write: NSObject {
    public let db: Database

    init(db: Database) {
        self.db = db
    }
}

public extension Write {
    func execute(sql: String) {
        db.execute(sql);
    }
}

This is my attempt at a Frida script to call the execute method inside a transaction block:

const writeBlock = new ObjC.Block({
    retType: 'void',
    argTypes: ['object'],
    implementation: function(write) {
    // We get the write instance from the transaction block

    var executeAddressByName = Module.findExportByName("MyModule","$s23MyModule10WriteC7execute3sqlWlE");

    // Build swift structs
    var StringStruct = Swift.structs.String;

    var executeSQL = Swift.NativeFunction(executeAddressByName, 'void', ['pointer', StringStruct]);

    const sqlCommand = `DROP TABLE 'tableName';`;

    // Allocate memory for the SQL string and encode it as UTF-8.
    var sqlStringPointer = Memory.allocUtf8String(sqlCommand);
    var NSString = ObjC.classes.NSString;

    // Convert the SQL command to an NSString.
    var sqlString = NSString["+ stringWithUTF8String:"](sqlStringPointer);

    executeSQL(write.handle, sqlString);
}

My questions are:

  1. NSString obviously wont work, how can I build a Swift String?
  2. Since execute is an instance method, is my signature correct?

LEAVE A COMMENT