HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //opt/imunify360/venv/lib/python3.11/site-packages/imav/wordpress/__pycache__/utils.cpython-311.pyc
�

��$8��	��^�dZddlZddlZddlZddlZddlmZddlmZmZddl	m
Z
ddlmZddl
mZmZmZddlmZdd	lmZdd
lmZddlmZmZddlmZdd
lmZddl m!Z!ddl"m#Z#ddl$m%Z%ddl&m'Z'dZ(dZ)ej*e+��Z,ed���de-e.e/e.ffd���Z0de.de.de/fd�Z1e
d���de2fd���Z3d�Z4de.d e/de/fd!�Z5de.d"e.de/e.fd#�Z6d$e#de.dee.fd%�Z7de.de/fd&�Z8de.de-fd'�Z9d(�Z:d)e;d*e;de.d$e#d+e-de-fd,�Z<d-e.d.e=d/e=ddfd0�Z>dS)1u

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)�defaultdict)�datetime�	timedelta)�	lru_cache)�Optional)�choose_value_from_config�MalwareScanSchedule�MalwareScanScheduleInterval)�
LicenseCLN)�HostingPanel)�Plesk)�async_lru_cache�atomic_rewrite)�
MalwareHit)�QueueSupervisorSync)�	user_list)�WPSite)�WP_CLI_WRAPPER_PATH)�PHPErrorz/usr/sbin/cagefs_enter_userz/usr/sbin/cagefsctl�<)�ttl�returnc��K�t��}|����d{V��}tt��}|���D]%\}}|D]}||�|����&|S)zN
    Get a mapping of docroots to their associated domains, with caching.
    N)r�get_domain_pathsr�list�items�append)�
hosting_panel�panel_paths�docroot_map�domain�docroots�docroots      �I/opt/imunify360/venv/lib/python3.11/site-packages/imav/wordpress/utils.pyrr=s�����
!�N�N�M�%�6�6�8�8�8�8�8�8�8�8�K��d�#�#�K�'�-�-�/�/�0�0�����	0�	0�G��� �'�'��/�/�/�/�	0����php_pathr#c�0�tt��||gS)zGet wp cli common command list)�strr)r&r#s  r$�
wp_wrapperr)Ks���#�$�$�h��8�8r%�)�maxsizec��tj�t��r$tjttj��st
��Stjtdgdd���}|j	dkrt
��S|j
����d��}t
|dd���S)z)Get the list of users enabled for CageFS.z--list-enabledT)�capture_output�textr�
r*N)
�os�path�isfile�CAGEFS_CTL_PATH�access�X_OK�set�
subprocess�run�
returncode�stdout�strip�split)�result�liness  r$�get_cagefs_enabled_usersr?Ps����7�>�>�/�*�*��"�)����3�3���u�u��
�^�	�*�+�D�t����F���A����u�u���M���!�!�'�'��-�-�E��u�Q�R�R�y�>�>�r%c�8�t���dS)z-Clear the cache for get_cagefs_enabled_users.N)r?�cache_clear�r%r$�$clear_get_cagefs_enabled_users_cacherCbs���(�(�*�*�*�*�*r%�username�argsc��|t��vrTtj�t��r0tjttj��rtd|g|�Sddd|dtj|��gS)zNBuild the necessary command to run the given cmdline args with specified user.z--no-io-and-memory-limit�suz-sz	/bin/bashz-c)	r?r0r1r2�CAGEFS_ENTER_PATHr4r5�shlex�join)rDrEs  r$�build_command_for_userrKgs����+�-�-�-�-�
�7�>�>�+�,�,�	����r�w�2
�2
�	�"�*����	�
�	
�����
�
�4���
�r%�domain_to_excludec��x�K�t���d{V��}|�|g��}�fd�|D��S)z�
    Get all domains associated with a given document root, excluding one domain.
    It's panel-agnostic and uses a cached mapping.
    Nc� ��g|]
}|�k�|��SrBrB)�.0r!rLs  �r$�
<listcomp>z+get_domains_for_docroot.<locals>.<listcomp>�s$���L�L�L�v��:K�0K�0K�F�0K�0K�0Kr%)r�get)r#rLr �all_domainss `  r$�get_domains_for_docrootrS~sS�����)�*�*�*�*�*�*�*�*�K��/�/�'�2�.�.�K�L�L�L�L��L�L�L�Lr%�sitec��P���	K�ddlm}m}|���|���	dtdttf��	�fd�}||j��}|r|St
|j|j����d{V��}|D]}||��}|r|cS�td|j�d	������)
z/Determine PHP binary path for the given WPSite.r)�get_domains_php_info�get_installed_php_versionsr!rc�����|��}|r|�d���krdS|�d��}|sdS�D]2}|�d��|kr|�d��cS�3dS)NrD�display_version�
identifier�bin)rQ)r!�domain_info�php_display_version�php_version�domains_php_info�installed_php_versionsrDs    ���r$�find_php_binary_for_domainz7get_php_binary_path.<locals>.find_php_binary_for_domain�s����&�*�*�6�2�2���	�k�o�o�j�9�9�X�E�E��4�)�o�o�.?�@�@��"�	��4�1�	.�	.�K����|�,�,�0C�C�C�"���u�-�-�-�-�-�D��tr%)rLNz+PHP binary was not identified for docroot: z, username: )	�clcommon.cpapirVrWr(rr!rSr#r)
rTrDrVrWra�php_binary_path�domainsr!r_r`s
 `      @@r$�get_php_binary_pathre�sX���������������
,�+�-�-��7�7�9�9���3��8�C�=���������1�0���=�=�O�����,��������������G��#�#��4�4�V�<�<���	#�"�"�"�"�	#��	�d�l�	�	��	�	���r%c�6�tj|���\}}|S)z�
    Get malware history for the specified user.

    This is an equivalent of calling `imunify360-agent malware history list --user {username}`.
    ``
    )�user)r�malicious_list)rD�	max_count�hitss   r$�get_malware_historyrk�s!��#�1�x�@�@�@��Y���Kr%c��K�t|��}tj|j|h����d{V��\}}|siStj|dd���}|dS)z�
    Get the last scan for the specified user.

    This is an equivalent of calling `imunify360-agent malware user list --user {username}`.
    )�matchN�	scan_dateT)�descr)�	ScanQueuer�fetch_user_list�get_scans_from_paths�sort)�sinkrD�queue�_�userss     r$�
get_last_scanrx�s�����
�d�O�O�E��.�
�"�8�*����������H�A�u����	��N�5�+�D�9�9�9�E���8�Or%c	���tj��}tjtjkrP|�tjddd���}||kr|td���z
}|�	��Stjtj
kr�tj|���dzdzz
dzdz}|dkr|j
tjkrd}|t|���z}|�tjddd����	��Stjtjk�r
ddlm��fd�}|jtjkpS|jtjko|j
tjkp)tj�|j|j��dk}|rU||j|jtj��\}}|�tj||tjddd���}n.|�tjtjddd�	��}|�	��SdS)
Nr)�hour�minute�second�microsecondr*)�days�)�
monthrangec���||}}|dz
}|dkrd}|dz
}	�||��d}||kr||fS|dz
}|dkrd}|dz
}�/)z;Find the next month that has at least given number of days.r*�rB)�year�monthr~�current_year�
current_month�
days_in_monthr�s      �r$�find_next_suitable_monthz?calculate_next_scan_timestamp.<locals>.find_next_suitable_month�s����*.��-�L�
�Q��M��r�!�!� !�
���!��
&� *�
�<�� G� G�� J�
��=�(�(�'��6�6���"�
� �2�%�%�$%�M� �A�%�L�
&r%)�dayr�r�rzr{r|r})r�rzr{r|r})r�utcnowr	�INTERVAL�Interval�DAY�replace�HOURr�	timestamp�WEEK�DAY_OF_WEEK�weekdayrz�MONTH�calendarr�r��DAY_OF_MONTHr�r�)	�today�	next_scan�
days_ahead�next_scan_dater��should_advance_month�	next_year�
next_monthr�s	        @r$�calculate_next_scan_timestampr��sw����O���E��#�x�|�3�3��M�M�$�)����	"�
�
�	��I������*�*�*�*�I��"�"�$�$�$��#�x�}�4�4�
 �+�u�}�}����/B�a�.G�G�!�K�
��
���?�?�u�z�-@�-E�E�E��J���
�!;�!;�!;�;���%�%�$�)�!�A�1�&�
�
�
�)�+�+�	��#�x�~�5�5�'�'�'�'�'�'�	&�	&�	&�	&�	&�0
�I�+�8�8�
5��	�0�=�=�;��J�"5�":�:�	
5�#�/��j���U�[�1�1�!�4�5�	� �	�$<�$<��
�E�K�)<�)I�%�%�!�I�z�#�]�]�'�4� ��(�-����+���N�N�#�]�]�'�4�(�-����+���N��'�'�)�)�)�6�5r%�last_scan_time�next_scan_time�malware_by_sitec��gd�}i}|D]B\}}||vri||<	t|||���\}	}
n#t$rd}	YnwxYw|	|||<�C||||�|jg��|t	j��d�S)a8
    Prepare scan data JSON for a WordPress site.

    Args:
        last_scan_time: Timestamp of the last scan
        next_scan_time: Timestamp of the next scheduled scan
        username: Username of the site owner
        site: WordPress site object
        malware_by_site: Dictionary mapping site docroots to their malware hits

    Returns:
        dict: JSON data ready to be written to scan_data.php. The response includes:
            - lastScanTimestamp: Timestamp of the last scan
            - nextScanTimestamp: Timestamp of the next scheduled scan
            - username: Username of the site owner
            - malware: List of malware hits for the site
            - config: Configuration items for the site
            - license: License information including status and eligibility for Imunify patch
    ))�MALWARE_SCANNING�enable_scan_cpanel)r��default_action)�PROACTIVE_DEFENCE�blamer)rDN)�lastScanTimestamp�nextScanTimestamprD�malware�config�license)r�KeyErrorrQr#r�license_info)r�r�rDrTr��config_sections�config_items�section�option�valuervs           r$�prepare_scan_datar�0s���6���O��L�*�.�.�����,�&�&�$&�L��!�	�/���!����H�E�1�1��
�	�	�	��E�E�E�	����(-��W��f�%�%�,�+��"�&�&�t�|�R�8�8���*�,�,�
��s�.�=�=�content�uid�gidc���|���s|���t��jtjkrdnd}t||d|||���dS)a
    Helper function to write a plugin data file atomically with optional touch.

    Args:
        file_path: Path to the file to write
        content: Content to write to the file
        uid: User ID for file ownership
        gid: Group ID for file ownership
    i �F)�backupr�r��permissionsN)�exists�touchr�NAMEr
r)�	file_pathr�r�r�r�s     r$�!write_plugin_data_file_atomicallyr�ksy��������������(�>�>�.�%�*�<�<�%�%�%�K��������
�����r%)?�__doc__�loggingr0rIr7�collectionsrrr�	functoolsr�typingr� defence360agent.contracts.configrr	r
r��!defence360agent.contracts.licenser�+defence360agent.subsys.panels.hosting_panelr�#defence360agent.subsys.panels.pleskr
�defence360agent.utilsrr�imav.malwarelib.modelr�*imav.malwarelib.scan.queue_supervisor_syncrrp�imav.malwarelib.utilsr�imav.model.wordpressr�imav.wordpressr�imav.wordpress.exceptionrrHr3�	getLogger�__name__�logger�dictr(rrr)r6r?rCrKrSrerkrxr��floatr��intr�rBr%r$�<module>r�s�����*����	�	�	�	���������#�#�#�#�#�#�(�(�(�(�(�(�(�(�����������������������
9�8�8�8�8�8�D�D�D�D�D�D�5�5�5�5�5�5�A�A�A�A�A�A�A�A�,�,�,�,�,�,�������,�+�+�+�+�+�'�'�'�'�'�'�������.�-�-�-�-�-�1��'��	��	�8�	$�	$����R����
��S�$�s�)�^� 4�
�
�
���
�9��9�s�9�t�9�9�9�9�
��1�����#�������"+�+�+�
�S���������.	M�
�	M�%(�	M�	�#�Y�	M�	M�	M�	M�)�F�)�c�)�h�s�m�)�)�)�)�X�#��$�������������$Z*�Z*�Z*�z8��8��8��8��	8�
�8�
�
8�8�8�8�v���"%��,/��	������r%