
    Xff7                        d Z ddlZddlZddlZddlmZmZmZmZ ddl	m
Z
 ddlmZmZ ddlmZ ddlmZ ddlmZ  ej(                  e      Z G d	 d
ej.                  j0                        Zdeej4                  j6                     dee   fdZy)aP  This module attempts to automatically stack layers.

This automagic module fulfills :class:`~volatility3.framework.interfaces.configuration.TranslationLayerRequirement` that are not already fulfilled, by attempting to
stack as many layers on top of each other as possible.  The base/lowest layer is derived from the
"automagic.general.single_location" configuration path.  Layers are then attempting in likely height order, and
once a layer successfully stacks on top of the existing layers, it is removed from the possible choices list
(so no layer type can exist twice in the layer stack).
    N)ListOptionalTupleType)	framework)
interfaces	constants)construct_layers)requirements)physicalc                       e Zd ZdZdZd fdZ	 ddej                  j                  de	dej                  j                  d	ej                  deee	      f
d
Zdej                  j                  de	dej                  j                  d	ej                  ddf
dZe	 	 ddej                  j                  de	deeej(                  j*                        d	ej                  fd       Zdeeej(                  j*                        fdZedej                  j                  de	dej                  j                  dee	   deee	e	f      f
d       Zedeej                  j                     fd       Z xZS )LayerStackera  Builds up layers in a single stack.

    This class mimics the volatility 2 style of stacking address spaces.  It builds up various layers based on
    separate :class:`~volatility3.framework.interfaces.automagic.StackerLayerInterface` classes.  These classes are
    built up based on a `stack_order` class variable each has.

    This has a high priority to provide other automagic modules as complete a context/configuration tree as possible.
    Upon completion it will re-call the :class:`~volatility3.framework.automagic.construct_layers.ConstructionMagic`,
    so that any stacked layers are actually constructed and added to the context.
    
   returnNc                 2    t        |   |i | d | _        y N)super__init___cached)selfargskwargs	__class__s      Y/home/panchajanya/Development/work/volatility3/volatility3/framework/automagic/stacker.pyr   zLayerStacker.__init__*   s    $)&)    contextconfig_pathrequirementprogress_callbackc                    t        j                  t        j                  d          |j	                  ||      sy| j	                  | j
                  | j                        }|r#t        j                  d|        t        |      S | j                  r| j                  j                  dd      st        d      | j                  ||||       |	 |dd       y)z)Runs the automagic over the configurable.volatility3.framework.layersNz5Unable to run LayerStacker, unsatisfied requirement: single_locationzBUnable to run LayerStacker, single_location parameter not providedd   zStacking attempts finished)r   import_filessysmodulesunsatisfiedr   r   volloginfolistconfigget
ValueErrorstack)r   r   r   r   r   r'   s         r   __call__zLayerStacker.__call__.   s     	s{{+IJK &&w< &&t||T5E5EFKKG}U $${{$++//2CT"JT 
 	

7K6GH(c#?@r   c                    | j                   r^| j                   \  }}| j                  ||||g      }|r1|\  }}	|j                  j                  ||       ||j                  |<   yd| _         |j	                         }
| j                  j                  dd      }|j                  j                  d      }t        j                  j                  |d|      }||
j                  t        j                  j                  |d      <   t        j                  |
||      }|
j                  |       | j                  |
|| j                         |      }|| j                  |
|||      }|r|\  }}|j                  j                  ||
j                  |   j!                                t#        j$                  |t        j                  j                  | j&                  d            } ||||       |j                  j                  |d      |j                  j)                  |      f| _         t*        j-                  d|j.                          t*        j-                  d|        y)	aw  Stacks the various layers and attaches these to a specific
        requirement.

        Args:
            context: Context on which to operate
            config_path: Configuration path under which to store stacking data
            requirement: Requirement that should have layers stacked on it
            progress_callback: Function to provide callback progress
        Nr"   	FileLayerr.   locationConstructionMagicz physical_layer maximum_address: zStacked layers: )r   find_suitable_requirementsr+   mergecloner,   layersfree_layer_namer   configuration	path_joinr   r1   	add_layerstack_layercreate_stackers_listbuild_configurationr
   r3   r   branchr(   debugmaximum_address)r   r   r   r   r   top_layer_name	subconfigresultappropriate_config_path
layer_namenew_contextr2   current_layer_namecurrent_config_pathphysical_layerstacked_layerspathlayerconstructors                      r   r.   zLayerStacker.stackP   sL   " <<(,%NI44kN3CF 6<3'$$%<iH:H67DLmmo;;??#4d; %^^;;KH(66@@"4
  	$$../BJO	
 "++,.@
 	n-))%%'	
 %44[+~F $e$$+,,U3GGI
 /@@,,66((*= G[+>  '~~11$=w~~?T?T@   	.~/M/M.NO	
 	''789r   initial_layer	stack_setc           	      :   d}|g}|6t        t        j                  t        j                  j
                              }|D ]?  }t        |t        j                  j
                        r(t        d|j                   d       |rd}d}d}	|D ]  }	 |	       }
	 t        j                  t        j                  d|	j                          |
j                  |||      }|r[|j                  j                  |       t        j                  t        j                  d|j                    d|	j                           n d}|r1|	r/|j                   g|z   }|j                   }d}|j5                  |	       |r|S # t"        $ r}t$        j&                  j)                  |      j+                  d	      }t        j                  t        j,                  d
t/        |              t        j                  t        j0                  dj3                  |             Y d}~d}~ww xY w)a  Stacks as many possible layers on top of the initial layer as can be done.

        WARNING: This modifies the context provided and may pollute it with unnecessary layers
        Recommended use is to:
        1. Pass in context.clone() instead of context
        2. When provided the layer list, choose the desired layer
        3. Build the configuration using layer.build_configuration()
        4. Merge the configuration into the original context with context.config.merge()
        5. Call Construction magic to reconstruct the layers from just the configuration

        Args:
            context: The context on which to operate
            initial_layer: The name of the initial layer within the context
            stack_set: A list of StackerLayerInterface objects in the order they should be stacked
            progress_callback: A function to report progress during the process

        Returns:
            A list of layer names that exist in the provided context, stacked in order (highest to lowest)
        TNzStacker z- is not a descendent of StackerLayerInterfaceFzAttempting to stack using zStacked z using )chainzException during stacking: 
)r*   r   class_subclassesr   	automagicStackerLayerInterface
issubclass	TypeError__name__r(   logr	   LOGLEVEL_VVr.   r7   r;   name	Exception	tracebackTracebackExceptionfrom_exceptionformatLOGLEVEL_VVVstrLOGLEVEL_VVVVjoinremove)clsr   rO   rP   r   stackedrK   stacker_item	new_layerstacker_clsstackerexcp	fulltraces                r   r<   zLayerStacker.stack_layer   s   8 '**:+?+?+U+UVI & 	LlJ,@,@,V,VW|4455bc 	 GIK(  %-NJJ!--4[5I5I4JK !(g}FW XI 00;

%11&y~~&6gk>R>R=ST  ! 4  ["+..!1N!B )  -G H # ! 	N ) < < K K!f4f(  JJ!..5c$i[A JJy66		)8LMM	Ns   BE44	H=BHHc                    t        t        j                  t        j                  j
                        d       }| j                  j                  dg       }t        |      r+g }|D ]"  }|j                  |v s|j                  |       $ |}|S )z>Creates the list of stackers to use based on the config optionc                     | j                   S r   stack_orderxs    r   <lambda>z3LayerStacker.create_stackers_list.<locals>.<lambda>   s
    !-- r   keystackers)sortedr   rT   r   rU   rV   r+   r,   lenrY   append)r   rP   stacker_listrD   rl   s        r   r=   z!LayerStacker.create_stackers_list   s     &&z';';'Q'QR'
	 {{z26|F$ +##|3MM'*+ Ir   rK   c                 8   t         j                  j                  ||j                        }t	        |t
        j                        r|j                  ||      rj|j                  j                  |d      }|D ])  }||j                  |<   |j                  ||      r%||fc S  |r||j                  |<   n,|j                  |= n||j                  j                  |d      fS |j
                  j                         D ]  \  }}	| j                  |||	|      }
|
s|
c S  y)a  Looks for translation layer requirements and attempts to apply the
        stacked layers to it.  If it succeeds it returns the configuration path
        and layer name where the stacked nodes were spliced into the tree.

        Returns:
            A tuple of a configuration path and layer name for the top of the stacked layers
                or None if suitable requirements are not found
        N)r   r9   r:   r\   
isinstancer   TranslationLayerRequirementr'   r+   r,   itemsr4   )rg   r   r   r   rK   child_config_pathoriginal_settingrF   req_namereqrD   s              r   r4   z'LayerStacker.find_suitable_requirements  s#     '44>>))
 k<#K#KL&&w<#*>>#5#56G#N "0 =J8BGNN#45&227KH0*<<=
 $8HGNN#45'89('..*<*<=NPT*UUU(55;;= 	MHc33*CF 	 r   c                 b    t        j                  ddd      t        j                  ddd      gS )Nr"   z+Specifies a base location on which to stackT)r\   descriptionoptionalrx   zList of stackers)r   URIRequirementListRequirement)rg   s    r   get_requirementszLayerStacker.get_requirements-  s=     ''&I
 ((-?$	
 		
r   )r   Nr   )NN)rY   
__module____qualname____doc__priorityr   r   r   ContextInterfacerc   r9   RequirementInterfacer	   ProgressCallbackr   r   r/   r.   classmethodr   rU   rV   r<   r=   r   r4   r   __classcell__)r   s   @r   r   r      s    	 H 9= ##44     --BB	 
 %55  
$s)	 DR:##44R: R:  --BB	R:
 %55R: 
R:h 
 MQ8<L##44L L Z11GGHI	L
 %55L L\	d:''==>	?" &##44& &  --BB	&
 S	& 
%S/	"& &P 
j&>&>&S&S!T 
 
r   r   pluginr   c                 b   | j                   j                  d      d   }t        j                  t        j
                  d          g }t        t        j                  t        j                  j                        d       D ],  }||j                  v r|j                  |j                         . |S )zVIdentifies the stackers that should be run, based on the plugin (and thus os) provided.   r!   c                     | j                   S r   rq   rs   s    r   ru   z$choose_os_stackers.<locals>.<lambda>F  s
    amm r   rv   )r   splitr   r$   r%   r&   ry   rT   r   rU   rV   exclusion_listr{   rY   )r   plugin_first_levelrD   rl   s       r   choose_os_stackersr   <  s    **005a8 3;;'EFGF"":#7#7#M#MN# ( !7!77g&&'( Mr   )r   loggingr%   r^   typingr   r   r   r   volatility3r   volatility3.frameworkr   r	   volatility3.framework.automagicr
   #volatility3.framework.configurationr   volatility3.framework.layersr   	getLoggerrY   r(   rU   AutomagicInterfacer   pluginsPluginInterfacerc   r    r   r   <module>r      s     
  . . ! 7 < < 1			8	$^
:'':: ^
B	tJ$6$6$F$FG DQTI r   