(C++) Inserting Garbage Bytes by Unprotect

Created the Tuesday 06 December 2022. Updated 1 month, 3 weeks ago.

Description:

In this example, the code first allocates an array of 10 random bytes and inserts them into the code. Then, it uses the garbage bytes to confuse a disassembler by adding them to the code in a way that does not affect the program's behavior. When the disassembler tries to analyze the code, it will be unable to correctly reconstruct the original program due to the presence of the garbage bytes. This can make it more difficult to understand and analyze the code's behavior and intentions.

Code

            #include <stdio.h>
#include <stdlib.h>

int main()
{
    // Insert garbage bytes into the code
    char* garbage = malloc(10);
    for (int i = 0; i < 10; i++)
    {
        garbage[i] = rand() % 256;
    }

    // Use the garbage bytes to confuse a disassembler
    int a = 5;
    int b = 10;
    int c = a + b;

    printf("The result is: %d\n", c);

    return 0;
}