Пример #1
0
    def get_history(status):
        db = database.get(context_bound=False)

        curr_cycle = db.execute(
            "SELECT MAX(sent_cycle) as cycle FROM flags").fetchone()["cycle"]
        if not curr_cycle:
            curr_cycle = 0

        ret = []

        for cycle in range(curr_cycle):
            elem = {"cycle": cycle, "sploits": {}}

            sploit_rows = db.execute(
                "SELECT sploit, COUNT(*) as n "
                "FROM flags "
                "WHERE status = ? AND sent_cycle = ? "
                "GROUP BY sploit", (status, cycle)).fetchall()

            for sploit in sploit_rows:
                sploit_name = sploit["sploit"]
                if sploit_name is None:
                    continue

                n = sploit['n']
                elem['sploits'][sploit_name] = n
            ret.append(elem)

            if len(ret) % 10 == 0:
                yield ret
                ret = []

        yield ret
Пример #2
0
def run_loop():
    app.logger.info('Starting submit loop')
    with app.app_context():
        db = database.get(context_bound=False)

    while True:
        submit_start_time = time.time()

        config = reloader.get_config()

        skip_time = round(submit_start_time - config['FLAG_LIFETIME'])
        db.execute("UPDATE flags SET status = ? WHERE status = ? AND time < ?",
                   (FlagStatus.SKIPPED.name, FlagStatus.QUEUED.name, skip_time))
        db.commit()

        cursor = db.execute("SELECT * FROM flags WHERE status = ?", (FlagStatus.QUEUED.name,))
        queued_flags = [Flag(**item) for item in cursor.fetchall()]

        if queued_flags:
            grouped_flags = defaultdict(list)
            for item in queued_flags:
                grouped_flags[item.sploit, item.team].append(item)
            flags = get_fair_share(grouped_flags.values(), config['SUBMIT_FLAG_LIMIT'])

            app.logger.debug('Submitting %s flags (out of %s in queue)', len(flags), len(queued_flags))
            results = submit_flags(flags, config)

            rows = [(item.status.name, item.checksystem_response, item.flag) for item in results]
            db.executemany("UPDATE flags SET status = ?, checksystem_response = ? "
                           "WHERE flag = ?", rows)
            db.commit()

        submit_spent = time.time() - submit_start_time
        if config['SUBMIT_PERIOD'] > submit_spent:
            time.sleep(config['SUBMIT_PERIOD'] - submit_spent)
Пример #3
0
def post_flags():
    flags = request.get_json()
    flags = [item for item in flags if not is_spam_flag(item['flag'])]

    cur_time = round(time.time())
    rows = [(item['flag'], item['sploit'], item['team'], cur_time, FlagStatus.QUEUED.name)
            for item in flags]

    db = database.get()
    db.executemany("INSERT OR IGNORE INTO flags (flag, sploit, team, time, status) "
                   "VALUES (?, ?, ?, ?, ?)", rows)
    db.commit()

    return ''
Пример #4
0
def post_flags_manual():
    config = reloader.get_config()
    flags = re.findall(config['FLAG_FORMAT'], request.form['text'])

    cur_time = round(time.time())
    rows = [(item, 'Manual', '*', cur_time, FlagStatus.QUEUED.name)
            for item in flags]

    db = database.get()
    db.executemany(
        "INSERT OR IGNORE INTO flags (flag, sploit, team, time, status) "
        "VALUES (?, ?, ?, ?, ?)", rows)
    db.commit()

    return ''
Пример #5
0
def run_loop():
    app.logger.info('Starting submit loop')
    with app.app_context():
        db = database.get(context_bound=False)

    cycle = db.execute("SELECT MAX(sent_cycle) AS last_cycle "
                       "FROM flags").fetchone()['last_cycle']
    if not cycle:
        cycle = 0

    while True:
        cycle += 1

        submit_start_time = time.time()

        config = reloader.get_config()

        # If flag time > FLAG_LIFETIME set as SKIPPED
        skip_time = round(submit_start_time - config['FLAG_LIFETIME'])
        db.execute(
            "UPDATE flags SET status = ? WHERE status = ? AND time < ?",
            (FlagStatus.SKIPPED.name, FlagStatus.QUEUED.name, skip_time))
        db.commit()

        cursor = db.execute("SELECT * FROM flags WHERE status = ?",
                            (FlagStatus.QUEUED.name, ))
        queued_flags = [Flag(**item) for item in cursor.fetchall()]

        if queued_flags:
            grouped_flags = defaultdict(list)
            for item in queued_flags:
                grouped_flags[item.sploit, item.team].append(item)

            flags = get_fair_share(grouped_flags.values(),
                                   config['SUBMIT_FLAG_LIMIT'])

            app.logger.debug(
                f'Submitting {len(flags)} flags (out of {len(queued_flags)} in queue)'
            )
            # Send flags to gameserver
            results = submit_flags(flags, config)

            rows = [(item.status.name, item.checksystem_response, cycle,
                     item.flag) for item in results]
            db.executemany(
                "UPDATE flags "
                "SET status = ?, checksystem_response = ?, sent_cycle = ? "
                "WHERE flag = ?", rows)
            db.commit()

            flags_status = {result.flag: result.status for result in results}

            def add_status(item: Flag):
                return Flag(item.flag, item.sploit, item.team, item.time,
                            flags_status[item.flag], item.checksystem_response,
                            item.sent_cycle)

            flags = list(map(add_status, flags))
            flag_ann.announce((cycle, flags))

        submit_spent = time.time() - submit_start_time
        if config['SUBMIT_PERIOD'] > submit_spent:
            time.sleep(config['SUBMIT_PERIOD'] - submit_spent)