Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Error check pointers

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
iLogic
Newbie cheater
Reputation: 0

Joined: 28 Oct 2012
Posts: 13

PostPosted: Sat Feb 23, 2013 12:42 am    Post subject: [C++] Error check pointers Reply with quote

I'm currently tring to modify the PointerHelper class that I got from SteveAndrew to perform extra checks when building pointers.
I had some trouble with it reading addresses that it shouldn't be reading, which caused it to crash.

This is what I've done to it so far.

Code:
unsigned long long Pointer::BuildOffsets(unsigned long long PtrValue, unsigned long long offset)
{
   MEMORY_BASIC_INFORMATION mbi;
   VirtualQueryEx(GetModuleHandleA(0), (VOID*)(PtrValue + offset), &mbi, sizeof(mbi));

   if((mbi.State == MEM_COMMIT) && (mbi.Protect == PAGE_READWRITE) && ((mbi.Type == MEM_PRIVATE) || (mbi.Type == MEM_MAPPED)))
   {
      if ( !IsBadReadPtr((VOID*)(PtrValue + offset), sizeof(ULONG_PTR)) )
      {
         PtrValue = *(unsigned long long*)(PtrValue + offset);
         return PtrValue;
      }
      else
      {
         return 0;
      }
   }
   return 0;
}

unsigned long long Pointer::GetDynamicAddress()
{
   unsigned long long PtrValue = (unsigned long long)BasePtr;
   
   if(OffsetsList)
   {
      //use all offsets in sequential order to end up with the final pointer
      PointerOffset *CurrentOffset = OffsetsList;
        
      while(CurrentOffset->NextOffset)
      {
         if(!PtrValue) //Don't try to access the pointer if its null
       {
            return 0;
       }

       PtrValue = BuildOffsets(PtrValue, CurrentOffset->Offset);
       CurrentOffset = CurrentOffset->NextOffset;
      }

     if(!PtrValue)
     {
         return 0;
     }

     PtrValue += CurrentOffset->Offset;   

     if(!PtrValue)
     {
         return 0;
     }
      CurrentDynamicAddress = (void*)PtrValue;     
      return PtrValue;
   }

   return 0;
}


Problem is that now it's no longer able to read the pointer that I wanted it to read.

The issue is this, from what I can see:

Code:
VirtualQueryEx(GetModuleHandleA(0), (VOID*)(PtrValue + offset), &mbi, sizeof(mbi));

if((mbi.State == MEM_COMMIT) && (mbi.Protect == PAGE_READWRITE) && ((mbi.Type == MEM_PRIVATE) || (mbi.Type == MEM_MAPPED)))

Any idea what I've done wrong here?


Edit: After some more error checking, it would seem that none of the mbi values are "true".. So, I'm either executing the "VirtualQueryEx" function wrong or the cheat protection have it hooked.
If it is hooked, any suggestions on what I can do to confirm that the address I'm trying to read is indeed readable to avoid crashes?


Edit2: Ok.. I figured out what was wrong...
After some thinking and time spent on msdn I realized that I shouldn't be using VirtualQueryEx. Since I'm doing this within the process I should of used VirtualQuery.


Edit 3:

I got a new issue with this now though. It would seem that VirtualQuery gets stuck in an infinite loop if it reads an invalid memory space.

I've changed it so it looks like this now:
Code:
unsigned long long Pointer::BuildOffsets(unsigned long long PtrValue, unsigned long long offset)
{
   MEMORY_BASIC_INFORMATION mbi;
   if(VirtualQuery((VOID*)(PtrValue + offset), &mbi, sizeof(MEMORY_BASIC_INFORMATION)) != 0)
   {   
      if((mbi.State & MEM_COMMIT) && (mbi.Protect & WRITABLE))
      {
         if ( !IsBadReadPtr((VOID*)(PtrValue + offset), sizeof(ULONG_PTR)) )
         {
            PtrValue = *(unsigned long long*)(PtrValue + offset);
            return PtrValue;
         }
         else
         {
            return 0;
         }
      }
   }
   return 0;
}


Where I've defined "WRITABLE" as:
Code:
#define WRITABLE (PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 471

Joined: 09 May 2003
Posts: 25827
Location: The netherlands

PostPosted: Sat Feb 23, 2013 4:45 am    Post subject: Reply with quote

If your memory is unwritable or unreadable nothing will happen to ptrvalue
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
iLogic
Newbie cheater
Reputation: 0

Joined: 28 Oct 2012
Posts: 13

PostPosted: Sat Feb 23, 2013 4:52 am    Post subject: Reply with quote

Yeah, but then it should leave the function with a "return 0".
The issue I got now is that it doesn't seem to leave the VirtualQuery function, it starts looping it over and over :/


I'm using this to go through a lot of different pointers, the issue I had before was that some times these pointers would break during certain circumstances, but the code tried to read them anyway causing a crash.

That's why I decided to use VirtualQuery since then I could check the area before I try to add the offsets.

Edit: I've tested it out some more.. turns out it wasn't going in an infinite loop.. It were just going insanely slow...

Any suggestions on how to make it check tons of addresses faster?
By tons of addresses, I mean like reading a span of:
0x2500000 -> 0x3500000
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 471

Joined: 09 May 2003
Posts: 25827
Location: The netherlands

PostPosted: Sat Feb 23, 2013 6:25 am    Post subject: Reply with quote

don't call virtualquery on every address
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites