Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A solution to validate a file type using magic number in file signature (.NET)?

I want to validate a type of an uploaded file (suppose the file has incorrect extension).

I figure it could be done by checking the magic number of file signature. Similarly, as it is done in unix. So I guess I need to read the first bytes of the file and compare them with the magic number mappings (and I need to have the mappings and I need to maintain them).

So I thought maybe there is a class out there just for this purpose? Or maybe someone has done this and can share their insights on the solution? Thanks

like image 731
Alvis Avatar asked Oct 20 '25 09:10

Alvis


2 Answers

I doubt there's a “silver bullet” solution for this problem. Nobody says every file format has any form of a magic number. Consider plain text files — no reasonable indication at all (beside a possible byte-order-mark at the beginning which, however, need not mean anything) and pure heuristics must be used to guess it's a plain text file. Like wise to recognize an XML or HTML (i.e. SGML) file it means to check a lot of possibilities.

The approach you should follow is:

  • Narrow down the set of expected file formats.
  • Either do-it-yourself based on the knowledge of the overall structure and/or characteristics of the file formats you need to support or look for a solution like GNU File mentioned by rerun. You can also have a look on sites like MagicDB — however, this one is particularly outdated (year 2005).
  • Specify behavior for unexpected file formats.

In case the set is narrow “enough” I'd consider a simple custom solution as the right one.

like image 94
Ondrej Tucny Avatar answered Oct 22 '25 02:10

Ondrej Tucny


i don't know of a .net option but you can see if file for windows from gnu solves your problem.

like image 23
rerun Avatar answered Oct 22 '25 01:10

rerun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!