alexguirre / NativeWatcher

MIT License
8 stars 3 forks source link

Exception on v1868 (1.50). Possible Registration Table pattern change? #1

Open all-in-simplicity opened 4 years ago

all-in-simplicity commented 4 years ago

In its current state the plugin won't work with version v1868. So I updated the NativesXmlGen project to correspond the native changes, but now I get an Exception when I try to load the plugin.

Exception

NativeWatcher:

NativeWatcher: ============================== NativeWatcher: UNHANDLED EXCEPTION DURING GAME FIBER TICK NativeWatcher: ------------------------------ NativeWatcher: Origin: Game fiber "Plugin "NativeWatcher" main fiber". NativeWatcher: ------------------------------ NativeWatcher: Exception type: System.TypeInitializationException NativeWatcher: Exception message: The type initializer for 'NativeWatcher.NativeTranslator' threw an exception. NativeWatcher: ------------------------------ NativeWatcher: Inner exceptions: NativeWatcher: Exception type: System.NullReferenceException NativeWatcher: Exception message: Object reference not set to an instance of an object. NativeWatcher: ------------------------------ NativeWatcher: Stack trace: NativeWatcher: at NativeWatcher.NativeRegistration.GetRegistrationTable() in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\NativeRegistration.cs:line 23 at NativeWatcher.NativeTranslator..cctor() in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\NativeTranslator.cs:line 55 NativeWatcher: ============================== NativeWatcher: NativeWatcher: ------------------------------ NativeWatcher: Stack trace: NativeWatcher: at NativeWatcher.NativeTranslator.AddressToOriginal(UInt64 address) at NativeWatcher.ScriptNative..ctor(UInt64 address) in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\ScriptNativeCalls.cs:line 31 at NativeWatcher.ScriptNativeCalls..ctor(scrProgram* program) in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\ScriptNativeCalls.cs:line 16 at NativeWatcher.ScriptNativeCallsFetcher.FetchStack() in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\ScriptNativeCallsFetcher.cs:line 101 at NativeWatcher.ScriptNativeCallsFetcher.Tick() in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\ScriptNativeCallsFetcher.cs:line 82 at NativeWatcher.Plugin.Main() in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\Plugin.cs:line 24 at Rage.GameFiber.Main() NativeWatcher: ============================== NativeWatcher:

The line in NativeRegistration.cs which seems to throw the Exception is IntPtr address = Game.FindPattern("48 8D 0D ?? ?? ?? ?? 4E 8B 1C C7 41 0F B6 C3 48 8B 0C C1"); which leads me to the question if the pattern to find the registration table changed. Any help to make it work with newer versions of the game would be much appreciated.

alexguirre commented 4 years ago

Yeah, R* obfuscated the NativeRegistration structure a while ago. See https://github.com/ivanmeler/OpenVHook/blob/b5b4d84e76feb05a988e9d69b6b5c164458341cb/OpenVHook/Scripting/ScriptEngine.cpp#L22 for the new structure. Also, the registrationTable pattern used there still works.

all-in-simplicity commented 4 years ago

Thanks for your quick reply. I updated the struct and the method to get the registration table, but the Exception of type System.AccessViolationException indicates that I did something wrong.

NativeRegistration.cs

[StructLayout(LayoutKind.Explicit)]
public unsafe struct NativeRegistration
{
    [FieldOffset(0x0000)] public NativeRegistration* Next;

    [FieldOffset(0x0008)] public NativeRegistration* Next2;

    [FieldOffset(0x0016)] public fixed ulong HandlersPointers[7];

    [FieldOffset(0x0048)] public uint EntriesCount;

    [FieldOffset(0x0056)] public uint EntriesCount2;

    [FieldOffset(0x0064)] public fixed ulong Hashes[7];

    private static NativeRegistration** registrationTable;
    public static NativeRegistration** GetRegistrationTable()
    {
        if (registrationTable == null)
        {
            IntPtr address = Game.FindPattern("76 32 48 8B 53 40");
            address += *(int*)(address + 4);
            registrationTable = (NativeRegistration**)address;
        }

        return registrationTable;
    }
}

Exception

Plugin "NativeWatcher" was loaded from "NativeWatcher.dll". NativeWatcher: NativeWatcher: ============================== NativeWatcher: UNHANDLED EXCEPTION DURING GAME FIBER TICK NativeWatcher: ------------------------------ NativeWatcher: Origin: Game fiber "Plugin "NativeWatcher" main fiber". NativeWatcher: ------------------------------ NativeWatcher: Exception type: System.TypeInitializationException NativeWatcher: Exception message: The type initializer for 'NativeWatcher.NativeTranslator' threw an exception. NativeWatcher: ------------------------------ NativeWatcher: Inner exceptions: NativeWatcher: Exception type: System.AccessViolationException NativeWatcher: Exception message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. NativeWatcher: ------------------------------ NativeWatcher: Stack trace: NativeWatcher: at NativeWatcher.NativeTranslator..cctor() in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\NativeTranslator.cs:line 58 NativeWatcher: ============================== NativeWatcher: NativeWatcher: ------------------------------ NativeWatcher: Stack trace: NativeWatcher: at NativeWatcher.NativeTranslator.AddressToOriginal(UInt64 address) at NativeWatcher.ScriptNative..ctor(UInt64 address) in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\ScriptNativeCalls.cs:line 31 at NativeWatcher.ScriptNativeCalls..ctor(scrProgram* program) in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\ScriptNativeCalls.cs:line 16 at NativeWatcher.ScriptNativeCallsFetcher.FetchStack() in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\ScriptNativeCallsFetcher.cs:line 101 at NativeWatcher.ScriptNativeCallsFetcher.Tick() in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\ScriptNativeCallsFetcher.cs:line 80 at NativeWatcher.Plugin.Main() in E:\Development\Projects\GTA5\NativeWatcher\NativeWatcher\Plugin.cs:line 26 at Rage.GameFiber.Main()

I assume the fields of the struct are correct, but address += *(int*)(address + 4); is what I'm uncertain about. Did I miss something?

alexguirre commented 4 years ago

The address should be

address += 9;
address += *(int*)address + 4;

The struct isn't correct either, Next and Next2 aren't pointers and the offsets are wrong. It should be something like this:

[StructLayout(LayoutKind.Explicit)]
public unsafe struct NativeRegistration
{
    [FieldOffset(0x00)] public ulong nextRegistration1;
    [FieldOffset(0x08)] public ulong nextRegistration2;
    [FieldOffset(0x10)] public fixed ulong HandlersPointers[7];
    [FieldOffset(0x48)] public uint numEntries1;
    [FieldOffset(0x4C)] public uint numEntries2;
}

Then you would need to reimplement the getNextRegistration, getNumEntries and getHash functions from the link to replace the old Next, EntriesCount and Hashes fields.