fd

{"author": ["ret2basic"]}

Challenge

Mommy! what is a file descriptor in Linux?

  • try to play the wargame your self but if you are ABSOLUTE beginner, follow this tutorial link: https://youtu.be/971eZhMHQQw

ssh fd@pwnable.kr -p2222 (pw:guest)

Code Review

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buf[32];
int main(int argc, char* argv[], char* envp[]){
        if(argc<2){
                printf("pass argv[1] a number\n");
                return 0;
        }
        int fd = atoi( argv[1] ) - 0x1234;
        int len = 0;
        len = read(fd, buf, 32);
        if(!strcmp("LETMEWIN\n", buf)){
                printf("good job :)\n");
                system("/bin/cat flag");
                exit(0);
        }
        printf("learn about Linux file IO\n");
        return 0;

}

Take a look at this portion of the code:

From man7.org:

read() syscall

Solution

In Linux, the default file descriptors are:

  • stdin (standard input) => 0

  • stdout (standard output) => 1

  • stderr (standard error) => 2

We are able to control the value of fd. The desirable value is fd = 0 since it will open a stdin session. We can send information through stdin and that information will be stored in buf. The idea is clear:

  1. Set argv[1] = 0x1234 so that fd = 0.

  2. Send "LETMEWIN" through stdin.

Exploit

Last updated