Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime patching in C++/inline assembly

I am trying to patch the function cat() to return true, but for some reason the program crashes when I don't even call the function. Is the problem my method of patching? I think I am writing to the correct address (address of the function is 004012e4). I am using code blocks (gcc) on a Windows XP 32-bit system.

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int cat()
{
    cout<<"false"<<endl;
    return false;
}

int main()
{
    DWORD beef;
    int (*css)();
    css=cat;
    cout<<css<<endl;
    system("PAUSE");
    VirtualProtect(&css,49,PAGE_EXECUTE_READWRITE,&beef);
    asm("mov $0x40130f,%eax");//move address of the mov $0x0,eax instruction to eax
    asm("movl $0x1,(%eax)");//write at address changing B800 to B801 or return true

    return 0;
}
like image 606
Jordan Avatar asked Sep 18 '26 09:09

Jordan


1 Answers

Why do you hardcode the function address? You have it in code, you are printing it out. If you print it and then change the code to include what you printed, you risk moving the function. Why not just generate the assembly statement dynamically?

like image 157
AShelly Avatar answered Sep 20 '26 22:09

AShelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!