o
    àý°jµ!  ã                   @   s¾   d dl Z d dlZd dlZzd dlmZ W n ey#   d dlmZ Y nw 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mZmZmZmZ d dlmZ d	gZG d
d	„ d	eƒZdS )é    N)ÚHTTPSConnection)Úunpack)ÚLOG)ÚProtocolClient)ÚSTATUS_SUCCESSÚSTATUS_ACCESS_DENIED)ÚNTLMAuthChallengeÚNTLMAuthNegotiateÚNTLMAuthChallengeResponseÚNTLMSSP_NEGOTIATE_SIGNÚNTLMSSP_NEGOTIATE_ALWAYS_SIGN)ÚSPNEGO_NegTokenRespÚWinRMSRelayClientc                   @   sL   e Zd ZdZddd„Zdd„ Zdd	„ Zddd„Zdd„ Zdd„ Z	dd„ Z
d
S )r   ÚWINRMSéb  Tc                 C   s@   t  | ||||¡ || _d | _d | _d | _d | _d| _d| _d S )NTz<xml></xml>)	r   Ú__init__ÚextendedSecurityÚnegotiateMessageÚauthenticateMessageBlobÚserverÚauthenticationMethodÚ
isFirstNegÚbasic_xml_data)ÚselfÚserverConfigÚtargetÚ
targetPortr   © r   ú¨/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/impacket/examples/ntlmrelayx/clients/winrmrelayclient.pyr      s   
zWinRMSRelayClient.__init__c                 C   sp   d | _ | jjdkrd| _n| jj| _zt tj¡}t| j| j|d�| _	W dS  t
y7   t| j| jƒ| _	Y dS w )NÚ z/wsman)ÚcontextT)Ú
lastresultr   ÚpathÚsslÚ
SSLContextÚPROTOCOL_SSLv23r   Ú
targetHostr   ÚsessionÚAttributeError)r   Ú
uv_contextr   r   r   ÚinitConnection    s   
þþz WinRMSRelayClient.initConnectionc              
   C   sl  t ƒ }| |¡ | jjr(|d t@ r|d  tN  < |d t@ r(|d  tN  < | ¡ | _|d t@ r8t 	d¡ t
| jƒddœ}z| jjd| j|| jd� | j ¡ }| ¡  W n! tyx } zt d| j| jt|ƒf ¡ W Y d }~dS d }~ww |jd	krˆt d
|j› d�¡ z)| d¡}d|vr¡d|vr¡t d|› �¡ W dS d|v r©d| _nd|v r°d| _W n$ ttfyÕ   t d| j› �¡ | jjrÐt d¡ d| _nY dS Y nw t |¡ d¡}| j› d|› �dt
| jƒdœ}| jjd| j|| jd� | j ¡ }| ¡  z t  !| j› d�| d¡¡ "d¡}t #|¡}	t$ƒ }
|
 |	¡ |
W S  t%tt&f�y5   t d¡ Y dS w )NÚflagsz@The client requested signing, relaying to WinRMS might not work!ú"application/soap+xml;charset=UTF-8©úContent-LengthúContent-TypeÚPOST©ÚheadersÚbodyzConnection to %s:%s failed: %sFé‘  zStatus code returned: z/. Authentication does not seem required for URLzWWW-AuthenticateÚNTLMÚ	Negotiatez1NTLM Auth not offered by URL, offered protocols: z2No authentication requested by the server for url zMIIS cert server may allow anonymous authentication, sending NTLM auth anywaysÚasciiú ©ÚAuthorizationr/   r.   z ([a-zA-Z0-9+/=]+)é   z&No NTLM challenge returned from server)'r	   Ú
fromStringr   Ú
remove_micr   r   ÚgetDatar   r   ÚwarningÚlenr   r'   Úrequestr"   ÚgetresponseÚreadÚ	ExceptionÚerrorr&   r   ÚstrÚstatusÚinfoÚ	getheaderr   ÚKeyErrorÚ	TypeErrorÚisADCSAttackÚbase64Ú	b64encodeÚdecodeÚreÚsearchÚgroupÚ	b64decoder   Ú
IndexErrorr(   )r   r   ÚnegoMessager2   ÚresÚeÚauth_headerÚ	negotiateÚserverChallengeBase64ÚserverChallengeÚ	challenger   r   r   ÚsendNegotiate-   sx   


þ
€þ

€
þüý
 


þzWinRMSRelayClient.sendNegotiateNc           
      C   s  t d|d d… ƒd tjkrt|ƒ}|d }n|}z%tƒ }| |¡ |d }t|ƒdkr=|dd… d	kr=t d
t|ƒ ¡ W n	 tyG   Y nw t	 
|¡ d¡}| j› d|› �dt| jƒdœ}| jjd| j|| jd� | j ¡ }	|	jdkryd tfS t d|	j› d�¡ |	 ¡ | _d tfS )NÚBr;   r   ÚResponseTokenÚntlmé0   é   é   s     zUNTLMv2 detected (NT response %d bytes). WinRMS relay may fail due to Channel Binding.r7   r8   r,   r9   r0   r1   r4   z HTTP server returned error code z2, this is expected, treating as a successful login)r   r   ÚSPNEGO_NEG_TOKEN_RESPr
   r<   r@   r   ÚdebugrD   rM   rN   rO   r   r   r'   rA   r"   rB   rG   r   rH   rC   r!   r   )
r   r   r[   Ú
respToken2ÚtokenÚauthMsgÚnt_responseÚauthr2   rV   r   r   r   ÚsendAutho   s4   

€ÿý


zWinRMSRelayClient.sendAuthc                 C   s"   | j d ur| j  ¡  d | _ d S d S ©N)r'   Úclose©r   r   r   r   ÚkillConnection’   s   


þz WinRMSRelayClient.killConnectionc                 C   s   d S rl   r   rn   r   r   r   ÚisAdmin—   s   zWinRMSRelayClient.isAdminc                 C   s>   d}t |ƒddœ}| jjd| j||d� | j ¡ }| ¡  d S )Na¢  
        <?xml version="1.0" encoding="utf-8"?>
        <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
                      xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
                      xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
                      xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd"
                      xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
          <env:Header>
            <a:To>http://windows-host:5986/wsman</a:To>
            <a:ReplyTo>
              <a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
            </a:ReplyTo>
            <a:MessageID>uuid:2a8ac24f-00f0-4a87-860c-bf58d33a1e0a</a:MessageID>
            <a:Action mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/09/transfer/Create</a:Action>
            <w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
            <w:OperationTimeout>PT20S</w:OperationTimeout>
            <w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
            <w:OptionSet>
              <w:Option Name="WINRS_NOPROFILE">FALSE</w:Option>
              <w:Option Name="WINRS_CODEPAGE">437</w:Option>
            </w:OptionSet>
            <w:Locale xml:lang="en-US"/>
            <p:DataLocale xml:lang="en-US"/>
          </env:Header>
          <env:Body>
            <rsp:Shell>
              <rsp:InputStreams>stdin</rsp:InputStreams>
              <rsp:OutputStreams>stdout stderr</rsp:OutputStreams>
            </rsp:Shell>
          </env:Body>
        </env:Envelope>
        r,   r-   r0   r1   )r@   r'   rA   r"   rB   rC   )r   Úheartbeat_xmlr2   rV   r   r   r   Ú	keepAliveœ   s   "þ
zWinRMSRelayClient.keepAlive)r   Trl   )Ú__name__Ú
__module__Ú__qualname__ÚPLUGIN_NAMEr   r*   r]   rk   ro   rp   rr   r   r   r   r   r      s    


B#)rP   r#   rM   Úhttp.clientr   ÚImportErrorÚhttplibÚstructr   Úimpacketr   Ú$impacket.examples.ntlmrelayx.clientsr   Úimpacket.nt_errorsr   r   Úimpacket.ntlmr   r	   r
   r   r   Úimpacket.spnegor   ÚPROTOCOL_CLIENT_CLASSESr   r   r   r   r   Ú<module>   s     ÿ