Skip to content

Commit

Permalink
Completed Logging Implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mostwanted002 committed Jul 29, 2019
1 parent adc459b commit 5a3a084
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/FlaskRTBCTF/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask_login import LoginManager
from flask_admin import Admin
from flask_mail import Mail
from FlaskRTBCTF.config import Config
from FlaskRTBCTF.config import Config, LOGGING
import os

db = SQLAlchemy()
Expand All @@ -26,9 +26,13 @@ def create_app(config_class=Config):
# Add model views
from FlaskRTBCTF.admin.views import MyModelView
from FlaskRTBCTF.models import User, Score, Notification
if LOGGING:
from FlaskRTBCTF.models import Logs
admin_manager.add_view(MyModelView(User, db.session))
admin_manager.add_view(MyModelView(Score, db.session))
admin_manager.add_view(MyModelView(Notification, db.session))
if LOGGING:
admin_manager.add_view(MyModelView(Logs, db.session))
mail.init_app(app)

from flask_sslify import SSLify
Expand Down
2 changes: 1 addition & 1 deletion src/FlaskRTBCTF/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def _handle_view(self, name, **kwargs):
abort(403)
#else:
# login
# return redirect(url_for('user.login', next=request.url))
# return redirect(url_for('user.login', next=request.url))
5 changes: 4 additions & 1 deletion src/FlaskRTBCTF/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ class Config:
userScore = 10
rootScore = 20

# NOTE: CHANGE DEFAULT ADMIN CREDENTIALS in create_db.py !!!
# Logging: Set to 'True' to enable Logging in Admin Views

LOGGING = False
# NOTE: CHANGE DEFAULT ADMIN CREDENTIALS in create_db.py !!!
23 changes: 20 additions & 3 deletions src/FlaskRTBCTF/ctf/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from flask import Blueprint, render_template, flash, request
from flask_login import current_user, login_required
from FlaskRTBCTF import db, bcrypt
from FlaskRTBCTF.config import organization, box, userHash, rootHash, userScore, rootScore, LOGGING
from FlaskRTBCTF.models import User, Score
if LOGGING:
from FlaskRTBCTF.models import Logs
from FlaskRTBCTF.ctf.forms import UserHashForm, RootHashForm
from FlaskRTBCTF.config import organization, box, userHash, rootHash, userScore, rootScore
from datetime import datetime

ctf = Blueprint('ctf', __name__)
Expand All @@ -29,6 +31,13 @@ def scoreboard():
@ctf.route("/machine")
@login_required
def machine():
user = User.query.get(current_user.id)
if user.visitedMachine is False and user.isAdmin is False:
user.visitedMachine = True
if LOGGING:
log = Logs.query.get(current_user.id)
log.machineVisitTime = datetime.utcnow()
db.session.commit()
userHashForm = UserHashForm()
rootHashForm = RootHashForm()
return render_template('machine.html', userHashForm=userHashForm,
Expand All @@ -50,7 +59,11 @@ def validateRootHash():
score.rootHash = True
score.points += rootScore
score.timestamp = datetime.utcnow()
score.rootSubmissionIP = request.access_route[0]
if LOGGING:
log = Logs.query.get(current_user.id)
log.rootSubmissionIP = request.access_route[0]
log.rootSubmissionTime = datetime.utcnow()
log.rootOwnTime = str(log.rootSubmissionTime - log.machineVisitTime)
db.session.commit()
flash("Congrats! correct system hash.", "success")
else:
Expand All @@ -76,7 +89,11 @@ def validateUserHash():
score.userHash = True
score.points += userScore
score.timestamp = datetime.utcnow()
score.userSubmissionIP = request.access_route[0]
if LOGGING:
log = Logs.query.get(current_user.id)
log.userSubmissionIP = request.access_route[0]
log.userSubmissionTime = datetime.utcnow()
log.userOwnTime = str(log.userSubmissionTime - log.machineVisitTime)
db.session.commit()
flash("Congrats! correct user hash.", "success")
else:
Expand Down
27 changes: 24 additions & 3 deletions src/FlaskRTBCTF/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
''' Models '''

from flask import current_app
from flask import request
from FlaskRTBCTF.config import LOGGING
from FlaskRTBCTF import db, login_manager
from flask_login import UserMixin
from datetime import datetime
Expand All @@ -20,7 +20,10 @@ class User(db.Model, UserMixin):
password = db.Column(db.String(60), nullable=False)
confirmed_at = db.Column(db.DateTime(), default=datetime.utcnow)
isAdmin = db.Column(db.Boolean, default=False)
visitedMachine = db.Column(db.Boolean, default=False)
score = db.relationship('Score', backref='user', lazy=True, uselist=False)
if LOGGING:
logs = db.relationship('Logs', backref='user', lazy=True, uselist=False)

def get_reset_token(self, expires_sec=1800):
s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
Expand Down Expand Up @@ -48,8 +51,7 @@ class Score(db.Model):
rootHash = db.Column(db.Boolean, default=False)
points = db.Column(db.Integer)
timestamp = db.Column(db.DateTime(), default=datetime.utcnow)
rootSubmissionIP = db.Column(db.String)
userSubmissionIP = db.Column(db.String)


def __repr__(self):
return f"Score('{self.user_id}', '{self.points}')"
Expand All @@ -65,3 +67,22 @@ class Notification(db.Model):

def __repr__(self):
return f"Notif('{self.title}', '{self.body}')"


''' Logging Table '''
if LOGGING:
class Logs(db.Model):
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, primary_key=True)
accountCreationTime = db.Column(db.DateTime, nullable=False)
machineVisitTime = db.Column(db.DateTime, nullable=True)
userSubmissionTime = db.Column(db.DateTime, nullable=True)
rootSubmissionTime = db.Column(db.DateTime, nullable=True)
userOwnTime = db.Column(db.String, nullable=True)
rootOwnTime = db.Column(db.String, nullable=True)
userSubmissionIP = db.Column(db.String, nullable=True)
rootSubmissionIP = db.Column(db.String, nullable=True)

def __repr__(self):
return f"Logs('{self.user_id}','{self.machineVisitTime}','{self.userSubmissionTime}'," \
f"'{self.rootSubmissionTime}','{self.userOwnTime}','{self.rootOwnTime}','{self.userSubmissionIP}," \
f" '{self.rootSubmissionIP}'"
11 changes: 9 additions & 2 deletions src/FlaskRTBCTF/users/routes.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from flask import render_template, url_for, flash, redirect, request, Blueprint
from flask_login import login_user, current_user, logout_user, login_required
from FlaskRTBCTF import db, bcrypt
from FlaskRTBCTF.config import organization, LOGGING
from datetime import datetime
from FlaskRTBCTF.models import User, Score
if LOGGING:
from FlaskRTBCTF.models import Logs
from FlaskRTBCTF.users.forms import (RegistrationForm, LoginForm, UpdateAccountForm,
RequestResetForm, ResetPasswordForm)
from FlaskRTBCTF.users.utils import send_reset_email
from FlaskRTBCTF.config import organization

users = Blueprint('users', __name__)

Expand All @@ -21,8 +24,12 @@ def register():
hashed_password = bcrypt.generate_password_hash(
form.password.data).decode('utf-8')
user = User(username=form.username.data,
email=form.email.data, password=hashed_password)
email=form.email.data, password=hashed_password, visitedMachine=False)
score = Score(user=user, userHash=False, rootHash=False, points=0)
if LOGGING:
log = Logs(user=user, accountCreationTime=datetime.utcnow(), machineVisitTime=None, userSubmissionTime=None,
rootSubmissionTime=None, userSubmissionIP=None, rootSubmissionIP=None)
db.session.add(log)
db.session.add(user)
db.session.add(score)
db.session.commit()
Expand Down
3 changes: 2 additions & 1 deletion src/create_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
email='[email protected]',
password=bcrypt.generate_password_hash('admin').decode('utf-8'),
confirmed_at=datetime.datetime.now(),
isAdmin = True
isAdmin = True,
visitedMachine = True
)
admin_score = Score(user=admin_user, userHash=False, rootHash=False, points=0)
db.session.add(admin_user)
Expand Down
2 changes: 1 addition & 1 deletion src/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

WORKERS=4 # change here to the change number of workers

echo "Starting CTFd"
echo "Starting RTB-CTFd"
exec gunicorn 'FlaskRTBCTF:create_app()' \
--bind '0.0.0.0:8080' \
--workers $WORKERS
1 change: 1 addition & 0 deletions src/run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from FlaskRTBCTF import create_app


app = create_app()

if __name__ == '__main__':
Expand Down

0 comments on commit 5a3a084

Please sign in to comment.