[C/C++] Memory Patcher

Posts 1–5 of 5 · Page 1 of 1
[C/C++] Memory Patcher
Well here is a simple memory patcher, takes a block of memory and searches for a string pattern that you pass and applies a patch to it, super simple.

Code:
//Give a memory string 'search' of size 'len' to find in a memory block
//of'look_size' starting at addr 'begin' returns the beggining of the memory
//of the found memory or null on fail
void *ELFindMemory( void *begin, unsigned int look_size,
                     unsigned char *search, unsigned int len )
{
     if begin == NULL || look_size == 0 || search == NULL || len == 0 )
         return NULL;
        
     //begin search
     bool isFound = false;
     for( unsigned int i = 0; i < look_size; i++ )
     {
          if( *(unsigned char *)(begin + i ) == search[0] )
          {
               for( unsigned int j = 1; j < len; j++ )
              {
                    if( *(unsigned char *)(begin + i + j ) == search[j] )
                        isFound = true;
                    else
                        isFound = false;
              }
           }
           if( isFound == true )
              return (void *)( begin + i );
     }
    
     return NULL;
}

//Takes the found addr 'memory' and writes the 'patch' of length 'patch_len' with a filler if the
//patch is less then the original memory contents 'filler'
int ELPatchMemory( void *memory, int orig_size, unsigned char *patch, int patch_len, unsigned char filler )
{
    if( puid == 0 || memory == NULL || orig_size == 0 || patch == NULL || patch_len == 0 )
        return -1;
        
    if( orig_size < patch_size )
        return -10;
        
    for( unsigned int i = 0; i < patch_len )
    {
         *(unsigned char *)( memory + i ) = *(unsigned char *)(patch + i );
    }
    
    if( orig_size > patch_len )
    {
        orig_size -= patch_len;
        
         for( unsigned int i = 0; i < orig_size; i++ )
        {
              *(unsigned char *)( memory + patch_len + i + 1 ) = filler;
        }
    }
     
    return 0;
}
Hope this helps people learning C++ in any way!
Very nice!

The patch memory function reminds me exactly of WriteProcessMemory(), and good error checking by the way. There were a few syntax errors, in other words, you can't just stick this into a compiler w/o getting a few errors.

Code:
for( unsigned int i = 0; i < patch_len )
    {
         *(unsigned char *)( memory + i ) = *(unsigned char *)(patch + i );
    }
This part is missing an incrementor. Unless ofcourse the incrementor is implied, I dunno all the shortcuts for for loops.

But there was something important... oh yeh!
Your memory searcher only checks the first and last character, and the length. Which might work, but what if you run into something like "dop" when u were lookin for "dip"?

Oh and filler needs to be assigned a value. perhaps NOP 0x90?

Someone correct me about anything if Im mistaken, but I assume *(unsigned char*) meant the value of a char*....
the incrementer is implied for this source code. thanks for the comments
Quote Originally Posted by Justin
for( unsigned int i = 0; i < patch_len )
{
*(unsigned char *)( memory + i ) = *(unsigned char *)(patch + i );
}
I is never incremented, so thats pretty much an infinite loop
Quote Originally Posted by Hell_Demon View Post
I is never incremented, so thats pretty much an infinite loop
O the incrementation isn't implied o_O?
Is or isn't wdf? Im confused, just right out the whole for statement for clarity's sake >_>...
Posts 1–5 of 5 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us