def gen_flag(db, challenge_id, content="flag", type="static", data=None, **kwargs): flag = Flags(challenge_id=challenge_id, content=content, type=type, **kwargs) if data: flag.data = data db.session.add(flag) db.session.commit() return flag
def update_flags(flags, chal_dbobj): from CTFd.models import db, Flags events = [] db_flag_objects = {flag.content:flag for flag in Flags.query.filter_by(challenge_id=chal_dbobj.id).all()} chal_flags = {flag['flag']:flag for flag in flags} tags_db = set(db_flag_objects.keys()) tags_in = set(chal_flags.keys()) new_flags = tags_in - tags_db del_flags = tags_db - tags_in upd_flags = tags_in & tags_db for flag in new_flags: fl_obj = chal_flags[flag] fl_type = fl_obj['type'] fl_data = 'case_insensitive' if fl_obj.get('case_insensitive') else None flag_db = Flags(challenge_id=chal_dbobj.id, content=flag, type=fl_type, data=fl_data) db.session.add(flag_db) events.append(Event(Event.Type.info, f"Challenge {chal_dbobj.name}: Adding new flag {flag}")) for flag in del_flags: db.session.delete(db_flag_objects[flag]) events.append(Event(Event.Type.warn, f"Challenge {chal_dbobj.name}: Deleting flag {flag}")) for flag in upd_flags: fl_obj = chal_flags[flag] flag_db = db_flag_objects[flag] flag_db.type = fl_obj['type'] flag_db.data = 'case_insensitive' if fl_obj.get('case_insensitive') else None return events