Reputation: 658
I've this little sample C program:
#include <stdio.h>
#define CIAO 0x15151515;
#define PROVA 0x14141414;
int main(){
unsigned char *ptr = (unsigned char *)CIAO;
long int nbytes = PROVA;
long int i;
int cpt = 0;
char key[] = { 'a','b','a','b','a','b','a','b','\0' };
int keyLength = 8;
for (i = 0; i < nbytes; i++){
ptr[i] = ptr[i] ^ key[cpt];
cpt = cpt + 1;
if (cpt == keyLength)
cpt = 0;
}
return 0;
}
My goal is to compile it and then using a HexEditor, find the specific values 0x15151515
and 0x14141414
and replace them with another value.
I've tried with other smaller sample program i managed to do this really easily. Using this instead i'm having some problem. 0x15151515
got written as 0x16151515
and i searched for 0x14141414
but I didn't manage to find anything similar.
Here is the relevant hex view of the exe: https://pastebin.com/dLi4tfU5
Do you have any clue on why sometimes the value written seems to change?
Is there any way to avoid this and be sure that whenever i write "CIAO" then in the exe viewed as hex I will always found 0x15151515
?
p.s. I know it's not something that you should normally do and of course i know that editing the exe could lead to unexpected behaviour of the program. I'm just doing it for fun
Upvotes: 1
Views: 89
Reputation: 21542
An optimizing compiler may make changes to constants in your code.
You can use the volatile
keyword as a hint to the compiler that the value of a variable may be modified or accessed by something external to the program itself, and thus to avoid optimizing it too heavily:
#include <stdint.h>
volatile const uint32_t CIAO = 0x15151515;
volatile const uint32_t PROVA = 0x14141414;
Try replacing your #define
statements with this code and then open the binary and look for these values.
Upvotes: 1