[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: mod_sec_blocker.py
File is not writable. Editing disabled.
"""Generate alert on modsec incident if it triggers limits.""" import logging import time from collections import deque from typing import Any, Optional from defence360agent.contracts.plugins import ( MessageSink, MessageSource, expect, ) from im360.contracts.config import ( ModsecBlockByCustomRules as CustomRulesConfig, ) from im360.contracts.config import ModsecBlockBySeverity as SeverityConfig from im360.contracts.config import ModsecSensor from defence360agent.contracts.messages import MessageType logger = logging.getLogger(__name__) class ModSecBlockBySeverity(MessageSink, MessageSource): async def create_sink(self, loop): self._loop = loop self.events = {} async def create_source(self, loop, sink): self._sink = sink def _is_over_limit(self, keys: Any, limit: int, period: int) -> bool: """Returns True if rate of events grouped by `key` is higher than `limit` for last `period` seconds, False otherwise.""" if keys not in self.events: self.events[keys] = deque(maxlen=limit) q = self.events[keys] q.append(time.monotonic()) # Dictionary, where keys are something and values are # double-ended queues which contain a time of occurred events. if len(q) == limit: interval = q[-1] - q[0] # Queue always contains one less than limit number # of event time and create an alert when the difference between # the time of the new event and the first event in a queue # is less than said `period` if interval <= period: del self.events[keys] return True else: # Remove the first item in a queue otherwise q.popleft() return False @expect(MessageType.SensorIncident, plugin_id=ModsecSensor.PLUGIN_ID) async def check_incident(self, message): reason = self._process_incident(message) if reason: logger.info("Creating alert from %s with reason: %s", message, reason) await self._sink.process_message( MessageType.SensorAlert.from_incident(message) ) def _process_incident( self, message: MessageType.SensorIncident ) -> Optional[str]: """Process an incident, update limits and return a string describing a reason to create alert from message. If no alert should be created returns None.""" ip = message.get('attackers_ip') rule = str(message.get('rule')) severity = message.get('severity') access_denied = message.get('access_denied') if access_denied and SeverityConfig.ENABLED: if self._is_over_limit((ip, access_denied), SeverityConfig.DENIED_NUM_LIMIT, SeverityConfig.CHECK_PERIOD): return 'multiple access denied triggered' elif (CustomRulesConfig.RULES and rule in CustomRulesConfig.RULES.keys()): if self._is_over_limit((ip, rule), CustomRulesConfig.get_limit(rule), CustomRulesConfig.get_timeout(rule)): return 'custom rule {} triggered'.format(rule) elif (SeverityConfig.ENABLED and severity and severity <= SeverityConfig.SEVERITY_LIMIT): if self._is_over_limit((ip, rule), SeverityConfig.MAX_REPETITION, SeverityConfig.CHECK_PERIOD): return 'severity rule {} triggered'.format(rule)
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: server05.hostinghome.co.in
Server IP: 192.168.74.40
PHP Version: 7.4.33
Server Software: Apache
System: Linux server05.hostinghome.co.in 3.10.0-962.3.2.lve1.5.81.el7.x86_64 #1 SMP Wed May 31 10:36:47 UTC 2023 x86_64
HDD Total: 1.95 TB
HDD Free: 691.58 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Disabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes
gcc:
Yes
pkexec:
No
git:
Yes
User Info
Username: itsweb
User ID (UID): 1619
Group ID (GID): 1621
Script Owner UID: 1619
Current Dir Owner: N/A