
    [
#gP3                        d dl Z d dlZd dlmZmZmZmZ d dlmZm	Z	m
Z
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  ej2                  e      Z G d
 de	j8                  j:                  ej<                        Zy)    N)CallableIterableListType)	renderers
interfaceslayers
exceptions	constants)requirements)utility)format_hints)intermed)pe)	timelinerc                      e Zd ZdZdZdZdZed        Zede	j                  j                  dedede	j                  j                  d	ee	j                   j"                     d
e	j                   j"                  fd       Ze	 ddee   ded
ee	j                  j                  gef   fd       Zed
ee	j                  j                  gef   fd       Ze	 ddee   ded
ee	j                  j                  gef   fd       Zed fde	j                  j                  dededee	j                  j                  gef   d
ee	j                  j                     f
d       Zd Zd Zd Zy)PsListzALists the processes present in a particular windows memory image.)   r   r   Fc           	          t        j                  ddddg      t        j                  dd| j                  d	      t        j                  d
t
        dd      t        j                  dddd	      gS )NkernelzWindows kernelIntel32Intel64)namedescriptionarchitecturesphysicalz+Display physical offsets instead of virtualT)r   r   defaultoptionalpidz8Process ID to include (all other processes are excluded))r   element_typer   r   dumpzExtract listed processesF)r   ModuleRequirementBooleanRequirementPHYSICAL_DEFAULTListRequirementintclss    ^/home/panchajanya/Development/work/volatility3/volatility3/framework/plugins/windows/pslist.pyget_requirementszPsList.get_requirements   s     **,()4
 ++I,,	 (( V	 ++6	%
 	
    contextkernel_table_namepe_table_nameprocopen_methodreturnc           
         d}d}	 |j                   }|j                         }|j                  |t        j                  z   dz   ||j
                        }	|j                  |t        j                  z   dz   |	j                  |      }
|j                  j                  d|j                  j                  j                  d	      } ||j                  |j                    d
| d
|	j                  dd            }|
j                         D ]'  \  }}|j                  |       |j                  |       ) 	 |S # t        $ r&}t         j#                  d| d|        Y d}~|S d}~ww xY w)ab  Extracts the complete data for a process as a FileHandlerInterface

        Args:
            context: the context to operate upon
            kernel_table_name: the name for the symbol table containing the kernel's symbols
            pe_table_name: the name for the symbol table containing the PE format symbols
            proc: the process object whose memory should be output
            open_method: class to provide context manager for opening the file

        Returns:
            An open FileHandlerInterface object containing the complete data for the process or None in the case of failure
        NzInvalid process object_PEB
layer_nameoffset_IMAGE_DOS_HEADER)r6   r5   stringreplace
max_lengtherrors.z#xz.dmpzUnable to dump PE with pid z: )UniqueProcessIdadd_process_layerobjectr   BANGPebImageBaseAddressImageFileNamecastvolcountsanitize_filenamereconstructseekwrite	Exceptionvollogdebug)r(   r,   r-   r.   r/   r0   file_handleproc_idproc_layer_namepeb
dos_headerprocess_namer6   dataexcps                  r)   process_dumpzPsList.process_dump7   s|   , *	J**G"446O..!INN2V;*xx ! C !	.1DD++* ( J  --22--1177  3 L &--++,Al^1S=Q=QRT<UUYZK !+ 6 6 8  (!!$' !9   	JLL6wir$HII	Js   D+D4 4	E#=EE#Npid_listexcludec                 j    d }|xs g }|D cg c]  }||	 c}r|rfd}|S fd}|S c c}w )aw  A factory for producing filter functions that filter based on a list
        of process IDs.

        Args:
            pid_list: A list of process IDs that are acceptable, all other processes will be filtered out
            exclude: Accept only tasks that are not in pid_list

        Returns:
            Filter function for passing to the `list_processes` method
        c                      yNF _s    r)   <lambda>z*PsList.create_pid_filter.<locals>.<lambda>       r+   c                      | j                   v S Nr>   xfilter_lists    r)   r`   z*PsList.create_pid_filter.<locals>.<lambda>   s    (9(9[(Hr+   c                      | j                   vS rc   rd   re   s    r)   r`   z*PsList.create_pid_filter.<locals>.<lambda>   s    (9(9(Lr+   r]   )r(   rX   rY   filter_funcrf   rg   s        @r)   create_pid_filterzPsList.create_pid_filterr   sM     &>r"*<Qamq<H  M =   00c                     d S )a0  A factory for producing a filter function that only returns
           active, userland processes. This prevents plugins from operating on terminated
           processes that are still in the process list due to smear or handle leaks as well
           as kernel processes (System, Registry, etc.). Use of this filter for plugins searching
           for system state anomalies significantly reduces false positive in smeared and terminated
           processes.
        Returns:
            Filter function for passing to the `list_processes` method
        c                    | j                         xrs | j                  dkD  xrb | j                  dk7  xrQ | j                  dk7  xr@ | j                  j
                  dk(  xr% | j                         t        j                         k7   S )Nr      )	is_validActiveThreadsr>   InheritedFromUniqueProcessIdExitTimeQuadPartget_handle_countr   UnreadableValue)rf   s    r)   r`   z5PsList.create_active_process_filter.<locals>.<lambda>   s    JJL D!#D!!Q&D ..!3D 

##q(	D
 ""$	(A(A(CC
r+   r]   r'   s    r)   create_active_process_filterz#PsList.create_active_process_filter   s    
 	
r+   	name_listc                 j    d }|xs g }|D cg c]  }||	 c}r|rfd}|S fd}|S c c}w )a|  A factory for producing filter functions that filter based on a list
        of process names.

        Args:
            name_list: A list of process names that are acceptable, all other processes will be filtered out
            exclude: Accept only tasks that are not in name_list
        Returns:
            Filter function for passing to the `list_processes` method
        c                      yr\   r]   r^   s    r)   r`   z+PsList.create_name_filter.<locals>.<lambda>   ra   r+   c                 F    t        j                  | j                        v S rc   r   array_to_stringrD   re   s    r)   r`   z+PsList.create_name_filter.<locals>.<lambda>   s    g55aooF+Ur+   c                 F    t        j                  | j                        vS rc   r{   re   s    r)   r`   z+PsList.create_name_filter.<locals>.<lambda>   s    g55aooF&'r+   r]   )r(   rw   rY   ri   rf   rg   s        @r)   create_name_filterzPsList.create_name_filter   sT     &O	"+=Qq}q=U  '   >rk   c                      yr\   r]   r^   s    r)   r`   zPsList.<lambda>   s    er+   r5   symbol_tableri   c              #     K   |j                   |   j                  d   }|j                  |||      }|j                  d      j                  }|j                  d|      }|j                  d      j                  d      }	|j                  d|j                  j                  |	z
  d	      }
|
j                  D ]  } ||      r|  y
w)a[  Lists all the processes in the primary layer that are in the pid
        config option.

        Args:
            context: The context to retrieve required elements (layers, symbol tables) from
            layer_name: The name of the layer on which to operate
            symbol_table: The name of the table containing the kernel symbols
            filter_func: A function which takes an EPROCESS object and returns True if the process should be ignored/filtered

        Returns:
            The list of EPROCESS objects from the `layer_name` layer's PsActiveProcessHead list after filtering
        kernel_virtual_offsetr4   PsActiveProcessHead_LIST_ENTRY)object_typer6   	_EPROCESSActiveProcessLinksT)r   r6   absoluteN)r	   configmodule
get_symboladdressr@   get_typerelative_child_offsetrF   r6   r   )r(   r,   r5   r   ri   kvontkrnlmpps_aph_offset
list_entryreloffeprocr/   s               r)   list_processeszPsList.list_processes   s     0 nnZ(//0GH>>,:c>R ++,ABJJ__}_U
 "";/EE 
 #>>((61   
 ,,Dt$
 -s   CCCc              #   *  K   | j                   j                  | j                  d      }t        j                  j                  | j                   | j                  ddt        j                        }| j                   j                  |j                     }t        |t        j                  j                        st        d      | j                  | j                   |j                  |j                   | j#                  | j                  j%                  dd                   D ]  }| j                  j%                  d| j&                        s|j(                  j*                  }n9t-        |j/                  |j(                  j*                  d	
            d	   \  }}}}}d}	 | j                  d   r\| j1                  | j                   |j                   ||| j2                        }d}|r%|j5                          t7        |j8                        }d	|j:                  |j<                  |j>                  jA                  d|j>                  j(                  jB                  d      tE        jF                  |      |jH                  |jK                         |jM                         |jO                         |jQ                         |jS                         |ff  y # tT        jV                  $ r2 tX        j[                  d|j(                  j*                  dd       Y w xY ww)Nr   windowsr   )class_typesz#Primary layer is not an intel layerr   )ri   r   r   )r6   lengthDisabledr!   zError outputting filer8   r9   r:   z"Invalid process found at address: rf   z
. Skipping).r,   modulesr   r   IntermediateSymbolTablecreateconfig_pathr   r   r	   r5   
isinstanceintelIntel	TypeErrorr   symbol_table_namerj   getr$   rF   r6   listmappingrW   openclosestrpreferred_filenamer>   rq   rD   rE   rG   r   Hexrp   rt   get_session_idget_is_wow64get_create_timeget_exit_timer
   InvalidAddressExceptionrM   info)	selfr   r.   memoryr/   r6   r_   file_outputrO   s	            r)   
_generatorzPsList._generator   s    %%dkk(&;< 88??LL$**It @ 
 $$V%6%67&&,,"4"45ABB''LL$$..t{{ud/KL	 ( 
D ;;??:t/D/DE'+NN$((//!ND(($Avq! %K&;;v&"&"3"300%		#K #:K"#))+&)+*H*H&I ,,99**//$'+'9'9'='='C'C#, 0 
 %((0**--/++-))+,,.**,# ;
d 55 88K:Vs,   FLD-KLALLLLc              #      K   | j                         D ]`  }|\  }}d|d    d|d    d|d    d}|t        j                  j                  |d   f |t        j                  j                  |d	   f b y w)
Nz	Process: r    r   z (   )   	   )r   r   TimeLinerTypeCREATEDMODIFIED)r   row_depthrow_datar   s        r)   generate_timelinezPsList.generate_timeline7  s     ??$C"FH%hqk]!HQK=8A;-qQK	 7 7 ? ?!MM	 7 7 @ @(1+NN	 %s   A5A7c                 z   | j                   j                  d| j                        sdnd}t        j                  dt
        fdt
        fdt        fd| t        j                  fdt
        fd	t
        fd
t
        fdt        fdt        j                  fdt        j                  fdt        fg| j                               S )Nr   z(V)z(P)PIDPPIDrD   OffsetThreadsHandles	SessionIdWow64
CreateTimerr   zFile output)r   r   r$   r   TreeGridr&   r   r   r   booldatetimer   )r   
offsettypes     r)   runz
PsList.run>  s    T5J5JKEQV 	 !! #&*&(8(89C C c"$x001X../$ OO
 	
r+   r\   )__name__
__module____qualname____doc___required_framework_version_versionr$   classmethodr*   r   r,   ContextInterfacer   objectsObjectInterfacer   pluginsFileHandlerInterfacerW   r   r&   r   r   rj   rv   r~   r   r   r   r   r   r]   r+   r)   r   r      s   K"+H
 
6 8##448 8 	8
   008 *,,AAB8 
			0	08 8t 9>Cy26	:%%556<	= 0 
	:%%556<	=
 
, :?S	37	:%%556<	= 8  3##443 3 	3
 //0$6
3 
*$$44	53 3j@DO
r+   r   ) r   loggingtypingr   r   r   r   volatility3.frameworkr   r   r	   r
   r   #volatility3.framework.configurationr   volatility3.framework.objectsr   volatility3.framework.renderersr   volatility3.framework.symbolsr   0volatility3.framework.symbols.windows.extensionsr   volatility3.pluginsr   	getLoggerr   rM   r   PluginInterfaceTimeLinerInterfacer   r]   r+   r)   <module>r      s_   
   1 1 V V < 1 8 2 ? )			8	$~
Z//1M1M ~
r+   