Hacked By AnonymousFox

Current Path : /opt/imunify360/venv/lib/python3.11/site-packages/imav/malwarelib/rpc/endpoints/__pycache__/
Upload File :
Current File : //opt/imunify360/venv/lib/python3.11/site-packages/imav/malwarelib/rpc/endpoints/__pycache__/ondemand.cpython-311.pyc

�

��g�7�
�h�UdZddlZddlZddlZddlZddlmZddlmZm	Z	m
Z
mZmZm
Z
mZmZmZddlmZmZmZmZddlmZmZddlmZmZmZddlmZdd	lm Z!dd
l"m#Z#ddl$m%Z%m&Z&m'Z'ddl(m)Z)dd
l*m+Z+ddl,m-Z-m.Z.m/Z/ddl0m1Z1m2Z2ej3e4��Z5e1j6re#hZ7eee%e8d<n!ddl9m:Z:m;Z;m<Z<e#e:e;e<hZ7eee%e8d<	dde!dee=ee
e>efffd�Z?Gd�de-��Z@Gd�de.��ZAd�ZBde	e>gee>ffd�ZCd�ZDd�ZEd�ZFdS) 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 <https://www.gnu.org/licenses/>.

Copyright © 2019 Cloud Linux Software Inc.

This software is also available under ImunifyAV commercial license,
see <https://www.imunify360.com/legal/eula>
�N)�
itemgetter)	�Any�Callable�Dict�Iterable�List�Optional�Set�Tuple�Type)�Core�Malware�MalwareScanIntensity�GENERIC_SENSOR_SOCKET_PATH)�MS_ON_DEMAND_SCAN�check_permission)�ExitDetachedScanType�MalwareScanResourceType�QueuedScanState)�MalwareScan)�QueueSupervisorSync)�AiBolitDetachedScan)�DetachedOperation�
DetachedState�PROCESS_START_TIME)�get_crontab)�ValidationError)�CommonEndpoints�
RootEndpoints�bind)�antivirus_mode�get_abspath_from_user_dir�ABORTABLE_DETACHED_OPERATIONS)�MDSDetachedCleanup�MDSDetachedRestore�MDSDetachedScan�queue�returnc�`����i}g}|�0tt|����t|��g}||d<tj��|||fi|��\}	}
|
D]@}t
jj|d<|d�|d�d|d<�,|d|dz
|d<�Ai�|�|����n8d�|�	|���D��}|r|�|����g}
��
��D]]\}}|d�d|d<n,ttj����|dz
|d<d|d<|
�
d	|i|����^�fd
�|
D��}|
|z}|D]b}|�d���|d|d<|�d���|d
|d<|�d
���|d|d
<�c��fd�|D��}|r@t|��D]0}|�t#|j��|j����1|	|d|�fS)N�paths�scan_status�started�	completed�durationc��g|]\}}|��S�r0)�.0�scan�_s   �[/opt/imunify360/venv/lib/python3.11/site-packages/imav/malwarelib/rpc/endpoints/ondemand.py�
<listcomp>z+_get_prepared_scan_list.<locals>.<listcomp>xs)��
�
�
��T�1�D�
�
�
�)r*)�scans�scanidc�(��g|]}|d�v�|��S�r8r0)r1r2�queued_scanss  �r4r5z+_get_prepared_scan_list.<locals>.<listcomp>�s.�������$�x�.��*L�*L��*L�*L�*Lr6�total�total_resources�createdc�@��g|]}�|dcxkr�k�nn|��S)r>r0)r1�u�since�tos  ��r4r5z+_get_prepared_scan_list.<locals>.<listcomp>�s>���=�=�=�1��!�I�,�!<�!<�!<�!<�"�!<�!<�!<�!<�!<�Q�!<�!<�!<r6)�key�reverse)�strr"rr�
ondemand_listr�stopped�value�scan_summaries�get_scans_from_paths�items�int�time�append�get�reversed�sortr�column_name�desc)rArB�limit�offset�order_byr'�user�	db_kwargs�
user_paths�	max_count�
scans_from_dbr2�user_queued_scans�incomplete_scansr8�complete_scansr7�orderr;s``                @r4�_get_prepared_scan_listr`as�������I��J����3�D�9�9�:�:�K��<M�<M�N�
�'�	�'��*�8�
�r�5�&�(� � �.7� � ��I�}��C�C��-�5�;��]���	�?�"�d�;�&7�&?�#�D����#�K�0�4�	�?�B�D�����L��|��+�+�-�-���
�
� %� :� :�� :� L� L�
�
�
���	I� �/�/�6G�/�H�H�L���$�*�*�,�,�<�<�����	�?�"�#�D����"�4�9�;�;�/�/�$�y�/�A�D��� ��[������6� :�T� :�;�;�;�;�����&����N�
�~�-�E��.�.���8�8�G���$� �!2�3�D��M��8�8�I���&�"�9�o�D��O��8�8�I���&�"�9�o�D��O��=�=�=�=�=��=�=�=�E��N��h�'�'�	N�	N�E��J�J�:�e�&7�8�8�%�*�J�M�M�M�M��e�F�U�F�m�#�#r6c	�,��eZdZ�fd�Zeddd��d���Zeddd��dd	���Zeddd
��deeeeefffd���Z	eddd��	dde
eeeee
fffd
���Z�xZS)�OnDemandUserEndpointsc�t��t���|��t|���|_dS�N)�sink��super�__init__�	ScanQueuer'��selfre�	__class__s  �r4rhzOnDemandUserEndpoints.__init__��1���
����������D�)�)�)��
�
�
r6�malware�	on-demandz
start-userc��K�tt|��tt|����}tjtjtj|d�}tj	g}tjr,tj
r |�dtj��|D]}|jjd|g|d�|���d{V���dS)N)�
intensity_cpu�intensity_io�
intensity_ram�	initiatorr)r*�
resource_typer0)rrrEr"r�USER_CPU�USER_IO�USER_RAMr�FILEr!�disabledr�DATABASE_SCAN_ENABLED�insert�DBr'�put)rkrW�path�	scan_args�resource_typesrus      r4�ondemand_startz$OnDemandUserEndpoints.ondemand_start�s������*�D�1�1�1��,�T�2�2�3�3��1�:�0�8�1�:��	
�
�	�2�6�7���"�	A�w�'D�	A��!�!�!�%<�%?�@�@�@�+�	�	�M� �$�*�.���f�+������
�
�
�
�
�
�
�
�	�	r6z	stop-userr(Nc��K�tt|��|jjj}t	t|����}|j�|g��D]m\}}|j�|jg��|j|jk}|j	�
|jtj
||j����d{V���ndS)N)re�	exit_type�kill�scan_started)rrr'�current_scanrEr"rJ�remover8�
detached_scan�handle_aborted_process�_sinkr�STOPPEDr,)rkrWr�rr2r3r�s       r4�
ondemand_stopz#OnDemandUserEndpoints.ondemand_stop�s������*�D�1�1�1��z�'�4���,�T�2�2�3�3���z�6�6��v�>�>�	�	�G�D�!��J���t�{�m�,�,�,��;�,�"5�5�D��$�;�;��Z�.�6��!�\�	<���
�
�
�
�
�
�
�
�	�	r6zstatus-userc��K�tt|����}|jj�|���}|�|���ndt
jji}dd|dii}d|vr|d|dd<|S)N)r�statusrK�progress)rEr"r'�findr�rrGrH)rkrWrr2r��results      r4�ondemand_statusz%OnDemandUserEndpoints.ondemand_status�s������,�T�2�2�3�3���z��$�$�$�$�/�/����
�K�K�M�M�M��O�3�9�:�	�
�H�f�X�&6�7�8������*0��*<�F�7�O�J�'��
r6z	list-userc	��K�|���dvsJd|�d����t||||||jj|���S)a�Get list of user scans.

        - Parses info about scans from DB
        - Updates info about scans in scan_queue and parses it

        :param since: scan start timestamp
        :param to: scan end timestamp
        :param limit: count of scans to print
        :param offset: offset of scans in DB to print
        :param order_by: name of column, by which sort data
        :return: (number of returned scans, list of data about scans)
        )��rootzUnexpected user '�')r'rW)�stripr`r')rkrWrArBrTrUrVs       r4rFz#OnDemandUserEndpoints.ondemand_list�sf���� �z�z�|�|�<�/�/�/�1L�T�1L�1L�1L�/�/�/�&�������*�"��
�
�
�	
r6)r(N�N)�__name__�
__module__�__qualname__rhr r�r�rrEr�rrLrrrF�
__classcell__�rls@r4rbrb�s/�������*�*�*�*�*�
�T�)�[�,�/�/���0�/��(
�T�)�[�+�.�.�
�
�
�/�.�
�
�T�)�[�-�0�0��T�#�t�C��H�~�2E�-F����1�0��
�T�)�[�+�.�.�7;�
�
�	�s�D��c�3�h��(�(�	)�
�
�
�/�.�
�
�
�
�
r6rbc	���eZdZ�fd�Zeddd��d���Zeddd��dedd	fd
���Zeddd��d���Zeddd
��	dde	e
eee
efffd���Zedddd��	dd���Zedddd��deee
fd���Zeddd��d���Z�xZS)�OnDemandEndpointsc�t��t���|��t|���|_dSrdrfrjs  �r4rhzOnDemandEndpoints.__init__�rmr6rnro�startc��\K�|s|std���|j���std���|r4|jjd|gtjdd�t
|�����d{V��|r6|jjd|gtjdd�t
|�����d{V��dSdS)N�3Either --scan-file or --scan-db should be specifiedz!On-demand scan is already running)r*rurtr0)rr'�is_emptyr~rr}�
split_argsry)rkr�	scan_file�scan_dbr�s     r4r�z OnDemandEndpoints.ondemand_start�s8�����	��	�!�E���
��z�"�"�$�$�	G�!�"E�F�F�F��	� �$�*�.���f�5�8�����Y�'�'�	��
�
�
�
�
�
�
��	� �$�*�.���f�5�:�����Y�'�'�	��
�
�
�
�
�
�
�
�
�	�	r6�stop�allr(Nc��zK�|jjj}|s|r|g}n+tt|jjj����}d�|D��}|j�|���|D]J}|j|jk}|j�|j	|tj|j����d{V���KdS)z�CLI method to remove scans from ScanQueue

        - Handles removed scans as aborted if they are detached

        :param all
        c��g|]	}|j��
Sr0r:)r1r2s  r4r5z3OnDemandEndpoints.ondemand_stop.<locals>.<listcomp>!s��:�:�:�D�D�K�:�:�:r6)�scan_ids)rer�r�r�N)
r'r��listrPr7r�r8r�r�r�rr�r,)rkr�r��
scans_to_stopr�r2r�s       r4r�zOnDemandEndpoints.ondemand_stops������z�'�4���	C�|�	C�)�N�M�M� ��$�*�*:�*@�!A�!A�B�B�M�:�:�M�:�:�:���
���8��,�,�,�!�	�	�D��;�,�"5�5�D��$�;�;��Z��.�6�!�\�	<���
�
�
�
�
�
�
�
�	�	r6r�c��@K�|j���}d|iS)NrK)r'r�)rkr�s  r4r�z!OnDemandEndpoints.ondemand_status,s$������"�"�$�$���� � r6r�c��DK�t||||||jj���S)a�CLI method to print list of scans.

        - Parses info about scans from DB
        - Updates info about scans in scan_queue and parses it

        :param since: scan start timestamp
        :param to: scan end timestamp
        :param limit: count of scans to print
        :param offset: offset of scans in DB to print
        :param order_by: name of column, by which sort data
        :return: (number of returned scans, list of data about scans)
        )r')r`r')rkrArBrTrUrVs      r4rFzOnDemandEndpoints.ondemand_list1s1���� '��2�u�f�h�d�j�6F�
�
�
�	
r6r'r~Fc��K�|s|std���|r3|jjd|tj|d�t|�����d{V��|r5|jjd|tj|d�t|�����d{V��dSdS)Nr�)r*ru�
prioritizer0)rr'r~rr}r�ry)rkr*r�r�r�r�s      r4�ondemand_queue_putz$OnDemandEndpoints.ondemand_queue_putEs�����	��	�!�E���
��	� �$�*�.���5�8�%����Y�'�'�	��
�
�
�
�
�
�
��	� �$�*�.���5�:�%����Y�'�'�	��
�
�
�
�
�
�
�
�
�	�	r6r�r�c��VK�|jjj}|s!|r|gng}|j���n9|jj�|��}|j�|��|D]6}|j�|jtj||u����d{V���7dS)N)rer�r�)	r'r�r��find_allr�r�r�rr�)rkr�r�r;r2s     r4�ondemand_queue_removez'OnDemandEndpoints.ondemand_queue_remove]s������z�'�4���	(�-9�A�L�>�>�r�L��J��������:�+�4�4�X�>�>�L��J���h�'�'�'� �	�	�D��$�;�;��Z�.�6��\�)�<���
�
�
�
�
�
�
�
�	�	r6zcheck-detachedc���K�t�d��tt���D]�}|�t
���}|tjkr+t�d��t��dS|tj	kr+t�d��t��dS��t�d��dS)z�
        Check if there are failed on-demand scans and start the agent so
        that we can handle aborted scans and proceed with the next scan
        in the queue
        z1Checking detached scan directory for failed scans)�
dir_lister)�
start_timez"Found failed scan. Waking up agentz$Finished scan found. Waking up agentzNo failed on-demand scans foundN)
�logger�info�_active_detached_ops�	_list_dir�get_detached_process_staterr�ABORTED�_try_wake_up_agent�FINISHED)rk�detached_op�_states   r4�ondemand_check_detachedz)OnDemandEndpoints.ondemand_check_detachedns�����	���G�H�H�H�/�9�E�E�E�
	;�
	;�K� �;�;�-�<���F���.�.�.����@�A�A�A�"�$�$�$������/�/�/����B�C�C�C�"�$�$�$����0�

�K�K�9�:�:�:�:�:r6r�)F)r�r�r�rhr r��boolr�r�rrLrrrErrFr�r	r�r�r�r�s@r4r�r��s��������*�*�*�*�*�
�T�)�[�'�*�*���+�*��.
�T�)�[�&�)�)��t������*�)��0
�T�)�[�(�+�+�!�!�,�+�!�
�T�)�[�&�)�)�15�
�
�	�s�D��c�3�h��(�(�	)�
�
�
�*�)�
�&
�T�)�[�'�5�1�1�49����2�1��.
�T�)�[�'�8�4�4��H�T�#�Y�4G����5�4�� 
�T�)�[�"2�3�3�;�;�4�3�;�;�;�;�;r6r�c#�dK�	tj|��Ed{V��dS#t$rgcYSwxYwr�)�os�listdir�FileNotFoundError)�_dirs r4r�r��sW������:�d�#�#�#�#�#�#�#�#�#�#�#�������	�	�	����s� �/�/r�c#�hK�tD]'}||jj��D]}||��V���(dSr�)r#�DETACHED_DIR_CLS�DETACHED_DIR)r��detached_op_cls�_ids   r4r�r��s\����8�'�'���:�o�>�K�L�L�	'�	'�C�!�/�#�&�&�&�&�&�&�	'�'�'r6c�n�	tjtjtj��5}|�tj��|�t��|�d��ddd��dS#1swxYwYdS#tttjf$rYdSwxYw)Ns+{"method": "MALWARE_CHECK_DETACHED_SCANS"}
)�socket�AF_UNIX�SOCK_STREAM�
settimeoutr
�DEFAULT_SOCKET_TIMEOUT�connectr�send�ConnectionRefusedErrorr��timeout)�ss r4r�r��s���
�
�]�6�>�6�+=�
>�
>�	D�!�
�L�L��4�5�5�5�
�I�I�0�1�1�1�
�F�F�B�C�C�C�	D�	D�	D�	D�	D�	D�	D�	D�	D�	D�	D�	D����	D�	D�	D�	D�	D�	D��
#�$5�v�~�F�
�
�
����
���s5�)B�AB�:B�B�B�B�B�B4�3B4c�~�|�:tttj|�d������SdS)N�,)r��maprEr��split)�masks r4�_split_maskr��s4�����C��	�4�:�:�c�?�?�3�3�4�4�4��r6c���t|��}d|vr%t|�d����|d<d|vr%t|�d����|d<|S)N�	file_mask�
file_patterns�ignore_mask�exclude_patterns)�dictr��pop)r��argss  r4r�r��se���	�?�?�D��d��� +�D�H�H�[�,A�,A� B� B��_������#.�t�x�x�
�/F�/F�#G�#G��
� ��Kr6r�)G�__doc__�loggingr�r�rM�operatorr�typingrrrrrr	r
rr� defence360agent.contracts.configr
rrr�%defence360agent.contracts.permissionsrr�imav.malwarelib.configrrr�imav.malwarelib.modelr�*imav.malwarelib.scan.queue_supervisor_syncrri�&imav.malwarelib.scan.ai_bolit.detachedr�imav.malwarelib.scan.detachedrrr�imav.malwarelib.scan.crontabr�defence360agent.rpc_toolsr� defence360agent.rpc_tools.lookuprrr �defence360agent.utilsr!r"�	getLoggerr�r��enabledr#�__annotations__�!imav.malwarelib.scan.mds.detachedr$r%r&rLrEr`rbr�r�r�r�r�r�r0r6r4�<module>r�s�����*����	�	�	�	�
�
�
�
�����������
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�������������������������������
.�-�-�-�-�-�����������������������
5�4�4�4�4�4�5�5�5�5�5�5�����������
L�K�K�K�K�K�K�K�	��	�8�	$�	$������C�!�3�t�,=�'>�#?���������������	����	C�!�3�t�,=�'>�#?����@D�;$�;$�/8�;$�
�3��T�#�s�(�^�$�$�%�;$�;$�;$�;$�BQ
�Q
�Q
�Q
�Q
�O�Q
�Q
�Q
�hM;�M;�M;�M;�M;�
�M;�M;�M;�`���'��#����
�1E�(F�'�'�'�'�
�
�
�5�5�5�
����r6

Hacked By AnonymousFox1.0, Coded By AnonymousFox