From bfbb5bd5156b6d992ddd95b768f2205b14445ccb Mon Sep 17 00:00:00 2001 From: Maxime Vorwerk Date: Mon, 24 Jun 2024 15:29:00 +0200 Subject: [PATCH] Endianness --- endianness/flag.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++ endianness/sol | Bin 0 -> 16176 bytes endianness/sol.c | 43 +++++++++++++++ 3 files changed, 177 insertions(+) create mode 100755 endianness/flag.c create mode 100755 endianness/sol create mode 100755 endianness/sol.c diff --git a/endianness/flag.c b/endianness/flag.c new file mode 100755 index 0000000..e91c383 --- /dev/null +++ b/endianness/flag.c @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include + +char *find_little_endian(const char *word) +{ + size_t word_len = strlen(word); + char *little_endian = (char *)malloc((2 * word_len + 1) * sizeof(char)); + + for (size_t i = word_len; i-- > 0;) + { + snprintf(&little_endian[(word_len - 1 - i) * 2], 3, "%02X", (unsigned char)word[i]); + } + + little_endian[2 * word_len] = '\0'; + return little_endian; +} + +char *find_big_endian(const char *word) +{ + size_t length = strlen(word); + char *big_endian = (char *)malloc((2 * length + 1) * sizeof(char)); + + for (size_t i = 0; i < length; i++) + { + snprintf(&big_endian[i * 2], 3, "%02X", (unsigned char)word[i]); + } + + big_endian[2 * length] = '\0'; + return big_endian; +} + +char *generate_random_word() +{ + printf("Welcome to the Endian CTF!\n"); + printf("You need to find both the little endian and big endian representations of a word.\n"); + printf("If you get both correct, you will receive the flag.\n"); + srand(time(NULL)); + + int word_length = 5; + char *word = (char *)malloc((word_length + 1) * sizeof(char)); + + for (int i = 0; i < word_length; i++) + { + word[i] = (rand() % 26) + 'a'; + } + + word[word_length] = '\0'; + return word; +} + +int main() +{ + char *challenge_word = generate_random_word(); + printf("Word: %s\n", challenge_word); + fflush(stdout); + + char *little_endian = find_little_endian(challenge_word); + size_t user_little_endian_size = strlen(little_endian); + char user_little_endian[user_little_endian_size + 1]; + bool correct_flag = false; + + while (!correct_flag) + { + printf("Enter the Little Endian representation: "); + fflush(stdout); + scanf("%10s", user_little_endian); + for (size_t i = 0; i < strlen(user_little_endian); i++) + { + user_little_endian[i] = toupper(user_little_endian[i]); + } + + if (strncmp(user_little_endian, little_endian, user_little_endian_size) == 0) + { + printf("Correct Little Endian representation!\n"); + fflush(stdout); + correct_flag = true; + } + else + { + printf("Incorrect Little Endian representation. Try again!\n"); + fflush(stdout); + } + } + + char *big_endian = find_big_endian(challenge_word); + size_t user_big_endian_size = strlen(big_endian); + char user_big_endian[user_big_endian_size + 1]; + + bool final_flag = false; + while (!final_flag) + { + printf("Enter the Big Endian representation: "); + fflush(stdout); + scanf("%10s", user_big_endian); + for (size_t i = 0; i < strlen(user_big_endian); i++) + { + user_big_endian[i] = toupper(user_big_endian[i]); + } + + if (strncmp(user_big_endian, big_endian, user_big_endian_size) == 0) + { + printf("Correct Big Endian representation!\n"); + fflush(stdout); + final_flag = true; + } + else + { + printf("Incorrect Big Endian representation. Try again!\n"); + fflush(stdout); + } + } + + FILE *flag = fopen("flag.txt", "r"); + if (flag == NULL) + { + printf("Flag not found. Please run this on the server\n"); + fflush(stdout); + exit(0); + } + + char flag_content[100]; + fgets(flag_content, sizeof(flag_content), flag); + printf("Congratulations! You found both endian representations correctly!\n"); + fflush(stdout); + printf("Your Flag is: %s\n", flag_content); + fflush(stdout); + exit(0); + + return 0; +} \ No newline at end of file diff --git a/endianness/sol b/endianness/sol new file mode 100755 index 0000000000000000000000000000000000000000..ea28db51fda9171b83da84f190a2097cba2073ae GIT binary patch literal 16176 zcmeHOYitzP6~4Q?oW@{7ATbcaHif7Zd9jTl1md!OEQ~1+2aHIiro-C1wzpdEy1TQD zNlHTE64eDu>7O=gQi)25AWBhz$exa|IAmYt&5q zE>`!b>5!*toox@<0IOXt`b)8b^;3YvtD#C6yxUeQLFpkO@g^(hsY57&iqC_1)5yTG z8)MuLL9rLx3lJG4pnr?_AUU82I(gKUL$%th4mFkH0Ow_pQFtU0?+KTATPG zzjB$kb9<9rhuxfiK~Aq)k%)J#Zd{Rw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o$GdKTx&{0k#e(~=P|NW8o6Aq*MDs{pa za5(t^<_UHj$}Al6z9sLqvcJ>qFS6iuaH|}AnfD*5g%U;lY-vIk{d+Lb*p>EWJ}&!p zWnV7ce^i7g*Z&ph==_B*@4bgskhfC1_3y-w@CB!X?BuHu`_|bg?MMXSiA>*UNhj^g z{IQz(ZhvaaS!Q$l>|Qhpe|b@3SA_3G89Pqb5C@d!@FEy#$}9bm=P!*<>ARC((`ZZg XyMjx*;yZg1{x +#include +#include +#include +#include +#include + +char *find_little_endian(const char *word) +{ + size_t word_len = strlen(word); + char *little_endian = (char *)malloc((2 * word_len + 1) * sizeof(char)); + + for (size_t i = word_len; i-- > 0;) + { + snprintf(&little_endian[(word_len - 1 - i) * 2], 3, "%02X", (unsigned char)word[i]); + } + + little_endian[2 * word_len] = '\0'; + return little_endian; +} + +char *find_big_endian(const char *word) +{ + size_t length = strlen(word); + char *big_endian = (char *)malloc((2 * length + 1) * sizeof(char)); + + for (size_t i = 0; i < length; i++) + { + snprintf(&big_endian[i * 2], 3, "%02X", (unsigned char)word[i]); + } + + big_endian[2 * length] = '\0'; + return big_endian; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + return 1; + } + + puts(find_little_endian(argv[1]));; + puts(find_big_endian(argv[1])); +}