Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTB parser #193

Merged
merged 2 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions front/src/components/Dialogs/TaskImportDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
color="positive"
:disable="btnDisable"
:label="btnLabel"
:loading="loading"
@click="btnClick"
/>
</q-card-actions>
Expand All @@ -60,15 +61,9 @@
import { useDialogPluginComponent } from 'quasar';
import ctfnote from 'src/ctfnote';
import { Ctf } from 'src/ctfnote/models';
import parsers from 'src/ctfnote/parsers';
import parsers, { ParsedTask } from 'src/ctfnote/parsers';
import { defineComponent, ref } from 'vue';

type ParsedTask = {
title: string;
category: string;
keep: boolean;
};

export default defineComponent({
props: {
ctf: { type: Object as () => Ctf, required: true },
Expand Down Expand Up @@ -96,6 +91,7 @@ export default defineComponent({
onDialogHide,
onDialogOK,
onDialogCancel,
loading: ref<boolean>(false),
};
},
computed: {
Expand Down Expand Up @@ -151,19 +147,19 @@ export default defineComponent({
return { ...task, keep: !taskSet.has(hash) };
});
},
btnClick() {
async btnClick() {
if (this.tab == 'parse') {
this.parsedTasks = this.parseTasks(this.model);
this.tab = 'confirm';
} else {
this.parsedTasks
this.loading = true;
const result = this.parsedTasks
.filter((t) => t.keep)
.map((task) => {
void this.createTask(this.ctf.id, {
title: task.title,
category: task.category,
});
return this.createTask(this.ctf.id, task);
});
await Promise.all(result);
this.loading = false;
this.onDialogOK();
}
},
Expand Down
79 changes: 79 additions & 0 deletions front/src/ctfnote/parsers/htb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ParsedTask, Parser } from '.';
import { parseJson, parseJsonStrict } from '../utils';

// output of https://ctf.hackthebox.com/api/public/challengeCategories
const challengeCategories: { [index: number]: string } = {
1: 'Fullpwn',
2: 'Web',
3: 'Pwn',
4: 'Crypto',
5: 'Reversing',
6: 'Stego',
7: 'Forensics',
8: 'Misc',
9: 'Start',
10: 'PCAP',
11: 'Coding',
12: 'Mobile',
13: 'OSINT',
14: 'Blockchain',
15: 'Hardware',
16: 'Warmup',
17: 'Attack',
18: 'Defence',
20: 'Cloud',
21: 'Scada',
};

const HTBParser: Parser = {
name: 'HTB parser',
hint: 'paste https://ctf.hackthebox.com/api/ctf/<ctfid> from the network tab',

parse(s: string): ParsedTask[] {
const tasks = [];
const data = parseJsonStrict<{
challenges: Array<{
id: number;
name: string;
description: string;
challenge_category_id: number;
}>;
}>(s);
if (data == null) {
return [];
}

for (const challenge of data.challenges) {
if (
!challenge.description ||
!challenge.name ||
!challenge.challenge_category_id
) {
continue;
}

let category = challengeCategories[challenge.challenge_category_id];
if (category == null) category = 'Unknown';

tasks.push({
title: challenge.name,
category: category,
description: challenge.description,
});
}
return tasks;
},
isValid(s) {
const data = parseJson<{
challenges: Array<{
id: number;
name: string;
description: string;
challenge_category_id: number;
}>;
}>(s);
return Array.isArray(data?.challenges);
},
};

export default HTBParser;
5 changes: 4 additions & 1 deletion front/src/ctfnote/parsers/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import CTFDParser from './ctfd';
import ECSCParser from './ecsc';
import RawParser from './raw';
import HTBParser from './htb';

export type ParsedTask = {
title: string;
category: string;
description?: string;
keep?: boolean;
};

export type Parser = {
Expand All @@ -14,4 +17,4 @@ export type Parser = {
parse(s: string): ParsedTask[];
};

export default [RawParser, CTFDParser, ECSCParser];
export default [RawParser, CTFDParser, ECSCParser, HTBParser];