Mommy told me to make a passcode based login system.
My initial C code compiled without any error!
Well, there was some compiler warning, but who cares about that?
엄마는 내게 인증번호 기반의 로그인 시스템을 만들어보라고 말했었다.
내 첫 C 코드 프로그램은 애러가 하나도 없었어!
근데, 워닝은 있더라고, 워닝은 뭐 신경 안써도 되니까.
The scanf is used incorrectly. This is why the warning occurs. The passcode must be modified to &passcode1 to work properly.
sacnf가 잘못 사용되고 있다. warning이 발생하는 이유다. passcode를 &passcode1로 수정해야 정상적으로 작동한다.
Since passcode1 contains a garbage value, if you use it like the above image, it invades another part of memory and errors occur.
passcode1은 쓰래기 값이 들어가있기 때문에, 위 이미지처럼 사용하면, 메모리의 다른 부분을 침해하여 에러가 발생한다.
Disassemble the welcome function with gdb considering the location of the functions, the address of the name variable is assumed to be [ebp-0x70] and the address of the passcode1 variable is assumed to be [ebp-0x10]. The distance between them is 0x60 == 96. You can enter 100 characters for the name variable. If you enter 100 characters, 96 will be in the name variable, and the remaining 4 will be in the passcode1 variable. This means that you can set the initial value of the passcode1 variable.
gdb로 welcome 함수를 디스어셈블 해보면, 함수들의 위치를 고려할 때, name 변수의 주소는 [ebp-0x70]으로 추정되고 passcode1 변수의 주소는 [ebp-0x10]으로 추정된다. 둘 사이의 거리는 0x60==96 이다. name 변수가 100글자를 입력할 수 있는데, 100글자를 입력하면 96개는 name 변수에 들어가고, 나머지 4개는 passcode1 변수에 들어간다. passcode1 변수의 초기값을 설정할 수 있다는 뜻이다.
When a function after scanf("%d", passcode1) is executed, you can change the memory so that system("/bin/bash") is executed instead of the function.
scanf("%d", passcode1) 함수 이후의 어떤 함수가 실행될 때, 그 함수 대신 system("/bin/bash")가 실행되도록 메모리를 변경할 수 있다.
※ Corresponding function : fflush, printf, exit
※ 해당하는 함수 : fflush, printf, exit
readelf -r ./passcode can be used to check the address of the function as the image above.
readelf -r ./passcode로 위 이미지처럼 함수들의 주소를 확인할 수 있다.
Alternatively, you can check the PLT(Procedure Linkage Table) address with the command readelf -S ./passcode, and then check the GOT(Global Offset Table) address of each function in the PLT.
다른 방법으로는, readelf -S ./passcode 명령으로 PLT(Procedure Linkage Table) 주소를 확인한 다음, PLT 에서 각 함수들의 GOT(Global Offset Table) 주소를 확인해도 된다.
Now check which function to fake. When you disassemble the login function, you can find the command that calls the system function.
이제 어떤 함수로 변조할지 확인한다. login 함수를 디스어셈블하면 system 함수를 호출하는 명령을 찾을 수 있다.
※ The reason for checking the address of the source code rather than the GOT address of the system function is that the system function should be called(call) after putting(mov) the string "/bin/cat flag" in 0x80487af.
※ system함수의 GOT 주소가 아닌 소스코드 상의 주소를 확인하는 이유는, 0x80487af에 들어있는 "/bin/cat flag"라는 문자열을 넣은(mov) 다음에 system 함수를 호출(call) 해야하기 때문이다.
The completed code is shown below.
완성된 코드는 아래와 같다.
□ (perl -e 'print "a"x96, "\x18\xa0\x04\x08", "134514147"') | ./passcode
○ 96 bytes of the name variable 100 bytes are filled with a
○ In 4 bytes(=passcode1) of name variable 100 bytes, input the address of the function to be faked
○ When entering data in the scanf("%d", passcode1), enter address (0x080485e3==134514147) of the system("/bin/cat flag")
□ (perl -e 'print "a"x96, "\x18\xa0\x04\x08", "134514147"') | ./passcode
○ name 변수 100 Byte 중 96 Byte는 a로 채운다
○ name 변수 100 Byte 중 4 Byte(=passcode1)에는 변조하고 싶은 함수의 주소를 입력한다
○ scanf("%d", passcode1)에 데이터를 입력할 때, system("/bin/cat flag")의 주소를 입력(0x080485e3==134514147)한다