Advertisement
Guest User

Guessing game with buffer overflow

a guest
Jun 2nd, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | Cybersecurity | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. int counter = 0;
  7. char username[16];
  8.  
  9. void win() {
  10.     printf("You win this round %s\n", username);
  11.     counter++;
  12. }
  13.  
  14. void loose() {
  15.     printf("You lose, better luck next time %s!\n\n", username);
  16.     counter = 0;
  17. }
  18.  
  19.  
  20. int calculate(char *text, int input1, int input2, int input3, int number1, int number2, int number3) {
  21.    
  22.     char name[16];
  23.     strcpy(name, text);
  24.  
  25.     if (number1 == input1 && number2 == input2 && number3 == input3)
  26.         return 0;
  27.     else
  28.         return 1;
  29.  
  30. }
  31.  
  32. int main(int argc, char ** argv) {
  33.  
  34.     int number1, number2, number3;
  35.     int input1 = 0, input2 = 0, input3 = 0;
  36.  
  37.     if(argc < 2){
  38.         printf("Please pass at least one argument with your program.\nOtherwise you won't be able to exploit it ;)\n");
  39.         exit(0);
  40.     }
  41.    
  42.  
  43.     printf("Please enter your name!\n");
  44.     fgets(username, sizeof(username), stdin);
  45.     username[strcspn(username, "\n")] = '\0';
  46.  
  47.    
  48.     while(counter < 5){
  49.         printf("Can you beat this minigame?\n\nEnter three numbers between 0-10 if you guess all correct you win, otherwise you lose!\n");
  50.        
  51.         printf("Enter your first guess!\n");
  52.         scanf("%d", &input1);
  53.         printf("Enter your second guess!\n");
  54.         scanf("%d", &input2);
  55.         printf("Enter your third guess!\n");
  56.         scanf("%d", &input3);
  57.  
  58.         srand(time(NULL));
  59.  
  60.         number1 = rand() % 10;
  61.         number2 = rand() % 10;
  62.         number3 = rand() % 10;
  63.  
  64.  
  65.         if(calculate(argv[1], input1, input2, input3, number1, number2, number3) == 0)
  66.             win();
  67.         else
  68.             loose();
  69.    
  70.     }
  71.  
  72.     printf("Against all odds you beat the game!\nCongratulations %s", username);
  73.    
  74.     exit(0);
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement