| 1 | """Python 표준 LZMA 구현으로 합성 7z 표본을 생성함.""" |
| 2 | import lzma |
| 3 | from pathlib import Path |
| 4 | import struct |
| 5 | import zlib |
| 6 | |
| 7 | ROOT = Path(__file__).resolve().parent / "fixtures" / "7z" |
| 8 | SIGNATURE = b"7z\xbc\xaf\x27\x1c\x00\x04" |
| 9 | PAYLOAD = b"Zipelo 7z interoperability\n" * 100 |
| 10 | |
| 11 | |
| 12 | def integer(n): |
| 13 | for i in range(8): |
| 14 | if n < 1 << (7 + 7 * i): |
| 15 | return bytes([(0xFF << (8 - i) & 255) | (n >> (8 * i))]) + n.to_bytes(8, "little")[:i] |
| 16 | return b"\xff" + struct.pack("<Q", n) |
| 17 | |
| 18 | |
| 19 | def bits(values): |
| 20 | return bytes(sum((128 >> bit) if i + bit < len(values) and values[i + bit] else 0 |
| 21 | for bit in range(8)) for i in range(0, len(values), 8)) |
| 22 | |
| 23 | |
| 24 | def property_bytes(tag, value): |
| 25 | return bytes([tag]) + integer(len(value)) + value |
| 26 | |
| 27 | |
| 28 | def packed(data, method, lc=3, lp=0, pb=2): |
| 29 | if method == "copy": |
| 30 | return data, b"\x01\x00" |
| 31 | is_lzma2 = method == "lzma2" |
| 32 | settings = {"id": lzma.FILTER_LZMA2 if is_lzma2 else lzma.FILTER_LZMA1, |
| 33 | "dict_size": 4096, "lc": lc, "lp": lp, "pb": pb} |
| 34 | stream = lzma.compress(data, format=lzma.FORMAT_RAW, filters=[settings]) |
| 35 | assert lzma.decompress(stream, format=lzma.FORMAT_RAW, filters=[settings]) == data |
| 36 | props = b"\x00" if is_lzma2 else bytes([(pb * 5 + lp) * 9 + lc]) + struct.pack("<I", 4096) |
| 37 | method_id = b"\x21" if is_lzma2 else b"\x03\x01\x01" |
| 38 | return stream, bytes([0x20 | len(method_id)]) + method_id + integer(len(props)) + props |
| 39 | |
| 40 | |
| 41 | def streams(position, packed_bytes, coder, data, substreams): |
| 42 | info = b"\x06" + integer(position) + b"\x01\x09" + integer(len(packed_bytes)) |
| 43 | info += b"\x0a\x01" + struct.pack("<I", zlib.crc32(packed_bytes)) + b"\x00" |
| 44 | info += b"\x07\x0b\x01\x00\x01" + coder + b"\x0c" + integer(len(data)) |
| 45 | info += b"\x0a\x01" + struct.pack("<I", zlib.crc32(data)) + b"\x00" |
| 46 | if len(substreams) != 1: |
| 47 | info += b"\x08\x0d" + integer(len(substreams)) |
| 48 | if substreams: |
| 49 | info += b"\x09" + b"".join(integer(len(part)) for part in substreams[:-1]) |
| 50 | info += b"\x0a\x01" + b"".join(struct.pack("<I", zlib.crc32(part)) for part in substreams) |
| 51 | info += b"\x00" |
| 52 | else: |
| 53 | info += b"\x08\x00" |
| 54 | return info + b"\x00" |
| 55 | |
| 56 | |
| 57 | def archive(entries, method="lzma2", encoded=False, properties=(3, 0, 2), damage=None): |
| 58 | parts = [data for _, data, _ in entries if data] |
| 59 | data = b"".join(parts) |
| 60 | compressed, coder = packed(data, method, *properties) |
| 61 | if damage == "payload": |
| 62 | compressed = compressed[:len(compressed) // 2] |
| 63 | if damage == "trailing": |
| 64 | compressed += b"unexpected" |
| 65 | if damage == "dictionary": |
| 66 | coder = b"\x21\x21\x01\x28" |
| 67 | info = b"\x01\x04" + streams(0, compressed, coder, data, parts) |
| 68 | files = b"\x05" + integer(len(entries)) |
| 69 | empties = [not content for _, content, _ in entries] |
| 70 | if any(empties): |
| 71 | files += property_bytes(14, bits(empties)) |
| 72 | files += property_bytes(15, bits([attr & 0x10 == 0 for _, content, attr in entries if not content])) |
| 73 | files += property_bytes(17, b"\x00" + b"".join(name.encode("utf-16le") + b"\x00\x00" for name, _, _ in entries)) |
| 74 | files += property_bytes(21, b"\x01\x00" + b"".join(struct.pack("<I", attr) for _, _, attr in entries)) |
| 75 | header = info + files + b"\x00\x00" |
| 76 | if damage == "crc": |
| 77 | # 데이터 CRC만 틀리게 하며 헤더와 packed CRC는 다시 계산함. |
| 78 | good = struct.pack("<I", zlib.crc32(data)) |
| 79 | at = header.index(good) |
| 80 | header = header[:at] + bytes([header[at] ^ 1]) + header[at + 1:] |
| 81 | if encoded: |
| 82 | encoded_bytes, encoded_coder = packed(header, "lzma") |
| 83 | next_header = b"\x17" + streams(len(compressed), encoded_bytes, encoded_coder, header, [header]) |
| 84 | compressed += encoded_bytes |
| 85 | header = next_header |
| 86 | start = struct.pack("<QQI", len(compressed), len(header), zlib.crc32(header)) |
| 87 | return SIGNATURE + struct.pack("<I", zlib.crc32(start)) + start + compressed + header |
| 88 | |
| 89 | |
| 90 | def build(): |
| 91 | result = {} |
| 92 | entries = [("folder/hello.txt", PAYLOAD, 0x20)] |
| 93 | for method in ("copy", "lzma", "lzma2"): |
| 94 | result[f"{method}.7z"] = archive(entries, method) |
| 95 | result["encoded-solid.7z"] = archive([("folder", b"", 0x10), ("folder/empty", b"", 0x20), |
| 96 | ("folder/hello.txt", PAYLOAD, 0x20), ("한글.txt", PAYLOAD[::-1], 0x20)], encoded=True) |
| 97 | for i, params in enumerate(((0, 0, 0), (4, 0, 4), (1, 2, 1), (2, 2, 3))): |
| 98 | result[f"properties-{i}.7z"] = archive(entries, "lzma", properties=params) |
| 99 | result["long-match.7z"] = archive([("folder/hello.txt", PAYLOAD * 100, 0x20)]) |
| 100 | noise = bytes(((i * 1103515245 + (i >> 2) * 12345) >> ((i % 4) * 8)) & 255 for i in range(100000)) |
| 101 | result["lzma2-chunks.7z"] = archive([("noise", noise, 0x20)]) |
| 102 | for name, hostile in (("dotdot", "../outside.txt"), ("absolute", "/outside.txt"), |
| 103 | ("drive", "C:/outside.txt"), ("backslash", "..\\outside.txt"), |
| 104 | ("deep", "a/" * 65 + "file")): |
| 105 | result[f"unsafe-{name}.7z"] = archive([("before.txt", PAYLOAD, 0x20), (hostile, PAYLOAD, 0x20)]) |
| 106 | result["symlink.7z"] = archive([("before.txt", PAYLOAD, 0x20), ("link", b"../outside", (0o120777 << 16) | 0x8000)]) |
| 107 | result["collision.7z"] = archive([("hello", PAYLOAD, 0x20), ("HELLO", PAYLOAD, 0x20)]) |
| 108 | for damage in ("payload", "crc", "dictionary", "trailing"): |
| 109 | result[f"bad-{damage}.7z"] = archive(entries, damage=damage) |
| 110 | return result |
| 111 | |
| 112 | |
| 113 | if __name__ == "__main__": |
| 114 | ROOT.mkdir(exist_ok=False) |
| 115 | for name, data in build().items(): |
| 116 | (ROOT / name).write_bytes(data) |