Adding magic to Python for file type recognition

Source: Internet
Author: User

Recently want to see how the Cuckoo file recognition function, turned the cuckoo source, found its file format judgment code as follows:

def_get_filetype (self, data):"""Gets filetype, uses libmagic if available.        @param data:data to be analyzed.        @return: File type or None. """        if  nothave_magic:returnNoneTry: Ms=Magic.open (Magic. Magic_none) ms.load () File_type=ms.buffer (data)except:            Try: File_type=magic.from_buffer (data)exceptException:returnNonefinally:            Try: Ms.close ()except:                Pass        returnFile_type

The magic,libmagic in the Libmagic Library is a development library that identifies file types based on the file header, which Python can use to make it easy to determine the file format. Record the installation process. Installation environment: WinXP + Python 2.7

Install the Magic module:
1, installation pycparser-2.14 link: https://pypi.python.org/pypi/pycparser
2, installation Vcforpython, Link: http://aka.ms/vcpython27
3, install Cffi module, Link: https://pypi.python.org/pypi/cffi/#downloads
4, installation Libmagic link: https://pypi.python.org/pypi/python-libmagic
5. Install file, add to environment variable path after installation:. \gnuwin32\bin
6. Install Magic Module Link: https://github.com/ahupp/python-magic
Test Import Magic successfully
More details see Https://github.com/ahupp/python-magic

Procedures for sorting samples using Kaspersky's scan results (including file type recognition) See: Https://github.com/Viwilla/ClassifySamples

Adding magic to Python for file type recognition

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.