-
Notifications
You must be signed in to change notification settings - Fork 591
Add Stack Plugin #1816
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
Open
the-rectifier
wants to merge
1
commit into
volatilityfoundation:develop
Choose a base branch
from
the-rectifier:stack
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Stack Plugin #1816
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import struct | ||
|
|
||
| from typing import List | ||
|
|
||
| from volatility3.framework.symbols.windows import extensions | ||
| from volatility3.framework.configuration import requirements | ||
| from volatility3.framework import interfaces, renderers, constants | ||
| from volatility3.framework.renderers import format_hints | ||
| from volatility3.plugins.windows import pslist | ||
|
|
||
|
|
||
| class Stack(interfaces.plugins.PluginInterface): | ||
| """Lists the Stack boundaries and dump Stack""" | ||
|
|
||
| _required_framework_version = (2, 0, 0) | ||
| _version = (3, 0, 1) | ||
|
|
||
| @classmethod | ||
| def get_requirements(cls): | ||
| return [ | ||
| requirements.ModuleRequirement( | ||
| name="kernel", | ||
| description="Windows Kernel", | ||
| architectures=["Intel32", "Intel64"], | ||
| ), | ||
| requirements.VersionRequirement( | ||
| name="pslist", component=pslist.PsList, version=(3, 0, 0) | ||
| ), | ||
| requirements.ListRequirement( | ||
| name="pid", | ||
| element_type=int, | ||
| description="Process IDs to operate on", | ||
| optional=False, | ||
| ), | ||
| requirements.BooleanRequirement( | ||
| name="dump", | ||
| description="Whether to dump the stack", | ||
| default=False, | ||
| optional=True, | ||
| ), | ||
| ] | ||
|
|
||
| def _generator(self): | ||
| file_output = "Disabled" | ||
|
|
||
| kernel = self.context.modules[self.config["kernel"]] | ||
| filter_func = pslist.PsList.create_pid_filter(self.config.get("pid"), None) | ||
|
|
||
| procs: List[extensions.EPROCESS] = pslist.PsList.list_processes( | ||
| context=self.context, | ||
| kernel_module_name=self.config["kernel"], | ||
| filter_func=filter_func, | ||
| ) | ||
|
|
||
| for proc in procs: | ||
| proc_layer_name = proc.add_process_layer() | ||
| proc_layer = self.context.layers[proc_layer_name] | ||
|
|
||
| thread_list: List[extensions.ETHREAD] = list( | ||
| proc.ThreadListHead.to_list( | ||
| f"{kernel.symbol_table_name}{constants.BANG}_ETHREAD", | ||
| "ThreadListEntry", | ||
| ) | ||
| ) | ||
|
|
||
| # no need to parse the time | ||
| active_thread_list: List[extensions.ETHREAD] = [ | ||
| t for t in thread_list if t.ExitTime.QuadPart < 0 | ||
| ] | ||
|
|
||
| for thread in active_thread_list: | ||
| trap_frame = thread.Tcb.TrapFrame.dereference() | ||
| thread_rsp = trap_frame.Rsp | ||
|
|
||
| # first entry of TEB is NT_TIB | ||
| # Stack Base is at NT_TIB + 8 | ||
| # https://github.com/wine-mirror/wine/blob/master/include/winternl.h#L494 | ||
| # https://github.com/wine-mirror/wine/blob/master/include/winnt.h#L2445 | ||
| stack_base = struct.unpack( | ||
| "Q", proc_layer.read(offset=thread.Tcb.Teb + 8, length=8) | ||
| )[0] | ||
|
|
||
| stack_size = stack_base - thread_rsp | ||
|
|
||
| if self.config["dump"]: | ||
| fname = f"{proc.UniqueProcessId}.{thread.Cid.UniqueThread}.dmp" | ||
| stack = proc_layer.read(offset=thread_rsp, length=stack_size) | ||
|
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. This is a reasonable use of read, because it's a big chunk of unstructured (or unknown structure) data, that you just want to write out as a big blob and aren't going to use again inside volatility. |
||
| with self.open(fname) as f: | ||
| f.write(stack) | ||
|
|
||
| file_output = fname | ||
|
|
||
| yield ( | ||
| 0, | ||
| ( | ||
| proc.UniqueProcessId, | ||
| thread.Cid.UniqueThread, | ||
| format_hints.Hex(thread.vol.offset), | ||
| format_hints.Hex(thread_rsp), | ||
| format_hints.Hex(stack_base), | ||
| format_hints.Hex(stack_size), | ||
| file_output, | ||
| ), | ||
| ) | ||
|
|
||
| def run(self): | ||
| return renderers.TreeGrid( | ||
| [ | ||
| ("PID", int), | ||
| ("TID", int), | ||
| ("Thread", format_hints.Hex), | ||
| ("RSP", format_hints.Hex), | ||
| ("Stack Base", format_hints.Hex), | ||
| ("Stack Size", format_hints.Hex), | ||
| ("File Output", str), | ||
| ], | ||
| self._generator(), | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please try to avoid reading bytes directly. Either make a struct or at least instantiate a new object of the right type for windows on it. Having to 0 index the results of unpack basically throws away all the extra goodness we provide through the volatility model (keeping track of the type, the offset, which layer it came from, etc).
The entire volatility framework is a giant parser, so if you're resorting to struct.unpack yourself please consider if there's no other way of doing it (or if you're doing it so often efficiency comes into question, which ones per thread, isn't the case here).