Thread: How can function like this be obtained?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    How can function like this be obtained?

    Hello,
    I have been provided a source to work with. And what it does is it patches a raw binary file.
    I looked at the source and there is this function which can patch a float, byte or dword to an address. But the source for it is missing. My question is how can I make this function again?

    I know it uses some operator overloading function and it can easily patch the variable and the exact size we would want. But I searched the net for every references but all has been in vain. So can you please help me out to recreate this function?

    Examples:
    InstallPatch<BYTE>(0x234, 0xFF);
    InstallPatch<DWORD>(0x254, 12345);
    InstallPatch<FLOAT>(0x839, 12.0f);

    My interpretation of it prototype:
    InstallPatch<DATA_TYPE>(ADDRESS, VALUE_ACCORDING_TO_DATA_TYPE)

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Please define the word "patch".
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Patch means to write data to an address.

    Example of patching a byte, DWORD:
    Code:
    PatchByte(DWORD dwAddress, BYTE bData) // Overwrites the file content with 1 byte
    {
    	*(BYTE*)dwAddress = bData;
    }
    
    PatchDword(DWORD dwAddress, DWORD dwData) // Overwrites the file content with 4 bytes
    {
    
            *(BYTE*)dwAddress = dwData;
    }
    Last edited by Swoorup; 02-18-2012 at 12:39 AM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by Swoorup View Post
    Patch means to write data to an address.
    The way you're referring to it, I'm not sure what kind of address you're talking about.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Code:
    PatchByte(DWORD dwAddress, BYTE bData) // Overwrites the file content with 1 byte { *(BYTE*)dwAddress = bData; } PatchDword(DWORD dwAddress, DWORD dwData) // Overwrites the file content with 4 bytes { *(BYTE*)dwAddress = dwData; }

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Is the file loaded on memory? because the comments make no sence otherwise...
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Yeah, its a memory address. I figured that The patching is done when it is at memory through dll.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This sounds very dubious. Please tell us more about what this is for, or forget about getting help.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Ok, I'll post some parts of the source code. Some of my own.

    Here's the thing that patches a byte at given memory address.
    Code:
    void PatchByte(DWORD dwAddress, BYTE patch)
    {
        DWORD dwProt;
            VirtualProtect((void*)dwAddress, sizeof(patch), PAGE_EXECUTE_READWRITE, &dwProt);
            *(BYTE*)dwAddress=patch;
            VirtualProtect((void*)dwAddress,  sizeof(patch), dwProt, &dwProt);
    }
    And another code to patch the floating point or DWORD at given address. (ALL 4 byte data types)
    Code:
    void PatchDWORD(DWORD dwAddress, DWORD patch)
    {
        DWORD dwProt;
            VirtualProtect((void*)dwAddress, sizeof(patch), PAGE_EXECUTE_READWRITE, &dwProt);
            *(BYTE*)dwAddress=patch;
            VirtualProtect((void*)dwAddress,  sizeof(patch), dwProt, &dwProt);
    }
    For each data types, depending on the size of them, I have have to call PatchDWORD for patching DWORDs, FLOATS, ints which is equal to 4 bytes.
    To write only a single byte/ char I would have to call PatchBYTE.

    What I would like to do is:
    By typing Patch<DWORD>(MemoryAddress, (DWORD)data) I would like to get DWORD (4 bytes) inserted into the specfic memory address.
    And by type Patch<BYTE>(MemoryAddress, (BYTE)data) I would like to get a single byte or char inserted into the specific memory address.

    Please don't be harsh on me. I cannot provide the whole source though. But here are some parts of it.
    Code:
        for(int i = 0; i < 1; i++) CMemory::InstallPatch<DWORD>( ARR_CCoronaStore_63[i], ADDR_CCorona_Store + 0x63);
        for(int i = 0; i < 1; i++) CMemory::InstallPatch<DWORD>( ARR_CCoronaStore_64[i], ADDR_CCorona_Store + 0x64);
        for(int i = 0; i < 1; i++) CMemory::InstallPatch<DWORD>( ARR_CCoronaStore_65[i], ADDR_CCorona_Store + 0x65);
    
    
        CMemory::InstallPatch<BYTE>(0x544591, MAX_CORONAS);
        CMemory::InstallPatch<BYTE>(0x5425BA, MAX_CORONAS);

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    The syntax you are looking for is called a function template.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Yup, exactly thank you.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Sorry for the bump, but I got a question. Does the whole templete needs to be in header file?
    I want to use it sources that includes it but I am getting a lot of compilation errors

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    The simple answer is yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Thank you

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    This smells suspiciously like an attempt at hacking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM