HackThisSite - Programming Mission 2
Challenge
Level 2 — Analyze the picture and find the ascii code
The pixels in the above image are numbered 0..99 for the first row, 100..199 for the second row etc. White pixels represent ascii codes. The ascii code for a particular white pixel is equal to the offset from the last white pixel. For example, the first white pixel at location 65 would represent ascii code 65 ('A'), the next at location 131 would represent ascii code (131 - 65) = 66 ('B') and so on. The text contained in the image is the answer encoded in Morse, where
a testwould be encoded as.- / - . ... -图片像素按行编号(第 1 行 0..99,第 2 行 100..199,依此类推),白色像素表示 ASCII 码:某个白像素的 ASCII 值 = 它相对上一个白像素的位置偏移(第一个白像素相对 0 计)。把偏移还原成字符会得到一串 Morse 编码,解出的文本就是答案,限时 15 秒。
每次访问实例页都会重新生成一张随机图片和对应答案,15 秒内必须完成取图
→ 解码 →
提交,手动看是来不及的。状态:verified(服务端返回
Good Job, ***, You have successfully completed this mission)。
Solution
- 实例页 HTML 里图片标签是
<img src="/missions/prog/2/PNG" alt="Image" />。 - 直接请求
/missions/prog/2/PNG返回301跳到带斜杠的/missions/prog/2/PNG/,图片真实地址是后者;少一个斜杠就多一次重定向,对 15 秒的预算不划算。 file报告PNG image data, 100 x 30, 1-bit colormap:100 列正好对应题面的行编号规则,1-bit palette 里index 0 = 黑, index 1 = 白。
Step 1: 图片地址与格式
1 | $ cd <hts-workspace> && export HTS_COOKIE='<mission-cookie>' |
1 | $ file evidence_sample.png |
Step 2: 像素 → ASCII → Morse
扫图顺序是逐行、行内从左到右,线性位置
pos = y*100 + x。维护
prev(上一个白像素的位置,初值 0),遇到白像素就输出
chr(pos - prev),再把 prev 更新为
pos。因为字符就是
45 ('-')、46 ('.')、32 (' ')
这三个 ASCII 值,还原出来的字符串天然就是
Morse:空格分隔字母。一次真实样本的完整中间结果:
1 | white pixel linear positions: [46, 92, 124, 169, 214, 260, 306, 338, 383, 428, 474, 520, 552, 597, 642, 688, 734, 766, 812, 858, 903, 949, 981, 1027, 1072, 1117, 1149, 1194, 1239, 1284, 1329, 1374, 1406, 1452, 1498, 1543, 1589, 1621, 1666, 1711, 1757, 1789, 1835, 1880, 1925, 1970, 2015, 2047] |
逐个核对着色位移:46-0 = 46 = '.',92-46 = 46 = '.',124-92 = 32 = ' ';后面的
45 是 '-'。三个偏移值恰好覆盖 Morse
的全部符号与分隔符,说明偏移量 = ASCII 值、逐行顺序扫描的假设成立。Morse
只编码 A–Z 与 0–9,提交的是解出的明文串而不是 Morse 码本身。
Step 3: 一次运行内提交
challenges/hts-prog/2/solve.py 全文(依赖
challenges/hts-prog/common.py 里的
session()/fetch_level()/submit(),session cookie
从环境变量读取):
1 | #!/usr/bin/env python3 |
运行(实测从 GET 实例页到提交成功 3.45 s,限时 15 s):
1 | $ cd <hts-workspace> && export HTS_COOKIE='<mission-cookie>' |
服务端响应正文:
1 | Congrats |