o
    Žõ±j+  ã                   @   sÆ   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 d dlmZ d dlmZ d d	lmZ d d
lmZmZmZmZmZmZmZmZ d dlmZmZ G dd„ dƒZG dd„ deƒZdS )é    )ÚSMBDCEFactory)Úlogger)ÚNULL©ÚEPM©Ú
DCERPCAuth©ÚDCERPCTarget)ÚSMBConnection)ÚDCERPC5Connection)Úuuidtup_to_bin)ÚRPC_C_AUTHN_LEVEL_NONEÚRPC_C_AUTHN_LEVEL_CONNECTÚRPC_C_AUTHN_LEVEL_CALLÚRPC_C_AUTHN_LEVEL_PKTÚRPC_C_AUTHN_LEVEL_PKT_INTEGRITYÚRPC_C_AUTHN_LEVEL_PKT_PRIVACYÚDCERPCExceptionÚRPC_C_AUTHN_GSS_NEGOTIATE)ÚABCÚabstractmethodc                   @   s*   e Zd Zdefdd„Zdd„ Zdd„ ZdS )ÚInterfaceEndpointNc                 C   sL   |  ¡ | _|| _|| _|| _|| _d | _| jdkr"| jd u r$tdƒ‚d S d S )NÚncan_npz4pipename must be specified for ncan_np endpoint type)ÚlowerÚetypeÚuuidÚversionÚpipenameÚ	authlevelÚ_smb_connection_to_closeÚ
ValueError)Úselfr   r   r   r   r   © r#   ú—/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/aiosmb/dcerpc/v5/interfaces/__init__.pyÚ__init__   s   
ÿzInterfaceEndpoint.__init__c                 C   s   t | j| jfƒS ©N)r   r   r   ©r"   r#   r#   r$   Úservice_uuid    s   zInterfaceEndpoint.service_uuidc                 C   s   d| j | j| jf S )Nz
(%s,%s,%s))r   r   r   r'   r#   r#   r$   Ú__str__#   s   zInterfaceEndpoint.__str__)Ú__name__Ú
__module__Ú__qualname__r   r%   r(   r)   r#   r#   r#   r$   r      s    r   c                   @   sÒ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zeed	d
„ ƒƒZ	edd„ ƒZ
edd„ ƒZdd„ Zed!dededefdd„ƒZededefdd„ƒZed"dededefdd„ƒZedd„ ƒZed#dededefdd „ƒZdS )$ÚInterfaceManagerc                 C   s   || _ || _d S r&   )ÚdceÚendpoint)r"   Ú
connectionr/   r#   r#   r$   r%   '   s   
zInterfaceManager.__init__c                 Ã   s   �| S r&   r#   r'   r#   r#   r$   Ú
__aenter__+   s   €zInterfaceManager.__aenter__c                 Ã   s   �|   ¡ I d H  d S r&   )Úclose)r"   Úexc_typeÚexcÚ	tracebackr#   r#   r$   Ú	__aexit__.   s   €zInterfaceManager.__aexit__c              
   Ã   s|   �z&z	|   ¡ I d H  W n   Y | j ¡ I d H  | jd ur%| j ¡ I d H  W dS  ty= } z
d |fW  Y d }~S d }~ww )N©TN)Úcleanupr.   Ú
disconnectr    Ú	Exception)r"   Úer#   r#   r$   r2   1   s   €
€ÿzInterfaceManager.closec                 C   ó   t dƒ‚©Nz&Must be implemented in the child class©ÚNotImplementedError©Úclsr#   r#   r$   Ú	endpoints@   s   zInterfaceManager.endpointsc                 Ã   s   �	 dS )z#Called when the object is destroyedNr#   r@   r#   r#   r$   r8   E   s   €zInterfaceManager.cleanupc                 C   r<   r=   r>   )r0   r/   r#   r#   r$   Úcreate_instanceK   s   z InterfaceManager.create_instancec              
   Ã   s~   �z'| j  ¡ I d H \}}|d ur|‚| j  | j ¡ ¡I d H \}}|d ur&|‚W dS  ty> } z
d|fW  Y d }~S d }~ww )Nr7   F)r.   ÚconnectÚbindr/   r(   r:   )r"   Ú_Úerrr;   r#   r#   r$   rD   O   s   €€ÿzInterfaceManager.connectNTr0   r/   rD   c           	   
   Ã   s”   �z2|d u r|   ¡ d }|  ||¡}|j |j¡ ||_|du r/| ¡ I d H \}}|d ur/|‚|d fW S  tyI } z
d|fW  Y d }~S d }~ww )Nr   TF)rB   rC   r.   Úset_auth_levelr   r    rD   r:   )	rA   r0   r/   rD   Úsmb_connection_to_closeÚservicerF   rG   r;   r#   r#   r$   Úfrom_rpcconnection]   s    €
€ÿz#InterfaceManager.from_rpcconnectionc              
   Ã   s  �zlt j|j ¡ |jjd�}| ¡ I d H \}}|d ur|‚|4 I d H š | | ¡ ¡I d H \}}|d ur6|‚W d   ƒI d H  n1 I d H sFw   Y  tj	||j ¡ |jj|jj
|jjd�}t |j¡}t||ƒ}	|	d fW S  tyƒ }
 z
d |
fW  Y d }
~
S d }
~
ww )N©Úproxies©ÚhostnamerM   Údc_ipÚdomain)r   Úfrom_addressÚtargetÚget_hostname_or_iprM   rD   Úmapr(   r
   Úfrom_connection_stringrP   rQ   r   Úfrom_smb_gssapiÚgssapir   r:   )rA   r0   r/   ÚepmrF   rG   Ú	constringrS   Údcerpc_authÚrpc_connectionr;   r#   r#   r$   Úswitch_protocolp   s2   €(ýû

€ÿz InterfaceManager.switch_protocolc              
   Ã   sê   �z]d }|d u r|   ¡ d }|jdkr%|  ||¡I d H \}}|d ur$|‚n!|jdu r;| ¡ I d H \}}|d ur9|‚|}t||jd�}| ¡ }| j||||d�I d H \}	}|d urZ|‚|	d fW S  t	yt }
 z
d |
fW  Y d }
~
S d }
~
ww )Nr   r   F)Úfilename)r/   rD   rI   )
rB   r   r]   Úlogin_okÚloginr   r   Úget_dce_rpcrK   r:   )rA   r0   r/   rD   rI   ÚrpctransportrG   rF   Úrpctransport_factoryrJ   r;   r#   r#   r$   Úfrom_smbconnection‹   s<   €
ÿ
ü
€ÿz#InterfaceManager.from_smbconnectionc                 C   s,   |  ¡ }|  ¡ D ]}|j|kr|  S qd S r&   )r   rB   r   )rA   r   Úepr#   r#   r$   Úget_endpoint°   s   
ÿzInterfaceManager.get_endpointÚendpoint_typec	              
   Ã   sê  �zÝ|d u r|d u r|   ¡ d }n|  |¡}|jdkrXddlm}	 |	j||||d|dd�}
|
 ¡ }| ¡ I d H \}}|d ur@|‚| j|||d�I d H \}}|d urS|‚|d fW S ddl	m
} dd	lm} dd
lm} |j||d�}| ¡ I d H \}}|d ur€|‚|4 I d H š | | ¡ ¡I d H \}}|d ur™|‚W d   ƒI d H  n1 I d H s©w   Y  |j|||d d d�}|j||d|dd�}t||ƒ}| j|||d�I d H \}}|d urÚ|‚|d fW S  tyô } z
d |fW  Y d }~S d }~ww )Nr   r   )ÚSMBConnectionFactoryÚpasswordÚsmb3)Úip_or_hostnameÚusernameÚsecretrQ   Ú
secrettyperM   Údialect)r/   rD   r   r   r	   rL   rN   Úntlm)rl   rm   rn   rQ   Ú	authproto)rB   rf   r   Ú!aiosmb.commons.connection.factoryrh   Úfrom_componentsÚget_connectionr`   rd   Ú'aiosmb.dcerpc.v5.interfaces.endpointmgrr   Ú1aiosmb.dcerpc.v5.common.connection.authenticationr   Ú)aiosmb.dcerpc.v5.common.connection.targetr
   rR   rD   rU   r(   rV   r   rK   r:   )rA   rS   rl   ri   rQ   r/   rg   rD   rM   rh   Úfactoryr0   rF   rG   rJ   r   r   r
   rY   rZ   r[   r\   r;   r#   r#   r$   Úfrom_ntlm_params¸   sx   €

ù

(ýûû

€ÿz!InterfaceManager.from_ntlm_params)NTN)NT)NNTN)r*   r+   r,   r%   r1   r6   r2   Úclassmethodr   rB   r8   ÚstaticmethodrC   rD   r   r   ÚboolrK   r   r]   rd   rf   Ústrry   r#   r#   r#   r$   r-   &   s.    

$
r-   N) Ú0aiosmb.dcerpc.v5.common.connection.smbdcefactoryr   Úaiosmbr   Úaiosmb.dcerpc.v5.dtypesr   ru   r   rv   r   rw   r
   Úaiosmb.connectionr   Úaiosmb.dcerpc.v5.connectionr   Úaiosmb.dcerpc.v5.uuidr   Úaiosmb.dcerpc.v5.rpcrtr   r   r   r   r   r   r   r   Úabcr   r   r   r-   r#   r#   r#   r$   Ú<module>   s    (