o
    j]                     @   s   d dl Z d dlZd dlZd dlZz
d dlmZmZ W n ey+   d dlmZmZ Y nw d dl	m
Z
mZ dZdZdZdZdZd	ZG d
d dZdS )    N)HTTPConnectionHTTPSConnection)ntlmLOGAutoBasicNTLM	NegotiateBearerDigestc                   @   sr   e Zd ZefddZdd Zdd Zdd Zd	d
 ZdddZ	dd Z
dd Zdd Zdd Zdd Zdd ZdS )HTTPClientSecurityProviderc                 C   sF   d | _ d | _d | _d| _d| _d| _d | _d | _|| _g | _	d | _
d S )N )%_HTTPClientSecurityProvider__username%_HTTPClientSecurityProvider__password#_HTTPClientSecurityProvider__domain#_HTTPClientSecurityProvider__lmhash#_HTTPClientSecurityProvider__nthash#_HTTPClientSecurityProvider__aesKey _HTTPClientSecurityProvider__TGT _HTTPClientSecurityProvider__TGS&_HTTPClientSecurityProvider__auth_type'_HTTPClientSecurityProvider__auth_types)_HTTPClientSecurityProvider__ntlmssp_infoself	auth_type r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/impacket/http.py__init__,   s   
z#HTTPClientSecurityProvider.__init__c                 C   s
   || _ d S Nr   r   r   r   r   set_auth_type;   s   
z(HTTPClientSecurityProvider.set_auth_typec                 C      | j S r   r    r   r   r   r   get_auth_type>      z(HTTPClientSecurityProvider.get_auth_typec                 C   r"   r   )r   r#   r   r   r   get_auth_typesA   r%   z)HTTPClientSecurityProvider.get_auth_typesc                 C   r"   r   )r   r#   r   r   r   get_ntlmssp_infoD   r%   z+HTTPClientSecurityProvider.get_ntlmssp_infor   Nc	           	      C   s   || _ || _|| _|dks|dkr>t|d rd| }t|d r%d| }zt|| _t|| _W n
   || _|| _Y || _|| _	|| _
d S )Nr      z0%s)r   r   r   lenbinascii	unhexlifyr   r   r   r   r   )	r   usernamepassworddomainlmhashnthashaesKeyTGTTGSr   r   r   set_credentialsG   s$   
z*HTTPClientSecurityProvider.set_credentialsc                 C   sb   g }d|v r| t d|v r| t d|v r| t d|v r&| t d|v r/| t |S )Nr   r   r	   r
   r   )append	AUTH_NTLM
AUTH_BASICAUTH_NEGOTIATEAUTH_BEARERAUTH_DIGEST)r   headerretr   r   r   parse_www_authenticate^   s   




z1HTTPClientSecurityProvider.parse_www_authenticatec                 C   sF   |dkrt |S zttj}t||dW S  ty"   t| Y S w )Nhttp)context)r   ssl
SSLContextPROTOCOL_SSLv23r   AttributeError)r   protocolhost_L6
uv_contextr   r   r   connectn   s   z"HTTPClientSecurityProvider.connectc                 C   sF   | j tkr| ||||S | j ttfv r| ||||S td| j  )Nz%s auth type not supported)r   r7   get_auth_headers_basic	AUTH_AUTOr6   get_auth_headers_auto	Exception)r   http_objmethodpathheadersr   r   r   get_auth_headersx   s
   
z+HTTPClientSecurityProvider.get_auth_headersc                 C   s   | j dks| jdks| jdks| jd ks| jd krtd| jdkr+| jd | j }n| jd | j d | j }dt	
|dd }d|id fS )	Nr   zXBasic authentication in HTTP connection used, so set a plaintext credentials to connect.:\zBasic %szUTF-8asciiAuthorization)r   r   r   r   r   rK   r   r   r   base64	b64encodeencodedecode)r   rL   rM   rN   rO   	auth_lineauth_line_httpr   r   r   rH      s   
z1HTTPClientSecurityProvider.get_auth_headers_basicc              
   C   s  |  }d|d< dt|d |d< |j|||d | }|  |jdkr2td|j|f |	d	d u r?td
| | j
g krM| |	d	| _
t| j
vrTdS ztd|	d	d}t|}	W n tttfyw   td| w | jst|	}
t|
d | _|	d fS )N0zContent-LengthNTLM %srS   rT   )rO   i  zJStatus code returned: %d. Authentication does not seem required for url %szWWW-Authenticatez4No authentication requested by the server for url %s)NNzNTLM ([a-zA-Z0-9+/]+={0,2})   z1No NTLM challenge returned from server for url %sTargetInfoFields)copyrU   rV   rX   requestgetresponsereadstatusrK   	getheaderr   r=   r6   researchgroup	b64decode
IndexErrorKeyErrorrC   r   r   NTLMAuthChallengeAV_PAIRS)r   rL   rM   rN   rO   negotiateMessageauth_headersresserverChallengeBase64serverChallenge	challenger   r   r   send_ntlm_type1   s@   



z*HTTPClientSecurityProvider.send_ntlm_type1c           
   	   C   s   | j dks| jd ks| jd krtdtj| jd}| ||||| d }|d urMt	| _
t||| j| j| j| j| j\}}dt| d }	n| j
tkrbt| jv rbt| _
| ||||S td| j d|	id fS )	Nr   zANTLM authentication in HTTP connection used, cannot use Kerberos.)r.   r   r\   rS   z$No supported auth offered by URL: %srT   )r   r   r   rK   r   getNTLMSSPType1r   rs   getDatar6   r   getNTLMSSPType3r   r   r   r   rU   rV   rX   rI   r7   r   rH   )
r   rL   rM   rN   rO   authrq   type3exportedSessionKeyrZ   r   r   r   rJ      s    z0HTTPClientSecurityProvider.get_auth_headers_auto)r   r   r   r   NN)__name__
__module____qualname__rI   r   r!   r$   r&   r'   r4   r=   rG   rP   rH   rs   rJ   r   r   r   r   r   +   s    

'r   )re   r@   rU   r*   http.clientr   r   ImportErrorhttplibimpacketr   r   rI   r7   r6   r8   r9   r:   r   r   r   r   r   <module>   s"   