· last year · Feb 06, 2025, 07:30 PM
1const express = require("express");
2const cookieParser = require("cookie-parser");
3const bodyParser = require("body-parser");
4
5const app = express();
6const PORT = 3000;
7
8// Store user data (for simplicity, not using a database)
9let users = {
10 admin: { password: "admin1234", isAdmin: false },
11 user: { password: "user1234", isAdmin: false }
12};
13
14const ADMIN_FLAG = "CTF{admin_flag_here}";
15
16app.use(express.static("public")); // Serve static files (your HTML page)
17app.use(cookieParser());
18app.use(bodyParser.urlencoded({ extended: true }));
19app.use(bodyParser.json());
20
21// Middleware to log incoming requests
22app.use((req, res, next) => {
23 console.log(`Incoming request: ${req.method} ${req.url} from ${req.ip}`);
24 next();
25});
26
27// // Set initial cookies
28// app.get("/init", (req, res) => {
29
30// });
31
32// Handle forgot password
33app.post("/reset-password", (req, res) => {
34 const { username, email, newPassword } = req.body;
35
36 if (username === "admin" && newPassword && !email) {
37 users.admin.password = newPassword;
38 res.cookie("admin", "true", { httpOnly: true });
39 res.json({ message: "Admin password reset successfully!" });
40 } else if (users[username] && email && newPassword) {
41 users[username].password = newPassword;
42 res.cookie("user", "true", { httpOnly: true });
43 res.json({ message: "User password reset successfully!" });
44 } else {
45 res.status(400).json({ error: "Invalid request!" });
46 }
47});
48
49// Handle login
50app.post("/login", (req, res) => {
51 const { username, password } = req.body;
52 const isAdmin = req.cookies.admin === "true";
53
54 if (username === "admin" && password === users.admin.password && isAdmin) {
55 res.json({ success: true, message: "Logged in as Admin!", flag: ADMIN_FLAG });
56 } else if (username === "user" && password === users.user.password) {
57 res.json({ success: true, message: "Logged in as User! No flag for you!" });
58 } else {
59 res.status(401).json({ error: "Invalid credentials or admin access not enabled!" });
60 }
61});
62
63const path = require("path");
64
65// Serve index.html when accessing the root route
66app.get("/", (req, res) => {
67 res.sendFile(path.join(__dirname, "public", "index.html"));
68 res.cookie("admin", "false", { httpOnly: true });
69 res.send("Cookies initialized.");
70
71});
72
73// Start server and log when a connection is made
74const server = app.listen(PORT, () => {
75 console.log(`Server running on http://localhost:${PORT}`);
76 console.log("Waiting for client connections...");
77});
78
79server.on("connection", (socket) => {
80 console.log(`New client connected from ${socket.remoteAddress}`);
81});
82