def run_print(): """This is called from test_read* tests as script. Prints & logs unicode""" from oletools.common.io_encoding import ensure_stdout_handles_unicode from oletools.common.log_helper import log_helper ensure_stdout_handles_unicode() print(u'Check: \u2713') # print check mark # check logging as well logger = log_helper.get_or_create_silent_logger('test_encoding_handler') log_helper.enable_logging(False, 'debug', stream=sys.stdout) logger.info(u'Check: \u2713') return 0
""" Test log_helpers """ import sys import logging from tests.common.log_helper import log_helper_test_imported from oletools.common.log_helper import log_helper DEBUG_MESSAGE = 'main: debug log' INFO_MESSAGE = 'main: info log' WARNING_MESSAGE = 'main: warning log' ERROR_MESSAGE = 'main: error log' CRITICAL_MESSAGE = 'main: critical log' RESULT_MESSAGE = 'main: result log' RESULT_TYPE = 'main: result' logger = log_helper.get_or_create_silent_logger('test_main') def enable_logging(): """Enable logging if imported by third party modules.""" logger.setLevel(log_helper.NOTSET) log_helper_test_imported.enable_logging() def main(args): """ Try to cover possible logging scenarios. For each scenario covered, here's the expected args and outcome: - Log without enabling: ['<level>'] * logging when being imported - should never print - Log as JSON without enabling: ['as-json', '<level>'] * logging as JSON when being imported - should never print
_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..')) if _parent_dir not in sys.path: sys.path.insert(0, _parent_dir) from oletools.common.errors import CryptoErrorBase, WrongEncryptionPassword, \ UnsupportedEncryptionError, MaxCryptoNestingReached, CryptoLibNotImported from oletools.common.log_helper import log_helper #: if there is an encrypted file embedded in an encrypted file, #: how deep down do we go MAX_NESTING_DEPTH = 10 # === LOGGING ================================================================= # a global logger object used for debugging: log = log_helper.get_or_create_silent_logger('crypto') def enable_logging(): """ Enable logging for this module (disabled by default). For use by third-party libraries that import `crypto` as module. This will set the module-specific logger level to NOTSET, which means the main application controls the actual logging level. """ log.setLevel(log_helper.NOTSET) def is_encrypted(some_file):
# so we add the oletools parent folder to sys.path (absolute+normalized path): _thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__))) # print('_thismodule_dir = %r' % _thismodule_dir) _parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..')) # print('_parent_dir = %r' % _thirdparty_dir) if _parent_dir not in sys.path: sys.path.insert(0, _parent_dir) from oletools.thirdparty.tablestream import tablestream from oletools import crypto, ftguess, olevba, mraptor, oleobj, ooxml from oletools.common.log_helper import log_helper from oletools.common.codepages import get_codepage_name # === LOGGING ================================================================= log = log_helper.get_or_create_silent_logger('oleid') # === CONSTANTS =============================================================== class RISK(object): """ Constants for risk levels """ HIGH = 'HIGH' MEDIUM = 'Medium' LOW = 'low' NONE = 'none' INFO = 'info' UNKNOWN = 'Unknown' ERROR = 'Error' # if a check triggered an unexpected error
_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..')) # print('_parent_dir = %r' % _thirdparty_dir) if not _parent_dir in sys.path: sys.path.insert(0, _parent_dir) from oletools.thirdparty.xglob import xglob from oletools.thirdparty.tablestream import tablestream from oletools import olevba from oletools.olevba import TYPE2TAG from oletools.common.log_helper import log_helper # === LOGGING ================================================================= # a global logger object used for debugging: log = log_helper.get_or_create_silent_logger('mraptor') #--- CONSTANTS ---------------------------------------------------------------- # URL and message to report issues: # TODO: make it a common variable for all oletools URL_ISSUES = 'https://github.com/decalage2/oletools/issues' MSG_ISSUES = 'Please report this issue on %s' % URL_ISSUES # 'AutoExec', 'AutoOpen', 'Auto_Open', 'AutoClose', 'Auto_Close', 'AutoNew', 'AutoExit', # 'Document_Open', 'DocumentOpen', # 'Document_Close', 'DocumentBeforeClose', 'Document_BeforeClose', # 'DocumentChange','Document_New', # 'NewDocument' # 'Workbook_Open', 'Workbook_Close',
import sys from oletools.common.log_helper import log_helper from zipfile import ZipFile, BadZipfile, is_zipfile from os.path import splitext import io import re # import lxml or ElementTree for XML parsing: try: # lxml: best performance for XML processing import lxml.etree as ET except ImportError: import xml.etree.cElementTree as ET logger = log_helper.get_or_create_silent_logger('ooxml') #: subfiles that have to be part of every ooxml file FILE_CONTENT_TYPES = '[Content_Types].xml' FILE_RELATIONSHIPS = '_rels/.rels' #: start of content type attributes CONTENT_TYPES_EXCEL = ( 'application/vnd.openxmlformats-officedocument.spreadsheetml.', 'application/vnd.ms-excel.', ) CONTENT_TYPES_WORD = ( 'application/vnd.openxmlformats-officedocument.wordprocessingml.', ) CONTENT_TYPES_PPT = ( 'application/vnd.openxmlformats-officedocument.presentationml.',
STGTY_LOCKBYTES = olefile.STGTY_LOCKBYTES # 3 STGTY_PROPERTY = olefile.STGTY_PROPERTY # 4 STGTY_ROOT = olefile.STGTY_ROOT # 5 STGTY_SUBSTREAM = 10 ENTRY_TYPE2STR = { olefile.STGTY_EMPTY: 'empty', olefile.STGTY_STORAGE: 'storage', olefile.STGTY_STREAM: 'stream', olefile.STGTY_LOCKBYTES: 'lock-bytes', olefile.STGTY_PROPERTY: 'property', olefile.STGTY_ROOT: 'root', STGTY_SUBSTREAM: 'substream' } logger = log_helper.get_or_create_silent_logger('record_base') def enable_olefile_logging(): """ enable logging in olefile e.g., to get debug info from OleFileIO """ olefile.enable_logging() def enable_logging(): """ Enable logging for this module (disabled by default). For use by third-party libraries that import `record_base` as module. This will set the module-specific logger level to NOTSET, which means the main application controls the actual logging level.
FIELD_FILTER_BLACKLIST = 'exclude blacklisted' FIELD_FILTER_ALL = 'keep all' FIELD_FILTER_DEFAULT = FIELD_FILTER_BLACKLIST # banner to be printed at program start BANNER = """msodde %s - http://decalage.info/python/oletools THIS IS WORK IN PROGRESS - Check updates regularly! Please report any issue at https://github.com/decalage2/oletools/issues """ % __version__ # === LOGGING ================================================================= DEFAULT_LOG_LEVEL = "warning" # Default log level # a global logger object used for debugging: logger = log_helper.get_or_create_silent_logger('msodde') # === UNICODE IN PY2 ========================================================= def ensure_stdout_handles_unicode(): """ Ensure stdout can handle unicode by wrapping it if necessary Required e.g. if output of this script is piped or redirected in a linux shell, since then sys.stdout.encoding is ascii and cannot handle print(unicode). In that case we need to find some compatible encoding and wrap sys.stdout into a encoder following (many thanks!) https://stackoverflow.com/a/1819009 or https://stackoverflow.com/a/20447935 Can be undone by setting sys.stdout = sys.__stdout__ """
""" Test log_helpers """ import sys from tests.common.log_helper import log_helper_test_imported from oletools.common.log_helper import log_helper DEBUG_MESSAGE = 'main: debug log' INFO_MESSAGE = 'main: info log' WARNING_MESSAGE = 'main: warning log' ERROR_MESSAGE = 'main: error log' CRITICAL_MESSAGE = 'main: critical log' RESULT_MESSAGE = 'main: result log' RESULT_TYPE = 'main: result' logger = log_helper.get_or_create_silent_logger('test_main') def init_logging_and_log(args): """ Try to cover possible logging scenarios. For each scenario covered, here's the expected args and outcome: - Log without enabling: ['<level>'] * logging when being imported - should never print - Log as JSON without enabling: ['as-json', '<level>'] * logging as JSON when being imported - should never print - Enable and log: ['enable', '<level>'] * logging when being run as script - should log messages - Enable and log as JSON: ['as-json', 'enable', '<level>'] * logging as JSON when being run as script - should log messages as JSON - Enable, log as JSON and throw: ['enable', 'as-json', 'throw', '<level>'] * should produce JSON-compatible output, even after an unhandled exception """
""" Dummy file that logs messages, meant to be imported by the main test file """ from oletools.common.log_helper import log_helper import logging DEBUG_MESSAGE = 'imported: debug log' INFO_MESSAGE = 'imported: info log' WARNING_MESSAGE = 'imported: warning log' ERROR_MESSAGE = 'imported: error log' CRITICAL_MESSAGE = 'imported: critical log' RESULT_MESSAGE = 'imported: result log' RESULT_TYPE = 'imported: result' logger = log_helper.get_or_create_silent_logger('test_imported', logging.ERROR) def log(): logger.debug(DEBUG_MESSAGE) logger.info(INFO_MESSAGE) logger.warning(WARNING_MESSAGE) logger.error(ERROR_MESSAGE) logger.critical(CRITICAL_MESSAGE) logger.info(RESULT_MESSAGE, type=RESULT_TYPE)
FIELD_FILTER_ALL = 'keep all' FIELD_FILTER_DEFAULT = FIELD_FILTER_BLACKLIST # banner to be printed at program start BANNER = """msodde %s - http://decalage.info/python/oletools THIS IS WORK IN PROGRESS - Check updates regularly! Please report any issue at https://github.com/decalage2/oletools/issues """ % __version__ # === LOGGING ================================================================= DEFAULT_LOG_LEVEL = "warning" # Default log level # a global logger object used for debugging: logger = log_helper.get_or_create_silent_logger('msodde') # === UNICODE IN PY2 ========================================================= def ensure_stdout_handles_unicode(): """ Ensure stdout can handle unicode by wrapping it if necessary Required e.g. if output of this script is piped or redirected in a linux shell, since then sys.stdout.encoding is ascii and cannot handle print(unicode). In that case we need to find some compatible encoding and wrap sys.stdout into a encoder following (many thanks!) https://stackoverflow.com/a/1819009 or https://stackoverflow.com/a/20447935 Can be undone by setting sys.stdout = sys.__stdout__ """
""" Dummy file that logs messages, meant to be imported by the main test file """ from oletools.common.log_helper import log_helper DEBUG_MESSAGE = 'imported: debug log' INFO_MESSAGE = 'imported: info log' WARNING_MESSAGE = 'imported: warning log' ERROR_MESSAGE = 'imported: error log' CRITICAL_MESSAGE = 'imported: critical log' RESULT_MESSAGE = 'imported: result log' RESULT_TYPE = 'imported: result' logger = log_helper.get_or_create_silent_logger('test_imported') def enable_logging(): """Enable logging if imported by third party modules.""" logger.setLevel(log_helper.NOTSET) def log(): logger.debug(DEBUG_MESSAGE) logger.info(INFO_MESSAGE) logger.warning(WARNING_MESSAGE) logger.error(ERROR_MESSAGE) logger.critical(CRITICAL_MESSAGE) logger.info(RESULT_MESSAGE, type=RESULT_TYPE)