-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCommunicator.py
101 lines (91 loc) · 3.82 KB
/
Communicator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import datetime
from flask import request
import db_controller as db
import JobController as jc
import MatterController as mc
def get_timestamp():
return datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
def cmd_i_dont_know(user,cmd):
attachment = {
"mrkdwn_in": ["text"],
"text": f"@{user} If you have any questions, please type `{cmd} help`."
}
return attachment
def cmd_howto(user,cmd):
attachment = {
"mrkdwn_in": ["text"],
"color": "#ffa500",
"text": f"@{user} If you want me to use {cmd}, please enter it in the following format." + "\n \
***Be careful to separate the commands with a space.***\n\
`nmap [Command Type] [Target IP Address]`\n\n\
### Command Type",
}
sql = """SELECT cmd_name,description FROM t_command_list WHERE cmd_type = %s;"""
cmdList = db.get_AllValues(sql, cmd)
attachfield = []
for selectCmd in cmdList:
attachfield.append({"title": selectCmd[0],"value": selectCmd[1],"short": "false"},)
attachment["fields"] = attachfield
return attachment
def ChatCommunication():
posted_user = request.json['user_name']
posted_msg = request.json['text']
strArry = posted_msg.split(" ")
# strArry[0] : WakeUp Code
# strArry[1] : Command
# strArry[2] : Argument
# strArry[3] : Target
command = strArry[1]
if command == "hello" or command == "Hello":
attachment = {"mrkdwn_in": ["text"], "text": f"Hi, @{posted_user} ! :wave:" }
elif command == "hi" or command == "Hi":
attachment = {"mrkdwn_in": ["text"], "text": f"Hello, @{posted_user} ! :wave:" }
# nmap or nikto
elif command == "nmap" or command == "nikto":
# gather all nmap commands
sql = """SELECT cmd_name,value FROM t_command_list WHERE cmd_type = %s;"""
cmdList = db.get_AllValues(sql, command)
cmdNames = []
for selectCmd in cmdList:
cmdName = selectCmd[0]
cmdNames.append(cmdName)
if len (strArry) == 3:
args = strArry[2]
if args == "help":
attachment = cmd_howto(posted_user,command)
else:
attachment = cmd_i_dont_know(posted_user,command)
elif len(strArry) == 4:
cmdType = strArry[2]
target = strArry[3]
for selectCmd in cmdList:
cmdName, value = selectCmd
if cmdName == cmdType:
filename = cmdName + "_" + target + "_" + get_timestamp()
if command == "nmap":
cmdValue = value + " " + target + " -oA tmp/" + filename + " && python xmlparser.py tmp/" + filename +".xml"
elif command == "nikto":
cmdValue = value + target + " -o tmp/" + filename + ".xml | tee tmp/" + filename + ".txt && python xmlparser.py tmp/" + filename + ".xml"
attachment = {
"mrkdwn_in": ["text"],
"fallback": "I'll run a " + cmdName + " to " + target,
"title": "Your order",
"pretext": "I'll run a " + cmdName + " to " + target,
"text": cmdValue
}
jc.Set_myjob(cmdValue, posted_user)
break
if cmdValue == "":
attachment = cmd_howto(posted_user,command)
else:
attachment = cmd_i_dont_know(posted_user,command)
# other or help
else:
pretext = "I don't konw :man_shrugging:"
attachment = {
"markdwn_in": ["text","pretext"],
"pretext" : pretext,
"color": "#ff0000",
"text": "If you have any questions, please type `help`. :thumbsup:"
}
return mc.botbot_information(attachment)