a
    kÄ#dþ\  ã                   @   sÊ  d Z ddlmZ ddlmZ ddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlmZ ddlmZmZmZ ddlm  mZ ddlmZmZmZ ddlmZ dd	lmZm Z m!Z!m"Z"m#Z# d
diZ$g d¢Z%dd„ Z&e'ƒ e(ƒ dfdd„Z)dd„ Z*e&dƒZ+e&dƒZ,dd„ Z-d7dd„Z.d8dd„Z/d9dd„Z0d:dd„Z1d;d!d"„Z2d#d$„ Z3d%d&„ Z4d<d)d*„Z5d=d+d,„Z6d>d-d.„Z7d?d/d0„Z8d@d2d3„Z9dddej:fd4d5„Z;e<d6k�rÆe;ƒ  dS )Az&
Main module for interactive startup.
é    )Úabsolute_import)Úprint_functionN)Úchoice)Úlog_interactiveÚlog_loadingÚScapy_Exception)ÚDefaultThemeÚBlackAndWhiteÚapply_ipython_style)ÚWINDOWS)ÚAnyÚDictÚListÚOptionalÚUnionZtlsztls.all))z0Craft packets like it is your last day on earth.zLao-Tze)z#Craft packets like I craft my beer.zJean De Clerck)z$Craft packets before they craft you.ZSocrate)zCraft me if you can.z
IPv6 layer)z`To craft a packet, you have to be a packet, and learn how to swim in the wires and in the waves.zJean-Claude Van Damme)z,We are in France, we say Skappee. OK? Merci.zSebastien Chabal)z'Wanna support scapy? Star us on GitHub!zSatoshi Nakamoto)zWhat is dead may never die!zPython 2c                 C   sB   t j t j d¡| ¡}zt  |¡ W n ty8   Y d S 0 |S d S )Nú~)ÚosÚpathÚjoinÚ
expanduserÚstatÚOSError)ÚcfZcf_path© r   úA/var/www/html/gsl.tools/lib/python3.9/site-packages/scapy/main.pyÚ_probe_config_file>   s    r   Tc              
   C   s°   t  d| ¡ zDt| ƒ�(}tt| ¡ | dƒ||ƒ W d  ƒ n1 sD0    Y  W nZ tyˆ } z |rf‚ t  d| |¡ W Y d}~n,d}~0  tyª   |rš‚ t  	d| ¡ Y n0 dS )a®  Read a config file: execute a python file while loading scapy, that
    may contain some pre-configured values.

    If _globals or _locals are specified, they will be updated with
    the loaded vars.  This allows an external program to use the
    function. Otherwise, vars are only available from inside the scapy
    console.

    params:
    - _globals: the globals() vars
    - _locals: the locals() vars
    - interactive: specified whether or not errors should be printed
    using the scapy console or raised.

    ex, content of a config.py file:
        'conf.verb = 42
'
    Manual loading:
        >>> _read_config_file("./config.py"))
        >>> conf.verb
        2

    zLoading config file [%s]ÚexecNz!Cannot read config file [%s] [%s]z+Error during evaluation of config file [%s])
r   ÚdebugÚopenr   ÚcompileÚreadÚIOErrorÚwarningÚ	ExceptionÚ	exception)r   Z_globalsZ_localsÚinteractiveZcfgfÚer   r   r   Ú_read_config_fileI   s"    
þ&"ÿr'   c                 C   s   | d dko| dvS )z5Returns whether or not a variable should be imported.r   Ú_)ÚrangeÚmapr   )Úkr   r   r   Ú_validate_localt   s    r,   z.scapy_prestart.pyz.scapy_startup.pyc                   C   s   t dƒ t d¡ d S )NzºUsage: scapy.py [-s sessionfile] [-c new_startup_file] [-p new_prestart_file] [-C] [-P] [-H]
Args:
	-H: header-less start
	-C: do not read startup file
	-P: do not read pre-startup file
r   )ÚprintÚsysÚexitr   r   r   r   Ú_usage~   s    ÿr0   c                 C   s¼   |du rt jjj}z‚t | ¡}d|jv rZ|jd D ]$}|durH| |¡ |j| ||< q2n8t  |j¡D ]*\}}t|ƒrf|durˆ| |¡ |||< qfW n" t	y¶   t
jd| dd� Y n0 dS )z¿Loads a Python module to make variables, objects and functions
available globally.

    The idea is to load the module using importlib, then copy the
symbols to the global symbol table.

    NÚ__all__zLoading module %sT)Úexc_info)ÚsixÚmovesÚbuiltinsÚ__dict__Ú	importlibÚimport_moduleÚappendÚ	iteritemsr,   r#   r   Úerror)ÚmoduleÚglobals_dictÚ	symb_listÚmodÚnameÚsymr   r   r   Ú_load�   s     	




rB   c                 C   s   t d|  ||d� dS )z[Loads a Scapy module to make variables, objects and functions
    available globally.

    zscapy.modules.©r=   r>   N)rB   ©r@   r=   r>   r   r   r   Úload_module®   s    ÿrE   c                 C   s   t dt | | ¡ ||d� dS )zaLoads a Scapy layer module to make variables, objects and functions
    available globally.

    zscapy.layers.rC   N)rB   ÚLAYER_ALIASESÚgetrD   r   r   r   Ú
load_layer¸   s    ÿrH   c                 C   sx   z$t  d|  ¡ td|  ||d� W nN tyr } z6zt| ||d� W n ty\   |‚Y n0 W Y d}~n
d}~0 0 dS )zñLoads a Scapy contrib module to make variables, objects and
    functions available globally.

    If no contrib module can be found with the given name, try to find
    a layer module, since a contrib module may become a layer module.

    úscapy.contrib.rC   N)r7   r8   rB   ÚImportErrorrH   )r@   r=   r>   r&   r   r   r   Úload_contribÂ   s    	ÿ
ÿ
rK   Fc              	   C   s  | du rd} n"d| vr0d| vr0|   d¡s0| d7 } g }tj tj t¡d¡}tjdkrttj |d| ¡} tj	| d	d
�}ntj || ¡} t 	| ¡}|D �]P}| 
tjjd¡ d¡d }| d¡r¼q�|  d¡rÒ|dd… }dd|dœ}tj|dd��²}	|	D ]œ}
|
d dk�rqò|
 d¡}|dk�rZ|d7 }|
 d|¡}|
||…  ¡ }|
|d d…  ¡ }|||< |d dk�rn �q�|d rò|d rò| |¡  �q�qòW d  ƒ n1 �s¦0    Y  |r�|d dk�rÄq�|d �rÖ|d s�td| ƒ‚q�|jdd„ d � |�rþ|S |D ]}td!| ƒ �qdS dS )"zûShow the list of all existing contribs.

    :param name: filter to search the contribs
    :param ret: whether the function should return a dict instead of
        printing it
    :returns: None or a dictionary containing the results if ret=True
    Nz*.pyÚ*ú?z.pyÚcontrib)é   é   z**T)Ú	recursiveÚ.zcontrib.é   Ú__éýÿÿÿÚ )ÚdescriptionÚstatusr@   Úreplace)Úerrorsr   ú#rI   é   ú=é   rX   ÚskiprW   z(Module %s is missing its contrib infos !c                 S   s   | d S )Nr@   r   )Úxr   r   r   Ú<lambda>  ó    zlist_contrib.<locals>.<lambda>)Úkeyz1%(name)-20s: %(description)-40s status=%(status)s)Úendswithr   r   r   ÚdirnameÚ__file__r.   Úversion_infoÚglobÚiglobrY   ÚsepÚ	partitionÚ
startswithÚior   ÚfindÚstripr9   r   Úsortr-   )r@   ÚretÚ_debugÚresultsZdir_pathÚiteratorÚfr?   ÚdescÚfdÚlineÚpÚqrc   Úvaluer   r   r   Úlist_contribØ   s^    







(ÿr|   c                 C   sV   d| vr i  | d< | d< i | d< zddl m} |ƒ j | ¡ W n tyP   Y n0 dS )z)Updates IPython session with a custom oneZ_ohÚOutÚInr   ©Úget_ipythonN)ÚIPythonr€   Úuser_nsÚupdater#   )Úsessionr€   r   r   r   Úupdate_ipython_session!  s    r…   c                   C   s"   dd„ t  t dd¡j ¡ ¡D ƒS )z"Load Scapy and return all builtinsc                 S   s   i | ]\}}t |ƒr||“qS r   )r,   )Ú.0r+   Úvr   r   r   Ú
<dictcomp>1  s   üz#_scapy_builtins.<locals>.<dictcomp>z.allÚscapy)r3   r:   r7   r8   r6   Úcopyr   r   r   r   Ú_scapy_builtins.  s
    ÿÿr‹   rV   éÿÿÿÿc                 C   sº  ddl m} ddlm}m} | s<|j} | s<|jdd� |_} t d| ¡ |s†zddl	m
} |ƒ j}W n  ty„   tjjjd }Y n0 |s˜t d	¡ d
S | dg ¡}g d¢}| ¡ }	t|	ƒD ]¨}
|	|
 }|
d dkrÜ|	|
= q¼t|dƒrú|j d¡rú|	|
= q¼t||ƒ�r|	|
= q¼|
|v �s"|
|v �r*|	|
= q¼t|ttjfƒr¼|
d dk�r^t d|
t|	|
 ƒ¡ |	|
= q¼zt | | d ¡ W n t�yŽ   Y n0 t  !| d¡}tjj" #|	||¡ | $¡  d
S )a  Save current Scapy session to the file specified in the fname arg.

    params:
     - fname: file to save the scapy session in
     - session: scapy session to use. If None, the console one will be used
     - pickleProto: pickle proto version (default: -1 = latest)r   )Úutils)ÚconfÚ	ConfClassT)ZkeepzSaving session into [%s]r   Úscapy_sessionzNo session found ?!NÚ_scpybuiltins)r�   r~   r}   r(   Ú
__module__r�   z[%s] (%s) can't be saved.z.bakÚwb)%r‰   r�   Úscapy.configrŽ   r�   r„   Zget_temp_filer   Úinfor�   r€   r‚   r#   r3   r4   r5   r6   r;   rG   rŠ   ÚlistÚhasattrr’   rl   Ú
isinstanceÚtypeÚtypesÚ
ModuleTyper"   r   Úrenamer   Úgzipr   ÚcPickleÚdumpÚclose)Úfnamer„   ZpickleProtor�   rŽ   r�   r€   ÚignoreZhard_ignoreZto_be_savedr+   Úiru   r   r   r   Úsave_session8  sT    

ÿr¤   c                    sÊ   ddl m} | du r|j} ztjj t | d¡¡}W n@ t	yt   ztjj t| dƒ¡}W n t	yn   ‚ Y n0 Y n0 tjj
jd ‰ | ‡ fdd„ˆ d D ƒ¡ ˆ  ¡  ˆ  |¡ tˆ ƒ t d	| ¡ dS )
z­Load current Scapy session from the file specified in the fname arg.
    This will erase any existing session.

    params:
     - fname: file to load the scapy session fromr   ©rŽ   NÚrbr�   c                    s   i | ]}|ˆ | “qS r   r   )r†   r+   ©r�   r   r   rˆ   …  rb   z load_session.<locals>.<dictcomp>r‘   zLoaded session [%s])r”   rŽ   r„   r3   r4   rž   Úloadr�   r   r!   r5   r6   rƒ   Úclearr…   r   r•   )r¡   rŽ   Úsr   r§   r   Úload_sessionq  s     
r«   c                 C   s€   ddl m} | du r|j} ztjj t | d¡¡}W n& t	yZ   tjj t| dƒ¡}Y n0 tjj
jd }| |¡ t|ƒ dS )z…Update current Scapy session from the file specified in the fname arg.

    params:
     - fname: file to load the scapy session fromr   r¥   Nr¦   r�   )r”   rŽ   r„   r3   r4   rž   r¨   r�   r   r!   r5   r6   rƒ   r…   )r¡   rŽ   rª   r�   r   r   r   Úupdate_session�  s    
r¬   c                 C   sž  ddl m} i }| �r8zt | ¡ W n tyB   t d| ¡ Y n°0 zRztjj	 
t | d¡¡}W n& ty†   tjj	 
t| dƒ¡}Y n0 t d| ¡ W nZ ty¸   d}t || ¡ Y n: tyÔ   t d| ¡ Y n tyð   t d| ¡ Y n0 |�r(d	|v �r | |d	 ¡ | |_||d	< n| |_n| |_d	|i}nd	|i}tƒ }| |¡ | ¡ |d
< |tjjjd< |d u�r�tjjjd  |¡ t|ƒ |�rš|S d S )Nr   r¥   zNew session [%s]r¦   zUsing existing session [%s]z5Error opening Python3 pickled session on Python2 [%s]zError opening session [%s]z-Error opening session [%s]. Attribute missingrŽ   r‘   r�   )r”   rŽ   r   r   r   r   r•   r3   r4   rž   r¨   r�   r   r!   Ú
ValueErrorr;   ÚEOFErrorÚAttributeErrorÚ	configurer„   r‹   rƒ   Úkeysr5   r6   r…   )Úsession_nameÚmydictrq   rŽ   ÚSESSIONÚmsgZscapy_builtinsr   r   r   Úinit_sessionŸ  sV    ÿ
ÿ





r¶   éN   c                 C   s´   |   d¡}|d8 }g }g }dd„ }|rr|rF||ƒt|d ƒ d |krX| | d¡¡ q"| dd |¡ ¡ g }q"|rŽ| dd |¡ ¡ g }| dd|t|ƒ d	  |f ¡ |S )
zhThis function processes a quote and returns a string that is ready
to be used in the fancy prompt.

    ú é   c                 S   s   t dd„ | D ƒƒt| ƒ d S )Nc                 s   s   | ]}t |ƒV  qd S ©N)Úlen)r†   Úeltr   r   r   Ú	<genexpr>ê  rb   z/_prepare_quote.<locals>._len.<locals>.<genexpr>r^   )Úsumr»   )rx   r   r   r   Ú_lenè  s    z_prepare_quote.<locals>._lenr   r^   z   | %sz   | %s-- %srP   )Úsplitr»   r9   Úpopr   )ÚquoteÚauthorÚmax_lenÚ_quoteÚlinesÚcur_liner¿   r   r   r   Ú_prepare_quoteÝ  s     
 "rÈ   c                    s´  t  d¡ ddlm‰  dˆ _tƒ ˆ _|dur2|ˆ _t}t	}d}|du rLt
j}zât |dd… d¡}|d D ]–\}}	|dkr‚tƒ  qj|d	kr d
ˆ _dˆ _tjˆ _qj|dkr®|	}qj|dkr¼|	}qj|dkrÊd}qj|dkrØ|	}qj|dkræd}qj|dkrjtdˆ jd ƒˆ _qjt|d ƒdk�r,t dd |d ¡ ¡‚W n< tj�yj }
 z t |
¡ t
 d¡ W Y d}
~
n
d}
~
0 0 t
jdd… t
_t|| dd�}|�rœt|dd� |�r®t|dd� ˆ j�rÆˆ j ¡ dv �r zddl}ddlm} W n: t�y   t  d¡ t!�rt  d¡ t"ƒ ˆ _d
}Y n0 d}nd
}ˆ j�rddl#m$} |ƒ �pBddk}g d¢}g d¢}ddd d!d"ˆ j% d d#d d$d g
}|�r¨|}d%d&„ |d'd(… D ƒ}dg| dg }n*t&t'ƒ\}}| (t)||d)d*�¡ | *d ¡ d+ d,d-„ t+j,j-‡ fd.d-„|D ƒ‡ fd/d-„|D ƒdd0�D ƒ¡}n
d1ˆ j% }|du�r6|d+7 }||7 }|�rŽ|d2|j.  }zdd3l/m0} W n^ t�y¸   t  d4¡ z|d
|d5| d6 gd7� W n" t1�y²   t2j3||d8� Y n0 Y nÔ0 |ƒ }zdd9lm4} |ƒ �sÚt‚W n0 t�y   t5|j6d:� d
|j6_7d|j6_8Y n0 t9|j.d ƒd;k�r:d<ˆ j% |j6_:d
|j;_<nd
|j6_=ˆ j>|j?_@||jA_Bz|||d=� W n& tCtDf�yŠ   t2j3||d8� Y n0 nt2j3||d8� ˆ jE�r°tFˆ jE|ƒ dS )>z!
    Starts Scapy's console.
    Úalwaysr   r¥   TNr^   zhs:Cc:Pp:d:Hz-hz-HFz-sz-cz-Cz-pz-Pz-dé
   zToo many parameters : [%s]r¸   )r³   rq   )r%   )ÚipythonÚauto)Ústart_ipythonzaIPython not available. Using standard Python shell instead.
AutoCompletion, History are disabled.z$On Windows, colors are also disabled)Úget_terminal_widthéT   éK   )ú&                                      z&                     aSPY//YASa       z&             apyyyyCY//////////YCa    z&            sY//////YSpcs  scpCY//Pp  z& ayp ayyyyyyySCP//Pp           syY//C z& AYAsAYYYYYYYY///Ps              cY//Sz&         pCCCCY//p          cSSps y//Yz&         SPPPP///a          pP///AC//Yz&              A//A            cyP////Cz&              p///Ac            sC///az&              P////YCpc           A//Az&       scccccp///pSP///p          p//Yz&      sY/////////y  caa           S//Pz&       cayCyayP//Ya              pY/Yaz&        sY/PsY////YCc          aC//Yp z&         sc  sccaCY//PCypaapyCP//YSs  z&                  spCPY//////YPSps    z&                       ccaacs         rÑ   )	z      .SYPACCCSASYY  zP /SCS/CCS        ACSz       /A          ACz     A/PS       /SPPSz        YP        (SCz       SPS/A.      SCz   Y/PACC          PPz    PY*AYC        CAAz         YYCY//SCYP  rV   z   |z   | Welcome to Scapyz   | Version %sz$   | https://github.com/secdev/scapyz   | Have fun!c                 S   s   g | ]}|d d… ‘qS )rS   Nr   )r†   r`   r   r   r   Ú
<listcomp>„  rb   zinteract.<locals>.<listcomp>rO   rŒ   é'   )rÄ   Ú
c                 s   s   | ]\}}|| V  qd S rº   r   )r†   ÚlogoÚbannerr   r   r   r½   Š  s   zinteract.<locals>.<genexpr>c                 3   s   | ]}ˆ j  |¡V  qd S rº   )Úcolor_themerÕ   ©r†   rx   r¥   r   r   r½   Œ  rb   c                 3   s   | ]}ˆ j  |¡V  qd S rº   )r×   ÚsuccessrØ   r¥   r   r   r½   �  rb   )Ú	fillvaluezWelcome to Scapy (%s)z using IPython %s
)ÚConfigzFtraitlets not available. Some Scapy shell features won't be available.z	print("""z"""))Zdisplay_bannerr‚   Z
exec_lines)rÖ   Úlocalr   )Úshellr¹   zScapy %s)Úconfigr‚   )GÚwarningsÚsimplefilterr”   rŽ   r%   r   r×   ZlogLevelÚDEFAULT_STARTUP_FILEÚDEFAULT_PRESTART_FILEr.   ÚargvÚgetoptr0   Zfancy_promptÚverbÚloggingÚWARNINGÚmaxr»   ÚGetoptErrorr   r   r;   r/   r¶   r'   Zinteractive_shellÚlowerr�   rÍ   rJ   r"   r   r	   Zscapy.utilsrÎ   Úversionr   ÚQUOTESÚextendrÈ   r9   r3   r4   Úzip_longestÚ__version__Ztraitlets.config.loaderrÛ   r#   ÚcodeÚinteractr€   r
   ÚTerminalInteractiveShellZconfirm_exitZseparate_inÚintZterm_title_formatZ	CompleterZuse_jediZ
term_titleZhistfileZHistoryAccessorZ	hist_fileZInteractiveShellZbanner1r¯   Ú	TypeErrorr„   r¤   )r³   rã   ZmybannerZloglevelZSTARTUP_FILEZPRESTART_FILEr²   ÚoptsÚoptÚparamrµ   r´   r�   rÍ   ZIPYTHONrÎ   Zmini_bannerZthe_logoZthe_logo_miniZ
the_bannerrÂ   rÃ   Zbanner_textrÖ   rÛ   Úcfgr€   r   r¥   r   rñ   ø  s   

ÿ
 ÿÿ
ö

ýÿ


ÿý
ÿ

rñ   Ú__main__)NN)NN)NN)NN)NFF)rV   NrŒ   )N)N)NF)r·   )=Ú__doc__Ú
__future__r   r   r.   r   rä   rð   r�   rh   r7   rm   ræ   rš   rß   Úrandomr   Zscapy.errorr   r   r   Zscapy.libs.sixZlibsr3   Zscapy.themesr   r	   r
   Zscapy.constsr   Zscapy.compatr   r   r   r   r   rF   rì   r   ÚglobalsÚlocalsr'   r,   râ   rá   r0   rB   rE   rH   rK   r|   r…   r‹   r¤   r«   r¬   r¶   rÈ   ÚINFOrñ   Ú__name__r   r   r   r   Ú<module>   sf   	ÿÿ
+





   þ
I

9

  þ
>
 T
