, ,

Unlocking Digital Secrets: How to Ensure Your Files Are What They Claim to Be.

Posted by

File signatures, also known as magic numbers, are unique sets of bytes at the beginning of files that help in identifying the file type regardless of the file’s extension. Validating a file’s signature is crucial for security and data integrity, especially in environments where file content should match its extension.

Here’s a list of some common file signatures with their corresponding extensions, followed by a C# example of how to validate a file’s signature based on its expected type:

Common File Signatures

  1. JPEG Image: .jpeg, .jpgFF D8 FF
  2. PNG Image: .png89 50 4E 47 0D 0A 1A 0A
  3. GIF Image: .gif47 49 46 38
  4. TIFF Image: .tiff49 49 2A 00 or 4D 4D 00 2A
  5. BMP Image: .bmp42 4D
  6. PDF Document: .pdf25 50 44 46
  7. ZIP Archive: .zip50 4B 03 04 or 50 4B 05 06 or 50 4B 07 08
  8. RAR Archive: .rar52 61 72 21 1A 07 00
  9. 7z Archive: .7z37 7A BC AF 27 1C
  10. MS Office/OpenOffice Documents: .docx, .xlsx, .pptx50 4B 03 04
  11. MS Office 97-2003 Document: .doc, .xls, .pptD0 CF 11 E0 A1 B1 1A E1
  12. WAV Audio: .wav52 49 46 46
  13. AVI Video: .avi52 49 46 46
  14. MP3 Audio: .mp3FF FB or 49 44 33
  15. FLV Video: .flv46 4C 56 01
  16. Real Audio: .ram2E 72 61 FD
  17. Real Media: .rm2E 52 4D 46
  18. MPEG Video: .mpeg, .mpg00 00 01 BA or 47 40
  19. QuickTime Video: .mov00 00 00 14 66 74 79 70
  20. MIDI Audio: .midi4D 54 68 64
  21. ISO Image: .iso43 44 30 30 31
  22. Executable File: .exe4D 5A
  23. ELF Executable: .elf7F 45 4C 46
  24. VMDK File: .vmdk4B 44 4D
  25. AIFF Audio: .aiff46 4F 52 4D 00
  26. XML Document: .xml3C 3F 78 6D 6C 20
  27. SQLite Database: .sqlite53 51 4C 69
  28. Photoshop Document: .psd38 42 50 53
  29. MP4 Video: .mp400 00 00 18 66 74 79 70
  30. AutoCAD Drawing: .dwg41 43 31 30

C# Code to Validate File Signature

The following C# function checks the file signature by comparing the beginning bytes of the file to known signatures:

using System;
using System.Collections.Generic;
using System.IO;

public class FileSignatureValidation
{
    private static Dictionary<string, List<byte[]>> fileSignatures = new Dictionary<string, List<byte[]>> {
        { ".jpeg", new List<byte[]> { new byte[] { 0xFF, 0xD8, 0xFF } } },
        { ".png", new List<byte[]> { new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A } } },
        // Add more signatures here
    };

    public static bool ValidateFileSignature(string filePath)
    {
        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                var fileExt

 = Path.GetExtension(filePath).ToLowerInvariant();
                if (fileSignatures.ContainsKey(fileExt))
                {
                    var headerBytes = br.ReadBytes(fileSignatures[fileExt][0].Length);
                    foreach (var sig in fileSignatures[fileExt])
                    {
                        if (headerBytes.SequenceEqual(sig))
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
        }
    }

    public static void Main(string[] args)
    {
        var filePath = @"path\to\your\file.jpg";
        bool isValid = ValidateFileSignature(filePath);
        Console.WriteLine($"File signature validation: {isValid}");
    }
}

This code initializes a dictionary with file signatures and checks if the beginning bytes of a file match any of the expected signatures for that file extension. You would need to expand the fileSignatures dictionary to include more file types and their corresponding signatures.