hfTdZddlZddlZddlZddlmZddlmZmZddl m Z ddl m Z m Z ddlmZmZddlmZdd lmZmZdd lmZmZdd lmZdd lmZdd lmZ ddl!m"Z"ddl#m$Z$ej%e&Z'eGddZ(GddZ)ddede e*fdZ+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)MalwareUserType)TheSink)antivirus_mode safe_fileops)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.pyrr.s III LLL JJJ]Is"""JK    s!#### """" """"!u!!!GGGr/rcbeZdZUdZeejZeed<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))r4r5r9r:s r0_copyzCleanupStorage._copyGsf$ HH HH             r/filenamereturnc t|S)zN Get file name for cleanup storage :return: file name r)clsr=s r0 storage_namezCleanupStorage.storage_nameRs """r/cF|j||jz SN)r3rA orig_file)r@hits r0get_hit_store_pathz!CleanupStorage.get_hit_store_pathZsx#**3=9999r/cK|j}||}t|}||||dd{VdSNT)r9r:)orig_file_pathrFrr<)r@rEr4r5r9s r0storezCleanupStorage.store^s^ $$S))c??iiS8diCCCCCCCCCCCr/cK|js|jdttt}}}|D]} ||d{V||4#t $r||YUttj f$rt}t dt||||tj|j|j|jd{VYd}~d}~wwxYw|||fS)Nz-Failed to store file before cleanup: %r -- %sr3 file_owner file_user)r3existsmkdirsetrJaddFileNotFoundErrorOSErrorr UnsafeFileOperationloggerwarningr)rcleanup_failed_storerDruser)r@hits succeeded not_existfailedrEes r0 store_allzCleanupStorage.store_allesx   " HNN5 ! ! !'*uuceeSUUf9   C iinn$$$$$$$ c""""$ # # # c"""""\=>   CHH  3#8"y!h &)++s%0BE7E A*D;;ErEcKt|j|j|j}||}|j}t |}tt5| j |_ dddn #1swxYwYtt5| }|j |_ |j |_tj|}||_dddn #1swxYwY|||d|d{Vt+j|_tt5| }|j |_|j |_tj|} | |_dddn #1swxYwY|SrH)rrD scanid_idrZrFrIrrrTstatst_mtimerr"st_sizer$hashlibsha256 read_bytes hexdigestr r<timerr#r%r!) r@rEreportr4r5r: st_before hash_beforest_after hash_afters r0rzCleanupStorage.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(Ktt}}|D]} ||d{V}tj|j|j|j||d{V||b#ttj f$rt}tj |j|j|jd{Vt dt||||Yd}~d}~wwxYw||fS)N)r3rNrOrrkrMz Failed to restore file: %r -- %s)rRrrcleanup_restored_originalrDrrZrSrUr rVcleanup_failed_restorerWrXr))r@r[rr\r^rErkr_s r0 restore_allzCleanupStorage.restore_allsg EE3556   C "{{3////////#="y!h'!  c""""\=>   #:"y!h A3s88QOOO 3 &  sABD A*DD keepctK|}|j|kr|dSdS)NTF)rcrdunlink)r@r3rtsts r0_clearzCleanupStorage._clears7 YY[[ ;   KKMMM4ur/cK|jddd}|jD]#}|||d{Vr|dz }$|S)zj Clear storage :param keep: keep files after specified timestamp :return: rLT)exist_okrN)r3rQiterdirrx)r@rtclearedr3s r0clearzCleanupStorage.clearsy ut,,,H$$&&  DZZd++++++++ 1 r/)FFrC)r&r'r(__doc__rConfigCLEANUP_STORAGEr3r* staticmethodr< classmethodr)rArFrJr`rrrrrrsr,boolrxintr~r.r/r0r2r2@sf,--D$---   D   \ #C#C###[#::[:DD[D ,,[,6 }[<@D!! #!08 !!![!6Ed[ u    [   r/r2sinkrcKd|D}t||d{V\}}tj|tjt jrtj ||d{V||fS)NcFg|]}|jtjjk|Sr.) resource_typerFILEvalue).0rEs r0 z restore_hits..s7    7 < B B B B B Br/) r2rsr set_statusrFOUNDr disabled mds_restore restore_hits)r[rr file_hitsr\r^s r0rrsI -88INNNNNNNNIv)%5%;<<<3&tT222222222 f r/rC),rrfloggingrj contextlibr dataclassesrrpathlibrtypingrr defence360agent.contracts.configr rr "defence360agent.internals.the_sinkr defence360agent.utilsr r imav.malwarelib.configrrimav.malwarelib.modelrimav.malwarelib.scan.crontabrimav.malwarelib.scan.mdsrrimav.malwarelib.subsys.malwarerimav.malwarelib.utilsr getLoggerr&rWrr2r)rr.r/r0rs* ))))))))!!!!!!!!HHHHHHHH666666>>>>>>>>-,,,,,333333;;;;;;888888++++++  8 $ $  "QQQQQQQQh  7 x}      r/