o
    àý°jÄT  ã                   @   sä   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZ d dlZdZdZd	Zd
ZdZd	ZdZdZdZdZdZdZddddddœZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZG dd„ dƒZ dS )é    )Ú	Structure)ÚlsadÚlsat)ÚSMBTransport)ÚSMBConnection)ÚFileSecInformationÚFILE_OPEN_REPARSE_POINTÚGENERIC_ALLÚREAD_CONTROLNé   é   é   é   é   i  é    iÿ  i‰  i i   iÿ )ÚRÚWÚDÚXÚFc                   @   s   e Zd ZdZdS )Ú
FileNTUser))ÚRevisionú<H=1)ÚSizer   )ÚNumACEsz<I=1)ÚBufferú:N)Ú__name__Ú
__module__Ú__qualname__Ú	structure© r!   r!   ú/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/impacket/acl.pyr   9   s    r   c                   @   sD   e Zd ZdZdd„ Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dS )ÚACL_SID))r   ú<B)ÚNumAuthr$   )Ú	AuthorityÚ6s)ÚSubauthoritiesr   c                 C   sh   t | d ƒd }t d| d dd… ¡d }d ttdt| d	 ƒ|gtt d
 t|ƒ¡| d ¡ƒ ƒ¡S )Nr(   r   z>Hr&   é   r   ú-ÚSr   ú<{}I)	ÚlenÚstructÚunpackÚjoinÚmapÚstrÚintÚlistÚformat)ÚselfÚnÚ	authorityr!   r!   r"   Ú__repr__K   s   ÿþÿzACL_SID.__repr__c                 C   sŠ   |   d¡dd … }t|d ƒ}t|d ƒ}t|ƒd }tjd |¡gttt|dd … ƒƒ¢R Ž }t d||¡d t d|¡ | }t|ƒS )	Nr*   r   r   r   r,   z<2Bs        r$   )	Úsplitr3   r-   r.   Úpackr5   Útupler1   r#   )ÚdataÚitemsÚrevisionÚnumAuthÚ
sub_lengthÚsubauthoritiesÚraw_sidr!   r!   r"   Úbuild_from_stringW   s   *ÿ
þýÿzACL_SID.build_from_stringc                 C   ó   |   ¡ S ©N©r9   ©r6   r!   r!   r"   Ú__str__f   ó   zACL_SID.__str__c                 C   ó   |   ¡ |  ¡ kS rF   rG   ©r6   Úotherr!   r!   r"   Ú__eq__i   ó   zACL_SID.__eq__c                 C   s   |   ¡  ¡ S rF   )r9   Ú__hash__rH   r!   r!   r"   rP   l   s   zACL_SID.__hash__c                 O   s   |   ¡ j|i |¤ŽS rF   )rI   r:   )r6   ÚargsÚkwargsr!   r!   r"   r:   o   s   zACL_SID.splitN)r   r   r   r    r9   ÚstaticmethodrD   rI   rN   rP   r:   r!   r!   r!   r"   r#   C   s    
r#   c                
   @   sF   e Zd Zddddddddd	effZd
d„ Zdd„ Zdd„ Zdd„ ZdS )Ú	FileNTACE)ÚTyper$   )ÚNTACE_Flagsr$   )r   ú<H)ÚSpecificRightsrW   )ÚStandardRightsr$   )ÚGenericRightsr$   )Ú_SIDz_-SIDz(self["Size"] - 8)ÚSIDz:=""c                 C   s4   |   ¡ }|  ¡ }|dkr|| S |  ¡ }|| | S )Nú(F))Úget_readable_ntace_flagsÚget_readable_specific_rightsÚget_readable_standard_rights)r6   ÚflagsÚspecificÚstandardr!   r!   r"   rI   €   s   zFileNTACE.__str__c                 C   sx   d}| d t @ r|d7 }| d t@ r|d7 }| d t@ r |d7 }| d t@ r*|d7 }| d t@ r4|d7 }|dkr:dS |S )zö
        Return the NTACE flags in readable format
        (OI) - object inherit
        (CI) - container inherit
        (IO) - inherit only
        (NP) - don't propagate inherit
        (I) - permission inherited from parent container
        Ú rV   z(OI)z(CI)z(IO)z(NP)z(I))ÚSMB_ACE_FLAG_OIÚSMB_ACE_FLAG_CIÚSMB_ACE_FLAG_IOÚSMB_ACE_FLAG_NPÚSMB_ACE_FLAG_I©r6   ra   r!   r!   r"   r^   Œ   s   	z"FileNTACE.get_readable_ntace_flagsc                 C   sD   d}| d t @ r|d7 }| d t@ r|d7 }| d t@ r |d7 }|S )zÚ
        Return the standard rights in readable format
        NOTE: does not cover all standard rights (WRITE_DAC and SYNC)
        R - read-only access
        W - write-only access
        D - delete access
        rd   rY   z(R)z(w)z(D))ÚSEC_INFO_STANDARD_READÚSEC_INFO_STANDARD_WRITEÚSEC_INFO_STANDARD_DELETErj   r!   r!   r"   r`   ¦   s   z&FileNTACE.get_readable_standard_rightsc                 C   sL   | d t @ t kr
dS d}| d t@ tkr|d7 }| d t@ tkr$|d7 }|S )zá
        Return the specific rights in readable format
        NOTE: does not cover all specific rights (only write, execute, full control)
        W - write access
        X - execute access
        F - full control
        rX   r]   rd   z(W)z(X))ÚSEC_INFO_SPECIFIC_FULLÚSEC_INFO_SPECIFIC_WRITEÚSEC_INFO_SPECIFIC_EXECUTErj   r!   r!   r"   r_   ¸   s   
ÿz&FileNTACE.get_readable_specific_rightsN)	r   r   r   r#   r    rI   r^   r`   r_   r!   r!   r!   r"   rT   t   s    ørT   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚSecurityAttributeszA
    This class represents the security attributes of a file
    c                 C   s   || _ || _i | _i | _d S rF   )ÚownerÚgroupÚdaclsÚreadable_dacls)r6   rr   rs   r!   r!   r"   Ú__init__Ô   s   
zSecurityAttributes.__init__c              	      s(   d  ˆ jˆ jd ‡ fdd„ˆ jD ƒ¡¡S )NzOwner:	{}
Group:	{}
ACLs:
	{}z
	c                    s   g | ]	}t ˆ j| ƒ‘qS r!   )r2   ru   )Ú.0ÚsidrH   r!   r"   Ú
<listcomp>à   s    z/SecurityAttributes.__repr__.<locals>.<listcomp>)r5   rr   rs   r0   ru   rH   r!   rH   r"   r9   Ú   s
   
ÿüzSecurityAttributes.__repr__c                 C   rE   rF   rG   rH   r!   r!   r"   rI   â   rJ   zSecurityAttributes.__str__c                 C   rK   rF   rG   rL   r!   r!   r"   rN   å   rO   zSecurityAttributes.__eq__N)r   r   r   Ú__doc__rv   r9   rI   rN   r!   r!   r!   r"   rq   Ï   s    rq   c                   @   sŽ   e Zd ZdZd"dd„Zdd„ Zd	d
„ Zefdd„Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zd#dd„Zdd„ Zdd„ Zedd„ ƒZd#d d!„ZdS )$Ú
SMBFileACLz‡
    Manage Windows file ACLs over SMB. You can view, set, or remove ACLs.
    You must have the appropriate permissions to do so.
    Nrd   Fc              	   C   sž   t  d¡di| _dddœ| _|du | _|dur|| _nt||ƒ| _|	r0| j |||||||
¡ n
| j |||||¡ |  	¡ | _
|  ¡ | _d| _d| _d| _dS )a€  
        @param ip: target server's remote address (IPv4, IPv6) or FQDN
        @param remote_name: Remote NetBIOS name
        @param username: username
        @param password: password
        @param domain: domain where the account is valid for
        @param lmhash: LM hash for NTLM authentication
        @param nthash: NT hash for NTLM authentication
        @param aesKey: AES key for Kerberos authentication
        @param doKerberos: Use Kerberos authentication
        @param kdcHost: KDC hostname or IP address
        @param smb_connection: existing SMBConnection to reuse (if provided, other auth params are ignored)
        zS-1-5-18zNT AUTHORITY\SYSTEMzBUILTIN\AdministratorszDomain Users)Ú544Ú513N)r#   rD   Úsid_to_nameÚrid_to_nameÚ_owns_connectionÚ
connectionr   ÚkerberosLoginÚloginÚstart_dce_rpcÚdce_rpcÚopen_policy_handleÚpolicy_handleÚ	transportÚtidÚfid)r6   ÚipÚremote_nameÚusernameÚpasswordÚdomainÚlmhashÚnthashÚaesKeyÚ
doKerberosÚkdcHostÚsmb_connectionr!   r!   r"   rv   ï   s"   
ÿþ



zSMBFileACL.__init__c                 C   s>   | j rt | j| j ¡ | jr| j ¡  | jr| j ¡  dS dS )zh
        Disconnect from the tree id, close the file
        and disconnect from the smb server
        N)	r‡   r   Ú
hLsarCloser…   rˆ   Ú
disconnectr€   r�   ÚcloserH   r!   r!   r"   Úclose_connection  s   
ÿzSMBFileACL.close_connectionc                 C   s4   | j r| j | j| j ¡ | jr| j | j¡ dS dS )z7
        Close the tree ID and file ID handles
        N)rŠ   r�   Ú	closeFiler‰   ÚdisconnectTreerH   r!   r!   r"   Ú
close_file'  s
   ÿzSMBFileACL.close_filec                 C   s2   | j  |¡| _| j j| j||td�| _| j| jfS )z·
        Open the given file in the specified share
        @param share_name: share to connect to
        @param file_name: file to open
        @return: tuple of (tid, fid)
        )ÚdesiredAccessÚcreationOption)r�   ÚconnectTreer‰   ÚopenFiler   rŠ   )r6   Ú
share_nameÚ	file_nameÚdesired_accessr!   r!   r"   Ú	open_file0  s
   
ÿzSMBFileACL.open_filec                 C   s0   t | j ¡ | jdd�| _| j ¡  | j ¡ }|S )z]
        Start a new DCE/RPC connection over SMB
        @return: DCE/RPC connection
        Úlsarpc)r•   Úfilename)r   r�   ÚgetRemoteNamerˆ   ÚconnectÚget_dce_rpc)r6   Údcer!   r!   r"   r„   =  s   ý

zSMBFileACL.start_dce_rpcc                 C   s&   | j  tj¡ t | j tj¡d }|S )zU
        Open a new handle to MSRPC_UUID_LSAD
        @return: policy handle
        ÚPolicyHandle)r…   Úbindr   ÚMSRPC_UUID_LSADÚhLsarOpenPolicy2ÚPOLICY_LOOKUP_NAMES)r6   r‡   r!   r!   r"   r†   L  s
   ÿzSMBFileACL.open_policy_handlec                 C   sˆ   dd„ |d d D ƒ}t |ƒD ]2\}}|dv r|| }| d¡d }|| jv r3| j| | jt |¡< q|| | jvrA|| j|| < qdS )	zÏ
        Set the sid_to_name dictionary according to the given SIDs
        :param sids: SIDs to translate to their names
        :param resp: the response containing the names
        :return: None
        c                 S   s   g | ]}|d  ‘qS )ÚNamer!   )rw   Únamer!   r!   r"   ry   _  s    z.SMBFileACL.set_sid_to_name.<locals>.<listcomp>ÚTranslatedNamesÚNames)ÚNoneó    r*   éÿÿÿÿN)Ú	enumerater:   r   r~   r#   rD   )r6   ÚsidsÚrespÚnamesÚir±   Úridr!   r!   r"   Úset_sid_to_nameX  s   
€øzSMBFileACL.set_sid_to_namec              
   C   sT   zt  | j| j|¡}W n t jy! } z	|j}W Y d}~nd}~ww |  ||¡ dS )z�
        Resolve SIDs to names using LSA_LookupSids
        :param sids: list of SIDs
        :return: list of usernames
        N)r   ÚhLsarLookupSids2r…   r‡   ÚDCERPCSessionErrorÚpacketr½   )r6   r¸   r¹   Úsession_errorr!   r!   r"   Úsids_to_namesj  s   €ÿzSMBFileACL.sids_to_namesc              
   C   sh   zt  | j| j|g¡}|d d d d  ¡ dd… W S  ty3 } ztd|› dt|ƒ› �ƒ‚d}~ww )	z=
        Translate name to SID using LSA_LookupNames
        ÚTranslatedSidsÚSidsr   ÚSidr   NzFailed to resolve name 'z
' to SID: )r   ÚhLsarLookupNames3r…   r‡   ÚgetDataÚ	Exceptionr2   )r6   r±   r¹   Úer!   r!   r"   Úname_to_sidw  s   "€ÿzSMBFileACL.name_to_sidÚgrantc              	   C   s¸   d}g }|r=|  d¡D ]}z
|t| ¡  O }W q ty%   | |¡ Y qw |r7ddl}| dd |¡› �¡ |s=tdƒ‚|  	|¡}dt
|ƒ }	t ddd|	|¡| }
t|
ƒ}||_|S )	zô
        Convert given permissions and user to binary format
        @param username: username to add/remove permissions
        @param permissions: permissions in the icacls format
        @param action: 'grant', 'revoke', or 'delete'
        r   ú,Nz"Ignoring unsupported permissions: z, zNo valid permissions specifiedr   z<BBHI)r:   ÚSUPPORTED_PERMISSIONSÚupperÚKeyErrorÚappendÚloggingÚwarningr0   rÈ   rÊ   r-   r.   r;   rT   Úaction)r6   r�   ÚpermissionsrÓ   Úaccess_requiredÚinvalid_permsÚ
permissionrÑ   Ú	sid_bytesÚ
total_sizeÚpermissions_as_bytesÚacer!   r!   r"   Úpermissions_to_ace�  s*   ÿ
ÿzSMBFileACL.permissions_to_acec              	   C   sj  t |j|d |d … ƒ}t |j|d |d … ƒ}|  ||g¡ z| j| }W n ty3   |}Y nw z| j| }W n tyF   |}Y nw t||ƒ}|j|d d… }t|ƒ}|d }	t|	ƒrƒt|	ƒ}
|	|
d d… }	|
d dkrtq]t |
d ƒ}|
|j	|< t|	ƒsa|  t
|j	 ¡ ƒ¡ |j	 ¡ D ] \}}z| j| }W n ty¨   |}Y nw d	 ||¡|j|< q’|S )
zª
        Gets the security information of the given FileSecInformation object
        :param sec: FileSecInformation instance
        :return: SecurityAttributes
        ÚOffsetToOwnerÚOffsetToGroupÚOffsetToDACLNr   r   r\   rµ   z{}:{})r#   ÚrawDatarÂ   r~   rÏ   rq   r   r-   rT   rt   r4   Úkeysr>   r5   ru   )r6   Úsecrr   rs   Ú
owner_nameÚ
group_nameÚsecurity_attributesÚntÚntuserÚntaceÚfacerx   rÔ   r±   r!   r!   r"   Úget_security_attributes¦  sB   ÿÿ

ûÿz"SMBFileACL.get_security_attributesc                 C   sV   | j ||d� z| jjj| j| jdddd�}t|ƒ}|  |¡}|W |  ¡  S |  ¡  w )zí
        Connect to the given share and get the file permissions
        @param share_name: the share name where the file is to be opened
        @param file_name: file to get permissions from
        @return: SecurityAttributes
        )r¡   r¢   r   é   é   ©ÚfileInfoClassÚinfoTypeÚadditionalInformation)	r¤   r�   Ú_SMBConnectionÚ	queryInfor‰   rŠ   r   rê   rœ   )r6   r¡   r¢   Úresultrâ   rå   r!   r!   r"   Úget_permissionsÖ  s   û
zSMBFileACL.get_permissionsc                 C   s  t | j| d d… ƒ}|d }d}d}d}|j}t|ƒr¬d}t|ƒ}	t|	d ƒ}
|
j|d kr˜d}|dkrT|	d	  |d	 O  < |	d
  |d
 O  < |	d  |d O  < nD|dkr�|	d	  |d	  M  < |	d
  |d
  M  < |	d  |d  M  < |	d	 dkr�|	d
 dkr�|	d dkr�d}d}n|dkr˜d}d}|s ||	 ¡ 7 }||	d d… }t|ƒs|rÅ||d< t|ƒd |d< |rÄ|d  d8  < n&|dkrë|dkrë|d  t| ¡ ƒ7  < |d  d7  < | ¡ |d  |d< | j| d | d … }| j| d | d … }|  ¡ | | | ¡  }|S )aÉ  
        This function will get the current security descriptor, and then
        insert the given permission to it. Supports different actions:
        - grant: Add permissions to existing ACE (OR operation)
        - revoke: Remove specific permissions from existing ACE (AND NOT operation)
        - delete: Remove the entire ACE
        @param sec: current security descriptor
        @param permission: new permission ACE with action attribute
        rß   Nr   rµ   Fr\   TrË   rX   rY   rZ   Úrevoker   Údeleter   r   r   r   rÝ   rÞ   )r   rà   rÓ   r-   rT   r#   rÇ   )râ   r×   rç   rè   Ú
new_bufferÚ	sid_foundÚace_deletedrÓ   Ú
delete_aceré   rx   rr   rs   Úsec_info_blobr!   r!   r"   Úinsert_permissionò  sb   €ß#€
ÿzSMBFileACL.insert_permissionc           
      C   sŽ   |   ||t¡\| _| _z6|  |||¡}| jjj| j| jdddd�}t|ƒ}| j	||d�}	| jjj
| j| jddd|	d�}|W |  ¡  S |  ¡  w )aä  
        Add, remove, or modify permissions for a given user to the given file
        @param share_name: the share name where the file is to be opened
        @param file_name: file to set permissions to
        @param user: user to edit permissions for; can be a SID as well
        @param permissions: permissions in the icacls format (example: R,W,X,D).
        NOTE: not all permission types are supported; currently supporting:
        R - read-only access
        W - write-only access
        D - delete access
        X - execute access
        F - full control
        @param action: action to perform - 'grant' (add), 'revoke' (remove), 'delete' (remove ACE)
        @return: bool. whether the operation succeeded or not
        r   rë   rì   rí   )râ   r×   r   )rî   rï   rð   Ú	inputBlob)r¤   r	   r‰   rŠ   rÜ   r�   rñ   rò   r   rü   ÚsetInforœ   )
r6   r¡   r¢   ÚuserrÔ   rÓ   r×   ró   râ   Úsecurity_descriptorr!   r!   r"   Úset_permissions?  s,   ûú	zSMBFileACL.set_permissions)NNrd   rd   rd   rd   rd   NFNN)rË   )r   r   r   rz   rv   r™   rœ   r
   r¤   r„   r†   r½   rÂ   rÊ   rÜ   rê   rô   rS   rü   r  r!   r!   r!   r"   r{   é   s"    
(	

%0
Lr{   )!Úimpacket.structurer   Úimpacket.dcerpc.v5r   r   Úimpacket.dcerpc.v5.transportr   Úimpacket.smbconnectionr   Úimpacket.smb3structsr   r   r	   r
   r.   re   rf   rg   rh   ri   rl   rk   rm   ro   rp   rn   ÚSEC_READ_RIGHTrÍ   r   r#   rT   rq   r{   r!   r!   r!   r"   Ú<module>   s:   û

1[