Skip to content

Commit 804143b

Browse files
committed
dotnet ELF support
Signed-off-by: Prabhu Subramanian <[email protected]>
1 parent 4644d3b commit 804143b

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

blint/binary.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,8 @@ def add_elf_metadata(exe_file, metadata, parsed_obj):
748748
if exe_type:
749749
metadata["exe_type"] = exe_type
750750
metadata["functions"] = parse_functions(parsed_obj.functions)
751-
752751
metadata["ctor_functions"] = parse_functions(parsed_obj.ctor_functions)
753-
752+
metadata["dotnet_dependencies"] = parse_overlay(parsed_obj)
754753
return metadata
755754

756755

@@ -886,11 +885,11 @@ def determine_elf_flags(header):
886885
return eflags_str
887886

888887

889-
def parse_pe_overlay(parsed_obj: lief.PE.Binary) -> dict[str, dict]:
888+
def parse_overlay(parsed_obj: lief.Binary) -> dict[str, dict]:
890889
"""
891-
Parse the PE overlay section to extract dependencies
890+
Parse the overlay section to extract dotnet dependencies
892891
Args:
893-
parsed_obj (lief.PE.Binary): The parsed object representing the PE binary.
892+
parsed_obj (lief.Binary): The parsed object representing the PE binary.
894893
895894
Returns:
896895
dict: Dict representing the deps.json if available.
@@ -902,6 +901,7 @@ def parse_pe_overlay(parsed_obj: lief.PE.Binary) -> dict[str, dict]:
902901
.replace("\0", "")
903902
.replace("\n", "")
904903
.replace(" ", ""))
904+
print (overlay_str)
905905
if overlay_str.find('{"runtimeTarget') > -1:
906906
start_index = overlay_str.find('{"runtimeTarget')
907907
end_index = overlay_str.rfind('}}}')
@@ -964,9 +964,9 @@ def add_pe_metadata(exe_file, metadata, parsed_obj):
964964
metadata["exception_functions"] = parse_functions(parsed_obj.exception_functions)
965965
# Detect if this PE might be dotnet
966966
for i, dd in enumerate(parsed_obj.data_directories):
967-
if i == 14 and type(dd) == "CLR_RUNTIME_HEADER":
967+
if i == 14 and isinstance(dd, lief.PE.DataDirectory.TYPES.CLR_RUNTIME_HEADER):
968968
metadata["is_dotnet"] = True
969-
metadata["pe_dependencies"] = parse_pe_overlay(parsed_obj)
969+
metadata["dotnet_dependencies"] = parse_overlay(parsed_obj)
970970
tls = parsed_obj.tls
971971
if tls and tls.sizeof_zero_fill:
972972
metadata["tls_address_index"] = tls.addressof_index

blint/sbom.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ def process_exe_file(
356356
for entry in metadata["dynamic_entries"]:
357357
comp = create_dynamic_component(entry, exe)
358358
lib_components.append(comp)
359-
# Convert libraries and targets from PE binaries
360-
if metadata.get("pe_dependencies"):
361-
pe_components = process_pe_dependencies(metadata.get("pe_dependencies"), dependencies_dict)
359+
# Convert libraries and targets from dotnet binaries
360+
if metadata.get("dotnet_dependencies"):
361+
pe_components = process_dotnet_dependencies(metadata.get("dotnet_dependencies"), dependencies_dict)
362362
lib_components += pe_components
363363
if lib_components:
364364
components += lib_components
@@ -411,10 +411,11 @@ def create_dynamic_component(entry: Dict, exe: str) -> Component:
411411
Returns:
412412
Component: The created dynamic component object.
413413
"""
414-
purl = f"pkg:file/{entry['name']}"
414+
name = entry.get("name", "").removeprefix("$ORIGIN/")
415+
purl = f"pkg:file/{name}"
415416
comp = Component(
416417
type=Type.library,
417-
name=entry["name"],
418+
name=name,
418419
purl=purl,
419420
evidence=create_component_evidence(exe, 0.5),
420421
properties=[
@@ -458,19 +459,19 @@ def process_android_file(
458459
return components
459460

460461

461-
def process_pe_dependencies(pe_deps: dict[str, dict], dependencies_dict: dict[str, set]) -> list[Component]:
462+
def process_dotnet_dependencies(dotnet_deps: dict[str, dict], dependencies_dict: dict[str, set]) -> list[Component]:
462463
"""
463-
Process the dependencies metadata extracted for PE binaries
464+
Process the dotnet dependencies metadata extracted for binary overlays
464465
465466
Args:
466-
pe_deps (dict[str, dict]): PE dependencies metadata
467+
dotnet_deps (dict[str, dict]): PE dependencies metadata
467468
dependencies_dict (dict[str, set]): Existing dependencies dictionary
468469
469470
Returns:
470471
list: New component list
471472
"""
472473
components = []
473-
libraries = pe_deps.get("libraries", {})
474+
libraries = dotnet_deps.get("libraries", {})
474475
# k: 'Microsoft.CodeAnalysis.Analyzers/3.3.4'
475476
# v: {'type': 'package', 'serviceable': True, 'sha512': 'sha512-AxkxcPR+rheX0SmvpLVIGLhOUXAKG56a64kV9VQZ4y9gR9ZmPXnqZvHJnmwLSwzrEP6junUF11vuc+aqo5r68g==', 'path': 'microsoft.codeanalysis.analyzers/3.3.4', 'hashPath': 'microsoft.codeanalysis.analyzers.3.3.4.nupkg.sha512'}
476477
for k, v in libraries.items():
@@ -498,7 +499,7 @@ def process_pe_dependencies(pe_deps: dict[str, dict], dependencies_dict: dict[st
498499
comp.hashes = [Hash(alg=HashAlg.SHA_512, content=hash_content)],
499500
comp.bom_ref = RefType(purl)
500501
components.append(comp)
501-
targets: dict[str, dict[str, dict]] = pe_deps.get("targets", {})
502+
targets: dict[str, dict[str, dict]] = dotnet_deps.get("targets", {})
502503
for tk, tv in targets.items():
503504
for k, v in tv.items():
504505
tmp_a = k.split("/")

0 commit comments

Comments
 (0)