"M왯tdZddlZddlZddlZddlmZddlmZmZddl m Z ddl m Z m Z ddlmZddlmZdd lmZdd lmZmZdd lmZdd lmZmZdd lmZddlmZddl m!Z"ddl#m$Z$ddl%m&Z&ej'e(Z)eGddZ*GddZ+ dde,edededzfdZ-dS)u  This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program.  If not, see . Copyright © 2019 Cloud Linux Software Inc. This software is also available under ImunifyAV commercial license, see N)suppress)asdict dataclass)Path)ListOptional)Malware)UserType)TheSink)antivirus_mode safe_fileops)CleanupRevertInitiator)MalwareHitStatusMalwareScanResourceType) MalwareHit) is_crontab)restore) MalwareAction hash_pathceZdZUeed<eed<eed<ejZeed<dZe ed<dZ e ed<dZ eed <dZ eed <dZ e ed <dZe ed <dZe ed <dZe ed<eZdS) RestoreReportfilescan_idowner initiator cleaned_at reverted_athash_before_reverthash_after_revertmtime_before_revertmtime_after_revertsize_before_revertsize_after_revertN)__name__ __module__ __qualname__str__annotations__r ROOTrrfloatrr!r"r#r$r%r&rto_dictT/opt/imunify360/venv/lib/python3.11/site-packages/imav/malwarelib/cleanup/storage.pyrr0s III LLL JJJ]Is"""JK    s!#### """" """"!u!!!GGGr0rcbeZdZUdZeejZeed<e ddedefdZ e de de fd Z e d Ze d Ze d Ze d edefdZe ddeedee fdZe dededefdZe dedefdZdS)CleanupStoragez@ Store files before cleanup and restore them by request pathFsrcdstc~Ktjt|t|dd||d{VdS)NFT) src_unlink dst_overwritesafe_srcsafe_dst)r safe_mover*)r5r6r:r;s r1_copyzCleanupStorage._copyIsf$ HH HH             r0filenamereturnc t|S)zN Get file name for cleanup storage :return: file name r)clsr>s r1 storage_namezCleanupStorage.storage_nameTs """r0cF|j||jz SN)r4rB orig_file)rAhits r1get_hit_store_pathz!CleanupStorage.get_hit_store_path\sx#**3=9999r0cK|j}||}t|}||||dd{VdSNT)r:r;)orig_file_pathrGrr=)rArFr5r6r:s r1storezCleanupStorage.store`s^ $$S))c??iiS8diCCCCCCCCCCCr0cK|js|jdttt}}}|D]} ||d{V||4#t $r||YUttj f$rz}t dt||||tj|j|j|j|jd{VYd}~d}~wwxYw|||fS)Nz-Failed to store file before cleanup: %r -- %sr4 file_owner file_user signature_id)r4existsmkdirsetrKaddFileNotFoundErrorOSErrorr UnsafeFileOperationloggerwarningr*rcleanup_failed_storerEruserrQ)rAhits succeeded not_existfailedrFes r1 store_allzCleanupStorage.store_allgsx   " HNN5 ! ! !'*uuceeSUUf9   C iinn$$$$$$$ c""""$ # # # c"""""\=>   CHH  3#8"y!h!$!1  &)++s%0BE7E A0EErFcKt|j|j|j}||}|j}t |}tt5| j |_ dddn #1swxYwYtt5| }|j |_ |j |_tj|}||_dddn #1swxYwY|||d|d{Vt+j|_tt5| }|j |_|j |_tj|} | |_dddn #1swxYwY|SrI)rrE scanid_idr\rGrJrrrVstatst_mtimerr#st_sizer%hashlibsha256 read_bytes hexdigestr!r=timerr$r&r") rArFreportr5r6r; st_before hash_beforest_after hash_afters r1rzCleanupStorage.restoresgs}cmSXFF$$S)) c?? ' ( ( 4 4 # 3F  4 4 4 4 4 4 4 4 4 4 4 4 4 4 4' ( ( 4 4 I)2);F &(1(9F %!.)9)9::DDFFK(3F %  4 4 4 4 4 4 4 4 4 4 4 4 4 4 4iiS4(iCCCCCCCCC!Y[[ ' ( ( 2 2xxzzH(0(9F %'/'7F $ (8(899CCEEJ'1F $  2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 s8"B  BB,A,D$$D(+D(:A,G22G69G6Nr]rc DKtt}}|D]} ||d{V}tj|j|j|j|j|pd|d{V||j#ttj f$rz}tj |j|j|j|jd{Vtdt||||Yd}~d}~wwxYw||fS)Nr )r4rOrPrQrrmrNz Failed to restore file: %r -- %s)rTrrcleanup_restored_originalrErr\rQrUrWr rXcleanup_failed_restorerYrZr*)rAr]rr^r`rFrmras r1 restore_allzCleanupStorage.restore_allsw EE3556   C "{{3////////#="y!h!$!1'o2!  c""""\=>   #:"y!h!$!1  A3s88QOOO 3 &  sA&B  D!A0DDkeepctK|}|j|kr|dSdS)NTF)rerfunlink)rAr4rvsts r1_clearzCleanupStorage._clears7 YY[[ ;   KKMMM4ur0cK|jddd}|jD]#}|||d{Vr|dz }$|S)zj Clear storage :param keep: keep files after specified timestamp :return: rMT)exist_okrN)r4rSiterdirrz)rArvclearedr4s r1clearzCleanupStorage.clearsy ut,,,H$$&&  DZZd++++++++ 1 r0)FFrD)r'r(r)__doc__rConfigCLEANUP_STORAGEr4r+ staticmethodr= classmethodr*rBrGrKrbrrrrrrur-boolrzintrr/r0r1r3r3Bsf,--D$---   D   \ #C#C###[#::[:DD[D ,,[,8 }[<@D!! #!08 !!![!:Ed[ u    [   r0r3r]sinkrcKd|D}t||d{V\}}tj|tjt jrtj |||d{V||fS)NcFg|]}|jtjjk|Sr/) resource_typerFILEvalue).0rFs r1 z restore_hits..s7    7 < B B B B B Br0) r3rur set_statusrFOUNDr disabled mds_restore restore_hits)r]rr file_hitsr^r`s r1rrs I -88INNNNNNNNIv)%5%;<<<>&tT9========= f r0rD).rrhloggingrl contextlibr dataclassesrrpathlibrtypingrr defence360agent.contracts.configr rr "defence360agent.internals.the_sinkr defence360agent.utilsr r imav.malwarelib.cleanup.typesrimav.malwarelib.configrrimav.malwarelib.modelrimav.malwarelib.scan.crontabrimav.malwarelib.scan.mdsrrimav.malwarelib.subsys.malwarerimav.malwarelib.utilsr getLoggerr'rYrr3listrr/r0r1rs'* ))))))))!!!!!!!!>>>>>>555555666666>>>>>>>>@@@@@@-,,,,,333333;;;;;;888888++++++  8 $ $  "TTTTTTTTt04 z  &,r0