IT’s Portfolio

[Python Challenge] Level 4 풀이 본문

Security Study/Wargame Explanation

[Python Challenge] Level 4 풀이

f1r3_r41n 2019. 11. 24. 14:48
728x90
반응형

http://pythonchallenge.com

 

The Python Challenge

What people have said about us: "These sorts of things are in my opinion the best way to learn a language.", brberg at Media Cloisters "It's the best web site of the year so far.", Andy Todd at halfcooked "Addictive way to learn the ins and outs of Python.

www.pythonchallenge.com

 

Python Challenge Level 4의 풀이이다.

linkedlist.php로 가보자.

중앙에 보이는 사진을 클릭하게되면

이런 페이지가 뜬다.

"and the next nothing is 44827" => "다음번의 nothing은 44827이다" 

웹 페이지의 주소를 보면 http://pythonchallenge.com/pc/def/linkedlist.php?nothing=12345 

 

linkedlist.php뒤에 nothing값을 넣으면 페이지의 내용이 달라지는 것 같다.

스크립트를 짜보자.

import requests
from bs4 import BeautifulSoup

nothing = "12345"

def get_data(nothing):
    url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + nothing

    hdr = {'User-Agent': ('mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/78.0.3904.70 safari/537.36')}
    req = requests.get(url, headers=hdr)
    html = req.text
    soup = BeautifulSoup(html, 'html.parser')

    data = str(soup)

    return get_nothing(data)

def get_nothing(data):
    txt = "and the next nothing is"
    if txt in data:
        nothing = data[data.find("is ") + 3:]
        print(nothing)
        return nothing
    else:
        return data

if __name__ == "__main__":
    while nothing.isnumeric() == True:
        nothing = get_data(nothing)
    print(nothing)

결과:

수많은 nothing들을 입력한 결과 while문에 걸린 문자열을 확인할 수 있다.

"Yes. Divide by two and keep going." => "예. 둘로 나누고 계속 진행하십시오."

 

16044/2 = 8022

위의 스크립트에서 첫 번째 nothing을 8022로 선언하고 다시 돌렸다.

82682를 nothing값에 넣고 페이지에 들어가보면

"There maybe misleading numbers in the text. One example is 82683. Look only for the next nothing and the next nothing is 63579" => "본문에 잘못된 숫자가있을 수 있습니다. 한 가지 예는 82683입니다. 다음 항목 만 찾고 다음 항목은 63579입니다."

 

다시 위의 스크립트에서 첫 nothing값을 63579로 선언하고 돌린다.

오랜 실행시간 끝에 peak.html이라는 URL 정답값이 나왔다.

 

URL 정답값: peak.html

 

최신화

2020.10.24

import re
from get_html_source import get_html

url = "http://www.pythonchallenge.com/pc/def/linkedlist.php"
A = get_html(url)

with open('source.txt', 'wt') as f:
    m = re.findall('<!--(.+?)-->', A.get_req().replace('\n', ''))
    nothing = ''.join(re.findall('[0-9]{5}', A.get_req().replace('\n', '')))
    f.write(''.join(m).strip())

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

linkedlist.php?nothing=12345
and the next nothing is 44827
'''

while nothing:
    print(nothing)
    A.change_url(url+'?nothing='+nothing)
    source = A.get_req()

    if m2:=re.findall('[0-9]+?', source):
        if len(m2) > 5:
            nothing = ''.join(m2[5:])
            continue
        nothing = ''.join(m2)
        continue
    elif not m2 and "Divide" in source:
        print(source.strip())
        nothing = str(int(int(nothing)/2))
        continue
    
    print(source)
    nothing = None
  • 정규식 사용
  • 페이지 소스 내의 nothing 값, 이 외의 변수들 자동 처리

 

Level 5로 갈 수 있는 URL = http://www.pythonchallenge.com/pc/def/peak.html

728x90
반응형
Comments