import zipfile,itertools,string,sys from multiprocessing import Pool
C=string.ascii_lowercase+string.digits
def crack(a): path,name,pre,c=a with zipfile.ZipFile(path) as z: for x in itertools.product(C,repeat=3): pwd=(pre+c+''.join(x)).encode() try: z.read(name,pwd=pwd) return pwd except (RuntimeError,zipfile.BadZipFile): pass
def solve(path): with zipfile.ZipFile(path) as z: name=z.namelist()[0] pre=z.comment.decode()[:-4]
pwd=None with Pool() as p: for r in p.imap_unordered(crack,[(path,name,pre,c) for c in C]): if r: pwd=r p.terminate() break
with zipfile.ZipFile(path) as z: open(name,'wb').write(z.read(name,pwd=pwd))
print(f'{path}: {pwd.decode()}') return name
if __name__=='__main__': p=sys.argv[1] if len(sys.argv)>1 else 'layer_29.zip' while p.endswith('.zip'): p=solve(p) print('final:',p)
# 枚举所有可能的宽度和高度组合 for i in range(1, 4096): # 宽度1-4095 for j in range(1, 4096): # 高度1-4095 # 构造IHDR块数据(不包括长度字段) data = crcbp[12:16] + struct.pack('>i', i) + struct.pack('>i', j) + crcbp[24:29]
import os, sys, zlib, gzip, struct, json from concurrent.futures import ProcessPoolExecutor import numpy as np
REGION_DIR = r"G:\ctf\pregen\server\world\dimensions\minecraft\overworld\region" OUT = r"G:\ctf\pregen\scan_out"
class P: __slots__ = ("d", "i") def __init__(self, d): self.d = d; self.i = 0 def u1(self): v = self.d[self.i]; self.i += 1; return v def fmt(self, f, n): v = struct.unpack_from(f, self.d, self.i)[0]; self.i += n; return v def s(self): n = struct.unpack_from('>H', self.d, self.i)[0]; self.i += 2 v = self.d[self.i:self.i+n]; self.i += n return v.decode('utf-8', 'replace') def payload(self, t): if t == 1: return self.fmt('>b',1) if t == 2: return self.fmt('>h',2) if t == 3: return self.fmt('>i',4) if t == 4: return self.fmt('>q',8) if t == 5: return self.fmt('>f',4) if t == 6: return self.fmt('>d',8) if t == 7: n = self.fmt('>i',4); v = self.d[self.i:self.i+n]; self.i += n; return v if t == 8: return self.s() if t == 9: it = self.u1(); n = self.fmt('>i',4) if it == 0: return [] if it in (1,2,3,4,5,6): sz = {1:1,2:2,3:4,4:8,5:4,6:8}[it] fk = {1:'>i1',2:'>i2',3:'>i4',4:'>i8',5:'>f4',6:'>f8'}[it] v = np.frombuffer(self.d, fk, n, self.i); self.i += sz*n return f'<prim x{n}>' return [self.payload(it) for _ in range(n)] if t == 10: d = {} while True: tt = self.u1() if tt == 0: return d name = self.s() d[name] = self.payload(tt) if t == 11: n = self.fmt('>i',4); v = np.frombuffer(self.d, '>i4', n, self.i); self.i += 4*n; return v if t == 12: n = self.fmt('>i',4); v = np.frombuffer(self.d, '>i8', n, self.i); self.i += 8*n; return v raise ValueError('bad tag')
def decode_indices(data, n, palette_len): bits = max(4, (palette_len - 1).bit_length()) per = 64 // bits longs = np.frombuffer(data, dtype=np.uint64).byteswap() idx = np.arange(n) li = idx // per sh = (idx % per).astype(np.uint64) * np.uint64(bits) mask = np.uint64((1 << bits) - 1) return ((longs[li] >> sh) & mask).astype(np.uint16)
# pattern template: T=tuff O=deepred A=air D=deepslate .=don't care # rows = z (row0 north side drawn first), cols = x TEMPLATE = [ list("TTTT"), list("TOT T".replace(" ", "")), list("ATOT"), list("AADT"), ] def norm(t): return [list(row) for row in t] TEMPLATE = [["T","T","T","T"],["T","O","T","T"],["A","T","O","T"],["A","A","D","T"]]
def rot90(m): return [list(row) for row in zip(*m[::-1])] def mirror(m): return [row[::-1] for row in m]
variants = [] m = TEMPLATE for k in range(4): variants.append([row[:] for row in m]) variants.append(mirror(m)) m = rot90(m)
def process_region(path): out = [] data = open(path, 'rb').read() if len(data) < 8192: return out for i in range(1024): b = data[i*4:i*4+4] off = (b[0] << 16) | (b[1] << 8) | b[2] if off == 0 or b[3] == 0: continue start = off * 4096 if start + 5 > len(data): continue ln = struct.unpack_from('>I', data, start)[0] if start + 4 + ln > len(data): continue comp = data[start+4] raw = data[start+5:start+4+ln] try: raw = zlib.decompress(raw) if comp == 2 else (gzip.decompress(raw) if comp == 1 else raw if comp == 3 else None) except Exception: continue if raw is None: continue try: p = P(raw); t = p.u1(); p.s(); d = p.payload(t) except Exception: continue if not isinstance(d, dict): continue if d.get('Status') not in ('minecraft:full', 'full'): continue cx, cz = d.get('xPos'), d.get('zPos') if cx is None: continue for s in d.get('sections', []): sy = int(s.get('Y', -999)) if not (-6 <= sy <= 2): continue bs = s.get('block_states') if bs is None: continue pal = [e.get('Name', '?') for e in bs.get('palette', [])] has_tuff = any(n.endswith(':tuff') for n in pal) has_dro = any(n == 'minecraft:deepslate_redstone_ore' for n in pal) if not (has_tuff and has_dro): continue dt = bs.get('data') if dt is None: continue # class ids: 0 air, 1 tuff, 2 deepslate_redstone_ore, 3 deepslate-ish, 4 other cls = np.full(len(pal), 4, dtype=np.uint8) for k, n in enumerate(pal): if n in AIR_IDS: cls[k] = 0 elif n == 'minecraft:tuff': cls[k] = 1 elif n == 'minecraft:deepslate_redstone_ore': cls[k] = 2 elif 'deepslate' in n: cls[k] = 3 ind = decode_indices(dt, 4096, len(pal)) # (y, z, x) after reshape vol = cls[ind].reshape(16, 16, 16) # (y, z, x) for yy in range(16): sl = vol[yy] # (z, x) for vi, tmpl in enumerate(variants): # slide 4x4 for zz in range(13): for xx in range(13): ok = True for dz in range(4): for dx in range(4): ch = tmpl[dz][dx] if ch == '.': continue want = {'T':1, 'O':2, 'A':0, 'D':3}[ch] got = int(sl[zz+dz, xx+dx]) if want == 3: if got != 3 and got != 4: ok = False; break elif got != want: ok = False; break if not ok: break if ok: wy = sy*16 + yy out.append((cx*16+xx, wy, cz*16+zz, vi)) return out
def main(): files = [os.path.join(REGION_DIR, f) for f in os.listdir(REGION_DIR) if f.endswith('.mca')] print(f"[*] {len(files)} region files", flush=True) hits = [] with ProcessPoolExecutor(max_workers=12) as ex: for res in ex.map(process_region, files, chunksize=1): hits.extend(res) print(f"[*] pattern hits: {len(hits)}", flush=True) for h in hits: print(" x=%d y=%d z=%d variant=%d" % h) with open(os.path.join(OUT, 'pattern_hits.json'), 'w') as f: json.dump(hits, f)