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

StackOverflow https://stackoverflow.com/questions/4874333

  •  28-10-2019
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top