# 1. 生成 21×21 二维二维码矩阵 defqr_matrix(data: str): qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=1, border=0 ) qr.add_data(data) qr.make(fit=False) mat = qr.get_matrix() # mat[y][x] # 转成 [x][y] return [[1if mat[y][x] else0for y inrange(21)] for x inrange(21)]
front = qr_matrix("Azure") left = qr_matrix("Assassin") top = qr_matrix("Alliance")
# 2. 建 ILP 并求解 prob = pulp.LpProblem("ctf_3d_qr", pulp.LpMinimize) p = [[[pulp.LpVariable(f"p_{x}_{y}_{z}", cat="Binary") for z inrange(21)] for y inrange(21)] for x inrange(21)]
# 目标:最小化体素数 prob += pulp.lpSum(p[x][y][z] for x inrange(21) for y inrange(21) for z inrange(21))
# front (XY) for x inrange(21): for y inrange(21): s = pulp.lpSum(p[x][y][z] for z inrange(21)) prob += (s >= 1) if front[x][y] else (s == 0)
# left (YZ) for y inrange(21): for z inrange(21): s = pulp.lpSum(p[x][y][z] for x inrange(21)) prob += (s >= 1) if left[y][z] else (s == 0)
# top (XZ) for x inrange(21): for z inrange(21): s = pulp.lpSum(p[x][y][z] for y inrange(21)) prob += (s >= 1) if top[x][z] else (s == 0)
# 限制总黑点 < 390 prob += pulp.lpSum(p[x][y][z] for x inrange(21) for y inrange(21) for z inrange(21)) <= 389
prob.solve(pulp.PULP_CBC_CMD(msg=False))
# 3. 平展成 9261 位二进制串 bits = [] for z inrange(21): for y inrange(21): for x inrange(21): bits.append('1'if pulp.value(p[x][y][z]) > 0.5else'0') data_str = ''.join(bits)
# 4. 远程连接 + POW + 发送 + 打 flag defsolve(): HOST, PORT = "1.95.71.197", 9999 r = remote(HOST, PORT, timeout=10) line = r.recvline().decode().strip() # e.g. sha256(XXXX+abcdef) == deadbeef... left_part, target = map(str.strip, line.split("==",1)) suffix = left_part.split("+",1)[1].rstrip(")") log.info(f"POW suffix = {suffix!r}, target hash = {target}") suffix_b = suffix.encode()
# 四重循环暴力破解 4 字符 XXXX chars = string.ascii_letters + string.digits found = None for a in chars: for b in chars: for c in chars: for d in chars: xxxx = a+b+c+d h = hashlib.sha256() h.update(xxxx.encode()) h.update(suffix_b) if h.hexdigest() == target: found = xxxx break if found: break if found: break if found: break
from ultralytics import YOLO import numpy as np from PIL import Image import time import base64 from io import BytesIO import torch import torch.nn as nn from torchvision import transforms