-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
285 lines (242 loc) · 8.61 KB
/
monitor.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import requests
import json
from flask import Flask, request,jsonify
import sys
import time
import hmac
import hashlib
import base64
import urllib.parse
import uuid
import threading
import urllib3
urllib3.disable_warnings()
# VER
VERSION = "v0.2.0_alpha"
## DINGDING_WEBHOOK = "https://oapi.dingtalk.com/robot/send?access_token=XXXXXXXXXXXXXXXXXXXXXXXX"
DING_WEBHOOK = ""
## DINGDING_SECRET = "SECXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
SECRET = ''
# SERVICE CONFIG
HOST = "127.0.0.1"
PORT = 14500
SERVICE_URL = "http://{}:{}/".format(HOST, PORT)
# CONST
DELAY_TIME = 2 * 60 * 60 # hour * minute * second
STANDARD_MSG = "## {0}\n\n \n\n**Path**: {1}\n\n**Description**: {2}"
class Shell():
def __init__(self, name: str, path: str, desc: str) -> None:
self.uuid = Utils.gen_uuid()
self.name = name
self.path = path
self.desc = desc
self.status = False
def to_dict(self) -> dict:
shell= {
"uuid": self.uuid,
"name": self.name,
"path": self.path,
"desc": self.desc,
"status": self.status
}
return shell
class Utils():
@staticmethod
def gen_uuid():
return str(uuid.uuid4())
@staticmethod
def sign_timestamp():
timestamp = str(round(time.time() * 1000))
secret_enc = SECRET.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, SECRET)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
return (sign, timestamp)
@staticmethod
def send_msg(msg: str):
body = {
"msgtype": "markdown",
"markdown": {
"title": "",
"text": msg
}
}
(sign, timestamp) = Utils.sign_timestamp()
webhook = DING_WEBHOOK + "&sign=" + sign + "×tamp=" + timestamp
resp = requests.post(url=webhook, json=body, verify=False)
# print(resp.text)
class Server():
def __init__(self):
self.shell_list: list[Shell] = []
self.app = Flask(__name__)
self.delay = DELAY_TIME
def service(self):
def alive_msg(webshell: Shell, isOnline: bool):
if isOnline:
title = "🟢{0} is Online🟢".format(webshell.name)
else:
title = "🔴{0} is Offline🔴".format(webshell.name)
msg = STANDARD_MSG.format(title, webshell.path, webshell.desc)
Utils.send_msg(msg)
@self.app.route("/add", methods = ['POST'])
def add_shell():
shell = request.get_json()
shell_obj = Shell(shell["name"], shell["path"], shell["desc"])
self.shell_list.append(shell_obj)
title = "✅{0} Add✅".format(shell_obj.name)
msg = STANDARD_MSG.format(title, shell_obj.path, shell_obj.desc)
Utils.send_msg(msg=msg)
return 'success', 200
@self.app.route("/list", methods=['GET', 'POST'])
def list_shell():
webshell_list = []
for shell in self.shell_list:
webshell_list.append(shell.to_dict())
return jsonify(self.shell_list), 200
@self.app.route("/del", methods=['POST'])
def del_shell():
j = request.get_json()
for i in range(len(self.shell_list)):
if self.shell_list[i].uuid == j["uuid"]:
title = "❌{0} Delete❌".format(self.shell_list[i].name)
msg = STANDARD_MSG.format(title, self.shell_list[i].path, self.shell_list[i].desc)
Utils.send_msg(msg=msg)
del self.shell_list[i]
return 'success', 200
@self.app.route("/load", methods=['POST'])
def load_shell():
shells = request.get_json()
for s in shells:
webshell = Shell(s["name"], s["path"], s["desc"])
self.shell_list.append(webshell)
return 'success', 200
@self.app.route("/delay", methods=["POST"])
def delay():
delay_time = request.get_json()
self.delay = delay_time['secs']
return 'success', 200
@self.app.before_first_request
def check_alive():
def run():
while(True):
print("[*] {} webshells are on monitor.".format(len(self.shell_list)))
if len(self.shell_list) == 0:
time.sleep(10)
else:
for i in range(len(self.shell_list)):
try:
resp = requests.get(self.shell_list[i].path, verify=False)
if resp.status_code == 200:
flag = True
else:
flag = False
except Exception:
flag = False
if self.shell_list[i].status ^ flag:
alive_msg(self.shell_list[i], flag)
self.shell_list[i].status = flag
time.sleep(self.delay)
thread = threading.Thread(target=run)
thread.start()
self.app.run(HOST, PORT)
class Client():
@staticmethod
def list_shell():
resp = requests.get(SERVICE_URL + "list", verify=False)
if resp.status_code == 200:
print(json.dumps(json.loads(resp.text), indent=4))
print("[+] Success")
@staticmethod
def add_shell(name: str = "", path: str = "", desc: str = ""):
if name == "" or path == "" or desc == "":
name = input("[*] Name: ")
path = input("[*] Path: ")
desc = input("[*] Desc: ")
body = {
"name": name,
"path": path,
"desc": desc,
}
resp = requests.post(SERVICE_URL + "add", json=body, verify=False)
if resp.status_code == 200:
print("[+] Success")
@staticmethod
def del_shell(uuid: str = ""):
if uuid == "":
uuid = input("[*] UUID: ")
body = {
"uuid": uuid
}
resp = requests.post(SERVICE_URL + "del", json=body, verify=False)
if resp.status_code == 200:
print("[+] Success")
@staticmethod
def load_shell(config: str):
with open(config, "r") as f:
config_json = f.read()
webshell_list = json.loads(config_json)
resp = requests.post(SERVICE_URL + "load", json=webshell_list, verify=False)
if resp.status_code == 200:
print("[+] Success")
@staticmethod
def delay(sec: int):
body = {
"secs": sec
}
resp = requests.post(SERVICE_URL + "delay", json=body, verify=False)
if resp.status_code == 200:
print("[+] Success" )
def help():
print("""
_____________________
< Oh!webshell Online! >
---------------------
\ ^__^
\ (oo)\_______ @Author: Arm!tage
(__)\ )\/\ @Version: {ver}
||----w |
|| ||
USAGE:
Run Service
python3 {script} server
Use Client
python3 {script} list
python3 {script} add [<name> <path> <description>]
python3 {script} del [uuid]
pyhton3 {script} load <config file>
python3 {script} delay <seconds>
Export
curl -k http://{host}:{port}/list
""".format(script=sys.argv[0], host=HOST, port=PORT, ver=VERSION))
exit()
def main():
cmd = sys.argv[1]
if cmd == "-h" or cmd == "--help":
help()
if cmd == "server":
print("[+] Start Monitor Service")
Server().service()
if cmd == "list":
print("[+] List Webshell")
Client.list_shell()
if cmd == "add":
print("[+] Add Webshell")
try:
Client.add_shell(sys.argv[2], sys.argv[3], sys.argv[4])
except Exception:
Client.add_shell()
if cmd == "del":
print("[+] Delete Webshell")
try:
Client.del_shell(sys.argv[2])
except Exception:
Client.del_shell()
if cmd == "load":
print("[+] Load Webshell file")
config_file = sys.argv[2]
Client.load_shell(config_file)
if cmd == "delay":
print("[+] Change delay seconds")
if __name__ == "__main__":
main()