g#$dZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZdd lmZmZmZmZdd lmZmZmZeje Z!Gd dZ"e dGddZ#dede$fdZ%GddZ&Gdde&Z'Gdde&Z(Gdde(Z)de$fdZ*de$fd Z+d!e,fd"Z-d#e,d$e$fd%Z.d&efd'Z/ed*d(Z0ed*d)Z1dS)+uQ 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 Migrate scan/cleanup/restore history from revisium extension for PleskN)abstractmethod) defaultdict) dataclass)iglob)Path)uuid4)file_hash_and_size)CleanupStorage)find_revisium_dbget_domain_docrootget_revisium_dbget_vhosts_dir)batched get_ownerskip_for_im360cFeZdZdZdZdZedZedZdS)dbNcr|jd|_|jd|_|jd|_dS)N malware_scans malware_hitsmalware_history)orm MalwareScan MalwareHitMalwareHistory)clsmigrators ^/opt/imunify360/venv/lib/python3.11/site-packages/imav/migrations/009_revisium_scan_history.pyinitzdb.init6s4",7!n5%\*;<ct|dD]6}|t|7dS)Ni)n)r insert_manylistexecute)rmodeldatabatchs rr#zdb.insert_many<sQTT*** 5 5E   d5kk * * 2 2 4 4 4 4 5 5r ) __name__ __module__ __qualname__rrr classmethodrr#r rrr1sWKJN==[= 55[555r rF)eqcLeZdZUeed<eed<dZeed<dZeed<dZ dZ dS)Hitpath signatureNhashsizec*t|jSN)r3r1selfs r__hash__z Hit.__hash__IsDIr c"|j|jkSr6)r1)r8others r__eq__z Hit.__eq__LsyEJ&&r ) r)r*r+r__annotations__strr3r4intr9r<r-r rr0r0Bsf JJJNNND#D#'''''r r0 report_pathreturnc |5}tj|cdddS#1swxYwYn3#t$r&}td||Yd}~nd}~wwxYwiS)NzCan't get report %s due to %s)openjsonload Exceptionloggererror)r@fexcs r get_reportrKPsH      19Q<<                 HHH 4k3GGGGGGGGH Is2A7 A;A;A A3 A..A3cVeZdZdZdZdededefdZe dZ e dZ dS)ReportN report_dirowner timestampcJ||jz |_||_ddd|d|_dS)Nfile on-demandroot) resource_typecause initiatorctime) REPORT_FILEr1 default_ownercommon_history_info)r8rNrOrPs r__init__zReport.__init__]s:!11 "#  $ $    r c*t|jSr6)rKr1r7s rrow_datazReport.row_datags$)$$$r cdSr6r-r7s rupdate_historyzReport.update_historyks r ) r)r*r+rYTASKrr>r?r\propertyr^rr`r-r rrMrMYs}K D 4      %%X%  ^   r rMc"eZdZdZdZdefdZdS)Scanzresult_scan.serscanhitsc |jdD]\}}|dkr |D]}tt|d|d|d|d}||t |jdt|jp|jt|jp|jd |j dS) Nr'vulnersfnsnsha256sz)r1r2r3r4foundr1event file_owner file_user) r^itemsr0rappendr>r1rrZr[)r8rfsection maliciousr'hits rr`zScan.update_historyts"&-"7"="="?"?   GY)##!  d4j))"4jhd  S   #CH !(%ch//E43E%.sx%8%8%NDzsave_hits..s ' r )keyTrorrm)scaniduser orig_filetyperur3r4r)rrr1existsr storage_namer r>hashlibrkr3r4sortedrsrrr2rr#r)rerfdb_hitsrvhistoryr last_changes r save_hitsrsQG  W 8??   &+n.I//I!! %7 NNGN&&"#(x38#3 .B.BCCCBGK NN"k%ch//!$SXM!%HH'w/3EEE (1      NN2='*****r domain_idct}|dt|f}|}|d|ddS)NzDSELECT last_scan_cnt_files, error_code FROM pool WHERE domain_id = ?rr)total_resourcesrH)r execute_sqlr?fetchone)rrcursordb_datas rget_extra_scan_datarsV   B ^^N YFooG&qzGAJ ? ??r scan_path scan_resultc |d}tjtjt |d|dz |dd|d||d|dd S) Nstatsend_time exec_timerSrrH cnt_cureablerR) rstarted completedrrr1rHtotal_maliciousrU)rrcreaterhexr?)rr scan_statss rcreate_malware_scanrsxW%J > ww{Jz*Z -DDEEZ( #$56 '"#N3 !   r domain_results_pathc@|jd\}}t|}t|}t |dz }t jtjh| kr]|tjdd|t jddkr| tjt|i|dt|}tt}tt tfD]C}|j|vr8|||||jdd|Dt#|t%||dS)Nz .revisiumz task.resultrrre)rr)rNrOrP)namesplitr rrOrKryrarkeyspoprrrr$rdr`rr) r_rrrOtasksrerfreports rprocess_domain_scan_resultsrs&+11+>>LAy"9--I OO ! ! # #E *]: ; ;E 49-- di!*-  7 #J / 0 0  $) GuV}G(;I(F(FG   D t  Dt$## ;%   F. ,W5jA   nT""" dDr c z|stsdSt|tjt d}t|D]R} tt| #t$r&}t d||Yd}~Kd}~wwxYwdS)Nz&*/.revisium_antivirus_cache/.revisium*z.Can't process revisium reports in %s due to %s) r rrosr1joinrrrrrFrG exception)rdatabasefakerscan_results_dir_patternrrJs rmigraters #%%GGH!w||0   %%=>>  '-@(A(A B B B B      @#         s+B B8B33B8c dSr6r-)rrrrs rrollbackr"sDr )F)2__doc__rrDrloggingrabcr collectionsr dataclassesrglobrpathlibruuidrdefence360agent.utilsr imav.malwarelib.cleanup.storager imav.migration_utils.revisiumr r r rimav.migration_utils.otherrrr getLoggerr)rGrr0rwrKrMrdryrrrr>rrrrrr-r rrsRJJ.  ######!!!!!!444444:::::: JIIIIIIIII  8 $ $55555555" e ' ' ' ' ' ' ' 'DT        .6868"""""4""",t,,,, +$++++B@3@@@@ 3 T    T>&      r