WeChall - 2021 Christmas Friday

Challenge

作者 gizmore,分类 Coding。实现一个支持自定义 HTTP method BUNNY 的 Web 服务器,返回 HTTP/1.1 202 BunnyAccepted。WeChall 的 checker 会主动访问你提交的 URL 来验证。

1
2
3
4
The christmas sales are going up, and we have won a special customer.
Our new client wants an httpd, but with a custom http method.
Basically you have to implement the "BUNNY" method which has to result in
a "HTTP/1.1 202 BunnyAccepted" response.

Solution

最小实现是一个 raw TCP socket server。不能用标准 HTTP 框架(Flask/Django 等),它们只认识标准 method 列表,碰到 BUNNY 直接返回 405。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import socket

HOST, PORT = "0.0.0.0", 8889

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(5)
while True:
conn, addr = s.accept()
with conn:
data = conn.recv(4096).decode("latin1", "replace")
method = data.split(None, 1)[0] if data.strip() else ""
if method == "BUNNY":
conn.sendall(b"HTTP/1.1 202 BunnyAccepted\r\nContent-Length: 0\r\n\r\n")
else:
conn.sendall(b"HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n")

逻辑很简单——检查请求行第一个 token,是 BUNNY 就返回 202,否则 405。

部署

本地起服务没用,WeChall 的 checker 需要访问公网地址。需要一个有公网 IP 且 WeChall 可达的服务器。

这里用 Vultr CLI 开一台临时 VPS:

1
2
3
4
5
vultr-cli instance create \
--region ewr \
--plan vc2-1c-1gb \
--os 2136 \
--label wechall-bunny

拿到 IP 和 root 密码后,SSH 进去部署:

1
2
3
4
sshpass -p 'PASSWORD' ssh root@IP 'ufw allow 8889/tcp'

# 部署 server 脚本
# nohup python3 bunny_server.py &

验证

向 WeChall 提交 URL http://<VPS_IP>:8889,checker 用 BUNNY method 访问,收到 202 即判定通过。

清理

挑战通过后立即删除 VPS 停止计费:

1
vultr-cli instance delete <INSTANCE_ID>