Skip to content

Instantly share code, notes, and snippets.

@jirojo2
Last active April 4, 2022 21:37
Show Gist options
  • Save jirojo2/ca4800e077ff3f172a2a to your computer and use it in GitHub Desktop.
Save jirojo2/ca4800e077ff3f172a2a to your computer and use it in GitHub Desktop.
CTF Telegram Bot
var telegram = require('telegram-bot-api');
var util = require('util');
var _ = require('lodash');
function CTFBot(token, ctf) {
this.id = null;
this.name = null;
this.username = null;
this.ctf = ctf;
this.currentChallenge = 0;
this.scores = {};
var self = this;
var api = this.api = new telegram({
token: token,
updates: {
enabled: true,
get_interval: 1000
}
});
function handleMessageCallback(err, generatedMessage) {
if (err) {
console.log("ERROR handling message: %j", err);
}
}
var commands = {
scores: function(msg, args) {
var t = "";
for (p in self.scores)
t += util.format("%s: %d\n", p, self.scores[p]);
api.sendMessage({
chat_id: msg.chat.id,
text: util.format("%s - Scores\n\n%s",
ctf.name,
t)
}, handleMessageCallback);
},
challenge: function(msg, args) {
api.sendMessage({
chat_id: msg.chat.id,
text: util.format("Challenge %d: %s\n\n%s",
self.currentChallenge,
ctf.challenges[self.currentChallenge].name,
ctf.challenges[self.currentChallenge].desc)
}, handleMessageCallback);
},
flag: function(msg, args) {
var sender = msg.from.username || 'troll';
var challenge = ctf.challenges[self.currentChallenge];
var response = {
chat_id: msg.chat.id,
text: util.format("@%s: nope", sender)
}
if (!args || args.length < 1) {
return api.sendMessage(_.extend(response, { text: "usage:\n\n/flag <flag>" }), handleMessageCallback);
}
else if (!challenge.resolved && challenge.flag === args[0]) {
challenge.resolved = true;
challenge.winner = sender;
api.sendMessage(_.extend(response, { text: util.format("@%s: correcto!", sender) }), handleMessageCallback);
setTimeout(function() {
self.scores[challenge.winner] = (self.scores[challenge.winner] || 0) + 1;
self.currentChallenge++;
if (self.currentChallenge >= ctf.challenges.length) {
api.sendMessage(_.extend(response, { text: "Fin del CTF!" }), handleMessageCallback);
var maxScore = 0;
for (var name in self.scores) {
if (maxScore < self.scores[name])
maxScore = self.scores[name];
}
var winners = [];
for (var name in self.scores) {
if (maxScore == self.scores[name])
winners.push(name);
}
if (winners.length > 1) {
var fw = winners.map(function(x) { return "@"+x });
api.sendMessage(_.extend(response, { text: util.format("Ganadores: %j", fw) }), handleMessageCallback);
}
else {
api.sendMessage(_.extend(response, { text: util.format("Ganador: @%s", winners[0]) }), handleMessageCallback);
}
return;
}
api.sendMessage(_.extend(response, { text: "Siguiente prueba!" }), handleMessageCallback);
return setTimeout(function() {
commands.challenge(msg, []);
}, 1000);
}, 1000);
}
else {
api.sendMessage(response, handleMessageCallback);
}
}
};
api.getMe(function(err, data)
{
if (err) {
throw util.format("ERROR initializing bot: %s", err);
}
console.log("Initialized bot %s [%d] @%s", data.first_name, data.id, data.username);
api.on('message', function handleMessage(msg) {
// handle command
var chatId = msg.chat.id;
var text = msg.text || '';
var args = text.split(' ');
var cmd = args.splice(0, 1)[0];
if (cmd.indexOf('/') === 0 && cmd.substr(1) in commands) {
commands[cmd.substr(1)](msg, args);
}
});
});
}
var ctf = {
name: 'JosiCTF',
challenges: [
{
name: 'This one is <easy>',
desc: '<flag>',
flag: 'easy'
},
{
name: 'Not so easy',
desc: 'Be fast!',
flag: 'fast'
},
{
name: 'Crypto for n00bs',
desc: 'YuttjqJwHEVwjgMojOMoHufpHgJtkgJgULl4GrY1UhG0lhNc',
flag: '<flag>'
},
]
};
var bot = new CTFBot('<token>', ctf);
@esthicodes
Copy link

ist das Python 3 version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment