-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.py
225 lines (188 loc) · 6.78 KB
/
routes.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
import os
from sentinelbackend.utils import convertforWindows, getcountry, fetchScanResults, getSuspectFiles, getCompany
from flask import request, jsonify
from sentinelbackend import app
from sentinelbackend.virustotal import lookup_process, adv_scan, quickScan, scanIp as virusTotalIPScan
from sentinelbackend.models import addToBlacklist, removeFromBlacklist, getRules, getScheduledFiles, removeFileFromScheduled, getbadIphealth
import psutil
from os.path import expanduser
from sentinelbackend.models import getbadIphealth
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/getProcesses', methods=['GET', 'POST'])
def getprocesses():
if request.method == 'POST' and os.name == 'nt':
pids = psutil.pids()
result = list(map(convertforWindows, pids))
return jsonify(
{
"processes" : list(filter(lambda x: len(x['remoteAddr']), list(filter(lambda x : x != None, result))))
}
)
elif request.method == 'POST':
processes = psutil.net_connections()
result = list(map(convert, processes))
return jsonify(
{
"processes": list(filter(lambda x: len(x['remoteAddr']), result))
}
)
@app.route('/getSystemUsage', methods=['POST'])
def getsystemUsage():
# TODO something wrong here
n_c = tuple(psutil.disk_io_counters())
n_b = tuple(psutil.net_io_counters())
print(n_c, n_b)
return jsonify(
{
"num_process": str(len(list(psutil.net_connections()))),
"cpu_usage": str(psutil.cpu_percent(interval=None, percpu=False)),
"memory_usage": str(dict(psutil.virtual_memory()._asdict())["percent"]),
"disk_io_percent": [(100.0*n_c[i+1]) / (n_c[i] if n_c[i] != 0 else 1) for i in range(0, len(n_c)-1, 2)],
"network_io_percent": [(100.0*n_b[i+1]) / (n_b[i] if n_b[i] != 0 else 1) for i in range(0, len(n_b)-1, 2)]
}
)
@app.route('/getProcessUsage', methods=['POST'])
def getProcessUsageStats():
# TODO implement full function
if request.method == 'POST':
pid = int(request.form.get('PID'))
process = psutil.Process(pid=pid)
return jsonify(
{
"cpu_uasage": process.cpu_percent(interval=2),
"memory_usage": str(int(process.memory_info().rss) / ( 1024 * 1024 ))+ " MB" ,
# "disk_io_percent": [(100.0 * n_c[i + 1]) / (n_c[i] if n_c[i] != 0 else 1) for i in range(0, len(n_c) - 1, 2)],
# "network_io_percent": ""
}
)
@app.route('/lookupProcess', methods=['POST'])
def quickscan():
if request.method == 'POST':
return jsonify(
{
"results": lookup_process(request.form.get('PID'))
}
)
@app.route('/blockIP', methods=['POST'])
def block_ip():
if request.method == 'POST':
response = addToBlacklist(request.form.get('IP'), request.form.get('port') if request.form.get('port') != None else "*")
return response
@app.route('/unblockIP', methods=['POST'])
def unblock_ip():
if request.method == 'POST':
response = removeFromBlacklist(request.form.get('IP'), request.form.get('port') if request.form.get('port') != None else "*")
return response
@app.route('/getRules', methods=['POST'])
def get_rules():
return jsonify(
{
"rules": list(getRules())
}
)
@app.route('/advancedScan', methods=['POST'])
def advanced_scan():
return jsonify(adv_scan(request.form.get('filepath')))
@app.route('/getScheduledFiles', methods=['POST'])
def getS():
return jsonify(
{
"files": getScheduledFiles()
}
)
@app.route('/removeFromScheduledFilesList', methods=['POST'])
def removeFromList():
removeFileFromScheduled(request.form.get('filepath'))
return "removed from list"
@app.route('/deleteFile', methods=['POST'])
def deleteme():
try:
os.remove(expanduser(request.form.get('filepath')))
return "deleted"
except:
return "file not found"
@app.route('/scanIP', methods=['POST'])
def scanIP():
return jsonify({
"results": virusTotalIPScan(request.form.get('IP'))
})
@app.route('/getReport', methods=['POST'])
def quick_scan():
return jsonify(quickScan(request.form.get('filepath')))
@app.route('/killProcess', methods=['POST'])
def killProcess():
if request.method == 'POST':
try:
pid = int(request.form.get('PID'))
process = psutil.Process(pid)
process.kill()
return "process terminated"
except:
return "some error occured. Are you sure you have sudo priviledge"
@app.route('/getchkrScanResults', methods=['POST'])
def chkscan():
if request.method == 'POST':
return jsonify({
"results": fetchScanResults("~/chkrootkitLogs/fileLog.txt")
})
@app.route('/chkrScan', methods=['POST'])
def scan():
if request.method == 'POST':
os.system(expanduser("~/chkrootkit2 -q"))
return "Scan Complete"
@app.route('/getSuspectFiles', methods=['POST'])
def getf():
if request.method == 'POST':
ans = []
for e in getSuspectFiles(' '):
if isinstance(e, list):
for i in e:
ans.append(i)
else:
ans.append(e)
return jsonify(
{
"files": ans
})
@app.route('/getConnectedCountries', methods=['POST'])
def countries():
if request.method == 'POST':
processes = psutil.net_connections()
s = {}
result = list(map(convert, processes))
for item in result:
if dict(item)["country"] == "" or dict(item)["country"] == "local address":
continue
s[
dict(item)["country"]] = len(list(filter(lambda x: x["country"] == item["country"], result)))
return jsonify(
{
"results": s
}
)
def convert(process):
country = ''
company = ''
try:
if process.raddr and process.raddr.ip == '127.0.0.1':
country = company = "local address"
elif process.raddr:
country = getcountry(process.raddr.ip)
company = getCompany(process.raddr.ip)
except:
country = "could not trace in current database"
return {
# TODO return correct connection type/protocol also
'localAddr': process.laddr,
'remoteAddr': process.raddr,
'PID': str(process.pid),
'status': process.status,
'country': country,
"Pname": psutil.Process(process.pid).name(),
"User": psutil.Process(process.pid).username(),
"cType": "tcp",
'company': company,
"health": getbadIphealth(process.raddr.ip if process.raddr else 0)
}