-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (43 loc) · 1.4 KB
/
app.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
const express = require("express");
const { execFile } = require("child_process");
const path = require("path");
const PORT = process.env.PORT || 3000;
const app = express();
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: false }));
app.get("/", (req, res) => {
res.render("index", {
filepath: null,
stdout: null,
error: null
});
});
app.post("/execute", (req, res) => {
let filepath = req.body.filepath;
// Simple Validation/Sanitization
// This is a basic example, adjust the regex according to your filepath rules
if (!/^[a-zA-Z0-9_\-./]+$/.test(filepath)) {
return res.render("index", {
filepath: null,
stdout: null,
error: "Invalid filepath entered"
});
}
// Normalize and restrict the filepath to a specific directory
filepath = path.normalize(`/home/kali/acme-ftp-server/${filepath}`).replace(/^(\.\.(\/|\\|$))+/, '');
execFile('ls', [filepath], (err, stdout, stderr) => {
let error = null;
stdout = stdout.split("\n").filter(n => n.length > 1);
if (err || stderr) {
error = "Something went wrong, please try again later";
}
res.render("index", {
filepath,
stdout,
error
});
});
});
app.listen(PORT, () => {
console.log(`Express is listening on port:${PORT}`);
});