PluginProbe
Patchstack – WordPress & Plugins Security / 2.3.6
Patchstack – WordPress & Plugins Security v2.3.6
2.3.7 trunk 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.17 2.1.18 2.1.19 2.1.2 2.1.20 2.1.21 2.1.22 2.1.23 2.1.24 2.1.25 2.1.3 2.1.4 2.1.5 2.1.6 All 49 releases
patchstack / includes / rules.php

rules.php in Patchstack – WordPress & Plugins Security 2.3.6, at includes/rules.php

137 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Do not allow the file to be called directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 /**
9 * This class is used to pull the firewall/whitelist rules from our API.
10 */
11 class P_Rules extends P_Core {
12
13 /**
14 * Add the actions required to pull the rules.
15 *
16 * @param Patchstack $core
17 * @return void
18 */
19 public function __construct( $core ) {
20 parent::__construct( $core );
21 add_action( 'patchstack_post_firewall_rules', [ $this, 'post_firewall_rules' ] );
22 add_action( 'patchstack_post_dynamic_firewall_rules', [ $this, 'dynamic_firewall_rules' ] );
23 }
24
25 /**
26 * Pull the hardening .htaccess rules from the API.
27 * Then apply it to the .htaccess file after we create a backup.
28 *
29 * @return void
30 */
31 public function post_firewall_rules() {
32 if ( $this->get_option( 'patchstack_license_free', 0 ) == 1 ) {
33 return;
34 }
35
36 // Check if server is supported and if htaccess modifications are disabled.
37 if ( get_site_option( 'patchstack_disable_htaccess', 0 ) || ( defined( 'PS_DISABLE_HTACCESS' ) && PS_DISABLE_HTACCESS ) ) {
38 return;
39 }
40
41 $rules = $this->plugin->htaccess->get_firewall_rule_settings();
42 $settings = json_encode( $rules );
43 $results = $this->plugin->api->post_firewall_rule( [ 'settings' => $settings ] );
44
45 // If no rules returned, we assume all settings are turned off.
46 if ( empty( $results ) ) {
47 $results['rules'] = '';
48 }
49
50 // We have rules so apply it to the .htaccess file.
51 if ( isset( $results['rules'] ) ) {
52 $this->plugin->htaccess->write_to_htaccess( $results['rules'] );
53 return;
54 }
55 }
56
57 /**
58 * Pull the firewall/whitelist rules from the API.
59 *
60 * @return void
61 */
62 public function dynamic_firewall_rules() {
63 if ( $this->get_option( 'patchstack_license_free', 0 ) == 1 ) {
64 return;
65 }
66
67 // Get the firewall and whitelist rules.
68 $results = $this->plugin->api->post_firewall_rule_json();
69 if ( ! isset( $results['firewall'] ) ) {
70 return;
71 }
72
73 // Separate the new firewall engine rules from the old ones.
74 $newRules = [];
75 $newRulesAP = [];
76 $oldRules = [];
77
78 // Counters for displaying purposes on the API key page.
79 $vPatchCount = 0;
80 $ruleCount = 0;
81
82 // Parse the rules.
83 foreach ( $results['firewall'] as $rule ) {
84 if ( isset( $rule['rule_v2'] ) ) {
85 $rule['rules'] = $rule['rule_v2'];
86 unset( $rule['rule_v2'] );
87
88 // Mark vPatches based on substring.
89 if ( stripos( $rule['title'], ' vulnerabilit' ) !== false && stripos( $rule['title'], 'block ' ) !== false ) {
90 $vPatchCount++;
91 } else {
92 $ruleCount++;
93 }
94
95 // Differentiate between auto prepend rules and regular ones.
96 if ( isset( $rule['ap'] ) && !empty( $rule['ap'] ) ) {
97 $newRulesAP[] = $rule;
98 } else {
99 $newRules[] = $rule;
100 }
101 } else {
102 $ruleCount++;
103 $oldRules[] = $rule;
104 }
105 }
106
107 // Update firewall rules.
108 update_option( 'patchstack_firewall_rules', json_encode( $oldRules ), true );
109 update_option( 'patchstack_firewall_rules_v3', json_encode( $newRules ), true );
110 update_option( 'patchstack_firewall_rules_v3_ap', json_encode( $newRulesAP ), true );
111
112 // Update the counters.
113 update_option( 'patchstack_vpatches_present', $vPatchCount );
114 update_option( 'patchstack_non_vpatches_present', $ruleCount );
115
116 // Separate the new firewall engine rules from the old ones.
117 $newRules = [];
118 $oldRules = [];
119 foreach ( $results['whitelists'] as $rule ) {
120 if ( isset( $rule['rule_v2'] ) ) {
121 $rule['rules'] = $rule['rule_v2'];
122 unset( $rule['rule_v2'] );
123 $newRules[] = $rule;
124 } else {
125 $oldRules[] = $rule;
126 }
127 }
128
129 // Update whitelist rules.
130 update_option( 'patchstack_whitelist_rules', json_encode( $oldRules ), true );
131 update_option( 'patchstack_whitelist_rules_v3', json_encode( $newRules ), true );
132
133 // Update the whitelisted keys.
134 update_option( 'patchstack_whitelist_keys_rules', json_encode( $results['whitelist_keys'] ), true );
135 }
136 }
137