Add File and Discord logging

This commit is contained in:
Elijah 2023-12-08 20:02:44 -05:00
parent 1ca65fe831
commit d8dbc351bb
5 changed files with 46 additions and 11 deletions

View file

@ -4,6 +4,8 @@
"MaxFileSize": 104857600, "MaxFileSize": 104857600,
"BlockedMD5": [], "BlockedMD5": [],
"RateLimit": 10, "RateLimit": 10,
"LogDir": "/var/log/agent",
"DiscordWebhook": "https://discordapp.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz",
"VMs": [ "VMs": [
{ {
"ID": "vm1", "ID": "vm1",

View file

@ -17,6 +17,7 @@
"dependencies": { "dependencies": {
"@types/md5": "^2.3.5", "@types/md5": "^2.3.5",
"async-mutex": "^0.4.0", "async-mutex": "^0.4.0",
"discord.js": "^14.14.1",
"fastify": "^4.24.3", "fastify": "^4.24.3",
"md5": "^2.3.0", "md5": "^2.3.0",
"msgpack-lite": "^0.1.26" "msgpack-lite": "^0.1.26"

View file

@ -4,6 +4,8 @@ export default interface IConfig {
MaxFileSize : number; MaxFileSize : number;
BlockedMD5: string[]; BlockedMD5: string[];
RateLimit : number; RateLimit : number;
LogDir? : string;
DiscordWebhook? : string;
VMs : { VMs : {
ID : string; ID : string;
SocketPath : string; SocketPath : string;

View file

@ -27,20 +27,23 @@ export default class VM {
FileData: data FileData: data
}; };
var payload = msgpack.encode(msg); var payload = msgpack.encode(msg);
var header = Buffer.alloc(4); await this.#sendMessage(payload);
header.writeUInt32LE(payload.length);
// Make sure we dont write two messages at the same time
var release = await this.#writeLock.acquire();
await this.#writeData(header);
// shit gets fucked if i dont do this
await sleep(100);
await this.#writeData(payload);
// All done
release();
res(); res();
}); });
} }
#sendMessage(data : Buffer) : Promise<void> {
return new Promise(async (res, rej) => {
var header = Buffer.alloc(4);
header.writeUInt32LE(data.length);
await this.#writeLock.runExclusive(async () => {
await this.#writeData(header);
await this.#writeData(data);
res();
});
});
}
#writeData(data : Buffer) : Promise<void> { #writeData(data : Buffer) : Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.#socket.write(data, (err) => { this.#socket.write(data, (err) => {

View file

@ -5,6 +5,8 @@ import VM from './VM.js';
import md5 from 'md5'; import md5 from 'md5';
import log from './log.js'; import log from './log.js';
import RateLimit from './Ratelimit.js'; import RateLimit from './Ratelimit.js';
import * as fs from 'fs';
import { EmbedBuilder, WebhookClient } from 'discord.js';
log("INFO", "CollabVM Agent Server Starting up..."); log("INFO", "CollabVM Agent Server Starting up...");
// Load the config file // Load the config file
@ -17,6 +19,13 @@ try {
process.exit(1); process.exit(1);
} }
var discordenabled : boolean = false;
var discord : WebhookClient | null = null;
if (config.DiscordWebhook) {
discordenabled = true;
discord = new WebhookClient({ url: config.DiscordWebhook });
}
var VMs = new Map<string, VM>(); var VMs = new Map<string, VM>();
config.VMs.forEach((v) => { config.VMs.forEach((v) => {
VMs.set(v.ID, new VM(v.SocketPath)); VMs.set(v.ID, new VM(v.SocketPath));
@ -88,6 +97,14 @@ app.put("/:vm/:filename", async (req, res) => {
} }
await VMs.get(vm)!.UploadFile(filename, filedata); await VMs.get(vm)!.UploadFile(filename, filedata);
log("INFO", `${vm}: ${req.ip} uploaded "${filename}" with MD5 ${hash}`); log("INFO", `${vm}: ${req.ip} uploaded "${filename}" with MD5 ${hash}`);
if (config.LogDir)
await appendFile(`${config.LogDir}/${vm}.log`, `${req.ip} uploaded "${filename}" with MD5 ${hash}\n`);
if (discordenabled) {
var embed = new EmbedBuilder()
.addFields({"name": "VM", "value": vm, "inline": true}, {"name": "Filename", "value": filename, "inline": true}, {"name": "IP", "value": req.ip, "inline": true}, {"name": "MD5", "value": hash, "inline": true})
.setTitle("File Uploaded");
discord!.send({ embeds: [embed] });
}
return { success: true, result: "File uploaded" }; return { success: true, result: "File uploaded" };
}); });
@ -95,4 +112,14 @@ log("INFO", "Starting HTTP server on port " + config.ListenPort);
app.listen({ app.listen({
port: config.ListenPort, port: config.ListenPort,
host: "127.0.0.1" host: "127.0.0.1"
}); });
function appendFile(path : string, data : string | Uint8Array) {
return new Promise<void>((res, rej) => {
fs.appendFile(path, data, (err) => {
if (err) {
rej(err);
} else res();
});
});
}