<?php
class WebSecurityMonitor {
private $suspiciousPatterns = [
'SQL injection' => '/(\%27)|(\')|(\-\-)|(%23)|(#)/',
'XSS' => '/((\%3C)|<)((\%2F)|\/)*[a-z0-9\%]+((\%3E)|>)/',
'File inclusion' => '/((\.\.\/)|(%2e%2e%2f))/',
'Command injection' => '/(&)|(\|)|(\;)/'
];
private $logFile = 'security.log';
public function monitorRequest() {
// Monitor GET and POST separately since $_REQUEST might not be available
$this->checkArray($_GET, 'GET');
$this->checkArray($_POST, 'POST');
$this->checkServerVariables();
$this->logAccess();
echo "Security check completed.\n";
}
private function checkArray($array, $type) {
foreach ($array as $key => $value) {
$this->checkInput($key, $value, $type);
}
}
private function checkInput($key, $value, $type) {
foreach ($value as $subKey => $subValue) {
$this->checkInput($key . '[' . $subKey . ']', $subValue, $type);
}
return;
}
foreach ($this->suspiciousPatterns as $attack => $pattern) {
$this->logAttack($attack, "$type: $key", $value);
$this->blockRequest();
}
}
}
private function checkServerVariables() {
$importantes = ['HTTP_USER_AGENT', 'HTTP_REFERER', 'REQUEST_METHOD'];
foreach ($importantes as $header) {
if (isset($_SERVER[$header])) { $this->checkInput($header, $_SERVER[$header], 'SERVER');
}
}
}
private function logAttack($type, $input, $value) {
"[%s] Attack detected: %s, Input: %s, Value: %s\n",
$type,
$input,
$value
);
error_log($log); // Write to error log instead of file echo $log; // Also output to console for testing
}
private function logAccess() {
"[%s] Access logged - IP: %s, Method: %s, URI: %s\n",
$_SERVER['REMOTE_ADDR'] ?? 'unknown',
$_SERVER['REQUEST_METHOD'] ?? 'unknown',
$_SERVER['REQUEST_URI'] ?? 'unknown'
);
echo $log;
}
private function blockRequest() {
echo "Access Denied - Suspicious activity detected\n";
}
}
// Test the monitor
$monitor = new WebSecurityMonitor();
// Simulate some requests for testing
$_GET['test1'] = 'normal input';
$_GET['test2'] = "'; DROP TABLE users; --"; // SQL injection attempt
$_POST['test3'] = '<script>alert("xss")</script>'; // XSS attempt
$monitor->monitorRequest();
?>
PD9waHAKY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YWx1ZSwgJHR5cGUpOwogICAgICAgIH0KICAgIH0KICAgIAogICAgcHJpdmF0ZSBmdW5jdGlvbiBjaGVja0lucHV0KCRrZXksICR2YWx1ZSwgJHR5cGUpIHsKICAgICAgICBpZiAoaXNfYXJyYXkoJHZhbHVlKSkgewogICAgICAgICAgICBmb3JlYWNoICgkdmFsdWUgYXMgJHN1YktleSA9PiAkc3ViVmFsdWUpIHsKICAgICAgICAgICAgICAgICR0aGlzLT5jaGVja0lucHV0KCRrZXkgLiAnWycgLiAkc3ViS2V5IC4gJ10nLCAkc3ViVmFsdWUsICR0eXBlKTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIAogICAgICAgIGZvcmVhY2ggKCR0aGlzLT5zdXNwaWNpb3VzUGF0dGVybnMgYXMgJGF0dGFjayA9PiAkcGF0dGVybikgewogICAgICAgICAgICBpZiAocHJlZ19tYXRjaCgkcGF0dGVybiwgJHZhbHVlKSkgewogICAgICAgICAgICAgICAgJHRoaXMtPmxvZ0F0dGFjaygkYXR0YWNrLCAiJHR5cGU6ICRrZXkiLCAkdmFsdWUpOwogICAgICAgICAgICAgICAgJHRoaXMtPmJsb2NrUmVxdWVzdCgpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICBwcml2YXRlIGZ1bmN0aW9uIGNoZWNrU2VydmVyVmFyaWFibGVzKCkgewogICAgICAgICRpbXBvcnRhbnRlcyA9IFsnSFRUUF9VU0VSX0FHRU5UJywgJ0hUVFBfUkVGRVJFUicsICdSRVFVRVNUX01FVEhPRCddOwogICAgICAgIGZvcmVhY2ggKCRpbXBvcnRhbnRlcyBhcyAkaGVhZGVyKSB7CiAgICAgICAgICAgIGlmIChpc3NldCgkX1NFUlZFUlskaGVhZGVyXSkpIHsKICAgICAgICAgICAgICAgICR0aGlzLT5jaGVja0lucHV0KCRoZWFkZXIsICRfU0VSVkVSWyRoZWFkZXJdLCAnU0VSVkVSJyk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICAKICAgIHByaXZhdGUgZnVuY3Rpb24gbG9nQXR0YWNrKCR0eXBlLCAkaW5wdXQsICR2YWx1ZSkgewogICAgICAgICRsb2cgPSBzcHJpbnRmKAogICAgICAgICAgICAiWyVzXSBBdHRhY2sgZGV0ZWN0ZWQ6ICVzLCBJbnB1dDogJXMsIFZhbHVlOiAlc1xuIiwKICAgICAgICAgICAgZGF0ZSgnWS1tLWQgSDppOnMnKSwKICAgICAgICAgICAgJHR5cGUsCiAgICAgICAgICAgICRpbnB1dCwKICAgICAgICAgICAgJHZhbHVlCiAgICAgICAgKTsKICAgICAgICBlcnJvcl9sb2coJGxvZyk7ICAvLyBXcml0ZSB0byBlcnJvciBsb2cgaW5zdGVhZCBvZiBmaWxlCiAgICAgICAgZWNobyAkbG9nOyAgLy8gQWxzbyBvdXRwdXQgdG8gY29uc29sZSBmb3IgdGVzdGluZwogICAgfQogICAgCiAgICBwcml2YXRlIGZ1bmN0aW9uIGxvZ0FjY2VzcygpIHsKICAgICAgICAkbG9nID0gc3ByaW50ZigKICAgICAgICAgICAgIlslc10gQWNjZXNzIGxvZ2dlZCAtIElQOiAlcywgTWV0aG9kOiAlcywgVVJJOiAlc1xuIiwKICAgICAgICAgICAgZGF0ZSgnWS1tLWQgSDppOnMnKSwKICAgICAgICAgICAgJF9TRVJWRVJbJ1JFTU9URV9BRERSJ10gPz8gJ3Vua25vd24nLAogICAgICAgICAgICAkX1NFUlZFUlsnUkVRVUVTVF9NRVRIT0QnXSA/PyAndW5rbm93bicsCiAgICAgICAgICAgICRfU0VSVkVSWydSRVFVRVNUX1VSSSddID8/ICd1bmtub3duJwogICAgICAgICk7CiAgICAgICAgZXJyb3JfbG9nKCRsb2cpOwogICAgICAgIGVjaG8gJGxvZzsKICAgIH0KICAgIAogICAgcHJpdmF0ZSBmdW5jdGlvbiBibG9ja1JlcXVlc3QoKSB7CiAgICAgICAgZWNobyAiQWNjZXNzIERlb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