o
    jeF                     @  sl  d dl mZ d dl mZ d dl mZ 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 dlmZ d d	lmZ d d
lmZ d dlmZmZmZmZmZmZmZmZmZmZ d dlmZm Z  dZ!dZ"e!e"f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Z0dZ1dZ2dZ3dZ4d Z5d!Z6e	G d"d# d#Z7d3d*d+Z8d4d.d/Z9d0d1 Z:e;d2kre:  dS dS )5    )division)print_function)annotationsN)	dataclass)logger)parse_target)version)smbconnection)SMB_SESSION_PORT)
SMB_DIALECTFILE_READ_ATTRIBUTESFILE_WRITE_ATTRIBUTESFILE_SHARE_READFILE_SHARE_WRITEFILE_SHARE_DELETESMB_QUERY_FILE_BASIC_INFOSMB_SET_FILE_BASIC_INFOSMBQueryFileBasicInfoSMBSetFileBasicInfo)SMB2_FILE_BASIC_INFOFILE_BASIC_INFORMATIONqueryset                         i   i   i   i   i    i @  i   i   i   i   i  @ c                   @  s"  e Zd ZU dZded< dZded< dZded< dZded< dZded< dZ	ded< dZ
ded	< dZded
< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< d#ddZed$ddZed%ddZd&d d!Zd"S )'FileAttributesFboolreadonlyhiddensystemvolume	directoryarchivenormal	temporarysparse_filereparse_point
compressedofflinenot_content_indexed	encryptedno_scrub_datarecall_on_openpinnedunpinnedrecall_on_data_accessreturnintc                 C  s  | j rtnd| jrtndB | jrtndB | jrtndB | jr t	ndB | j
r'tndB | jr.tndB | jr5tndB | jr<tndB | jrCtndB | jrJtndB | jrQtndB | jrXtndB | jr_tndB | jrftndB | jrmtndB | j rtt!ndB | j"r{t#ndB | j$rt%B S dB S )Nr   )&r#   FILE_ATTRIBUTE_READONLYr$   FILE_ATTRIBUTE_HIDDENr%   FILE_ATTRIBUTE_SYSTEMr&   FILE_ATTRIBUTE_VOLUMEr'   FILE_ATTRIBUTE_DIRECTORYr(   FILE_ATTRIBUTE_ARCHIVEr)   FILE_ATTRIBUTE_NORMALr*   FILE_ATTRIBUTE_TEMPORARYr+   FILE_ATTRIBUTE_SPARSE_FILEr,   FILE_ATTRIBUTE_REPARSE_POINTr-   FILE_ATTRIBUTE_COMPRESSEDr.   FILE_ATTRIBUTE_OFFLINEr/   "FILE_ATTRIBUTE_NOT_CONTENT_INDEXEDr0   FILE_ATTRIBUTE_ENCRYPTEDr1   FILE_ATTRIBUTE_NO_SCRUB_DATAr2   FILE_ATTRIBUTE_RECALL_ON_OPENr3   FILE_ATTRIBUTE_PINNEDr4   FILE_ATTRIBUTE_UNPINNEDr5   $FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESSself rM   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/../../../bin/attrib.pypackc   sR   	
zFileAttributes.packdatac                 C  s  | di dt |t@ dt |t@ dt |t@ dt |t@ dt |t@ dt |t@ dt |t@ dt |t@ d	t |t	@ d
t |t
@ dt |t@ dt |t@ dt |t@ dt |t@ dt |t@ dt |t@ dt |t@ dt |t@ dt |t@ S )Nr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   rM   )r"   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   )clsrP   rM   rM   rN   unpacky   sN   	
zFileAttributes.unpackstrc                 C  s   t |  S )N)r!   rR   __repr__)rP   rM   rM   rN   repr_attribs   s   zFileAttributes.repr_attribsc                 C  s   | j rdnd| jrdnd | jrdnd | jrdnd | jr dnd | jr'dnd | jr.dnd | jr5d	nd | jr<d
nd | j	rCdnd | j
rJdnd | jrQdnd | jrYd S d S )NR-HSVDANTCOEPU)r#   r$   r%   r&   r'   r(   r)   r*   r-   r.   r0   r3   r4   rK   rM   rM   rN   rT      s:   	
zFileAttributes.__repr__N)r6   r7   )rP   r7   r6   r!   )rP   r7   r6   rS   )r6   rS   )__name__
__module____qualname__r#   __annotations__r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   rO   classmethodrR   staticmethodrU   rT   rM   rM   rM   rN   r!   M   s4   
 
r!   
connectionsmbconnection.SMBConnectiontidr7   fidr6   c                 C  s\   |   tkrt| ||t}|d }nt| ||t}|d }td| t	
|S )NExtFileAttributesr!   z"Got file / directory attributes = )
getDialectr   r   	queryInfor   r   r   loggingdebugr!   rR   )rj   rl   rm   	basicinfo
attributesrM   rM   rN   attrib_query   s   

ru   attribsNonec                 C  s   |   tkr"t }d|d< d|d< d|d< d|d< | |d< t}nt }d|d< d|d< d|d< d|d< | |d< t}td|   | 	|||| d S )	Nr   CreationTimeLastAccessTimeLastWriteTime
ChangeTimern   r!   z&Setting file / directory attributes = )
ro   r   r   rO   r   r   r   rq   rr   setInfo)rj   rl   rm   rv   	info_datafileInfoClassrM   rM   rN   
attrib_set   s"   r   c                  C  sf  t   ttj tjddd} | jdddd | jdtd	d
 | jdtdd
 | jdddd | 	d}|jddddd |jdddd |jdddd |jddddd | 	d}|jddt
tdd d! |jd"dd#d$d |jd%dd#d&d |jd'd(d)td*d+d! | jd,d-}|jtd.d/}|jtd0d/}|jd1d2d3dd4d5 |jd6d7d8dd9d5 |jd:d;d<dd=d5 |jd>d?d@ddAd5 |jdBdCdDddEd5 |jd'dFdGddHd5 |jdIdJdKddLd5 |jdMdNdOddPd5 |jdQdRdSddTd5 |jddUdVddWd5 |jdXdYdZdd[d5 ttjd\kr|   td\ |  }|jdu r1t tj tt  nt tj |jtvrItd]j |jd^ t!|j"\}}}}	|j#d u r[|	|_#|d u rbd_}|d_kr|d_kr|j$d u r|j%d`u r|j&d u rdadbl'm'}
 |
dc}|j&d urd|_(|j$d ur|j$)dd\}}nd_}d_}|j*}t+,|j-}zt.j/|	|j#t|j0t|j1de}|j(du r|2||||||j&|j3 n	|4||||| |5|}|jdfkrt6}n|jdgkrt7}|j8||t9t:B t;B da|dh}zw|jdfkrtt<||||| nS|jdgkrQt=|j>|j?|j@|jA|jB|jC|jD|jE|jF|jG|jHdi}tI|||| t||| W |J|| |K| |L  W d S W |J|| |K| |L  W d S W |J|| |K| |L  W d S |J|| |K| |L  w  tMy } zt jNtjkrdad lO}|P  tt| W Y d }~d S d }~ww )jNTz3File Attribute Modification Utility implementation.)add_helpdescriptiontargetstorez6[[domain/]username[:password]@]<targetName or address>)actionhelpsharez?The share in which the desired file to query or modify resides.)typer   pathzIThe path of the desired file whose attributes we wish to query or modify.z-debug
store_truezTurn DEBUG output ONauthenticationz-hasheszLMHASH:NTHASHz$NTLM hashes, format is LMHASH:NTHASH)r   metavarr   z-no-passz&Don't ask for password (useful for -k)z-kzUse Kerberos authentication. Grabs credentials from ccache file (KRB5CCNAME) based on target parameters. If valid credentials cannot be found, it will use the ones specified in the command linez-aesKeyzhex keyz<AES key to use for Kerberos Authentication (128 or 256 bits)rj   z-pz--portzdestination portz.Destination port to connect to the SMB Server.)defaultr   r   r   z-dc-ipz
ip addressztIP Address of the domain controller. If omitted it will use the domain part (FQDN) specified in the target parameterz
-target-ipzIP Address of the target machine. If omitted it will use whatever was specified as target. This is useful when target is the NetBIOS name and you cannot resolve itz-tz	--timeout<   secondsz!Set connection timeout (seconds).r   )destz*Query current file / directory attributes.)r   z#Modify file / directory attributes.z-rz
--readonlyr#   zA file or directory that is read-only. For a file, applications can read the file but cannot write to it or delete it. For a directory, applications cannot delete it, but applications can create and delete files from that directory.)r   r   r   z-Hz--hiddenr$   zA file or directory that is hidden. Files and directories marked with this attribute do not appear in an ordinary directory listing.z-sz--systemr%   zQA file or directory that the operating system uses a part of or uses exclusively.z-az	--archiver(   zvA file or directory that requires to be archived. Applications use this attribute to mark files for backup or removal.z-nz--normalr)   zA file that does not have other attributes set. This flag is used to clear all other flags by specifying it with no other flags set.z--temporaryr*   zA file that is being used for temporary storage. The operating system can choose to store this file's data in memory rather than on mass storage, writing the data to mass storage only if data remains in the file when the file is closed.z-cz--compressedr-   zA file or directory that is compressed. For a file, all of the data in the file is compressed. For a directory, compression is the default for newly created files and subdirectories.z-oz	--offliner.   zThe data in this file is not available immediately. This attribute indicates that the file data is physically moved to offline storage. This attribute is used by Remote Storage, which is hierarchical storage management software.z-ez--encryptedr0   zA file or directory that is encrypted. For a file, all data streams in the file are encrypted. For a directory, encryption is the default for newly created files and subdirectories.z--pinnedr3   zThis attribute indicates user intent that the file or directory should be kept fully present locally even when not being actively accessed. This attribute is for use with hierarchical storage management software.z-uz
--unpinnedr4   zThis attribute indicates that the file or directory should not be kept fully present locally except when being actively accessed. This attribute is for use with hierarchical storage management software.r   zInvalid action '{action}')r    Fr   )getpassz	Password::)	sess_porttimeoutr   r   )	shareModecreationOptiondesiredAccess)r#   r$   r%   r(   r)   r*   r-   r.   r0   r3   r4   )Qr   initprintr   BANNERargparseArgumentParseradd_argumentrS   add_argument_groupr
   r7   add_subparsers
add_parserATTRIB_QUERY_ACTIONATTRIB_SET_ACTIONlensysargv
print_helpexit
parse_argsrr   rq   	getLoggersetLevelDEBUGgetInstallationPathINFOr   VALID_ATTRIB_ACTIONSerrorformatr   r   	target_iphashesno_passaesKeyr   ksplitr   ntpathnormpathr   r	   SMBConnectionportr   kerberosLogindc_iploginconnectTreer   r   openFiler   r   r   ru   r!   r#   r$   r%   r(   r)   r*   r-   r.   r0   r3   r4   r   	closeFiledisconnectTreeclose	Exceptionlevel	traceback	print_exc)parsergroupsubcommandsquery_parser
set_parseroptionsdomainusernamepasswordaddressr   lmhashnthashr   r   rj   rl   r   rm   rv   er   rM   rM   rN   main   s   






8 






r   __main__)rj   rk   rl   r7   rm   r7   r6   r!   )
rj   rk   rl   r7   rm   r7   rv   r!   r6   rw   )<
__future__r   r   r   r   r   rq   r   dataclassesr   impacket.examplesr   impacket.examples.utilsr   impacketr   r	   impacket.nmbr
   impacket.smbr   r   r   r   r   r   r   r   r   r   impacket.smb3structsr   r   r   r   r   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   r!   ru   r   r   rd   rM   rM   rM   rN   <module>   s\   0
X
 
