o
    j                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ G dd dZG dd deZG dd deZdS )    N)sha256)logger)UniProto)RequestManager)ISC_REQ)NTLMCredential)KerberosCredential)SPNEGOCredential)CREDSSPCredential)UniCredentialc                   @   s0   e Zd Zdd Zdd Zed
defddZd	S )HTTPAuthManagerc                 C   s    || _ d| _| | _|| _d S )Nstart)sessionstatusbuild_contextauthobj
credentialselfr   r    r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/asysocks/unicomm/protocol/client/http/auth.py__init__   s   

zHTTPAuthManager.__init__c                    s   d S )Nr   )r   requestmanagerr   r   r   authenticate   s   zHTTPAuthManager.authenticateauto	auth_typec                 C   s  |  }t|tr| }|dkr4t|tdu rt| |S t|ttfdu r0t|g}t| |S td|dkr~t|tdu rDt| |S t|ttfdu rWt|g}t| |S t	|ddu rvt|j
ttfdu rvtt|j
g}t| |S td|jj |dkrt|tdu rt| |S t|ttfdu rt|g}t| |S t	|ddu rt|j
ttfdu rtt|j
g}t| |S td|jj td	||jjf )
Nr   TzNo supported auth type found!spnegor   z<Credential type %s cannot be used for SPNEGO authentication!credsspz=Credential type %s cannot be used for CREDSSP authentication!z2Unsupported auth type! Authtype: %s Credential: %s)lower
isinstancer   r   r	   HTTPAuthManagerNegotiater   r   	Exceptionhasattrr   copydeepcopy	__class____name__r
   HTTPAuthManagerCredSSP)r   r   r   r   r   r   from_credential   s>   






"



"
zHTTPAuthManager.from_credentialN)r   )r&   
__module____qualname__r   r   staticmethodstrr(   r   r   r   r   r      s
    r   c                   @   "   e Zd ZdefddZdd ZdS )r'   r   c                 C      t | || d| _d | _d S NFr   r   
auth_stepscb_datar   r   r   r   r   C      
zHTTPAuthManagerCredSSP.__init__c              	      s  d| _ d }d}| jjD ]}|j} 	 tjtjB tjB }d|jj	
 |f }| jj|||dI d H \}}}	|	d ur;|	|du rF|
j dkrF|
S dt|  }g }|d	|f |jD ]}|| q[t| j|j|j||j|j|jd
4 I d H }
|du r|
j dkrtd|
j  |
 I d H  |
jjd ur|
jjdu r|
j| _|
d}|d u st|dkr|du r|
j dkr|
W  d   I d H  S td|D ]}|dr|} nq|du r|
j dkr|
W  d   I d H  S td|dd  ! }|dkr|du r|
j dkr|
W  d   I d H  S tdt"|}W d   I d H  n1 I d H s/w   Y  |du rA|
j dkrA|
S q)NprogressUNKNOWNT
HTTP/%s@%s)spnflagsF   zCredSSP Authorizationheadersdataneed_length	transportAuthentication failed! %swww-authenticater   z8Authentication failed! No www-authenticate header found!CredSSPz/Authentication failed! No CredSSP header found!     z2Authentication failed! Empty CredSSP header found!)#r   r   credentialsdomainr   
CONNECTIONCONFIDENTIALITY	INTEGRITYr?   targetget_hostname_or_ipr   r   base64	b64encodedecodeappendr<   r   r   urlreq_typer=   r>   r!   read
connection	can_reuse
getheaderslen
startswithreplacestripencode	b64decode)r   r   	authreplyrF   credr8   
target_spnauthdatato_continueerrresponseauth_header
newheadersentryresponse_auth_headerrauthhresponse_header_data_b64r   r   r   r   H   sh   
,


*z#HTTPAuthManagerCredSSP.authenticateN)r&   r)   r*   r
   r   r   r   r   r   r   r'   B       r'   c                   @   r-   )r    r   c                 C   r.   r/   r0   r   r   r   r   r      r3   z!HTTPAuthManagerNegotiate.__init__c              	      sB  d| _ d }d}| jjD ]}|j} 	 tjtjB tjB tjB tj	B }|j
jjtjkrB| jd u rB|j
j }|d urBdt|  | _d|j
j |f }| jj|||| jdI d H \}}	}
|
d urc|
|	du rs|j dkrstd	 |S d
t|  }g }|d|f |jD ]}|| qtd|  t| j|j |j!||j"|j#|j
d4 I d H V}td|j   td|j  |	du r|j dkrt$d|j  |% I d H  |j
jd ur|j
j&du r|j
| _
|'dd (d
d) * }t+|}W d   I d H  n1 I d H s	w   Y  |	du r |j dkr td	 |S q)Nr4   r5   Ts   tls-server-end-point:r6   )r7   r8   r2   Fr9   zAuthentication successful!z
Negotiate r:   zUpdated Headers: %sr;   zResponse Status: %szResponse Headers: %sr@   rA   r   rC   ),r   r   rE   rF   r   rG   rH   rI   USE_DCE_STYLEMUTUAL_AUTHr?   rJ   protocolr   CLIENT_SSL_TCPr2   rS   get_peer_certificater   digestrK   r   r   r   debugrL   rM   rN   rO   r<   r   r   rP   rQ   r=   r>   r!   rR   rT   rU   rX   rY   rZ   r[   )r   r   r\   rF   r]   r8   certdatar^   r_   r`   ra   rb   rc   rd   re   r   r   r   r      sT   
"

,*
z%HTTPAuthManagerNegotiate.authenticateN)r&   r)   r*   r	   r   r   r   r   r   r   r       ri   r    )rL   r#   hashlibr   %asysocks.unicomm.protocol.client.httpr   asysocks.unicomm.common.targetr   0asysocks.unicomm.protocol.client.http.requestmgrr   asyauth.common.winapi.constantsr   asyauth.common.credentials.ntlmr   #asyauth.common.credentials.kerberosr   !asyauth.common.credentials.spnegor	   "asyauth.common.credentials.credsspr
   asyauth.common.credentialsr   r   r'   r    r   r   r   r   <module>   s    2?