o
    �õ±jG$  ã                   @   s†   d dl Z d dlmZ d dlmZ d dlmZ d dlT G dd„ dƒZdd	„ Z	d
d„ Z
ddd„Zdd„ ZedkrAd dlmZ e	ƒ  dS dS )é    N)ÚFields)ÚNegotiateFlags)ÚVersion)Ú*c                   @   sP   e Zd Zddd„Zeddd„ƒZdd	„ Zedd
d„ƒZeddd„ƒZdd„ Z	dS )ÚNTLMAuthenticateTc                 C   sv   d| _ d| _d | _d | _d | _d | _d | _d | _d | _d | _	d | _
d | _d | _d | _d | _d | _d | _d | _|| _d S )Ns   NTLMSSP é   )Ú	SignatureÚMessageTypeÚLmChallengeResponseFieldsÚNtChallengeResponseFieldsÚDomainNameFieldsÚUserNameFieldsÚWorkstationFieldsÚEncryptedRandomSessionKeyFieldsr   r   ÚMICÚPayloadÚLMChallengeÚNTChallengeÚ
DomainNameÚUserNameÚWorkstationÚEncryptedRandomSessionÚ_use_NTLMv2)Úselfr   © r   úŸ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/asyauth/protocols/ntlm/messages/authenticate.pyÚ__init__
   s&   
zNTLMAuthenticate.__init__Nó                   c	                 C   s  t ƒ }	d|	_d}
| tj@ r|stdƒ‚||	_|
d7 }
|d ur%||	_|
d7 }
|rD| ¡ }|	 j|7  _tt	|ƒ|
ƒ|	_
|
t	|ƒ7 }
||	_ntddƒ|	_
|ri| ¡ }|	 j|7  _tt	|ƒ|
ƒ|	_|
t	|ƒ7 }
||	_ntd|
ƒ|	_|r�| d¡}|	 j|7  _tt	|ƒ|
ƒ|	_|
t	|ƒ7 }
||	_ntd|
ƒ|	_|rµ| d¡}|	 j|7  _tt	|ƒ|
ƒ|	_|
t	|ƒ7 }
||	_ntd|
ƒ|	_|rÛ| d¡}|	 j|7  _tt	|ƒ|
ƒ|	_|
t	|ƒ7 }
||	_ntd|
ƒ|	_|rþ|}|	 j|7  _tt	|ƒ|
ƒ|	_|
t	|ƒ7 }
||	_ntd|
ƒ|	_| |	_|	S )Nó    é@   z.NEGOTIATE_VERSION set but no Version supplied!é   é   r   úutf-16le)r   r   r   ÚNEGOTIATE_VERSIONÚ	Exceptionr   r   Úto_bytesr   Úlenr
   r   r   r   Úencoder   r   r   r   r   r   r   r   )ÚflagsÚ
domainnameÚworkstationnameÚusernameÚencrypted_sessionÚlm_responseÚnt_responseÚversionÚmicÚauthÚpayload_posÚdatar   r   r   Ú	construct#   sn   



zNTLMAuthenticate.constructc                 C   sÄ   d}|| j 7 }|| jjdddd�7 }|| j ¡ 7 }|| j ¡ 7 }|| j ¡ 7 }|| j ¡ 7 }|| j ¡ 7 }|| j ¡ 7 }|| j	jdddd�7 }| j
rQ|| j
 ¡ 7 }| jd ur[|| j7 }|| j7 }|S )Nr   é   ÚlittleF©Ú	byteorderÚsigned)r   r	   r%   r
   r   r   r   r   r   r   r   r   r   ©r   Útr   r   r   r%   o   s    



zNTLMAuthenticate.to_bytesc                 C   s   t jt | ¡|d�S )N)r   )r   Úfrom_bufferÚioÚBytesIO)Úbbuffr   r   r   r   Ú
from_bytesƒ   s   zNTLMAuthenticate.from_bytesc                 C   s
  t |ƒ}|  d¡|_tj|  d¡ddd�|_t | ¡|_t | ¡|_	t | ¡|_
t | ¡|_t | ¡|_t | ¡|_ttj|  d¡ddd�ƒ|_|jtj@ rTt | ¡|_|jtj@ r`|  d¡|_|  ¡ }|  ¡ |_|jr‘|j	jdkr‘|  |jjtj¡ t | ¡|_|  |j	jtj¡ t | ¡|_n|  |jjtj¡ t | ¡|_|  |j	jtj¡ t  | ¡|_|  |j
jtj¡ |  |j
j¡ !d¡|_"|  |jjtj¡ |  |jj¡ !d¡|_#|  |jjtj¡ |  |jj¡ !d¡|_$|  |jjtj¡ |  |jj¡|_%|  |tj¡ |S )	Nr    r5   r6   Fr7   r!   é   r"   )&r   Úreadr   Úintr@   r	   r   r<   r
   r   r   r   r   r   r   r#   r   ÚNEGOTIATE_ALWAYS_SIGNr   Útellr   r   ÚlengthÚseekÚoffsetr=   ÚSEEK_SETÚLMv2Responser   ÚNTLMv2Responser   Ú
LMResponseÚNTLMv1ResponseÚdecoder   r   r   r   )Úbuffr   r1   ÚcurrPosr   r   r   r<   ‡   sF   
zNTLMAuthenticate.from_bufferc                 C   sà   d}|dt | jƒ 7 }|dt | jƒ 7 }|dt | jƒ 7 }|dt | jƒ 7 }|dt | jr1| j ¡ ndƒ 7 }|dt | jƒ 7 }|d	t | jƒ 7 }|d
t | j	ƒ 7 }|dt | j
ƒ 7 }|dt | jƒ 7 }|dt | j ¡ ƒ 7 }|S )Nz== NTLMAuthenticate ==
zSignature     : %s
zMessageType   : %s
zNegotiateFlags: %s
zVersion       : %s
zMIC           : %s
ÚNonezLMChallenge   : %s
zNTChallenge   : %s
zDomainName    : %s
zUserName      : %s
zWorkstation   : %s
zEncryptedRandomSession: %s
)Úreprr   r	   r   r   r   Úhexr   r   r   r   r   r   r:   r   r   r   Ú__repr__¼   s    zNTLMAuthenticate.__repr__)T)NNNNNNNr   )
Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr4   r%   r@   r<   rT   r   r   r   r   r   	   s    
K4r   c                   C   s   t ƒ  tƒ  tƒ  d S ©N)Útest_reconstrutÚtest_constructÚtest_2r   r   r   r   ÚtestË   s   
r]   c                  C   s$   t  d¡} t | ¡}tt|ƒƒ d S )Na©  4e 54 4c 4d 53 53 50 00 03 00 00 00 18 00 18 006c 00 00 00 54 00 54 00 84 00 00 00 0c 00 0c 0048 00 00 00 08 00 08 00 54 00 00 00 10 00 10 005c 00 00 00 10 00 10 00 d8 00 00 00 35 82 88 e205 01 28 0a 00 00 00 0f 44 00 6f 00 6d 00 61 0069 00 6e 00 55 00 73 00 65 00 72 00 43 00 4f 004d 00 50 00 55 00 54 00 45 00 52 00 86 c3 50 97ac 9c ec 10 25 54 76 4a 57 cc cc 19 aa aa aa aaaa aa aa aa 68 cd 0a b8 51 e5 1c 96 aa bc 92 7beb ef 6a 1c 01 01 00 00 00 00 00 00 00 00 00 0000 00 00 00 aa aa aa aa aa aa aa aa 00 00 00 0002 00 0c 00 44 00 6f 00 6d 00 61 00 69 00 6e 0001 00 0c 00 53 00 65 00 72 00 76 00 65 00 72 0000 00 00 00 00 00 00 00 c5 da d2 54 4f c9 79 9094 ce 1c e9 0b c9 d0 3e)ÚbytesÚfromhexr   r@   ÚprintrR   )r3   Ú	challenger   r   r   r\   Ð   s   

r\   c                 C   sr   t dƒ | st d¡}n| }t |¡}t t|ƒƒ | ¡ }t dƒ t t|ƒƒ t dƒ t t|ƒƒ ||ks7J ‚d S )Nz=== reconstruct===Áx  4e544c4d5353500003000000180018007c000000180118019400000008000800580000000c000c0060000000100010006c00000010001000ac010000158288e20a00d73a0000000f0d98eb57e9c52820709c99b98ca321a15400450053005400760069006300740069006d00570049004e0031003000580036003400000000000000000000000000000000000000000000000000fade3940b9381c53c91ddcdd0d44000b0101000000000000aec600bfc5fdd4011bfa20699d7628730000000002000800540045005300540001001200570049004e003200300031003900410044000400120074006500730074002e0063006f007200700003002600570049004e003200300031003900410044002e0074006500730074002e0063006f007200700007000800aec600bfc5fdd40106000400020000000800300030000000000000000000000000200000527d27f234de743760966384d36f61ae2aa4fc2a380699f8caa600011b486d890a0010000000000000000000000000000000000009001e0063006900660073002f00310030002e00310030002e00310030002e003200000000000000000000000000fd67edfb41c09465a91fd733deb0b55bz====== reconstructed ====z====== original ====)r`   r^   r_   r   r@   rR   r%   Úhexdump)r3   Úauth_test_datara   Úauth_test_data_verifyr   r   r   rZ   Õ   s   
rZ   c                   C   s   d S rY   r   r   r   r   r   r[   å   s   r[   Ú__main__)rc   rY   )r=   Ú(asyauth.protocols.ntlm.structures.fieldsr   Ú1asyauth.protocols.ntlm.structures.negotiate_flagsr   Ú)asyauth.protocols.ntlm.structures.versionr   Ú4asyauth.protocols.ntlm.structures.challenge_responser   r]   r\   rZ   r[   rU   Úasyauth.utils.hexdumprc   r   r   r   r   Ú<module>   s     C

ý