[Bug] No checks for address in the zero page before write
Hi, so first of all I would like to thank you very much for this project and for your contribution for the open source community. I started working with .NET due to my daily job as a Software Engineer and in order to practice a little bit I started re writing some of my own personal softwares and game trainers in C# and this way I found your amazing NuGet. I do like to point out there is a bug in the NuGet (well at least it is a bug in my opinion) so while writing my hacks for the old game Overlord - rising hell I found out that when ever my character moves between maps it is loading the character data from the beginning and the health data and mana data and so many more are being re loaded probably from disk (I have no idea why they did it like that but that is the way it is.) so when I am freezing my health and mana on that game your NuGet essentially writing the same data to that memory location every 25ms but since for like 2 seconds when I move between maps some of the pointers to the mana and health address are not initialize (holds the null value) I found that you keep on writing to the memory location but you trying to get the location every time before writing with the GetCode function and you can't because of the pointers that are null (essentially 0). lets say the mana is in offset of 0x470 (last offset) you are writing to the address 0x470 which is in the Zero Page and this makes the game crash for some reason which I haven't figure out.
So I added my own code in a private branch that checks if the Pointer is in a valid address (below 0x10000 (first 64kb)) and if it is not, it won't make a write operation to the address.
just to clarify the bug:
while I am playing the game I can get to my player mana location in memory via this code: base+4BCD84,4,24,198,6c,24,78,470 where 4,24,198,6c,24,78,470 are the offsets. but when ever I move to a different map the offset 198 is taking me to a pointer which is set to null until the new map is being loaded. now I can probably fix this also by try and finding a new set of pointers and offset that never get set to null when moving between maps in the game but I also think that write to the zero page is no a good idea.
p.s - while I know the zero page is the first 4096 address, modern OS doesn't load process to addresses below the first 64kb.
Added in https://github.com/erfg12/memory.dll/commit/036274b4dcef81333fb4472edbc6014af8e01d7a Let me know if that fixes it. Thanks!
Hi Sorry for taking so long. it looks good. I haven't tried it yet (and not sure I can since you didn't bump NuGet Version) but I suggest you add a check after every time you write (I saw you added that only in 1 function in the write.cs). I guess you can check before reading as well but I am not sure if reading will create an issue since the memory in lower addresses should be readable anyway. Anyway I wrote an help extension method for this:
using System;
namespace Memory.Helper
{
public static class UIntPtrExtensionMethod
{
// modern OS doesn't load normal programs to address below 64kb.
private static ulong MinValidAddress = 0x10000;
///<summary>
///Check if a pointer is holding a valid address to write to.
///</summary>
///<param name="ptr">address to be checked</param>
public static bool IsPointerValid(this UIntPtr ptr)
{
return (ulong)ptr >= MinValidAddress;
}
}
}
example of use I did with it locally in the method WriteBytes in Write.cs:
public void WriteBytes(string code, byte[] write, string file = "")
{
UIntPtr theCode;
theCode = GetCode(code, file);
if(theCode.IsPointerValid())
WriteProcessMemory(mProc.Handle, theCode, write, (UIntPtr)write.Length, IntPtr.Zero);
}
of course you need to have using Memory.Helper; in Write.cs
if you can give me permission to create a PR I will send you a PR so you can take a look, I would love to join this project, it seems cool. (no need for permissions to approve PR of course or to push master, just to create branches and push them so I can create PR's.)