Skip to content

Commit

Permalink
Merge pull request #2 from imApoorva36/main
Browse files Browse the repository at this point in the history
Configured Docker Plugin basic working functionality
  • Loading branch information
ChinmayaSharma-hue authored Mar 6, 2024
2 parents c936100 + 6720067 commit bb58cfc
Show file tree
Hide file tree
Showing 134 changed files with 11,597 additions and 1,713 deletions.
685 changes: 685 additions & 0 deletions CTFd/plugins/docker_challenges/__init__.py

Large diffs are not rendered by default.

695 changes: 695 additions & 0 deletions CTFd/plugins/docker_challenges/aa__init__.py

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions CTFd/plugins/docker_challenges/assets/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "admin/challenges/create.html" %}
{% block category %}
<div class="form-group">
<label>
Category:<br>
<small class="form-text text-muted">
The category of your challenge
</small>
</label>
<input type="text" class="form-control" name="category" placeholder="Enter challenge category">
</div>
<div class="form-group">
<label for="DockerImage" id='dockerimage_label'>Docker Image:
<i class="far fa-question-circle text-muted cursor-help" data-toggle="tooltip" data-placement="right" title="The docker image for your challenge"></i>
</label>
<select id="dockerimage_select" name="docker_image" class="form-control" required></select>
</div>
{% endblock %}
{% block type %}
<input type="hidden" name="type" value="docker" id="chaltype">
{% endblock %}
26 changes: 26 additions & 0 deletions CTFd/plugins/docker_challenges/assets/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
CTFd.plugin.run((_CTFd) => {
const $ = _CTFd.lib.$
const md = _CTFd.lib.markdown()
$('a[href="#new-desc-preview"]').on('shown.bs.tab', function (event) {
if (event.target.hash == '#new-desc-preview') {
var editor_value = $('#new-desc-editor').val();
$(event.target.hash).html(
md.render(editor_value)
);
}
});
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
$.getJSON("/api/v1/docker", function(result){
$.each(result['data'], function(i, item){
if (item.name == 'Error in Docker Config!') {
document.docker_form.dockerimage_select.disabled = true;
$("label[for='DockerImage']").text('Docker Image ' + item.name)
}
else {
$("#dockerimage_select").append($("<option />").val(item.name).text(item.name));
}
});
});
});
});
21 changes: 21 additions & 0 deletions CTFd/plugins/docker_challenges/assets/update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "admin/challenges/update.html" %}
{% block category %}
<div class="form-group">
<label>
Category<br>
<small class="form-text text-muted">Challenge Category</small>
</label>
<input type="text" class="form-control chal-category" name="category" value="{{ challenge.category }}">
</div>
<div class="form-group">
<label for="DockerImage" id='dockerimage_label'>Docker Image:
<i class="far fa-question-circle text-muted cursor-help" data-toggle="tooltip" data-placement="right" title="The docker image for your challenge"></i>
</label>
<select id="dockerimage_select" name="docker_image" class="form-control" required></select>
</div>
{% endblock %}
{% block footer %}
<script>
var DOCKER_IMAGE = '{{ challenge.docker_image }}';
</script>
{% endblock %}
12 changes: 12 additions & 0 deletions CTFd/plugins/docker_challenges/assets/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CTFd.plugin.run((_CTFd) => {
const $ = _CTFd.lib.$
const md = _CTFd.lib.markdown()
$(document).ready(function() {
$.getJSON("/api/v1/docker", function(result) {
$.each(result['data'], function(i, item) {
$("#dockerimage_select").append($("<option />").val(item.name).text(item.name));
});
$("#dockerimage_select").val(DOCKER_IMAGE).change();
});
});
});
26 changes: 26 additions & 0 deletions CTFd/plugins/docker_challenges/assets/view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "challenge.html" %}
{% block description %}
<script>
var DOCKER_CONTAINER = '{{ challenge.docker_image | safe }}';
(function() {
get_docker_status(DOCKER_CONTAINER);
})();
</script>
{{ challenge.html }}
<div class='mb-3 text-center' id='docker_container'>
<span>
<a onclick="start_container('{{ challenge.docker_image | safe }}');" class='btn btn-dark'>
<small style='color:white;'><i class="fas fa-play"></i> Start Docker Instance</small>
</a>
</span>
</div>
{% endblock %}
{% block input %}
<input id="challenge-id" class="challenge-id" type="hidden" value="{{ challenge.id }}">
<input id="challenge-input" class="challenge-input" type="text" name="answer" placeholder="Flag" />
{% endblock %}
{% block submit %}
<button id="challenge-submit" class="challenge-submit" type="submit">
Submit
</button>
{% endblock %}
125 changes: 125 additions & 0 deletions CTFd/plugins/docker_challenges/assets/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
CTFd._internal.challenge.data = undefined

CTFd._internal.challenge.renderer = CTFd.lib.markdown();


CTFd._internal.challenge.preRender = function() {}

CTFd._internal.challenge.render = function(markdown) {

return CTFd._internal.challenge.renderer.render(markdown)
}


CTFd._internal.challenge.postRender = function() {}


CTFd._internal.challenge.submit = function(preview) {
var challenge_id = parseInt(CTFd.lib.$('#challenge-id').val())
var submission = CTFd.lib.$('#challenge-input').val()

var body = {
'challenge_id': challenge_id,
'submission': submission,
}
var params = {}
if (preview) {
params['preview'] = true
}

return CTFd.api.post_challenge_attempt(params, body).then(function(response) {
if (response.status === 429) {
// User was ratelimited but process response
return response
}
if (response.status === 403) {
// User is not logged in or CTF is paused.
return response
}
return response
})
};

function get_docker_status(container) {
$.get("/api/v1/docker_status", function(result) {
$.each(result['data'], function(i, item) {
if (item.docker_image == container) {
var ports = String(item.ports).split(',');
var data = '';
$.each(ports, function(x, port) {
port = String(port)
data = data + 'Host: ' + '3.236.225.76' + ' Port: ' + port + '<br />'; //data = data + 'Host: ' + item.host + ' Port: ' + port + '<br />';
})
$('#docker_container').html('<pre>Docker Container Information:<br />' + data + '<div class="mt-2" id="' + String(item.instance_id).substring(0,10) + '_revert_container"></div>');
var countDownDate = new Date(parseInt(item.revert_time) * 1000).getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
if (seconds < 10) {
seconds = "0" + seconds
}
$("#" + String(item.instance_id).substring(0,10) + "_revert_container").html('Next Revert Available in ' + minutes + ':' + seconds);
if (distance < 0) {
clearInterval(x);
$("#" + String(item.instance_id).substring(0,10) + "_revert_container").html('<a onclick="start_container(\'' + item.docker_image + '\');" class=\'btn btn-dark\'><small style=\'color:white;\'><i class="fas fa-redo"></i> Revert</small></a>');
}
}, 1000);
return false;
};
});
});
};

function start_container(container) {
$('#docker_container').html('<div class="text-center"><i class="fas fa-circle-notch fa-spin fa-1x"></i></div>');
$.get("/api/v1/container", { 'name': container }, function(result) {
get_docker_status(container);
})
.fail(function(jqxhr, settings, ex) {
ezal({
title: "Attention!",
body: "You can only revert a container once per 5 minutes! Please be patient.",
button: "Got it!"
});
$(get_docker_status(container));
});
}

var modal =
'<div class="modal fade" tabindex="-1" role="dialog">' +
' <div class="modal-dialog" role="document">' +
' <div class="modal-content">' +
' <div class="modal-header">' +
' <h5 class="modal-title">{0}</h5>' +
' <button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
' <span aria-hidden="true">&times;</span>' +
" </button>" +
" </div>" +
' <div class="modal-body">' +
" <p>{1}</p>" +
" </div>" +
' <div class="modal-footer">' +
" </div>" +
" </div>" +
" </div>" +
"</div>";

function ezal(args) {
var res = modal.format(args.title, args.body);
var obj = $(res);
var button = '<button type="button" class="btn btn-primary" data-dismiss="modal">{0}</button>'.format(
args.button
);
obj.find(".modal-footer").append(button);
$("main").append(obj);

obj.modal("show");

$(obj).on("hidden.bs.modal", function(e) {
$(this).modal("dispose");
});

return obj;
}
10 changes: 10 additions & 0 deletions CTFd/plugins/docker_challenges/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"name": "Docker Config",
"route": "/admin/docker_config"
},
{
"name": "Docker Status",
"route": "/admin/docker_status"
}
]
Loading

0 comments on commit bb58cfc

Please sign in to comment.