-
Notifications
You must be signed in to change notification settings - Fork 89
Add ProcFdPlugin for Linux process FD analysis #1628
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
base: main
Are you sure you want to change the base?
Changes from all commits
9008bd0
399be09
c91a33d
8920bc8
127eea6
ad40264
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,67 @@ | ||||||||||||||||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| from typing import TYPE_CHECKING | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| from dissect.target.exceptions import UnsupportedPluginError | ||||||||||||||||||||||||||||||||||||||||||
| from dissect.target.helpers.record import TargetRecordDescriptor | ||||||||||||||||||||||||||||||||||||||||||
| from dissect.target.plugin import Plugin, export | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if TYPE_CHECKING: | ||||||||||||||||||||||||||||||||||||||||||
| from collections.abc import Iterator | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| FileDescriptorRecord = TargetRecordDescriptor( | ||||||||||||||||||||||||||||||||||||||||||
| "linux/proc/fd", | ||||||||||||||||||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||||||||||||||||||
| ("datetime", "ts"), | ||||||||||||||||||||||||||||||||||||||||||
| ("varint", "pid"), | ||||||||||||||||||||||||||||||||||||||||||
| ("string", "name"), | ||||||||||||||||||||||||||||||||||||||||||
| ("varint", "fd"), | ||||||||||||||||||||||||||||||||||||||||||
| ("string", "link"), | ||||||||||||||||||||||||||||||||||||||||||
| ("varint", "pos"), | ||||||||||||||||||||||||||||||||||||||||||
| ("string", "flags"), | ||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| class ProcFdPlugin(Plugin): | ||||||||||||||||||||||||||||||||||||||||||
| """Linux process file descriptor plugin.""" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def check_compatible(self) -> None: | ||||||||||||||||||||||||||||||||||||||||||
| if not self.target.has_function("proc"): | ||||||||||||||||||||||||||||||||||||||||||
| raise UnsupportedPluginError("proc filesystem not available") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @export(record=FileDescriptorRecord) | ||||||||||||||||||||||||||||||||||||||||||
| def fd(self) -> Iterator[FileDescriptorRecord]: | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+34
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So you can access the plugin using
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| """Return information about open file descriptors for all processes. | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| This plugin identifies files, sockets, pipes, and other artifacts | ||||||||||||||||||||||||||||||||||||||||||
| currently in use by processes by parsing /proc/[pid]/fd and fdinfo. | ||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| Yields FileDescriptorRecord with the following fields: | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| .. code-block:: text | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ts (datetime): The modification timestamp of the fd directory. | ||||||||||||||||||||||||||||||||||||||||||
| pid (varint): The process id (pid) of the process. | ||||||||||||||||||||||||||||||||||||||||||
| name (string): The name associated to the pid. | ||||||||||||||||||||||||||||||||||||||||||
| fd (varint): The file descriptor number. | ||||||||||||||||||||||||||||||||||||||||||
| path (string): The resolved path or resource link. | ||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| pos (uint64): The current file offset from fdinfo. | ||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| flags (string): The access flags from fdinfo. | ||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||
| for process in self.target.proc.processes(): | ||||||||||||||||||||||||||||||||||||||||||
| for fd_obj in process.fd(): | ||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||
| ts = fd_obj.path.stat().st_mtime | ||||||||||||||||||||||||||||||||||||||||||
| except Exception: | ||||||||||||||||||||||||||||||||||||||||||
| ts = None | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+54
to
+57
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make the exception a bit more specific here.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| yield FileDescriptorRecord( | ||||||||||||||||||||||||||||||||||||||||||
| ts=ts, | ||||||||||||||||||||||||||||||||||||||||||
| pid=process.pid, | ||||||||||||||||||||||||||||||||||||||||||
| name=process.name, | ||||||||||||||||||||||||||||||||||||||||||
| fd=fd_obj.number, | ||||||||||||||||||||||||||||||||||||||||||
| link=fd_obj.link, | ||||||||||||||||||||||||||||||||||||||||||
| pos=int(fd_obj.info.get("pos", 0)), | ||||||||||||||||||||||||||||||||||||||||||
| flags=fd_obj.info.get("flags", "0"), | ||||||||||||||||||||||||||||||||||||||||||
| _target=self.target, | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+58
to
+67
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think passing
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,15 @@ | ||||||||
| from __future__ import annotations | ||||||||
|
|
||||||||
| from typing import TYPE_CHECKING | ||||||||
|
|
||||||||
| from dissect.target.plugins.os.unix.linux.proc import ProcPlugin | ||||||||
|
|
||||||||
| if TYPE_CHECKING: | ||||||||
| from dissect.target.filesystem import VirtualFilesystem | ||||||||
| from dissect.target.target import Target | ||||||||
|
|
||||||||
|
|
||||||||
| def test_fd(target_linux_users: Target, fs_linux_proc: VirtualFilesystem) -> None: | ||||||||
| target_linux_users.add_plugin(ProcPlugin) | ||||||||
| results = list(target_linux_users.fd()) | ||||||||
| assert len(results) == 4 | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing newline at end of file.
Suggested change
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import for the
aliassuggestion.