fork download
  1. <?php
  2. class WebSecurityMonitor {
  3. private $suspiciousPatterns = [
  4. 'SQL injection' => '/(\%27)|(\')|(\-\-)|(%23)|(#)/',
  5. 'XSS' => '/((\%3C)|<)((\%2F)|\/)*[a-z0-9\%]+((\%3E)|>)/',
  6. 'File inclusion' => '/((\.\.\/)|(%2e%2e%2f))/',
  7. 'Command injection' => '/(&)|(\|)|(\;)/'
  8. ];
  9.  
  10. private $logFile = 'security.log';
  11.  
  12. public function monitorRequest() {
  13. // Monitor GET and POST separately since $_REQUEST might not be available
  14. $this->checkArray($_GET, 'GET');
  15. $this->checkArray($_POST, 'POST');
  16.  
  17. $this->checkServerVariables();
  18. $this->logAccess();
  19.  
  20. echo "Security check completed.\n";
  21. }
  22.  
  23. private function checkArray($array, $type) {
  24. foreach ($array as $key => $value) {
  25. $this->checkInput($key, $value, $type);
  26. }
  27. }
  28.  
  29. private function checkInput($key, $value, $type) {
  30. if (is_array($value)) {
  31. foreach ($value as $subKey => $subValue) {
  32. $this->checkInput($key . '[' . $subKey . ']', $subValue, $type);
  33. }
  34. return;
  35. }
  36.  
  37. foreach ($this->suspiciousPatterns as $attack => $pattern) {
  38. if (preg_match($pattern, $value)) {
  39. $this->logAttack($attack, "$type: $key", $value);
  40. $this->blockRequest();
  41. }
  42. }
  43. }
  44.  
  45. private function checkServerVariables() {
  46. $importantes = ['HTTP_USER_AGENT', 'HTTP_REFERER', 'REQUEST_METHOD'];
  47. foreach ($importantes as $header) {
  48. if (isset($_SERVER[$header])) {
  49. $this->checkInput($header, $_SERVER[$header], 'SERVER');
  50. }
  51. }
  52. }
  53.  
  54. private function logAttack($type, $input, $value) {
  55. $log = sprintf(
  56. "[%s] Attack detected: %s, Input: %s, Value: %s\n",
  57. date('Y-m-d H:i:s'),
  58. $type,
  59. $input,
  60. $value
  61. );
  62. error_log($log); // Write to error log instead of file
  63. echo $log; // Also output to console for testing
  64. }
  65.  
  66. private function logAccess() {
  67. $log = sprintf(
  68. "[%s] Access logged - IP: %s, Method: %s, URI: %s\n",
  69. date('Y-m-d H:i:s'),
  70. $_SERVER['REMOTE_ADDR'] ?? 'unknown',
  71. $_SERVER['REQUEST_METHOD'] ?? 'unknown',
  72. $_SERVER['REQUEST_URI'] ?? 'unknown'
  73. );
  74. error_log($log);
  75. echo $log;
  76. }
  77.  
  78. private function blockRequest() {
  79. echo "Access Denied - Suspicious activity detected\n";
  80. exit();
  81. }
  82. }
  83.  
  84. // Test the monitor
  85. $monitor = new WebSecurityMonitor();
  86.  
  87. // Simulate some requests for testing
  88. $_GET['test1'] = 'normal input';
  89. $_GET['test2'] = "'; DROP TABLE users; --"; // SQL injection attempt
  90. $_POST['test3'] = '<script>alert("xss")</script>'; // XSS attempt
  91.  
  92. $monitor->monitorRequest();
  93. ?>
Success #stdin #stdout #stderr 0.03s 26488KB
stdin
Standard input is empty
stdout
[2025-01-13 07:55:21] Attack detected: SQL injection, Input: GET: test2, Value: '; DROP TABLE users; --
Access Denied - Suspicious activity detected
stderr
[2025-01-13 07:55:21] Attack detected: SQL injection, Input: GET: test2, Value: '; DROP TABLE users; --