
     wf"                         d dl mZmZmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ  G d	 d
ej*                  j,                        Zy)    )AnyCallableIterableListTuple)
interfaces	renderers)requirements)utility)format_hints)intermed)elf)elfsc                      e Zd ZdZdZdZedeej                  j                     fd       Zeddee   deegef   fd       Ze	 dd
ej"                  j$                  dedeeeeef   fd       Zd
ej"                  j$                  defdZ	 	 	 ddeegef   dededefdZed d	fdej0                  j2                  dedeegef   dedeej"                  j$                     f
d       Zd Zy)PsListz?Lists the processes present in a particular linux memory image.   r   r   )r   r      returnc                 J   t        j                  ddg d      t        j                  dt        j                  d      t        j
                  dd	t        d
      t        j                  ddd
d      t        j                  ddd
d      t        j                  ddd
d      gS )NkernelzLinux kernel)Intel32Intel64AArch64)namedescriptionarchitecturesr   r   )r   pluginversionpidzFilter on specific process IDsT)r   r   element_typeoptionalthreadszInclude user threadsF)r   r   r"   defaultdecorate_commzXShow `user threads` comm in curly brackets, and `kernel threads` comm in square bracketsdumpzExtract listed processes)r
   ModuleRequirementPluginRequirementr   ElfsListRequirementintBooleanRequirement)clss    \/home/panchajanya/Development/work/volatility3/volatility3/framework/plugins/linux/pslist.pyget_requirementszPsList.get_requirements   s     ***?
 **DIIy ((< 	 ++2	 ++$v	 ++6	7!
 !	
    Npid_listc                 X    |xs g }|D cg c]  }||	 c}rfd}|S d S c c}w )a/  Constructs a filter function for process IDs.

        Args:
            pid_list: List of process IDs that are acceptable (or None if all are acceptable)

        Returns:
            Function which, when provided a process object, returns True if the process is to be filtered out of the list
        c                      | j                   vS N)r    )xfilter_lists    r.   filter_funcz-PsList.create_pid_filter.<locals>.filter_funcJ   s    uuK//r0   c                      yNF _s    r.   <lambda>z*PsList.create_pid_filter.<locals>.<lambda>O       r0   r:   )r-   r1   r5   r7   r6   s       @r.   create_pid_filterzPsList.create_pid_filter;   s=     >r"*<Qamq<0 "" =s   ''Ftaskr%   c                 @   |j                   }|j                  }|j                  r|j                  j                   nd}t        j                  |j
                        }|r%|j                  rd| d}n|j                  rd| d}|j                  j                  ||||f}|S )a  Extract the fields needed for the final output

        Args:
            task: A task object from where to get the fields.
            decorate_comm: If True, it decorates the comm string of user threads in curly brackets,
                           and of Kernel threads in square brackets.
                           Defaults to False.
        Returns:
            A tuple with the fields to show in the plugin output.
        r   []{})
tgidr    parentr   array_to_stringcommis_kernel_threadis_user_threadvoloffset)r-   r@   r%   r    tidppidr   task_fieldss           r.   get_task_fieldszPsList.get_task_fieldsQ   s     iihh#';;t{{A&&tyy1$$4&{$$D6}xxS$=r0   c           	      .   t         j                  j                  | j                  | j                  ddt
        j                        }|j                         }|st        j                         S d}|j                  j                         D ]  }|j                  |j                  j                  k(  s't        j                  j!                  | j                  ||||| j"                        }|r%t%        |j&                        }|j)                           |S  d}|S )zExtract the elf for the process if requested
        Args:
            task: A task object to extract from.
        Returns:
            A string showing the results of the extraction, either
            the filename used or an error.
        linuxr   )class_typeszError outputting filez,VMA start matching task start_code not found)r   IntermediateSymbolTablecreatecontextconfig_pathr   rT   add_process_layerr	   NotApplicableValuemmget_vma_itervm_start
start_coder   r)   elf_dumpopenstrpreferred_filenameclose)selfr@   elf_table_nameproc_layer_namefile_outputvfile_handles          r.   _get_file_outputzPsList._get_file_outputl   s    "99@@LL A 
 002 //11 2KWW))+ M::!3!33"&))"4"4'&		#K #&)+*H*H&I#))+ !M Mr0   
pid_filterinclude_threadsr&   c              #     K   | j                  | j                  | j                  d   ||      D ]O  }|r| j                  |      }nd}| j	                  ||      \  }}}	}
}dt        j                  |      ||	|
||ff Q yw)a  Generates the tasks list.

        Args:
            pid_filter: A function which takes a process object and returns True if the process should be ignored/filtered
            include_threads: If True, the output will also show the user threads
                             If False, only the thread group leaders will be shown
                             Defaults to False.
            decorate_comm: If True, it decorates the comm string of
                            - User threads: in curly brackets,
                            - Kernel threads: in square brackets
                           Defaults to False.
            dump: If True, the main executable of the process is written to a file
                  Defaults to False.
        Yields:
            Each rows
        r   Disabledr   N)
list_tasksrW   configrj   rQ   r   Hex)rd   rk   rl   r%   r&   r@   rg   rM   r    rN   rO   r   s               r.   
_generatorzPsList._generator   s     . OOLL$++h/_
 	D "33D9(+/+?+?m+T(FCdD  (  	s   A?Bc                      yr9   r:   r;   s    r.   r=   zPsList.<lambda>   r>   r0   rW   vmlinux_module_namer7   c              #      K   |j                   |   }|j                  d      }|j                  D ]*  } ||      r| |s|j                         E d{    , y7 w)a  Lists all the tasks in the primary layer.

        Args:
            context: The context to retrieve required elements (layers, symbol tables) from
            vmlinux_module_name: The name of the kernel module on which to operate
            filter_func: A function which takes a process object and returns True if the process should be ignored/filtered
            include_threads: If True, it will also return user threads.
        Yields:
            Task objects
        	init_task)symbol_nameN)modulesobject_from_symboltasksget_threads)r-   rW   rt   r7   rl   vmlinuxrv   r@   s           r.   ro   zPsList.list_tasks   si     $ //"56..;.G	 OO 	.D4 J++---	. .s   A A A AA c           	         | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                  |      }dt        j                  fdt
        fdt
        fdt
        fd	t        fd
t        fg}t        j                  || j                  ||||            S )Nr    r#   r%   r&   z
OFFSET (V)PIDTIDPPIDCOMMzFile output)
rp   getr?   r   rq   r+   ra   r	   TreeGridrr   )rd   pidsrl   r%   r&   r7   columnss          r.   runz
PsList.run   s    {{u%++//)48{{v&,,T2 <++,CLCLSMSMC 
 !!T__[/=RVW
 	
r0   r4   )F)FFF)__name__
__module____qualname____doc___required_framework_version_versionclassmethodr   r   configurationRequirementInterfacer/   r+   r   r   boolr?   objectsObjectInterfacer   ra   rQ   rj   rr   rW   ContextInterfacer   ro   r   r:   r0   r.   r   r      s   I"+H"
j&>&>&S&S!T "
 "
H #c #hud{>S # #* MR%%55FJ	sCc!	" 4'Z%7%7%G%G 'C 'X !&#(cUD[)( ( 	(
 (T 
 .= %.##44. !. seTk*	.
 . 
*$$44	5. .>
r0   r   N)typingr   r   r   r   r   volatility3.frameworkr   r	   #volatility3.framework.configurationr
   volatility3.framework.objectsr   volatility3.framework.renderersr   volatility3.framework.symbolsr   .volatility3.framework.symbols.linux.extensionsr   volatility3.plugins.linuxr   pluginsPluginInterfacer   r:   r0   r.   <module>r      s<    8 7 7 < 1 8 2 > *a
Z// a
r0   