2021-09-10 09:54:34 +00:00
|
|
|
|
from typing import Callable
|
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
|
from users.models import User
|
|
|
|
|
from common.utils import get_logger, reverse
|
|
|
|
|
from notifications.notifications import SystemMessage
|
|
|
|
|
from terminal.models import Session, Command
|
|
|
|
|
from notifications.models import SystemMsgSubscription
|
2021-06-16 06:38:17 +00:00
|
|
|
|
from notifications.backends import BACKEND
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
__all__ = ('CommandAlertMessage', 'CommandExecutionAlert')
|
|
|
|
|
|
|
|
|
|
CATEGORY = 'terminal'
|
2021-06-16 05:20:50 +00:00
|
|
|
|
CATEGORY_LABEL = _('Sessions')
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandAlertMixin:
|
2021-09-10 09:54:34 +00:00
|
|
|
|
command: dict
|
|
|
|
|
_get_message: Callable
|
|
|
|
|
message_type_label: str
|
|
|
|
|
|
2021-06-16 07:13:42 +00:00
|
|
|
|
def get_dingtalk_msg(self) -> str:
|
|
|
|
|
msg = self._get_message()
|
|
|
|
|
msg = msg.replace('<br>', '')
|
|
|
|
|
return msg
|
|
|
|
|
|
2021-09-10 09:54:34 +00:00
|
|
|
|
def get_subject(self):
|
|
|
|
|
_input = self.command['input']
|
|
|
|
|
if isinstance(_input, str):
|
|
|
|
|
_input = _input.replace('\r\n', ' ').replace('\r', ' ').replace('\n', ' ')
|
|
|
|
|
|
|
|
|
|
subject = self.message_type_label + "%(cmd)s" % {
|
|
|
|
|
'cmd': _input
|
|
|
|
|
}
|
|
|
|
|
return subject
|
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def post_insert_to_db(cls, subscription: SystemMsgSubscription):
|
|
|
|
|
"""
|
2021-09-10 09:54:34 +00:00
|
|
|
|
兼容操作,试图用 `settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER` 的邮件地址
|
|
|
|
|
assets_systemuser_assets找到用户,把用户设置为默认接收者
|
2021-05-31 09:20:38 +00:00
|
|
|
|
"""
|
2021-06-16 06:38:17 +00:00
|
|
|
|
from settings.models import Setting
|
2021-09-10 09:54:34 +00:00
|
|
|
|
db_setting = Setting.objects.filter(
|
|
|
|
|
name='SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER'
|
|
|
|
|
).first()
|
2021-06-16 06:38:17 +00:00
|
|
|
|
if db_setting:
|
|
|
|
|
emails = db_setting.value
|
2021-06-18 02:05:44 +00:00
|
|
|
|
else:
|
|
|
|
|
emails = settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER
|
2021-06-16 06:38:17 +00:00
|
|
|
|
emails = emails.split(',')
|
|
|
|
|
emails = [email.strip().strip('"') for email in emails]
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
users = User.objects.filter(email__in=emails)
|
2021-06-18 02:05:44 +00:00
|
|
|
|
if users:
|
|
|
|
|
subscription.users.add(*users)
|
|
|
|
|
subscription.receive_backends = [BACKEND.EMAIL]
|
|
|
|
|
subscription.save()
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandAlertMessage(CommandAlertMixin, SystemMessage):
|
|
|
|
|
category = CATEGORY
|
|
|
|
|
category_label = CATEGORY_LABEL
|
2021-06-16 05:20:50 +00:00
|
|
|
|
message_type_label = _('Danger command alert')
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
def __init__(self, command):
|
|
|
|
|
self.command = command
|
|
|
|
|
|
|
|
|
|
def _get_message(self):
|
|
|
|
|
command = self.command
|
2021-09-10 09:54:34 +00:00
|
|
|
|
session = Session.objects.get(id=command['session'])
|
|
|
|
|
session_detail_url = reverse(
|
|
|
|
|
'api-terminal:session-detail', kwargs={'pk': command['session']},
|
|
|
|
|
external=True, api_to_ui=True
|
|
|
|
|
)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
message = _("""
|
2021-09-10 09:54:34 +00:00
|
|
|
|
Command: %(command)s
|
|
|
|
|
<br>
|
|
|
|
|
Asset: %(hostname)s (%(host_ip)s)
|
|
|
|
|
<br>
|
|
|
|
|
User: %(user)s
|
|
|
|
|
<br>
|
|
|
|
|
Level: %(risk_level)s
|
|
|
|
|
<br>
|
|
|
|
|
Session: <a href="%(session_detail_url)s?oid=%(oid)s">session detail</a>
|
|
|
|
|
<br>
|
|
|
|
|
""") % {
|
2021-05-31 09:20:38 +00:00
|
|
|
|
'command': command['input'],
|
2021-09-10 09:54:34 +00:00
|
|
|
|
'hostname': command['asset'],
|
|
|
|
|
'host_ip': session.asset_obj.ip,
|
2021-05-31 09:20:38 +00:00
|
|
|
|
'user': command['user'],
|
|
|
|
|
'risk_level': Command.get_risk_level_str(command['risk_level']),
|
2021-09-10 09:54:34 +00:00
|
|
|
|
'session_detail_url': session_detail_url,
|
|
|
|
|
'oid': session.org_id
|
2021-05-31 09:20:38 +00:00
|
|
|
|
}
|
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
|
def get_common_msg(self):
|
2021-08-24 06:20:54 +00:00
|
|
|
|
msg = self._get_message()
|
2021-09-10 09:54:34 +00:00
|
|
|
|
subject = self.get_subject()
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'subject': subject,
|
2021-09-10 09:54:34 +00:00
|
|
|
|
'message': msg
|
2021-05-31 09:20:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandExecutionAlert(CommandAlertMixin, SystemMessage):
|
|
|
|
|
category = CATEGORY
|
|
|
|
|
category_label = CATEGORY_LABEL
|
2021-06-16 05:20:50 +00:00
|
|
|
|
message_type_label = _('Batch danger command alert')
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
def __init__(self, command):
|
|
|
|
|
self.command = command
|
|
|
|
|
|
|
|
|
|
def _get_message(self):
|
|
|
|
|
command = self.command
|
2021-09-10 09:54:34 +00:00
|
|
|
|
_input = command['input']
|
|
|
|
|
_input = _input.replace('\n', '<br>')
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
assets = ', '.join([str(asset) for asset in command['assets']])
|
|
|
|
|
message = _("""
|
|
|
|
|
Assets: %(assets)s
|
|
|
|
|
<br>
|
|
|
|
|
User: %(user)s
|
|
|
|
|
<br>
|
|
|
|
|
Level: %(risk_level)s
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
----------------- Commands ---------------- <br>
|
|
|
|
|
%(command)s <br>
|
|
|
|
|
----------------- Commands ---------------- <br>
|
|
|
|
|
""") % {
|
2021-09-10 09:54:34 +00:00
|
|
|
|
'command': _input,
|
2021-05-31 09:20:38 +00:00
|
|
|
|
'assets': assets,
|
|
|
|
|
'user': command['user'],
|
2021-09-10 09:54:34 +00:00
|
|
|
|
'risk_level': Command.get_risk_level_str(command['risk_level'])
|
2021-05-31 09:20:38 +00:00
|
|
|
|
}
|
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
|
def get_common_msg(self):
|
2021-09-10 09:54:34 +00:00
|
|
|
|
subject = self.get_subject()
|
2021-06-16 03:07:06 +00:00
|
|
|
|
message = self._get_message()
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'message': message
|
|
|
|
|
}
|