Jump to content

[IL2CPP] ICallHook - Unity Internal Hooking: MonoBehaviour, Time, Input & Much More


2 posts in this topic

Recommended Posts

Posted

 

 

ICallHook - Tutorial

Hello everyone I am Rednick16 (Red16) its been a while since I last made a tutorial so stay with me if this is a bit hard to follow along with.

 

Ann ale (Lets begin).

 

By now I am sure everyone participating in the unity modding scene have heard about IL2CPP (Intermediate Language To C++)

Okay so let's actually begin. I am going to try and keep this tutorial straight forward and easy as possible to follow along with.
In this tutorial you will be learning how to hook the following icalls. I should also mention that this does not need JIT (Just-In-Time).

UnityEngine.Camera::get_fieldOfView()
  
UnityEngine.Time::get_timeScale()
UnityEngine.Time::set_timeScale(System.Single)

UnityEngine.Input::GetKeyUpInt(UnityEngine.KeyCode)
UnityEngine.Input::GetKeyDownInt(UnityEngine.KeyCode)

 

The hooking method.

See the spoiler box for a thorough tutorial on how this was achieved and why it works. 

NOTE: Timing is everything with this method.  You must be very precise with where you setup the hook calls, for example the best place to put it would be in il2cpp_init ref:  https://github.com/Rednick16/ICallHook/blob/a4cb7a17ac1abc9187080f0d7afea8ee6cd7cc46/Tweak.x#L67

#include <stdbool.h>
#include <dlfcn.h>

void* il2cpp_library_get_handle(void)
{
    static void *handle = NULL;
#if defined(__ANDROID__)
    if (NULL == handle)
        handle = dlopen("libil2cpp.so", RTLD_NOLOAD | RTLD_NOW | RTLD_GLOBAL);
    return handle;
#elif defined(__APPLE__)
    return RTLD_DEFAULT;
#else
#error "Unsupported target"
#endif
}

bool il2cpp_hook_icall(const char* name, void* hook, void** old)
{
    if (name == NULL || hook == NULL)
        return false;
    
    /* NOTE: resolve these ahead of time if possible, dlsym is really slow, but should be fine to use here
     since we only use these at startup anyways */
    void *(*il2cpp_resolve_icall)(const char *name) = NULL;
    if (il2cpp_resolve_icall == NULL)
        il2cpp_resolve_icall = (void * (*)(const char*))dlsym(il2cpp_library_get_handle(), "il2cpp_resolve_icall");
    
    void (*il2cpp_add_internal_call)(const char* name, void* methodPointer) = NULL;
    if (!il2cpp_add_internal_call)
        il2cpp_add_internal_call = (void (*)(const char*, void*))dlsym(il2cpp_library_get_handle(), "il2cpp_add_internal_call");
    
    if (il2cpp_resolve_icall == NULL || il2cpp_add_internal_call == NULL)
        return false;
    
    // The actual impl
    
    void* resolved = il2cpp_resolve_icall(name);
    if (!resolved) return false;
    
    if (old)
        *old = resolved;
    
    il2cpp_add_internal_call(name, hook);
    
    return true;
}
Spoiler

Swaps a registered function in the ICallMap

HOW IT WORKS: Unity registers internal calls via il2cpp_add_internal_call during engine startup, building a map of name -> fptr (s_InternalCalls). When managed code calls an icall, il2cpp resolves it through il2cpp_resolve_icall.

THE FEAT: Stumbled upon this while poking around UnityEngine.Time::get_timeScale() in IDA Pro. Noticed the generated code was doing this:

float __fastcall UnityEngine_Time__get_timeScale(const MethodInfo *method)
{
    void *v1; // x0
    
    v1 = off_340F6E0; /* cached static ptr */
    if ( !off_340F6E0 )
    {
        /* first call resolves icall by name via il2cpp_resolve_icall */
        v1 = (void *)sub_886C28("UnityEngine.Time::get_timeScale()"); // -> il2cpp_resolve_icall
        off_340F6E0 = v1; /* icall gets cached here */
    }
    /* subsequent calls directly use the cached fptr */
    return ((float (*)(void))v1)();
}

Lazy caching. The function resolves the icall once, caches it. That means if we replace the icall BEFORE the first call, our hook survives otherwise the hook fails.

That's when it clicked. I had no idea the engine was doing this - saw the pattern, said "wtf", and instantly knew I could abuse it.

So by calling il2cpp_add_internal_call again with the same name, we overwrite the existing entry. Just an entry swap in the ICallMap. The original function pointer is backed up so we can call through if needed.

Here's why this works: ref https://github.com/MlgmXyysd/libil2cpp/blob/df0b51462fbbfa111ea3e741a7edf3795536ff2b/libil2cpp/Unity_2021.3/2021.3.16f1/vm/InternalCalls.cpp#L15

void InternalCalls::Add(const char* name, Il2CppMethodPointer method)
{
    IL2CPP_ASSERT(method);
    s_InternalCalls[name] = method; // std::map, last writer wins
}

No duplicate check. Nothing prevents us from calling il2cpp_add_internal_call twice with the same name. Last writer wins.

NOTE: Must only be used after il2cpp has been initialized. Unity registers icalls during engine startup; using this before il2cpp is initialized will fail.

Lets have some fun and make some hooks

static float (*_get_fieldOfView)(void *) = NULL;
static float hooked_get_fieldOfView(void *_this) 
{
	float fov = _get_fieldOfView(_this);
	// printf(@"--> get_fieldOfView(): %f\n", fov);
    return fov;
}

static float (*_get_timeScale)() = NULL;
static float hooked_get_timeScale()
{
	float scale = _get_timeScale();
    // printf(@"--> get_timeScale(): %f\n", scale);
    return scale;
}

static bool (*_GetKeyUpInt)(int keyCode) = NULL;
static bool hooked_GetKeyUpInt(int keyCode) 
{
	bool status = _GetKeyUpInt(keyCode);
    // printf(@"--> GetKeyUpInt(): up: %s\n", status ? "YES" : "NO");
    return status;
}

static bool (*_GetKeyDownInt)(int keyCode) = NULL;
static bool hooked_GetKeyDownInt(int keyCode) 
{
	bool status = _GetKeyDownInt(keyCode);
    // printf(@"--> GetKeyDownInt(): down: %s\n", status ? "YES" : "NO");
    return status;
}

void Il2CppDidInit(void) 
{
    // Called after il2cpp_init completes

	Il2CppHookInternalCall("UnityEngine.Camera::get_fieldOfView()", 
                           (void *)&hooked_get_fieldOfView, 
                           (void **)&_get_fieldOfView);

	Il2CppHookInternalCall("UnityEngine.Time::get_timeScale()", 
                           (void *)&hooked_get_timeScale, 
                           (void **)&_get_timeScale);

	Il2CppHookInternalCall("UnityEngine.Input::GetKeyUpInt(UnityEngine.KeyCode)", 
                           (void *)&hooked_GetKeyUpInt, 
                           (void **)&_GetKeyUpInt);
    
    Il2CppHookInternalCall("UnityEngine.Input::GetKeyDownInt(UnityEngine.KeyCode)", 
                           (void *)&hooked_GetKeyDownInt, 
                           (void **)&_GetKeyDownInt);
}


Thats it.
You guys were probably expecting more from me but this all I got haha
Test it out: https://github.com/Rednick16/ICallHook

Yep thats it there are many other techniques that can be used along side this such as methodPointer swaps on Update, LateUpdate, Start, Awake for classes deriving from MonoBehaviour, and VTables are mostly intact as well. goodby

 

Credits:
@Red16

  • Winner 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Our picks

    • All Who Wander: Roguelike RPG v1.3.1 [ +4 Cheats ] Currency Max
      Modded/Hacked App: All Who Wander: Roguelike RPG By Michael Weil
      Bundle ID: com.Frumpydoodle-Games.All-Who-Wander
      App Store Link: https://apps.apple.com/us/app/all-who-wander-roguelike-rpg/id6748367625?uo=4

      🤩 Hack Features

      - Full Unlock
      - Unlimited Gold
      - HP MAX
      - MANA MAX
      • 0 replies
    • All Who Wander: Roguelike RPG v1.3.1 [ +4 Jailed ] Currency Max
      Modded/Hacked App: All Who Wander: Roguelike RPG By Michael Weil
      Bundle ID: com.Frumpydoodle-Games.All-Who-Wander
      App Store Link: https://apps.apple.com/us/app/all-who-wander-roguelike-rpg/id6748367625?uo=4

      🤩 Hack Features

      - Full Unlock
      - Unlimited Gold
      - HP MAX
      - MANA MAX
      • 1 reply
    • Cat Island: Idle Tycoon Game v1.0.5 [ +1 Cheats ] Gems Max
      Modded/Hacked App: Cat Island: Idle Tycoon Game By Neptune Company
      Bundle ID: com.nep.tr.idle.cat.island
      App Store Link: https://apps.apple.com/us/app/cat-island-idle-tycoon-game/id6763426921?uo=4

      🤩 Hack Features

      - Gems Max
      • 1 reply
    • Cat Island: Idle Tycoon Game v1.0.5 [ +1 Jailed ] Gems Max
      Modded/Hacked App: Cat Island: Idle Tycoon Game By Neptune Company
      Bundle ID: com.nep.tr.idle.cat.island
      App Store Link: https://apps.apple.com/us/app/cat-island-idle-tycoon-game/id6763426921?uo=4

      🤩 Hack Features

      - Gems Max
      • 1 reply
    • Brobuddy v1.0.0 [ +2 Cheats ] Currency Max
      Modded/Hacked App: Brobuddy By 24 HIT Riga SIA
      Bundle ID: com.brobuddy.game
      App Store Link: https://apps.apple.com/us/app/brobuddy/id6785948193?uo=4

      🤩 Hack Features

      - Unlimited Gold
      - Unlimited Resources
      • 0 replies
    • Brobuddy v1.0.0 [ +2 Jailed ] Currency Max
      Modded/Hacked App: Brobuddy By 24 HIT Riga SIA
      Bundle ID: com.brobuddy.game
      App Store Link: https://apps.apple.com/us/app/brobuddy/id6785948193?uo=4

      🤩 Hack Features

      - Unlimited Gold
      - Unlimited Resources
      • 0 replies
    • DRAGON BALL Z DOKKAN BATTLE Japan (ドラゴンボールZ ドッカンバトル) +7 Cheats!
      Modded/Hacked App: ドラゴンボールZ ドッカンバトル By BANDAI NAMCO Entertainment Inc.
      Bundle ID: jp.co.bandainamcogames.BNGI0211
      iTunes Link: https://itunes.apple.com/jp/app/ドラゴンボールz-ドッカンバトル/id951627670


      Hack Features
      - Unlimited HP  -  (Put .0 at the back of your value: 1000.0)
      - Unlimited Damage  -  (Put .0 at the back of your value: 1000.0)
      - Unlimited Defense  -  (Put .0 at the back of your value: 1000.0)
      - Dice Hack -  [ONLY RANGE BETWEEN 1 - 6 or it will crash]  -  (Put .0 at the back of your value: 4.0)
      - Dice Hack 1, 2, 3
      - Dice Hack 4, 5, 6
      - Auto Win Battles -> Disable if you get errors.
      PUT .0 at the back of all values!
        • Like
      • 8,089 replies
    • Bunker TapTap Defense +5 Jailed Cheats
      Modded/Hacked App: Bunker TapTap Defense By Teamsparta Inc.
      Bundle ID: com.TeamSparta.brd
      App Store Link: https://apps.apple.com/us/app/bunker-taptap-defense/id6778697840?uo=4

       

      📌 Mod Requirements

      - Non-Jailbroken/Jailed or Jailbroken iPhone or iPad.
      - Sideloadly or alternatives.
      - Computer running Windows/macOS/Linux with iTunes installed.

       

      🤩 Hack Features

      - Damage Multiplier
      - Defense Multiplier
      - Gems/Gold Increase
      - Game Speed Multiplier
      - No ADS

       

      ⬇️ iOS Hack Download IPA Link


      Hidden Content

      Download via the iOSGods App







       

      📖 PC Installation Instructions

      STEP 1: Download the pre-hacked .IPA file from the link above to your computer. To download from the iOSGods App, see our iOSGods App IPA Download Tutorial which includes a video example.
      STEP 2: Download Sideloadly and install it on your Windows or Mac.
      STEP 3: Open Sideloadly on your computer, connect your iOS device, and wait until your device name appears in Sideloadly.
      STEP 4: Once your iDevice is recognized, drag the modded .IPA file you downloaded and drop it into the Sideloadly application.
      STEP 5: Enter your Apple Account email, then press “Start.” You’ll then be asked to enter your password. Go ahead and provide the required information.
      STEP 6: Wait for Sideloadly to finish sideloading/installing the hacked IPA. If there are issues during installation, please read the note below.
      STEP 7: Once the installation is complete and you see the app on your Home Screen, you will need to go to Settings -> General -> Profiles / VPN & Device Management. Once there, tap on the email you entered from step 6, and then tap on 'Trust [email protected]'.
      STEP 8: Now go to your Home Screen and open the newly installed app and everything should work fine. You may need to follow further per app instructions inside the hack's popup in-game.

      NOTE: iOS/iPadOS 16 and later, you must enable Developer Mode. For free Apple Developer accounts, you will need to repeat this process every 7 days. If you have any questions or problems, read our Sideloadly FAQ section of the topic and if you don't find a solution, please post your issue below and we'll do our best to help! If the hack does work for you, post your feedback below and help out other fellow members that are encountering issues.

       

      🙌 Credits

      - AlyssaX64

       

      📷 Cheat Video/Screenshots

      N/A
      • 4 replies
    • FIST OF THE NORTH STAR +3 Cheats
      Mod APK Game Name: FIST OF THE NORTH STAR By SEGA CORPORATION
      Rooted Device: Not Required.
      Google Play Store Link: https://play.google.com/store/apps/details?id=com.sega.HokutoRevive.en

       

      🤩 Hack Features

      - Damage Multiplier
      - Defense Multiplier
      - Instant Win

       

      ⬇️ Android Mod APK Download Link


      Hidden Content

      Download via the iOSGods App for Android







       

      📖 Android Installation Instructions

      STEP 1: Download the modded APK file from the link above using your preferred Android browser or download manager.
      STEP 2: Once the download is complete, open your file manager and locate the downloaded .apk file (usually in the Downloads folder).
      STEP 3: Tap the APK file, then select Install. If prompted, enable Install from Unknown Sources in your device settings.
      STEP 3A: If the mod includes an OBB file, extract it if it’s inside an archive. Then move the folder to: /Android/obb/
      STEP 3B: If the mod includes a DATA file, extract it if it’s archived. Then move the folder to: /Android/data/
      STEP 4: Once installed, open the game and toggle your desired cheats & features through the APK mod menu. Enjoy!

       

      NOTE: If you have any questions or issues, read our Frequently Asked Questions topic. If you still need help, post your issue below and we’ll assist you as soon as possible. If the mod works for you, please share your feedback to help other members!

       

      🙌 Credits

      - AlyssaX64

       

      📷 Cheat Video/Screenshots

      N/A

       

       iOS & iPadOS App Hacks
      If you’re looking for Non-Jailbroken & No Jailbreak required iOS IPA hacks, visit the iOS Game Cheats & Hacks or the iOSGods App for a variety of modded games and apps for non-jailbroken iOS devices.
      • 6 replies
    • No Limit Drag Racing 2 +2 Cheats
      Modded/Hacked App: No Limit Drag Racing 2 By Zach Smith
      Bundle ID: com.battlecreek.nolimit2
      iTunes Store Link: https://apps.apple.com/us/app/no-limit-drag-racing-2/id1563031984?uo=4


      Mod Requirements:
      - Non-Jailbroken/Jailed or Jailbroken iPhone/iPad/iPod Touch.
      - Sideloadly / Cydia Impactor or alternatives.
      - A Computer Running Windows/Mac/Linux with iTunes installed.


      Hack Features:
      - Unlimited Gold
      - Unlimited Money


      Jailbreak required hack(s): https://iosgods.com/forum/5-game-cheats-hack-requests/
      Modded Android APK(s): https://iosgods.com/forum/68-android-section/
      For more fun, check out the Club(s): https://iosgods.com/clubs/


      iOS Hack Download Link:

      Hidden Content
      Download via the iOSGods App







      PC Installation Instructions:
      STEP 1: If necessary, uninstall the app if you have it installed on your iDevice. Some hacked IPAs will install as a duplicate app. Make sure to back it up so you don't lose your progress.
      STEP 2: Download the pre-hacked .IPA file from the link above to your computer. To download from the iOSGods App, see this tutorial topic.
      STEP 3: Download Sideloadly and install it on your PC.
      STEP 4: Open/Run Sideloadly on your computer, connect your iOS Device, and wait until your device name shows up.
      STEP 5: Once your iDevice appears, drag the modded .IPA file you downloaded and drop it inside the Sideloadly application.
      STEP 6: You will now have to enter your iTunes/Apple ID email login & then your password. Go ahead and enter the required information.
      STEP 7: Wait for Sideloadly to finish sideloading/installing the hacked IPA.
      STEP 8: Once the installation is complete and you see the app on your Home Screen, you will need to go to Settings -> General -> Profiles & Device Management. Once there, tap on the email you entered from step 6, and then tap on 'Trust [email protected]'.
      STEP 9: Now go to your Home Screen and open the newly installed app and everything should work fine. You may need to follow further per app instructions inside the hack's popup in-game.
      NOTE: For free Apple Developer accounts, you will need to repeat this process every 7 days. Using a disposable Apple ID for this process is suggested but not required. Jailbroken iDevices can also use Sideloadly to install the IPA with AppSync. Filza & IPA Installer (or alternatives) from Cydia also work. If you have any questions or problems, read our Sideloadly FAQ section of the topic and if you don't find a solution, post your issue down below and we'll do our best to help! If the hack does work for you, post your feedback below and help out other fellow members that are encountering issues.


      Credits:
      - @Zahir


      Cheat Video/Screenshots:

      N/A
      • 619 replies
    • APEX Racer +6 Jailed Cheats
      Modded/Hacked App: APEX Racer By G63 Holdings LTD
      Bundle ID: com.pixeldev.APEXRacers
      iTunes Store Link: https://apps.apple.com/us/app/apex-racer/id1668705222?uo=4

       


      Mod Requirements:
      - Non-Jailbroken/Jailed or Jailbroken iPhone/iPad/iPod Touch.
      - Sideloadly / Cydia Impactor or alternatives.
      - A Computer Running Windows/macOS/Linux with iTunes installed.


      Hack Features:
      - Unlimited currency
        • Like
      • 454 replies
    • Galaxy Attack: Space Shooter +3 Jailed Cheats
      Modded/Hacked App: Galaxy Attack: Space Shooter By RocketAds Ltd
      Bundle ID: com.game.space.shooter2
      iTunes Store Link: https://apps.apple.com/us/app/galaxy-attack-space-shooter/id1225548580?uo=4


      Mod Requirements:
      - Non-Jailbroken/Jailed or Jailbroken iPhone/iPad/iPod Touch.
      - Sideloadly / Cydia Impactor or alternatives.
      - A Computer Running Windows/macOS/Linux with iTunes installed.


      Hack Features:
      - 1 Hit Kill
      - Never Die
      - Instant Kill


      Jailbreak required hack(s): 


      iOS Hack Download IPA Link:

      Hidden Content

      Download via the iOSGods App








      PC Installation Instructions:
      STEP 1: If necessary, uninstall the app if you have it installed on your iDevice. Some hacked IPAs will install as a duplicate app. Make sure to back it up so you don't lose your progress.
      STEP 2: Download the pre-hacked .IPA file from the link above to your computer. To download from the iOSGods App, see this tutorial topic.
      STEP 3: Download Sideloadly and install it on your PC.
      STEP 4: Open/Run Sideloadly on your computer, connect your iOS Device, and wait until your device name shows up.
      STEP 5: Once your iDevice appears, drag the modded .IPA file you downloaded and drop it inside the Sideloadly application.
      STEP 6: You will now have to enter your iTunes/Apple ID email login, press "Start" & then you will be asked to enter your password. Go ahead and enter the required information.
      STEP 7: Wait for Sideloadly to finish sideloading/installing the hacked IPA. If there are issues during installation, please read the note below.
      STEP 8: Once the installation is complete and you see the app on your Home Screen, you will need to go to Settings -> General -> Profiles/VPN & Device Management. Once there, tap on the email you entered from step 6, and then tap on 'Trust [email protected]'.
      STEP 9: Now go to your Home Screen and open the newly installed app and everything should work fine. You may need to follow further per app instructions inside the hack's popup in-game.

      NOTE: iOS/iPadOS 16 and later, you must enable Developer Mode. For free Apple Developer accounts, you will need to repeat this process every 7 days. Jailbroken iDevices can also use Sideloadly/Filza/IPA Installer to normally install the IPA with AppSync. If you have any questions or problems, read our Sideloadly FAQ section of the topic and if you don't find a solution, please post your issue down below and we'll do our best to help! If the hack does work for you, post your feedback below and help out other fellow members that are encountering issues.


      Credits:
      - AlyssaX64


      Cheat Video/Screenshots:

      N/A
        • Informative
      • 251 replies
×
  • Create New...

Important Information

We would like to place cookies on your device to help make this website better. The website cannot give you the best user experience without cookies. You can accept or decline our cookies. You may also adjust your cookie settings. Privacy Policy - Guidelines