h8dZddlZddlZddlZddlmZddlmZmZddl m Z ddl m Z m Z ddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZeje Z!eGddZ"GddZ#dde 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)UserType) MessageType)g) safe_fileops)Malware)VulnerabilityHitStatus)VulnerabilityHit 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 patched_at reverted_athash_before_reverthash_after_revertmtime_before_revertmtime_after_revertsize_before_revertsize_after_revertN)__name__ __module__ __qualname__str__annotations__r ROOTrrfloatrrrrrr r!rto_dict\/opt/imunify360/venv/lib/python3.11/site-packages/imav/malwarelib/vulnerabilities/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) PatchStoragez> Store files before patch and restore them by request pathFsrcdstc~Ktjt|t|dd||d{VdS)NFT) src_unlink dst_overwritesafe_srcsafe_dst)r safe_mover%)r0r1r5r6s r,_copyzPatchStorage._copyCsf$ HH HH             r+filenamereturnc t|S)zV Get file name for vulnerabilities storage :return: file name r)clsr9s r, storage_namezPatchStorage.storage_nameNs """r+cF|j||jz SN)r/r= orig_file)r<hits r,get_hit_store_pathzPatchStorage.get_hit_store_pathVsx#**3=9999r+cK|j}||}|||ddd{VdS)NFTr5r6)orig_file_pathrBr8)r<rAr0r1s r,storezPatchStorage.storeZsS $$S))iiS54i@@@@@@@@@@@r+c8K|js|jdttt}}}|D]} ||d{V||4#t $r||YUttj f$rH}t dt||||Yd}~d}~wwxYw|||fS)Nz+Failed to store file before patch: %r -- %s) r/existsmkdirsetrFaddFileNotFoundErrorOSErrorr UnsafeFileOperationloggerwarningr%)r<hits succeeded not_existfailedrAes r, store_allzPatchStorage.store_all`s2x   " HNN5 ! ! !'*uuceeSUUf9  C iinn$$$$$$$ c""""$ # # # c"""""\=>   AHH  3  &)++s%0BD7D >DDrAcKt|j|j|j}||}|j}t t5|j |_ dddn #1swxYwYt t5|}|j |_ |j |_ tj|}||_dddn #1swxYwY|||ddd{Vt)j|_t t5|}|j |_|j |_tj|}||_dddn #1swxYwY|S)NTFrD)rr@ scanid_iduserrBrErrMstatst_mtimerrst_sizer hashlibsha256 read_bytes hexdigestrr8timerrr!r) r<rAreportr0r1 st_before hash_beforest_after hash_afters r,restorezPatchStorage.restoreus\s}cmSXFF$$S))  ' ( ( 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%i@@@@@@@@@!Y[[ ' ( ( 2 2xxzzH(0(9F %'/'7F $ (8(899CCEEJ'1F $  2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 s8A>>BBA,DDD+A,G##G'*G'NrRrc@Ktt}}|D]} ||d{V}|rdtjdrP||_tjtj| d{V| |#ttj f$rH}tdt!||| |Yd}~d}~wwxYw||fS)Nsinkz Failed to restore file: %r -- %s)rKrhr getrrjprocess_messager VulnerabilityPatchRevertr)rLrNr rOrPrQr%)r<rRrrSrUrArcrVs r, restore_allzPatchStorage.restore_alls@ EE3556  C "{{3////////aeFmm'0F$&00#   A3s88QOOO 3 &  sBB;;D>DDkeepctK|}|j|kr|dSdS)NTF)r[r\unlink)r<r/rosts r,_clearzPatchStorage._clears7 YY[[ ;   KKMMM4ur+cK|jddd}|jD]#}|||d{Vr|dz }$|S)zj Clear storage :param keep: keep files after specified timestamp :return: rHT)exist_okrN)r/rJiterdirrs)r<roclearedr/s r,clearzPatchStorage.clearsy ut,,,H$$&&  DZZd++++++++ 1 r+)FFr?)r"r#r$__doc__rConfig PATCH_STORAGEr/r& staticmethodr8 classmethodr%r=rBrFrWrrrhrrrnr(boolrsintryr*r+r,r.r.<sf*++D$+++   D   \ #C#C###[#::[:AA[A ,,[,( 0][:FJ!!()!6>sm!!![!(Ed[ u    [   r+r.rcKt||d{V\}}tj|tj||fSr?)r.rnr set_statusrREVERTED)rRrrSrUs r, restore_hitsrsT*66tYGGGGGGGGIv +A+JKKK f r+r?)&rzr^loggingrb contextlibr dataclassesrrpathlibrtypingrr defence360agent.contracts.configr "defence360agent.contracts.messagesr &defence360agent.internals.global_scoper defence360agent.utilsr imav.contracts.configr r{imav.malwarelib.configrimav.malwarelib.modelrimav.malwarelib.utilsr getLoggerr"rPrr.r%rr*r+r,rs* ))))))))!!!!!!!!555555::::::444444......333333999999222222++++++  8 $ $  "AAAAAAAAH r+