Skip to content

Commit

Permalink
Improve lnk plugin exception handling (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
JSCU-CNI authored Dec 4, 2024
1 parent b681e75 commit e728a2c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions dissect/target/plugins/os/windows/lnk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Iterator, Optional
from __future__ import annotations

from typing import Iterator

from dissect.shellitem.lnk import Lnk
from dissect.util import ts
Expand Down Expand Up @@ -34,7 +36,7 @@
)


def parse_lnk_file(target: Target, lnk_file: Lnk, lnk_path: TargetPath) -> Iterator[LnkRecord]:
def parse_lnk_file(target: Target, lnk_file: Lnk, lnk_path: TargetPath) -> LnkRecord:
# we need to get the active codepage from the system to properly decode some values
codepage = target.codepage or "ascii"

Expand Down Expand Up @@ -132,7 +134,7 @@ def check_compatible(self) -> None:

@arg("--path", "-p", dest="path", default=None, help="Path to directory or .lnk file in target")
@export(record=LnkRecord)
def lnk(self, path: Optional[str] = None) -> Iterator[LnkRecord]:
def lnk(self, path: str | None = None) -> Iterator[LnkRecord]:
"""Parse all .lnk files in /ProgramData, /Users, and /Windows or from a specified path in record format.
Yields a LnkRecord record with the following fields:
Expand Down Expand Up @@ -160,10 +162,14 @@ def lnk(self, path: Optional[str] = None) -> Iterator[LnkRecord]:
"""

for entry in self.lnk_entries(path):
lnk_file = Lnk(entry.open())
yield parse_lnk_file(self.target, lnk_file, entry)

def lnk_entries(self, path: Optional[str] = None) -> Iterator[TargetPath]:
try:
lnk_file = Lnk(entry.open())
yield parse_lnk_file(self.target, lnk_file, entry)
except Exception as e:
self.target.log.warning("Failed to parse link file %s", lnk_file)
self.target.log.debug("", exc_info=e)

def lnk_entries(self, path: str | None = None) -> Iterator[TargetPath]:
if path:
target_path = self.target.fs.path(path)
if not target_path.exists():
Expand Down

0 comments on commit e728a2c

Please sign in to comment.