Pwnable
Published
Oct 7, 2024
5
-
min read
Pwnable.kr: fd Solution
So beginning with the puzzle, the hint we have is
Mommy! what is a file descriptor in Linux?
ssh fd@pwnable.kr -p2222 (pw:guest)
File Descriptor (fd):
If you have ever had to work with C, Linux, and pipes you must know this already. But for those who don't it's basically an I/O resource identifier for a running process. Apart from being used for stdout
and stdin
streams, it is also used for reading and writing into files.
Walkthrough:
Let's start with the SSH credentials provided to us and login to the server via SSH. Upon logging in we'll be greeted by this.
Now we start searching for the flag. We type ls
into the terminal and are greeted by this
But we can't open that file directly if we wanted to because that file is owned by another user and we don't have access to open that file as shown by the ls -la
command
But if you look closely we can execute the fd file which also appears to be an executable which we can confirm by the file
command
And the other one appears to be the source code for the fd
file judging by the .c
extension. Let's read the contents of the fd.c
file.
Reading the source code we can infer the following about the fd file
It requires 1 argument to be passed because of the following check
if(argc<2) { … return 0;}
This file can open the flag file via a system call to the
cat
commandsystem("/bin/cat flag")
This file can only be opened if the
buf
equalsLETMEWIN\n
which it is reading from a file descriptorThe file descriptor being used is calculated via
atoi( argv[1] ) - 0x1234
Taking all of this into account we can plan a strategy via the user parameter input into the executable. But how do we arrange a LETMEWIN\n
into the buf
to open the flag. It is reading from a file descriptor after all but there are no other files we can manipulate. Or do we even need other files?
If you have used file descriptors for piping you must know about special file descriptors 0
and 1
. The file descriptor denoted by 0
is input stream for the process and can be used to take input from the user sometimes instead of scanf
. If we could somehow make the fd
value equal to 0
we can make the program take our own input and let us type in the key to open the flag.
Since fd
is calculated by this atoi( argv[1] ) - 0x1234
we need to somehow make this value equal 0. The atoi
function is used to convert a string to number. We also have 0x1234
which is a hexadecimal representation of the number 4660
. So if we enter 4660 as the parameter this should equate to 0 and let us do our trick.
Tadaa!!!. The program is now waiting for our input and if we enter LETMEWIN
and press enter this should read and print the outputs of the flag.
The last line is our flag and we have successfully solved this puzzle