We can see that we are provided with a ssh login and some hints for the challenge.
File descriptors is a number that uniquely identifies an open file in a computer Operating System.
In C, file descriptors are represented as integers as follows:
stdin
is represented with a 0
stdout
is represented as 1
stderr
is represented as 2
stdin
. When it provides an output, then that is stdout
. When the program gives back an argument then that is stderr
Lets now look at our challenge by "sshing" into the machine.
we can see that we are provided with 3 files to work with. The binary fd
, the source code fd.c
and a flag file flag
.
Trying to read the flag with cat flag
, we are prompted with Permission denied
. On the other hand, runing the fd binary, it asks for a number as an argument. Passing 1234
as our argument, we are told learn about Linux file IO
. The only option remaining now is to try and analyse the source code, and find what is expected of us.
First, we can see char buf[32]
. What this does is, it assigns a buffer of 32 chars to the variable buf
Now, this tries to check if we have passed an argument. If no argument is passed, then we get pass argv[1] a number
We have a variable declared fd
, which its value is atoi( argv[1] ) - 0x1234
. It takes our argument then subtracts it with the integer value of 0x1234
which is equal to 4660
when converted.
The atoi()
function stands for Ascii to Integer in C.
It then declares another integer len
assigned a 0
Then it takes the value of fd
and puts it into buf
We see it then takes fd
and reads our input, and stores it in buf
. The if statement, checks if the value of buf
is equal to LETMEWIN
. If yes, it prints good job :)
and gives us the flag. Otherwise, it outputs learn about Linux file IO
.
We can start by running the program with an argument and see its behaviour.
we see that we get learn about Linux file IO
. This is basically because we didn't get fd
. In what we understood about file descriptors, is that we have three basic ones ranging from 0, 1, 2
.
The rule of fd, is for our argument to subtract 4660. So if we supply the same as our argument, fd will be 0
which is the fd representation of standard input in C.
We see after passing 4660 as the argument, we don't get learn about Linux file IO
. So we have bypassed the line len = read(fd, buf, 32);
By the state of the program, it looks like it is waiting to be supplied with an input. We could try now and input the string LETMEWIN
to make it execute the if statement.
aaand, we successfully pwned it.
we just pwned it again.