diff --git a/oletools/olemeta.py b/oletools/olemeta.py index 66318a0..573f1c5 100644 --- a/oletools/olemeta.py +++ b/oletools/olemeta.py @@ -48,19 +48,20 @@ http://www.decalage.info/python/oletools # 2016-10-25 PL: - fixed print for Python 3 # 2016-10-28 PL: - removed the UTF8 codec for console display # 2017-04-26 v0.51 PL: - fixed absolute imports (issue #141) +# 2017-05-04 PL: - added optparse and xglob (issue #141) -__version__ = '0.51' +__version__ = '0.51dev7' #------------------------------------------------------------------------------ # TODO: -# + optparse # + nicer output: table with fixed columns, datetime, etc # + CSV output # + option to only show available properties (by default) +# + display codepage names #=== IMPORTS ================================================================= -import sys, os, codecs +import sys, os, optparse # IMPORTANT: it should be possible to run oletools directly as scripts # in any directory without installing them with pip or setup.py. @@ -75,17 +76,13 @@ if not _parent_dir in sys.path: sys.path.insert(0, _parent_dir) from oletools.thirdparty import olefile +from oletools.thirdparty import xglob from oletools.thirdparty.tablestream import tablestream #=== MAIN ================================================================= -def main(): - try: - ole = olefile.OleFileIO(sys.argv[1]) - except IndexError: - sys.exit(__doc__) - +def process_ole(ole): # parse and display metadata: meta = ole.get_metadata() @@ -128,7 +125,53 @@ def main(): t.write_row([prop, value], colors=[None, 'yellow']) t.close() - ole.close() + +# === MAIN =================================================================== + +def main(): + # print banner with version + print('olemeta %s - http://decalage.info/python/oletools' % __version__) + print ('THIS IS WORK IN PROGRESS - Check updates regularly!') + print ('Please report any issue at https://github.com/decalage2/oletools/issues') + + usage = 'usage: olemeta [options] [filename2 ...]' + parser = optparse.OptionParser(usage=usage) + parser.add_option("-r", action="store_true", dest="recursive", + help='find files recursively in subdirectories.') + parser.add_option("-z", "--zip", dest='zip_password', type='str', default=None, + help='if the file is a zip archive, open all files from it, using the provided password (requires Python 2.6+)') + parser.add_option("-f", "--zipfname", dest='zip_fname', type='str', default='*', + help='if the file is a zip archive, file(s) to be opened within the zip. Wildcards * and ? are supported. (default:*)') + + # TODO: add logfile option + # parser.add_option('-l', '--loglevel', dest="loglevel", action="store", default=DEFAULT_LOG_LEVEL, + # help="logging level debug/info/warning/error/critical (default=%default)") + + (options, args) = parser.parse_args() + + # Print help if no arguments are passed + if len(args) == 0: + print(__doc__) + parser.print_help() + sys.exit() + + for container, filename, data in xglob.iter_files(args, recursive=options.recursive, + zip_password=options.zip_password, zip_fname=options.zip_fname): + # TODO: handle xglob errors + # ignore directory names stored in zip files: + if container and filename.endswith('/'): + continue + full_name = '%s in %s' % (filename, container) if container else filename + print("=" * 79) + print('FILE: %s\n' % full_name) + if data is not None: + # data extracted from zip file + ole = olefile.OleFileIO(data) + else: + # normal filename + ole = olefile.OleFileIO(filename) + process_ole(ole) + ole.close() if __name__ == '__main__': main() diff --git a/oletools/oletimes.py b/oletools/oletimes.py index 1807958..997a957 100644 --- a/oletools/oletimes.py +++ b/oletools/oletimes.py @@ -49,7 +49,7 @@ http://www.decalage.info/python/oletools # 2016-07-20 v0.50 SL: - added Python 3 support # 2016-09-05 PL: - added main entry point for setup.py # 2017-05-03 v0.51 PL: - fixed absolute imports (issue #141) -# 2017-05-04 PL: - added optparse and xglob +# 2017-05-04 PL: - added optparse and xglob (issue #141) __version__ = '0.51dev7' @@ -112,7 +112,6 @@ def main(): print('oletimes %s - http://decalage.info/python/oletools' % __version__) print ('THIS IS WORK IN PROGRESS - Check updates regularly!') print ('Please report any issue at https://github.com/decalage2/oletools/issues') - print ('') usage = 'usage: oletimes [options] [filename2 ...]' parser = optparse.OptionParser(usage=usage)