dev.forensics
identify files by their contents, unpack compressed data and archives, and find files hidden inside others
Permissions: files.read
Most file formats start with a few fixed bytes, a signature or “magic number”, that say what they are whatever the file’s name. Signatures identify a file and find the files hidden inside a larger blob, like a disk image or a firmware dump, and compressed data and archives can be unpacked. A look at how random the data is shows which parts are usually compressed or encrypted.
Tags: binary, files
magicwhat kind of file this is, from the signature bytes it starts with, like thefilecommanddecompressuncompress bytes in any format this module knows (gzip, zlib, xz, zstd, bzip2, or raw deflate), picked by its signatureinflateuncompress raw deflate data, the format inside zip files, gzip and PNG imagesdeflatecompress bytes with raw deflate, no header or checksumgunzipuncompress gzip data, the.gzformat, checking its CRC-32gzipcompress bytes into gzip, the.gzformatunzlibuncompress zlib data, deflate with a 2-byte header and an Adler-32 checksum, as in PDFs and PNG chunkszlibcompress bytes into zlib, deflate with a 2-byte header and an Adler-32 checksumunxzuncompress xz data, the.xzformatxzcompress bytes into xz, the.xzformatunzstduncompress Zstandard data, the.zstformatzstdcompress bytes into Zstandard, the.zstformat, at its fastest levelbunzip2uncompress bzip2 data, the.bz2formatbzip2compress bytes into bzip2, the.bz2formatpng_chunksthe chunks of a PNG file as a table of offset, type, length, data, crc and crc_okexifthe EXIF metadata of a JPEG, PNG or TIFF file as a map, like the camera, the time a photo was taken and wherepcapthe packets in a pcap or pcapng capture, as Wireshark and tcpdump save them, as a table of time, src, dst, proto, sport, dport, len and payloadstreamsthe TCP conversations in a table from pcap, each connection’s payloads joined in the order they were captured, as src, dst, packets and dataflask_sessiondecode a Flask session cookie into {payload, time, signature}, without checking the signaturephp_unserializeread PHP’s serialize() format into zil valuespickle_dislist the opcodes of a Python pickle, like pickletools.dis, as a table of offset, op and argentropy_mapthe entropy of each block of bytes, 256 by default, to spot compressed or encrypted regions in a fileunzipthe files in a zip archive as a table of name, size, data, crc, encrypted and commentzip_infothe files listed in a zip archive, without unpacking them: name, size, packed, method, crc, encrypted and commentuntarthe files in a tar archive as a table of name, size and dataun7zthe files in a 7z archive as a table of name, size, data and crcinfo_7zthe files listed in a 7z archive, without unpacking them: name, size and crcsevenzpack a table of name and data into a 7z archive, compressed with LZMA2carvefind the files embedded in bytes, like binwalk, by searching for every signature magic() knows
Functions
Section titled “Functions”magic(file: str|bytes) -> map|nil
what kind of file this is, from the signature bytes it starts with, like the file command
Takes a path or bytes, and returns {name, mime, ext}, or nil when no signature matches. Only the contents count, never the file’s name
1magic(b"\x89PNG\r\n\x1a\n"){name: "PNG image", mime: "image/png", ext: "png"}2magic(b"PK\x03\x04").ext"zip"3magic(b"hello")nil4magic("download.bin")decompress
Section titled “decompress”decompress(b: bytes) -> any
uncompress bytes in any format this module knows (gzip, zlib, xz, zstd, bzip2, or raw deflate), picked by its signature
1decompress(gzip(b"hi"))b"hi"2decompress(zlib(b"hi"))b"hi"See also magic, gunzip, unzlib, inflate
inflate
Section titled “inflate”inflate(b: bytes) -> any
uncompress raw deflate data, the format inside zip files, gzip and PNG images
1inflate(deflate(b"hello"))b"hello"See also deflate, decompress
deflate
Section titled “deflate”deflate(b: bytes) -> any
compress bytes with raw deflate, no header or checksum
1deflate(b"aaaaaaaaaa")b"KL\x84\x01\0"gunzip
Section titled “gunzip”gunzip(b: bytes) -> any
uncompress gzip data, the .gz format, checking its CRC-32
1gunzip(gzip(b"hello"))b"hello"See also gzip, decompress
gzip(b: bytes) -> any
compress bytes into gzip, the .gz format
1gzip(b"hello").len25See also gunzip, deflate, zlib
unzlib
Section titled “unzlib”unzlib(b: bytes) -> any
uncompress zlib data, deflate with a 2-byte header and an Adler-32 checksum, as in PDFs and PNG chunks
1unzlib(zlib(b"hello"))b"hello"See also zlib, decompress
zlib(b: bytes) -> any
compress bytes into zlib, deflate with a 2-byte header and an Adler-32 checksum
1zlib(b"hello")b"x\x9c\xcbH\xcd\xc9\xc9\x07\0\x06,\x02\x15"See also unzlib, deflate, gzip
unxz(b: bytes) -> any
uncompress xz data, the .xz format
1unxz(xz(b"hello"))b"hello"See also xz, decompress
xz(b: bytes) -> any
compress bytes into xz, the .xz format
The output is valid xz that any tool reads, but zil’s encoder doesn’t look for repeats, so it barely shrinks anything
1xz(b"hello").len56unzstd
Section titled “unzstd”unzstd(b: bytes) -> any
uncompress Zstandard data, the .zst format
1unzstd(zstd(b"hello"))b"hello"See also zstd, decompress
zstd(b: bytes) -> any
compress bytes into Zstandard, the .zst format, at its fastest level
1zstd(b"a" * 100).len14bunzip2
Section titled “bunzip2”bunzip2(b: bytes) -> any
uncompress bzip2 data, the .bz2 format
1bunzip2(bzip2(b"hello"))b"hello"See also bzip2, decompress
bzip2(b: bytes) -> any
compress bytes into bzip2, the .bz2 format
1bzip2(b"a" * 100).len37png_chunks
Section titled “png_chunks”png_chunks(file: str|bytes) -> table
the chunks of a PNG file as a table of offset, type, length, data, crc and crc_ok
Takes a path or bytes. Text chunks (tEXt, zTXt, iTXt) often hide messages, and a crc_ok of false means a chunk was edited by hand, often its width or height in IHDR
1png_chunks(b"\x89PNG\r\n\x1a\n\x00\x00\x00\x04tEXthi!!\xce\xe6\xfc\xb6\x00\x00\x00\x00IEND\xaeB`\x82").data[b"hi!!", b""]2png_chunks("image.png").typeexif(file: str|bytes) -> map
the EXIF metadata of a JPEG, PNG or TIFF file as a map, like the camera, the time a photo was taken and where
Takes a path or bytes. Keys are the standard EXIF tag names, like Make, Model and DateTimeOriginal, or the tag number in hex when zil doesn’t know it. When the photo has a GPS position, lat and lon give it in degrees
1exif("photo.jpg")2exif("photo.jpg").latSee also png_chunks, magic
pcap(file: str|bytes) -> table
the packets in a pcap or pcapng capture, as Wireshark and tcpdump save them, as a table of time, src, dst, proto, sport, dport, len and payload
Takes a path or bytes. Ethernet, raw IP and Linux “any” captures are read through IPv4 or IPv6 down to TCP and UDP, whose payload is what the packet carried. ARP and other frames keep their whole contents as payload. Packets aren’t reassembled, so use streams for whole TCP conversations
1pcap("capture.pcap")2pcap("capture.pcap").filter(|p| p.dport == 53)streams
Section titled “streams”streams(t: table) -> table
the TCP conversations in a table from pcap, each connection’s payloads joined in the order they were captured, as src, dst, packets and data
Both directions go into data, so a request is followed by its reply. Retransmitted or out-of-order packets aren’t fixed up
1pcap("capture.pcap").streams2pcap("capture.pcap").streams[0].data.strSee also pcap
flask_session
Section titled “flask_session”flask_session(cookie: str) -> map
decode a Flask session cookie into {payload, time, signature}, without checking the signature
Flask signs its cookies but doesn’t encrypt them, so anyone holding one can read what’s inside. A cookie starting with . is zlib-compressed, which this undoes. Values Flask tags, like tuples or bytes, stay as the tagged maps it stores
1flask_session("eyJ1c2VyIjoiYWRhIn0.ZQ8Mpw.c2ln").payload{user: "ada"}See also jwt, php_unserialize
php_unserialize
Section titled “php_unserialize”php_unserialize(s: str|bytes) -> any
read PHP’s serialize() format into zil values
Arrays with keys 0, 1, 2… become lists and other arrays maps. An object becomes a map of its properties with its class under “__class”, and the \0 markers on private and protected property names are dropped. Nothing is run or constructed
1php_unserialize(b"a:2:{i:0;s:3:\"ada\";i:1;b:1;}")["ada", true]2php_unserialize(b"O:4:\"User\":1:{s:4:\"name\";s:3:\"ada\";}"){__class: "User", name: "ada"}See also flask_session, pickle_dis
pickle_dis
Section titled “pickle_dis”pickle_dis(b: bytes) -> table
list the opcodes of a Python pickle, like pickletools.dis, as a table of offset, op and arg
Loading a pickle can run any code, which is how pickle exploits work: watch for GLOBAL or STACK_GLOBAL naming something like os system, followed by REDUCE. This only reads the opcodes, so it’s safe on any input
1pickle_dis(b"\x80\x04K*.")table([{offset: 0x0, op: "PROTO", arg: 4}, {offset: 0x2, op: "BININT1", arg: 42}, {offset: 0x4, op: "STOP", arg: nil}])2pickle_dis(b"cos\nsystem\n(S'id'\ntR.").arg["os system", nil, "'id'", nil, nil, nil]See also php_unserialize, flask_session
entropy_map
Section titled “entropy_map”entropy_map(b: bytes, block?: int) -> list
the entropy of each block of bytes, 256 by default, to spot compressed or encrypted regions in a file
Each value goes from 0 bits per byte, every byte the same, to 8 for random data. Plain text sits around 4 to 5, and compressed or encrypted data near 8
1entropy_map(b"\x00" * 512 + gzip(b"hi"), 256)[0, 0, 2.94946]unzip(b: bytes) -> table
the files in a zip archive as a table of name, size, data, crc, encrypted and comment
Stored and deflated entries are unpacked, and data is nil for encrypted ones. Folders are rows with a name ending in /
1unzip(b"PK\x05\x06" + b"\x00" * 18)table([])See also zip_info, untar, un7z, decompress
zip_info
Section titled “zip_info”zip_info(b: bytes) -> table
the files listed in a zip archive, without unpacking them: name, size, packed, method, crc, encrypted and comment
1zip_info(b"PK\x05\x06" + b"\x00" * 18).len0See also unzip
untar(b: bytes) -> table
the files in a tar archive as a table of name, size and data
Only regular files are listed. A .tar.gz needs gunzip first
1untar(b"\x00" * 1024).len0un7z(b: bytes, password?: str) -> table
the files in a 7z archive as a table of name, size, data and crc
Folders are rows with a name ending in /. For an encrypted archive, pass its password, and a missing or wrong one is an error
1un7z(sevenz(table([{name: "a.txt", data: b"hi"}]))).data[b"hi"]See also info_7z, sevenz, unzip, decompress
info_7z
Section titled “info_7z”info_7z(b: bytes, password?: str) -> table
the files listed in a 7z archive, without unpacking them: name, size and crc
Takes the password when the archive’s file names are encrypted
1info_7z(sevenz(table([{name: "a.txt", data: b"hi"}]))).size[2]See also un7z
sevenz
Section titled “sevenz”sevenz(files: table, password?: str) -> any
pack a table of name and data into a 7z archive, compressed with LZMA2
Takes a table shaped like the one un7z returns, and other columns are ignored. A name ending in / or nil data makes a folder. With a password, the contents and file names are encrypted with AES-256
1sevenz(table([{name: "a.txt", data: b"hi"}])).un7z.name["a.txt"]carve(b: bytes, *, base?: bool|int) -> table
find the files embedded in bytes, like binwalk, by searching for every signature magic() knows
Returns a table of offset, name, ext, size and data. size is the length of the embedded file when its format records where it ends, else nil, and data then runs to the next hit. Short signatures are skipped, since they match by chance. With base, offsets start at that address instead of 0, and base: true means 0x400000. Save the pieces with carve(b).each(|r| r.data.write_file("out/{r.offset}.{r.ext}"))
basethe address the first byte is at, true for 0x400000
1carve(b"junk" + gzip(b"hi") + b"\x89PNG\r\n\x1a\n")table([{offset: 0x4, name: "gzip data", ext: "gz", size: nil, data: b"\x1f\x8b\x08\0\0\0\0\0\0\xff\xcb\xc8\x04\0\xac*\x93\xd8\x02\0\0\0"}, {offset: 0x1a, name: "PNG image", ext: "png", size: nil, data: b"\x89PNG\r\n\x1a\n"}])See also magic, entropy_map, decompress
More examples
Section titled “More examples”forensics
Section titled “forensics”1# what kind of file is this?2magic(b"\x89PNG\r\n\x1a\n"){name: "PNG image", mime: "image/png", ext: "png"}3# compress and back4gzip(b"hello hello hello").decompressb"hello hello hello"5# files hidden in a blob6carve(b"junk" + gzip(b"hi")).name["gzip data"]Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.