o
    àý°jï9  ã                   @  s˜  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 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 dlZd dl%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! d dl*m$Z$ d dl"m+Z+m#Z# dZ,dZ-e,e-fZ.eG dd„ dƒƒZ/d%dd„Z0d&d d!„Z1d"d#„ Z2e3d$krÊe2ƒ  dS dS )'é    )Údivision)Úprint_function)ÚannotationsN)ÚTuple)Ú
namedtuple)Ú	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)r   ÚFILE_READ_DATAÚFILE_WRITE_DATAr   r   Ú	POSIXtoFTÚ	FTtoPOSIXr   )r   )ÚSMB2_DIALECT_311r   ÚstatÚtouchc                   @  sF   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d„ ZdS )	Ú	FileTimesNÚintÚcreation_timeÚlast_access_timeÚlast_write_timeÚchange_timec                 C  s–   dj | jd u r	dn
tj t| jƒ¡ ¡ | jd u rdn
tj t| jƒ¡ ¡ | jd u r+dn
tj t| jƒ¡ ¡ | jd u r>ddS tj t| jƒ¡ ¡ dS )Nz}
CreationTime: {creation_time}
LastAccessTime: {last_access_time}
LastWriteTime: {last_write_time}
ChangeTime: {change_time}
zN/A©r"   r#   r$   r%   )	Úformatr"   ÚdatetimeÚfromtimestampr   Ú	isoformatr#   r$   r%   )Úself© r,   úˆ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/../../../bin/filetime.pyÚpretty_reprJ   s   """÷	÷zFileTimes.pretty_repr)	Ú__name__Ú
__module__Ú__qualname__r"   Ú__annotations__r#   r$   r%   r.   r,   r,   r,   r-   r    C   s   
 r    Ú
connectionúsmbconnection.SMBConnectionÚtidr!   ÚfidÚreturnc                 C  sf   |   ¡ tkrt|  ||t¡ƒ}n	t|  ||t¡ƒ}t|d |d |d |d d}t 	d|›¡ |S )NÚCreationTimeÚLastAccessTimeÚLastWriteTimeÚ
ChangeTimer&   z!Got file / directory filetimes = )
Ú
getDialectr   r   Ú	queryInfor   r   r   r    ÚloggingÚdebug)r3   r5   r6   Ú	basicinfoÚ	filetimesr,   r,   r-   Úfiletime_queryW   s   ürB   rA   ÚNonec                 C  s¶   |   ¡ tkrtƒ }d|d< t}n	tƒ }d|d< t}|jd ur!|jnd|d< |jd ur-|jnd|d< |jd ur9|jnd|d< |j	d urE|j	nd|d< t
 d|› ¡ |  ||||¡ d S )	Nr   ÚExtFileAttributesÚFileAttributesr8   r9   r:   r;   z%Setting file / directory filetimes = )r<   r   r   r   r   r   r"   r#   r$   r%   r>   r?   ÚsetInfo)r3   r5   r6   rA   Ú	info_dataÚfileInfoClassr,   r,   r-   Úfiletime_setg   s   rI   c               
   C  sx  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d1d2dd3d |jd4d5dd6d |jd7d8dd9d |jd:d;dd<d |jd=d>d d?d@dAdB |jd'dCd dDdEdF |jdGdHddId ttjƒdJkrê|  ¡  t dJ¡ |  ¡ }|jdu rt ¡  tj¡ t t ¡ ¡ nt ¡  tj¡ |jtvrt dKj |jdL¡ t!|j"ƒ\}}}}	|j#d u r.|	|_#|d u r5dM}|dMkr[|dMkr[|j$d u r[|j%dNu r[|j&d u r[dOdPl'm'}
 |
dQƒ}|j&d urdd|_(|j$d urs|j$ )dR¡\}}ndM}dM}|j*}t+ ,|j-¡}|jtkrøt.|j/|j0fƒsšt.|j/ |j0 fƒr¤t dS¡ t dJ¡ |j/rÆt|j/ƒd@kr»t dT¡ t dJ¡ n=|j/dO }|j/dJ }n2|j0røz	t1j1 2|j0¡}W n$ t3y÷ } zt dU¡ t t|ƒ¡ t dJ¡ W Y d }~nd }~ww z–t4j5|	|j#t|j6ƒt|j7ƒdV}|j(du r| 8||||||j&|j9¡ n	| :|||||¡ |jtkr±|j/rpz2| ;|¡}|j<||t=t>B t?B dOt@dW}t dX|› dY|› dZ¡ tA|||ƒ}W | B||¡ | C|¡ n1| B||¡ | C|¡ w |j0r•t d[|j0› dZ¡ tDtE| 0¡ ƒtE| 0¡ ƒtE| 0¡ ƒtE| 0¡ ƒƒ}|jFsœd |_G|jHs£d |_I|jJsªd |_K|jLs±d |_MzÎ| ;|¡}|jtkrÀt@}n|jtkrÐtN}|jOrÐ|t@O }|j<||t=t>B t?B dO|dW}|jtkrût Pd\|› dY|› dZ¡ ttA|||ƒ Q¡ ƒ n_|jtkrGt Pd]|› dY|› dZ¡ t| Q¡ ƒ tR||||ƒ |jOrmt Pd^|› dY|› dZ¡ ttA|||ƒ Q¡ ƒ W | B||¡ | C|¡ | S¡  W d S W | B||¡ | C|¡ | S¡  W d S W | B||¡ | C|¡ | S¡  W d S W | B||¡ | C|¡ | S¡  W d S | B||¡ | C|¡ | S¡  w  t3y» } zt ¡ jTtjkr©dOd lU}| V¡  t t|ƒ¡ W Y d }~d S d }~ww )_NTzJFile / Directory Timestamp Querying & Modification Utility implementation.)Úadd_helpÚdescriptionÚtargetÚstorez6[[domain/]username[:password]@]<targetName or address>)ÚactionÚhelpÚsharezKThe share in which the desired file / directory to query or modify resides.)ÚtyperO   ÚpathzUThe path of the desired file / directory whose timestamps we wish to query or modify.z-debugÚ
store_truezTurn DEBUG output ONÚauthenticationz-hasheszLMHASH:NTHASHz$NTLM hashes, format is LMHASH:NTHASH)rN   ÚmetavarrO   z-no-passz&Don't ask for password (useful for -k)z-kzÁUse 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)r3   z-pz--portzdestination portz.Destination port to connect to the SMB Server.)ÚdefaultrQ   rU   rO   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-ipz£IP 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).rN   )Údestz)Show current file / directory timestamps.)rK   z#Modify file / directory timestamps.z-cz--createz2Change the "CreationTime" of the file / directory.z-az--accessz4Change the "LastAccessTime" of the file / directory.z-wz--writez3Change the "LastWriteTime" of the file / directory.z-mz--modifyz0Change the "ChangeTime" of the file / directory.z-rz--reference)z<share>z<path>é   z^Specify a file / directory to reference and copy the timestamps of (format is <share> <path>).)rV   rU   ÚnargsrO   z--timestampÚSTAMPz[Specify a timestamp to set for the selected filetimes (format: YYYY-MM-DD_HH:MM:SS.mmmmmm).)rV   rU   rO   z-vz
--validatez4Query the file after touching to verify the changes.é   zInvalid action '{action}')rN   Ú Fr   )Úgetpassz	Password:ú:zIError! Must select one touch method! Either by reference or by timestamp!zAError! Reference must be in the following format: <share> <path>!z:Error parsing timestamp, make sure it is valid ISO format!)Ú	sess_portÚtimeout)Ú	shareModeÚcreationOptionÚdesiredAccessz#Querying Reference FileTimes from 'z' on share 'z'!zGot TimeStamp: 'zQueried FileTimes for 'zChanging FileTimes for 'z"Validating Updated FileTimes for ')Wr   ÚinitÚprintr
   ÚBANNERÚargparseÚArgumentParserÚadd_argumentÚstrÚadd_argument_groupr   r!   Úadd_subparsersÚ
add_parserÚFILETIME_READ_ACTIONÚFILETIME_WRITE_ACTIONÚlenÚsysÚargvÚ
print_helpÚexitÚ
parse_argsr?   r>   Ú	getLoggerÚsetLevelÚDEBUGÚgetInstallationPathÚINFOrN   ÚVALID_FILETIME_ACTIONSÚerrorr'   r	   rL   Ú	target_ipÚhashesÚno_passÚaesKeyr_   ÚkÚsplitrP   ÚntpathÚnormpathrR   ÚallÚ	referenceÚ	timestampr(   ÚfromisoformatÚ	Exceptionr   ÚSMBConnectionÚportrb   ÚkerberosLoginÚdc_ipÚloginÚconnectTreeÚopenFiler   r   r   r   rB   Ú	closeFileÚdisconnectTreer    r   Úcreater"   Úaccessr#   Úwriter$   Úmodifyr%   r   ÚvalidateÚinfor.   rI   ÚcloseÚlevelÚ	tracebackÚ	print_exc)ÚparserÚgroupÚsubcommandsÚquery_parserÚtouch_parserÚoptionsÚdomainÚusernameÚpasswordÚaddressr_   ÚlmhashÚnthashrP   rR   Ú	ref_shareÚref_pathÚtouch_timestampÚexcr3   Úref_tidÚref_fidÚnew_filetimesr5   re   r6   Úer   r,   r,   r-   Úmainz   sH  



ÿ
ÿ

8(




€ý 

ûÿ



ü

û
ö
õ	
ú
þ

€ür³   Ú__main__)r3   r4   r5   r!   r6   r!   r7   r    )
r3   r4   r5   r!   r6   r!   rA   r    r7   rC   )4Ú
__future__r   r   r   rs   ri   r>   r…   Útypingr   Úcollectionsr   Ú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   r   r   Úimpacket.smb3r   rp   rq   r}   r    rB   rI   r³   r/   r,   r,   r,   r-   Ú<module>   sF   0(

 =
ÿ