2022-07-29 02:02:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import json
|
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
2022-07-29 02:02:23 +00:00
|
|
|
|
|
|
|
from common.db.utils import close_old_connections
|
|
|
|
from common.utils import get_logger
|
2023-09-06 02:30:55 +00:00
|
|
|
from .tools import (
|
|
|
|
verbose_ping, verbose_telnet, verbose_nmap,
|
|
|
|
verbose_tcpdump, verbose_traceroute
|
|
|
|
)
|
2023-08-07 01:37:24 +00:00
|
|
|
|
2022-07-29 02:02:23 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
class ToolsWebsocket(AsyncJsonWebsocketConsumer):
|
2022-07-29 02:02:23 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def connect(self):
|
2022-07-29 02:02:23 +00:00
|
|
|
user = self.scope["user"]
|
|
|
|
if user.is_authenticated:
|
2023-08-07 01:37:24 +00:00
|
|
|
await self.accept()
|
2022-07-29 02:02:23 +00:00
|
|
|
else:
|
2023-08-07 01:37:24 +00:00
|
|
|
await self.close()
|
2022-07-29 02:02:23 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def send_msg(self, msg=''):
|
2023-08-29 09:02:51 +00:00
|
|
|
await self.send_json({'msg': f'{msg}\r\n'})
|
2023-02-17 11:38:34 +00:00
|
|
|
|
2023-08-29 09:02:51 +00:00
|
|
|
async def imitate_ping(self, dest_ips, timeout=3, count=5, psize=64):
|
2023-08-07 01:37:24 +00:00
|
|
|
params = {
|
2023-08-29 09:02:51 +00:00
|
|
|
'dest_ips': dest_ips, 'timeout': timeout,
|
2023-08-07 01:37:24 +00:00
|
|
|
'count': count, 'psize': psize
|
|
|
|
}
|
|
|
|
logger.info(f'Receive request ping: {params}')
|
|
|
|
await verbose_ping(display=self.send_msg, **params)
|
2022-07-29 02:02:23 +00:00
|
|
|
|
2023-08-29 09:02:51 +00:00
|
|
|
async def imitate_telnet(self, dest_ips, dest_port=23, timeout=10):
|
2023-08-07 01:37:24 +00:00
|
|
|
params = {
|
2023-08-29 09:02:51 +00:00
|
|
|
'dest_ips': dest_ips, 'dest_port': dest_port, 'timeout': timeout,
|
2023-08-07 01:37:24 +00:00
|
|
|
}
|
|
|
|
logger.info(f'Receive request telnet: {params}')
|
|
|
|
await verbose_telnet(display=self.send_msg, **params)
|
2023-07-28 02:40:48 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def imitate_nmap(self, dest_ips, dest_ports=None, timeout=None):
|
|
|
|
params = {
|
|
|
|
'dest_ips': dest_ips, 'dest_ports': dest_ports, 'timeout': timeout,
|
|
|
|
}
|
|
|
|
logger.info(f'Receive request nmap: {params}')
|
|
|
|
await verbose_nmap(display=self.send_msg, **params)
|
2022-07-29 02:02:23 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def imitate_tcpdump(
|
|
|
|
self, interfaces=None, src_ips='',
|
|
|
|
src_ports='', dest_ips='', dest_ports=''
|
|
|
|
):
|
|
|
|
params = {
|
|
|
|
'interfaces': interfaces, 'src_ips': src_ips, 'src_ports': src_ports,
|
|
|
|
'dest_ips': dest_ips, 'dest_ports': dest_ports
|
|
|
|
}
|
|
|
|
logger.info(f'Receive request tcpdump: {params}')
|
|
|
|
await verbose_tcpdump(display=self.send_msg, **params)
|
|
|
|
|
2023-09-06 02:30:55 +00:00
|
|
|
async def imitate_traceroute(self,dest_ips):
|
|
|
|
params = {'dest_ips': dest_ips}
|
|
|
|
await verbose_traceroute(display=self.send_msg, **params)
|
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def receive(self, text_data=None, bytes_data=None, **kwargs):
|
2022-07-29 02:02:23 +00:00
|
|
|
data = json.loads(text_data)
|
2023-07-28 02:40:48 +00:00
|
|
|
tool_type = data.pop('tool_type', 'Ping')
|
2023-08-07 01:37:24 +00:00
|
|
|
try:
|
|
|
|
tool_func = getattr(self, f'imitate_{tool_type.lower()}')
|
|
|
|
await tool_func(**data)
|
|
|
|
except Exception as error:
|
|
|
|
await self.send_msg('Exception: %s' % error)
|
|
|
|
await self.send_msg()
|
|
|
|
await self.close()
|
2023-07-28 02:40:48 +00:00
|
|
|
|
2023-08-07 01:37:24 +00:00
|
|
|
async def disconnect(self, code):
|
|
|
|
await self.close()
|
2022-07-29 02:02:23 +00:00
|
|
|
close_old_connections()
|