o
    Žõ±j/2  ã                   @   sÈ   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T d dlm	Z	 d dl
mZ d dlmZ d dlT d dlmZ d dlT d dlT d dlT d dlT d dlT d dlT G dd	„ d	e jƒZG d
d„ dƒZdS )é    N)Úlogger)Ú*)Ú
SMBCommand)ÚNTStatus)Ú
SMBMessage)ÚSMB2Messagec                   @   s   e Zd ZdZdZdZdZdS )ÚSMBConnectionStatusÚNEGOTIATINGÚSESSIONSETUPÚRUNNINGÚCLOSEDN)Ú__name__Ú
__module__Ú__qualname__r	   r
   r   r   © r   r   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/aiosmb/relay/serverconnection.pyr      s
    r   c                   @   sd   e Zd Ze ¡ fdd„Zdd„ Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdefdd„Zddd„ZdS )ÚSMBRelayServerConnectionc                 C   s
  || _ | j  |¡| _|| _| j j| _|| _d | _d | _| j j	| _
d| _d| _tj| _i | _i | _i | _i | _d| _| j j| _| j j| _| j j| _| j j| _d| _d| _d| _d| _d| _d| _d| _t j!t j"B | _#d| _$t%j&| _'| j
du r}|  j't%j(O  _'d| _)d | _*d S )NFé
   r   T)+ÚsettingsÚcreate_new_gssapiÚgssapiÚconnection_idÚpreferred_dialectsÚsupported_dialectsÚ
connectionÚincoming_taskÚselected_dialectÚRequireSigningÚsigning_requiredÚencryption_requiredÚlast_treeidr   r	   ÚstatusÚOutstandingResponsesEventÚOutstandingRequestsÚOutstandingResponsesÚpending_tableÚSequenceWindowÚMaxTransactSizeÚMaxReadSizeÚMaxWriteSizeÚ
ServerGuidÚSupportsFileLeasingÚSupportsMultiCreditÚSupportsDirectoryLeasingÚSupportsMultiChannelÚSupportsPersistentHandlesÚSupportsEncryptionÚClientCapabilitiesÚNegotiateCapabilitiesÚDFSÚ	LARGE_MTUÚServerCapabilitiesÚClientSecurityModeÚNegotiateSecurityModeÚSMB2_NEGOTIATE_SIGNING_ENABLEDÚServerSecurityModeÚSMB2_NEGOTIATE_SIGNING_REQUIREDÚ	SessionIdÚ
SessionKey)Úselfr   r   r   Úshutdown_evtr   r   r   Ú__init__   sD   







z!SMBRelayServerConnection.__init__c                 Ã   s   �t  |  ¡ ¡| _| jS )N)ÚasyncioÚcreate_taskÚ(_SMBRelayServerConnection__handle_smb_inr   )r=   r   r   r   ÚrunP   s   €zSMBRelayServerConnection.runc              
   Ã   s�   �z%|   d¡I d H  | jd ur| j ¡  | jd ur$| j ¡ I d H  W d S W d S  tyG } zt ¡  |   d| ¡I d H  W Y d }~d S d }~ww )Nz[INF] Stopping connectionú[ERR] %s)Úprintr   Úcancelr   ÚcloseÚ	ExceptionÚ	tracebackÚ	print_exc)r=   Úer   r   r   ÚstopT   s   €


ÿ"€þzSMBRelayServerConnection.stopc                 Ã   s6   �| j jd ur| j  d| j› d|› �¡I d H  d S d S )Nz[SMBSERVERCONN][z] )r   Úlog_callbackr   )r=   Úmsgr   r   r   rE   _   s   €$ÿzSMBRelayServerConnection.printc                 Ã   s–  ��z@�z| j  ¡ 2 �z3 dH W }|du r nû|d dk r%td|d  ƒ‚|d dkr/tdƒ‚|d dkr9tdƒ‚|d dkr¸zt |¡}|  d	| ¡I dH  W n ty` } ztd
ƒ‚d}~ww | jtjkrp|  	|¡I dH  nH| jtj
kr|  |¡I dH  n9| jtjkr¸|jjtjkr•|  |¡I dH  n#|jjtjkr¥|  |¡I dH  n|jjtjkr¸ W W |  ¡ I dH  dS |d dkrìt |¡}|  d	| ¡I dH  | jtjkrã|jjtjkrâ|  	|¡I dH  n	td|jj ƒ q
t dd|jj ¡ |jj| jv �r| j|jj  ¡ I dH  | j|jj= q
6 W n% tj�y'   Y W |  ¡ I dH  dS    t  !¡  Y W |  ¡ I dH  dS W |  ¡ I dH  dS |  ¡ I dH  w )z±
		Waits from SMB message bytes from the transport in_queue, and fills the connection table.
		This function started automatically when calling connect.
		Pls don't touch it.
		Nr   éü   zUnknown SMB packet type %séý   z2SMB2_COMPRESSED_DATA_HEADER is not implemented yetz7SMB2_COMPRESSED_TRANSFORM_HEADER is not implemented yetéþ   z[PACKET] %sz;Failed to parse SMB data. Probably not implemented feature.éÿ   z.SMBv1 message recieved! This is unexpected! %sé   z*__handle_smb_in got new message with Id %s)"r   ÚreadrH   r   Ú
from_bytesrE   r!   r   r	   Ú	negotiater
   Úsession_setupr   ÚheaderÚCommandÚSMB2CommandÚTREE_CONNECTÚtree_connectÚCREATEÚcreateÚIOCTLrL   Úioctlr   r   ÚSMB_COM_NEGOTIATEr   ÚlogÚ	MessageIdr%   r@   ÚCancelledErrorrI   rJ   )r=   Úmsg_datarN   rK   r   r   r   Ú__handle_smb_inc   sn   €
€ÿê
€€Ê€8ý€" z(SMBRelayServerConnection.__handle_smb_inc              
   Ã   s\  �z•t |tƒrd|jjv st |tƒr”tƒ }tƒ |_| j|j_tj	|j_
d|j_| j|j_| j|j_| j|j_| j|j_| j|j_tj ¡ |j_tj ¡ |j_| j ¡ |j_d|j_g |j_tƒ |_tj|j_t j!|j_"d|j_#|  $|¡I d H  tj	| _%t&j'| jv | _(t&j)| jv | _*t&j+| jv | _,t-j.| _/W d S  t0y­ } zt1 2¡  W Y d }~d S d }~ww )Nz	SMB 2.???r   rS   )3Ú
isinstancer   ÚcommandÚDialectsr   ÚNEGOTIATE_REPLYr9   ÚSecurityModeÚNegotiateDialectsÚSMB202ÚDialectRevisionÚNegotiateContextCountr*   r5   ÚCapabilitiesr'   r(   r)   ÚdatetimeÚutcnowÚ
SystemTimeÚServerStartTimer   Úget_mechtypes_listÚSecurityBufferÚNegotiateContextOffsetÚNegotiateContextListÚSMB2Header_SYNCrX   rZ   Ú	NEGOTIATErY   ÚSMB2HeaderFlagÚSMB2_FLAGS_SERVER_TO_REDIRÚFlagsÚCreditChargeÚsendSMBr   r2   ÚMULTI_CHANNELr.   ÚLEASINGr+   r4   r,   r   r
   r!   rH   rI   rJ   )r=   ÚreqÚreplyrK   r   r   r   rV   ©   sB   € 








€ÿz"SMBRelayServerConnection.negotiatec              
   Ã   sT  �d }�zVd}| j dkr|  j d7  _ |dk�rXtƒ }tƒ |_d|j_z¢|  d¡I d H  | j |jj¡I d H \|j_}}|  d|jj||f ¡I d H  |d urQ|‚|du rÄ|jjd u rÄtƒ }tƒ |_d|j_| j 	¡ I d H \|j_}|d urx|‚t
ƒ |_tj|j_tj|j_|j jtjO  _| j |j_ d|j_d|j_tj|j_|j jtjO  _|  |¡I d H  | j ¡ d d… | _tj| _W W dS W n tyß } zt|ƒ  d	¡d
krÙt!ƒ ‚|‚d }~ww |du �rt
ƒ |_tj|j_tj|j_| j |j_ d|j_d|j_tj"|j_|  |¡I d H  |du �rUt
ƒ |_tj|j_tj|j_| j |j_ d|j_d|j_tj|j_|j jtjO  _d|j_|  |¡I d H  | j ¡ d d… | _tj| _W dS W d S  t�y© } zB|  d| ¡I d H  t
ƒ |_tj|j_tj|j_|j jtjO  _| j |j_ d|j_d|j_tj#|j_|  |¡I d H  W Y d }~dS d }~ww )Né   r   rS   z'[INF] Calling authenticate_relay_serverz&[INF] Authenticate results: %s, %s, %sFé   ©FNÚPreauthéÿÿÿÿTé   )TNrD   )$r;   r   ÚSESSION_SETUP_REPLYrh   ÚSessionFlagsrE   r   Úauthenticate_relay_serverÚBufferÚ"authenticate_relay_server_finishedry   rX   rZ   ÚSESSION_SETUPrY   r{   r|   r}   ÚSMB2_FLAGS_SIGNEDr~   Ú	CreditReqr   ÚSUCCESSÚStatusr   Úget_session_keyr<   r   r   r!   rH   ÚstrÚfindÚSMBKerberosPreauthFailedÚMORE_PROCESSING_REQUIREDÚACCESS_DENIED)r=   rN   ÚerrÚmaxiterrƒ   Úto_continuerK   r   r   r   rW   Ï   s¢   €

 



€€û









±P



€ôz&SMBRelayServerConnection.session_setuprN   c                 Ã   s²   �t  t|j¡}tƒ }tƒ |_tj|j_	t
j|j_| j|j_d|j_d|j_tj|j_tƒ |_|j d¡r;tj|j_ntj|j_tj|j_tj|j_tj|j_ |  !|¡I d H  d S )Nr   rS   zIPC$)"ÚtypingÚcastÚTREE_CONNECT_REQrh   r   ry   rX   rZ   r[   rY   r{   r|   r}   r;   r~   r‘   r   r’   r“   ÚTREE_CONNECT_REPLYÚPathÚendswithÚ	ShareTypeÚPIPEÚDISKÚ
ShareFlagsÚSMB2_SHAREFLAG_MANUAL_CACHINGÚTreeCapabilitiesÚSMB2_SHARE_CAP_NONErp   ÚFileAccessMaskÚGENERIC_ALLÚMaximalAccessr   )r=   rN   rh   rƒ   r   r   r   r\   4  s$   €







z%SMBRelayServerConnection.tree_connectFNc                 Ã   s2  �| j tjkrOt|tƒrd}|  jd7  _nd|j_|jjdu r$d|j_| j|j_	| j}|  jd7  _t
 ¡ | j|< | j | ¡ ¡I dH  |du rM||fS |S |jjtjurb| j|j_	|  jd7  _| j|j_|jjsod|j_| j tjkryd|j_|jj	}t
 ¡ | j|< | j | ¡ ¡I dH  |du r—||fS |S )zp
		Sends an SMB message to teh remote endpoint.
		msg: SMB2Message or SMBMessage
		Returns: MessageId integer
		r   rS   NTr‰   )r!   r   r	   rg   r   r&   rX   r~   r‘   rc   r@   ÚEventr"   r   ÚwriteÚto_bytesrY   rZ   ÚCANCELr;   r
   )r=   rN   Úret_messageÚcompression_cbÚ
message_idr   r   r   r   M  s<   €



z SMBRelayServerConnection.sendSMBr†   )r   r   r   r@   r­   r?   rC   rL   rE   rB   rV   rW   r   r\   r   r   r   r   r   r      s    2F&er   )Úenumr@   rq   rI   r�   Úaiosmbr   Úaiosmb.commons.exceptionsÚ!aiosmb.protocol.smb.command_codesr   Úaiosmb.wintypes.ntstatusr   Úaiosmb.protocol.smb.messager   Úaiosmb.protocol.smb.commandsÚaiosmb.protocol.smb2.messager   Ú*aiosmb.protocol.smb2.commands.tree_connectÚaiosmb.protocol.smb2.commandsÚaiosmb.protocol.smb2.headersÚ"aiosmb.protocol.smb2.command_codesÚaiosmb.protocol.commonÚ.aiosmb.wintypes.dtyp.constrcuted_security.guidÚEnumr   r   r   r   r   r   Ú<module>   s(    