/
utils.js
290 lines (250 loc) · 8.67 KB
/
utils.js
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
286
287
288
289
290
const labels = [
'Base',
'Container',
'OU',
'GPO',
'User',
'Computer',
'Group',
'Domain',
'AZApp',
'AZDevice',
'AZGroup',
'AZKeyVault',
'AZResourceGroup',
'AZServicePrincipal',
'AZSubscription',
'AZTenant',
'AZUser',
'AZVM',
'AZRole',
'AZManagementGroup',
'AZFunctionApp',
'AZWebApp',
'AZLogicApp',
'AZAutomationAccount',
'AZContainerRegistry',
'AZManagedCluster',
'AZVMScaleSet'
];
export function generateUniqueId(sigmaInstance, isNode) {
let i = Math.floor(Math.random() * (100000 - 10 + 1)) + 10;
if (isNode) {
while (typeof sigmaInstance.graph.nodes(i) !== 'undefined') {
i = Math.floor(Math.random() * (100000 - 10 + 1)) + 10;
}
} else {
while (typeof sigmaInstance.graph.edges(i) !== 'undefined') {
i = Math.floor(Math.random() * (100000 - 10 + 1)) + 10;
}
}
return i;
}
function getRealLabel(label) {
let ret = null;
let comp = label.toLowerCase()
for (let l of labels){
if (comp === l.toLowerCase()){
ret = l;
break;
}
}
return ret;
}
export function buildSearchQuery(searchterm) {
if (searchterm.includes(':')) {
let [type, term] = searchterm.split(':');
type = getRealLabel(type);
let statement = `MATCH (n:${type}) WHERE n.name = $name OR n.azname=$name RETURN n LIMIT 10 UNION MATCH (n:${type}) WHERE n.name CONTAINS $name OR n.azname CONTAINS $name OR n.objectid CONTAINS $name RETURN n LIMIT 10`;
return [statement, term.toUpperCase()];
} else {
return [
'MATCH (n:Base) WHERE n.name = $name OR n.azname=$name RETURN n LIMIT 10 UNION MATCH (n) WHERE n.name CONTAINS $name OR n.azname CONTAINS $name OR n.objectid CONTAINS $name RETURN n LIMIT 10',
searchterm.toUpperCase(),
];
}
}
export function buildSelectQuery(startNode, endNode) {
let apart = `MATCH (n:${startNode.type} {objectid: $sourceid})`;
let bpart = `MATCH (m:${endNode.type} {objectid: $targetid})`;
let query = `${apart} ${bpart} MATCH p=allShortestPaths((n)-[r:{}*1..]->(m)) RETURN p`;
return [
query,
{ sourceid: startNode.objectid, targetid: endNode.objectid },
startNode.name || startNode.objectid,
endNode.name || endNode.objectid,
];
}
//Recursive function to highlight paths to start/end nodes
export function findGraphPath(sigmaInstance, reverse, nodeid, traversed) {
let target = reverse ? appStore.startNode : appStore.endNode;
traversed.push(nodeid);
//This is our stop condition for recursing
if (nodeid !== target.id) {
let edges = sigmaInstance.graph.adjacentEdges(nodeid);
let nodes = reverse
? sigmaInstance.graph.inboundNodes(nodeid)
: sigmaInstance.graph.outboundNodes(nodeid);
//Loop over the nodes near us and the edges connecting to those nodes
$.each(nodes, function (index, node) {
$.each(edges, function (index, edge) {
let check = reverse ? edge.source : edge.target;
//If an edge is pointing in the right direction, set its color
//Push the edge into our store and then
node = parseInt(node);
if (check === node && !traversed.includes(node)) {
edge.color = reverse ? 'blue' : 'red';
appStore.highlightedEdges.push(edge);
findGraphPath(sigmaInstance, reverse, node, traversed);
}
});
});
} else {
return;
}
}
export async function clearSessions() {
emitter.emit('openClearingModal');
await deleteSessions();
}
async function deleteSessions() {
let session = driver.session();
let results = await session.run('MATCH ()-[r:HasSession]-() WITH r LIMIT 100000 DELETE r RETURN count(r)')
let count = results.records[0].get(0)
if (count === 0){
emitter.emit('hideDBClearModal')
}else{
await deleteSessions()
}
}
export async function clearDatabase() {
emitter.emit('openClearingModal');
await deleteDb();
}
async function deleteDb() {
let session = driver.session()
let results = await session.run('MATCH (n) CALL { WITH n DETACH DELETE n} IN TRANSACTIONS OF 500 ROWS')
console.log(results.summary.counters)
await dropConstraints()
}
//
// async function deleteEdges() {
// let session = driver.session();
// let results = await session.run('MATCH ()-[r]-() WITH r LIMIT 100000 DELETE r RETURN count(r)')
// emitter.emit('refreshDBData')
// let count = results.records[0].get(0)
// await session.close()
//
// if (count === 0){
// await deleteNodes()
// }else{
// await deleteEdges()
// }
//
// }
//
// async function deleteNodes() {
// let session = driver.session();
// let results = await session.run('MATCH (n) WITH n LIMIT 100000 DELETE n RETURN count(n)')
// emitter.emit('refreshDBData')
// let count = results.records[0].get(0)
// await session.close()
//
// if (count === 0){
// await dropConstraints()
// }else{
// await deleteNodes()
// }
// }
async function dropConstraints() {
let session = driver.session();
let constraints = [];
let result = await session.run('CALL db.constraints')
for (let record of result.records){
let constraint = record.get(0)
let query = 'DROP CONSTRAINT ' + constraint;
constraints.push(query)
}
for (let constraintQuery of constraints){
await session.run(constraintQuery)
}
await session.close()
await dropIndexes()
}
async function dropIndexes() {
let session = driver.session();
let indexes = [];
let result = await session.run('CALL db.constraints')
for (let record of result.records){
let constraint = record.get(0)
let query = 'DROP INDEX ' + constraint;
indexes.push(query)
}
for (let indexQuery of indexes){
await session.run(indexQuery)
}
await session.close()
await setSchema()
}
export async function setSchema() {
const luceneIndexProvider = "lucene+native-3.0"
let labels = ["User", "Group", "Computer", "GPO", "OU", "Domain", "Container", "Base", "AZBase", "AZApp", "AZDevice", "AZGroup", "AZKeyVault", "AZResourceGroup", "AZServicePrincipal", "AZTenant", "AZUser", "AZVM", "AZFunctionApp", "AZLogicApp", "AZAutomationAccount", "AZContainerRegistry", "AZWebApp", "AZManagedCluster", "AZVMScaleSet"]
let azLabels = ["AZBase", "AZApp", "AZDevice", "AZGroup", "AZKeyVault", "AZResourceGroup", "AZServicePrincipal", "AZTenant", "AZUser", "AZVM", "AZFunctionApp", "AZLogicApp", "AZAutomationAccount", "AZContainerRegistry", "AZWebApp", "AZManagedCluster", "AZVMScaleSet"]
let schema = {}
for (let label of labels){
schema[label] = {
name: label,
indexes: [{
name: "{}_{}_index".format(label.toLowerCase(), "name"),
provider: luceneIndexProvider,
property: "name"
}],
constraints: [{
name: "{}_{}_constraint".format(label.toLowerCase(), "objectid"),
provider: luceneIndexProvider,
property: "objectid"
}],
}
}
for (let label of azLabels) {
schema[label]["indexes"].push({
name: "{}_{}_index".format(label.toLowerCase(), "azname"),
provider: luceneIndexProvider,
property: "azname"
})
}
let session = driver.session();
for (let label of labels){
for (let constraint of schema[label].constraints){
let props = {
name: constraint.name,
label: [label],
properties: [constraint.property],
provider: constraint.provider
}
try{
await session.run("CALL db.createUniquePropertyConstraint($name, $label, $properties, $provider)", props)
}catch (e) {
//console.error(e)
}
}
for (let index of schema[label].indexes) {
let props = {
name: index.name,
label: [label],
properties: [index.property],
provider: index.provider
}
try{
await session.run("CALL db.createIndex($name, $label, $properties, $provider)", props)
}catch (e) {
//console.error(e)
}
}
}
await session.close();
emitter.emit('hideDBClearModal');
}
export function escapeRegExp(str) {
return str.replace(/[\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}