
    [
#g                        d dl Z 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  e j$                  e      ZdZ	 d dlZd	Z G d dej<                  j>                        Z  G d dejB                        Z"y# e$ rb 	 d dlZ ed
 ej4                  j7                  d      D              dk  reej9                  d       n# e$ r ej;                  d        w xY wY w xY w)    N)AnyDictIterableListTuple)
interfaces	renderers)requirements)plugins)	resources)format_hintsFTc              #   2   K   | ]  }t        |        y wNint.0xs     X/home/panchajanya/Development/work/volatility3/volatility3/framework/plugins/yarascan.py	<genexpr>r      s     =AQ=   .)      zUsing yara-python modulezfNeither yara-x nor yara-python (>3.8.0) module not found, plugin (and dependent plugins) not availablec                   |     e Zd ZdZd
 fdZdededeeee	e	ef      fdZ
ed        Zed        Zed	        Z xZS )YaraScanner)      r   returnc                     t         |           |t        d      || _        t        rd | _	        y t        d t        j                  j                  d      D              dk   | _	        y )Nz No rules provided to YaraScannerc              3   2   K   | ]  }t        |        y wr   r   r   s     r   r   z'YaraScanner.__init__.<locals>.<genexpr>3   s     Ga3q6Gr   r      r   )
super__init__
ValueError_rules
USE_YARA_Xtupleyara__version__split	st_object)selfrules	__class__s     r   r%   zYaraScanner.__init__+   sg    =?@@   	 G4+;+;+A+A#+FGG&PP 	    datadata_offsetc           
   #     K   t         r| j                  j                  |      j                  D ]  }|j                  D ]o  }|j
                  D ]^  }|j                  |z   |j                   d|j                   |j                  ||j                  |j                  |j                  z    f ` q  y | j                  j                  |      D ]  }t        j                         rV|j                  D ]F  }|j                  D ]5  }|j                  |z   |j                  |j                  |j                   f 7 H m|j                  D ]  \  }}}||z   |j                  ||f   y w)Nr   )r2   )r(   r'   scanmatching_rulespatternsmatchesoffset	namespace
identifierlengthmatchYaraScanyara_returns_instancesstrings	instancesrulematched_data)	r.   r2   r3   r=   match_stringinstancer9   namevalues	            r   __call__zYaraScanner.__call__6   sB     ))$/>>$)NNL$0$8$8$OOk9$/q1A1A0BC(33 8??X__3TU	  %9 %3 ? ***5224(-(4(>(>H (+ = %

 , 7 7 ( 5 5	#  )? )6 05}}+e%3UZZuMM 0= 6s   E&E(c                 x    t         rt        j                  d|  d      S t        j                  dd|  di      S )Nzrule r1 {strings: $a = z condition: $a}n)sources)r(   yara_xcompiler*   )rB   s    r   get_rulezYaraScanner.get_ruleR   sC    >>$<TFBR"STT||4TF:JKL
 	
r1   c                     t        j                         j                  | d      5 }t        r)t        j
                  j                  |      cd d d        S t        j                  |      cd d d        S # 1 sw Y   y xY wNrb)file)	r   ResourceAccessoropenr(   rL   Rulesdeserialize_fromr*   loadfilepathfps     r   from_compiled_filezYaraScanner.from_compiled_fileZ   sb    '')..x> 	&"||44"4=	& 	& 99"%	& 	& 	&s   &A4A44A=c                 "   t        j                         j                  | d      5 }t        r:t	        j
                  |j                         j                               cd d d        S t        j
                  |      cd d d        S # 1 sw Y   y xY wrP   )	r   rS   rT   r(   rL   rM   readdecoder*   rX   s     r   	from_filezYaraScanner.from_filea   sh    '')..x> 	)"~~bggi&6&6&89	) 	) <<R(	) 	) 	)s   7B&BB)r   N)__name__
__module____qualname___versionr%   bytesr   r   r   strrH   staticmethodrN   r[   r_   __classcell__)r0   s   @r   r   r   '   s}    H	
NN(+N	%S#u,-	.N8 
 
 & & ) )r1   r   c                       e Zd ZdZdZdZeZede	e
j                  j                     fd       Zede	e
j                  j                     fd       Zedefd       Zedeeef   fd       Zd	 Zd
 Zy)r>   z6Scans kernel memory using yara rules (string or file).)r   r   r   r   c                 Z    | j                         t        j                  ddddg      gz   S )zReturns the requirements needed to run yarascan directly, combining the TranslationLayerRequirement
        and the requirements from get_yarascan_option_requirements.primaryzMemory layer for the kernelIntel32Intel64)rF   descriptionarchitectures) get_yarascan_option_requirementsr
   TranslationLayerRequirementclss    r   get_requirementszYaraScan.get_requirementsp   s;     335449()49
 
 	
r1   c                     t        j                  dddd      t        j                  dddd      t        j                  dd	d
      t        j                  ddd
      t        j                  ddd
      t        j                  dddd      gS )zReturns the requirements needed for the command lines options used by yarascan. This can
        then also be used by other plugins that are using yarascan. This does not include a
        TranslationLayerRequirement or a ModuleRequirement.insensitivez!Makes the search case insensitiveFT)rF   rm   defaultoptionalwidezMatch wide (unicode) stringsyara_stringzYara rules (as a string))rF   rm   rw   	yara_filezYara rules (as a file)yara_compiled_filezYara compiled rules (as a file)max_sizei   @z%Set the maximum size (default is 1GB))rF   rv   rm   rw   )r
   BooleanRequirementStringRequirementURIRequirementIntRequirementrq   s    r   ro   z)YaraScan.get_yarascan_option_requirements|   s     ++"?	 ++:	 **"6
 '' 4 '')=
 ''"C	=$
 $	
r1   c                 h    t        d t        j                  j                  d      D              dk   S )Nc              3   2   K   | ]  }t        |        y wr   r   r   s     r   r   z2YaraScan.yara_returns_instances.<locals>.<genexpr>   s     EAQEr   r   r"   )r)   r*   r+   r,   rq   s    r   r?   zYaraScan.yara_returns_instances   s,    E)9)9)?)?)DEENNNr1   configc                 8   d }|j                  d      W|d   }|d   dvrd| d}|j                  dd      r|dz  }|j                  dd      r|d	z  }t        j                  |      }|S |j                  d
      <t        j	                  d|d
    dt
                t        j                  |d
         }|S |j                  d      <t        j	                  d|d    dt
                t        j                  |d         }|S t        j                  d       |S )Nry   r   ){/"caseFz nocaserx   z wide asciirz   zPlain file: z - yara-x: r{   zCompiled file: z1No yara rules, nor yara rules file were specified)	getr   rN   vollogdebugr(   r_   r[   error)rr   r   r/   rB   s       r   process_yara_optionszYaraScan.process_yara_options   s4   ::m$0-(DAwj(4&{zz&%(	!zz&%(%((.E  ZZ$0LL<{(;'<K
|TU))&*=>E  ZZ,-9LL!&)=">!?{:,W  226:N3OPE  LLLMr1   c              #   <  K   | j                  t        | j                              }| j                  j                  | j                  d      }|j                  | j                  t        |            D ]$  \  }}}}dt        j                  |      |||ff & y w)Nrj   )r/   )contextscannerr   )	r   dictr   r   layersr5   r   r   Hex)r.   r/   layerr9   	rule_namerF   rG   s          r   
_generatorzYaraScan._generator   s     ))$t{{*;<##DKK	$:;.3jjLL+E*B /9 /
*FItU l&&v.	4GGG/
s   BBc                     t        j                  dt        j                  fdt        fdt        fdt
        fg| j                               S )NOffsetRule	ComponentValue)r	   TreeGridr   r   re   rd   r   )r.   s    r   runzYaraScan.run   sK    !!<++,c"% 	 OO
 	
r1   N)r`   ra   rb   __doc___required_framework_versionrc   r(   _yara_xclassmethodr   r   configurationRequirementInterfacers   ro   boolr?   r   re   r   r   r   r    r1   r   r>   r>   i   s    @"+HG	
j&>&>&S&S!T 	
 	
 *
	j&&;;	<*
 *
X Ot O O $sCx.  .H	
r1   r>   )#loggingtypingr   r   r   r   r   volatility3.frameworkr   r	   #volatility3.framework.configurationr
    volatility3.framework.interfacesr   volatility3.framework.layersr   volatility3.framework.renderersr   	getLoggerr`   r   r(   rL   ImportErrorr*   r)   r+   r,   r   infor   ScannerInterfacer   PluginInterfacer>   r   r1   r   <module>r      s   
  3 3 7 < 4 2 8			8	$
J$?)*##44 ?)Dn
w&& n
e  =!1!1!7!7!<==F/0 t	
 		 	1s+   B C*	ACC*C$$C*)C*