Esempio n. 1
0
def test_scoring_logic():
    """Test that scoring logic is correct"""
    app = create_ctfd()
    with app.app_context():
        admin = login_as_user(app, name="admin", password="******")

        register_user(app,
                      name="user1",
                      email="*****@*****.**",
                      password="******")
        client1 = login_as_user(app, name="user1", password="******")
        register_user(app,
                      name="user2",
                      email="*****@*****.**",
                      password="******")
        client2 = login_as_user(app, name="user2", password="******")

        chal1 = gen_challenge(app.db)
        gen_flag(app.db, challenge_id=chal1.id, content="flag")
        chal1_id = chal1.id

        chal2 = gen_challenge(app.db)
        gen_flag(app.db, challenge_id=chal2.id, content="flag")
        chal2_id = chal2.id

        # user1 solves chal1
        with freeze_time("2017-10-3 03:21:34"):
            with client1.session_transaction():
                data = {"submission": "flag", "challenge_id": chal1_id}
                client1.post("/api/v1/challenges/attempt", json=data)

        # user1 is now on top
        scores = get_scores(admin)
        assert scores[0]["name"] == "user1"

        # user2 solves chal1 and chal2
        with freeze_time("2017-10-4 03:30:34"):
            with client2.session_transaction():
                # solve chal1
                data = {"submission": "flag", "challenge_id": chal1_id}
                client2.post("/api/v1/challenges/attempt", json=data)
                # solve chal2
                data = {"submission": "flag", "challenge_id": chal2_id}
                client2.post("/api/v1/challenges/attempt", json=data)

        # user2 is now on top
        scores = get_scores(admin)
        assert scores[0]["name"] == "user2"

        # user1 solves chal2
        with freeze_time("2017-10-5 03:50:34"):
            with client1.session_transaction():
                data = {"submission": "flag", "challenge_id": chal2_id}
                client1.post("/api/v1/challenges/attempt", json=data)

        # user2 should still be on top because they solved chal2 first
        scores = get_scores(admin)
        assert scores[0]["name"] == "user2"
    destroy_ctfd(app)
Esempio n. 2
0
def test_hidden_users_should_not_influence_scores():
    app = create_ctfd()
    with app.app_context():
        register_user(app,
                      name="user1",
                      email="*****@*****.**",
                      password="******")
        register_user(app,
                      name="user2",
                      email="*****@*****.**",
                      password="******")
        register_user(app,
                      name="user3",
                      email="*****@*****.**",
                      password="******")

        user = Users.query.filter_by(name="user3").first()
        user.hidden = True
        app.db.session.commit()

        client1 = login_as_user(app, name="user1", password="******")

        # User 1 solves 1st challenge
        chal1 = gen_challenge(app.db)
        gen_solve(app.db, user_id=2, challenge_id=chal1.id)

        # User 2 solves 2nd challenge
        chal2 = gen_challenge(app.db)
        gen_solve(app.db, user_id=3, challenge_id=chal2.id)

        # User 3 solves both
        gen_solve(app.db, user_id=4, challenge_id=chal1.id)
        gen_solve(app.db, user_id=4, challenge_id=chal2.id)

        scores = get_scores(client1)

        for entry in scores:
            assert entry["name"] != "user3"

        user1 = Users.query.filter_by(name="user1").first()
        assert user1.place == "1st"

        user2 = Users.query.filter_by(name="user2").first()
        assert user2.place == "2nd"
    destroy_ctfd(app)
Esempio n. 3
0
def test_scoring_logic_with_zero_point_challenges():
    """Test that scoring logic is correct with zero point challenges. Zero point challenges should not tie break"""
    app = create_ctfd()
    with app.app_context():
        admin = login_as_user(app, name="admin", password="******")

        register_user(app,
                      name="user1",
                      email="*****@*****.**",
                      password="******")
        client1 = login_as_user(app, name="user1", password="******")
        register_user(app,
                      name="user2",
                      email="*****@*****.**",
                      password="******")
        client2 = login_as_user(app, name="user2", password="******")

        chal1 = gen_challenge(app.db)
        gen_flag(app.db, challenge_id=chal1.id, content='flag')
        chal1_id = chal1.id

        chal2 = gen_challenge(app.db)
        gen_flag(app.db, challenge_id=chal2.id, content='flag')
        chal2_id = chal2.id

        # A 0 point challenge shouldn't influence the scoreboard (see #577)
        chal0 = gen_challenge(app.db, value=0)
        gen_flag(app.db, challenge_id=chal0.id, content='flag')
        chal0_id = chal0.id

        # user1 solves chal1
        with freeze_time("2017-10-3 03:21:34"):
            with client1.session_transaction():
                data = {"submission": 'flag', "challenge_id": chal1_id}
                client1.post('/api/v1/challenges/attempt', json=data)

        # user1 is now on top
        scores = get_scores(admin)
        assert scores[0]['name'] == 'user1'

        # user2 solves chal1 and chal2
        with freeze_time("2017-10-4 03:30:34"):
            with client2.session_transaction():
                # solve chal1
                data = {"submission": 'flag', "challenge_id": chal1_id}
                client2.post('/api/v1/challenges/attempt'.format(chal1_id),
                             json=data)
                # solve chal2
                data = {"submission": 'flag', "challenge_id": chal2_id}
                client2.post('/api/v1/challenges/attempt'.format(chal2_id),
                             json=data)

        # user2 is now on top
        scores = get_scores(admin)
        assert scores[0]['name'] == 'user2'

        # user1 solves chal2
        with freeze_time("2017-10-5 03:50:34"):
            with client1.session_transaction():
                data = {"submission": 'flag', "challenge_id": chal2_id}
                client1.post('/api/v1/challenges/attempt'.format(chal2_id),
                             json=data)

        # user2 should still be on top because they solved chal2 first
        scores = get_scores(admin)
        assert scores[0]['name'] == 'user2'

        # user2 solves a 0 point challenge
        with freeze_time("2017-10-5 03:55:34"):
            with client2.session_transaction():
                data = {"submission": 'flag', "challenge_id": chal0_id}
                client2.post('/api/v1/challenges/attempt'.format(chal0_id),
                             json=data)

        # user2 should still be on top because 0 point challenges should not tie break
        scores = get_scores(admin)
        assert scores[0]['name'] == 'user2'
    destroy_ctfd(app)