Browse Source

Make simple number guesser game and the ability to hack it using LD_PRELOAD

master
stew3254 1 year ago
parent
commit
5f7944f27b
  1. 3
      .gitignore
  2. 1
      README.md
  3. 2
      evil_random.c
  4. 19
      makefile
  5. 40
      number_guesser.c

3
.gitignore

@ -0,0 +1,3 @@
*.o
*.so
guess

1
README.md

@ -1,2 +1,3 @@
# cs241_final
Simple number guesser game and supporting files

2
evil_random.c

@ -0,0 +1,2 @@
// The answer to everything
int rand() {return 42;}

19
makefile

@ -0,0 +1,19 @@
CC=gcc
CFLAGS=-O2
OBJ = number_guesser.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
guess: $(OBJ)
$(CC) -o $@ $^ $(CFLAGS)
evil: guess
$(CC) -shared -fPIC evil_random.c -o evil_random.so
LD_PRELOAD="$(PWD)/evil_random.so" ./guess
run: guess
./guess
clean:
rm -f *.o *.so guess

40
number_guesser.c

@ -0,0 +1,40 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main() {
srand(time(NULL));
int rand_num = (rand()%100);
int tries = 5;
int attempts = 0;
int guess_num;
char guess[100]; // Can overflow this, but not the point for this talk
bool playing = true;
printf("Number guesser game\n");
printf("You have %d tries\n", tries);
printf("Please guess a number between 0 and 99\n> ");
do {
//Bad scanf
if (scanf("%s", guess) == 0) {
printf("Bad input\n> ");
continue;
}
guess_num = atoi(guess);
if (guess_num > rand_num)
printf("Too high\n> ");
else if (guess_num < rand_num)
printf("Too low\n> ");
else {
printf("Good job! You won using only %d attempts!\n", attempts+1);
playing = false;
continue;
}
if (++attempts == tries) {
printf("Sorry, you lost. The number was %d\n", rand_num);
playing = false;
}
} while(playing);
return 0;
}
Loading…
Cancel
Save