Created
November 9, 2024 15:02
-
-
Save komodoooo/9df08a7fd32f13c5e08773512ed14910 to your computer and use it in GitHub Desktop.
Edit strings in binary files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char *argv[]){ | |
FILE * f = fopen(argv[1], "rb+"); | |
fseek(f, 0, SEEK_END); | |
unsigned long long int size = ftell(f); | |
fseek(f, 0, SEEK_SET); | |
unsigned char *data = (unsigned char *)malloc(size); | |
fread(data, 1, size, f); | |
size_t old_len = strlen(argv[2]), new_len = strlen(argv[3]); | |
for (unsigned long long int i=0; i<size-old_len; i++) { | |
if(!memcmp(&data[i], argv[2], old_len)) { | |
memmove(&data[i+new_len], &data[i+old_len], size-(i+old_len)); | |
memcpy(&data[i], argv[3], new_len); | |
break; | |
} | |
} | |
fseek(f, 0, SEEK_SET); | |
fwrite(data, 1, size, f); | |
fclose(f); | |
free(data); | |
return 0; | |
} | |
/* | |
USAGE: stredit file.bin "old string" "new string" | |
COMPILE & SETUP: gcc stredit.c -o stredit && sudo cp stredit /usr/local/bin/stredit | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment