Skip to content

Commit 3133b8e

Browse files
authored
Merge pull request #78 from CybercentreCanada/cli_improvements
Add flag in CLI to force installation of packages in environment (both virtual and non-)
2 parents b969aa4 + 8eea1db commit 3133b8e

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

maco/cli.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,17 @@ def process_filesystem(
9292
force: bool,
9393
include_base64: bool,
9494
create_venv: bool = False,
95+
skip_install: bool = False,
9596
) -> Tuple[int, int, int]:
9697
"""Process filesystem with extractors and print results of extraction.
9798
9899
Returns total number of analysed files, yara hits and successful maco extractions.
99100
"""
100101
if force:
101102
logger.warning("force execute will cause errors if an extractor requires a yara rule hit during execution")
102-
collected = collector.Collector(path_extractors, include=include, exclude=exclude, create_venv=create_venv)
103+
collected = collector.Collector(
104+
path_extractors, include=include, exclude=exclude, create_venv=create_venv, skip_install=skip_install
105+
)
103106

104107
logger.info(f"extractors loaded: {[x for x in collected.extractors.keys()]}\n")
105108
for _, extractor in collected.extractors.items():
@@ -191,6 +194,11 @@ def main():
191194
"This runs much slower than the alternative but may be necessary "
192195
"when there are many extractors with conflicting dependencies.",
193196
)
197+
parser.add_argument(
198+
"--force_install",
199+
action="store_true",
200+
help="Force installation of Python dependencies for extractors (in both host and virtual environments).",
201+
)
194202
args = parser.parse_args()
195203
inc = args.include.split(",") if args.include else []
196204
exc = args.exclude.split(",") if args.exclude else []
@@ -236,6 +244,7 @@ def main():
236244
force=args.force,
237245
include_base64=args.base64,
238246
create_venv=args.create_venv,
247+
skip_install=not args.force_install,
239248
)
240249

241250

maco/collector.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def __init__(
6767
include: List[str] = None,
6868
exclude: List[str] = None,
6969
create_venv: bool = False,
70+
skip_install: bool = False,
7071
):
7172
"""Discover and load extractors from file system."""
7273
# maco requires the extractor to be imported directly, so ensure they are available on the path
@@ -135,6 +136,7 @@ def extractor_module_callback(module: ModuleType, venv: str):
135136
root_directory=path_extractors,
136137
scanner=yara.compile(source=utils.MACO_YARA_RULE),
137138
create_venv=create_venv and os.path.isdir(path_extractors),
139+
skip_install=skip_install,
138140
),
139141
)
140142
p.start()

maco/utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,19 @@ def run_extractor(
450450
key = f"{module_name}_{extractor_class}"
451451
if key not in _loaded_extractors:
452452
# dynamic import of extractor
453-
mod = importlib.import_module(module_name)
454-
extractor_cls = mod.__getattribute__(extractor_class)
455-
extractor = extractor_cls()
453+
try:
454+
# Add the correct directory to the PATH before attempting to load the extractor
455+
import_path = module_path[: -4 - len(module_name)]
456+
sys.path.insert(1, import_path)
457+
mod = importlib.import_module(module_name)
458+
extractor_cls = mod.__getattribute__(extractor_class)
459+
extractor = extractor_cls()
460+
461+
# Add to cache
462+
_loaded_extractors[key] = extractor
463+
finally:
464+
sys.path.pop(1)
465+
456466
else:
457467
# retrieve cached extractor
458468
extractor = _loaded_extractors[key]

0 commit comments

Comments
 (0)