This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
204 lines (174 loc) · 4.91 KB
/
index.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
const express = require("express");
const jwt = require("jsonwebtoken");
const crypto = require("crypto");
const PORT = 5504;
const FLAG = "flag{this_is_trial3_flag}";
const app = express();
app.use(express.json());
// シークレットキーを生成
const secret = crypto.randomBytes(16).toString("hex");
// 初期ユーザーを設定
const users = [
{
id: "admin",
username: "admin",
password: crypto.randomBytes(16).toString("hex"),
},
{
id: "guest",
username: "guest",
password: "guest"
}
]
// ユーザーのプロパティが存在するか確認する関数
function isUserPropertyExist(key, value) {
return users.some(user => user[key] === value);
}
app.get("/", (req, res) => {
res.send("Welcome to SECCON Beginners Live 2023 JWT Challenge!");
});
// ユーザー登録エンドポイント
app.post("/signup", (req, res) => {
const { id, username, password } = req.body;
if (!id || !username || !password) {
res.status(400).json({ error: "Please send id, username and password" });
return;
}
if (isUserPropertyExist('username', username)) {
res.status(400).json({ error: "Username already exists" });
return;
}
if (isUserPropertyExist('id', id)) {
res.status(400).json({ error: "ID already exists" });
return;
}
users.push({
id,
username,
password
});
res.json({ message: "User registered successfully" });
});
// サインインエンドポイント
app.post("/signin", (req, res) => {
const { id, password } = req.body;
if (!id || !password) {
res.status(400).json({ error: "Please send username and password" });
return;
}
let user = users.find(u => u.id === id && u.password === password);
if (!user) {
res.status(401).json({ error: "Invalid id or password" });
return;
}
let signed;
try {
signed = jwt.sign({
id: user.id,
username: user.username,
solved: false
},
secret,
{ algorithm: "HS256", expiresIn: "1h" }
);
} catch (err) {
res.status(500).json({ error: "Internal server error" });
return;
}
res.header("Authorization", signed);
res.json({ message: "ok" });
});
// ユーザー名更新エンドポイント
app.post("/username/update", (req, res) => {
const { newUsername } = req.body;
const token = req.header("Authorization");
if (!newUsername || !token) {
res.status(400).json({ error: "Please send newUsername and provide JWT token" });
return;
}
let verified;
try {
verified = jwt.verify(token, secret, { algorithms: ["HS256"] });
} catch (err) {
res.status(401).json({ error: "Invalid Token" });
return;
}
const currentId = verified.id;
let user = users.find(u => u.id === currentId);
if (!user) {
res.status(404).json({ error: "User not found" });
return;
}
if (user.username === "admin") {
res.status(403).json({ error: "Admin user cannot change its username" });
return;
}
user.username = newUsername;
res.json({ message: "Username updated successfully" });
});
// flag取得エンドポイント
app.get("/flag", (req, res) => {
if (!req.header("Authorization")) {
res.status(400).json({ error: "No JWT Token" });
return;
}
let verified;
try {
verified = jwt.verify(
req.header("Authorization"),
secret,
{ algorithms: ["HS256"] }
);
} catch (err) {
res.status(401).json({ error: "Invalid Token" });
return;
}
if (verified.username === "admin") {
let newToken;
try {
newToken = jwt.sign({
id: verified.id,
username: verified.username,
solved: true
},
secret,
{ algorithm: "HS256", expiresIn: "1h" }
);
} catch (err) {
res.status(500).json({ error: "Internal server error" });
return;
}
res.header("Authorization", newToken);
res.send(`Congratulations! Here's your flag: ${FLAG}`);
return;
}
res.send("No flag for you");
});
// 問題が解けているかどうかを確認するエンドポイント
app.get("/solved/check", (req, res) => {
if (!req.header("Authorization")) {
res.status(400).json({ error: "No JWT Token" });
return;
}
let verified;
try {
verified = jwt.verify(
req.header("Authorization"),
secret,
{ algorithms: ["HS256"] }
);
} catch (err) {
res.status(401).json({ error: "Invalid Token" });
return;
}
if (verified.solved) {
res.send(`Congratulations ${verified.id}, \nYou solved SECCON Beginners Live 2023 JWT Challenge!!`);
return;
} else {
res.status(403).json({ error: "Challenge not yet solved!" });
}
});
// サーバーの起動
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});