Skip to content

Commit ff08b99

Browse files
Merge pull request #700 from Adir-Shemesh/elf
Add initial elf files support
2 parents 6d0a777 + d0e9c00 commit ff08b99

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Change Log
22

33
## master (unreleased)
4+
- main: add initial elf files support
45

56
### New Features
67

capa/features/extractors/viv/file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def extract_file_import_names(vw, file_path):
4141
"""
4242
for va, _, _, tinfo in vw.getImports():
4343
# vivisect source: tinfo = "%s.%s" % (libname, impname)
44-
modname, impname = tinfo.split(".")
44+
modname, impname = tinfo.split(".", 1)
4545
if is_viv_ord_impname(impname):
4646
# replace ord prefix with #
4747
impname = "#%s" % impname[len("ord") :]

capa/features/extractors/viv/insn.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ def extract_insn_api_features(f, bb, insn):
127127
for name in capa.features.extractors.helpers.generate_symbols(dll, symbol):
128128
yield API(name), insn.va
129129

130+
# if jump leads to an ENDBRANCH instruction, skip it
131+
if f.vw.getByteDef(target)[1].startswith(b"\xf3\x0f\x1e"):
132+
target += 4
133+
130134
target = capa.features.extractors.viv.helpers.get_coderef_from(f.vw, target)
131135
if not target:
132136
return

capa/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
RULES_PATH_DEFAULT_STRING = "(embedded rules)"
4747
SIGNATURES_PATH_DEFAULT_STRING = "(embedded signatures)"
48-
SUPPORTED_FILE_MAGIC = set([b"MZ"])
48+
SUPPORTED_FILE_MAGIC = (b"MZ", b"\x7fELF")
4949
BACKEND_VIV = "vivisect"
5050
BACKEND_SMDA = "smda"
5151
EXTENSIONS_SHELLCODE_32 = ("sc32", "raw32")
@@ -240,8 +240,8 @@ def is_supported_file_type(sample: str) -> bool:
240240
Return if this is a supported file based on magic header values
241241
"""
242242
with open(sample, "rb") as f:
243-
magic = f.read(2)
244-
if magic in SUPPORTED_FILE_MAGIC:
243+
magic = f.read(4)
244+
if magic.startswith(SUPPORTED_FILE_MAGIC):
245245
return True
246246
else:
247247
return False
@@ -414,7 +414,7 @@ def get_workspace(path, format, sigpaths):
414414

415415
# don't analyze, so that we can add our Flirt function analyzer first.
416416
vw = viv_utils.getWorkspace(path, analyze=False, should_save=False)
417-
elif format == "pe":
417+
elif format in {"pe", "elf"}:
418418
vw = viv_utils.getWorkspace(path, analyze=False, should_save=False)
419419
elif format == "sc32":
420420
# these are not analyzed nor saved.
@@ -668,6 +668,7 @@ def install_common_args(parser, wanted=None):
668668
formats = [
669669
("auto", "(default) detect file type automatically"),
670670
("pe", "Windows PE file"),
671+
("elf", "Executable and Linkable Format"),
671672
("sc32", "32-bit shellcode"),
672673
("sc64", "64-bit shellcode"),
673674
("freeze", "features previously frozen by capa"),

0 commit comments

Comments
 (0)