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

Add config merge in extract_manager #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
51 changes: 46 additions & 5 deletions malduck/extractor/extract_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,42 @@

__all__ = ["ExtractManager"]

def merge_configs(base_config: Config, new_config: Config) -> Config:
"""
Merge static configurations.

:param base_config: Base configuration
:param new_config: Changes to apply
:return: Merged configuration
"""
config = dict(base_config)
for k, v in new_config.items():
if k == "family":
continue
if k not in config:
config[k] = v
elif config[k] == v:
continue
elif type(config[k]) == type(v):
if isinstance(config[k], list):
for el in v:
if el not in config[k]:
config[k] = config[k] + [el]
elif isinstance(config[k], dict):
config[k] = merge_configs(config[k], v)
else:
log.warning(
f"Merge error for key '{k}' of type {type(v)}: Merging not implemented, "
"using value from base_config"
)
else:
log.warning(
f"TypeError for key '{k}': Failed to merge existing value ({type(config[k])}) "
f"'{config[k]}' with new value (new type: {type(v)}) '{v}', "
"using value from base_config"
)
return config


class ExtractManager:
"""
Expand Down Expand Up @@ -136,13 +172,18 @@ def push_config(self, config: Config) -> bool:

family = config["family"]
if family in self.configs:
base_config = None
new_config = None
if is_config_better(base_config=self.configs[family], new_config=config):
self.configs[family] = config
log.debug("%s config looks better than previous one", family)
return True
log.debug(f"{family} new config looks better - use as base and merge existing")
base_config = config
new_config = self.configs[family]
else:
log.debug("%s config doesn't look better than previous one", family)
return False
log.debug(f"{family} new config doesn't look better - try merging into existing.")
base_config = self.configs[family]
new_config = config
self.configs[family] = merge_configs(base_config=base_config, new_config=new_config)
return True

if family in self.modules.override_paths:
# 'citadel' > 'zeus'
Expand Down
1 change: 1 addition & 0 deletions tests/files/modules/configmerge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .configmerge import ConfigMerge
16 changes: 16 additions & 0 deletions tests/files/modules/configmerge/configmerge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from malduck.extractor import Extractor


class ConfigMerge(Extractor):
yara_rules = "configmerge",
family = "ConfigMerge"

@Extractor.final
def final(self, p):
return {
"constant": "CONST",
"mem_types": [str(type(p))],
"dict": {
hex(p.imgbase): "imagebase"
}
}
6 changes: 6 additions & 0 deletions tests/files/modules/configmerge/configmerge.yar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rule configmerge {
strings:
$calc_exe_0x80000 = { E5 ED 2D 7A 0E 1D 32 DB 8E 73 56 1E 1C 95 93 A4 }
condition:
any of them
}
21 changes: 21 additions & 0 deletions tests/test_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,24 @@ def test_multirules():
'matched': ['v2'],
'third': ['ThIrD string']
}]


def test_configmerge():
modules = ExtractorModules("tests/files/modules")
calc_exe_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "files", "calc.exe")
extractor = ExtractManager(modules)
extractor.push_file(calc_exe_path)
assert len(extractor.config) == 1

conf = extractor.config[0]
assert conf == {
'family': "ConfigMerge",
'constant': "CONST",
'mem_types': [str(procmem), str(procmempe)],
'dict': {
'0x0': "imagebase",
'0x1000000': "imagebase"
}
}