T1)qdZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZmZmZddlZddlmZmZmZddlmZdd lmZdd lmZdd lmZdd lmZdd l m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'ddl(m)Z)m*Z*ddl+m,Z,m-Z-ddl.m/Z/m0Z0m1Z1ddl2m3Z3m4Z4ddl5m6Z6m7Z7ddl8m9Z9dZ:ej;e<Z=Gdde>Z?Gdde/Z@Gdde/ZAGdde/ZBe GddZCGdd e0eZDGd!d"eDe1ZEGd#d$eDeZFGd%d&eFZGGd'd(eFZHdS))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)ABC) dataclass)Path)ListOptionalType) MDSReportMessage ScanFailed)g)TheSink) hosting_panel)rmtree) to_thread)MalwareCleanCompleteMalwareDatabaseCleanupMalwareDatabaseCleanupFailedMalwareDatabaseRestoreMalwareDatabaseRestoreFailedMalwareDatabaseScanMalwareRestoreComplete)ExitDetachedScanTypeMalwareScanResourceType)ScanAlreadyCompleteError ScanInfoError) DetachedDirDetachedOperation DetachedScan)MDSMDS_PATH)MalwareDatabaseHitInfo scan_report)trim_file_contentceZdZdS)DetachedOperationFailedN)__name__ __module__ __qualname__V/opt/imunify360/venv/lib/python3.11/site-packages/imav/malwarelib/scan/mds/detached.pyr&r&OsDr+r&cPeZdZUdZeed<eed<eed<iejddddZdS) MDSDetachedScanDirz/var/imunify360/dbscan/run/scan ignore_file report_filescan_info_fileignorezscan_info.json)r/r0r1Nr'r(r) DETACHED_DIRr__annotations__rFILESr*r+r,r.r.Ssc4L    $*    EEEr+r.c8eZdZUdZeed<iejddiZdS)MDSDetachedCleanupDirz /var/imunify360/dbscan/run/cleanr0Nr3r*r+r,r8r8bsB5L    }  EEEr+r8c8eZdZUdZeed<iejddiZdS)MDSDetachedRestoreDirz"/var/imunify360/dbscan/run/restorer0Nr3r*r+r,r:r:msB7L    }  EEEr+r:c\eZdZUeeed<dZedzed<dZedzed<dZe ed<dS) DbScanInfocmdN scan_type initial_pathrstarted) r'r(r)rstrr5r>r?r@intr*r+r,r<r<xsW cNNN IsTz   #L#*###GSr+r<c&eZdZddeffd ZxZS)MDSDetachedOperationrreturncHt|S)N) start_time)superget_detached_process_state)selfrG __class__s r,rIz/MDSDetachedOperation.get_detached_process_statesww11Z1HHHr+)r)r'r(r)rArI __classcell__)rKs@r,rDrDsUII#IIIIIIIIIIr+rDcVeZdZeZedZdZdZedZ edZ edZ de e fdZd Zedefd Zdefd Zejfd Zejd ddddddddedede ede ede ede eedededdfdZdS)MDSDetachedScanctjSN)rDBrJs r, RESOURCE_TYPEzMDSDetachedScan.RESOURCE_TYPEs &))r+c>|jjSrP) detached_dir done_fileexistsrRs r,_is_scan_finishedz!MDSDetachedScan._is_scan_finisheds *11333r+c |jj5}tj|cdddS#1swxYwYdS#t tjf$ricYSwxYwrP)rU progress_fileopenjsonloadFileNotFoundErrorJSONDecodeError)rJfps r,_get_progress_infoz"MDSDetachedScan._get_progress_infos "05577 %2y}} % % % % % % % % % % % % % % % % % %!4#78   III s3AA AAAA AA)(A)c|} tt|dS#t$r|rdndcYSwxYw)N progress_maindr)rarBfloatKeyErrorrX)rJ progress_infos r,progresszMDSDetachedScan.progressso//11  :u]?;<<== = : : :0022933 9 9 9 :s!8"AAcP|jdkrdSdtS)Nrz avd scanningz {} scanning)rhformatrrRs r,phasezMDSDetachedScan.phases( =A  !>##C(((r+ct t|dS#t$rYdSwxYw)Ntotal_db_countr)rBrarfrRs r,total_resourceszMDSDetachedScan.total_resourcessI t..001ABCC C   11 s &) 77rEc~ |jj5}tj|}dddn #1swxYwY|jjj}n#ttj f$rYdSwxYwt|d|d|dt|S)Nr=r>r?)r=r>r?r@) rUr1r[r\r]pathstatst_mtimer^r_r<rB)rJr`infor@s r,_load_scan_infozMDSDetachedScan._load_scan_infos "16688 %By}} % % % % % % % % % % % % % % %',11338w+> >ur+cKx}tt|j|jj|j} fdjj dD}nj#t$r }t|d}~wt$rH}td|t!||cYd}~Sd}~wwxYw|s|d|S|D]=}t#|j}t$j|d{V>t+|jd{V}|||S)N)argsrpscan_idtypec:g|]}|Sr*rx).0r0rJs r, z,MDSDetachedScan.complete..s7((55r+ report*.jsonzUnable to parse MDS reportzNo reports foundr)rtrrr=r? detached_idr>rUrpglobr^r Exceptionlogger exceptionupdate_with_errorrAr r sinkprocess_messager"update_with_report)rJ scan_infomessagehit_report_listereport report_msg result_reports` r,completezMDSDetachedScan.completes--// /I 8 %'$$     #'#4#9#>#>~#N#NOO! 2 2 2* 1      9 : : :  % %c!ff - - -NNNNNN     % %&8 9 9 9N% 5 5F"643CDDDJ&((44 4 4 4 4 4 4 4 4)/4;KLLLLLLLL ""=111s* +A55 C?B C=CCCc@Kd}|tz}||kr^ |}nk#ttf$r&}t jdd{V|}Yd}~nd}~wwxYw||k^t d|j|dS tj |}| |r| dSdS#tj $r&}t d|Yd}~dSd}~wwxYw)NzICannot find the mds process to kill (%s): %r. Assuming it's already dead.z0Problem when killing the running mds process: %s)MDS_PID_WAIT_TIMEget_pidr^ ValueErrorasynciosleeprwarningrpsutilProcessrkillError)rJtimererrordeadlinepiderrrs r,kill_running_scan_processz)MDSDetachedScan.kill_running_scan_processsv577..egg   llnn%z2   mA&&&&&&&&&  egg   NN/      F >#&&D''--    |    NNBC          s-;A2 A--A2'=C((D7DDTN) exit_typer scan_pathr> scan_startedr=outrrrrr>rr=rrc K|r|d{V|ptg} |p| j}|p| j}|p| j}t |p| j}|t||||j ||t tj d{V|j } |tjkrt| jp|} t| jp| } t%}| |d<| |d<t&d|j | | ||d<d|d <|j |d <||d <||d{V| jst&d | dSt/t1| dS) aRemoves aborted detached scan from scan_queue and writes it to DB. - Parses data about scan from scan_queue and writes it to DB - Kills scan process, if it exists - Deletes scan_dir - Processes MalwareScan and ScanFailed(in case of 'ABORTED') messages :param sink: the sink to send messages :param exit_type: 'ABORTED' by default, if stopped by user, then 'STOPPED' :param kill: try to kill a process :param scan_path: which path was scanned :param scan_type: what is the scan's type :param scan_started: when was the scan started (if known) :param cmd: command line arguments :param out: command stdout :param err: command stderr N)r=)rrrprrr@ completedrrzScan %s was aborted: %s, %scommandabortedrrrpzNo such directory: %s)rrtr<r=r?r>rBr@rrrtimerUrABORTEDr#log_fileerr_filer rrrpis_dirrrA)rJrrrrr>rr=rrrscan_dirstdoutstderrmsgs r,handle_aborted_processz&MDSDetachedScan.handle_aborted_processs>  30022 2 2 2 2 2 2 2((**@jR.@.@.@ "Y]7!7 4!4 <<9+<== "" ($dikk**      $ ,4 4 4&x'899@SF&x'899@SF,,CCJCJ NN-t/?   !C N&C N!-C N#CK&&s++ + + + + + + +}##%% " NN2H = = = = = 3x== ! ! ! ! !r+)r'r(r)r.DETACHED_DIR_CLSpropertyrSrXrarhrkrnrr<rtrx classmethodboolrrrr monotonicrrrrArerrr*r+r,rNrNs) **X*444::X:))X) X  *!5        $[ ! 3!!!!F59N>.5#'#'(,#'E"E"E" E"  E" C= E"C=E"uoE"d3i E"E"E" E"E"E"E"E"E"r+rNcneZdZUdZeeed<eeed<defdZedZ de fdZ dS) MDSDetachedMutableOperationz=Parsing of operations that can succeed or fail for any DB hit SUCCESS_MSGFAIL_MSGrEcKi} fdjjdD}|d{V}|sdjdS|D]=}t |j}tj |d{V>j|d<j d i|S#t$r }t|d}~wt$rb}t||d<t d jd t|cYd}~Sd}~wwxYw) Nc:g|]}|Sr*r)rrrJs r,rz8MDSDetachedMutableOperation.complete..Ts7((00r+rzNo z reports foundrrrrzUnable to parse MDS z reportr*)rUrpr _parse_reportrNAMEr rr rrrr^rr&rArr)rJrrrrrs` r,rz$MDSDetachedMutableOperation.completeQs /"/499.IIO!..????????G# L}}+J+J+J+J}KKK) 9 9&vt7GHHH f,,Z8888888888!%!1GI #4#..g.. .! 2 2 2* 1& / / /"1vvGG    FDIFFF G G G==s1vv=.. . . . . . . /s+AC EC E$AE;EEc|5}tj|cdddS#1swxYwYdSrPrv)r0rws r,rxz/MDSDetachedMutableOperation._load_single_reportjs      19Q<<                  ryc <Kttjd{V}t t jd{V}t}t}|D]}|dx}r3td|j d|t||ds-|ttj ||||j z}t|ttj ||||j z}||dS)N error_listzErrors in MDS z: %srows_with_error) succeededfailed)setr HostingPanel get_usersrpwdgetpwallrrrr&r! from_reportr)rJrusers_from_panelpw_allrrrerrorss r,rz)MDSDetachedMutableOperation._parse_reportosI]%?%A%A%K%K%M%MMMMMMMNN ........EE %  F --v 6 =di===vFFF-f555+, S*6 0&$:J #*6 0&$:J '&999r+N) r'r(r)__doc__rr r5r staticmethodrxdictrr*r+r,rrKsGGg7m/////2  \ :d::::::r+rc eZdZdZeZeZeZ e de fdZ e jdddededeeddfd Zedefd ZdS) MDSDetachedCleanupcleanuprEc,t|jSNr)rrrRs r,on_complete_messagez&MDSDetachedCleanup.on_complete_messages#$    r+Nrrrrrc K|tjks Jdtd|j|t |jj}t |jj}td|d|jd|d|}| |d{Vtj t|jjd dS) Nz;Cleanup cannot be stopped, only aborted status is supportedzCleanup %s was %spath: , detached_id: , out: , err: rT ignore_errors)rrrrsrr#rUrrrrshutilrrArprJrrrrrrs r,rz)MDSDetachedCleanup.handle_aborted_processs  -5 5 5 5 H 6 5 5  ')99EEE"4#4#=>>"4#4#=>>*!!! $ 0!!!!!!   ""3''''''''' c$+011FFFFFFr+cL|r!|}t|vod|vSdS)Nz--cleanFr{r}s r,rz&MDSDetachedCleanup.process_is_suitables2  @llnnGw&?9+? ?ur+)r'r(r)rr8rrrrrrrrrrr rArrrrrr*r+r,rrs D,(K+H  %9   X .5#' GGGG G C= G  GGGG:$[r+rc eZdZdZeZeZeZ e de fdZ e defdZejdddeded eeddfd ZdS) MDSDetachedRestorerestorerEcL|r!|}t|vod|vSdS)Nz --restoreFr{r}s r,rz&MDSDetachedRestore.process_is_suitables2  BllnnGw&A;'+A Aur+c,t|jSr)rrrRs r,rz&MDSDetachedRestore.on_complete_messages%$    r+Nrrrrc K|tjks Jdtd|j|t |jj}t |jj}td|d|jd|d|}| |d{Vtj t|jjd dS) Nz;Restore cannot be stopped, only aborted status is supportedzRestore %s was %srrrrrTr)rrrrsrr#rUrrrrrrrArprs r,rz)MDSDetachedRestore.handle_aborted_processs  -5 5 5 5 H 6 5 5  ')99EEE"4#4#=>>"4#4#=>>*!!! $ 0!!!!!!   ""3''''''''' c$+011FFFFFFr+)r'r(r)rr:rrrrrrrrrrrrrr rArrr*r+r,rrs D,(K+H$[  %;   X .5#' GGGG G C= G  GGGGGGr+r)Irrr\loggingrrrabcr dataclassesrpathlibrtypingrrrr"defence360agent.contracts.messagesr r r &defence360agent.internals.global_scoper "defence360agent.internals.the_sinkr defence360agent.subsys.panelsrdefence360agent.utilsrdefence360agent.utils.threadsrimav.contracts.messagesrrrrrrrimav.malwarelib.configrrimav.malwarelib.scanrrimav.malwarelib.scan.detachedrrrimav.malwarelib.scan.mdsrr imav.malwarelib.scan.mds.reportr!r"imav.malwarelib.scan.utilsr#r getLoggerr'rrr&r.r8r:r<rDrNrrrr*r+r,r s\*  !!!!!!''''''''''  544444666666777777((((((333333 32222222988888  8 $ $     i           KK  IIIII,cIII C"C"C"C"C"*LC"C"C"L;:;:;:;:;:"6;:;:;:|.....4...b,G,G,G,G,G4,G,G,G,G,Gr+