Question

I'm trying to open an exe file and place input taken from the user and replace existing data (overwriting it) of the same length at specific locations. I can do this with my code, but I'm seeing data corruption in other parts of my file. This is my first time with C++, I've tried looking at everything I could to help myself, but I'm at a loss. Only thing I can think is that its related to a null string char at the end of 'char test1[100];' (If I read the documentation right). But doesnt help my issue of resolving the issue. See linked image for example from Hex Viewer of Output and Original

    #include <stdio.h>
    #include <string.h>

    int main(void)

    {
    FILE *key;
    key=fopen ("Testfile.exe","r+b");

    char test1[100];
    char test2[100];

    printf("Test data to input:");
    fgets(test1, sizeof test1, stdin);
    printf("Second test data to input:");
    fgets(test2, sizeof test2, stdin);

    fseek (key,24523,SEEK_SET);  //file offset location to begin write
    fwrite (test1,1,sizeof(test1),key);
    fseek (key,24582,SEEK_SET); //file offset location to begin write
    fwrite (test2,1,sizeof(test2),key);
    fseek (key,24889,SEEK_SET); //file offset location to begin write
    fwrite (test2,1,sizeof(test2),key);

    fclose(key);
    printf ("Finished");
    return(0);
    }

After my initial edits, I was still fighting with a Null Terminator being written at the end of my string (and thus affecting operation of the edited exe file). After a bit more reading this is my final solution that works as intended without any weird data being written. I used scanf ("%10s") to ensure only my string was being used and to get rid of any Null Terminator. Does anyone see anything majorly wrong here or improvements to be made? Eventually I'd like to implement string length checking to ensure proper length was input by user. Thanks for everyone's help.

    #include <stdio.h>
    #include <string.h>

    int main(void)

    {
    FILE *key;
    key=fopen ("test.exe","r+b");
    char test1[10];
    char test2[32];
    printf("Input Test1 data:");
    scanf ("%10s",test1);  //only read 10 Chars
    printf("Input test2 data:");
    scanf ("%32s",test2); //only read 32 Chars
    fseek (key,24523,SEEK_SET); //file offset location to begin write
    fputs (test1,key);
    fseek (key,24582,SEEK_SET); //file offset location to begin write
    fputs (test2,key);
    fseek (key,24889,SEEK_SET); //file offset location to begin write
    fputs (test2,key);
    fclose(key);
    printf ("Finished");
    return(0);
    }
Was it helpful?

Solution

It looks like you're to write a string into the exe file but actually you're writing a string padded with garbage values up to a length of 100 bytes.

If you just want to write the string, replace fwrite with fputs.

sizeof(array) gives the allocated size of the static array (100 in this case) , not the string length. string length is done via strlen() which doesn't include the terminating NULL character.

OTHER TIPS

You have two problems.

First: you're writing 100 byte buffers which have not been initialized except via fgets()... everything not put in there by fgets() is whatever happened to be in memory (on the stack in this case).

Second: you're writing 100 bytes with each write however your seek does not advance to at least 100 bytes later, meaning the second write() in this snippet partially overwrites the first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top