o
    Žõ±j  ã                   @   sd   d dl Z d dlmZmZ d dlmZ d dlmZ G dd„ dƒZdd„ Z	d	d
„ Z
edkr0e
ƒ  dS dS )é    N)Ú
EnumResultÚEnumResultStatus)ÚSMBConnectionFactory)Ú
SMBMachinec                   @   sF   e Zd Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Z	dS )Ú
SMBScanneréd   é   Tc                 C   sR   g | _ t |¡| _|| _d | _d | _|| _g | _d | _	|| _
d| _d| _d| _d S )NFr   )Útarget_gensr   Úfrom_urlÚsmb_mgrÚworker_countÚtask_qÚres_qÚdepthÚworkersÚresult_processing_taskÚenum_urlÚ_SMBScanner__gens_finishedÚ_SMBScanner__total_targetsÚ_SMBScanner__total_finished)ÚselfÚsmb_urlr   r   r   © r   ú‘/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/aiosmb/commons/scanner/scanner.pyÚ__init__   s   
zSMBScanner.__init__c                 Ã   s¦  �zÀz[| j  |¡}|4 I d H šB | ¡ I d H \}}|d ur|‚t|ƒ}|j| jd�2 z3 d H W \}}}t|||||fƒ}	| j |	¡I d H  q*6 W d   ƒI d H  n1 I d H sWw   Y  W n> t	j
yy   Y W | j t||d tjd�¡I d H  d S  ty› }
 z| j t||d |
tjd�¡I d H  W Y d }
~
nd }
~
ww W | j t||d tjd�¡I d H  d S W | j t||d tjd�¡I d H  d S | j t||d tjd�¡I d H  w )N)r   )Ústatus)Úerrorr   )r   Úcreate_connection_newtargetÚloginr   Úenum_all_recursivelyr   r   r   ÚputÚasyncioÚCancelledErrorr   ÚFINISHEDÚ	ExceptionÚERROR)r   ÚtidÚtargetÚ
connectionÚ_ÚerrÚmachineÚpathÚotypeÚerÚer   r   r   Ú
__executor   s2   € þ(ù€
$ý.€ÿ€$þFzSMBScanner.__executorc              
   Ã   s”   �z*	 | j  ¡ I d H }|d u rW d S |\}}ztj|  ||¡dd�I d H  W n   Y q tjy6   Y d S  tyI } z|W  Y d }~S d }~ww )NTé
   )Útimeout)r   Úgetr!   Úwait_forÚ_SMBScanner__executorr"   r$   )r   Úindatar&   r'   r/   r   r   r   Úworker.   s$   € ÷
€ÿzSMBScanner.workerc              
   Ã   sì   �zS	 | j  ¡ I d H }|jtjkr-|  jd7  _| j| jkr-| jdu r-|  ¡ I d H  W d S |j	d urT|j	\}}}|d urHt
d|d  ¡ |f ƒ |d urTt
d||f ƒ q tjy_   Y d S  tyu } zt
|ƒ W Y d }~d S d }~ww )NTé   z[%s] %sr   z	[E] %s %s)r   r3   r   r   r#   r   r   r   Ú	terminateÚresultÚprintÚupperr!   r"   r$   )r   r.   r,   r-   r*   r/   r   r   r   Úresult_processing@   s,   €
ó€ÿzSMBScanner.result_processingc                 Ã   s2   �| j D ]}| ¡  q| jd ur| j ¡  d S d S ©N)r   Úcancelr   )r   r7   r   r   r   r9   V   s   €


ÿzSMBScanner.terminatec              
   Ã   s˜   �z4| j d u rt | j¡| _ t |  ¡ ¡| _| jd u r t ¡ | _t| jƒD ]}| j	 
t |  ¡ ¡¡ q%W dS  tyK } z
d |fW  Y d }~S d }~ww )N©TN)r   r!   ÚQueuer   Úcreate_taskr=   r   r   Úranger   Úappendr7   r$   )r   r)   r/   r   r   r   Úsetup\   s   €


€ÿzSMBScanner.setupc              
   Ã   s   �zd|   ¡ I d H \}}|d ur|‚| jdu r1|  jd7  _| j tt ¡ ƒ| j 	¡  
¡ f¡I d H  | jD ]}| | j¡I d H \}}|  j|7  _|d urRtd| ƒ q4d| _tj| jŽ I d H  tdƒ W dS  ty } zt|ƒ d |fW  Y d }~S d }~ww )NTr8   zTarget gen error! %sr@   )rE   r   r   r   r    ÚstrÚuuidÚuuid4r   Ú
get_targetÚget_hostname_or_ipr	   Úrunr;   r   r!   Úgatherr   r$   )r   r)   r*   Ú
target_genÚtotalr/   r   r   r   rK   k   s.   €
(
€€þzSMBScanner.runN)r   r   T)
Ú__name__Ú
__module__Ú__qualname__r   r5   r7   r=   r9   rE   rK   r   r   r   r   r   
   s    
r   c                  Ã   sÚ   �dd l } | jdd�}|jddtddd� |jd	d
tddd� |jdddd� |jdddd� |jddd� | ¡ }t|j|j|jd�}|j	d urR|j
 t|j	ƒ¡ |jd ur`|j
 t|jƒ¡ | ¡ I d H  tdƒ d S )Nr   zSMB Share enumerator)Údescriptionz-dz--depthr   z"Recursion depth, -1 means infinite)ÚtypeÚdefaultÚhelpz-wz--smb-worker-countr   zParallell countz-tz--targets-filez)Targets file, one ip or hostname per line)rU   z-mz	--machinez-Targetmachoine ip or hostname, can be stackedr   zƒConnection string that describes the authentication and target. Example: smb+ntlm-password://TEST\Administrator:password@10.10.10.2)r   r   zDone!)ÚargparseÚArgumentParserÚadd_argumentÚintÚ
parse_argsÚSMBFileEnumr   Úsmb_worker_countr   Útargets_filer	   rD   ÚFileTargetGenr+   ÚListTargetGenrK   r;   )rV   ÚparserÚargsÚ
enumeratorr   r   r   Úamain„   s    €

rc   c                   C   s   t  tƒ ¡ d S r>   )r!   rK   rc   r   r   r   r   Úmainš   s   rd   Ú__main__)r!   Ú#aiosmb.commons.scanner.common.commsr   r   Ú!aiosmb.commons.connection.factoryr   Ú!aiosmb.commons.interfaces.machiner   r   rc   rd   rO   r   r   r   r   Ú<module>   s   z
ÿ