Tutorial How to hook IEnumerable methods in Unity's Il2cpp

Numark

Awesome Active Platinian
Original poster
May 23, 2017
116
925
193
Hey guys, slice cast here again with another tutorial. I will show you how to hook an IEnumerable.

Let's say you found a method something like Username, Ban, Recoil, Reload, or whatever it is and it's an IEnumerable type. I don't really understand the concept of IEnumerable, but you can find out what it does by searching it.

Anyways let's get started.

We have this method like
C++:
private IEnumerator UnBanPlayer (string playerToKick)
Now if you scroll all the way down of the class, you will find something like this called
<UnbanPlayer>d__507>
Something like that.


We open that and we see some fields. One could say UserID or playername.

lets say we modify UserID and it's an integer/int type. So how do we modify it?


It's simple.

We're basically gonna hook this by calling the method with MoveNext().

C++:
//the void *instance is a self-created variable.
Void (*old_UnbanPlayer_MoveNext)(void *instance);
Void UnbanPlayer_MoveNext(void *instance) {
    //Check if instance is NULL to prevent CRASH
    if (instance != NULL)
    {
        *(int *) ((uint64_t)instance + 0x4C) = 18372819; //Return how many value
    }
    old_UnbanPlayer_MoveNext(instance);
}
Easy, now call your hooks using MSHook if using substrate.

C++:
MSHookFunction((void*)getAbsoluteAddress(OFFSETS), (void*)UnbanPlayer_MoveNext, (void**)&UnbanPlayer_MoveNext;
That's all for now, hope you enjoyed this tutorial.
If you're new to this and don't know how to do it, please don't come into the comment section and asking me for help. This is all the help you will get because I explained it for you since this is a tutorial after all. If you're a newbie, don't try the hard stuff.
 
Last edited:

DawnBreaker

Platinian
Nov 4, 2018
39
3,795
183
24
Earth
what if the offset of the methods just like this " 0x6C " the offset are shorter than usual offset? should i just put it straight into the hook?
 

HizroMxDz

1/3 Games Approved
Dec 25, 2019
92
95
53
x____x
@Tiuu I just need help with what do we hook?
Do we hook this part of an IEnumerable:
C#:
private IEnumerator UnBanPlayer (string playerToKick)
Or this part:
<UnbanPlayer>d__507>
 

HizroMxDz

1/3 Games Approved
Dec 25, 2019
92
95
53
x____x
And also do we need to include a specific header or library or some other code to use the MoveNext(); function
(Accidently posted this again, sorry😐)
 
  • Like
Reactions: DawnBreaker

Numark

Awesome Active Platinian
Original poster
May 23, 2017
116
925
193
@Tiuu I just need help with what do we hook?
Do we hook this part of an IEnumerable:
C#:
private IEnumerator UnBanPlayer (string playerToKick)
Or this part:
<UnbanPlayer>d__507>
You're looking for an IEnumerator or Enumeral method, so yeah you will need to use and hook <UnbanPlayer>d__507>
 

7.Hackers

Platinian
Apr 22, 2020
41
1,007
193
Egypt
You're looking for an IEnumerator or Enumeral method, so yeah you will need to use and hook <UnbanPlayer>d__507>
if i have an invoke method like this [Address(RVA = "0x1B5AA2C", Offset = "0x1B5AA2C", VA = "0x1B5AA2C", Slot = "12")]
public extern void Invoke(int inXP, bool lVisible);
How to use it ? i can't use pointer function cuz it needs Update method . i can't use Hooking Function cuz that invoke has int inXP, bool lVisible , it does not return like normal functions to hook it like [Address(RVA = "0x1A57F80", Offset = "0x1A57F80", VA = "0x1A57F80")]
public bool IsBuildable()
{
return default(bool);
}
 
  • Like
Reactions: Abhay Sinha

ASDFGHJKLQWE

Solid & Active Platinian
Jul 10, 2022
65
17
8
Nepal
//the void *instance is a self-created variable.
Void (*old_UnbanPlayer_MoveNext)(void *instance);
Void UnbanPlayer_MoveNext(void *instance) {
//Check if instance is NULL to prevent CRASH
if (instance != NULL)
{
*(int *) ((uint64_t)instance + 0x4C) = 18372819; //Return how many value
}
old_UnbanPlayer_MoveNext(instance);
}


...............................................................................
Is this work in 32 bit support or I say in arm7