Skip to content

Commit

Permalink
Merge pull request #98 from jdabtieu/install-script
Browse files Browse the repository at this point in the history
Install script
  • Loading branch information
jdabtieu authored May 16, 2021
2 parents f484f0f + 4e9d0eb commit 1ee8be6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
32 changes: 32 additions & 0 deletions INSTALL.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
cd src
echo "Installing dependencies..."
pip3 install -r requirements.txt
echo "Creating database..."
sqlite3 database.db << EOF
CREATE TABLE 'users' ('id' integer PRIMARY KEY NOT NULL, 'username' varchar(20) NOT NULL, 'password' varchar(64) NOT NULL, 'email' varchar(128), 'join_date' datetime NOT NULL DEFAULT (0), 'admin' boolean NOT NULL DEFAULT (0), 'banned' boolean NOT NULL DEFAULT (0), 'verified' boolean NOT NULL DEFAULT (0), 'twofa' boolean NOT NULL DEFAULT (0));
CREATE TABLE 'submissions' ('sub_id' integer PRIMARY KEY NOT NULL, 'date' datetime NOT NULL,'user_id' integer NOT NULL,'problem_id' varchar(32) NOT NULL,'contest_id' varchar(32), 'correct' boolean NOT NULL, 'submitted' text NOT NULL DEFAULT(''));
CREATE TABLE 'problems' ('id' varchar(64) NOT NULL, 'name' varchar(256) NOT NULL, 'point_value' integer NOT NULL DEFAULT (0), 'category' varchar(64), 'flag' varchar(256) NOT NULL, 'draft' boolean NOT NULL DEFAULT(0));
CREATE TABLE 'contests' ('id' varchar(32) NOT NULL, 'name' varchar(256) NOT NULL, 'start' datetime NOT NULL, 'end' datetime NOT NULL, 'scoreboard_visible' boolean NOT NULL DEFAULT (1));
CREATE TABLE 'announcements' ('id' integer PRIMARY KEY NOT NULL, 'name' varchar(256) NOT NULL, 'date' datetime NOT NULL);
CREATE TABLE 'contest_users' ('contest_id' varchar(32) NOT NULL, 'user_id' integer NOT NULL, 'points' integer NOT NULL DEFAULT (0) , 'lastAC' datetime);
CREATE TABLE 'contest_solved' ('contest_id' varchar(32) NOT NULL, 'user_id' integer NOT NULL, 'problem_id' varchar(64) NOT NULL);
CREATE TABLE 'contest_problems' ('contest_id' varchar(32) NOT NULL, 'problem_id' varchar(64) NOT NULL, 'name' varchar(256) NOT NULL, 'point_value' integer NOT NULL DEFAULT(0), 'category' varchar(64), 'flag' varchar(256) NOT NULL, 'draft' boolean NOT NULL DEFAULT(0), 'score_min' integer NOT NULL DEFAULT(0), 'score_max' integer NOT NULL DEFAULT(0), 'score_users' integer NOT NULL DEFAULT(-1));
CREATE TABLE 'problem_solved' ('user_id' integer NOT NULL, 'problem_id' varchar(64) NOT NULL);
INSERT INTO 'users' VALUES(1, 'admin', 'pbkdf2:sha256:150000\$XoLKRd3I\$2dbdacb6a37de2168298e419c6c54e768d242aee475aadf1fa9e6c30aa02997f', 'e', datetime('now'), 1, 0, 1, 0);
EOF
echo "Finishing setup..."
mkdir logs dl metadata metadata/contests metadata/problems metadata/announcements
chmod +x daily_tasks.py
python3 daily_tasks.py
cp default_settings.py settings.py
echo "Configuring settings..."
echo "Admin Email: "
read ADMIN_EMAIL
sqlite3 database.db << EOF
UPDATE 'users' SET email='$ADMIN_EMAIL' WHERE id=1;
EOF
nano settings.py
echo "Running application as debug..."
export FLASK_APP=application.py
flask run
22 changes: 14 additions & 8 deletions docs/INSTALL.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Installation
Prerequisites: Python 3, SQLite 3<br>
Prerequisites: Python 3, SQLite 3

Although CTFOJ can run on Linux, Windows, and MacOS, it is recommended to run it on a modern Linux distribution.

It is recommended to create a venv (virtual environment) first.
Expand All @@ -10,6 +11,9 @@ The setup process involves 3 main steps:
2. Create database
3. Configure application

# Options
You can either follow the steps below, or run the INSTALL.sh script provided and skip to the step "Logging in for the first time" at the bottom of this page.

&nbsp;
1.
```bash
Expand All @@ -36,13 +40,19 @@ INSERT INTO 'users' VALUES(1, 'admin', 'pbkdf2:sha256:150000$XoLKRd3I$2dbdacb6a3
3.
```bash
$ mkdir logs dl metadata metadata/contests metadata/problems metadata/announcements
$ chmod +x daily_tasks.py
$ python3 daily_tasks.py
$ cp default_settings.py settings.py
$ nano settings.py
```
In settings.py, you should add your email credentials as indicated by default_settings.py. Additionally, you may change the other email settings if you use a SMTP provider other than Gmail. Next, you should choose whether to use a CAPTCHA or not, and add your hCaptcha site and secret keys if you are using a CAPTCHA. Finally, you should add a custom name for your club and change any other settings that you wish to change.

Next, you should set up cron to run daily_tasks.py every day. Make sure daily_tasks.py is executable by running `chmod +x daily_tasks.py`. Then, run `crontab -e` and paste the following into the file: `0 0 * * * cd PATH_TO_INSTALL && ./daily_tasks.py`, making sure you replace `PATH_TO_INSTALL` with the installation path.
4. Now you should change the admin email manually so that you can reset your password in the future through the web app.
```sql
$ sqlite3 database.db
sqlite3>
UPDATE 'users' SET email='YOUR EMAIL HERE' WHERE id=1;
```

# Running in Debug Mode
```
Expand All @@ -56,12 +66,6 @@ Do not expose the app to the web using debug mode. You should run the app throug
# Logging in for the first time
An admin account has been created in step 2. You can log in to it using the credentials `admin:CTFOJadmin`. Make sure you change your password immediately after logging in. Enabling 2FA is also recommended for the admin account. You can change your password and enable 2FA through the settings page.

You should also change the admin email manually so that you can reset your password in the future through the web app.
```sql
$ sqlite3 database.db
sqlite3>
UPDATE 'users' SET email='YOUR EMAIL HERE' WHERE id=1;
```
Furthermore, when regular users log in for the first time, they will be directed to a helloworld problem. You should create a helloworld problem as a welcome/landing page. This problem must have an id of 'helloworld', without the single quotes. You can do this on the 'Create Problem' page in the admin toolbar, once logged in. Markdown is supported. See below for an example helloworld problem:
```
**Welcome to CTF Club!** In each problem, you must find a flag hidden somewhere on the problem page.
Expand All @@ -71,3 +75,5 @@ The flag for this problem is: `CTF{your_first_ctf_flag}`

# Optional Steps
You may optionally replace the default favicon.png file in the static folder with another icon of your choice (must be named favicon.png

You should also set up cron to run daily_tasks.py every day. Run `crontab -e` and paste the following into the file: `0 0 * * * cd PATH_TO_INSTALL && ./daily_tasks.py`, making sure you replace `PATH_TO_INSTALL` with the installation path.
Empty file modified src/daily_tasks.py
100644 → 100755
Empty file.
18 changes: 13 additions & 5 deletions src/default_settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import secrets
import sys

# The secret key is located in secret_key.txt by default
try:
with open('secret_key.txt', 'r') as file:
secret_key = file.readline().strip()
Expand All @@ -12,19 +13,26 @@
file.write(secret)
secret_key = file.readline().strip()
SECRET_KEY = secret_key

TEMPLATES_AUTO_RELOAD = True
SESSION_PERMANENT = False
SESSION_TYPE = "filesystem"
MAIL_SERVER = "smtp.gmail.com" # configured to work with gmail
SESSION_COOKIE_SAMESITE = 'Lax'

# Configure your smtp server here
MAIL_SERVER = "smtp.gmail.com"
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = "your email address"
MAIL_PASSWORD = "your email password"
MAIL_DEFAULT_SENDER = ("sender name", "sender email")
CLUB_NAME = "your club name"
LOGGING_FILE_LOCATION = 'logs/application.log'
SESSION_COOKIE_SAMESITE = 'Lax'
USE_CAPTCHA = True

# Configure your hcaptcha settings here
USE_CAPTCHA = False
HCAPTCHA_SECRET = 0xdeadbeef
HCAPTCHA_SITE = 'site_key'

# Configure other settings here
CLUB_NAME = "your club name"
LOGGING_FILE_LOCATION = 'logs/application.log'
USE_HOMEPAGE = False

0 comments on commit 1ee8be6

Please sign in to comment.