먼저 문제의 소스코드는 다음과 같다.
#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]);
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);
}
}
먼저 컴파일 시켜 실행시켜보자.
gcc -z execstack -w -no-pie -o stack1 stack1.c

이후 실행을 시키면 다음과 같은 문구가 나온다.

소스코드와 함께 분석을 하면 argument 가 1일때 위와 같은 문구를 출력시킨다.
int main(int argc, char **argv)
{
volatile int modified;
char buffer[64];
if(argc == 1) {
errx(1, "please specify an argument\\n");
}
argc가 1이 아니게 하려면 실행을 시킬때 다음과 같이 입려해야 한다.
./sudo stack1 1234
argc가 1이 아니여야 하기에 뒤에 추가로 값을 넣어 함수를 호출할때 입력되는 값이 1이 아니게 해야한다는 것이다.
그러면 정상 실행이 되므로 다음과 같은 화면이 뜬다.

다시 한번 문제 소스코드를 봐보자.
#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]);
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);
}
}
여기서 주목해야할 것은 modified 값이다.
변수 modified 값은 0으로 선언되며 이 변수가 0x61626364 일때 "you have correctly got the variable to the right value\n" 구문이 출력된다.
다음으로 주목해야 할 부분은 strcpy 함수이다. Buffer Over Flow 의 취약점이 존재한다.