pee2pee January 2017

C: IF statement not firing when expected

I have the following function. The executable runs fine. At the prompt, after the program is run, I enter \x0037337331, the value of B is set as B: 0x31333337

Any advice on how I'd trigger to open log.txt

int play() {

    int a;
    int b;
    char buffer[010];
    a = 0x41414141;
    b = 0x42424242;

    if (write(STDOUT_FILENO, "For a moment, nothing happened. Then, after a second or so, nothing continued to happen.\n> ", 91) < 0) {
        perror("write");
    }

    if (read(STDIN_FILENO, &buffer, 0xC) < 0) {                                              
        perror("read");                                                                   
    }

    if (a == 31337) {
        system(buffer);
    }

    else if (b == 1337) {
        readfile("log.txt");
    }

    else {
        printf("B: 0x%08x\n", b);
    }

}

Answers


Ed Heal January 2017

You have two mistakes:

Line:

char buffer[010]; // This is octal i.e. 8!

should be

char buffer[0xc]; 

Also

read(STDIN_FILENO, &buffer, 0xC)

should be

read(STDIN_FILENO, buffer, 0xC)

As you need the pointer to the start of the buffer.

EDIT

Also you need to add the null character to buffer before system.

Post Status

Asked in January 2017
Viewed 3,004 times
Voted 9
Answered 1 times

Search




Leave an answer