Introduction

Frida is a powerful dynamic instrumentation toolkit that enables developers, security researchers, and enthusiasts to inspect and modify applications in real-time. While its primary use is in reverse engineering and security analysis, it’s also a handy tool for debugging and understanding complex apps. In this blog post, I will walk you through using Frida with an iOS app using an example project.


Prerequisites

Before diving in, ensure you have the following:

  • A macOS environment with Xcode installed.
  • An iOS device (jailbroken or non-jailbroken, depending on your use case).
  • Frida installed on your system.
  • Basic knowledge of iOS app development.
  • Python (optional but helpful for scripting Frida).

Step 1: Setting Up Frida

  1. Install Frida on Your System
    Frida can be installed using pip:
   pip install frida-tools
  1. Check Frida Installation
    Verify that Frida is installed correctly:
   frida --version

This should return the version number.

  1. Install Frida on the iOS Device
    For jailbroken devices, use the following command to install the Frida server:
   ssh root@<device-ip>
   apt update
   apt install frida-server

Replace <device-ip> with your device’s IP address.

Start the Frida server:

   frida-server &
  1. Using Frida Without Jailbreak
    If you are using a non-jailbroken device, you will need to leverage Frida’s gadget library. Here’s how you can do it:
  • Download the Frida gadget library from the official Frida releases page.
  • Embed the frida-gadget.dylib file into your app’s binary during development.
  • Modify your app’s Info.plist to load the Frida gadget:
    xml <key>LD_PRELOAD</key> <string>@executable_path/frida-gadget.dylib</string>
  • Ensure proper code signing of the modified app before deploying it onto your device.
  • Use Frida commands as usual to interact with the app. Note: This approach requires access to the app’s source code or a debuggable build.

Step 2: Create an Example iOS App

To demonstrate Frida’s capabilities, let’s create a simple iOS app in Xcode:

  1. Open Xcode and create a new project.
  2. Choose “App” and click “Next”.
  3. Provide a name (e.g., FridaDemoApp), select Swift or Objective-C, and set the interface to “Storyboard”.
  4. Add a basic UI with a button and a label.
  5. Implement a simple function triggered by the button to update the label’s text.
   @IBAction func buttonTapped(_ sender: UIButton) {
       label.text = "Original Text Updated"
   }

Build and run the app on your device.


Step 3: Hook the App with Frida

  1. Find the App’s Process
    Use Frida to list the processes running on your iOS device:
   frida-ps -U

Identify your app’s process ID (PID).

  1. Attach to the App
    Attach Frida to the app:
   frida -U -n FridaDemoApp
  1. Write a Frida Script
    Create a simple Frida script to modify the behavior of the buttonTapped function:
   var hook = ObjC.classes.ViewController["- buttonTapped:"];
   Interceptor.attach(hook.implementation, {
       onEnter: function (args) {
           console.log("buttonTapped function called!");
       },
       onLeave: function (retval) {
           console.log("Original return value: " + retval);
           retval.replace(ObjC.classes.NSString.stringWithString_("Modified by Frida"));
       }
   });
  1. Run the Script
    Execute the script while the app is running:
   frida -U -n FridaDemoApp -s script.js

Replace script.js with the name of your script file.

  1. Observe Changes
    Interact with the app’s button and observe how the behavior changes as per the Frida script.

Step 4: Automate with Python

For repetitive tasks, you can automate Frida scripting using Python:

import frida

def on_message(message, data):
    print(message)

session = frida.get_usb_device().attach("FridaDemoApp")
script = session.create_script(open("script.js").read())
script.on('message', on_message)
script.load()
input("Press Enter to exit...")

Run the Python script:

python frida_automation.py

Best Practices and Considerations

  1. Always ensure you have permission to analyze or modify an app.
  2. Be mindful of legal and ethical considerations when using Frida.
  3. Use Frida for educational and security testing purposes.

Conclusion

Frida is a versatile tool for iOS app debugging, reverse engineering, and security analysis. With the example project above, you’ve seen how to hook into an iOS app and modify its behavior dynamically. This is just the beginning—Frida’s capabilities extend far beyond basic hooks, allowing you to explore and manipulate apps in depth.

Happy hacking!

Written By
Fareeth John

I’m working as a Sr. Solution Architect in Akamai Technologies. I have more than 13 years of experience in the Mobile app development industry. Worked on different technologies like VR, Augmented reality, OTT, and IoT in iOS, Android, flutter, and other cross-platform apps. Have worked on 45+ apps from scratch which are in the AppStore and PlayStore. My knowledge of mobile development including design/architecting solutions, app development, knowledge around backend systems,  cloud computing, CDN, Test Automation, CI/CD, Frida Pentesting, and finding mobile app vulnerabilities

Leave a Reply

Your email address will not be published. Required fields are marked *