2018-04-04 00:21:39 +02:00
|
|
|
#!/usr/bin/python3
|
2018-04-04 23:15:00 +02:00
|
|
|
|
2018-04-02 19:12:10 +02:00
|
|
|
import os
|
2018-04-29 22:55:26 +02:00
|
|
|
from typing import Tuple
|
|
|
|
import sys
|
|
|
|
import itertools
|
2018-04-01 17:13:34 +02:00
|
|
|
import mimetypes
|
2018-03-06 23:20:18 +01:00
|
|
|
import argparse
|
2018-04-14 16:10:45 +02:00
|
|
|
import multiprocessing
|
2018-03-06 23:20:18 +01:00
|
|
|
|
2018-06-24 22:40:57 +02:00
|
|
|
try:
|
2018-07-10 20:49:54 +02:00
|
|
|
from libmat2 import parser_factory, UNSUPPORTED_EXTENSIONS, check_dependencies
|
2018-06-24 22:40:57 +02:00
|
|
|
except ValueError as e:
|
|
|
|
print(e)
|
|
|
|
sys.exit(1)
|
2018-03-06 23:20:18 +01:00
|
|
|
|
2018-08-03 22:20:24 +02:00
|
|
|
__version__ = '0.3.0'
|
2018-04-04 23:18:32 +02:00
|
|
|
|
2018-07-23 23:42:56 +02:00
|
|
|
def __check_file(filename: str, mode: int=os.R_OK) -> bool:
|
2018-06-10 00:07:49 +02:00
|
|
|
if not os.path.exists(filename):
|
|
|
|
print("[-] %s is doesn't exist." % filename)
|
|
|
|
return False
|
|
|
|
elif not os.path.isfile(filename):
|
2018-04-02 19:12:10 +02:00
|
|
|
print("[-] %s is not a regular file." % filename)
|
|
|
|
return False
|
|
|
|
elif not os.access(filename, mode):
|
|
|
|
print("[-] %s is not readable and writeable." % filename)
|
|
|
|
return False
|
|
|
|
return True
|
2018-03-06 23:20:18 +01:00
|
|
|
|
2018-04-04 23:21:48 +02:00
|
|
|
|
2018-03-06 23:20:18 +01:00
|
|
|
def create_arg_parser():
|
|
|
|
parser = argparse.ArgumentParser(description='Metadata anonymisation toolkit 2')
|
2018-07-24 22:34:33 +02:00
|
|
|
parser.add_argument('files', nargs='*', help='the files to process')
|
2018-05-14 22:44:31 +02:00
|
|
|
parser.add_argument('-v', '--version', action='version',
|
2018-05-16 22:36:59 +02:00
|
|
|
version='MAT2 %s' % __version__)
|
2018-05-15 23:27:58 +02:00
|
|
|
parser.add_argument('-l', '--list', action='store_true',
|
2018-05-16 22:36:59 +02:00
|
|
|
help='list all supported fileformats')
|
2018-07-10 20:49:54 +02:00
|
|
|
parser.add_argument('-c', '--check-dependencies', action='store_true',
|
2018-07-23 23:42:56 +02:00
|
|
|
help='check if MAT2 has all the dependencies it needs')
|
2018-07-10 20:49:54 +02:00
|
|
|
|
2018-03-06 23:20:18 +01:00
|
|
|
|
2018-05-16 00:07:04 +02:00
|
|
|
info = parser.add_mutually_exclusive_group()
|
2018-03-06 23:20:18 +01:00
|
|
|
info.add_argument('-s', '--show', action='store_true',
|
2018-07-09 00:13:16 +02:00
|
|
|
help='list harmful metadata detectable by MAT2 without removing them')
|
2018-04-14 21:23:31 +02:00
|
|
|
info.add_argument('-L', '--lightweight', action='store_true',
|
|
|
|
help='remove SOME metadata')
|
2018-03-06 23:20:18 +01:00
|
|
|
return parser
|
|
|
|
|
2018-04-04 23:21:48 +02:00
|
|
|
|
2018-05-16 22:36:59 +02:00
|
|
|
def show_meta(filename: str):
|
2018-04-02 19:12:10 +02:00
|
|
|
if not __check_file(filename):
|
|
|
|
return
|
|
|
|
|
2018-06-04 22:54:01 +02:00
|
|
|
p, mtype = parser_factory.get_parser(filename) # type: ignore
|
2018-03-31 21:15:48 +02:00
|
|
|
if p is None:
|
2018-04-01 17:13:34 +02:00
|
|
|
print("[-] %s's format (%s) is not supported" % (filename, mtype))
|
2018-03-31 21:15:48 +02:00
|
|
|
return
|
2018-05-16 22:36:59 +02:00
|
|
|
|
2018-04-02 19:12:10 +02:00
|
|
|
print("[+] Metadata for %s:" % filename)
|
2018-05-16 22:36:59 +02:00
|
|
|
for k, v in p.get_meta().items():
|
2018-04-04 00:44:54 +02:00
|
|
|
try: # FIXME this is ugly.
|
|
|
|
print(" %s: %s" % (k, v))
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
print(" %s: harmful content" % k)
|
2018-04-02 19:12:10 +02:00
|
|
|
|
2018-05-16 22:36:59 +02:00
|
|
|
def clean_meta(params: Tuple[str, bool]) -> bool:
|
2018-04-29 22:55:26 +02:00
|
|
|
filename, is_lightweigth = params
|
2018-04-02 19:12:10 +02:00
|
|
|
if not __check_file(filename, os.R_OK|os.W_OK):
|
2018-05-16 22:36:59 +02:00
|
|
|
return False
|
2018-04-02 19:12:10 +02:00
|
|
|
|
2018-06-04 22:54:01 +02:00
|
|
|
p, mtype = parser_factory.get_parser(filename) # type: ignore
|
2018-04-02 19:12:10 +02:00
|
|
|
if p is None:
|
|
|
|
print("[-] %s's format (%s) is not supported" % (filename, mtype))
|
2018-04-29 22:55:26 +02:00
|
|
|
return False
|
2018-04-14 21:23:31 +02:00
|
|
|
if is_lightweigth:
|
2018-04-23 00:11:25 +02:00
|
|
|
return p.remove_all_lightweight()
|
|
|
|
return p.remove_all()
|
2018-03-06 23:20:18 +01:00
|
|
|
|
2018-04-04 23:21:48 +02:00
|
|
|
|
2018-04-04 23:15:00 +02:00
|
|
|
def show_parsers():
|
|
|
|
print('[+] Supported formats:')
|
2018-06-04 23:32:13 +02:00
|
|
|
formats = list()
|
2018-04-04 23:15:00 +02:00
|
|
|
for parser in parser_factory._get_parsers():
|
|
|
|
for mtype in parser.mimetypes:
|
2018-05-16 22:00:37 +02:00
|
|
|
extensions = set()
|
|
|
|
for extension in mimetypes.guess_all_extensions(mtype):
|
2018-07-08 22:40:36 +02:00
|
|
|
if extension[1:] not in UNSUPPORTED_EXTENSIONS: # skip the dot
|
2018-05-16 22:00:37 +02:00
|
|
|
extensions.add(extension)
|
|
|
|
if not extensions:
|
|
|
|
# we're not supporting a single extension in the current
|
|
|
|
# mimetype, so there is not point in showing the mimetype at all
|
|
|
|
continue
|
2018-06-04 23:32:13 +02:00
|
|
|
formats.append(' - %s (%s)' % (mtype, ', '.join(extensions)))
|
|
|
|
print('\n'.join(sorted(formats)))
|
2018-04-04 23:15:00 +02:00
|
|
|
|
2018-04-04 23:21:48 +02:00
|
|
|
|
2018-04-04 23:15:00 +02:00
|
|
|
def __get_files_recursively(files):
|
|
|
|
for f in files:
|
2018-06-10 00:07:49 +02:00
|
|
|
if os.path.isdir(f):
|
2018-04-04 23:15:00 +02:00
|
|
|
for path, _, _files in os.walk(f):
|
|
|
|
for _f in _files:
|
2018-06-10 00:07:49 +02:00
|
|
|
fname = os.path.join(path, _f)
|
|
|
|
if __check_file(fname):
|
|
|
|
yield fname
|
|
|
|
elif __check_file(f):
|
|
|
|
yield f
|
2018-04-04 23:15:00 +02:00
|
|
|
|
2018-03-06 23:20:18 +01:00
|
|
|
def main():
|
2018-04-03 23:57:13 +02:00
|
|
|
arg_parser = create_arg_parser()
|
|
|
|
args = arg_parser.parse_args()
|
2018-04-02 19:12:10 +02:00
|
|
|
|
2018-04-04 23:15:00 +02:00
|
|
|
if not args.files:
|
2018-07-10 20:49:54 +02:00
|
|
|
if args.list:
|
|
|
|
show_parsers()
|
|
|
|
elif args.check_dependencies:
|
|
|
|
print("Dependencies required for MAT2 %s:" % __version__)
|
|
|
|
for key, value in sorted(check_dependencies().items()):
|
|
|
|
print('- %s: %s' % (key, 'yes' if value else 'no'))
|
|
|
|
else:
|
2018-04-04 23:15:00 +02:00
|
|
|
return arg_parser.print_help()
|
2018-05-16 22:36:59 +02:00
|
|
|
return 0
|
2018-04-04 23:15:00 +02:00
|
|
|
|
2018-04-14 16:10:45 +02:00
|
|
|
elif args.show:
|
2018-04-04 23:18:32 +02:00
|
|
|
for f in __get_files_recursively(args.files):
|
2018-03-06 23:20:18 +01:00
|
|
|
show_meta(f)
|
2018-05-16 22:36:59 +02:00
|
|
|
return 0
|
2018-04-14 16:10:45 +02:00
|
|
|
|
2018-04-29 22:55:26 +02:00
|
|
|
else:
|
|
|
|
p = multiprocessing.Pool()
|
2018-04-16 22:06:55 +02:00
|
|
|
mode = (args.lightweight is True)
|
2018-04-29 22:55:26 +02:00
|
|
|
l = zip(__get_files_recursively(args.files), itertools.repeat(mode))
|
2018-03-06 23:20:18 +01:00
|
|
|
|
2018-04-29 22:55:26 +02:00
|
|
|
ret = list(p.imap_unordered(clean_meta, list(l)))
|
|
|
|
return 0 if all(ret) else -1
|
2018-03-06 23:20:18 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-04-29 22:55:26 +02:00
|
|
|
sys.exit(main())
|