o
    Žõ±jÇ'  ã                   @   sØ   d dl Z d dlmZ d dlT 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 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 	 G dd„ dƒZdd„ Zedkrjd dlZe eƒ ¡ dS dS )é    N)ÚList)Ú*)ÚDCERPC5Connection)ÚDCERPCTarget)Ú
DCERPCAuth)Ú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)ÚUniProxyTarget)Ú	UniTarget)ÚSPNEGOCredential)ÚNTLMCredential)ÚKerberosCredentialc                	   @   s  e Zd Zd,dedefdd„Zdd„ Zdd	„ Zed-de	de
dee fdd„ƒZed.de	de
f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d0dee de
de
fdd„ƒZdd„ Zd1d d!„Zefd"d#„Zd$d„ Zd%d&„ Zd'd(„ Zeeeeeƒ d)fd*d+„ZdS )2ÚEPMNÚ
connectionÚdata_representationc                 C   s&   || _ || _|d u rtdƒ| _d S d S )N)z$8a885d04-1ceb-11c9-9fe8-08002b104860z2.0)Údcer   Úuuidtup_to_bin)Úselfr   r   © r   úš/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/aiosmb/dcerpc/v5/interfaces/endpointmgr.pyÚ__init__   s
   ÿzEPM.__init__c                 Ã   s   �| S ©Nr   ©r   r   r   r   Ú
__aenter__!   s   €zEPM.__aenter__c                 Ã   s   �|   ¡ I d H  d S r   )Ú
disconnect)r   Úexc_typeÚexcÚ	tracebackr   r   r   Ú	__aexit__$   s   €zEPM.__aexit__é‡   Úncacn_ip_tcpÚportÚprotocolÚproxiesc           	      C   s>   d|| |f }t j||d�}d}t||ƒ}| t¡ t||ƒS )zF
		Sets up the EPM object from IP/hostname port protocol parameters
		ú	%s:%s[%s]©r'   N)r   Úfrom_connection_stringr   Úset_auth_typer   r   )	Úipr%   r&   r   r'   Údcerpc_target_strÚtargetÚauthr   r   r   r   Úfrom_address'   s   þ


zEPM.from_addressc                 C   sd   d|| j  ¡ |f }tj|| j j| j j| j j| j  ¡ d�}t 	| j
¡}t||ƒ}| t¡ t||ƒS )z@
		Sets up the EPM connection from an existing SMB connection
		r(   ©r'   Údc_ipÚdomainÚhostname)r.   Úget_ip_or_hostnamer   r*   r'   r2   r3   Úget_hostname_or_ipr   Úfrom_smb_gssapiÚgssapir   r+   r   r   )Úsmb_connectionr%   r&   r   r-   r.   r/   r   r   r   r   Úfrom_smbconnection6   s   û


zEPM.from_smbconnectionr.   c                 C   sl   d||   ¡ |f }tj|| j| j| j|  ¡ d�} d }|d ur#t |¡}t	|| ƒ}|d u r1| 
t¡ t||ƒS )Nr(   r1   )r5   r   r*   r'   r2   r3   r6   r   r7   r   r+   r   r   )r.   r&   r%   r   Úcredentialsr-   r/   r   r   r   r   Úfrom_unitargetH   s   û



zEPM.from_unitargetÚ
credentialc           
   
   Ã   sþ   �d }znzJt  | ¡}| ¡ I d H \}}|d ur|‚| |¡I d H \}}|d ur)|‚tj|| j| j| jd�}t	 
|¡}t||ƒd fW W |d urN| ¡ I d H  S S  tyq }	 zd|	fW  Y d }	~	W |d url| ¡ I d H  S S d }	~	ww |d ur~| ¡ I d H  w w )N©r'   r2   r3   F)r   r<   ÚconnectÚmapr   r*   r'   r2   r3   r   r7   r   r   Ú	Exception)
r.   r=   ÚremoteIfÚepmÚ_ÚerrÚresÚ	dcetargetÚdcecredÚer   r   r   Úcreate_connectionZ   s4   €

ÿýÿ€ýÿzEPM.create_connectionr2   r3   c           
   
   Ã   sè   �d }zcz?t j| |d�}| ¡ I d H \}}|d ur|‚| |¡I d H \}}|d ur+|‚tj||||d�d fW W |d urC| ¡ I d H  S S  tyf }	 zd|	fW  Y d }	~	W |d ura| ¡ I d H  S S d }	~	ww |d urs| ¡ I d H  w w )Nr)   r>   F)r   r0   r?   r@   r   r*   r   rA   )
r,   rB   r'   r2   r3   rC   rD   rE   rF   rI   r   r   r   Úcreate_targetq   s0   €ÿýÿ€ýÿzEPM.create_targetc                 Ã   s$   �| j d ur| j  ¡ I d H  d S d S r   ©r   r   r   r   r   r   r   …   s   €
ÿzEPM.disconnectTc              
   Ã   sj   �z| j  ¡ I d H \}}|d ur|‚|sW dS |  ¡ I d H W S  ty4 } z
d|fW  Y d }~S d }~ww ©N)TNF)r   r?   ÚbindrA   )r   ÚautobindrD   rE   rI   r   r   r   r?   Š   s   €€ÿzEPM.connectc              
   Ã   sX   �z| j  |¡I d H \}}|d ur|‚W dS  ty+ } z
d|fW  Y d }~S d }~ww rM   )r   rN   rA   )r   ÚuuidrD   rE   rI   r   r   r   rN   –   s   €€ÿzEPM.bindc                 Ã   s   �| j  ¡ I d H  d S r   rL   r   r   r   r   r   Ÿ   s   €c              
   Ã   sD   �z
| j  |¡I d H W S  ty! } z
d |fW  Y d }~S d }~ww r   )r   ÚrequestrA   )r   rQ   rI   r   r   r   rQ   ¢   s   €€ÿzEPM.requestc              
   Ã   sV  ��z‘t ƒ }tƒ }|d d… |d< td|dd … d d… ƒd |d< td|dd … ƒd |d< tƒ }| jd d… |d	< td| jdd … d d… ƒd |d< td| jdd … ƒd |d< tƒ }t|d
< | jjj	dkr‡t
ƒ }d|d< tƒ }td| jjj ƒ|d< | ¡ | ¡  }nG| jjj	dkr¥tƒ }	d|	d< tƒ }
d|
d< |	 ¡ |
 ¡  }n)| jjj	dkrÇtƒ }	t|	d< d|	d< tƒ }
d|
d< |	 ¡ |
 ¡  }ntd| j ƒ‚d|d< | ¡ | ¡  | ¡  | |d< tƒ }d|d< t|ƒ|d d< | ¡ |d d< d|jd jd < d|jd jd < | j |¡I d H \}}|d u�r|‚t d! |d" d d# d ¡ƒ}d }| jjj	dk�rRt
|d d$  ¡ ƒ}d%| jj ¡ |d  d&¡d d'… f }n=| jjj	dk�rqt|d d$  ¡ ƒ}	d(| jj ¡ |	d f }n| jjj	dk�r�t|d d$  ¡ ƒ}	d)| jj ¡ |	d f }|d fW S  t�yª } z
d |fW  Y d }~S d }~ww )*Né   ÚInterfaceUUIDz<Hé   r   ÚMajorVersioné   ÚMinorVersionÚDataRepUuidÚProtIdentifierÚncacn_npó    ÚPipeNamez%s ÚHostNamer$   ÚIpPorts       ÚIp4addrÚ
ncacn_httpÚPortIdentifierzUnsupported protocol! %sé   ÚNumberOfFloorsÚFloorsé   Ú
max_towersÚ	map_towerÚtower_lengthÚtower_octet_stringÚobjÚ
ReferentIDó    ÚITowersÚDataé   zncacn_np:%s[%s]zutf-8éÿÿÿÿzncacn_ip_tcp:%s[%s]zncacn_http:%s[%s])ÚEPMTowerÚEPMRPCInterfaceÚunpackÚEPMRPCDataRepresentationr   ÚEPMProtocolIdentifierÚFLOOR_RPCV5_IDENTIFIERr   r.   ÚrpcprotocolÚEPMPipeNameÚEPMHostNameÚbr,   ÚgetDataÚEPMPortAddrÚEPMHostAddrÚFLOOR_HTTP_IDENTIFIERrA   r&   Úept_mapÚlenÚfieldsrQ   Újoinr5   Údecode)r   rB   ÚtowerÚ	interfaceÚdataRepÚprotIdÚpipeNameÚhostNameÚtransportDataÚportAddrÚhostAddrrQ   ÚresprE   ÚresultrI   r   r   r   r@   ¨   sx   €"$ 
(
€ÿzEPM.mapió  c              
   Ã   sH  �zŒt ƒ }||d< ||d< |tkr4|d d… |d d< |dd … d d… |d d< |dd … |d d	< n||d< ||d
< ||d< ||d< | j |¡I d H \}}	|	d urU|	‚g }
t|d ƒD ]+}i }|d | }|d |d< d |d ¡|d< td |d d ¡ƒ|d< |
 |¡ q]|
d fW S  ty£ } z
d |fW  Y d }~S d }~ww )NÚinquiry_typeÚobjectrR   ÚIfidÚUuidrT   Ú	VersMajorrV   Ú	VersMinorÚvers_optionÚentry_handleÚmax_entsÚnum_entsÚentriesrl   Ú
annotationr„   ri   )	Ú
ept_lookupÚNULLr   rQ   Úranger‚   rq   ÚappendrA   )r   r�   Ú
objectUUIDÚifIdr•   r–   r—   rQ   r�   rE   r™   ÚiÚtmpEntryÚentryrI   r   r   r   Úlookupû   s:   €
€ÿz
EPM.lookupr   )r#   r$   NN)r#   r$   N)r$   r#   NN)NNN)T)Ú__name__Ú
__module__Ú__qualname__r   Úbytesr   r   r"   ÚstaticmethodÚintÚstrr   r   r0   r:   r   r<   r   rJ   rK   r   r?   ÚMSRPC_UUID_PORTMAPrN   rQ   r@   ÚRPC_C_EP_ALL_ELTSrœ   ÚRPC_C_VERS_ALLÚept_lookup_handle_tr¤   r   r   r   r   r      s*    
	Sr   c               
   Ã   sÂ   �zHddl m}  t d¡}| ¡ I d H \}}|d ur|‚| ¡ I d H \}}|d ur+|‚t|ƒ tƒ  | | j¡I d H \}}|d urC|‚t|ƒ W d S  t	y` } zt
 ¡  W Y d }~d S d }~ww )Nr   )Únrpcz
10.10.10.2)Úaiosmb.dcerpc.v5r°   r   r0   r?   r¤   Úprintr@   ÚMSRPC_UUID_NRPCrA   r!   Ú	print_exc)r°   rC   rD   rE   rF   rI   r   r   r   Úamain  s(   €
€ÿrµ   Ú__main__) r!   Útypingr   Úaiosmb.dcerpc.v5.epmÚaiosmb.dcerpc.v5.connectionr   Ú)aiosmb.dcerpc.v5.common.connection.targetr   Ú1aiosmb.dcerpc.v5.common.connection.authenticationr   Úaiosmb.dcerpc.v5.rpcrtr   r   r	   r
   r   r   Úasysocks.unicomm.common.proxyr   Úasysocks.unicomm.common.targetr   Ú!asyauth.common.credentials.spnegor   Úasyauth.common.credentials.ntlmr   Ú#asyauth.common.credentials.kerberosr   r   rµ   r¥   ÚasyncioÚrunr   r   r   r   Ú<module>   s*       þ