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rz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rt|}| 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\  zt |trd|jjv st |trt }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t12  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 rt }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rt! |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PreauthT   )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(    