Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/config refactor #201

Merged
merged 41 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
0e87460
Consolidate config parsing code
clenk Feb 14, 2019
c61a84b
Consolidate module directory parsing code
clenk Feb 20, 2019
2fed099
Rename ModuleList -> module_list to be more pythonic
clenk Feb 21, 2019
10fb615
Merge branch 'dev' into config-refactor
clenk Feb 22, 2019
5d386a3
Ignore an expected error when parsing configs
clenk Feb 25, 2019
08b856a
Move config functions from utils.py to config.py
clenk Feb 26, 2019
882bf3d
Only parse main MultiScanner config once
clenk Feb 27, 2019
ec1fdeb
Streamline get_config_path() usage
clenk Feb 27, 2019
0e31c03
Cleanup code
clenk Feb 28, 2019
328d58a
Remove configregen; use `multiscanner init` instead
clenk Feb 28, 2019
322a7ba
Switch multiscan() to not use config filename
clenk Mar 1, 2019
74245b3
Update module list handling
clenk Mar 5, 2019
1404df1
Fix a few calls to multiscan()
clenk Mar 5, 2019
089d17b
Allow selecting which modules to run in web UI
clenk Mar 5, 2019
2734f2d
Fix failing test
clenk Mar 26, 2019
c5e34ef
Declare config globals at top of file
clenk Mar 26, 2019
7ca5ea2
Remove unused `recursive` param from multiscan()
clenk Mar 26, 2019
ac90491
Fix modules list
clenk Mar 28, 2019
a5bbc89
Simplify storage handler initialization
clenk Apr 1, 2019
ae138e9
Consolidate _rewrite_config functions
clenk Apr 4, 2019
6745bb8
Fix config globals imports
clenk Apr 4, 2019
ced6d75
Consolidate config functions into config.py
clenk Apr 11, 2019
1384def
Merge branch 'dev' into feature/config-refactor
clenk Apr 11, 2019
798369d
Fix API test due to filemeta module
clenk Apr 12, 2019
d526d36
Update tests
clenk Apr 23, 2019
508b7f4
Switch to using ConfigParser objects
clenk Apr 29, 2019
c3586f7
Remove _load_default handling from ms.py
clenk Apr 29, 2019
e6eaad2
Use custom ConfigParser class
clenk May 1, 2019
b5e2c2e
Don't pass entire main config to multiscan
clenk May 7, 2019
fa43f68
Fix tests, including...
clenk May 9, 2019
4a7a731
Fix a test, remove unnecessary lines
clenk May 13, 2019
3a01655
Convert config vals in sections to Python literals
clenk May 13, 2019
6817f4b
Improve some tests
clenk May 16, 2019
9edd050
Add/improve config tests
clenk May 24, 2019
0f8cd11
Always run filemeta and ssdeeper modules
clenk Jun 11, 2019
b5e3c87
Merge branch 'dev' into feature/config-refactor
clenk Jun 18, 2019
db1ca0d
Change pdf config to .ini
clenk Jun 18, 2019
d66ed9c
Rename CONFIG_FILE -> CONFIG_FILEPATH
clenk Jun 20, 2019
625b5fa
move imports, metadata_list is now cleared after the loop completes
emmanvg Jun 29, 2019
ddc4451
change slash for correct one and convert dict into object
emmanvg Jul 15, 2019
8dc9732
Convert config dict to object for distributed mode
clenk Aug 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docker_utils/api_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ password =
vhost = /
flush_every = 100
flush_interval = 10
tz = US/Eastern

[Database]
db_type = sqlite
Expand Down
6 changes: 3 additions & 3 deletions multiscanner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from .config import ( # noqa F401
PY3, MS_WD, CONFIG, MODULESDIR
MSConfigParser, MS_WD, PY3, config_init, update_ms_config, update_ms_config_file
)

from .ms import ( # noqa F401
config_init, multiscan, parse_reports, _ModuleInterface,
_GlobalModuleInterface, _Thread, _run_module, _main
multiscan, parse_reports, _ModuleInterface,
_GlobalModuleInterface, _Thread, _run_module, _main, _get_main_modules
)

from .version import __version__ # noqa F401
12 changes: 2 additions & 10 deletions multiscanner/analytics/ssdeep_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
'''

import argparse
import configparser
import json
import logging
import sys
Expand All @@ -37,20 +36,13 @@
ssdeep = False


from multiscanner import CONFIG as MS_CONFIG
from multiscanner.common import utils
from multiscanner.storage import storage


class SSDeepAnalytic:

def __init__(self):
storage_conf = utils.get_config_path(MS_CONFIG, 'storage')
config_object = configparser.ConfigParser()
config_object.optionxform = str
config_object.read(storage_conf)
conf = utils.parse_config(config_object)
storage_handler = storage.StorageHandler(configfile=storage_conf)
storage_handler = storage.StorageHandler()
es_handler = storage_handler.load_required_module('ElasticSearchStorage')

if not es_handler:
Expand All @@ -59,7 +51,7 @@ def __init__(self):

# probably not ideal...
self.es = es_handler.es
self.index = conf['ElasticSearchStorage']['index']
self.index = es_handler.index
self.doc_type = '_doc'

def ssdeep_compare(self):
Expand Down
15 changes: 8 additions & 7 deletions multiscanner/common/dir_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

from multiscanner import CONFIG as MS_CONFIG
from multiscanner import multiscan, parse_reports
from multiscanner.common import utils
from multiscanner import config as msconf
from multiscanner.storage import storage

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -81,8 +80,7 @@ def start_observer(directory, work_queue, recursive=False):
def multiscanner_process(work_queue, config, batch_size, wait_seconds, delete, exit_signal):
filelist = []
time_stamp = None
storage_conf = utils.get_config_path(config, 'storage')
storage_handler = storage.StorageHandler(configfile=storage_conf)
storage_handler = storage.StorageHandler()
while not exit_signal.value:
time.sleep(1)
try:
Expand All @@ -102,7 +100,7 @@ def multiscanner_process(work_queue, config, batch_size, wait_seconds, delete, e
else:
continue

resultlist = multiscan(filelist, configfile=config)
resultlist = multiscan(filelist, config=config)
results = parse_reports(resultlist, python=True)
if delete:
for file_name in results:
Expand All @@ -118,13 +116,16 @@ def multiscanner_process(work_queue, config, batch_size, wait_seconds, delete, e

def _main():
args = _parse_args()
if args.config != msconf.CONFIG_FILEPATH:
msconf.update_ms_config_file(args.config)

work_queue = multiprocessing.Queue()
exit_signal = multiprocessing.Value('b')
exit_signal.value = False
observer = start_observer(args.Directory, work_queue, args.recursive)
ms_process = multiprocessing.Process(
target=multiscanner_process,
args=(work_queue, args.config, args.batch, args.seconds, args.delete, exit_signal))
args=(work_queue, msconf.MS_CONFIG, args.batch, args.seconds, args.delete, exit_signal))
ms_process.start()
try:
while True:
Expand All @@ -141,7 +142,7 @@ def _main():
def _parse_args():
parser = argparse.ArgumentParser(description='Monitor a directory and submit new files to MultiScanner')
parser.add_argument("-c", "--config", help="The config file to use", required=False,
default=MS_CONFIG)
default=msconf.CONFIG_FILEPATH)
parser.add_argument("-s", "--seconds", help="The number of seconds to wait for additional files",
required=False, default=120, type=int)
parser.add_argument("-b", "--batch", help="The max number of files per batch", required=False,
Expand Down
9 changes: 4 additions & 5 deletions multiscanner/common/pdf_generator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
from __future__ import (division, absolute_import, with_statement,
print_function, unicode_literals)

import json
import os

from reportlab.lib import colors, units
from reportlab.platypus import TableStyle

from multiscanner.common.pdf_generator import generic_pdf
from multiscanner import config as msconf


def create_pdf_document(DIR, report):
'''
Method to create a PDF report based of a multiscanner JSON report.
Args:
DIR: Represents the a directory containing the 'pdf_config.json' file.
DIR: Represents the a directory containing the 'pdf_config.ini' file.
report: A JSON object.
'''
with open(os.path.join(os.path.split(DIR)[0], 'pdf_config.json')) as data_file:
pdf_components = json.load(data_file)

pdf_config = os.path.join(DIR, 'pdf_config.ini')
pdf_components = msconf.read_config(pdf_config).get_section('pdf')
gen_pdf = generic_pdf.GenericPDF(pdf_components)

notice = []
Expand Down
49 changes: 5 additions & 44 deletions multiscanner/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import (absolute_import, division, unicode_literals, with_statement)

import ast
import configparser
import imp
import logging
import os
import sys

from multiscanner.config import PY3
from six import PY3

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,42 +73,6 @@ def convert_encoding(data, encoding='UTF-8', errors='replace'):
return data


def parse_config(config_object):
"""Take a config object and returns it as a dictionary"""
return_var = {}
for section in config_object.sections():
section_dict = dict(config_object.items(section))
for key in section_dict:
try:
section_dict[key] = ast.literal_eval(section_dict[key])
except Exception as e:
logger.debug(e)
return_var[section] = section_dict
return return_var


def get_config_path(config_file, component):
"""Gets the location of the config file for the given multiscanner component
from the multiscanner config file
Components:
storage
api
web"""
conf = configparser.ConfigParser()
conf.read(config_file)
conf = parse_config(conf)
try:
return conf['main']['%s-config' % component]
except KeyError:
logger.error(
"Couldn't find '{}-config' value in 'main' section "
"of config file. Have you run 'python multiscanner.py init'?"
.format(component)
)
sys.exit()


def dirname(path):
"""OS independent version of os.path.dirname"""
split = path.split('/')
Expand All @@ -134,7 +95,7 @@ def basename(path):
return split[-1]


def parseDir(directory, recursive=False, exclude=['__init__']):
def parse_dir(directory, recursive=False, exclude=['__init__']):
"""
Returns a list of files in a directory.
Expand All @@ -148,7 +109,7 @@ def parseDir(directory, recursive=False, exclude=['__init__']):
item = os.path.join(directory, item)
if os.path.isdir(item):
if recursive:
filelist.extend(parseDir(item, recursive))
filelist.extend(parse_dir(item, recursive))
else:
continue
else:
Expand All @@ -162,7 +123,7 @@ def parseDir(directory, recursive=False, exclude=['__init__']):
return filelist


def parseFileList(FileList, recursive=False):
def parse_file_list(FileList, recursive=False):
"""
Takes a list of files and directories and returns a list of files.
Expand All @@ -173,7 +134,7 @@ def parseFileList(FileList, recursive=False):
filelist = []
for item in FileList:
if os.path.isdir(item):
filelist.extend(parseDir(item, recursive))
filelist.extend(parse_dir(item, recursive))
elif os.path.isfile(item):
if not PY3:
filelist.append(item.decode('utf8'))
Expand Down
Loading