-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel_21.py
51 lines (42 loc) · 1.32 KB
/
level_21.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/env python
# coding=utf-8
# No url
# Decompress, decompress and reverse.
# @see http://tools.ietf.org/html/rfc1950#section-2.2
# @see http://en.wikipedia.org/wiki/Bzip2#File_format
import codecs
import zipfile
from io import BytesIO
import requests
PREFIX = "http://butter:fly@www.pythonchallenge.com/pc/hex/"
url = PREFIX + 'unreal.jpg'
ARCH_POS = 1152983631
PASSWD = b'redavni'
def prepare():
headers = {'Range': 'bytes={}-'.format(ARCH_POS)}
r = requests.get(url, headers=headers)
with zipfile.ZipFile(BytesIO(r.content), 'r') as zip:
return zip.read('package.pack', PASSWD)
def solve(pack):
zlib_header = b'\x78\x9c'
bz2_header = b'BZh'
result = []
while (True):
if pack.startswith(zlib_header):
pack = codecs.decode(pack, 'zlib')
result.append(' ')
elif pack.startswith(bz2_header):
pack = codecs.decode(pack, 'bz2')
result.append('#')
elif pack.endswith(zlib_header[::-1]):
pack = pack[::-1]
result.append('\n')
else:
break
return "".join(result)
if __name__ == "__main__":
pack = prepare()
answer = solve(pack)
print(answer)
# copper
# http://butter:fly@www.pythonchallenge.com/pc/hex/copper.html