Everyday techExhibit 19

What does a file extension actually do?

A file extension is a label in the file's name. It tells the system which program to start, and nothing inside the file depends on it. The real type is usually written in the file's first bytes. We renamed a picture from .png to .txt: not one byte changed, two of six readers believed the name, and four read the bytes and still saw a picture.

Try it: what do the first bytes say?

Choose any file, or try one of ours. The page reads the first bytes in your browser and compares them with what the name claims. Nothing is uploaded: the file never leaves your device.

or drop one anywhere on this box
Files from our video (first 16 bytes of each)

The page knows about 25 common signatures; thousands exist, so an unknown result only means it is not in our short list. First bytes can be faked too, so a matching result does not prove a file is safe.

The video for this exhibit comes out on YouTube on Friday 15 January. Subscribe so you don’t miss it

A label on the box

The extension is part of the file's name, not its contents. When you double-click a file, Windows reads the extension, looks it up in a list of file associations, and starts the program registered for it. The three-letter habit comes from 1970s systems such as CP/M, where a name was up to eight characters, a dot, and up to three more. MS-DOS copied that, and Windows inherited it.

The first bytes

Most formats announce themselves at the very start of the file; plain text is the main exception, because it has no signature at all. Every PNG begins with the same eight bytes, 89 50 4E 47 0D 0A 1A 0A; three of them are the letters P, N, G. A JPEG begins with FF D8, and a PDF with %PDF. Programmers call these magic numbers. Unix has had a command for this, simply called file, since 1973.

What we measured

We renamed photo.png to photo.txt. Our picture is a 640 by 480 PNG drawn by the test script. Its size stayed at 7,253 bytes and its SHA-256 fingerprint did not change. Then we ran six tests with five programs. Two went by the name and said text: Python's type guesser (the kind of lookup web servers use) and the file lookup of the GNOME desktop on Linux (GLib). Four read the bytes and found a 640 by 480 PNG: the file command, the Pillow image library, and the Chromium browser twice, once opening the file directly even though the server had labelled it text, and once inside an image tag. In the reverse test, a plain text file named .png was refused by the image library. This was one picture on one Linux machine with one browser, Chromium 157; other systems, browsers and server settings can behave differently.

A Word file is a ZIP

Our Word document and our ZIP archive start with the same bytes, 50 4B 03 04, the letters P and K. A .docx file is a ZIP archive of XML files. We copied ours, named the copy .zip and opened it: 17 files, 16 of them XML, and word/document.xml held our text. PK are the initials of Phil Katz, who created the ZIP format in 1989. Modern Excel and PowerPoint files are built the same way. Our document was made by a script, so 17 is the count for our file, not for every .docx.

When the label lies

By default, Windows hides the extension of file types it knows. A file named invoice.pdf.exe can then appear as invoice.pdf: it looks like a document and is a program. MITRE's ATT&CK knowledge base lists this double extension as a known technique. One setting helps: in File Explorer, turn on file name extensions, then read a name all the way to the last dot. MITRE lists user training as the other defence.

Frequently asked questions

Does renaming a file change its type?

No. Renaming changes only the name. In our test a picture renamed from .png to .txt kept the same size and the same SHA-256 fingerprint. To really change the type you have to convert the file with a program that rewrites its contents.

How does a computer know what type a file really is?

Many file formats begin with fixed bytes, often called magic numbers. A PNG starts with eight fixed bytes that include the letters PNG, a JPEG starts with FF D8, and a PDF starts with %PDF. Programs that open files usually check these bytes.

Is a .docx file really a ZIP?

Yes. Modern Word, Excel and PowerPoint files (.docx, .xlsx, .pptx) are ZIP archives of XML files. Our test document, copied and renamed to .zip, opened as an archive with 17 files inside.

Why should I show file extensions in Windows?

By default Windows hides the extension of file types it knows, so a program named invoice.pdf.exe can appear as invoice.pdf. With extensions shown you can read the name to the last dot and see that it is a program. It helps, but it is not a complete defence: Windows still hides the extension of shortcuts (.lnk).

Sources

Last updated

Keep exploring