Exemple #1
0
def is_executable(path):
    '''Determine if a file is a binary executable.'''
    if os.name == 'nt':
        return magic.from_file(path, mime=True) == 'application/x-dosexec'
    else:
        return magic.from_file(path,
                               mime=True) == 'application/x-pie-executable'
Exemple #2
0
def fixfile(file_path: str):
    detected = magic.from_file(file_path, mime=True)
    if detected in file_exts:
        print("%s -> %s" % (detected, file_exts[detected]))
        os.rename(file_path, file_path + "." + file_exts[detected])
    else:
        print("%s not found?" % (detected))
        assert False
Exemple #3
0
def validate_picture_type(upload):
    # Make uploaded file accessible for analysis by saving in tmp
    tmp_path = 'tmp/%s' % upload.name[2:]
    default_storage.save(tmp_path, ContentFile(upload.file.read()))
    upload.file.seek(0)  # reset reading
    full_tmp_path = os.path.join(settings.MEDIA_ROOT, tmp_path)
    # Get MIME type of file using python-magic and then delete
    file_type = magic.from_file(full_tmp_path, mime=True)
    default_storage.delete(tmp_path)
    # Raise validation error if uploaded file is not an acceptable form of media
    if file_type not in settings.ALLOWED_PIC_TYPES:
        raise ValidationError('File type not supported.')
Exemple #4
0
    def is_pycfile(self):
        """Return True if it is a Python bytecode file, otherwise False."""

        if magic is None:
            raise ImportError("No module named magic")

        header = magic.from_file(self.path)
        if re.match(r"python (2\.[6-7]|3.[0-9]) byte-compiled", header):
            return True

        if header == "data":
            return True

        return False
Exemple #5
0
    def is_pyfile(self):
        """Return True if it is a Python file, otherwise False."""

        if magic is None:
            raise ImportError("No module named magic")

        header = magic.from_file(self.path)
        if re.match(r".*[pP]ython3? script.*", header):
            return True

        if self.path.endswith(".py"):
            if re.match(r"empty", header) or re.match(r"ASCII text.*", header):
                return True

        return False
def parse_input_file(arg_file):
    file_format = magic.Magic(mime=True)
    file_type = file_format.from_file(arg_file)
    log.info('Auto-detection of input file type: {0}'.format(
        magic.from_file(arg_file)))

    if 'text/plain' in file_type:
        log.info('Reader Mode: Text / Flat file Reader')
        delimiter = get_file_delimiter(arg_file)
        df = pd.read_csv(arg_file, sep=delimiter, dtype=str)
    elif 'spreadsheetml' in file_type:
        log.info('Reader Mode: Excel Reader')
        df = pd.read_excel(arg_file, dtype=str)

    return df.fillna('')
Exemple #7
0
def get_media_file_path(allow_videos=False):
    stay_in_loop = True
    show_warning = False
    while stay_in_loop:
        if show_warning:
            message = 'Please select an image file.' if not allow_videos else 'Please select an image or video file.'
            showwarning('Incorrect File Type', message)
        else:
            show_warning = True

        title = 'Select Image File' if not allow_videos else 'Select Image or Video File'
        file_path = askopenfilename(title=title)
        if file_path:
            file_type = magic.from_file(file_path, mime=True)

            # expect string of the form '<media_type>/<file_extension>'
            if file_type.split('/')[0] == 'image' or (
                    allow_videos and file_type.split('/')[0] == 'video'):
                is_video = (file_type.split('/')[0] == 'video')
                stay_in_loop = False

    return is_video, file_path
Exemple #8
0
    def test_write_png(self):
        image = slicedimage.TileSet(
            dimensions=[DimensionNames.X, DimensionNames.Y, "ch", "hyb"],
            shape={'ch': 2, 'hyb': 2},
            default_tile_shape={DimensionNames.Y: 120, DimensionNames.X: 80},
        )

        for hyb in range(2):
            for ch in range(2):
                tile = slicedimage.Tile(
                    coordinates={
                        DimensionNames.X: (0.0, 0.01),
                        DimensionNames.Y: (0.0, 0.01),
                    },
                    indices={
                        'hyb': hyb,
                        'ch': ch,
                    },
                )
                tile.numpy_array = np.zeros((120, 80), dtype=np.uint32)
                tile.numpy_array[hyb, ch] = 1
                image.add_tile(tile)

        with tempfile.TemporaryDirectory() as tempdir:
            with tempfile.NamedTemporaryFile(
                    suffix=".json", dir=tempdir, delete=False) as partition_file:
                partition_file_path = Path(partition_file.name)
                # create the tileset and save it.
                partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
                    image, partition_file_path.as_uri(), tile_format=ImageFormat.PNG)
                writer = codecs.getwriter("utf-8")
                json.dump(partition_doc, writer(partition_file))
                partition_file.flush()

            # construct a URL to the tileset we wrote, and load the tileset.
            loaded = slicedimage.Reader.parse_doc(
                partition_file_path.name, partition_file_path.parent.as_uri())

            # verify that we wrote some pngs, and all the pngs we wrote actually identify as pngs.
            pngfiles = list(Path(tempdir).glob("*.png"))
            assert len(pngfiles) > 0
            for pngfile in pngfiles:
                filetype = magic.from_file(fspath(pngfile))
                assert filetype.lower().startswith("png")

            # compare the tiles we loaded to the tiles we set up.
            for hyb in range(2):
                for ch in range(2):
                    tiles = [_tile
                             for _tile in loaded.tiles(
                                 lambda tile: (
                                     tile.indices['hyb'] == hyb
                                     and tile.indices['ch'] == ch))]

                    self.assertEqual(len(tiles), 1)

                    expected = np.zeros((120, 80), dtype=np.uint32)
                    expected[hyb, ch] = 1

                    self.assertEqual(tiles[0].numpy_array.all(), expected.all())
                    self.assertIsNotNone(tiles[0].sha256)
def check_file_type(file):
    """check for file types in directory"""
    file_type = magic.from_file(file)
    print(f"File type: {file_type}")
Exemple #10
0
import sys
sourceFileName = sys.argv[1]

#check that file is plaintext
from winmagic import magic
if magic.from_file(sourceFileName,mime = True) != 'text/plain':
    quit("Source file is not plaintext.")

#new file will be original filename + "utf"
import os
splitName = os.path.splitext(sourceFileName)
targetFileName = splitName[0] + 'utf' + splitName[1]

#detect source encoding
import cchardet as chardet
with open(sourceFileName, "rb") as f:
    msg = f.read()
    result = chardet.detect(msg)
    sourceEncoding = result.get('encoding')

#end if the source is already unicode
if sourceEncoding == 'UTF-8':
    print('Source file is already unicode. Exiting program...')
    quit()

#output UTF-8 encoded file
import codecs
BLOCKSIZE = 1048576 # or some other, desired size in bytes
with codecs.open(sourceFileName, "r", result.get('encoding')) as sourceFile:
    with codecs.open(targetFileName, "w", "utf-8") as targetFile:
        while True:
Exemple #11
0
    def run(self):
        i = 0
        while True:
            new_file = "decompressed-" + str(i)

            # Check if it exist
            if not os.path.isfile(self.pathToArchive):
                print("Error while processing the archive")
                return ""

            # Analyzing file type.
            file_type = magic.from_file(self.pathToArchive)
            print("[*] File '{}' is '{}'.".format(self.pathToArchive,
                                                  file_type))

            # Analyzing archives.
            if "bzip2" in file_type:
                if self.unBzip2(new_file):
                    print(" └ [+] bzip2 uncompresssed")
                else:
                    print(
                        " └ [!] Error happened while trying to uncompress bzip2"
                    )

            elif "POSIX tar archive (GNU)" in file_type:
                if self.unTar(new_file):
                    print(" └ [+] tar uncompresssed")
                else:
                    print(
                        " └ [!] Error happened while trying to uncompress tar")

            elif "Zip" in file_type:
                if self.unZip(new_file):
                    print(" └ [+] zip uncompresssed")
                else:
                    print(
                        " └ [!] Error happened while trying to crack the zip")

            elif "ASCII text" in file_type:
                if (self.handleFlag(new_file)):
                    flag = ""
                    with open(new_file, "r") as nf:
                        flag = nf.read()
                    return flag

            elif "gzip" in file_type:
                if self.unGzip(new_file):
                    print(" └ [+] gzip uncompresssed")
                else:
                    print(
                        " └ [!] Error happened while trying to uncompress gzip"
                    )

            else:
                print("[!] Unknown archive, exiting.")
                break

            # Removing old file and going on with analysis.
            if i > 0:
                os.remove(self.pathToArchive)
            self.pathToArchive = new_file
            i += 1