Skip to content

Commit 2f8aecc

Browse files
committed
linux_utilities show deleted fd + lsof files_only arg
1 parent 1eb8717 commit 2f8aecc

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

volatility3/framework/plugins/linux/lsof.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ def get_requirements(cls) -> List[interfaces.configuration.RequirementInterface]
137137
element_type=int,
138138
optional=True,
139139
),
140+
requirements.BooleanRequirement(
141+
name="files_only",
142+
description="Include only file descriptors of type file",
143+
optional=True,
144+
default=False,
145+
),
140146
]
141147

142148
@classmethod
@@ -145,6 +151,7 @@ def list_fds(
145151
context: interfaces.context.ContextInterface,
146152
vmlinux_module_name: str,
147153
filter_func: Callable[[int], bool] = lambda _: False,
154+
include_files_only: bool = False,
148155
) -> Iterable[FDInternal]:
149156
"""Enumerates open file descriptors in tasks
150157
@@ -167,16 +174,20 @@ def list_fds(
167174
linuxutils_symbol_table = task.vol.type_name.split(constants.BANG)[0]
168175

169176
fd_generator = linux.LinuxUtilities.files_descriptors_for_process(
170-
context, linuxutils_symbol_table, task
177+
context, linuxutils_symbol_table, task, files_only=include_files_only
171178
)
172179

173180
for fd_fields in fd_generator:
174181
yield FDInternal(task=task, fd_fields=fd_fields)
175182

176183
def _generator(self, pids, vmlinux_module_name):
177184
filter_func = pslist.PsList.create_pid_filter(pids)
185+
include_files_only = self.config.get("files_only")
178186
for fd_internal in self.list_fds(
179-
self.context, vmlinux_module_name, filter_func=filter_func
187+
self.context,
188+
vmlinux_module_name,
189+
filter_func=filter_func,
190+
include_files_only=include_files_only,
180191
):
181192
fd_user = fd_internal.to_user()
182193
yield (0, dataclasses.astuple(fd_user))

volatility3/framework/symbols/linux/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class LinuxUtilities(interfaces.configuration.VersionableInterface):
101101

102102
_version = (2, 3, 1)
103103
_required_framework_version = (2, 0, 0)
104+
deleted = " (deleted)"
104105

105106
framework.require_interface_version(*_required_framework_version)
106107

@@ -168,6 +169,7 @@ def do_get_path(cls, rdentry, rmnt, dentry, vfsmnt) -> Union[None, str]:
168169
# vfsmnt can be the vfsmount object itself (>=3.3) or a vfsmount * (<3.3)
169170
return ""
170171

172+
inode = dentry.d_inode
171173
path_reversed = []
172174
smeared = False
173175
while (
@@ -191,6 +193,7 @@ def do_get_path(cls, rdentry, rmnt, dentry, vfsmnt) -> Union[None, str]:
191193

192194
parent = dentry.d_parent
193195
dname = dentry.d_name.name_as_str()
196+
194197
# empty dentry names are most likely
195198
# the result of smearing
196199
if not dname:
@@ -204,6 +207,9 @@ def do_get_path(cls, rdentry, rmnt, dentry, vfsmnt) -> Union[None, str]:
204207
# path would be /foo/bar/baz, but bar is missing due to smear the results
205208
# returned here will show /foo//baz. Note the // for the missing dname.
206209
return f"<potentially smeared> {path}"
210+
print(path, inode.i_nlink)
211+
if inode and inode.is_readable() and inode.is_valid() and inode.i_nlink == 0:
212+
path += LinuxUtilities.deleted
207213
return path
208214

209215
@classmethod
@@ -260,7 +266,7 @@ def _get_new_sock_pipe_path(cls, context, task, filp) -> str:
260266
pre_name = name.dereference().cast(
261267
"string", max_length=255, errors="replace"
262268
)
263-
return "/" + pre_name + " (deleted)"
269+
return "/" + pre_name + LinuxUtilities.deleted
264270
else:
265271
pre_name = ""
266272

@@ -301,7 +307,7 @@ def _get_new_sock_pipe_path(cls, context, task, filp) -> str:
301307
return f"{pre_name}:[{inode.i_ino:d}]"
302308

303309
@classmethod
304-
def path_for_file(cls, context, task, filp) -> str:
310+
def path_for_file(cls, context, task, filp, files_only) -> str:
305311
"""Returns a file (or sock pipe) pathname relative to the task's root directory.
306312
307313
A 'file' structure doesn't have enough information to properly restore its
@@ -340,7 +346,7 @@ def path_for_file(cls, context, task, filp) -> str:
340346
except exceptions.InvalidAddressException:
341347
dname_is_valid = False
342348

343-
if dname_is_valid:
349+
if dname_is_valid and not files_only:
344350
ret = LinuxUtilities._get_new_sock_pipe_path(context, task, filp)
345351
else:
346352
ret = LinuxUtilities._get_path_file(task, filp)
@@ -353,6 +359,7 @@ def files_descriptors_for_process(
353359
context: interfaces.context.ContextInterface,
354360
symbol_table: str,
355361
task: interfaces.objects.ObjectInterface,
362+
files_only: bool = False,
356363
):
357364
try:
358365
files = task.files
@@ -376,7 +383,9 @@ def files_descriptors_for_process(
376383

377384
for fd_num, filp in enumerate(fds):
378385
if filp and filp.is_readable():
379-
full_path = LinuxUtilities.path_for_file(context, task, filp)
386+
full_path = LinuxUtilities.path_for_file(
387+
context, task, filp, files_only
388+
)
380389

381390
yield fd_num, filp, full_path
382391

0 commit comments

Comments
 (0)