forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.js
345 lines (318 loc) · 10.5 KB
/
verify.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
/*
* Copyright (c) 2014-2020 Bjoern Kimminich.
* SPDX-License-Identifier: MIT
*/
const utils = require('../lib/utils')
const insecurity = require('../lib/insecurity')
const jwt = require('jsonwebtoken')
const jws = require('jws')
const models = require('../models/index')
const cache = require('../data/datacache')
const Op = models.Sequelize.Op
const challenges = cache.challenges
const products = cache.products
const config = require('config')
exports.forgedFeedbackChallenge = () => (req, res, next) => {
utils.solveIf(challenges.forgedFeedbackChallenge, () => {
const user = insecurity.authenticatedUsers.from(req)
const userId = user && user.data ? user.data.id : undefined
return req.body && req.body.UserId && req.body.UserId != userId // eslint-disable-line eqeqeq
})
next()
}
exports.captchaBypassChallenge = () => (req, res, next) => {
if (utils.notSolved(challenges.captchaBypassChallenge)) {
if (req.app.locals.captchaReqId >= 10) {
if ((new Date().getTime() - req.app.locals.captchaBypassReqTimes[req.app.locals.captchaReqId - 10]) <= 10000) {
utils.solve(challenges.captchaBypassChallenge)
}
}
req.app.locals.captchaBypassReqTimes[req.app.locals.captchaReqId - 1] = new Date().getTime()
req.app.locals.captchaReqId++
}
next()
}
exports.registerAdminChallenge = () => (req, res, next) => {
utils.solveIf(challenges.registerAdminChallenge, () => { return req.body && req.body.role === insecurity.roles.admin })
next()
}
exports.passwordRepeatChallenge = () => (req, res, next) => {
utils.solveIf(challenges.passwordRepeatChallenge, () => { return req.body && req.body.passwordRepeat !== req.body.password })
next()
}
exports.accessControlChallenges = () => ({ url }, res, next) => {
utils.solveIf(challenges.scoreBoardChallenge, () => { return utils.endsWith(url, '/1px.png') })
utils.solveIf(challenges.adminSectionChallenge, () => { return utils.endsWith(url, '/19px.png') })
utils.solveIf(challenges.tokenSaleChallenge, () => { return utils.endsWith(url, '/56px.png') })
utils.solveIf(challenges.privacyPolicyChallenge, () => { return utils.endsWith(url, '/81px.png') })
utils.solveIf(challenges.extraLanguageChallenge, () => { return utils.endsWith(url, '/tlh_AA.json') })
utils.solveIf(challenges.retrieveBlueprintChallenge, () => { return utils.endsWith(url, cache.retrieveBlueprintChallengeFile) })
utils.solveIf(challenges.securityPolicyChallenge, () => { return utils.endsWith(url, '/security.txt') })
utils.solveIf(challenges.missingEncodingChallenge, () => { return utils.endsWith(url, '/%F0%9F%98%BC-%23zatschi-%23whoneedsfourlegs-1572600969477.jpg') })
utils.solveIf(challenges.accessLogDisclosureChallenge, () => { return url.match(/access\.log(0-9-)*/) })
next()
}
exports.errorHandlingChallenge = () => (err, req, { statusCode }, next) => {
utils.solveIf(challenges.errorHandlingChallenge, () => { return err && (statusCode === 200 || statusCode > 401) })
next(err)
}
exports.jwtChallenges = () => (req, res, next) => {
if (utils.notSolved(challenges.jwtUnsignedChallenge)) {
jwtChallenge(challenges.jwtUnsignedChallenge, req, 'none', /jwtn3d@/)
}
if (!utils.disableOnWindowsEnv() && utils.notSolved(challenges.jwtForgedChallenge)) {
jwtChallenge(challenges.jwtForgedChallenge, req, 'HS256', /rsa_lord@/)
}
next()
}
exports.serverSideChallenges = () => (req, res, next) => {
if (req.query.key === 'tRy_H4rd3r_n0thIng_iS_Imp0ssibl3') {
if (utils.notSolved(challenges.sstiChallenge) && req.app.locals.abused_ssti_bug === true) {
utils.solve(challenges.sstiChallenge)
res.status(204).send()
return
}
if (utils.notSolved(challenges.ssrfChallenge) && req.app.locals.abused_ssrf_bug === true) {
utils.solve(challenges.ssrfChallenge)
res.status(204).send()
return
}
}
next()
}
function jwtChallenge (challenge, req, algorithm, email) {
const token = utils.jwtFrom(req)
if (token) {
const decoded = jws.decode(token) ? jwt.decode(token) : null
jwt.verify(token, insecurity.publicKey, (err, verified) => {
if (err === null) {
utils.solveIf(challenge, () => { return hasAlgorithm(token, algorithm) && hasEmail(decoded, email) })
}
})
}
}
function hasAlgorithm (token, algorithm) {
const header = JSON.parse(Buffer.from(token.split('.')[0], 'base64').toString())
return token && header && header.alg === algorithm
}
function hasEmail (token, email) {
return token && token.data && token.data.email && token.data.email.match(email)
}
exports.databaseRelatedChallenges = () => (req, res, next) => {
if (utils.notSolved(challenges.changeProductChallenge) && products.osaft) {
changeProductChallenge(products.osaft)
}
if (utils.notSolved(challenges.feedbackChallenge)) {
feedbackChallenge()
}
if (utils.notSolved(challenges.knownVulnerableComponentChallenge)) {
knownVulnerableComponentChallenge()
}
if (utils.notSolved(challenges.weirdCryptoChallenge)) {
weirdCryptoChallenge()
}
if (utils.notSolved(challenges.typosquattingNpmChallenge)) {
typosquattingNpmChallenge()
}
if (utils.notSolved(challenges.typosquattingAngularChallenge)) {
typosquattingAngularChallenge()
}
if (utils.notSolved(challenges.hiddenImageChallenge)) {
hiddenImageChallenge()
}
if (utils.notSolved(challenges.supplyChainAttackChallenge)) {
supplyChainAttackChallenge()
}
if (utils.notSolved(challenges.dlpPastebinDataLeakChallenge)) {
dlpPastebinDataLeakChallenge()
}
next()
}
function changeProductChallenge (osaft) {
let urlForProductTamperingChallenge = null
osaft.reload().then(() => {
for (const product of config.products) {
if (product.urlForProductTamperingChallenge !== undefined) {
urlForProductTamperingChallenge = product.urlForProductTamperingChallenge
break
}
}
if (urlForProductTamperingChallenge) {
if (!utils.contains(osaft.description, `${urlForProductTamperingChallenge}`)) {
if (utils.contains(osaft.description, `<a href="${config.get('challenges.overwriteUrlForProductTamperingChallenge')}" target="_blank">More...</a>`)) {
utils.solve(challenges.changeProductChallenge)
}
}
}
})
}
function feedbackChallenge () {
models.Feedback.findAndCountAll({ where: { rating: 5 } }).then(({ count }) => {
if (count === 0) {
utils.solve(challenges.feedbackChallenge)
}
})
}
function knownVulnerableComponentChallenge () {
models.Feedback.findAndCountAll({
where: {
comment: {
[Op.or]: knownVulnerableComponents()
}
}
}).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.knownVulnerableComponentChallenge)
}
})
models.Complaint.findAndCountAll({
where: {
message: {
[Op.or]: knownVulnerableComponents()
}
}
}).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.knownVulnerableComponentChallenge)
}
})
}
function knownVulnerableComponents () {
return [
{
[Op.and]: [
{ [Op.like]: '%sanitize-html%' },
{ [Op.like]: '%1.4.2%' }
]
},
{
[Op.and]: [
{ [Op.like]: '%express-jwt%' },
{ [Op.like]: '%0.1.3%' }
]
}
]
}
function weirdCryptoChallenge () {
models.Feedback.findAndCountAll({
where: {
comment: {
[Op.or]: weirdCryptos()
}
}
}).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.weirdCryptoChallenge)
}
})
models.Complaint.findAndCountAll({
where: {
message: {
[Op.or]: weirdCryptos()
}
}
}).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.weirdCryptoChallenge)
}
})
}
function weirdCryptos () {
return [
{ [Op.like]: '%z85%' },
{ [Op.like]: '%base85%' },
{ [Op.like]: '%hashids%' },
{ [Op.like]: '%md5%' },
{ [Op.like]: '%base64%' }
]
}
function typosquattingNpmChallenge () {
models.Feedback.findAndCountAll({ where: { comment: { [Op.like]: '%epilogue-js%' } } }
).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.typosquattingNpmChallenge)
}
})
models.Complaint.findAndCountAll({ where: { message: { [Op.like]: '%epilogue-js%' } } }
).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.typosquattingNpmChallenge)
}
})
}
function typosquattingAngularChallenge () {
models.Feedback.findAndCountAll({ where: { comment: { [Op.like]: '%ng2-bar-rating%' } } }
).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.typosquattingAngularChallenge)
}
})
models.Complaint.findAndCountAll({ where: { message: { [Op.like]: '%ng2-bar-rating%' } } }
).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.typosquattingAngularChallenge)
}
})
}
function hiddenImageChallenge () {
models.Feedback.findAndCountAll({ where: { comment: { [Op.like]: '%pickle rick%' } } }
).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.hiddenImageChallenge)
}
})
models.Complaint.findAndCountAll({ where: { message: { [Op.like]: '%pickle rick%' } } }
).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.hiddenImageChallenge)
}
})
}
function supplyChainAttackChallenge () {
models.Feedback.findAndCountAll({ where: { comment: { [Op.or]: eslintScopeVulnIds() } } }
).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.supplyChainAttackChallenge)
}
})
models.Complaint.findAndCountAll({ where: { message: { [Op.or]: eslintScopeVulnIds() } } }
).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.supplyChainAttackChallenge)
}
})
}
function eslintScopeVulnIds () {
return [
{ [Op.like]: '%eslint-scope/issues/39%' },
{ [Op.like]: '%npm:eslint-scope:20180712%' }
]
}
function dlpPastebinDataLeakChallenge () {
models.Feedback.findAndCountAll({
where: {
comment: { [Op.and]: dangerousIngredients() }
}
}).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.dlpPastebinDataLeakChallenge)
}
})
models.Complaint.findAndCountAll({
where: {
message: { [Op.and]: dangerousIngredients() }
}
}).then(({ count }) => {
if (count > 0) {
utils.solve(challenges.dlpPastebinDataLeakChallenge)
}
})
}
function dangerousIngredients () {
const ingredients = []
const dangerousProduct = config.get('products').filter(product => product.keywordsForPastebinDataLeakChallenge)[0]
dangerousProduct.keywordsForPastebinDataLeakChallenge.map((keyword) => {
ingredients.push({ [Op.like]: `%${keyword}%` })
})
return ingredients
}