Skip to content

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

  • magic what kind of file this is, from the signature bytes it starts with, like the file command
  • decompress uncompress bytes in any format this module knows (gzip, zlib, xz, zstd, bzip2, or raw deflate), picked by its signature
  • inflate uncompress raw deflate data, the format inside zip files, gzip and PNG images
  • deflate compress bytes with raw deflate, no header or checksum
  • gunzip uncompress gzip data, the .gz format, checking its CRC-32
  • gzip compress bytes into gzip, the .gz format
  • unzlib uncompress zlib data, deflate with a 2-byte header and an Adler-32 checksum, as in PDFs and PNG chunks
  • zlib compress bytes into zlib, deflate with a 2-byte header and an Adler-32 checksum
  • unxz uncompress xz data, the .xz format
  • xz compress bytes into xz, the .xz format
  • unzstd uncompress Zstandard data, the .zst format
  • zstd compress bytes into Zstandard, the .zst format, at its fastest level
  • bunzip2 uncompress bzip2 data, the .bz2 format
  • bzip2 compress bytes into bzip2, the .bz2 format
  • png_chunks the chunks of a PNG file as a table of offset, type, length, data, crc and crc_ok
  • exif the EXIF metadata of a JPEG, PNG or TIFF file as a map, like the camera, the time a photo was taken and where
  • pcap 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
  • streams 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
  • flask_session decode a Flask session cookie into {payload, time, signature}, without checking the signature
  • php_unserialize read PHP’s serialize() format into zil values
  • pickle_dis list the opcodes of a Python pickle, like pickletools.dis, as a table of offset, op and arg
  • entropy_map the entropy of each block of bytes, 256 by default, to spot compressed or encrypted regions in a file
  • unzip the files in a zip archive as a table of name, size, data, crc, encrypted and comment
  • zip_info the files listed in a zip archive, without unpacking them: name, size, packed, method, crc, encrypted and comment
  • untar the files in a tar archive as a table of name, size and data
  • un7z the files in a 7z archive as a table of name, size, data and crc
  • info_7z the files listed in a 7z archive, without unpacking them: name, size and crc
  • sevenz pack a table of name and data into a 7z archive, compressed with LZMA2
  • carve find the files embedded in bytes, like binwalk, by searching for every signature magic() knows
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

1
magic(b"\x89PNG\r\n\x1a\n"){name: "PNG image", mime: "image/png", ext: "png"}
2
magic(b"PK\x03\x04").ext"zip"
3
magic(b"hello")nil
4
magic("download.bin")
Example outputCtrl+Enter to run

See also mime, carve

decompress(b: bytes) -> any

uncompress bytes in any format this module knows (gzip, zlib, xz, zstd, bzip2, or raw deflate), picked by its signature

1
decompress(gzip(b"hi"))b"hi"
2
decompress(zlib(b"hi"))b"hi"
Example outputCtrl+Enter to run

See also magic, gunzip, unzlib, inflate

inflate(b: bytes) -> any

uncompress raw deflate data, the format inside zip files, gzip and PNG images

1
inflate(deflate(b"hello"))b"hello"
Example outputCtrl+Enter to run

See also deflate, decompress

deflate(b: bytes) -> any

compress bytes with raw deflate, no header or checksum

1
deflate(b"aaaaaaaaaa")b"KL\x84\x01\0"
Example outputCtrl+Enter to run

See also inflate, gzip, zlib

gunzip(b: bytes) -> any

uncompress gzip data, the .gz format, checking its CRC-32

1
gunzip(gzip(b"hello"))b"hello"
Example outputCtrl+Enter to run

See also gzip, decompress

gzip(b: bytes) -> any

compress bytes into gzip, the .gz format

1
gzip(b"hello").len25
Example outputCtrl+Enter to run

See also gunzip, deflate, zlib

unzlib(b: bytes) -> any

uncompress zlib data, deflate with a 2-byte header and an Adler-32 checksum, as in PDFs and PNG chunks

1
unzlib(zlib(b"hello"))b"hello"
Example outputCtrl+Enter to run

See also zlib, decompress

zlib(b: bytes) -> any

compress bytes into zlib, deflate with a 2-byte header and an Adler-32 checksum

1
zlib(b"hello")b"x\x9c\xcbH\xcd\xc9\xc9\x07\0\x06,\x02\x15"
Example outputCtrl+Enter to run

See also unzlib, deflate, gzip

unxz(b: bytes) -> any

uncompress xz data, the .xz format

1
unxz(xz(b"hello"))b"hello"
Example outputCtrl+Enter to run

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

1
xz(b"hello").len56
Example outputCtrl+Enter to run

See also unxz, zstd, gzip

unzstd(b: bytes) -> any

uncompress Zstandard data, the .zst format

1
unzstd(zstd(b"hello"))b"hello"
Example outputCtrl+Enter to run

See also zstd, decompress

zstd(b: bytes) -> any

compress bytes into Zstandard, the .zst format, at its fastest level

1
zstd(b"a" * 100).len14
Example outputCtrl+Enter to run

See also unzstd, xz, gzip

bunzip2(b: bytes) -> any

uncompress bzip2 data, the .bz2 format

1
bunzip2(bzip2(b"hello"))b"hello"
Example outputCtrl+Enter to run

See also bzip2, decompress

bzip2(b: bytes) -> any

compress bytes into bzip2, the .bz2 format

1
bzip2(b"a" * 100).len37
Example outputCtrl+Enter to run

See also bunzip2, xz, gzip

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

1
png_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""]
2
png_chunks("image.png").type
Example outputCtrl+Enter to run

See also exif, magic, crc32

exif(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

1
exif("photo.jpg")
2
exif("photo.jpg").lat
Example outputCtrl+Enter to run

See 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

1
pcap("capture.pcap")
2
pcap("capture.pcap").filter(|p| p.dport == 53)
Example outputCtrl+Enter to run

See also streams, carve

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

1
pcap("capture.pcap").streams
2
pcap("capture.pcap").streams[0].data.str
Example outputCtrl+Enter to run

See also pcap

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

1
flask_session("eyJ1c2VyIjoiYWRhIn0.ZQ8Mpw.c2ln").payload{user: "ada"}
Example outputCtrl+Enter to run

See also jwt, 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

1
php_unserialize(b"a:2:{i:0;s:3:\"ada\";i:1;b:1;}")["ada", true]
2
php_unserialize(b"O:4:\"User\":1:{s:4:\"name\";s:3:\"ada\";}"){__class: "User", name: "ada"}
Example outputCtrl+Enter to run

See also flask_session, 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

1
pickle_dis(b"\x80\x04K*.")table([{offset: 0x0, op: "PROTO", arg: 4}, {offset: 0x2, op: "BININT1", arg: 42}, {offset: 0x4, op: "STOP", arg: nil}])
2
pickle_dis(b"cos\nsystem\n(S'id'\ntR.").arg["os system", nil, "'id'", nil, nil, nil]
Example outputCtrl+Enter to run

See also php_unserialize, flask_session

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

1
entropy_map(b"\x00" * 512 + gzip(b"hi"), 256)[0, 0, 2.94946]
Example outputCtrl+Enter to run

See also entropy, carve

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 /

1
unzip(b"PK\x05\x06" + b"\x00" * 18)table([])
Example outputCtrl+Enter to run

See also zip_info, untar, un7z, decompress

zip_info(b: bytes) -> table

the files listed in a zip archive, without unpacking them: name, size, packed, method, crc, encrypted and comment

1
zip_info(b"PK\x05\x06" + b"\x00" * 18).len0
Example outputCtrl+Enter to run

See 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

1
untar(b"\x00" * 1024).len0
Example outputCtrl+Enter to run

See also unzip, un7z, gunzip

un7z(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

1
un7z(sevenz(table([{name: "a.txt", data: b"hi"}]))).data[b"hi"]
Example outputCtrl+Enter to run

See also info_7z, sevenz, unzip, decompress

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

1
info_7z(sevenz(table([{name: "a.txt", data: b"hi"}]))).size[2]
Example outputCtrl+Enter to run

See also un7z

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

1
sevenz(table([{name: "a.txt", data: b"hi"}])).un7z.name["a.txt"]
Example outputCtrl+Enter to run

See also un7z, zlib, gzip

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}"))

  • base the address the first byte is at, true for 0x400000
1
carve(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"}])
Example outputCtrl+Enter to run

See also magic, entropy_map, decompress

1
# what kind of file is this?
2
magic(b"\x89PNG\r\n\x1a\n"){name: "PNG image", mime: "image/png", ext: "png"}
3
# compress and back
4
gzip(b"hello hello hello").decompressb"hello hello hello"
5
# files hidden in a blob
6
carve(b"junk" + gzip(b"hi")).name["gzip data"]
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.