2017-11-29 11:27:04 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
import datetime
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
from django.db import transaction
|
|
|
|
from django.db.utils import OperationalError
|
2024-02-26 02:50:44 +00:00
|
|
|
from django.utils import timezone
|
2017-11-29 11:27:04 +00:00
|
|
|
|
2024-02-26 02:50:44 +00:00
|
|
|
from common.utils.common import pretty_string
|
2017-11-29 11:27:04 +00:00
|
|
|
from .base import CommandBase
|
|
|
|
|
|
|
|
|
|
|
|
class CommandStore(CommandBase):
|
|
|
|
|
2018-01-20 14:22:09 +00:00
|
|
|
def __init__(self, params):
|
2017-12-01 09:28:47 +00:00
|
|
|
from terminal.models import Command
|
|
|
|
self.model = Command
|
2017-11-29 11:27:04 +00:00
|
|
|
|
|
|
|
def save(self, command):
|
|
|
|
"""
|
|
|
|
保存命令到数据库
|
|
|
|
"""
|
2024-02-26 02:50:44 +00:00
|
|
|
cmd_input = pretty_string(command['input'])
|
2017-11-29 11:27:04 +00:00
|
|
|
self.model.objects.create(
|
|
|
|
user=command["user"], asset=command["asset"],
|
2024-02-26 02:50:44 +00:00
|
|
|
account=command["account"], input=cmd_input,
|
2017-11-29 11:27:04 +00:00
|
|
|
output=command["output"], session=command["session"],
|
2020-03-12 08:24:38 +00:00
|
|
|
risk_level=command.get("risk_level", 0), org_id=command["org_id"],
|
|
|
|
timestamp=command["timestamp"]
|
2017-11-29 11:27:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def bulk_save(self, commands):
|
|
|
|
"""
|
|
|
|
批量保存命令到数据库, command的顺序和save中一致
|
|
|
|
"""
|
|
|
|
_commands = []
|
|
|
|
for c in commands:
|
2022-03-30 11:07:49 +00:00
|
|
|
cmd_input = pretty_string(c['input'])
|
|
|
|
cmd_output = pretty_string(c['output'], max_length=1024)
|
2017-11-29 11:27:04 +00:00
|
|
|
_commands.append(self.model(
|
2022-12-05 05:27:51 +00:00
|
|
|
user=c["user"], asset=c["asset"], account=c["account"],
|
2022-03-30 11:07:49 +00:00
|
|
|
input=cmd_input, output=cmd_output, session=c["session"],
|
2020-03-12 08:24:38 +00:00
|
|
|
risk_level=c.get("risk_level", 0), org_id=c["org_id"],
|
|
|
|
timestamp=c["timestamp"]
|
2017-11-29 11:27:04 +00:00
|
|
|
))
|
2019-01-15 02:23:30 +00:00
|
|
|
error = False
|
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
|
|
|
self.model.objects.bulk_create(_commands)
|
|
|
|
except OperationalError:
|
|
|
|
error = True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not error:
|
|
|
|
return True
|
|
|
|
for command in _commands:
|
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
|
|
|
command.save()
|
|
|
|
except OperationalError:
|
|
|
|
command.output = str(command.output.encode())
|
|
|
|
command.save()
|
|
|
|
return True
|
2017-11-29 11:27:04 +00:00
|
|
|
|
2018-01-20 14:22:09 +00:00
|
|
|
@staticmethod
|
|
|
|
def make_filter_kwargs(
|
|
|
|
date_from=None, date_to=None,
|
2022-12-05 05:27:51 +00:00
|
|
|
user=None, asset=None, account=None,
|
2020-03-12 08:24:38 +00:00
|
|
|
input=None, session=None, risk_level=None, org_id=None):
|
2017-11-29 11:27:04 +00:00
|
|
|
filter_kwargs = {}
|
2017-12-03 16:21:26 +00:00
|
|
|
date_from_default = timezone.now() - datetime.timedelta(days=7)
|
|
|
|
date_to_default = timezone.now()
|
|
|
|
|
2019-03-18 03:34:13 +00:00
|
|
|
if not date_from and not session:
|
|
|
|
date_from = date_from_default
|
|
|
|
if not date_to and not session:
|
|
|
|
date_to = date_to_default
|
|
|
|
if date_from is not None:
|
2019-07-03 08:29:39 +00:00
|
|
|
if isinstance(date_from, datetime.datetime):
|
|
|
|
date_from = date_from.timestamp()
|
|
|
|
filter_kwargs['timestamp__gte'] = int(date_from)
|
2019-03-18 03:34:13 +00:00
|
|
|
if date_to is not None:
|
2019-07-03 08:29:39 +00:00
|
|
|
if isinstance(date_to, datetime.datetime):
|
|
|
|
date_to = date_to.timestamp()
|
|
|
|
filter_kwargs['timestamp__lte'] = int(date_to)
|
2017-11-29 11:27:04 +00:00
|
|
|
|
|
|
|
if user:
|
2019-07-04 08:28:42 +00:00
|
|
|
filter_kwargs["user__startswith"] = user
|
2017-11-29 11:27:04 +00:00
|
|
|
if asset:
|
|
|
|
filter_kwargs['asset'] = asset
|
2022-12-05 05:27:51 +00:00
|
|
|
if account:
|
|
|
|
filter_kwargs['account'] = account
|
2017-12-03 16:21:26 +00:00
|
|
|
if input:
|
|
|
|
filter_kwargs['input__icontains'] = input
|
2017-11-29 11:27:04 +00:00
|
|
|
if session:
|
|
|
|
filter_kwargs['session'] = session
|
2020-03-12 08:24:38 +00:00
|
|
|
if org_id is not None:
|
|
|
|
filter_kwargs['org_id'] = org_id
|
|
|
|
if risk_level is not None:
|
|
|
|
filter_kwargs['risk_level'] = risk_level
|
2018-01-20 14:22:09 +00:00
|
|
|
return filter_kwargs
|
|
|
|
|
|
|
|
def filter(self, date_from=None, date_to=None,
|
2022-12-05 05:27:51 +00:00
|
|
|
user=None, asset=None, account=None,
|
2020-03-12 08:24:38 +00:00
|
|
|
input=None, session=None, risk_level=None, org_id=None):
|
2018-01-20 14:22:09 +00:00
|
|
|
filter_kwargs = self.make_filter_kwargs(
|
|
|
|
date_from=date_from, date_to=date_to, user=user,
|
2022-12-05 05:27:51 +00:00
|
|
|
asset=asset, account=account, input=input,
|
2020-03-12 08:24:38 +00:00
|
|
|
session=session, risk_level=risk_level, org_id=org_id,
|
2018-01-20 14:22:09 +00:00
|
|
|
)
|
2017-11-29 11:27:04 +00:00
|
|
|
queryset = self.model.objects.filter(**filter_kwargs)
|
2018-03-28 07:47:20 +00:00
|
|
|
return queryset
|
2017-11-29 11:27:04 +00:00
|
|
|
|
2018-01-20 14:22:09 +00:00
|
|
|
def count(self, date_from=None, date_to=None,
|
2022-12-05 05:27:51 +00:00
|
|
|
user=None, asset=None, account=None,
|
2018-03-28 07:47:20 +00:00
|
|
|
input=None, session=None):
|
2018-01-20 14:22:09 +00:00
|
|
|
filter_kwargs = self.make_filter_kwargs(
|
|
|
|
date_from=date_from, date_to=date_to, user=user,
|
2022-12-05 05:27:51 +00:00
|
|
|
asset=asset, account=account, input=input,
|
2018-01-20 14:22:09 +00:00
|
|
|
session=session,
|
|
|
|
)
|
|
|
|
count = self.model.objects.filter(**filter_kwargs).count()
|
|
|
|
return count
|
|
|
|
|
2017-11-29 11:27:04 +00:00
|
|
|
|