-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam_password_reset.ts
113 lines (91 loc) · 2.66 KB
/
team_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
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 teamId = await new Promise((resolve) => {
rl.question('Enter team id: ', resolve);
});
console.log('');
console.log(`Team id: ${teamId}`);
console.log('');
console.log('Getting Team 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/${teamId}/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 teamPassword = nanoid(16);
const { data: patchResult } = await axios.patch(`${CTFD_HOST}/api/v1/teams/${teamId}`, JSON.stringify({
password: teamPassword,
}), {
headers: {
Cookie: `session=${CTFD_SESSION}`,
'CSRF-Token': token,
'Content-Type': 'application/json',
},
});
const teamName = get(patchResult, ['data', 'name']);
console.log({ teamName, teamPassword, 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 team ${teamName} <br>
We reset your team password. The new password is ${teamPassword} <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 team password reset',
text: content,
html: content,
}, (error, body) => {
resolve(body);
});
});
console.log(mailResult);
})();