|
| 1 | +import requests |
| 2 | +import zipfile |
| 3 | + |
| 4 | + |
| 5 | +def main(): |
| 6 | + url = "http://www.pythonchallenge.com/pc/hex/unreal.jpg" |
| 7 | + auth = ("butter", "fly") |
| 8 | + # first_step(url, auth) |
| 9 | + # second_step(url, auth) |
| 10 | + # third_step(url, auth) |
| 11 | + # fourth_step(hint) |
| 12 | + # five_step(url, auth) |
| 13 | + # six_step(url, auth) |
| 14 | + seven_step("fence/1152983631.zip") |
| 15 | + |
| 16 | + |
| 17 | +def first_step(url, auth): |
| 18 | + res = requests.get(url, auth=auth) |
| 19 | + res_header = res.headers |
| 20 | + res_content = res.content |
| 21 | + print("res_header is {}, res_content is {}".format(res_header, res_content)) |
| 22 | + # 'Content-Range': 'bytes 0-30202/2123456789 |
| 23 | + |
| 24 | + |
| 25 | +def second_step(url, auth): |
| 26 | + start = 0 |
| 27 | + while True: |
| 28 | + print("start is {}".format(start)) |
| 29 | + if start >= 2123456789: |
| 30 | + break |
| 31 | + headers = {"RANGE": "bytes={}-".format(start)} |
| 32 | + res = requests.get(url, auth=auth, headers=headers) |
| 33 | + res_headers = res.headers |
| 34 | + res_content_range = res_headers.get("Content-Range") |
| 35 | + if res_content_range is not None: |
| 36 | + begin, end = get_start_end(res_content_range) |
| 37 | + print("===================== {} ========================".format(start)) |
| 38 | + print("res_content is {}".format(res.content)) |
| 39 | + print("res_heads is {}".format(res.headers)) |
| 40 | + print("res_raw is {}".format(res.raw)) |
| 41 | + print("res_text is {}".format(res.text)) |
| 42 | + print("res_cookies is {}".format(res.cookies)) |
| 43 | + print("=================================================") |
| 44 | + start = int(end) |
| 45 | + else: |
| 46 | + start += 1 |
| 47 | + |
| 48 | + |
| 49 | +def get_start_end(st): |
| 50 | + bscount_sum = st[6:] |
| 51 | + start_to_end = bscount_sum.split("/")[0] |
| 52 | + start = start_to_end.split("-")[0] |
| 53 | + end = start_to_end.split("-")[1] |
| 54 | + print("start is {}, end is {}".format(start, end)) |
| 55 | + return start, end |
| 56 | + |
| 57 | + |
| 58 | +def third_step(url, auth): |
| 59 | + request_url_with_range(url, auth, 2123456789) |
| 60 | + # esrever ni emankcin wen ruoy si drowssap eht |
| 61 | + |
| 62 | + |
| 63 | +def request_url_with_range(url, auth, num): |
| 64 | + headers = {"RANGE": "bytes={}-".format(num)} |
| 65 | + res = requests.get(url, auth=auth, headers=headers) |
| 66 | + print("=================================================") |
| 67 | + print("res_content is {}".format(res.content)) |
| 68 | + print("res_heads is {}".format(res.headers)) |
| 69 | + print("res_raw is {}".format(res.raw)) |
| 70 | + print("res_text is {}".format(res.text)) |
| 71 | + print("res_cookies is {}".format(res.cookies)) |
| 72 | + print("=================================================") |
| 73 | + |
| 74 | + |
| 75 | +def fourth_step(hint): |
| 76 | + result = "" |
| 77 | + for i in range(len(hint) - 1, -1, -1): |
| 78 | + result += hint[i] |
| 79 | + print(result) |
| 80 | + return result |
| 81 | + # the password is your new nickname in reverse |
| 82 | + |
| 83 | + |
| 84 | +def five_step(url, auth): |
| 85 | + request_url_with_range(url, auth, 2123456743) |
| 86 | + # and it is hiding at 1152983631. |
| 87 | + |
| 88 | + |
| 89 | +def six_step(url, auth): |
| 90 | + headers = {"RANGE": "bytes={}-".format(1152983631)} |
| 91 | + res = requests.get(url, auth=auth, headers=headers) |
| 92 | + print(res.content) |
| 93 | + file = open("fence/1152983631.zip", "wb") |
| 94 | + file.write(res.content) |
| 95 | + print("finished") |
| 96 | + |
| 97 | + |
| 98 | +def seven_step(file_path): |
| 99 | + with zipfile.ZipFile(file_path) as myzip: |
| 100 | + pwd = fourth_step("invader") |
| 101 | + print(pwd) |
| 102 | + files = myzip.filelist |
| 103 | + for file in files: |
| 104 | + with myzip.open(file.filename, pwd=pwd.encode()) as tem_file: |
| 105 | + print(tem_file.read()) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments