|
1 | | -from volatility3.plugins import envvars |
| 1 | +# This file is Copyright 2022 Volatility Foundation and licensed under the Volatility Software License 1.0 |
| 2 | +# which is available at https://www.volatilityfoundation.org/license/vsl-v1.0 |
| 3 | +# |
| 4 | + |
2 | 5 | import logging |
3 | 6 |
|
| 7 | +from volatility3.framework import exceptions, renderers |
| 8 | +from volatility3.framework.configuration import requirements |
| 9 | +from volatility3.framework.interfaces import plugins |
| 10 | +from volatility3.framework.objects import utility |
| 11 | +from volatility3.plugins.linux import pslist |
| 12 | + |
4 | 13 | vollog = logging.getLogger(__name__) |
5 | 14 |
|
6 | 15 |
|
7 | | -class Envars(envvars.Envvars): |
8 | | - def run(self, *args, **kwargs): |
9 | | - vollog.warning( |
10 | | - "The linux.envars plugin has been renamed to linux.envvars and will only be accessible through the new name in a future release" |
| 16 | +class Envars(plugins.PluginInterface): |
| 17 | + """Lists processes with their environment variables""" |
| 18 | + |
| 19 | + _required_framework_version = (2, 0, 0) |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def get_requirements(cls): |
| 23 | + # Since we're calling the plugin, make sure we have the plugin's requirements |
| 24 | + return [ |
| 25 | + requirements.ModuleRequirement( |
| 26 | + name="kernel", |
| 27 | + description="Linux kernel", |
| 28 | + architectures=["Intel32", "Intel64"], |
| 29 | + ), |
| 30 | + requirements.PluginRequirement( |
| 31 | + name="pslist", plugin=pslist.PsList, version=(2, 0, 0) |
| 32 | + ), |
| 33 | + requirements.ListRequirement( |
| 34 | + name="pid", |
| 35 | + description="Filter on specific process IDs", |
| 36 | + element_type=int, |
| 37 | + optional=True, |
| 38 | + ), |
| 39 | + ] |
| 40 | + |
| 41 | + def _generator(self, tasks): |
| 42 | + """Generates a listing of processes along with environment variables""" |
| 43 | + |
| 44 | + # walk the process list and return the envars |
| 45 | + for task in tasks: |
| 46 | + pid = task.pid |
| 47 | + |
| 48 | + # get process name as string |
| 49 | + name = utility.array_to_string(task.comm) |
| 50 | + |
| 51 | + # try and get task parent |
| 52 | + try: |
| 53 | + ppid = task.parent.pid |
| 54 | + except exceptions.InvalidAddressException: |
| 55 | + vollog.debug( |
| 56 | + f"Unable to read parent pid for task {pid} {name}, setting ppid to 0." |
| 57 | + ) |
| 58 | + ppid = 0 |
| 59 | + |
| 60 | + # kernel threads never have an mm as they do not have userland mappings |
| 61 | + try: |
| 62 | + mm = task.mm |
| 63 | + except exceptions.InvalidAddressException: |
| 64 | + # no mm so cannot get envars |
| 65 | + vollog.debug( |
| 66 | + f"Unable to access mm for task {pid} {name} it is likely a kernel thread, will not extract any envars." |
| 67 | + ) |
| 68 | + mm = None |
| 69 | + continue |
| 70 | + |
| 71 | + # if mm exists attempt to get envars |
| 72 | + if mm: |
| 73 | + # get process layer to read envars from |
| 74 | + proc_layer_name = task.add_process_layer() |
| 75 | + if proc_layer_name is None: |
| 76 | + vollog.debug( |
| 77 | + f"Unable to construct process layer for task {pid} {name}, will not extract any envars." |
| 78 | + ) |
| 79 | + continue |
| 80 | + proc_layer = self.context.layers[proc_layer_name] |
| 81 | + |
| 82 | + # get the size of the envars with sanity checking |
| 83 | + envars_size = task.mm.env_end - task.mm.env_start |
| 84 | + if not (0 < envars_size <= 8192): |
| 85 | + vollog.debug( |
| 86 | + f"Task {pid} {name} appears to have envars of size {envars_size} bytes which fails the sanity checking, will not extract any envars." |
| 87 | + ) |
| 88 | + continue |
| 89 | + |
| 90 | + # attempt to read all envars data |
| 91 | + try: |
| 92 | + envar_data = proc_layer.read(task.mm.env_start, envars_size) |
| 93 | + except exceptions.InvalidAddressException: |
| 94 | + vollog.debug( |
| 95 | + f"Unable to read full envars for {pid} {name} starting at virtual offset {hex(task.mm.env_start)} for {envars_size} bytes, will not extract any envars." |
| 96 | + ) |
| 97 | + continue |
| 98 | + |
| 99 | + # parse envar data, envars are null terminated, keys and values are separated by '=' |
| 100 | + envar_data = envar_data.rstrip(b"\x00") |
| 101 | + for envar_pair in envar_data.split(b"\x00"): |
| 102 | + try: |
| 103 | + key, value = envar_pair.decode().split("=", 1) |
| 104 | + except ValueError: |
| 105 | + vollog.debug( |
| 106 | + f"Unable to extract envars for {pid} {name} starting at virtual offset {hex(task.mm.env_start)}, they don't appear to be '=' separated" |
| 107 | + ) |
| 108 | + continue |
| 109 | + yield (0, (pid, ppid, name, key, value)) |
| 110 | + |
| 111 | + def run(self): |
| 112 | + filter_func = pslist.PsList.create_pid_filter(self.config.get("pid", None)) |
| 113 | + |
| 114 | + return renderers.TreeGrid( |
| 115 | + [("PID", int), ("PPID", int), ("COMM", str), ("KEY", str), ("VALUE", str)], |
| 116 | + self._generator( |
| 117 | + pslist.PsList.list_tasks( |
| 118 | + self.context, self.config["kernel"], filter_func=filter_func |
| 119 | + ) |
| 120 | + ), |
11 | 121 | ) |
12 | | - return super().run(*args, **kwargs) |
|
0 commit comments