Thread: What is wrong with my function?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    What is wrong with my function?

    im making a small anti-cheat, and i want to check if functions are patched.

    Code:
    bool cAntiCheat::IsFuncValid( DWORD dwAddress, char* szMemMatch, int iSize )
    {
    	BYTE* szMemOrig;
    	DWORD dwOldProtection, dwDummy;
    	VirtualProtect( ( PVOID )dwAddress, iSize, PAGE_EXECUTE_READWRITE, &dwOldProtection );
    	memcpy( ( BYTE* )szMemOrig, ( BYTE* )dwAddress, iSize );
    	VirtualProtect( ( PVOID )dwAddress, iSize, dwOldProtection, &dwDummy );
    	if( !memcmp( ( BYTE* )szMemOrig, ( BYTE* )szMemMatch , iSize ))return true;
    	return false;
    }
    you use it like this:
    Code:
    			if( !cAC.IsFuncValid( 0x7428E6, "\xFF\x4E\x0C\x8B\x46\x08", 6 ) )
    				KICKTYPE = KICK_AMMO;
    but it always crashes (also by this point i should mention im making an anti-cheat without source code access ( hacky ) )

    thanks.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    Quote Originally Posted by s0beit
    Code:
    	BYTE* szMemOrig;
    	memcpy( ( BYTE* )szMemOrig, ( BYTE* )dwAddress, iSize );
    szMemOrig is an uninitialized pointer, and then you try to copy something to the place it points to

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    It is best to initalize pointers to NULL if you do not directly assign them after declaration. This can help avoid dangling pointers
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Code:
    bool cAntiCheat::IsFuncValid( PVOID dwAddress, char* szMemMatch, int iSize )
    {
    	char* szMemOrig = 0;
    	DWORD dwOldProtection, dwDummy;
    	VirtualProtect( dwAddress, iSize, PAGE_EXECUTE_READWRITE, &dwOldProtection );
    	memcpy( ( char* )szMemOrig, ( char* )dwAddress, iSize );
    	VirtualProtect( dwAddress, iSize, dwOldProtection, &dwDummy );
    	if( !memcmp( ( char* )szMemOrig, ( char* )szMemMatch , iSize ))return true;
    	return false;
    }
    still crashes, ive done stuff like this before and its never been a problem.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    Quote Originally Posted by s0beit
    Code:
    bool cAntiCheat::IsFuncValid( PVOID dwAddress, char* szMemMatch, int iSize )
    {
    	char* szMemOrig = 0;
    	DWORD dwOldProtection, dwDummy;
    	VirtualProtect( dwAddress, iSize, PAGE_EXECUTE_READWRITE, &dwOldProtection );
    	memcpy( ( char* )szMemOrig, ( char* )dwAddress, iSize );
    	VirtualProtect( dwAddress, iSize, dwOldProtection, &dwDummy );
    	if( !memcmp( ( char* )szMemOrig, ( char* )szMemMatch , iSize ))return true;
    	return false;
    }
    still crashes, ive done stuff like this before and its never been a problem.
    you did not fix the problem: you need to allocate memory before you can copy things into it

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Code:
    bool cAntiCheat::IsFuncValid( PVOID dwAddress, char* szMemMatch, int iSize )
    {
    	char* szMemOrig = ( char* )malloc( iSize );
    	DWORD dwOldProtection, dwDummy = 0x0;
    	VirtualProtect( dwAddress, iSize, PAGE_EXECUTE_READWRITE, &dwOldProtection );
    	memcpy( szMemOrig, dwAddress, iSize );
    	VirtualProtect( dwAddress, iSize, dwOldProtection, &dwDummy );
    	//if( !memcmp( ( char* )szMemOrig, ( char* )szMemMatch , iSize ))return true;
    	return true;
    }
    i still need to do the compare code, but it doesnt crash.

    thanks for the hints.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should also free the memory after you finish with it...
    Otherwise you got a memory leak

    And because it is C++ I think using new/delete is better than malloc/free
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM