HackThisSite - Programming Mission 12

Challenge

Level 12 — String manipulation

页面给出一串随机字符串。把所有数字取出来,按质数/合数分类(都是一位数,01 不计):先求所有合数之和,再求所有质数之和,两者相乘得到一个乘积。然后取字符串里前 25 个非数字字符,把每个字符的 ASCII 值加一(例:#$),把这 25 个字符与乘积首尾相接连成答案。限时 5 秒

Your answer should look like this: oc{lujxdpb%jvqrt{luruudtx140224

Solution

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
"""Solve HackThisSite Programming Mission 12."""

import html
import os
import re

import requests

BASE = "https://www.hackthissite.org"
LEVEL_URL = f"{BASE}/missions/prog/12/"
USER_AGENT = (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
)
PRIMES = {2, 3, 5, 7}
COMPOSITES = {4, 6, 8, 9}


def make_session():
cookie = os.environ.get("HTS_COOKIE", "").strip().strip("'\"")
if not cookie:
raise SystemExit("HTS_COOKIE is not set")
client = requests.Session()
client.headers.update(
{
"User-Agent": USER_AGENT,
"Cookie": cookie,
"Referer": f"{BASE}/missions/programming/",
"Accept-Language": "en-US,en;q=0.9",
}
)
return client


def body_text(source):
source = re.sub(r"<script.*?</script>", "", source, flags=re.S)
source = re.sub(r"<br\s*/?>", "\n", source)
source = re.sub(r"</(?:p|div|tr)>", "\n", source)
source = re.sub(r"<[^>]+>", " ", source)
source = html.unescape(source)
return re.sub(r"\n\s*\n+", "\n", re.sub(r"[ \t]+", " ", source))


def parse(page_text):
match = re.search(r'<input type="text" value="([^"]+)"', page_text)
if match is None:
raise SystemExit("random string not found in page")
return match.group(1)


def solve(value):
digits = [int(char) for char in value if char.isdigit()]
composite_sum = sum(digit for digit in digits if digit in COMPOSITES)
prime_sum = sum(digit for digit in digits if digit in PRIMES)
product = composite_sum * prime_sum
first25 = [char for char in value if not char.isdigit()][:25]
shifted = "".join(chr(ord(char) + 1) for char in first25)
return shifted + str(product), (composite_sum, prime_sum, product)


def verdict(response_text):
text = body_text(response_text).lower()
success = any(
marker in text
for marker in (
"congratulation",
"you have completed",
"completed this",
"successfully",
"correct",
"well done",
"mission accomplished",
"complete!",
)
)
failure = any(
marker in text
for marker in (
"wrong",
"incorrect",
"not correct",
"try again",
"failed",
"too late",
"time is up",
"sorry",
)
)
if success and not failure:
return True
if failure and not success:
return False
return None


def submit(client, answer):
response = client.post(
f"{LEVEL_URL}index.php",
data={"solution": answer, "submitbutton": "submit"},
headers={"Referer": LEVEL_URL},
timeout=30,
)
response.raise_for_status()
return verdict(response.text)


def main():
client = make_session()
page = client.get(LEVEL_URL, timeout=20)
page.raise_for_status()
value = parse(page.text)
answer, info = solve(value)
composite_sum, prime_sum, product = info
print("input prefix:", value[:60])
print(
"sums: composite=%d prime=%d product=%d"
% (composite_sum, prime_sum, product)
)
print("answer:", answer)
print("verdict:", submit(client, answer))


if __name__ == "__main__":
main()