Skip to content

Commit 77a719a

Browse files
authored
Merge pull request #148 from ColdHeat/ctfd-paid-hints-json
Add paid hints back to CTFd exporter
2 parents ba4586f + 763f1b3 commit 77a719a

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

lib/generators/ctfd.js

+33-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
const Promise = require('bluebird')
77
const calculateScore = require('../calculateScore')
8-
// const calculateHintCost = require('../calculateHintCost')
8+
const calculateHintCost = require('../calculateHintCost')
99
const hmacSha1 = require('../hmac')
1010
const options = require('../options')
1111

@@ -21,10 +21,9 @@ function createCtfdExport (challenges, { insertHints, insertHintUrls, insertHint
2121
if (vulnSnippets[challenge.key] && insertHintSnippets !== options.noHintSnippets) {
2222
hints.push('<pre><code>' + vulnSnippets[challenge.key].replaceAll('"', '""').replaceAll(',', '٬') + '</code></pre>')
2323
}
24-
return (hints.length === 0 ? '' : `"${hints.join(',')}"`)
24+
return hints
2525
}
2626

27-
/*
2827
function insertChallengeHintCosts (challenge) {
2928
const hintCosts = []
3029
if (challenge.hint && insertHints !== options.noTextHints) {
@@ -36,9 +35,8 @@ function createCtfdExport (challenges, { insertHints, insertHintUrls, insertHint
3635
if (vulnSnippets[challenge.key] && insertHintSnippets !== options.noHintSnippets) {
3736
hintCosts.push(calculateHintCost(challenge, insertHintSnippets))
3837
}
39-
return (hintCosts.length === 0 ? '' : `"${hintCosts.join(',')}"`)
38+
return hintCosts
4039
}
41-
*/
4240

4341
// In the flags section of the returned data we iterate through the result of string splitting by comma, and compute the hash of the single flag key + challenge name.
4442
// Format expected is: challenge3,challenge description,category3,100,dynamic,visible,0,"flag1,flag2,flag3","tag1,tag2,tag3","hint1,hint2,hint3","{""initial"":100, ""minimum"":10, ""decay"":10}"
@@ -49,22 +47,37 @@ function createCtfdExport (challenges, { insertHints, insertHintUrls, insertHint
4947
for (const key in challenges) {
5048
if (Object.prototype.hasOwnProperty.call(challenges, key)) {
5149
const challenge = challenges[key]
52-
data.push(
53-
{
54-
name: challenge.name,
55-
description: `"${challenge.description.replaceAll('"', '""')} (Difficulty Level: ${challenge.difficulty})"`,
56-
category: challenge.category,
57-
value: calculateScore(challenge.difficulty),
58-
type: 'standard',
59-
state: 'visible',
60-
max_attempts: 0,
61-
flags: ctfKey.split(',').length === 1 ? hmacSha1(ctfKey, challenge.name) : `"${ctfKey.split(',').map(key => `${hmacSha1(key, challenge.name)}`).join(',')}"`,
62-
tags: challenge.tags ? `"${challenge.tags}"` : '',
63-
hints: insertChallengeHints(challenge),
64-
// hint_cost: insertChallengeHintCosts(challenge),
65-
type_data: ''
50+
const row = {
51+
name: challenge.name,
52+
description: `"${challenge.description.replaceAll('"', '""')} (Difficulty Level: ${challenge.difficulty})"`,
53+
category: challenge.category,
54+
value: calculateScore(challenge.difficulty),
55+
type: 'standard',
56+
state: 'visible',
57+
max_attempts: 0,
58+
flags: ctfKey.split(',').length === 1 ? hmacSha1(ctfKey, challenge.name) : `"${ctfKey.split(',').map(key => `${hmacSha1(key, challenge.name)}`).join(',')}"`,
59+
tags: challenge.tags ? `"${challenge.tags}"` : '',
60+
hints_raw: insertChallengeHints(challenge),
61+
hint_cost: insertChallengeHintCosts(challenge),
62+
type_data: ''
63+
}
64+
const hints = []
65+
if (row.hints_raw.length !== 0) {
66+
for (let index = 0; index < row.hints_raw.length; index++) {
67+
const hint = {
68+
content: row.hints_raw[index],
69+
cost: row.hint_cost[index]
70+
}
71+
hints.push(hint)
6672
}
67-
)
73+
const hints_obj = JSON.stringify(hints).replace(/"/g, '""')
74+
row.hints = `"${hints_obj}"`
75+
} else {
76+
row.hints = ''
77+
}
78+
delete row.hints_raw
79+
delete row.hint_cost
80+
data.push(row)
6881
}
6982
}
7083
resolve(data)

0 commit comments

Comments
 (0)