-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
379 lines (334 loc) · 13.4 KB
/
server.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
const express = require('express');
const Redis = require('ioredis');
const cors = require('cors');
const path = require('path');
//const Queue = require('bull');
const { log } = require('console');
const MongoClient = require('mongodb').MongoClient;
// Make sure your mongodb client is correctly initialized!
const mongoURI = process.env.MONGODB_URI || 'mongodb://localhost:27017'; // Replace with your MongoDB connection string
const dbName = process.env.MONGODB_DB_NAME || 'patterns'; // Replace with your database name
const mongoClient = new MongoClient(mongoURI);
let db;
mongoClient.connect()
.then(() => {
db = mongoClient.db(dbName);
mongoConnected = true; // Set the flag to true on successful connection
console.log('Connected to MongoDB');
})
.catch(err => {
console.error('Failed to connect to MongoDB:', err);
console.error('Continuing without MongoDB support.');
});
const app = express();
app.use(express.json());
const port = process.env.PORT || 3000; // Default to 3000 if not set
const redisHost = process.env.REDIS_HOST || '127.0.0.1';
const redisPort = process.env.REDIS_PORT || 6379; // Default Redis port
const redisKeyPrefix = process.env.REDIS_KEY_PREFIX || 'tetrastix';
const REDIS_PATTERNKEY = process.env.REDIS_PATTERNKEY || 'tetra_pattern';
const RAW_LOGS_KEY = process.env.RAW_LOGS_KEY || 'raw_logs';
const TETRA = process.env.TETRA || 'tetra';
const ACTIVE_QUEUE_KEY = process.env.ACTIVE_LOGS_KEY|| 'active_logs';
const BENIGN_LOGS_KEY = 'benign_logs';
const LIGHTENINGROD = process.env.LIGHTENINGROD || 'http://localhost:8000';
const redisClient = new Redis({ host: redisHost, port: redisPort });
//confirm redis connection
redisClient.on('connect', () => {
console.log('Connected to Redis');
});
redisClient.on('error', (err) => {
console.error('Redis error:', err);
});
app.use(cors());
app.use(express.static(path.join(__dirname)));
app.get('/health', async (req, res) => {
try {
if (redisClient.status === 'ready' ){
//&& db.serverConfig.isConnected()) {
res.status(200).send("OK");
} else {
res.status(503).send("Not Ready");
}
} catch (err) {
res.status(500).send("Error");
}
});
app.get('/reload-tetra', async (req, res) => {
try {
const rawLogs = await redisClient.lrange(TETRA, 0, -1);
await redisClient.del(RAW_LOGS_KEY);
if (rawLogs.length > 0) {
await redisClient.rpush(RAW_LOGS_KEY, ...rawLogs);
console.log("Reloaded raw logs from Tetra");
}
res.json({ message: "Raw logs reloaded" }); // Send a JSON response
} catch (err) {
console.error("Error reloading raw logs:", err);
res.status(500).send("Error reloading raw logs"); // Send error response
}
});
app.get('/raw-logs', async (req, res) => {
try {
const page = parseInt(req.query.page) || 1;
const perPage = parseInt(req.query.perPage) || 5;
const startIndex = (page - 1) * perPage;
const endIndex = startIndex + perPage - 1;
const rawLogs = await redisClient.lrange(RAW_LOGS_KEY, startIndex, endIndex);
const totalLogs = await redisClient.llen(RAW_LOGS_KEY);
res.json({
logs: rawLogs,
page: page,
perPage: perPage,
total: totalLogs
});
} catch (err) {
console.error("Error fetching raw logs:", err);
res.status(500).send("Error fetching raw logs");
}
});
app.get('/active-logs-count', async (req, res) => {
try {
const count = await redisClient.llen(ACTIVE_QUEUE_KEY); //Gets the total number of jobs waiting in the active logs queue
res.json({ total: count });
} catch (err) {
console.error("Error getting active logs count:", err);
res.status(500).json({ error: "Error getting active logs count" });
}
});
app.get('/active-logs', async (req, res) => {
try {
const page = parseInt(req.query.page) || 1;
const perPage = parseInt(req.query.perPage) || 5;
const startIndex = (page - 1) * perPage;
const logs = await redisClient.lrange(ACTIVE_QUEUE_KEY, startIndex, startIndex + perPage - 1);
const totalLogs = await redisClient.llen(ACTIVE_QUEUE_KEY);
res.json({
logs: logs,
page,
perPage,
total: totalLogs
});
} catch (err) {
console.error("Error fetching active logs:", err);
res.status(500).json({ error: "Error fetching active logs" });
}
//}
});
app.get('/baseline-init-all', async (req, res) => {
try {
const rawLogs = await redisClient.lrange(TETRA, 0, -1); // tetra is a list
const pipeline = redisClient.pipeline();
for (const log of rawLogs) {
try {
const md5 = JSON.parse(log).md5_hash;
console.log("md5", md5);
if (md5) {
if (!await redisClient.hexists(BENIGN_LOGS_KEY, md5)){
pipeline.hset(BENIGN_LOGS_KEY, md5, Date.now()); }
}
} catch (err) { console.error("Error in baseline-init-all pipeline hset:", err); }
}
await pipeline.exec();
console.log("Baseline initialized with all logs");
res.json({ message: 'Baseline initialized with all logs' });
} catch (err) {
console.error("Error initializing baseline with all logs:", err);
res.status(500).send("Error initializing baseline with all logs");
}
});
app.get('/add-all-logs', async (req, res) => {
try {
const rawLogs = await redisClient.lrange(RAW_LOGS_KEY, 0, -1);
const md5s = await redisClient.hkeys(BENIGN_LOGS_KEY);
const filteredLogs = rawLogs.filter(log => !md5s.includes(JSON.parse(log).md5_hash));
if (filteredLogs.length > 0) {
// Use pipeline for efficiency
const pipeline = redisClient.pipeline();
filteredLogs.forEach(log => pipeline.rpush(ACTIVE_QUEUE_KEY, log));
await pipeline.exec();
console.log(`${filteredLogs.length} logs added to the queue.`);
return res.json({ message: 'Logs added' });
} else {
console.log("No new logs to add.");
return res.json({ message: 'No new logs to add' });
}
} catch (err) {
console.error("Error queuing logs:", err);
return res.status(500).send("Error queuing logs");
}
});
app.get('/rm-all-logs', async (req, res) => {
try {
await redisClient.del(ACTIVE_QUEUE_KEY);
return res.json({ message: 'All jobs removes'});
}
catch (err) {
console.error("Error removing all jobs:", err);
return res.status(500).send("Error removing all jobs");
}
});
app.get('/stix-transform', async (req, res) => {
try {
q = req.query.queue.toString().trim();
request = await fetch(`${LIGHTENINGROD}/convert_list_to_stix?queue=${q}`);
}
catch (err) {
console.error("Error transforming logs:", err);
return res.status(500).send("Error transforming logs");
}
try {
request = await fetch(`${LIGHTENINGROD}/bundle_for_viz`);
console.log("Logs bundled");
}
catch (err) {
console.error("Error bundleing logs:", err);
return res.status(500).send("Error bundeling logs");
}
res.json({ message: 'Logs transformed'});
});
app.get('/add-log', async (req, res) => {
if (req.query.id) {
try {
const logToAdd = req.query.id;
await redisClient.rpush(ACTIVE_QUEUE_KEY, logToAdd); // Directly add the log (string)
console.log("Log Added");
return res.json({ message: 'Log added' });
} catch (err) {
console.error("Error queuing log:", err);
return res.status(500).send("Error queuing log");
}
} else {
return res.status(400).send("Missing data");
}
});
app.get('/rem-log', async (req, res) => {
try {
const logToRemove = req.query.id;
//lrem removes all instances of that log, that is why we can simply call it with 1
const removedCount = await redisClient.lrem(ACTIVE_QUEUE_KEY, 1, logToRemove);
if (removedCount > 0) {
res.json({ message: 'Log removed'});
console.log("Log Removed. Count:", removedCount); // removedCount for debug
} else {
res.status(404).json({ error: 'Log not found in queue' }); // Handle not found
}
} catch (err) {
console.error("Error removing log:", err);
res.status(500).send("Error removing log");
}
});
app.get('/redis-keys', async (req, res) => {
try {
const tableName = req.query.table || redisKeyPrefix;
const keys = await redisClient.hkeys(tableName);
console.log("Fetching Redis keys:", keys);
res.json(keys);
} catch (err) {
console.error("Error fetching Redis keys:", err);
res.status(500).send("Error fetching Redis keys");
}
});
app.get('/stix-bundle/:key', async (req, res) => {
const key= req.params.key;
const tableName = req.query.table || redisKeyPrefix;
try {
console.log("Fetching STIX bundle with key:", key);
const data = await redisClient.hget(tableName, key);
res.json(data);
} catch (err) {
console.error("Error fetching STIX bundle:", err);
res.status(500).send("Error fetching STIX bundle");
}
});
app.get('/attack-bundles', async (req, res) => {
try {
const keys = await redisClient.hgetall(REDIS_PATTERNKEY);
const formattedBundles = Object.entries(keys).map(([key, value]) => ({
id: key,
data: JSON.parse(value) // Important to parse the JSON string!
}));
res.json(formattedBundles);
//console.log("Fetching Redis Attack Bundle:", res.status());
} catch (err) {
console.error("Error fetching Redis attack bundles:", err);
res.status(500).send("Error fetching Redis attack bundles");
}
});
app.get('/attack-bundle-max', async (req, res) => {
try {
//retrieve the highest ID from Redis
const keys = await redisClient.hgetall(REDIS_PATTERNKEY);
if (Object.keys(keys).length === 0) { // Handle empty hash
return res.json(0); // Or whatever default value you want
}
const maxId = Math.max(...Object.keys(keys).map(Number)); // Correct way to find max
res.json(maxId);
console.log("Fetching highest ID:", maxId);
} catch (err) {
console.error("Error fetching highest ID:", err);
res.status(500).send("Error fetching highest ID");
}
});
app.post('/add-attack-bundles', async (req, res) => {
try {
const newBundle = req.body;
// TODO: add check for exact identity to avoid duplicates
const nextBundleId = newBundle.data.id; // Assuming integer IDs
await redisClient.hset(REDIS_PATTERNKEY, nextBundleId, JSON.stringify(newBundle.data));
res.json({ message: 'Attack bundle added', id: nextBundleId });
} catch (err) {
console.error("Error adding attack bundle:", err);
res.status(500).send("Error adding attack bundle");
}
});
app.post('/modify-attack-bundles', async (req, res) => {
try {
if (!req ) { // Check if data exists
return res.status(400).send("Missing request body"); // Or handle it differently
}
const newBundle = req.body;
await redisClient.hset(REDIS_PATTERNKEY, newBundle.id, JSON.stringify(newBundle.data));
res.json({ message: 'Attack bundle overwritten', id: newBundle.id });
} catch (err) {
console.error("Error overwriting attack bundle:", err);
res.status(500).send("Error overwriting attack bundle");
}
});
app.post('/delete-attack-bundles', async (req, res) => {
try {
const newBundle = req.body;
await redisClient.hdel(REDIS_PATTERNKEY, newBundle.data.id);
res.json({ message: 'Attack bundle deleted', id: newBundle.data.id });
} catch (err) {
console.error("Error deleting attack bundle:", err);
res.status(500).send("Error deleting attack bundle");
}
});
app.post('/persist-to-mongodb', async (req, res) => {
try {
if (!db) { // Check if connected to the database
throw new Error('Not connected to MongoDB');
}
const patternData = await redisClient.hgetall(REDIS_PATTERNKEY);
const collection = db.collection('attack_patterns'); // Replace 'stix_patterns' with your collection name
const operations = Object.entries(patternData).map(([key, value]) => ({
updateOne: { // Use updateOne operation for upsert
filter: { id: key }, // Filter by id
update: { $set: { id: key, pattern: JSON.parse(value) } }, // Update or insert this document
upsert: true
}
}));
const result = await collection.bulkWrite(operations);
console.log("MongoDB Upsert Result:", result);
// Clear Redis after successful persistence (optional) - If you clear, update the frontend to re-fetch
// await redisClient.del(REDIS_PATTERNKEY);
res.json({ message: 'Data persisted to MongoDB' });
} catch (err) {
console.error("Error persisting to MongoDB:", err);
res.status(500).send("Error persisting to MongoDB");
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});