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 126 127 128 129 130 131 132 133 134 135
| import sys, re from collections import defaultdict
try: from PIL import Image except ImportError: sys.exit("[!] 需要 Pillow: pip install pillow")
def parse_sixel(raw: str): m = re.search(r'\x1bPq(.+?)\x1b\\', raw, re.DOTALL) \ or re.search(r'\x1bP(.+?)\x1b\\', raw, re.DOTALL) payload = m.group(1) if m else raw payload = re.sub(r'^P?q?', '', payload, count=1) if not m else payload
x, y, cur_color = 0, 0, 0 pixel_map = defaultdict(list)
i, n = 0, len(payload) while i < n: ch = payload[i]
pm = re.match(r'"(\d+);(\d+);(\d+);(\d+)', payload[i:]) if pm: i += len(pm.group(0)); continue
pm = re.match(r'#\d+;\d+;\d+;\d+;\d+', payload[i:]) if pm: i += len(pm.group(0)); continue
pm = re.match(r'#(\d+)', payload[i:]) if pm: cur_color = int(pm.group(1)); i += len(pm.group(0)); continue
if ch == '!': pm = re.match(r'!(\d+)(.)', payload[i:]) if pm: cnt, sch = int(pm.group(1)), pm.group(2) for _ in range(cnt): pixel_map[(x, y)].append((cur_color, sch)); x += 1 i += len(pm.group(0)); continue i += 1; continue
if ch == '$': x = 0; i += 1; continue if ch == '-': x = 0; y += 6; i += 1; continue
if 63 <= ord(ch) <= 126: pixel_map[(x, y)].append((cur_color, ch)); x += 1; i += 1; continue
i += 1
return pixel_map
def sixel_to_bits(ch): v = ord(ch) - 63 return [(v >> b) & 1 for b in range(6)]
def render_layer(pixel_map, layer_index, only_repeated=True): targets = {pos: layers for pos, layers in pixel_map.items() if (not only_repeated) or len(layers) > 1} if not targets: return None max_col = max(p[0] for p in targets) max_row = max(p[1] for p in targets) + 6 bmp = [[0] * (max_col + 1) for _ in range(max_row)] for (cx, cy), layers in targets.items(): if layer_index >= len(layers): continue _, sch = layers[layer_index] for b, bit in enumerate(sixel_to_bits(sch)): if bit: bmp[cy + b][cx] = 1 return bmp
def save_png(bmp, path, scale=4): if not bmp: print(f"[!] 空位图,跳过 {path}"); return h, w = len(bmp), len(bmp[0]) img = Image.new('L', (w * scale, h * scale), 255) px = img.load() for r in range(h): for c in range(w): if bmp[r][c]: for dr in range(scale): for dc in range(scale): px[c*scale+dc, r*scale+dr] = 0 img.save(path) print(f"[+] 已保存 {path} (逻辑 {w}x{h}, 实际 {w*scale}x{h*scale})")
def ascii_preview(bmp, max_rows=60): rows = bmp[:max_rows] return '\n'.join(''.join('█' if c else ' ' for c in row) for row in rows)
def main(): path = sys.argv[1] if len(sys.argv) > 1 else 'flag.txt' with open(path, encoding='utf-8', errors='replace') as f: raw = f.read()
pmap = parse_sixel(raw) repeated = {p: v for p, v in pmap.items() if len(v) > 1}
print(f"[*] 文件: {path}") print(f"[*] 像素坐标总数: {len(pmap)}") print(f"[*] 被重复绘制(覆盖)坐标数: {len(repeated)}") if not repeated: sys.exit("[!] 未发现重复像素点")
max_layer = max(len(v) for v in repeated.values()) print(f"[*] 最大覆盖层数: {max_layer}") for idx in range(max_layer): chars = sorted({repeated[p][idx][1] for p in repeated if idx < len(repeated[p])}) print(f" layer{idx}: {len(chars)} 种 -> {''.join(chars)[:60]}")
top_idx = max_layer - 1
print("\n=== [1] 重复点 · 最上层 -> 二维码 ===") save_png(render_layer(pmap, top_idx, only_repeated=True), 'repeated_top.png')
print("\n=== [2] 重复点 · 首次写入层(被盖住) -> Malbolge 位图 ===") bmp_hid = render_layer(pmap, 0, only_repeated=True) save_png(bmp_hid, 'repeated_hidden.png') print("\n--- 隐藏层 ASCII 预览(前60行) ---") print(ascii_preview(bmp_hid))
code = ''.join(repeated[(cx, cy)][0][1] for cx, cy in sorted(repeated, key=lambda p: (p[1], p[0]))) print(f"\n[+] 隐藏层 sixel 字符序列 (len={len(code)}):\n{code}")
print("\n=== [3] 整张图 · 最上层 (对照) ===") save_png(render_layer(pmap, top_idx, only_repeated=False), 'full_top.png'
if __name__ == '__main__': main()
|