From 5995531918252b819862e38cf8cbc7b57f2fbd68 Mon Sep 17 00:00:00 2001 From: XuCcc <2604133533@qq.com> Date: Mon, 19 Feb 2018 16:26:38 +0800 Subject: [PATCH] feat: add Serve version to accept flag --- __init__.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 __init__.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..fe7d3f5 --- /dev/null +++ b/__init__.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Date : 2018/2/14 12:56 +# @Author : Xu +# @Site : https://xuccc.github.io/ +# @Version : $ + +import arrow + +from flask import render_template, Blueprint +from flask import request, jsonify + +from CTFd.plugins.keys import BaseKey,KEY_CLASSES +from CTFd.models import db, Keys +from CTFd.utils import admins_only, is_admin + +dynamic = Blueprint('dynamic', __name__) + + +class DynamicKey(BaseKey): + id = 2 + name = "dynamic" + + @staticmethod + def compare(saved, provided): + if len(saved) != len(provided): + return False + result = 0 + for x, y in zip(saved, provided): + result |= ord(x) ^ ord(y) + return result == 0 + + +def filter(token=None): + k = Keys.query.filter_by(data=token).first() + return k + +def save(k, flag): + """ + + :param k: class 'CTFd.models.Keys' + :param token: + :param flag: + :return: + """ + k.flag = flag + db.session.commit() + db.session.close() + return True + + +def load(app): + @dynamic.route('/dynamic/keys', methods=['POST', 'GET']) + def show(): + if request.method == 'GET': + flag = request.args.get('flag') + token = request.args.get('token') + time = request.args.get('time') + if flag and token and time: + k = filter(token) + if k is not None: + data = { + 'check': True, + 'old' : k.flag, + 'new' : flag, + 'time' : time or arrow.now().timestamp + } + save(k,flag) + else: + data = { + 'check' : False, + 'reason': 'token wrong', + 'time' : time or arrow.now().timestamp + } + return jsonify(data) + else: + return jsonify({'check': 'wrong'}) + elif request.method == 'POST': + # TODO + data = {} + return jsonify(data) + + + KEY_CLASSES['dynamic'] = DynamicKey + app.register_blueprint(dynamic) +