PageRenderTime 32ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordfence/views/waf/debug.php

https://gitlab.com/edgarze188/sunrise
PHP | 225 lines | 199 code | 22 blank | 4 comment | 11 complexity | c40ca285d88d4761c65f26ef0b7f2362 MD5 | raw file
  1. <?php
  2. /** @var wfRequestModel $hit */
  3. /** @var stdClass $hitData */
  4. $title = sprintf('Debugging #%d as False Positive', $hit->id);
  5. $fields = array(
  6. 'URL' => $hit->URL,
  7. 'Timestamp' => date('r', $hit->ctime),
  8. 'IP' => wfUtils::inet_ntop($hit->IP),
  9. 'Status Code' => $hit->statusCode,
  10. 'User Agent' => $hit->UA,
  11. 'Referer' => $hit->referer,
  12. );
  13. if (isset($hitData->fullRequest)) {
  14. $requestString = base64_decode($hitData->fullRequest);
  15. $request = wfWAFRequest::parseString($requestString);
  16. } else {
  17. $request = new wfWAFRequest();
  18. $request->setAuth(array());
  19. $request->setBody(array());
  20. $request->setCookies(array());
  21. $request->setFileNames(array());
  22. $request->setFiles(array());
  23. $request->setHeaders(array());
  24. $request->setHost('');
  25. $request->setIp('');
  26. $request->setMethod('GET');
  27. $request->setPath('');
  28. $request->setProtocol('http');
  29. $request->setQueryString(array());
  30. $request->setTimestamp('');
  31. $request->setUri('');
  32. $headers = array();
  33. $urlPieces = parse_url($hit->URL);
  34. if ($urlPieces) {
  35. if (array_key_exists('scheme', $urlPieces)) {
  36. $request->setProtocol($urlPieces['scheme']);
  37. }
  38. if (array_key_exists('host', $urlPieces)) {
  39. $request->setHost($urlPieces['host']);
  40. $headers['Host'] = $urlPieces['host'];
  41. }
  42. $uri = '/';
  43. if (array_key_exists('path', $urlPieces)) {
  44. $request->setPath($urlPieces['path']);
  45. $uri = $urlPieces['path'];
  46. }
  47. if (array_key_exists('query', $urlPieces)) {
  48. $uri .= '?' . $urlPieces['query'];
  49. parse_str($urlPieces['query'], $query);
  50. $request->setQueryString($query);
  51. }
  52. $request->setUri($uri);
  53. }
  54. $headers['User-Agent'] = $hit->UA;
  55. $headers['Referer'] = $hit->referer;
  56. $request->setHeaders($headers);
  57. preg_match('/request\.([a-z]+)(?:\[(.*?)\](.*?))?/i', $hitData->paramKey, $matches);
  58. if ($matches) {
  59. switch ($matches[1]) {
  60. case 'body':
  61. $request->setMethod('POST');
  62. parse_str("$matches[2]$matches[3]", $body);
  63. $request->setBody($body);
  64. break;
  65. }
  66. }
  67. }
  68. $request->setIP(wfUtils::inet_ntop($hit->IP));
  69. $request->setTimestamp($hit->ctime);
  70. $waf = wfWAF::getInstance();
  71. $waf->setRequest($request);
  72. $result = '<strong class="ok">Passed</strong>';
  73. $failedRules = array();
  74. try {
  75. $waf->runRules();
  76. } catch (wfWAFAllowException $e) {
  77. $result = '<strong class="ok">Whitelisted</strong>';
  78. } catch (wfWAFBlockException $e) {
  79. $result = '<strong class="error">Blocked</strong>';
  80. $failedRules = $waf->getFailedRules();
  81. } catch (wfWAFBlockSQLiException $e) {
  82. $result = '<strong class="error">Blocked For SQLi</strong>';
  83. $failedRules = $waf->getFailedRules();
  84. } catch (wfWAFBlockXSSException $e) {
  85. $result = '<strong class="error">Blocked For XSS</strong>';
  86. $failedRules = $waf->getFailedRules();
  87. }
  88. ?>
  89. <!doctype html>
  90. <html lang="en">
  91. <head>
  92. <meta charset="UTF-8">
  93. <title><?php echo esc_html($title) ?></title>
  94. <link rel="stylesheet" href="<?php echo wfUtils::getBaseURL() . 'css/main.css' ?>">
  95. <style>
  96. html {
  97. font-family: "Open Sans", Helvetica, Arial, sans-serif;
  98. }
  99. h1, h2, h3, h4, h5 {
  100. margin: 20px 0px 8px;
  101. }
  102. pre, p {
  103. margin: 8px 0px 20px;
  104. }
  105. pre.request-debug {
  106. padding: 12px;
  107. background: #fafafa;
  108. border: 1px solid #999999;
  109. overflow: auto;
  110. }
  111. pre.request-debug em {
  112. font-style: normal;
  113. padding: 1px;
  114. border: 1px solid #ffb463;
  115. background-color: #ffffe0;
  116. border-radius: 2px;
  117. }
  118. pre.request-debug strong {
  119. border: 1px solid #ff4a35;
  120. background-color: #ffefe7;
  121. margin: 1px;
  122. }
  123. .ok {
  124. color: #00c000;
  125. }
  126. .error {
  127. color: #ff4a35;
  128. }
  129. #wrapper {
  130. max-width: 1060px;
  131. margin: 0px auto;
  132. }
  133. </style>
  134. </head>
  135. <body>
  136. <div id="wrapper">
  137. <h1><?php echo esc_html($title) ?></h1>
  138. <table class="wf-table">
  139. <thead>
  140. <tr>
  141. <th colspan="2">Request Details</th>
  142. </tr>
  143. </thead>
  144. <?php foreach ($fields as $label => $value): ?>
  145. <tr>
  146. <td><?php echo esc_html($label) ?>:</td>
  147. <td><?php echo esc_html($value) ?></td>
  148. </tr>
  149. <?php endforeach ?>
  150. </table>
  151. <h4>HTTP Request: <?php echo $result ?></h4>
  152. <?php if (!isset($hitData->fullRequest)): ?>
  153. <em style="font-size: 14px;">This is a reconstruction of the request using what was flagged by the WAF.
  154. Full requests are only stored when <code>WFWAF_DEBUG</code> is enabled.</em>
  155. <?php endif ?>
  156. <pre class="request-debug"><?php
  157. $paramKey = wp_hash(uniqid('param', true));
  158. $matchKey = wp_hash(uniqid('match', true));
  159. $template = array(
  160. "[$paramKey]" => '<em>',
  161. "[/$paramKey]" => '</em>',
  162. "[$matchKey]" => '<strong>',
  163. "[/$matchKey]" => '</strong>',
  164. );
  165. $highlightParamFormat = "[$paramKey]%s[/$paramKey]";
  166. $highlightMatchFormat = "[$matchKey]%s[/$matchKey]";
  167. $requestOut = esc_html($request->highlightFailedParams($failedRules, $highlightParamFormat, $highlightMatchFormat));
  168. echo str_replace(array_keys($template), $template, $requestOut) ?></pre>
  169. <?php if ($failedRules): ?>
  170. <h4>Failed Rules</h4>
  171. <table class="wf-table">
  172. <thead>
  173. <tr>
  174. <th>ID</th>
  175. <th>Category</th>
  176. </tr>
  177. </thead>
  178. <tbody>
  179. <?php
  180. foreach ($failedRules as $paramKey => $categories) {
  181. foreach ($categories as $categoryKey => $failed) {
  182. foreach ($failed as $failedRule) {
  183. /** @var wfWAFRule $rule */
  184. $rule = $failedRule['rule'];
  185. printf("<tr><td>%d</td><td>%s</td></tr>", $rule->getRuleID(), $rule->getDescription());
  186. }
  187. }
  188. }
  189. ?>
  190. </tbody>
  191. </table>
  192. <?php endif ?>
  193. <p>
  194. <button type="button" id="run-waf-rules">Run Through WAF Rules</button>
  195. </p>
  196. <script>
  197. document.getElementById('run-waf-rules').onclick = function() {
  198. document.location.href = document.location.href;
  199. }
  200. </script>
  201. </div>
  202. </body>
  203. </html>