Commit 427daf6f by dorp

inital commit

parents
.venv
.pyc
__pycache__/
.idea/
.cache/
.pytest_cache/
from .type import get_file_type_from_path, get_file_type_from_binary
name = "fact_helper_file"
__all__ = [
'get_file_type_from_path',
'get_file_type_from_binary'
]
# ====================== archives ======================
# cpio archives
# original code from file repository
0 short 070707 cpio archive
!:mime application/x-cpio
0 short 0143561 byte-swapped cpio archive
!:mime application/x-cpio # encoding: swapped
0 string 070707 ASCII cpio archive (pre-SVR4 or odc)
!:mime application/x-cpio
0 string 070701 ASCII cpio archive (SVR4 with no CRC)
!:mime application/x-cpio
0 string 070702 ASCII cpio archive (SVR4 with CRC)
!:mime application/x-cpio
# ====================== bootloader ======================
# ---- uImage file ----
# original code from binwalk
# From: Craig Heffner, U-Boot image.h header definitions file
0 belong 0x27051956 uImage header, header size: 64 bytes,
!:mime firmware/u-boot
>4 ubelong x header CRC: 0x%X,
>8 bedate x created: %s,
>12 belong <1 {invalid}
>12 ubelong x image size: %d bytes,
>16 ubelong x Data Address: 0x%X,
>20 ubelong x Entry Point: 0x%X,
>24 ubelong x data CRC: 0x%X,
>28 byte 0 OS: {invalid}invalid OS,
>28 byte 1 OS: OpenBSD,
>28 byte 2 OS: NetBSD,
>28 byte 3 OS: FreeBSD,
>28 byte 4 OS: 4.4BSD,
>28 byte 5 OS: Linux,
>28 byte 6 OS: SVR4,
>28 byte 7 OS: Esix,
>28 byte 8 OS: Solaris,
>28 byte 9 OS: Irix,
>28 byte 10 OS: SCO,
>28 byte 11 OS: Dell,
>28 byte 12 OS: NCR,
>28 byte 13 OS: LynxOS,
>28 byte 14 OS: VxWorks,
>28 byte 15 OS: pSOS,
>28 byte 16 OS: QNX,
>28 byte 17 OS: Firmware,
>28 byte 18 OS: RTEMS,
>28 byte 19 OS: ARTOS,
>28 byte 20 OS: Unity OS,
>29 byte 0 CPU: {invalid}invalid CPU,
>29 byte 1 CPU: Alpha,
>29 byte 2 CPU: ARM,
>29 byte 3 CPU: Intel x86,
>29 byte 4 CPU: IA64,
>29 byte 5 CPU: MIPS,
>29 byte 6 CPU: MIPS 64 bit,
>29 byte 7 CPU: PowerPC,
>29 byte 8 CPU: IBM S390,
>29 byte 9 CPU: SuperH,
>29 byte 10 CPU: Sparc,
>29 byte 11 CPU: Sparc 64 bit,
>29 byte 12 CPU: M68K,
>29 byte 13 CPU: Nios-32,
>29 byte 14 CPU: MicroBlaze,
>29 byte 15 CPU: Nios-II,
>29 byte 16 CPU: Blackfin,
>29 byte 17 CPU: AVR,
>29 byte 18 CPU: STMicroelectronics ST200,
#>30 byte x image type: %d,
>30 byte 0 image type: {invalid} Image,
>30 byte 1 image type: Standalone Program,
>30 byte 2 image type: OS Kernel Image,
>30 byte 3 image type: RAMDisk Image,
>30 byte 4 image type: Multi-File Image,
>30 byte 5 image type: Firmware Image,
>30 byte 6 image type: Script file,
>30 byte 7 image type: Filesystem Image,
>30 byte 8 image type: Binary Flat Device Tree Blob
>31 byte 0 compression type: none,
>31 byte 1 compression type: gzip,
>31 byte 2 compression type: bzip2,
>31 byte 3 compression type: lzma,
>32 string x image name: "%s"
# U-Boot boot loader
# original code from binwalk
0 string U-Boot\x20 U-Boot version string,
!:mime bootloader/u-boot
>7 byte <48 {invalid}
>7 byte >57 {invalid}
>8 byte !0x2E {invalid}
>0 string x "%s"
# CFE bootloader
# original code from binwalk
0 string CFE1 CFE boot loader
!:mime bootloader/cfe
>4 string !CFE1 {invalid}
>40 string CFE1CFE1 {invalid}
# ====================== compressed streams ======================
# Type: LZMA
0 lelong&0xffffff =0x5d
>12 leshort 0xff LZMA compressed data,
!:mime application/x-lzma
>>5 lequad =0xffffffffffffffff streamed
>>5 lequad !0xffffffffffffffff non-streamed, size %lld
>12 leshort 0 LZMA compressed data,
!:mime application/x-lzma
>>5 lequad =0xffffffffffffffff streamed
>>5 lequad !0xffffffffffffffff non-streamed, size %lld
# HP_LZMA
0 string \x39\x00\x00\x00\x02 LZMA compressed data,
!:mime application/x-lzma
>5 lequad =0xffffffffffffffff streamed
>5 lequad !0xffffffffffffffff non-streamed, size %lld
# Zlib - Original Code from binwalk
0 beshort 0x789c Zlib compressed data, default compression
!:mime compression/zlib
0 beshort 0x78da Zlib compressed data, best compression
!:mime compression/zlib
0 beshort 0x785e Zlib compressed data, compressed
!:mime compression/zlib
#DJI drone image signature by Fraunhofer FKIE
0 string xV4\x12 DJI Drone Image;
!:mime firmware/dji-drone
>0xc string x Vendor: %s;
>0x1c string x Device-ID: %s;
>0x2c leshort x Number of Modules: %d
#Ambarella image signature by Fraunhofer FKIE
0xa4 string \x90\xEB\x24\xA3\x00\x00 Ambarella Image;
!:mime firmware/ambarella
>0 string x Device-ID: %s;
#Ambarella Rom FS signature by Fraunhofer FKIE
0x04 string \x8A\x32\xFC\x66 Ambarella RomFS;
!:mime filesystem/ambarella-romfs
# ====================== faf internal ======================
# ---- faf internal link representation ----
0 string symbolic\ link\ -> symbolic link
>17 string x to '%s'
!:mime inode/symlink
This source diff could not be displayed because it is too large. You can view the blob instead.
# HP LaserJet 1000 series downloadable firmware file
0 string \xbe\xefABCDEFGH HP LaserJet 1000 series downloadable firmware
# HP Printer Job Language (original from file magic db)
# From: Uwe Bonnes <bon@elektron.ikp.physik.th-darmstadt.de>
#
0 string \033%-12345X@PJL HP Printer Job Language data
!:mime firmware/hp-pjl
>&0 string >\0 %s
>>&0 string >\0 %s
>>>&0 string >\0 %s
>>>>&0 string >\0 %s
#>15 string \ ENTER\ LANGUAGE\ =
#>31 string PostScript PostScript
# HP Update Streams
3 string \x00\x00\x11\x00\x00\x00\x00\x00\x00\xbe\xac HP Update Stream 1
!:mime firmware/hp-us
3 string \x00\x00\x10\x00\x00 HP Update Stream 2
!:mime firmware/hp-us
# DSK 1.0
# Reversed by Fraunhofer FKIE
0 string \x1bDSK1.0 DSK1.0 Image
!:mime firmware/dsk1.0
>0x0C ulelong x \b, payload size: %d bytes
# Extended DSK 1.0
0x0C string \x1bDSK1.0 Extended DSK1.0 Image
!:mime firmware/dsk1.0-extended
>0x18 ulelong x \b, payload size: %d bytes
# PostScript
# Reversed by Fraunhofer FKIE
0 string %!PS Postscript
!:mime text/postscript
>4 string -Adobe Adobe
# RAW data from flash
8 string \xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff RAW
!:strength / 254
!:mime data/raw
# ====================== UEFI ======================
# ASUS CAP
0 string \x8b\xa6\x3c\x4a\x23\x77\xfb\x48\x80 ASUS UEFI Image
!:mime firmware/uefi
# Gigabyte UEFI
0x20000 string \xaa\x55\xaa\x55\x00\x00 GigaByte UEFI Image
!:mime firmware/uefi
# Dell UEFI HDR
0 string PFS\x2eHDR Dell HDR-PFS UEFI Image
!:mime firmware/uefi
0 string \x24TAG\x01\x00\x00\x00\x5D\xc2\x8e\x14 Dell HDR-TAG UEFI Image
!:mime firmware/uefi
# Dell UEFI k
2 string \x6b\x17\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x78\xe5\x8c\x8c\x3d\x8a\x1c Dell k UEFI Image
!:mime firmware/uefi
# Flash descriptors for Intel SPI flash roms.
# original code from file magic db
# From Dr. Jesus <j@hug.gs>
0 lelong 0x0ff0a55a Intel serial flash for ICH/PCH ROM <= 5 or 3400 series A-step
!:mime firmware/uefi
16 lelong 0x0ff0a55a Intel serial flash for PCH ROM
!:mime firmware/uefi
import logging
from pathlib import Path
from typing import Union
import magic
def get_file_type_from_path(file_path: Union[str, Path]) -> dict:
'''
Generate a dict containing full and mime file type from file path.
First it tries to use a custom magic file, then defaults to system magic.
:param binary: bytes
:return: dict
'''
path_string = file_path if isinstance(file_path, str) else str(file_path)
return _get_file_type(path_string, 'from_file')
def get_file_type_from_binary(binary: bytes) -> dict:
'''
Generate a dict containing full and mime file type from bytes object
First it tries to use a custom magic file, then defaults to system magic.
:param binary: bytes
:return: dict
'''
return _get_file_type(binary, 'from_buffer')
def _get_file_type(path_or_binary, function_name):
magic_path = str(Path(__file__).parent / 'bin' / 'custommime.mgc')
magic_wrapper = magic.Magic(magic_file=magic_path, mime=True)
mime = _get_type_from_magic_object(path_or_binary, magic_wrapper, function_name, mime=True)
magic_wrapper = magic.Magic(magic_file=magic_path, mime=False)
full = _get_type_from_magic_object(path_or_binary, magic_wrapper, function_name, mime=False)
if mime == 'application/octet-stream':
mime = _get_type_from_magic_object(path_or_binary, magic, function_name, mime=True)
full = _get_type_from_magic_object(path_or_binary, magic, function_name, mime=False)
return {'mime': mime, 'full': full}
def _get_type_from_magic_object(path_or_binary, magic_object, function_name, mime=True):
try:
if isinstance(magic_object, magic.Magic):
result = getattr(magic_object, function_name)(path_or_binary)
else:
result = getattr(magic_object, function_name)(path_or_binary, mime=mime)
except FileNotFoundError as e:
logging.error('File not found: {}'.format(e))
result = 'error/file-not-found' if mime else 'Error: File not in storage!'
except Exception as exception:
logging.error('Could not determine file type: {} {}'.format(type(exception), str(exception)))
result = 'application/octet-stream' if mime else 'data'
return result
#!/usr/bin/env python
import os
from pathlib import Path
from subprocess import Popen, PIPE
from setuptools import setup
MODULE_NAME = 'fact_helper_file'
MIME_DIR = Path(__file__).parent / MODULE_NAME / 'mime'
class OperateInDirectory:
def __init__(self, target_directory: str):
self._current_working_dir = None
self._target_directory = target_directory
def __enter__(self):
self._current_working_dir = os.getcwd()
os.chdir(self._target_directory)
def __exit__(self, *_):
os.chdir(self._current_working_dir)
def execute_shell_command(shell_command):
with Popen(shell_command, shell=True, stdout=PIPE, stderr=PIPE) as pl:
output = pl.communicate()[0].decode('utf-8', errors='replace')
return output, pl.returncode
os.makedirs(str(MIME_DIR.parent / 'bin'), exist_ok=True)
with OperateInDirectory(str(MIME_DIR)):
file_output, file_code = execute_shell_command(
'(cat custom_* > custommime)'
' && file -C -m custommime'
' && mv -f custommime.mgc ../bin/'
' && rm custommime'
)
if file_code != 0:
exit('Failed to properly compile magic file\n{}'.format(file_output))
print('!!!!! \n\n\n !!!!!!')
setup(
name=MODULE_NAME,
version='0.2',
description='Helper functions for file type generation',
author='Johannes vom Dorp',
url='https://github.com/fkie-cad/fact_helper_file',
install_requires=['python-magic'],
python_requires='>=3.5',
packages=[MODULE_NAME, ],
package_data={MODULE_NAME: [str(MIME_DIR.parent / 'bin' / 'custommime.mgc'), ]}
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment