Question

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?

Was it helpful?

Solution

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'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top