RingDotNetfor Ring

What kind of file is it?

The name of a file is only a habit. Many formats start with a few fixed bytes, the magic number, and a program that wants to know what a file really is reads the first bytes. This lesson builds a signature table, tells text from binary data, finds names that tell a lie, and shows why an upload check must not trust the name.

What you will learn#

The program#

samples/UsingDotNet/tutorials/ch05_file_types.ring
# finding out what a file is - the first bytes (magic numbers), a signature table, text or binary, and a file whose name tells a lie
load "dotnet.ring"

using {
	"System"
	"System.IO"
	"System.Text"
}

aSignatures = []                  # [ name, mime type, the first bytes ]

func main

	oFile = new File()
	oPath = new Path()
	cFolder = oPath.Combine([ oPath.GetTempPath(), "ringdotnet-types-" + oPath.GetRandomFileName() ])
	new Directory().CreateDirectory(cFolder)

	# The NAME of a file is only a habit: anybody can call a picture "notes.txt". Many file formats start with a few fixed bytes, the
	# MAGIC NUMBER (or signature), and a program that wants to know what a file really is looks at the first bytes.
	addSignatures()

	# 1. Files made by the program: only the first bytes are real, and the rest is filler (that is all the detection looks at)
	makeFile(cFolder + "/picture.png",char(137) + "PNG" + char(13) + char(10) + char(26) + char(10),"filler")
	makeFile(cFolder + "/animation.gif","GIF89a","filler")
	makeFile(cFolder + "/photo.jpg",char(255) + char(216) + char(255) + char(224),"filler")
	makeFile(cFolder + "/archive.zip","PK" + char(3) + char(4),"filler")
	makeFile(cFolder + "/document.pdf","%PDF-1.7","filler")
	makeFile(cFolder + "/program.exe","MZ" + char(144) + char(0),"filler")
	makeFile(cFolder + "/packed.gz",char(31) + char(139) + char(8),"filler")
	oFile.WriteAllText([ :path = cFolder + "/readme.txt", :contents = "Just a text file" + nl ])
	oFile.WriteAllBytes(cFolder + "/data.bin","a" + char(0) + char(1) + char(2) + char(200) + char(201))
	oFile.WriteAllBytes(cFolder + "/empty.dat","")
	? "1. what each file is:"
	aNames = sortNames(cFolder)
	for cName in aNames
		? "   " + padRight(cName,14) + describe(cFolder + "/" + cName)
	next

	# 2. A file with a wrong name: a PNG picture that is called notes.txt, and a text called photo.jpg. The program compares what the
	#    NAME says with what the BYTES say.
	oFile.Move([ :sourceFileName = cFolder + "/picture.png", :destFileName = cFolder + "/notes.txt" ])
	oFile.Move([ :sourceFileName = cFolder + "/readme.txt", :destFileName = cFolder + "/photo2.jpg" ])
	? nl + "2. names that tell a lie:"
	for cName in [ "notes.txt", "photo2.jpg", "photo.jpg" ]
		? "   " + padRight(cName,12) + checkName(cFolder + "/" + cName)
	next

	# 3. Why this is a security matter: a web site that lets users upload "pictures" must not trust the name or the type that the
	#    browser sends, but look at the bytes. (The bytes can lie too: only the first bytes are checked. A real check opens the picture.)
	? nl + "3. an upload check: is the file a PNG, a JPEG or a GIF picture?"
	for cName in [ "notes.txt", "photo.jpg", "animation.gif", "program.exe" ]
		? "   " + padRight(cName,14) + yesNo(isPicture(cFolder + "/" + cName))
	next

	new Directory().Delete([ :path = cFolder, :recursive = True ])

# the known signatures: the bytes at the start of the file
func addSignatures

	aSignatures + [ "PNG picture","image/png",char(137) + "PNG" + char(13) + char(10) + char(26) + char(10) ]
	aSignatures + [ "GIF picture","image/gif","GIF8" ]
	aSignatures + [ "JPEG picture","image/jpeg",char(255) + char(216) + char(255) ]
	aSignatures + [ "ZIP archive","application/zip","PK" + char(3) + char(4) ]
	aSignatures + [ "GZip data","application/gzip",char(31) + char(139) ]
	aSignatures + [ "PDF document","application/pdf","%PDF-" ]
	aSignatures + [ "Windows program","application/x-msdownload","MZ" ]

func makeFile cFile,cStart,cFill

	new File().WriteAllBytes(cFile,cStart + copy(cFill,20))

# the first nCount bytes of a file (fewer when the file is shorter), read into a byte buffer
func firstBytes cFile,nCount

	oStream = (new FileStream).@New([ :path = cFile, :mode = "Open", :access = "Read" ])
	oBuffer = dnBuffer(nCount)
	nRead = oStream.Read(oBuffer,0,nCount)
	oStream.Dispose()
	return dnBufferBytes(oBuffer,nRead)

# [name, mime type] of a file, from its first bytes
func detect cFile

	cStart = firstBytes(cFile,16)
	if len(cStart) = 0
		return [ "empty file","" ]
	ok
	for aSignature in aSignatures
		if left(cStart,len(aSignature[3])) = aSignature[3]
			return [ aSignature[1],aSignature[2] ]
		ok
	next
	if looksLikeText(cStart)
		return [ "text","text/plain" ]
	ok
	return [ "unknown binary data","application/octet-stream" ]

# text has no NUL and no control characters except tab, new line and return, and it is valid UTF-8
func looksLikeText cBytes

	for i = 1 to len(cBytes)
		nCode = ascii(substr(cBytes,i,1))
		if nCode = 0 or (nCode < 32 and nCode != 9 and nCode != 10 and nCode != 13)
			return False
		ok
	next
	oStrict = (new UTF8Encoding).@New([ :encoderShouldEmitUTF8Identifier = False, :throwOnInvalidBytes = True ])
	try
		oStrict.GetString(cBytes)
		return True
	catch
		return False
	done

func describe cFile

	aType = detect(cFile)
	if aType[2] = ""
		return aType[1]
	ok
	return padRight(aType[1],22) + aType[2]

# what the name says (the extension) and what the bytes say
func checkName cFile

	cExtension = lower(new Path().GetExtension(cFile))
	aType = detect(cFile)
	aExpected = [ [".png","PNG picture"], [".gif","GIF picture"], [".jpg","JPEG picture"], [".jpeg","JPEG picture"], [".zip","ZIP archive"], [".pdf","PDF document"], [".gz","GZip data"], [".txt","text"] ]
	for aItem in aExpected
		if aItem[1] = cExtension
			if aItem[2] = aType[1]
				return "the name fits the content (" + aType[1] + ")"
			ok
			return "THE NAME SAYS " + aItem[2] + " BUT THE BYTES SAY " + aType[1]
		ok
	next
	return "unknown extension, the bytes say " + aType[1]

func isPicture cFile

	cMime = detect(cFile)[2]
	return cMime = "image/png" or cMime = "image/jpeg" or cMime = "image/gif"

func sortNames cFolder

	aList = []
	aItems = new DirectoryInfo(cFolder).GetFiles(NULL).toList()
	for oItem in aItems
		aList + oItem.Name
	next
	return sort(aList)

func yesNo lValue

	if lValue
		return "yes"
	ok
	return "no"

func padRight cText,nWidth

	return cText + copy(" ",nWidth - len(cText))
Output
1. what each file is:
   animation.gif GIF picture           image/gif
   archive.zip   ZIP archive           application/zip
   data.bin      unknown binary data   application/octet-stream
   document.pdf  PDF document          application/pdf
   empty.dat     empty file
   packed.gz     GZip data             application/gzip
   photo.jpg     JPEG picture          image/jpeg
   picture.png   PNG picture           image/png
   program.exe   Windows program       application/x-msdownload
   readme.txt    text                  text/plain

2. names that tell a lie:
   notes.txt   THE NAME SAYS text BUT THE BYTES SAY PNG picture
   photo2.jpg  THE NAME SAYS JPEG picture BUT THE BYTES SAY text
   photo.jpg   the name fits the content (JPEG picture)

3. an upload check: is the file a PNG, a JPEG or a GIF picture?
   notes.txt     yes
   photo.jpg     yes
   animation.gif yes
   program.exe   no
Note

This program uses a few generic functions (dnNew, dnBuffer) where a class cannot be used. The lesson The generic functions explains them.

How it works#

The first bytes#

A PNG picture always starts with the bytes 137 80 78 71 13 10 26 10 (the second to the fourth are the letters PNG), a GIF with GIF8, a JPEG with 255 216 255, a ZIP file with PK and the codes 3 and 4, a GZip file with 31 139, a PDF with %PDF- and a Windows program with MZ. firstBytes opens the file with a FileStream, reads up to 16 bytes into a byte buffer and returns them as a Ring text; it does not read the rest of the file, so it is fast for any size. A file that is shorter than 16 bytes gives fewer bytes, and an empty file gives none.

The table#

aSignatures is a Ring list of [name, mime type, start bytes]. detect compares the start of the file with each entry (left(cStart, len(signature)) = signature) and returns the first match. The table is data: a new format is a new line. The MIME types are the ones of the tutorial MIME types, and a web server could send them in the Content-Type header.

Text or binary#

A file that matches no signature is either text or unknown binary data. The check is: no NUL byte, no control character except tab and the line ends, and valid UTF-8 (the strict decoder of the tutorial Text files and encodings). The program looks only at the first bytes, so a file that is text at the start and binary later is called text; for a real decision look at more bytes.

The name tells a lie#

The extension is just part of the name. After the program renamed the PNG to notes.txt and the text to photo2.jpg, checkName compares the type that the extension promises with the type that the bytes show, and says THE NAME SAYS text BUT THE BYTES SAY PNG picture. The same file with the right name says that the name fits.

Why this is a security matter#

A web site that lets users upload pictures must not trust the file name or the type that the browser sends, because the sender chooses both. isPicture looks at the bytes: notes.txt is accepted as a picture (it is one), program.exe is refused although a user could have named it photo.png. This is only the first check: somebody can put a PNG header in front of other data, so a real service opens the picture with an image library and refuses what it cannot read, and it stores the uploaded file under a name that it makes itself, never under the sender's name or in a folder where files are run.

Note

The program writes only the first bytes of each sample file (and filler after them), which is all that the detection reads. Real files of these formats have more structure after the signature.

What you learned#

Exercises#

  1. Add the signatures of BMP (BM), RAR and 7z (look up their first bytes).
  2. Make detectAll(folder) that prints the type of every file of a folder and lists the files whose name does not fit.
  3. Detect a text file with a byte order mark and tell UTF-8 from UTF-16 (the tutorial Text files and encodings).
  4. Read the first 4 bytes of a ZIP file made by ZipFile.CreateFromDirectory and check PK.

Next#

Compression

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.