Search code examples
iphonexcode5sandboxjailbreakios7.1

How to get Root privileges developing iPhone app using Xcode


I cannot access /var/mobile/Library/SMS etc. in xcode even if the iphone is jailbroken.

Apple sandboxing doesn't allow root access.

A Similar Answer is here Gaining root permissions on iOS for NSFileManager (Jailbreak) .

But i am a newbie. Except for the first two steps, i don't understand what he is doing. That would be great if anyone can explain that answer.

OR

Is there any other way to access /private/var/mobile/Library....?

Xcode: 5.0.1
Device: iPhone 5s with iOS 7.1
OSX: 10.9.2


Solution

  • 3 . Create a copy of the executable file in the app bundle.

    When you build a project in Xcode, it will produce an output directory. This varies by machine, so you'll have to search your filesystem. However, if your app is named HelloWorld, normally, you'd have a directory named HelloWorld.app. This is what the answer is referring to as the app bundle. From the command line (or using your Mac's Finder), go inside HelloWorld.app and make a copy of the HelloWorld executable file. Normally, I name the copy MobileHelloWorld.

    4 . Open the original executable file and replace its content with this script:

    #!/bin/bash
    dir=$(dirname "$0")
    exec "${dir}"/COPIED_EXECUTABLE_NAME "$@"

    Directly launching a root app fails on iOS. Therefore we replace the app's main executable with a script that launches the root executable.

    I guess I would have described this step differently. You can delete the file. Create a new script with the same filename (HelloWorld) and edit it to include the lines above, starting with #!/bin/bash. Of course, COPIED_EXECUTABLE_NAME would be replaced with MobileHelloWorld in my example.

    So, iOS will launch your script directly, instead of your executable. However, your script will then launch your executable and because of the permissions you've given those files, your running executable will have root privileges.

    5 . In terminal, navigate to the app bundle.

    You're probably already in this "bundle" directory. (HelloWorld.app)

    6 . chmod 0775 the original executable file and chmod 6775 the copied executable file.

    Issue the chmod command so that the HelloWorld file has 775 permissions (rwxrwxr-x). The MobileHelloWorld file should then have 6775 permissions (rwsrwsr-x).

    7 . Copy the app bundle to /Applications to a device. Restart SpringBoard and you should be good to go. If the app doesn't launch then repeat step 5 & 6 on the device.

    Using whatever tool you like (I just use scp since my device is jailbroken with openssh installed), copy the entire HelloWorld.app folder to the iOS device. So, you would have a folder named: /Applications/HelloWorld.app/ which contains the bash script, the copied/renamed executable, and any other bundle resources (.png files, .xib files, etc.) your app contains.

    Example

    If you have a jailbroken device, install openssh and ssh into the phone, then check out how the Cydia app itself accomplishes this. You can view the /Applications/Cydia.app/Cydia script file, which launches the MobileCydia executable with root privileges.

    Another Way

    Actually, if you only want to access /var/mobile/Library, that doesn't require root access. That directory is owned by the mobile user, so root isn't necessary. What is necessary is escaping the normal iOS 3rd-party app sandbox. To do that, simply copy your HelloWorld.app folder and its contents to the /Applications/ folder on your device. Apps installed there, as opposed to /var/mobile/Applications won't have such tight sandbox restrictions.

    So, none of that copying of the executable, inserting a bash script, are necessary. Steps 3 through 6 can be skipped.

    Hope that helps. Sorry for my snarky comment.