MASTG-TECH-0095: 方法 Hook
Frida¶
在 执行跟踪中,当我们在 Safari 中导航到某个网站时,我们使用了 frida-trace,发现调用了 initWithURL:
方法来初始化一个新的 URL 请求对象。我们可以在 Apple 开发者网站上查找此方法的声明
- (instancetype)initWithURL:(NSURL *)url;
利用这些信息,我们可以编写一个 Frida 脚本来拦截 initWithURL:
方法,并打印传递给该方法的 URL。 完整的脚本如下。 确保您阅读代码和内联注释,以了解发生了什么。
import sys
import frida
# JavaScript to be injected
frida_code = """
// Obtain a reference to the initWithURL: method of the NSURLRequest class
var URL = ObjC.classes.NSURLRequest["- initWithURL:"];
// Intercept the method
Interceptor.attach(URL.implementation, {
onEnter: function(args) {
// Get a handle on NSString
var NSString = ObjC.classes.NSString;
// Obtain a reference to the NSLog function, and use it to print the URL value
// args[2] refers to the first method argument (NSURL *url)
var NSLog = new NativeFunction(Module.findExportByName('Foundation', 'NSLog'), 'void', ['pointer', '...']);
// We should always initialize an autorelease pool before interacting with Objective-C APIs
var pool = ObjC.classes.NSAutoreleasePool.alloc().init();
try {
// Creates a JS binding given a NativePointer.
var myNSURL = new ObjC.Object(args[2]);
// Create an immutable ObjC string object from a JS string object.
var str_url = NSString.stringWithString_(myNSURL.toString());
// Call the iOS NSLog function to print the URL to the iOS device logs
NSLog(str_url);
// Use Frida's console.log to print the URL to your terminal
console.log(str_url);
} finally {
pool.release();
}
}
});
"""
process = frida.get_usb_device().attach("Safari")
script = process.create_script(frida_code)
script.load()
sys.stdin.read()
在 iOS 设备上启动 Safari。 在连接的主机上运行上述 Python 脚本,并打开设备日志(如“iOS 基本安全测试”一章中的“监控系统日志”部分所述)。 尝试在 Safari 中打开一个新 URL,例如 https://github.com/OWASP/mastg;您应该在日志和终端中看到 Frida 的输出。
当然,这个例子只说明了您可以使用 Frida 做的事情之一。 要释放该工具的全部潜力,您应该学习使用其 JavaScript API。 Frida 网站的文档部分包含一个 教程 和 示例,用于在 iOS 上使用 Frida。