3ByteBadVM/ctf-challenge-src/main.c

53 lines
1.3 KiB
C
Raw Normal View History

/* x1phosura 2021 */
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vm.h"
#include "rom.h" // automatically generated from rom.bin by `make`
char *embedded_msg = "Strings won't save you here...\n";
int main()
{
char flag_input[21];
uint8_t *memory = NULL;
struct CPU cpu;
// believe me, I _really_ tried to put the below calloc() in vm_init()
memory = calloc(RAM_SIZE, sizeof(uint8_t));
if (memory == NULL)
perror("Failed to allocate memory. Aborting...");
// src_rom_bin* comes from "rom.h", generated by make from rom.bin
vm_init(&cpu, memory, src_rom_bin, src_rom_bin_len);
printf("Fill in the rest of the flag: RS{");
fgets(flag_input, 20, stdin);
flag_input[strcspn(flag_input, "\n")] = 0; // trim trailing newline
printf("The inputted flag was RS{%s}\n\n", flag_input);
for (uint8_t i = 0; i < strlen(flag_input); ++i)
memory[128+i] = (uint8_t)flag_input[i];
#ifdef TRACE
printf("[DEBUG MODE]: Running VM in debugger...\n");
#endif
vm_run(&cpu, memory);
uint8_t win = memory[0x30]; // memory test to see if
if (win == 7) // flag was correct
printf("YAY, you got the flag!\n");
else
printf("Sorry, that's not the flag. Try again!\n");
free(memory);
return EXIT_SUCCESS;
}