
    [
#g8Z                     D   d dl Z d dlZd dlmZ d dlm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mZ d dlmZ d dlm Z m!Z!m"Z"  e jF                  e$      Z% G d dejL                  jN                  ejP                        Z)y)    N)datetime)count)IteratorListOptionalTuple)	constants
exceptions
interfaces	rendererssymbols)requirements)array_to_string)format_hints)intermed)versions)pe	shimcache)	timeliner)modulespslistvadinfoc                      e Zd ZU dZdZej                  ddfej                  ddfej                  ddfej                  ddfej                  dd	fej                  dd
fej                  ddfej                  ddfej                  ddfej                  ddfej                  ddfej                  ddfej                  ddfej                  ddfgZeeej"                  eef      ed<   g dZdeeeej0                  ef      fdZedeej:                  j<                     fd       Ze dejB                  jD                  dededefd       Z#edejB                  jD                  dedededee$jJ                     f
d       Z&edejB                  jD                  ded ed!ededee$jJ                     fd"       Z'edejB                  jD                  dededed#e(d$e(d%e(de)e$jJ                     fd&       Z*edejB                  jD                  ded ed!ededee$jJ                     fd'       Z+d( Z,d) Z-edejB                  jD                  dededed*ee   d+ede)ee(e(f      fd,       Z.y-).ShimcacheMemz5Reads Shimcache entries from the ahcache.sys AVL tree   r   r   Tzshimcache-win10-x64Fzshimcache-win10-x86zshimcache-win8-x64zshimcache-win8-x86zshimcache-win7-x64zshimcache-win7-x86zshimcache-vista-x64zshimcache-vista-x86zshimcache-2003-x86zshimcache-2003-x64zshimcache-xp-sp3-x86zshimcache-xp-sp2-x86zshimcache-xp-2003-x64zshimcache-xp-2003-x86_win_version_file_map)zntoskrnl.exezntkrnlpa.exezntkrnlmp.exezntkrpamp.exereturnc              #     K   | j                         D ]s  \  }\  }}}}}}t        |t              r#d| dt        j                  j
                  |f t        |t              sQd| dt        j                  j                  |f u y w)NzShimcache: File z	 executedz	 modified)
_generator
isinstancer   r   TimeLinerTypeACCESSEDMODIFIED)self_last_modifiedlast_update	file_paths        d/home/panchajanya/Development/work/volatility3/volatility3/framework/plugins/windows/shimcachemem.pygenerate_timelinezShimcacheMem.generate_timeline2   s      DH??CT?A?=+q!Y+x0(9=y?V?V?_?_alll-2(9=y?V?V?_?_annn	 DUs   A!B
$&B
c                    t        j                  ddddg      t        j                  dt        j                  d      t        j
                  d	t        j                  d
      t        j
                  dt        j                  d
      gS )NkernelzWindows kernelIntel32Intel64)namedescriptionarchitecturesr   r   )r0   pluginversionr   )r0   	componentr4   r   )
r   ModuleRequirementPluginRequirementr   PsListVersionRequirementr   VadInfor   Modules)clss    r*   get_requirementszShimcacheMem.get_requirements;   s{     **,()4
 **fmmY ++'//9 ++'//9
 	
    contextsymbol_tableconfig_pathc           	           j                      j                  }t        j                         di}	 t	         fdt
        j                  D              }t        j                  d|        t        j                  j                   |t        j                  j!                  dd      |t"        j$                  ||      S # t        $ r t        d      w xY w)a  Creates a shimcache symbol table

        Args:
            context: The context to retrieve required elements (layers, symbol tables) from
            symbol_table: The name of an existing symbol table containing the kernel symbols
            config_path: The configuration path within the context of the symbol table to create

        Returns:
            The name of the constructed shimcache table
        
nt_symbolsc              3   H   K   | ]  \  }}}|k(  r |       r|  yw))r?   r@   N ).0version_check	for_64bitfilenamer?   is_64bitr@   s       r*   	<genexpr>z6ShimcacheMem.create_shimcache_table.<locals>.<genexpr>d   s1      #6M9hy(!'M #s   "z)This version of Windows is not supported!zUsing shimcache table windowsr   )class_typesnative_typestable_mapping)symbol_spacenativesr   symbol_table_is_64bitnextr   r   StopIterationNotImplementedErrorvollogdebugr   IntermediateSymbolTablecreateospathjoinr   rM   )r?   r@   rA   rN   rO   symbol_filenamerJ   s   ``    @r*   create_shimcache_tablez#ShimcacheMem.create_shimcache_tableO   s      ++L9AA00,G%|4	S" #:F:\:\# O 	-o->?@//66GGLLK0!--%' 7 
 	
  	S%&QRR	Ss   $C C
layer_namekernel_symbol_tableshimcache_symbol_tablec              #     K   d}d}d}d}d}	t               }
t        j                  j                  |||      D ]  }|j                  }t
        j                  d|z         t        j                  j                  |d       D ]  }	 |j                         }|j                  |   }	 |j                  |j                         d      d	k7  r|d
k(  rt
        j                  d       c	 |j!                  |t"        j$                  z   dz   ||j                         |z         }||kD  r|j                         |z   }t'        |      D ]   }|j(                  j!                  |t"        j$                  z   dz   ||      }|dz  }||dz
  kD  rC|j                         |z   |	|z  z   }|j+                  |      sn|j-                  |      }||
v r|
j/                  |       |j(                  j!                  |t"        j$                  z   dz   ||      }|j+                  |j0                  j2                        s|j+                         s|    y# t        j                  $ r Y w xY w# t        j                  $ r Y w xY ww)a  Attempts to find the shimcache in a Windows XP memory image

        :param context: The context to retrieve required elements (layers, symbol tables) from
        :param layer_name: The name of the memory layer on which to operate.
        :param kernel_symbol_table: The name of an existing symbol table containing the kernel symbols
        :param shimcache_symbol_table: The name of a symbol table containing the hand-crafted shimcache symbols
           `      i  i(  zchecking process %dc                 J    | j                         dk(  xr | j                  dk(  S )Ns   Vad    )get_tag
Protection)xs    r*   <lambda>z4ShimcacheMem.find_shimcache_win_xp.<locals>.<lambda>   s     199;'#9#Oalla>O#Or>   rg   s   ﾭip  z$VAD magic bytes don't match DEADBEEFzunsigned intzunsigned long   SHIM_CACHE_ENTRYN)setr   r8   list_processesUniqueProcessIdrV   rW   r   r:   	list_vadsadd_process_layerlayersr
   InvalidAddressExceptionread	get_startobjectr	   BANGranger?   is_valid	translateaddvoloffset)r<   r?   r_   r`   ra   SHIM_NUM_ENTRIES_OFFSETSHIM_MAX_ENTRIESSHIM_LRU_OFFSETSHIM_HEADER_SIZESHIM_CACHE_ENTRY_SIZEseenprocesspidvadproc_layer_name
proc_layernum_entriescache_idx_ptrr&   cache_idx_valshim_entry_offsetphysical_addr
shim_entrys                          r*   find_shimcache_win_xpz"ShimcacheMem.find_shimcache_win_xpy   s      #&  %u}}33Z!4
G ))CLL.4500O&-&?&?&AO!(!@J!s}}:>QQ#:"LL)OP  R &nn*Y^^;nL#MMO&== !11 #/ A{+A$.$6$6$=$=.?/Q'%%M "Q&M$'7!';;  *+0=@B & &../@A $.$8$89J$KM$, HH]+!+!3!3!:!:.?BTT')"J
 &..z~~/D/DE %..0 $$K ,9
 "99  "99 sI   BJ I&=I&#E)J I#J "I##J &I=9J <I==J kernel_layer_nament_symbol_tablec           
   #     K   | j                  ||||| j                  d      }| j                  ||||| j                  d      }|r|sy|\  }}	|\  }
}t        j                  ||      rdnd}d}t	        |||	z   |      D ]   }| j                  |||||
|
|z   |      }|s  n |sy|j                  j                  |t        j                  z   dz   d      D ]  }|  yw)a  Implements the algorithm to search for the shim cache on Windows 2000
        (x64) through Windows 7 / 2008 R2. The algorithm consists of the following:

        1) Find the NT kernel module's .data and PAGE sections
        2) Iterate over every 4/8 bytes (depending on OS bitness) in the .data
           section and test for the following:
           a) offset represents a valid RTL_AVL_TABLE object
           b) RTL_AVL_TABLE is preceeded by an ERESOURCE object
           c) RTL_AVL_TABLE is followed by the beginning of the SHIM LRU list

        :param context: The context to retrieve required elements (layers, symbol tables) from
        :param layer_name: The name of the memory layer on which to operate.
        :param kernel_symbol_table: The name of an existing symbol table containing the kernel symbols
        :param shimcache_symbol_table: The name of a symbol table containing the hand-crafted shimcache symbols
        .dataPAGENrc   rg   rm   	ListEntry)
get_module_section_rangeNT_KRNL_MODSr   rR   ry   try_get_shim_head_at_offsetr   to_listr	   rx   )r<   r?   rA   r   r   ra   data_secmod_pagedata_sec_offsetdata_sec_sizemod_page_offsetmod_page_size	addr_size	shim_headr~   r   s                   r*   find_shimcache_win_2k3_to_7z(ShimcacheMem.find_shimcache_win_2k3_to_7   s"    2 //
 //
 X)1&)1& 66wPAVW		_}<i
F 77&!-/I 
  #--55"Y^^36HH+
J 
s   BCA Cmod_page_startmod_page_endr~   c                    |j                  |t        j                  z   dz   ||      }|j                  ||      syt        j                  dt        |              |j                  j                  |t        j                  z   dz         j                  }	t        j                  ||      rdnd}
t        j                  dt        |	       dt        |
              |	||	z
  |
z  z   }||z
  }t        j                  d	t        |      z         |j                  |t        j                  z   dz   ||      }|j                         st        j                  d
       y||j                  j                  z   }|j                  |   j                  |      sy|j                  |t        j                  z   dz   ||      }|j                         st        j                  d       yt        j                  d       |S )a  Attempts to construct a SHIM_CACHE_HEAD within a layer of the given context,
        using the provided offset within that layer, as well as the start and end offsets
        of the kernel module's `PAGE` section start and end offsets.

        If a number of validity checks are passed, this method will return the `SHIM_CACHE_HEAD`
        object. Otherwise, `None` is returned.
        _RTL_AVL_TABLENz(Candidate RTL_AVL_TABLE found at offset 
_ERESOURCE    re   zERESOURCE size: z, ERESOURCE alignment: zConstructing ERESOURCE at %szERESOURCE Invalidrm   zshim head invalidzreturning shim head)rw   r	   rx   rz   rV   rW   hexrP   get_typesizer   rR   r}   rs   )r<   r?   r@   r`   r_   r   r   r~   rtl_avl_table
ersrc_sizeersrc_alignmenteresource_rel_offeresource_offset	eresourceshim_head_offsetr   s                   r*   r   z(ShimcacheMem.try_get_shim_head_at_offset%  s   &  9>>),<<j&
 %%nlC?F}MN))22)..0<?

$ 	
 ,,W6IJ  	 	s://Fs?G[F\]	
 '6J+>/*QR!$553c:J6KKLNN)..0<?
	
 !!#LL,-!M$5$5$:$::~~j)223CDNN9>>),>>
	 !!#LL,-LL./r>   c              #   :  K   t        j                  ||      xs t        j                  ||      }|rdgn| j                  }t        j                  d|        | j                  |||||d      }| j                  |||||d      }	|r|	sy|	\  }
}|\  }}g }t        j                  dt        |
              t        j                  dt        |              |j                  j                  |t        j                  z   dz         }t        |||z   t        j                  ||      rd	nd
      D ]  }t        j                  dt        |              |j                  |t        j                  z   dz   |||      }|j!                  |
|
|z         sc|j"                  Pt        j                  dt        |j$                  j&                                |j)                  |j"                         t+        |      dk(  s n t+        |      dk7  rt        j                  d       yt        j                  ||      s|s|d   }n|s|d   }n|d   }|j,                  j/                  |t        j                  z   dz   d      D ]  }|j!                         s|  yw)a  Attempts to locate and yield shimcache entries from a Windows 8 or later memory image.

        :param context: The context to retrieve required elements (layers, symbol tables) from
        :param layer_name: The name of the memory layer on which to operate.
        :param kernel_symbol_table: The name of an existing symbol table containing the kernel symbols
        :param shimcache_symbol_table: The name of a symbol table containing the hand-crafted shimcache symbols
        zahcache.syszSearching for modules r   r   NzPAGE offset: z.data offset: SHIM_CACHE_HANDLErc   rg   z Building shim handle pointer at pointer)object_typer_   subtyper~   zFound valid shim handle @ r   z9Failed to identify two valid SHIM_CACHE_HANDLE structuresrl   r   rm   r   )r   is_windows_8_1_or_lateris_win10r   rV   rW   r   r   rP   r   r	   rx   ry   r   rR   rw   rz   headr}   r~   appendlenr   r   )r<   r?   rA   r   r   ra   is_8_1_or_latermodule_namesr   r   r   r   r   r   
shim_headshandle_typer~   shim_handle
valid_headr   s                       r*   find_shimcache_win_8_or_laterz*ShimcacheMem.find_shimcache_win_8_or_laterl  s    " #::_
 9w8 	 +:s?O?O-l^<=//
 //
 X)1&)1& 
}S%9$:;<~c/&:%;<=**33"Y^^36II
 m+..wHAa
F
 LL;CK=IJ!..2Y^^CiO,#	 ) K ##O_}5TU##/LL4S9O9O5P4QR %%k&6&67z?a')
, z?aLLTU
 --gG##AJ #AJ#AJ$..66"Y^^36HH+
J ""$  	
s   FJA*J>BJJc              #   `  K   | j                   j                  | j                  d      }| j                  | j                   |j                  | j
                        }t               }t        j                  | j                  |j                        rTt        j                  d       | j                  | j                   | j
                  |j                  |j                  |      }nt        j                  | j                   |j                        sTt        j                  | j                   |j                        s*t        j                   | j                   |j                        rSt        j                  d       | j#                  | j                   | j
                  |j                  |j                  |      }nt        j$                  | j                  |j                        s*t        j&                  | j                   |j                        rHt        j                  d       | j)                  | j                  |j                  |j                  |      }nt        j+                  d       y |D ]  }	 t        j-                  d|j.                          |j0                  |j2                  |j4                  t7        |j8                  t:              rt=        j>                  |j8                        n|j8                  |j@                  f}dtG        |      g|f  y # tB        jD                  $ r Y w xY ww)Nr-   z*Finding shimcache entries for Windows 8.0+z1Finding shimcache entries for Windows 2k3/Vista/7z#Finding shimcache entries for WinXPz:Cannot parse shimcache entries for this version of WindowszSHIM_CACHE_ENTRY type: r   )$r?   r   configr^   symbol_table_namerA   r   r   is_windows_8_or_later_contextrV   infor   r_   is_2003is_vista_or_lateris_windows_7r   is_windows_xp_sp2is_windows_xp_sp3r   warnrW   	__class__r'   r(   	exec_flagr!   	file_sizeintr   Hexr)   r
   rt   rS   )r%   r-   shimcache_table_namecentriesentryr   s          r*   r    zShimcacheMem._generator  sw    %%dkk(&;<#::LL&22D4D4D 
 G))$--9Q9QRKKDE88  !!(($G T\\6+C+CD))$,,8P8PQ$$T\\63K3KLKKKL66  !!(($G ''MM633
''f6N6NOKK=>00!!(($	G KKTUE6u6GHI''%%OO &eoos; %((9"__OO

 a&:& #  55 s+   I(L.+BL?L.L+(L.*L++L.c           	          t        j                  dt        fdt        fdt        fdt        fdt
        j                  fdt        fg| j                               S )NOrderzLast ModifiedzLast Updatez	Exec Flagz	File Sizez	File Path)	r   TreeGridr   r   boolr   r   strr    )r%   s    r*   runzShimcacheMem.run  s[    !!# (+)d#l../c" OO

 
	
r>   module_listsection_namec                    	 t        fdt        j                  j                  |||      D              }t
        j                  j                  |t        j                  j                  |d      ddt        j                        }|j                  |t        j                  z   dz   ||j                         }	|	sy|	j#                         }
	 t        fd|
j%                         D              }|j                   |j&                  z   }|j(                  j*                  }||fS # t        $ r Y yw xY w# t        $ r Y yw xY w)	a  Locates the size and offset of the first found module section
        specified by name from the list of modules.

        :param context: The context to operate on
        :param layer_name: The memory layer to read from
        :param module_list: A list of module names to search for the given section
        :param section_name: The name of the section to search for.

        :return: The offset and size of the module, if found; Otherwise, returns `None`
        c              3   R   K   | ]  }|j                   j                  v r|   y wN)BaseDllNameString)rF   moduler   s     r*   rK   z8ShimcacheMem.get_module_section_range.<locals>.<genexpr>8  s/       %%,,;	 s   $'Nr   rL   )rM   _IMAGE_DOS_HEADER)r~   c              3      K   | ]:  }j                         t        |j                        j                         k(  r| < y wr   )lowerr   Name)rF   secr   s     r*   rK   z8ShimcacheMem.get_module_section_range.<locals>.<genexpr>W  s<      %%'?388+D+J+J+LL s   A A)rS   r   r;   list_modulesrT   r   rX   rY   r   configuration	path_joinr   rM   rw   r	   rx   DllBaseget_nt_headerget_sectionsVirtualAddressMiscVirtualSize)r<   r?   rA   r_   r@   r   r   krnl_modpe_table_name
dos_header	nt_headersectionsection_offsetsection_sizes        ``       r*   r   z%ShimcacheMem.get_module_section_range"  sA   *		 %oo::Z H !88??$$..{DA @ 
 ^^INN*-@@## $ 

 ,,.		 $113 G "))G,B,BB||//|++G  		:  		s#   3D" "D1 "	D.-D.1	D=<D=N)/__name__
__module____qualname____doc___required_framework_versionr   r   r   r   r   r   r   r   is_xp_or_2003r   r   r   OsDistinguisherr   r   __annotations__r   r   r   r"   r   r+   classmethodr   r   RequirementInterfacer=   staticmethodr?   ContextInterfacer^   r   rm   r   r   r   r   r   r   r    r   r   rE   r>   r*   r   r      s   ?"+ 
		D"78			E#89		'	'/CD		'	'0DE			&:;			';<		#	#T+@A		#	#U,AB			5"67			4!56		#	#U,BC		#	#U,BC			'>?			(?@O4h&>&>c&I JK " TLo	%Y44h>?	@o 
j&>&>&S&S!T 
 
& '
##44'
'
 '
 
	'
 '
R ]%##44]% ]% !	]%
 !$]% 
),,	-]% ]%~ I##44I I 	I
 I !$I 
),,	-I IV D##44D D !	D
 D D D D 
),,	-D DL a!##44a! a! 	a!
 a! !$a! 
),,	-a! a!FCJ
 ?,##44?, ?, 	?,
 ?, #Y?, ?, 
%S/	"?, ?,r>   r   )*loggingrZ   r   	itertoolsr   typingr   r   r   r   volatility3.frameworkr	   r
   r   r   r   #volatility3.framework.configurationr   %volatility3.framework.objects.utilityr   volatility3.framework.renderersr   volatility3.framework.symbolsr   %volatility3.framework.symbols.windowsr   0volatility3.framework.symbols.windows.extensionsr   r   volatility3.pluginsr   volatility3.plugins.windowsr   r   r   	getLoggerr   rV   pluginsPluginInterfaceTimeLinerInterfacer   rE   r>   r*   <module>r     sp     	   2 2 W W < A 8 2 : J ) @ @ 
		8	$I	,:%%55y7S7S I	,r>   