How can I find the mimetype of a file without extension that is sent to me inside a zip file?

1.3k views Asked by At

I have a simple problem: in a system I'm developing the user can send us zipfiles and I need to filter the content of it. (block applications and malicious scripts)

To block the inner files by extension is easy, but files without extension are very common and the extension isn't the most reliable source about the content of the file.

I've already tried to use python magic, but it requires some packages that my server doesn't support and the server isn't going to help me. Oh! I don't have the option of changing the system to another server. So, there's no python magic for me in this case.

Does anyone have an idea of how to check the file type by its header?

1

There are 1 answers

6
Mr Shark On BEST ANSWER

Not a direct answer, but the format of /etc/magic is not that complicated, so if its only a few filetypes you need to detect, perhaps its easiest to write your own detection routine.

# Java

0       beshort         0xcafe
>2      beshort         0xbabe          application/x-java-applet

we get:

data = open(path).read()
if data[0:4] == '\xca\xfe\xba\xbe':
    minetype = 'application/x-java-applet'