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 sw   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    (