-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_password_reset.ts
132 lines (110 loc) · 3.16 KB
/
user_password_reset.ts
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
import dotenv from "dotenv";
import axios from "axios";
import readline from "readline";
import get from "lodash/get";
import Mailgun from "mailgun-js";
import { stripIndent } from "common-tags";
import { nanoid } from "nanoid";
dotenv.config();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const { CTFD_HOST, CTFD_SESSION, MAILGUN_API_KEY } = process.env;
const mailgun = Mailgun({ apiKey: MAILGUN_API_KEY!, domain: "tsg.ne.jp" });
(async () => {
const userId = await new Promise((resolve) => {
rl.question("Enter user id: ", resolve);
});
console.log("");
console.log(`User id: ${userId}`);
console.log("");
console.log("Getting User configuration...");
const emails: string[] = ["[email protected]"];
console.log("Getting CSRF token...");
const { data } = await axios.get(`${CTFD_HOST}/admin/notifications`, {
headers: {
Cookie: `session=${CTFD_SESSION}`,
},
});
const [, token] = data.match(/'csrfNonce'\s*:\s*"(.+?)"/);
console.log(`Got CSRF token: ${token}`);
/*
console.log('Getting team members...');
const {data: result} = await axios.get(`${CTFD_HOST}/api/v1/teams/${userId}/members`, {
headers: {
Cookie: `session=${CTFD_SESSION}`,
'CSRF-Token': token,
},
}); */
// const members = get(result, 'data', []);
// console.log('CTFd team members:', members);
/* for (const member of members) {
const {data: result} = await axios.get(`${CTFD_HOST}/api/v1/users/${member}`, {
headers: {
Cookie: `session=${CTFD_SESSION}`,
'CSRF-Token': token,
},
});
const email = get(result, ['data', 'email']);
emails.push(email);
} */
const { data: result } = await axios.get(
`${CTFD_HOST}/api/v1/users/${userId}`,
{
headers: {
Cookie: `session=${CTFD_SESSION}`,
"CSRF-Token": token,
},
}
);
const email = get(result, ["data", "email"]);
emails.push(email);
const userPassword = nanoid(16);
const { data: patchResult } = await axios.patch(
`${CTFD_HOST}/api/v1/users/${userId}`,
JSON.stringify({
password: userPassword,
}),
{
headers: {
Cookie: `session=${CTFD_SESSION}`,
"CSRF-Token": token,
"Content-Type": "application/json",
},
}
);
const userName = get(patchResult, ["data", "name"]);
console.log({ userName, userPassword: userPassword, emails });
await new Promise<void>((resolve) => {
rl.question("Is this ok? [yN] ", (answer) => {
if (answer.toLowerCase() === "y") {
rl.close();
resolve();
} else {
process.exit();
}
});
});
const content = stripIndent`
Hi ${userName} <br>
We reset your user password. The new password is ${userPassword} <br>
TSG
`;
const mailResult = await new Promise((resolve) => {
mailgun.messages().send(
{
from: "TSG CTF 2024 <[email protected]>",
to: "[email protected]",
bcc: emails,
subject: "TSG CTF 2024 user password reset",
text: content,
html: content,
},
(error, body) => {
resolve(body);
}
);
});
console.log(mailResult);
})();