| 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_firewall_htaccess_rules', [ $this, 'post_firewall_htaccess_rules' ] ); |
| 23 |
add_action( 'patchstack_post_dynamic_firewall_rules', [ $this, 'dynamic_firewall_rules' ] ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Pull the hardening .htaccess rules from the API. |
| 28 |
* Then apply it to the .htaccess file after we create a backup. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function post_firewall_rules() { |
| 33 |
if ( $this->get_option( 'patchstack_license_free', 0 ) == 1 ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$rules = $this->plugin->htaccess->get_firewall_rule_settings(); |
| 38 |
$settings = json_encode( $rules ); |
| 39 |
$results = $this->plugin->api->post_firewall_rule( [ 'settings' => $settings ] ); |
| 40 |
|
| 41 |
// If no rules returned, we assume all settings are turned off. |
| 42 |
if ( empty( $results ) ) { |
| 43 |
$results['rules'] = ''; |
| 44 |
} |
| 45 |
|
| 46 |
// We have rules so apply it to the .htaccess file. |
| 47 |
if ( isset( $results['rules'] ) ) { |
| 48 |
$this->plugin->htaccess->write_to_htaccess( $results['rules'] ); |
| 49 |
return; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Pull the firewall .htaccess rules from the API. |
| 55 |
* Then apply it to the .htaccess file after we create a backup. |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function post_firewall_htaccess_rules() { |
| 60 |
if ( $this->get_option( 'patchstack_license_free', 0 ) == 1 ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$results = $this->plugin->api->post_firewall_htaccess_rule(); |
| 65 |
$rules = ! isset( $results['rules'] ) || empty( $results ) ? '' : $results['rules']; |
| 66 |
|
| 67 |
// Check if we have to update anything at all. |
| 68 |
$hash = sha1( $rules ); |
| 69 |
if ( get_option( 'patchstack_firewall_htaccess_hash', '' ) == $hash || ( get_option( 'patchstack_firewall_htaccess_hash', '' ) == '' && $rules == '' ) ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
// We have rules so apply it to the .htaccess file. |
| 74 |
update_option( 'patchstack_firewall_htaccess_hash', $hash ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Pull the firewall/whitelist rules from the API. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function dynamic_firewall_rules() { |
| 83 |
if ( $this->get_option( 'patchstack_license_free', 0 ) == 1 ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
// Get the firewall and whitelist rules. |
| 88 |
$results = $this->plugin->api->post_firewall_rule_json(); |
| 89 |
if ( ! isset( $results['firewall'] ) ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
// Separate the new firewall engine rules from the old ones. |
| 94 |
$newRules = []; |
| 95 |
$newRulesAP = []; |
| 96 |
$oldRules = []; |
| 97 |
|
| 98 |
// Counters for displaying purposes on the API key page. |
| 99 |
$vPatchCount = 0; |
| 100 |
$ruleCount = 0; |
| 101 |
|
| 102 |
// Parse the rules. |
| 103 |
foreach ( $results['firewall'] as $rule ) { |
| 104 |
if ( isset( $rule['rule_v2'] ) ) { |
| 105 |
$rule['rules'] = $rule['rule_v2']; |
| 106 |
unset( $rule['rule_v2'] ); |
| 107 |
|
| 108 |
// Mark vPatches based on substring. |
| 109 |
if ( stripos( $rule['title'], ' vulnerabilit' ) !== false && stripos( $rule['title'], 'block ' ) !== false ) { |
| 110 |
$vPatchCount++; |
| 111 |
} else { |
| 112 |
$ruleCount++; |
| 113 |
} |
| 114 |
|
| 115 |
// Differentiate between auto prepend rules and regular ones. |
| 116 |
if ( isset( $rule['ap'] ) && !empty( $rule['ap'] ) ) { |
| 117 |
$newRulesAP[] = $rule; |
| 118 |
} else { |
| 119 |
$newRules[] = $rule; |
| 120 |
} |
| 121 |
} else { |
| 122 |
$ruleCount++; |
| 123 |
$oldRules[] = $rule; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Update firewall rules. |
| 128 |
update_option( 'patchstack_firewall_rules', json_encode( $oldRules ), true ); |
| 129 |
update_option( 'patchstack_firewall_rules_v3', json_encode( $newRules ), true ); |
| 130 |
update_option( 'patchstack_firewall_rules_v3_ap', json_encode( $newRulesAP ), true ); |
| 131 |
|
| 132 |
// Update the counters. |
| 133 |
update_option( 'patchstack_vpatches_present', $vPatchCount ); |
| 134 |
update_option( 'patchstack_non_vpatches_present', $ruleCount ); |
| 135 |
|
| 136 |
// Separate the new firewall engine rules from the old ones. |
| 137 |
$newRules = []; |
| 138 |
$oldRules = []; |
| 139 |
foreach ( $results['whitelists'] as $rule ) { |
| 140 |
if ( isset( $rule['rule_v2'] ) ) { |
| 141 |
$rule['rules'] = $rule['rule_v2']; |
| 142 |
unset( $rule['rule_v2'] ); |
| 143 |
$newRules[] = $rule; |
| 144 |
} else { |
| 145 |
$oldRules[] = $rule; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Update whitelist rules. |
| 150 |
update_option( 'patchstack_whitelist_rules', json_encode( $oldRules ), true ); |
| 151 |
update_option( 'patchstack_whitelist_rules_v3', json_encode( $newRules ), true ); |
| 152 |
|
| 153 |
// Update the whitelisted keys. |
| 154 |
update_option( 'patchstack_whitelist_keys_rules', json_encode( $results['whitelist_keys'] ), true ); |
| 155 |
} |
| 156 |
} |
| 157 |
|