
    vf!                     6   d dl Z d dlZd dlZd dlZd dlmZm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  ej*                  e      Z G d d	e j0                        Z G d
 de
j4                  j6                        Z G d dej:                        Zy)    N)DictList)	constants
exceptions
interfaces	renderers)requirements)configurationplugins)format_hints)intermedc                       e Zd Z ej                         Z ej                         Z ej                         Z ej                         Z ej                         Z	y)VMCSTestN)
__name__
__module____qualname__enumautoVMCS_ABORT_INVALIDVMCS_LINK_PTR_IS_NOT_FSVMCS_HOST_CR4_NO_VTXVMCS_CR3_IS_ZEROVMCS_GUEST_CR4_RESERVED     V/home/panchajanya/Development/work/volatility3/volatility3/framework/plugins/vmscan.pyr   r      sH    "'diik$499; tyy{'diikr   r   c                   @     e Zd Zddee   def fdZdedefdZ xZS )PageStartScanner
signatures	page_sizec                     t         |           t        |      st        d      t        |d         | _        |D ]%  }t        |      | j                  k7  st        d       || _        || _        y )Nz#No signatures passed to constructorr   z:Signatures of different lengths passed to PageStartScanner)super__init__len
ValueError_siglen_signatures
_page_size)selfr   r    item	__class__s       r   r#   zPageStartScanner.__init__   sq    :BCC:a=) 	D4yDLL( P 	
 &#r   datadata_offsetc              #      K   t        || j                  z  t        |      | j                        D ]:  }|||| j                  z    | j                  v s"||z   |||| j                  z    f < yw)zPScans only the start of every page, to see whether a signature is present or notN)ranger(   r$   r&   r'   )r)   r,   r-   
page_starts       r   __call__zPageStartScanner.__call__*   sw     $//)3t9doo
 	J Jdll!:;t?O?OO,j4<<&?@ 		s   AA.A.)i   )	r   r   r   r   bytesintr#   r1   __classcell__)r+   s   @r   r   r      s-    $4; $3 $	U 	 	r   r   c                   h   e Zd ZdZdZdZej                  ej                  ej                  hZ
edeej                  j                     fd       Zdej"                  j$                  dedeeef   fdZedej"                  j$                  d	ej.                  j0                  dee   fd
       Zd Zd Zy)VmscanzKScans for Intel VT-d structues and generates VM volatility configs for them)   r7   r   )   r   r   returnc                 b    t        j                  dd      t        j                  dddd      gS )	NprimaryzPhysical base memory layer)namedescriptionlog-thresholdz0Number of criteria failed to log to debug outputr7   T)r<   r=   defaultoptional)r	   TranslationLayerRequirementIntRequirement)clss    r   get_requirementszVmscan.get_requirementsB   s>     44,H ''$N		

 
	
r   contextconfig_pathc           	      j   t         j                  j                  t        j                  j                  dd            }g }|D ]  }t        j                  j                  |      j                  d      d   }t         j                  j                  |t        j                  |d      t        j                  j                  dd      |      }|j                  |        i }|D ]N  }|j                  |   }	t        j                  dt        |	j!                  d      j"                              }
|||
<   P |S )a  Enumerate all JSON files containing VMCS information and return the structures
        Signatures can be generated using data extracted using the vmcs_layout tool at
        https://github.com/google/rekall/tree/master/tools/linux/vmcs_layout

        Args:
            context: The volatility context to work against
            config_path: The location to store symbol table configurations under

        Returns:
            A dictionary of pattern bytes to the string representation of the architecture
        genericvmcs.r   )filenamez<Irevision_id)r   IntermediateSymbolTablefile_symbol_urlospathjoinbasenamesplitcreater
   	path_joinappendsymbol_spacestructpackr3   
get_symbolconstant_data)r)   rE   rF   	filenamestable_namesrK   	base_name
table_nameresultsymbol_tablerL   s              r   _gather_vmcs_structureszVmscan._gather_vmcs_structuresS   s    44DDGGLLF+
	 ! 	+H((288=a@I!99@@''V<Y/"	 A J z*	+ % 	-J"//
;L ++c,11-@NNOK #-F;	- r   rI   c                 h   |j                   j                  }t        d      }|j                  |   j	                  |j                   j
                  dz   d      dvr|t        j                  z  }|j                  dk7  r|t        j                  z  }|j                  dz  dk(  r|t        j                  z  }|j                  dk(  s|j                  dk(  r|t        j                  z  }|j                  dz  r|t        j                  z  }|r)|j                   r|j                   j#                  d      }|S g S )a  Runs tests to verify whether a block of data is a VMCS page
        Some tests based on the Hypervisor Memory Forensics paper by
        Mariano Graziano, Andrea Lanzi and Davide Balzarotti

        Args:
            context: The volatility context to be used for this call
            vmcs: The instantiated VMCS object to verify

        Returns:
            The list of failed criteria that the VMCS did not meet
        r      )s       l    i    l     |)vol
layer_namer   layersreadoffsetr   vmcs_link_ptrr   host_cr4r   	guest_cr3host_cr3r   	guest_cr4r   r<   rS   )rC   rE   rI   physical_layer_namefailed_testsfailed_lists         r   _verify_vmcs_pagezVmscan._verify_vmcs_pagey   s   & #hh11!)!>>-.33DHHOOa4GK T
 
 H777L!33H<<<LMMG#)H999LNNaT]]a%7H555L>>..H<<<LL--&++11#6K	r   c           	   #   B  K   | j                  | j                  | j                        }| j                  j                  | j                  d      }|j                  j                  d      r&| j                  j                  |j                  d      }|j                  | j                  t        t        |j                                     | j                        D ]#  \  }}	 | j                  j                  ||   t        j                  z   dz   |j                  |      }| j                  | j                  |      }|sd||   t!        j"                  |j$                  j&                        t!        j"                  |j(                        t!        j"                  |j*                        ff t-        |      | j                  d   k  r6t.        j1                  d||    d|j$                  j&                  d	d
|        & y # t2        j4                  t6        f$ r Y Dw xY ww)Nr;   memory_layer_VMCS)rj   r   r>   z
Potential z VMCS found at xz with failed criteria: )rb   rE   rF   rh   configgetscanr   listkeys_progress_callbackobjectr   BANGr<   rs   r   Hexrf   rj   eptrm   r$   vollogdebugr   InvalidAddressExceptionAttributeError)r)   
structureslayerrj   matchrI   rr   s          r   
_generatorzVmscan._generator   s    11$,,@P@PQ
##DKK	$:; <<N+LL''^(DEE #ZZLLT*//"345##
 	MFE
||**u%	6@JJ! + 
 #44T\\4H"&u-(,,TXX__=(,,TXX6(,,T^^<	  ;'4;;+GG(E):(;?488??[\J]]t  vA  uB  C/	4 66G s+   CHDG?;H?HHHHc                     t        j                  dt        fdt        j                  fdt        j                  fdt        j                  fg| j                               S )NArchitecturezVMCS Physical offsetEPTz	Guest CR3)r   TreeGridstrr   r   r   )r)   s    r   runz
Vmscan.run   sX    !!%')9)9:(()l../	 OO
 	
r   N)r   r   r   __doc___required_framework_version_versionr   r   r   r   STRICTLY_REQUIRED_TESTSclassmethodr   r   r
   RequirementInterfacerD   rE   ContextInterfacer   r   r2   rb   objectsObjectInterfacers   r   r   r   r   r   r6   r6   6   s    U"+H 	##((%% 
j&>&>&S&S!T 
 
 $!))::$IL$	eSj	$L +##44+   00+ 
c	+ +Z'R	
r   r6   )r   loggingrO   rX   typingr   r   volatility3.frameworkr   r   r   r   #volatility3.framework.configurationr	    volatility3.framework.interfacesr
   r   volatility3.framework.renderersr   volatility3.framework.symbolsr   	getLoggerr   r   IntFlagr   rh   ScannerInterfacer   PluginInterfacer6   r   r   r   <module>r      sv   
   	   N N < C 8 2			8	$*t|| *z((99 4c
W$$ c
r   