n($ d Z ddlZddlZddlZddlmZ ddlmZ ej e Z
dZdZdede
d edz fd
Zded edz fdZded edz fd
Zded e
fdZdS )zWordPress rules file management.
This module provides utilities for loading and parsing wp-rules.yaml
from the files.imunify360.com index system.
Available for both AV and IM360 modes.
N)Path)Indexzwp-rules.zipVERSIONindexfilenamereturnc 4 | D ]N}|d |k r@t | |d }| r|c S Ot d|| | j dS )z
Find a file path from the index by filename.
Args:
index: files.Index object
filename: Name of the file to find (e.g., WP_RULES_ZIP_FILENAME)
Returns:
Path to the file or None if not found
nameurlz%s not found in %sN)itemsr
localfilepathexistsloggererror
files_pathtype)r r item file_paths W/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/wordpress/wp_rules.pyfind_file_in_indexr s
! !<8##U00e==>>I!!
!
LL%x1A1A%*1M1MNNN4 zip_pathc t j | d 5 }| d 5 }t j | }ddd n# 1 swxY w Y ddd n# 1 swxY w Y nJ# t j t t j f$ r&}t d| Y d}~dS d}~ww xY wt |t st d| dS |S )z
Extract and parse wp-rules.yaml from the zip file.
Args:
zip_path: Path to wp-rules.zip file
Returns:
Parsed YAML data as dict or None if extraction/parsing fails
rz
wp-rules.yamlNz,Failed to extract or parse wp-rules.yaml: %sz Invalid wp-rules.yaml format: %s)zipfileZipFileopenyaml safe_load
BadZipFileKeyError YAMLErrorr r
isinstancedict)r zip_file yaml_file
rules_dataes r extract_wp_rules_yamlr) , sb
_Xs
+
+ 7x//
79!^I66
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
$.9 CQGGGttttt j$'' 7DDDts] A1 A%AA%A A%A A%A1 %A))A1 ,A)-A1 1!B8B33B8c t | t }|sdS t | }|sdS t d |S )a
Retrieve the latest WordPress rules and return them as a dictionary.
Args:
index: The files.Index object used to locate the wp-rules.zip file.
Returns:
The parsed wp-rules data as a dictionary.
If the wp-rules archive or data cannot be found or parsed, returns None.
Note:
This function returns the raw rules data. Callers that need to modify
rules based on product mode (e.g., ANTIVIRUS_MODE) should do so after
calling this function.
Nz!Successfully parsed wp-rules.yaml)r WP_RULES_ZIP_FILENAMEr) r info)r r r' s r get_wp_rules_datar- D sV " "%)>??H t 'x00J t
KK3444r c " t | t }|sdS | }t d| |S # t $ r&}t d| Y d}~dS d}~ww xY w)a#
Retrieve the WordPress ruleset version string from the VERSION file.
Args:
index: The files.Index object used to locate the VERSION file.
Returns:
The version string from the VERSION file.
If the VERSION file cannot be found or read, returns "NA".
NAz&Successfully read wp-rules version: %szFailed to read VERSION file: %sN)r WP_RULES_VERSION_FILENAME read_textstripr r, Exceptionr )r version_pathversion_stringr( s r get_wp_ruleset_versionr6 b s &e-FGGL t%//117799