
    5i                        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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 d d	lmZ d d
lmZ d dlmZ d dlmZmZmZ d dlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&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-m.Z. d dl/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7T d dl8m9Z9 d dl8m:Z: d dl;m<Z<m=Z=m>Z> d dl?m@Z@ d dlAmBZB d dlCmDZE d dlCmFZG d dlHmIZI  G d de jJ                  ZK G d d          ZL G d d           ZM G d! d"          ZN G d# d$          ZOd% ZPeQd&k    rCd d'lRmSZS  ejT        d(           d)ZU eSjV        eU          ZW ejX         ePeW                     dS dS )*    N)	Coroutine)logger)*)NetBIOSPacketizer)
SMBCommand)NTStatus)	SMBHeaderSMBHeaderFlags2Enum)
SMBMessage)SMBSecurityMode)SMB_COM_NEGOTIATE_REQ)SMB2MessageSMB2TransformSMB2Compression)
SMB2ContextType SMB2PreauthIntegrityCapabilitiesSMB2HashAlgorithm
SMB2CipherSMB2CompressionTypeSMB2CompressionFlagsSMB2EncryptionCapabilitiesSMB2CompressionCapabilitiesSMB2SigningAlgorithmSMB2SigningCapabilities)FileInfoClass) FileFullDirectoryInformationList)FileAttributes)SECURITY_DESCRIPTOR)SE_OBJECT_TYPE)hmac)hashlib)AESMODE_CCMMODE_GCM)AES_CMAC)KDF_CounterMode)compress)
decompress)	UniClientc                       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/        G/home/kali/Ninja/venv/lib/python3.11/site-packages/aiosmb/connection.pyr+   r+   -   s"        
r4   r+   c                   *    e Zd Zd Zed             ZdS )	TreeEntryc                     d | _         d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d S N)	
share_nametree_id
session_idnumber_of_usersis_DFSis_CAis_scaleoutencryptmaximal_accessselfs    r5   __init__zTreeEntry.__init__4   sJ    $/$,$/$$+$*$$,$r4   c                    t                      }||_        | j        j        |_        | j        j        |_        d|_        t          j	        | j
        j        v |_        t          j        | j
        j        v |_        t          j        | j
        j        v |_        t"          j        | j
        j        v |_        | j
        j        |_        |S N   )r7   r:   headerTreeIdr;   	SessionIdr<   r=   TreeCapabilitiesSMB2_SHARE_CAP_DFScommandCapabilitiesr>   &SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITYr?   SMB2_SHARE_CAP_SCALEOUTr@   
ShareFlagsSMB2_SHAREFLAG_ENCRYPT_DATArA   MaximalAccessrB   )replyr:   tes      r5   from_tree_replyzTreeEntry.from_tree_reply?   s    {{""-|""*,("-"1U]5OO")DHbb"(#;u}?YY".59QQ"*m1"	)r4   N)r0   r1   r2   rE   staticmethodrW   r3   r4   r5   r7   r7   3   s>        	 	 	   ,  r4   r7   c                   *    e Zd Zd Zed             ZdS )
FileHandlec                     d | _         d | _        d | _        d | _        d | _        d | _        d | _        t          j        d          | _	        d S rG   )
file_idr;   oplock_level
is_durableis_resilientlast_disconnect_time	file_nameasyncio	SemaphoreoplockrC   s    r5   rE   zFileHandle.__init__N   sL    $,$,$$/$"$$.!!$$$+++r4   c                     t                      }| j        j        |_        ||_        ||_        d|_        d|_        d|_        ||_	        |S )NFr   )
rZ   rN   FileIdr\   r;   r]   r^   r_   r`   ra   )rU   r;   ra   r]   fhs        r5   from_create_replyzFileHandle.from_create_replyX   sG    ||"}#"*"* "/"-"/"",	)r4   N)r0   r1   r2   rE   rX   rh   r3   r4   r5   rZ   rZ   M   s>        % % % 	 	 ,	 	 	r4   rZ   c                   4    e Zd Zd
dZd Zd Zd Zd Zd Zd	S )SMBPendingMsg  d   c                 Z    || _         || _        || _        || _        || _        d | _        d S r9   )
message_idmax_renewaltimeoutOutstandingResponsesOutstandingResponsesEventpending_task)rD   rn   rq   rr   rp   ro   s         r5   rE   zSMBPendingMsg.__init__e   s6    $/ $$,2$#<$ $r4   c                    K   t          j        d           d {V  |                     t                                 d {V  d S )Nrk   )rb   sleep_SMBPendingMsg__destroy_messageSMBPendingTimeoutrC   s    r5   __pending_waiterzSMBPendingMsg.__pending_waiterm   s[      d02233333333333r4   c                    K   d |f| j         | j        <   | j        | j        v r$| j        | j                                                  d S r9   )rq   rn   rr   set)rD   problems     r5   __destroy_messagezSMBPendingMsg.__destroy_messageq   sJ      04g$DO,	_666!$/266888&r4   c                 2  K   | j         | j                                          | j        B| xj        dz  c_        | j        dk    r'|                     t	                                 d {V  t          j        |                                           | _         d S )NrH   r   )rs   cancelro   rv   SMBPendingMaxRenewalrb   create_task_SMBPendingMsg__pending_waiterrC   s    r5   updatezSMBPendingMsg.updatew   s       
"	!q
!

 
 !5!7!7
8
88888888)$*?*?*A*ABB$r4   c                 `   K   t          j        |                                           | _        d S r9   )rb   r   r   rs   rC   s    r5   runzSMBPendingMsg.run   s+      )$*?*?*A*ABB$r4   c                 N   K   | j         | j                                          d S d S r9   )rs   r~   rC   s    r5   stopzSMBPendingMsg.stop   s2      	" #"r4   N)rk   rl   )	r0   r1   r2   rE   r   rv   r   r   r   r3   r4   r5   rj   rj   d   sy           4 4 4	 	 	
C 
C 
CC C C    r4   rj   c                      e Zd ZdZd5dededefdZd Zd	 Zd
 Z	d Z
d Zd Zd Zd Zd Zd Zd ZdefdZd6dZd6dZdeeeef         fdZdefdZdedefdZdefdZd7defdZde fd Z!e"j#        e$j%        ddfd!e&d"e"fd#Z'd8d%Z(d9d&Z)e*j+        e,j-        d$d$d'fd(Z.d)d$e,j/        dd$fd*Z0de1j2        d+fd,Z3e4j5        fd-Z6d. Z7d/ Z8d0 Z9d1 Z:d2 Z;d3 Z<d4 Z=dS ):SMBConnectionzr
	Connection class for network connectivity and SMB messages management (sending/recieveing/singing/encrypting).
	TFtargetpreserve_gssapinosignc                 F   || _         || _        d | _        |du rt          j        |          | _        || _        | j        j        | _        d | _        d | _	        d | _
        d | _        d | _        d| _        d | _        d| _        d| _        d | _        d | _        d| _        d| _        t*          j        | _        i | _        i | _        i | _        i | _        i | _        i | _        i | _        i | _        d| _         | j        j!        | j        j!        nd| _!        | j        j"        | j        j"        nd| _"        | j        j#        | j        j#        nd| _#        d | _$        d| _%        d | _&        tN          (                                | _)        d| _*        d| _+        d| _,        d| _-        d| _.        d| _/        d| _0        d| _1        td          j3        | _4        |du rtd          j5        td          j6        z  | _4        d| _7        d| _8        d | _9        d | _:        d | _;        d | _<        d | _=        d| _>        t~          j@        | _A        d| _B        d | _C        d | _D        d | _E        	 t          jG        g| _E        d| _H        t          jJ        g| _K        d | _L        t          jN        | _O        d| _P        d S )NT   Fr   i   s@                                                                   )Qr   gssapioriginal_gssapicopydeepcopyr   preferred_dialectssupported_dialectssettingsnetwork_connectionnetbios_transportincoming_taskkeepalive_taskkeepalive_timeoutconnection_closed_evtlogin_oksupress_keepaliveactivity_atselected_dialectsigning_requiredencryption_requiredr+   r,   statusrr   OutstandingRequestsrq   pending_tablemessageIdToOplockTreeConnectTable_idTreeConnectTable_shareFileHandleTableSequenceWindowMaxTransactSizeMaxReadSizeMaxWriteSize
ServerGuidRequireSigning
ServerNameGUIDrandom
ClientGUIDSupportsFileLeasingSupportsMultiCreditSupportsDirectoryLeasingSupportsMultiChannelSupportsPersistentHandlesSupportsEncryptionClientCapabilitiesServerCapabilitiesNegotiateSecurityModeNONEClientSecurityModeSMB2_NEGOTIATE_SIGNING_ENABLEDSMB2_NEGOTIATE_SIGNING_REQUIREDServerSecurityModerK   
SessionKey
SigningKeyApplicationKeyEncryptionKeyDecryptionKeyCompressAllRequestsr   SHA_512PreauthIntegrityHashIdPreauthIntegrityHashValueCompressionIdCipherIdCompressionIdsr   LZNT1SupportsChainedCompressionr   AES_128_CCMsupported_encryptionssupported_signaturesr!   sha512preauth_ctxsession_closed)rD   r   r   r   r   s        r5   rE   zSMBConnection.__init__   s   $+$+$-//4$+
 !K:$ $- $$$$$#$$- $$$$"$#/$+#%$ $ $$$  $ "$$$8<8S8_44em$040G0ST[,,Ya$26+2J2Vdk..\d$$/$$/KKMM$/ #$"$"'$#$#($ !$$$16$u__2QTi  UJ  J4$ $.$/$/$$$ #$ !2 9$#-$ $$-$5-344$)$! * 67$"$^$ $r4   c                 
   K   | S r9   r3   rC   s    r5   
__aenter__zSMBConnection.__aenter__   s      	+r4   c                 f   K   t          j        |                                 d           d {V  d S )N   rp   )rb   wait_for	terminate)rD   exc_typeexc	tracebacks       r5   	__aexit__zSMBConnection.__aexit__   s>      ))Q777777777777r4   c                 r    | j         t          j        t          j        t          j        fv r| j        S | j        S r9   )r   NegotiateDialectsSMB300SMB302SMB311r   r   rC   s    r5   get_session_keyzSMBConnection.get_session_key   s4    	07:K:RUfUmnnn

	r4   c                     	 | j                                         }||                                }n#  t          j                     d }Y nxY w| j        (|t          j        | j        v t          j        | j        v dS d S )N)	ntlm_datasigning_enabledr   )	r   get_extra_infoto_dictr   	print_excr   r   r   r   )rD   r   s     r5   r   zSMBConnection.get_extra_info  s    {))++9!!##I999	(-LPTPgg.NRVRii  
 
s	   /2 Ac                 Z  K   	 | j                                         2 3 d{V }t          j                                        | _        |/t          | j        | j                                        nd          |d         dk     rt          d|d         z            |d         dk    rt          j
        |          }|j        j        t          j        k    r^t          | j        t"          |j        j        dd         d	          }|                    |j        |d
d         |j        j                  }n|j        j        t          j        k    r\t          | j        t.          |j        j        dd                   }|                    |j        |d
d         |j        j                  }nt          d|j        j        z            |d         dk    rt1          j
        |          }|j        j        t4          j        k    r|j        j        | j        k    rt=          j        d           |j        j        t@          j!        k    rE|j        d|j        j"                 }|tG          |j        |j        j"        d                   z  }|}n0t          d|j        j        j$        z            t          d          |d         dk    rtK          j
        |          }|d         dk    rtM          j
        |          }|j        j'        tP          j)        k    r|j        j*        | j+        v r)| j+        |j        j*                 ,                                 t[          |j        j*        | j.        | j/        | j        j0        | j        j1                  | j2        |j        j*        <   | j2        |j        j*                 3                                 d{V  |j        j*        | j2        v rA| j2        |j        j*                 4                                 d{V  | j2        |j        j*        = ||f| j.        |j        j*        <   |j        j*        | j/        v r+| j/        |j        j*                 5                                 Y[6 n# tl          j7        $ r Y t=          j        dt          d          z             | j/        D ]:}dt          d          f| j.        |<   | j/        |         5                                 ;| 8                                 d{V  dS  t=          j9        d           Y nxY wt=          j        dt          d          z             | j/        D ]:}dt          d          f| j.        |<   | j/        |         5                                 ;| 8                                 d{V  dS # t=          j        dt          d          z             | j/        D ]:}dt          d          f| j.        |<   | j/        |         5                                 ;| 8                                 d{V  w xY 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.
		NzUnknown targetr      zUnknown SMB packet type %s         segment_size   4      z7Common encryption algo is %s but it is not implemented!zFServer is using a different compression algo than whats agreed upon...z5Server used %s compression, but it is not implementedz=Server sent chained compression, but its not implemented here      )rp   ro   z1__handle_smb_in got error from transport layer %szConnection closed__handle_smb_in):r   readdatetimeutcnowr   SMBConnectionTerminatedr   get_hostname_or_ip	Exceptionr   
from_bytesrI   EncryptionAlgorithmr   r   r"   r   r#   Noncedecryptdata	SignatureAES_128_GCMr$   r   Flagsr   r   CompressionAlgorithmr   r   debugr   r   Offsetlznt1_decompressnamer   r   Statusr   PENDING	MessageIdr   releaserj   rq   rr   PendingTimeoutPendingMaxRenewalr   r   r   rz   rb   CancelledErrorr   	exception)rD   msg_datamsgctxuncompressed_datamids         r5   r   zSMBConnection.__handle_smb_in  sM     T05577 F F F F F F FX(//11D"t{G^4;#A#A#C#C#Cdtuuu{S1HQK?@@@{d#H--S
&**@@@"Hcj.>ss.CRTUUUcSXx2
8LMMhh *
(J,B
B
B"Hcj.>ss.CDDcSXx2
8LMMhh ORUR\Rppqqq{d%h//S
0555		(D,>	>	>|\]]]		(,?,E	E	E8$6SZ%6$67,SXcj6G6H6H-IJJJ#xxNQTQ[QpQuuvvvUVVV{d!(++S{d **S zH,,,
 666
SZ12::<<<0=cj>RTXTmos  pN  Z^  Ze  Zt  BF  BM  B_  1`  1`  1`T
,-cj2377999999999
zt111cj2388:::::::::		CJ0	17:HoDcj23
zt===	#CJ$89==???? M 87P 
	 
 
 
	 
<CiPcFdFddeee, . .s&*I6I,J,J%KDc""3'++----			'	%&&&&&	<CiPcFdFddeee, . .s&*I6I,J,J%KDc""3'++----			 
<CiPcFdFddeee, . .s&*I6I,J,J%KDc""3'++----			s5   Q: Q8QQ: 9V( :T#	V( T#!V( (BX*c                   K   	 |                                   d{V \  }}|||                                  d{V \  }}|||                                  d{V \  }}||t          j        |                                           | _        d| _        dS # t          $ r(}| 	                                 d{V  d|fcY d}~S d}~ww xY w)a  
		This is the normal starting function.
		Performs establishment of the TCP connection, then the negotiation and finally the session setup.
		If this function returns without an exception, then I'm happy.
		Also it means that you have a working and active session to the server.
		NTTNF)
connect	negotiatesession_setuprb   r   	keepaliver   r   r   
disconnectrD   _erres       r5   loginzSMBConnection.logint  s     ,,..      61c	o
I..""""""""61c	o
I$$&&&&&&&&61c	o
I ,T^^-=-=>>44=
*	   			
(??????s   BB 
C%CCCc                   K   	 d| j         j        vrt          d          |                                  d {V \  }}|||                                  d {V \  }}|||                     d           d {V \  }}||| j                                         d f|                                  d {V  S # t          $ r(}d |fcY d }~|                                  d {V  S d }~ww xY w# |                                  d {V  w xY w)Nz2NTLMSSP - Microsoft NTLM Security Support Providerz6Fake authentication is only supported via NTLM packageT)	fake_auth)r   authentication_contextsr   r  r  r  r   r  r  s       r5   
fake_loginzSMBConnection.fake_login  s{     :$+Beee
L
M
MM,,..      61c	o
I..""""""""61c	o
I$$$6666666661c	o
I
+
$
$
&
&
, 
		 
   
'>>>>>			 
		s0   BB; ;
C-C(C-	C0 (C--C0 0Dc                   K   	 || _         |                                  d{V \  }}||d}d}|                     d           d{V \  }}}t          |t                    r1t
          j        |j        j        v }t
          j	        |j        j        v }nEt          |t                    r0t          j        |j        j        v }t          j        |j        j        v }||||||df|                                  d{V  S # t          $ r+}dddd|fcY d}~|                                  d{V  S d}~ww xY w# |                                  d{V  w xY w)z
		Checks if the remote end supports a given protocol.
		On success it returns True and the reply from the server (for checking SMB3 capabilities)
		NT)protocol_testF)r   r  r  
isinstancer   r   r   rN   SecurityModer   r   r   %NEGOTIATE_SECURITY_SIGNATURES_ENABLED&NEGOTIATE_SECURITY_SIGNATURES_REQUIREDr  r   )	rD   protocolr   r!  sign_ensign_reqresrplyr"  s	            r5   r)  zSMBConnection.protocol_test  s     
%4,,..      61c	o
I78...>>>>>>>>>3c{## c#BdlF__G$DHaaHH4$$ cCt|G``GEIbbH	o
I
w$
, 
		 
 % % %
tT1
$$$$$$			% 
		s0   C!D   
D5
D0D5D8 0D55D8 8Ec                   K   	 t          j                    | _        t          | j                  }t          | j        |          }|                                 d{V | _        t          j	        | 
                                          | _        dS # t          $ r(}|                                  d{V  d|fcY d}~S d}~ww xY w)zh
		Establishes socket connection to the remote endpoint. Also starts the internal reading procedures.
		Nr  F)rb   Eventr   r   r   r)   r   r  r   r   _SMBConnection__handle_smb_inr   r   r  )rD   
packetizerclientr"  s       r5   r  zSMBConnection.connect  s      	 '4!$"233:dk:..6#)>>#3#33333334+D,@,@,B,BCC4
*	   			
(??????s   BB 
CB>8C>Cc                 ^  K   | j         t          j        k    rdS t          j        | _         | j        | j                                         | j        9| j                                         d{V  t          j        d           d{V  | j	        | j	                                         dS dS )z
		Tears down the socket connecting as well as the reading cycle.
		Doesn't do any cleanup! 
		For proper cleanup call the terminate function.
		Nr   )
r   r+   r/   r   r~   r   closerb   ru   r   rC   s    r5   r  zSMBConnection.disconnect  s       
['...	6#*$+	#	(		 	&	&	(	((((((((	q			$ %$r4   c                 F  K   	 d}| j         j        dk     rdS | j         j        dk    rt          | j         j        dz
  |          }	 t          j        |           d{V  t
          j                                        | j        z
  j        |k    r<| j	        du r3t          j
        |                                 | j                   d{V  # t          j        $ r Y dS t          $ r9}t          j        d           |                                  d{V  Y d}~dS d}~ww xY w)	zR
		Sends an echo message every X seconds to the server to keep the channel open
		
   r   NrH   TFr   z/Keepalive failed! Server probably disconnected!)r   rp   maxrb   ru   r   r   r   secondsr   r   echor   r  r   r   r  r  )rD   
sleep_timer"  s      r5   r  zSMBConnection.keepalive  se     :
kA
FaT[(1,j99JK
-

#
########  ""T%55>KKPTPfjoPoPoDIIKK43IJJJJJJJJJJK
 
	 
 
 
	66	   	<ABBB			s#   C B4C D 	D '.DD r  c                     t          j                    }|                    | j        |z              |                                | _        d S r9   )r!   r   r   r   digest)rD   r  r  s      r5   update_integrityzSMBConnection.update_integrity  s?    #**T+h6777#&::<<$   r4   c                   K   	 d}t           j        | j        v r>t                      }t          j        |_        t          j        |_	        d|_
        t          j        |_        t                      }|du r	dg|_        n	ddg|_        t!          ||          }|                     |           d{V \  }}|||                     |           d{V \  }}}||t'          |t                     r-|du r|j        j        dk    rd|dfS d|dfS t-          d	          |j        j	        t          j        k    rt-          d
          | j        t           j        = t1                      }| j        |_        d|_        | j        |_        d | j        D             |_        t=          d | j        D                       rt>          j         |_        t           j!        | j        v r|j"        #                    tI          j%        | j&        g                     | j'        K|xj        t>          j         z  c_        |j"        #                    tQ          j)        | j'                             | j*        1|j"        #                    tW          j)        | j*                             | j,        7|j"        #                    t[          j.        | j,        | j/                             ta                      }tb          j2        |_        d|_3        ti          ||          }|                     |           d{V \  }}|||                     |           d{V \  }}}||t'          |t                     rt-          d	          | 5                    |           |j        j	        t          j        k    rR|j        j	        t          j6        k    rt-          d|j        j	        z            t-          d|j        j	        z            |j        j7        | j        vrtq                      |j        j7        | _9        |j        j        | _:        tv          j<        |j        j        v | _=        | j>        du rd| _=        t>          j         |j        j        v rd| _?        t          jA        | _B        |j        j"        D ]W}	|	jC        t          jE        k    rd| _?        |	jF        d         | _B        |	jC        t          jG        k    r|	jH        d         | _I        Xt          jK        dd| j9        z             t          | jM        |j        jM                  | _M        t          | jN        |j        jN                  | _N        t          | jO        |j        jO                  | _O        |j        jP        | _P        t>          jQ        |j        j        v | _R        t>          jS        |j        j        v | _T        t>          jU        |j        j        v | _V        |j        j        | _W        |j        j        | _X        t          jZ        | _[        | jN        | j\        j]        _^        |du rd|dfS dS # t,          $ r}
|du rY d}
~
dS d|
fcY d}
~
S d}
~
ww xY w)zj
		Initiates protocol negotiation.
		First we send an SMB_COM_NEGOTIATE_REQ with our supported dialects
		Nr   Tz
NT LM 0.12z	SMB 2.???z	SMB 2.002  Fz7Server replied with SMBv1 message, doesnt support SMBv2z<SMB2 negotiate error! Server replied with error status code!c                     g | ]}|S r3   r3   .0dialects     r5   
<listcomp>z+SMBConnection.negotiate.<locals>.<listcomp>2  s    MMM'gMMMr4   c              3   (   K   | ]}|t           v V  d S r9   )SMB2_NEGOTIATE_DIALTECTS_3rF  s     r5   	<genexpr>z*SMBConnection.negotiate.<locals>.<genexpr>6  s(      	W	WG'/
/	W	W	W	W	W	Wr4   zDnegotiate_1 dialect probably not suppported by the server. reply: %sznegotiate_1 reply: %srH   zServer selected dialect: %sr  )FNN)_r   WILDCARDr   r	   r   SMB_COM_NEGOTIATECommandr   SUCCESSr  r  r
   SMB_FLAGS2_UNICODEFlags2r   Dialectsr   sendSMBrecvSMBr*  rN   DialectIndexr   rI   NEGOTIATE_REQr   r+  rO   r   
ClientGuidallNegotiateCapabilities
ENCRYPTIONr   NegotiateContextListappendr   	constructr   r   r   from_enc_listr   r   r   r   from_comp_listr   SMB2Header_SYNCSMB2Command	NEGOTIATE	CreditReqr   rB  NOT_SUPPORTEDDialectRevisionSMBUnsupportedDialectSelectedr   r   r   r   r   r   r   r   r   r   ContextTyper   ENCRYPTION_CAPABILITIESCiphersCOMPRESSION_CAPABILITIESCompressionAlgorithmsr   r   logminr   r   r   r   MULTI_CHANNELr   LEASINGr   	LARGE_MTUr   r   r   r+   r-   r   r   r6  buffer_size)rD   r)  r2  rI   rN   r  rn   r!  	rply_datanegctxr"  s              r5   r  zSMBConnection.negotiate  sl     
_
4 D$;;;[[F 2FN&FMFL)<FM#%%G%W$[1W
VW
%
%C LL--------OJ
Y "&j!9!9999999D)S
Y$
## Q		"e	+	+T44OPPP{X---STTT 1 :; __7!477!_7MMT5LMMM7 
	W	Wt?V	W	W	WWW ?3>G $"999  ''%/"#    !-2==!(( .!     ,!((+     &!((!0&    6 *6>6	VW	%	%3<<,,,,,,,,?:s	o
I $Z 8 88888884C	o
Iz"" O
M
N
NN###
kH,,,{X333[^b^i^ppqqq,t{/AABBB
l"$*AAA
'
)
))<74!\640OSWS_Sll4
kT!D &$,*CCC#D*DM2 : :v_DDD $T^A&T]_EEE 6q9T 
:a.1FFGGGd2DL4PQQ4$*DL,DEE44,dl.GHH4\,4?4BdlF__43;t|?XX43=AZZ4!\64!\64$14;484D4%1tt
*	   t
(??????s1   DY  	Y  T/Y   
Y"
YYY"Y"c                 0	  K   	 d }t           j        }d}|t           j        k    rD|dk    r=t                      }	 | j                            || j                                        | j                   d {V \  |_        }}|||dk    r| j        j        | j        j        j	        dS nV# t          $ rI}t          |                              d          dk    rt          t          |                    |d }~ww xY wd|_        t          j        |_        | j        du rt          j        |_        d|_        d|_        d|_        t/                      }	t0          j        |	_        d|	_        t9          |	|          }
|                     |
           d {V \  }}|||                     |           d {V \  }}}||| j        dk    r|j         j        | _        |j         j!        t           j"        t           j        fvrnc|j         j!        t           j"        k    r| #                    |           |j$        j        }|j         j!        }|dz  }|t           j        k    r|dk    =|j         j!        t           j"        k    r| j                            || j                                        	           d {V \  |_        }}||| j        %                                du rd
| _&        | j        '                                d d         | _(        | j&        r5| j)        tT          j+        tT          j,        tT          j-        fv r
| j)        tT          j-        k    rt]          | j(        d| j/        d          | _0        t]          | j(        d| j/        d          | _1        t]          | j(        d| j/        d          | _2        t]          | j(        d| j/        d          | _3        npt]          | j(        ddd          | _0        t]          | j(        ddd          | _1        t]          | j(        ddd          | _2        t]          | j(        ddd          | _3        th          j5        | _6        nto          d|j         j!                  dS # t          $ r}d
|fcY d }~S d }~ww xY w)Nr   r   )spnr   Tr  PreauthrH   )rv  Fr   s   SMBSigningKey    s
   SMBAppKey s   SMBC2SCipherKey s   SMBS2CCipherKey s   SMB2AESCMAC s   SmbSign s   SMB2APP s   SmbRpc s   SMB2AESCCM s
   ServerIn  s
   ServerOut z.session_setup (authentication probably failed))8r   MORE_PROCESSING_REQUIREDSESSION_SETUP_REQr   authenticater   to_target_stringBufferselected_authentication_contextntlmChallenger   strfindSMBKerberosPreauthFailedr  r   r   r+  r   r   rO   ChannelPreviousSessionIdra  rb  SESSION_SETUPrO  rd  r   rT  rU  rK   rI   r  rP  rB  rN   is_guestr   r   r   r   r   r   r   r   r&   r   r   r   r   r   r+   r.   r   SMBException)rD   r%  authdatar   maxiterrN   r1  r!  r"  rI   r  rn   r2  rs  s                 r5   r  zSMBConnection.session_setup  s     Z8-67	84	4	41!!G'+{'?'?dkNjNjNlNluy  vA'?  (B  (B  "B  "B  "B  "B  "B  "BW^S#iT		4	@T[EpE~  FKj    	AI"$$$SVV,,,g GM0OG{d16WGGO#$GF!/FNF
VW
%
%C LL--------OJ
Y!%j!9!9999999D)S
Y~k+T^{("2H4U!VVV
{X---	9%%%|"H[FqLGe 
84	4	41h kH,,,&*k&>&>xT[MiMiMkMk&>&l&l l l l l l lGNC
Y{%%"Tk1133CRC8DO  h!6;L;SVgVn  rC  rJ  ;K  "K  "K!2!999,T_>RTXTrtwxxdo,T_>NPTPnpsttd,T_>TVZVtvyzzd,T_>TVZVtvyzzd,T_>PR`beffdo,T_nm]`aad,T_>OQacfggd,T_>OQacfggd%-DKK GI[
\
\\
*	   
(??????sC   6Q= A/B- ,Q= -
D 7AC;;D  M;Q= =
RR
RRreturnc                   K   	 || j         vr%| j        |                                          d{V  | j                             |          \  }}||| j        t
          j        k    r2| j        t          j	        k    r| xj
        |j        j        dz
  z  c_
        |j        j        t          j        k    r|| j        v r| j        |= n| j        |                                          ||dfS # t"          $ r}dd|fcY d}~S d}~ww xY w)zo
		Returns an SMB message from the outstandingresponse dict, OR waits until the expected message_id appears.
		NrH   )rq   rr   waitpopr   r+   r,   r   r   SMB202r   rI   CreditCharger  r   r  clearr   )rD   rn   r  r  r"  s        r5   rU  zSMBConnection.recvSMB  s8     111

(
4
9
9
;
;;;;;;;;,00<<=3	k
N
k(444 1 888	SZ4q89	j8+++T333		'
	3":.44666
x
	   
a-s   C%C* *
D4C>8D>Dr  c                    | j         t          j        t          j        fv r| j        r|j        j        t          j        z  |j        _        t          j
        | j        |                                t          j                                                  }|d d         |j        _        d S d S | j        rE|                                }t#          | j        |t%          |                    }||j        _        d S d S )Nr   )r   r   r  SMB210r   rI   r  SMB2HeaderFlagSMB2_FLAGS_SIGNEDr    newto_bytesr!   sha256rA  r  r   r%   len)rD   r  rA  r  	signatures        r5   sign_messagezSMBConnection.sign_message!  s    	079J9QRRR
o 'z'.*JJCJXdos||~~w~FFMMOOF!#2#;CJ' '
 o %||~~H(CMMBBI$CJ% %r4   c                 @   | j         t          j        k    rt                              d          }t                      }|dz   |_        t          |          |_        | j         |_	        | j
        |_
        t          | j        t          |d          }|                    ||                                dd                   \  }|_        n| j         t          j        k    rt                              d          }t                      }|dz   |_        t          |          |_        | j         |_	        | j
        |_
        t          | j        t$          |          }|                    ||                                dd                   \  }|_        t'          ||          S )	Nr   s        r   r   r   r   r   s       )r   r   r   osurandomSMB2Header_TRANSFORMr   r  OriginalMessageSizer   rK   r"   r   r#   rA   r  r  r  r$   r   )rD   r  noncehdrr  enc_datas         r5   encrypt_messagezSMBConnection.encrypt_message5  sI   	]j,,,::b>>5			3$39 ]]3!]3>3=	T5r	B	B	B3 [[3<<>>"R%3HII8S]] }
...::b>>5			3$39 ]]3!]3>3=	T5	1	13 [[3<<>>"R%3HII8S] 
sH	%	%%r4   c                    |                                 }| j        du r| j        t          j        k    r|}n<| j        t          j        k    rt          |          }nt          d| j        z            t                      }t          |          |_
        | j        |_        t          j        |_        d|_        t          ||          S t          d          )NFz6Common compression type is %s but its not implemented!r   z$Chained compression not implemented!)r  r   r   r   r   r   lznt1_compressr    SMB2Header_COMPRESSION_TRANSFORMr  OriginalCompressedSegmentSizer  r   r  r  r   )rD   r  r  compressed_datacomp_hdrs        r5   compress_messagezSMBConnection.compress_message_  s    \\^^(	$--
/444OO1777$X..OO
LtOaa
b
bb.008,/MM8)#'#58 (-8>8?
(O
4
44	9	:	::r4   Nc                   K   	 t           j                                         | _        | j        t          j        k    rt          |t                    rd}| xj        dz  c_        ngd|j	        _
        d|j	        _        | j        |j	        _        | j        }| xj        dz  c_        |                     |                                           t          j                    | j        |<   | j                            |                                           d{V  |dfS |j	        j        t*          j        ur!| j        |j	        _        | xj        dz  c_        | j        |j	        _        |j	        j
        sd|j	        _
        | j        t          j        k    rd|j	        _        |j	        j        }||j        | j        |<   | j        | j        |                     |          }| j        du r|                     |           | j         du r/| j        (| !                    |                                          }n'|                     |                                           t          j                    | j        |<   | j                            |                                           d{V  |dfS # tD          $ r}d|fcY d}~S d}~ww xY w)zp
		Sends an SMB message to teh remote endpoint.
		msg: SMB2Message or SMBMessage
		Returns: MessageId integer
		r   rH   N   T)#r   r   r   r   r+   r,   r*  r   r   rI   r  rd  r  rB  r  rb   r4  rr   r   writerO  rb  CANCELrK   r-   rd   r   r   r   r  r   r  r   r  r   )rD   r  rg   rn   r"  s        r5   rT  zSMBConnection.sendSMBt  s     6'..004
k(444#z"" 	+Z	A SZSZ /SZ%Z	A	3<<>>***18D":.

!
'
'
7
77777777t 
j 222.CJ1.3:
*
!  CJ k(555CJ
$:n)+D:&
$);)G



$
$C
t##c
$&&4+=+I


s||~~
.
.CC 	#,,..)))074!*-		 	&	&s||~~	6	66666666
d
	   
'>>>>>>s%   DJ- FJ- -
K7K :K Kr:   c                   K   	 | j         dk    s| j        t          j        k    rt	                      |t          d          t                      }||_        d|_        t                      }t          j        |_        t          ||          }|                     |           d{V \  }}|||                     |           d{V \  }}}|||j        j        t$          j        k    r=t(                              ||          }	|	| j        |j        j        <   |	| j        |<   |	dfS |j        j        t$          j        k    rt5                      |j        j        t$          j        k    r!d| _         t9          dt$          j                  t9          d|j        j                  # t
          $ r}
d|
fcY d}
~
S d}
~
ww xY w)ze
		share_name MUST be in "\\server\share" format! Server can be NetBIOS name OR IP4 OR IP6 OR FQDN
		TNzShare name is None!r   zsession delted )r   r   r+   r/   r   r   TREE_CONNECT_REQPathr  ra  rb  TREE_CONNECTrO  r   rT  rU  rI   r  r   rP  r7   rW   r   rJ   r   BAD_NETWORK_NAMESMBIncorrectShareNameUSER_SESSION_DELETEDr  )rD   r:   rN   rI   r  rn   r!  r2  r   rV   r"  s              r5   tree_connectzSMBConnection.tree_connect  s     )
T!!T[4G4N%N%N
!
#
##
)
*
**77<7=6 -6>	FG	$	$3<<,,,,,,,,?:s	o
IZ00000000<4C	o
I
kH,,,		"	"4	4	4B35DT[/0.0D
+t8Oh777

!
!!h;;;D
')F
G
GG r4;-
.
..	   
'>>>>>>s%   D%F' *A=F' '
F?1F:4F?:F?
file_attrsimpresonation_levelc                 @  K   	 | j         dk    s| j        t          j        k    rt	                      || j        vrt          d|z            t                      }|	|_        ||_	        ||_
        ||_        ||_        ||_        ||_        ||_        |
|_        t#                      }t$          j        |_        ||_        t-          ||          }|                     |           d {V \  }}|||                     |           d {V \  }}}|||j        j        t6          j        k    rTt:                              ||||	          }|| j        |j         <   |dk    r|j!        j"        |j!        d fS |j!        j"        d fS |j        j        t6          j#        k    r,tI          d|j        j        j%        z  |j        j                  tI          d|j        j        j%        z  |j        j                  # t          $ r}|dk    r
d d |fcY d }~S d |fcY d }~S d }~ww xY w)NTUnknown Tree ID! %s%s)&r   r   r+   r/   r   r   r   
CREATE_REQRequestedOplockLevelImpersonationLevelDesiredAccessr   ShareAccessCreateDispositionCreateOptionsNameCreateContextra  rb  CREATErO  rJ   r   rT  rU  rI   r  r   rP  rZ   rh   r   r\   rN   rf   ACCESS_DENIEDr  r
  )rD   r;   	file_pathdesired_access
share_modecreate_optionscreate_dispositionr  r  r]   create_contextsreturn_replyrN   rI   r  rn   r!  r2  r   rg   r"  s                        r5   createzSMBConnection.create  sQ     1
T!!T[4G4N%N%N
!
#
##T---
)G3
4
44\\7#/7!47,7 *7(7%77"077<*76 '6>6=	FG	$	$3<<,,,,,,,,?:s	o
IZ00000000<4C	o
I
kH,,,		%	%dGY	M	MB')D$tLt33<$$h444
tdk055t{7I
J
JJ tdk055t{7I
J
JJ	   dq=
'>>>>>>s7   E0G5 5G5 A2G5 5
H?
H	HHHHr   c                   K   	 | j         dk    s| j        t          j        k    rt	                      || j        vrt          d|z            || j        vrt          d|z            | j        |         j        4 d{V  t                      }t          j        |_        ||_        | j        t          j        k    r| j        dk    rd|dz
  dz  z   |_        nt'          d|          }t)                      }||_        ||_        ||_        d|_        d|_        t5          ||          }|                     || j        |                    d{V \  }}	|	|	|                     |           d{V \  }
}}	|	|	|
j        j        t>          j         k    r+|
j!        j"        |
j!        j#        dfcddd          d{V  S |
j        j        t>          j$        k    r	 ddd          d{V  d	S |
j        j        t>          j%        k    r!|
j!        j"        ddfcddd          d{V  S tM          d
|
j        j                  # 1 d{V swxY w Y   dS # t          $ r}dd|fcY d}~S d}~ww xY w)a  
		Will issue one read command only then waits for reply. To read a whole file you must use a filereader logic! 
		returns the data bytes and the remaining data length
		
		IMPORTANT: remaning data length is dependent on the length of the requested chunk (length param) not on the actual file length.
		to get the remaining length for the actual file you must set the length parameter to the correct file size!
		
		If and EOF happens the function returns an empty byte array and the remaining data is set to 0
		Tr  Unknown File ID! %sNrH      r   rg   )r4   r   NzSMB READ Error!)'r   r   r+   r/   r   r   r   r   rd   ra  rb  READrO  rJ   r   r   r  r   r  rn  READ_REQLengthr  rf   MinimumCountRemainingBytesr   rT  rU  rI   r  r   rP  rN   r~  DataRemainingEND_OF_FILEBUFFER_OVERFLOWr  )rD   r;   r\   offsetlengthrI   rN   r  rn   r!  r2  r   r"  s                r5   r   zSMBConnection.read  s     4
T!!T[4G4N%N%N
!
#
##T---
)G3
4
44T)))
)G3
4
44"7+2 (? (? (? (? (? (? (? (?F!&FNFM
  1 888T=UY]=]=]&1*!66V%VjjGGNGNGNGG
VW
%
%C LL43G3PLQQQQQQQQOJ
Yj11111111LD!S
Y{X---L!;TA=(? (? (? (? (? (? (? (? (? (? (? (? (? (?@ 
	x3	3	3C(? (? (? (? (? (? (? (? (? (? (? (? (? (?F 
	x7	7	7LD(K(? (? (? (? (? (? (? (? (? (? (? (? (? (?P )4;+=>>>Q(? (? (? (? (? (? (? (? (? (? (? (? (? (? (? (?T 
   
a-sa   A<I  D-I-I  II .(II )I
II II 
I/ I*$I/*I/c           	        K   	 | j         dk    s| j        t          j        k    rt	                      || j        vrt          d|z            || j        vrt          d|z            | j        |         j        4 d{V  t                      }t          j        |_        ||_        t          |          | j        k    r|d| j                 }| j        t"          j        k    r)| j        dk    rdt          |          dz
  dz  z   |_        n%|dt+          dt          |                             }t-                      }t          |          |_        ||_        ||_        ||_        t7          ||          }|                     || j        |                    d{V \  }}	|	|	|                     |           d{V \  }
}}	|	|	|
j        j        t@          j!        k    r |
j"        j#        dfcddd          d{V  S tI          d|
j        j                  # 1 d{V swxY w Y   dS # t          $ r}d|fcY d}~S d}~ww xY w)	aW  
		This function will send one packet only! The data size can be larger than what one packet allows, but it will be truncated
		to the maximum. 
		Also, there is no guarantee that the actual sent data will be fully written to the remote file! This will be indicated in the returned value.
		Use a high-level function to get a full write.
		
		Tr  r  NrH   r  r  r  )%r   r   r+   r/   r   r   r   r   rd   ra  rb  WRITErO  rJ   r  r   r   r   r  r   r  rn  	WRITE_REQr  r  rf   Datar   rT  rU  rI   r  r   rP  rN   Countr  )rD   r;   r\   r  r  rI   rN   r  rn   r!  r2  r   r"  s                r5   r  zSMBConnection.writeW  s     +
T!!T[4G4N%N%N
!
#
##T---
)G3
4
44T)))
)G3
4
44"7+2  0  0  0  0  0  0  0  0F!'FNFM
4yy4$$$#$##$T 1 888T=UY]=]=]#d))a-E!99V&#eCII&&&'TkkGYYGNGNGNGL
VW
%
%C LL43G3PLQQQQQQQQOJ
Yj11111111LD!S
Y{X---L$; 0  0  0  0  0  0  0  0  0  0  0  0  0  0@ DK.///A 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0B 
   
'>>>>>>sI   A<H1  E1H1H1 H
H((H1 +H(,H1 1
I	;I>I	I	r  c                 |  K   	 | j         dk    s| j        t          j        k    rt	                      || j        vrt          d|z            || j        vrt          d|z            t                      }||_	        ||_
        ||_        ||_        ||_        ||_        t                      }	t           j        |	_        ||	_        t)          |	|          }
|                     |
           d{V \  }}|||                     |           d{V \  }}}|||j        j        t2          j        k    r|t6          j        k    r,t;          j        |j        j        t@          j!                  dfS |t6          j"        k    r>|t          j#        k    r tI          j        |j        j                  dfS |j        j        dfS |t6          j%        k    r|j        j        dfS |t6          j&        k    r|j        j        dfS |j        j        dfS tO          d|j        j                  # t          $ r}d|fcY d}~S d}~ww xY w)a  
		Queires the file or directory for specific information. The information returned is depending on the input parameters, check the documentation on msdn for a better understanding.
		The resturned data can by raw bytes or an actual object, depending on wther your info is implemented in the library.
		Sorry there are a TON of classes to deal with :(
		
		IMPORTANT: in case you are requesting big amounts of data, the result will arrive in chunks. You will need to invoke this function until None is returned to get the full data!!!
		Tr  r  N)object_typer  )(r   r   r+   r/   r   r   r   r   QUERY_INFO_REQInfoTyper   AdditionalInformationr  rf   r  ra  rb  
QUERY_INFOrO  rJ   r   rT  rU  rI   r  r   rP  QueryInfoTypeSECURITYr   r   rN   r   SE_FILE_OBJECTFILEFileFullDirectoryInformationr   
FILESYSTEMQUOTAr  )rD   r;   r\   	info_typeinformation_classadditional_informationflagsdata_inrN   rI   r  rn   r!  r2  r   r"  s                   r5   
query_infozSMBConnection.query_info  se     :
T!!T[4G4N%N%N
!
#
##T---
)G3
4
44T)))
)G3
4
4477,7#97 7=7>7<6 +6>6=	FG	$	$3<<,,,,,,,,?:s	o
IZ00000000<4C	o
I
kH,,,M****4<+<.Jghhhjnnn	m(	(	(]GGG-89JKKTQQ \$$	m.	.	.Lt##	m)	)	)Lt## Lt## r4;-
.
..	   
'>>>>>>sB   E,H# 1?H# 1H# ?H# H# ;H# 	H# #
H;-H60H;6H;r   c                   K   	 | j         dk    s| j        t          j        k    rt	                      || j        vrt          d|z            || j        vrt          d|z            t                      }||_	        d|_
        |dk    r|xj
        t          j        z  c_
        ||_        ||_        ||_        t!                      }	t"          j        |	_        ||	_        t+          |	|          }
|                     |
           d{V \  }}|||                     |           d{V \  }}}|||j        j        t4          j        k    r>|t8          j        k    r t=          j        |j         j!                  dfS |j         j!        dfS |j        j        t4          j"        k    rdS t          d|j        j        z            # t          $ r}d|fcY d}~S d}~ww xY w)z
		
		IMPORTANT: in case you are requesting big amounts of data, the result will arrive in chunks. You will need to invoke this function until None is returned to get the full data!!!
		Tr  r  r   N)NNzquery_directory reply: %s)#r   r   r+   r/   r   r   r   r   QUERY_DIRECTORY_REQFileInformationClassr  QueryDirectoryFlagSMB2_INDEX_SPECIFIED	FileIndexrf   FileNamera  rb  QUERY_DIRECTORYrO  rJ   r   rT  rU  rI   r  r   rP  r   r  r   r   rN   r  NO_MORE_FILES)rD   r;   r\   search_patternresume_indexr  maxBufferSizer  rN   rI   r  rn   r!  r2  r   r"  s                   r5   query_directoryzSMBConnection.query_directory  s     
-
T!!T[4G4N%N%N
!
#
##T---
)G3
4
44T)))
)G3
4
44 !""7#477=aMM'<<MM$77>$76 06>6=	VW	%	%3<<,,,,,,,,?:s	o
IZ00000000<4C	o
I
kH,,,MFFF,78IJJDPP Lt##h444:
/$+2DD
E
EE	   
'>>>>>>s0   E9G >G G (G 
GGGGrD  c                   K   	 | j         dk    s| j        t          j        k    rt	                      t                      }||_        ||_        ||_        ||_	        ||_
        t                      }t          j        |_        ||_        t!          ||          }	|                     |	           d {V \  }
}|||                     |
           d {V \  }}}|||j        j	        d fS # t(          $ r}d |fcY d }~S d }~ww xY w)NT)r   r   r+   r/   r   	IOCTL_REQCtlCoderf   r  r~  MaxOutputResponsera  rb  IOCTLrO  rJ   r   rT  rU  rN   r   )rD   r;   r\   ctlcoder  r  r  rN   rI   r  rn   r!  r2  r   r"  s                  r5   ioctlzSMBConnection.ioctl  s3     
T!!T[4G4N%N%N
!
#
##[[77?7>7=7>076 &6>6=	VW	%	%3<<,,,,,,,,?:s	o
IZ00000000<4C	o
I
,
t
##	   
'>>>>>>s   C'C, ,
D6C?9D?Dc                 "  K   	 | j         dk    s| j        t          j        k    rt	                      t                      }||_        ||_        t                      }t          j
        |_        ||_        t          ||          }|                     |           d{V \  }}|||                     |           d{V \  }	}
}|||	j        j        t$          j        k    r|| j        v r| j        |= dS # t*          $ r}d|fcY d}~S d}~ww xY w)z{
		Closes the file/directory/pipe/whatever based on file_id. It will automatically remove all traces of the file handle.
		TNr  )r   r   r+   r/   r   	CLOSE_REQr  rf   ra  rb  CLOSErO  rJ   r   rT  rU  rI   r  r   rP  r   r   )rD   r;   r\   r  rN   rI   r  rn   r!  r2  r   r"  s               r5   r9  zSMBConnection.close"  s@     
T!!T[4G4N%N%N
!
#
##[[77=7>6 &6>6=	VW	%	%3<<,,,,,,,,?:s	o
I Z00000000<4C	o
I
kH,,,$&&&		g	&
*	   
'>>>>>>s   C0C6 6
D D	D	Dc                   K   	 | j         dk    s| j        t          j        k    rt	                      t                      }||_        t                      }t          j	        |_
        ||_        t          ||          }|                     |           d{V \  }}|||                     |           d{V \  }}	}||dS # t          $ r}
d|
fcY d}
~
S d}
~
ww xY w)zK
		Flushes all cached data that may be on the server for the given file.
		TNr  )r   r   r+   r/   r   	FLUSH_REQrf   ra  rb  FLUSHrO  rJ   r   rT  rU  r   )rD   r;   r\   rN   rI   r  rn   r!  r2  r   r"  s              r5   flushzSMBConnection.flushB  s     
T!!T[4G4N%N%N
!
#
##[[77>6 &6>6=	VW	%	%3<<,,,,,,,,?:s	o
IZ00000000<4C	o
I
*	   
'>>>>>>s   B>C 
CCCCc                   K   	 | j         dk    s| j        t          j        k    rt	                      t                      }t                      }t          j        |_	        t          ||          }|                     |           d{V \  }}|||                     |           d{V \  }}}||dS # t          $ r}d|fcY d}~S d}~ww xY w)z
		Logs off from the server, effectively terminates the session. 
		The underlying connection will still be active, so please either clean it up manually or dont touch this function
		For proper closing of the connection use the terminate function
		TNr  )r   r   r+   r/   r   
LOGOFF_REQra  rb  LOGOFFrO  r   rT  rU  r   	rD   rN   rI   r  rn   r!  r2  r   r"  s	            r5   logoffzSMBConnection.logoff\  s      
T!!T[4G4N%N%N
!
#
##\\76 '6>	VW	%	%3<<,,,,,,,,?:s	o
IZ00000000<4C	o
I
*	   
'>>>>>>s   B0B6 6
C C	C	Cc                 2  K   	 | j         dk    s| j        t          j        k    rt	                      t                      }t                      }t          j        |_	        t          ||          }|                     |           d{V \  }}|||                     |           d{V \  }}}|||j        j        t          j        k    rdS dt#          d|j        j        j        z  |j        j                  fS # t&          $ r}d|fcY d}~S d}~ww xY w)zi
		Issues an ECHO request to the server. Server will reply with and ECHO response, if it's still alive
		TNr  r  )r   r   r+   r/   r   ECHO_REQra  rb  ECHOrO  r   rT  rU  rI   r  r   rP  r  r
  r   r  s	            r5   r>  zSMBConnection.echov  s7     
T!!T[4G4N%N%N
!
#
##ZZ76 %6>	VW	%	%3<<,,,,,,,,?:s	o
IZ00000000<4C	o
I
kH,,,:dT[%7%<<dk>PQQQQ	   
'>>>>>>s$   C
C> -C> >
DDDDc                   K   	 | j         dk    s| j        t          j        k    rt	                      t                      }t                      }t          j        |_	        ||_
        t          ||          }|                     |           d{V \  }}|||                     |           d{V \  }}}|||j        j        t           j        k    rvg }	| j        |         j        }
| j        D ]=}| j        |         j        |k    r%|	                    | j        |         j                   >|	D ]
}| j        |= | j        |= | j        |
= dS # t2          $ r}d|fcY d}~S d}~ww xY w)zL
		Disconnects from tree, removes all file entries associated to the tree
		TNr  )r   r   r+   r/   r   TREE_DISCONNECT_REQra  rb  TREE_DISCONNECTrO  rJ   r   rT  rU  rI   r  r   rP  r   r:   r   r;   r]  r\   r   r   )rD   r;   rN   rI   r  rn   r!  r2  r   del_file_idsr:   fer\   r"  s                 r5   tree_disconnectzSMBConnection.tree_disconnect  s     !
T!!T[4G4N%N%N
!
#
## ""76 06>6=	VW	%	%3<<,,,,,,,,?:s	o
IZ00000000<4C	o
I
kH,,,L)'2=J" < <R (G33$.r2:;;; ' '		g	&	& )#J/
*	   
'>>>>>>s   EE 
E%E E% E%c                   K   	 | j         dk    s| j        t          j        k    rt	                      t                      }t                      }t          j        |_	        t          ||          }||j        _        |                     |           d{V \  }}|||                     |           d{V \  }}}||dS # t          $ r}d|fcY d}~S d}~ww xY w)z6
		Issues a CANCEL command for the given message_id
		TNr  )r   r   r+   r/   r   
CANCEL_REQra  rb  r  rO  r   rI   r  rT  rU  r   )	rD   rn   rN   rI   r  r!  r2  r   r"  s	            r5   r~   zSMBConnection.cancel  s     
T!!T[4G4N%N%N
!
#
##\\76 '6>	VW	%	%3$3:<<,,,,,,,,?:s	o
IZ00000000<4C	o
I
*	   
'>>>>>>s   B<C 
CCCCc                    K   	 | j         dk    s| j        t          j        k    r%	 | j        | j                                         dS dS | j        t          j        k    rt          | j        	                                          D ]9}	 t          j        |                     |          d           d{V  3#  Y 7xY w	 t          j        |                                 d           d{V  n# t          $ r
}Y d}~nd}~ww xY wt          j        |                                 d           d{V  t!          j        d           nn# t          j        $ r& Y | j        | j                                         dS dS t          $ r.}t!          j        dt'          |          z             Y d}~nd}~ww xY w| j        | j                                         dS dS # | j        | j                                         w w xY w)z
		Use this function to properly terminate the SBM connection.
		Terminates the connection. Closes all tree handles, logs off and disconnects the TCP connection.
		TNrH   r   zTerminate finished!zterminate error %s)r   r   r+   r/   r   rz   r.   listr   keysrb   r   r  r  r   r  r   r  r  r  )rD   r;   r"  s      r5   r   zSMBConnection.terminate  sr     %
T!!T[4G4N%N%N
,  ,""$$$$$ -,) k(0000557788  T11'::aHHHHHHHHHHH
d
DKKMMQ77777777777 
 
 
	TTTT
 
	$//++q	9	9	99999999	<%&&&&		 
 
 
	  ,""$$$$$ -, 
 / / /	<$s1vv-......../  ,""$$$$$ -,d ,""$$$$ -s~    E 	=E /B76E 7B;9E ?.C. -E .
D8E =DAE G F3G <	F3$F.)G .F33G #G=c                   K   d| _         d }t          d           |                                  d {V  t          d           t                      }t	                      }t
          j        |_        t          ||          }| 	                    ||           d {V \  }}	 t          j        |                     |          d           d {V }dS #  Y dS xY w)	NTc                    t          d           |                                 }t          |          }t                      }t	          |          |_        t          j        |_        t          j
        |_        d|_        t          ||          S )NzCallback is here!r   )printr  r  r  r  r  r   r   r  r   r   r  r  r   )r  r  r  r  s       r5   compress_callbackz1SMBConnection.ghosting.<locals>.compress_callback  sp    	llnn8#H--?.008,/MM8)#6#<8 (-8>8?
(O
4
44r4   z[################################### TESTING compression ###################################zY################################### SPLOIT SPLOIT SPLOIT ################################)compression_cbg      ?r   F)r   r'  r>  r  ra  rb  r  rO  r   rT  rb   r   rU  )rD   r(  rN   rI   r  rn   r!  r2  s           r5   ghostingzSMBConnection.ghosting  s     $
5 
5 
5 efff		 cddd JJ'&$&.FG$$#,,s=N,OOOOOOOO/*c j!9!9SIII
I
I
I
I
I
I4
$
%%s   #/C C)TF)Fr9   )r   r   )r   )>r0   r1   r2   __doc__	SMBTargetboolrE   r   r   r   r   r5  r#  r'  r)  r  r  r  bytesrB  r  r  r   r   r   rU  r  r   r  r   r  rT  r  r  r  ImpersonationOplockLevelSMB2_OPLOCK_LEVEL_NONEr   r  r   r  r  r  r   FileStandardInformationr  r  r  IOCTLREQFlagsIS_IOCTLr
  	CloseFlagr   r9  r  r  r>  r  r~   r   r*  r3   r4   r5   r   r      s        e e9 ed eRV e e e eN  8 8 8  
  (Z Z Zz  8  (  :       .  *0U 0 0 0 0
d d d dL[ [ [ [|	+ui2O(P    8
%K 
% 
% 
% 
%('&E '&m '& '& '& '&T;O ; ; ; ;*< <[ < < < <~- - - - -^ qC  qQ  bm  bD  X\  mr 2 2  xF 2  \n 2 2 2 2h> > > >B3 3 3 3j ;H:Lbo  cH  cd  no  {} B B B BH EHXYo|  pZ  lp  z{ 2 2 2 2h :>}G]qv    < 2;    @  4  4  6% % %P  2% % %B% % % % %r4   r   c                 f   K   |                                  }|                                 d {V  d S r9   )get_connectionr#  )cuconns     r5   ctestr:    s=      
zz||r4   __main__)SMBConnectionFactory   z5smb+ntlm-password://TEST\victim:Passw0rd!1@10.10.10.2)Yenumrb   r   r   r   typingr   aiosmbr   aiosmb.commons.exceptionsaiosmb.transport.netbiosr   !aiosmb.protocol.smb.command_codesr   aiosmb.wintypes.ntstatusr   aiosmb.protocol.smb.headerr	   r
   aiosmb.protocol.smb.messager   aiosmb.protocol.smb.commonsr   aiosmb.protocol.smb.commandsr   aiosmb.protocol.smb2.messager   r   r   'aiosmb.protocol.smb2.commands.negotiater   r   r   r   r   r   r   r   r   r   aiosmb.protocol.smb2.commandsaiosmb.protocol.smb2.headers"aiosmb.protocol.smb2.command_codesaiosmb.protocol.common.aiosmb.wintypes.dtyp.constrcuted_security.guid-aiosmb.wintypes.fscc.structures.fileinfoclassr   <aiosmb.wintypes.fscc.structures.FileFullDirectoryInformationr   #aiosmb.wintypes.fscc.FileAttributesr   winacl.dtyp.security_descriptorr   winacl.functions.constantsr    aiosmb.commons.connection.target	unicryptor    r!   unicrypto.symmetricr"   r#   r$   unicrypto.cmacr%   unicrypto.kdfr&   !aiosmb.protocol.compression.lznt1r'   r  r(   r	  asysocks.unicomm.clientr)   Enumr+   r7   rZ   rj   r   r:  r0   !aiosmb.commons.connection.factoryr<  setLevelurlfrom_urlr8  r   r3   r4   r5   <module>ra     s/                       ' ' ' ' 6 6 6 6 6 6 8 8 8 8 8 8 - - - - - - E E E E E E E E 2 2 2 2 2 2 7 7 7 7 7 7 > > > > > > T T T T T T T T T TL L L L L L L L L L L L L L L L L L L L L L L L , + + + * * * * 0 0 0 0 $ $ $ $ < < < < G G G G G G i i i i i i > > > > > > ? ? ? ? ? ? 5 5 5 5 5 5 . . . .             7 7 7 7 7 7 7 7 7 7 # # # # # # ) ) ) ) ) ) H H H H H H L L L L L L - - - - - -    $)          4       .' ' ' ' ' ' ' 'RK K K K K K K K\,   zCCCCCC? $#C((UU2YY r4   