Repository: CTFd/CTFd

Issue:
Interface to view wrong key submissions



Discussion:

Resolving patch:
diff --git a/CTFd/admin.py b/CTFd/admin.py
--- a/CTFd/admin.py
+++ b/CTFd/admin.py
@@ -428,6 +428,15 @@ def admin_solves(teamid="all"):
             json['solves'].append({'id':x.id, 'chal':x.chal.name, 'chalid':x.chalid,'team':x.teamid, 'value': x.chal.value, 'category':x.chal.category, 'time':unix_time(x.date)})
         return jsonify(json)
 
+
+    @app.route('/admin/solves/<teamid>/<chalid>/delete', methods=['POST'])
+    @admins_only
+    def delete_solve(teamid, chalid):
+        solve = Solves.query.filter_by(teamid=teamid, chalid=chalid).first()
+        db.session.delete(solve)
+        db.session.commit()
+        return '1'
+
     @app.route('/admin/statistics', methods=['GET'])
     @admins_only
     def admin_stats():
@@ -450,7 +459,39 @@ def admin_stats():
             least_solved=least_solved_chal
             )
 
+    @app.route('/admin/wrong_keys/<page>', methods=['GET'])
+    @admins_only
+    def admin_wrong_key(page='1'):
+        page = abs(int(page))
+        results_per_page = 50
+        page_start = results_per_page * ( page - 1 )
+        page_end = results_per_page * ( page - 1 ) + results_per_page
+
+        wrong_keys = WrongKeys.query.add_columns(WrongKeys.flag, WrongKeys.team, WrongKeys.date,\
+                    Challenges.name.label('chal_name'), Teams.name.label('team_name')).\
+                    join(Challenges).join(Teams).order_by('team_name ASC').slice(page_start, page_end).all()
+
+        wrong_count = db.session.query(db.func.count(WrongKeys.id)).first()[0]
+        pages = int(wrong_count / results_per_page) + (wrong_count % results_per_page > 0)
+
+        return render_template('admin/wrong_keys.html', wrong_keys=wrong_keys, pages=pages)
+
+    @app.route('/admin/correct_keys/<page>', methods=['GET'])
+    @admins_only
+    def admin_correct_key(page='1'):
+        page = abs(int(page))
+        results_per_page = 50
+        page_start = results_per_page * (page - 1)
+        page_end = results_per_page * (page - 1) + results_per_page
+
+        solves = Solves.query.add_columns(Solves.chalid, Solves.teamid, Solves.date, Solves.flag, \
+                    Challenges.name.label('chal_name'), Teams.name.label('team_name')).\
+                    join(Challenges).join(Teams).order_by('team_name ASC').slice(page_start, page_end).all()
+
+        solve_count = db.session.query(db.func.count(Solves.id)).first()[0]
+        pages = int(solve_count / results_per_page) + (solve_count % results_per_page > 0)
 
+        return render_template('admin/correct_keys.html', solves=solves, pages=pages)
 
     @app.route('/admin/fails/<teamid>', methods=['GET'])
     @admins_only
diff --git a/CTFd/challenges.py b/CTFd/challenges.py
--- a/CTFd/challenges.py
+++ b/CTFd/challenges.py
@@ -99,7 +99,7 @@ def chal(chalid):
                 for x in keys:
                     if x.key_type == 0: #static key
                         if x.flag.strip().lower() == key:
-                            solve = Solves(chalid=chalid, teamid=session['id'], ip=request.remote_addr)
+                            solve = Solves(chalid=chalid, teamid=session['id'], ip=request.remote_addr, flag=key)
                             db.session.add(solve)
                             db.session.commit()
                             db.session.close()
@@ -108,7 +108,7 @@ def chal(chalid):
                     elif x.key_type == 1: #regex 
                         res = re.match(str(x), key, re.IGNORECASE)
                         if res and res.group() == key:
-                            solve = Solves(chalid=chalid, teamid=session['id'], ip=request.remote_addr)
+                            solve = Solves(chalid=chalid, teamid=session['id'], ip=request.remote_addr, flag=key)
                             db.session.add(solve)
                             db.session.commit()
                             db.session.close()
diff --git a/CTFd/models.py b/CTFd/models.py
--- a/CTFd/models.py
+++ b/CTFd/models.py
@@ -135,15 +135,17 @@ class Solves(db.Model):
     chalid = db.Column(db.Integer, db.ForeignKey('challenges.id'))
     teamid = db.Column(db.Integer, db.ForeignKey('teams.id'))
     ip = db.Column(db.Integer)
+    flag = db.Column(db.Text)
     date = db.Column(db.DateTime, default=datetime.datetime.utcnow)
     team = db.relationship('Teams', foreign_keys="Solves.teamid", lazy='joined')
     chal = db.relationship('Challenges', foreign_keys="Solves.chalid", lazy='joined')
     # value = db.Column(db.Integer) 
 
-    def __init__(self, chalid, teamid, ip):
+    def __init__(self, chalid, teamid, ip, flag):
         self.ip = ip2long(ip)
         self.chalid = chalid
         self.teamid = teamid
+        self.flag = flag
         # self.value = value
 
     def __repr__(self):


Test patch: