Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, redirect, url_for, request, session
- from flask_mysqldb import MySQL
- import MySQLdb.cursors
- from app import app
- import time
- import subprocess
- import random
- import re
- app.config['MYSQL_HOST'] = '10.5.0.10'
- app.config['MYSQL_USER'] = 'dbpad'
- app.config['MYSQL_PASSWORD'] = 'padteamc03'
- app.config['MYSQL_DB'] = 'team_c'
- SERVER_NAME = 'docker.net:443'
- app.secret_key = '123'
- mysql = MySQL(app)
- @app.route('/', methods= ['GET', 'POST'])
- def index():
- # Output message if something goes wrong...
- msg = ''
- # Check if "username", "password" and "email" POST requests exist (user submitted form)
- if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
- # Create variables for easy access
- username = request.form['username']
- password = request.form['password']
- elif request.method == 'POST':
- # Form is empty... (no POST data)
- msg = 'Please fill out the form!'
- # Show registration form with message (if any)
- return render_template('register.html', msg=msg)
- # Check if account exists using MySQL
- if request.method == 'POST':
- cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
- cursor.execute('SELECT * FROM user WHERE username = %s', (username,))
- account = cursor.fetchone()
- # If account exists show error and validation checks
- if account:
- msg = 'Account already exists!'
- elif not re.match(r'[A-Za-z0-9]+', username):
- msg = 'Username must contain only characters and numbers!'
- elif not username or not password:
- msg = 'Please fill out the form!'
- else:
- # Account doesnt exists and the form data is valid, now insert new account into accounts table
- cursor.execute('INSERT INTO user VALUES (%s, %s)', (username, password,))
- mysql.connection.commit()
- msg = 'You have successfully registered!'
- return redirect(url_for('login'))
- return render_template('register.html', msg=msg)
- @app.route('/login', methods= ['GET', 'POST'])
- def login():
- # Output message if something goes wrong...
- msg = ''
- # Check if "username" and "password" POST requests exist (user submitted form)
- if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
- # Create variables for easy access
- username = request.form['username']
- password = request.form['password']
- # Check if account exists using MySQL
- cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
- cursor.execute('SELECT * FROM user WHERE username = %s AND password = %s', (username, password,))
- # Fetch one record and return result
- account = cursor.fetchone()
- # If account exists in user table in out database
- if account:
- # Create session data, we can access this data in other routes
- session['loggedin'] = True
- session['username'] = account['username']
- # Redirect to home page
- return redirect(url_for('welcome'))
- else:
- # Account doesnt exist or username/password incorrect
- msg = 'Incorrect username/password!'
- # Show the login form with message (if any)
- return render_template('login.html', msg=msg)
- @app.route('/welcome', methods= ['GET', 'POST'])
- def welcome():
- print(session.keys())
- if session['loggedin'] == True:
- # User is loggedin show them the home page
- return render_template('welcome.html', htmlvar=session['username'])
- # User is not loggedin redirect to login page
- return redirect(url_for('login'))
- @app.route('/challenges')
- def challenges():
- if session['loggedin'] == True:
- return render_template('challenges.html')
- @app.route('/challenge1')
- def challenge1():
- while True:
- eport = str(random.choice(range(50500, 51000))) #zelf range bepalen
- proc = subprocess.Popen(['python3', '/var/www/apache-flask/scripts/challenge1.py', eport])
- returncode = proc.wait()
- if returncode == 0:
- break
- #print(stdout)
- time.sleep(3)
- return redirect(f'http://localhost:{eport}')
- @app.route('/challenge2')
- def challenge2():
- while True:
- eport = str(random.choice(range(51000, 51500))) #zelf range bepalen
- proc = subprocess.Popen(['python3', '/var/www/apache-flask/scripts/challenge2.py', eport])
- returncode = proc.wait()
- if returncode == 0:
- break
- time.sleep(3)
- return redirect(f'http://localhost:{eport}')
- @app.route('/challenge3')
- def challenge3():
- while True:
- eport = str(random.choice(range(51500, 52000))) #zelf range bepalen
- proc = subprocess.Popen(['python3', '/var/www/apache-flask/scripts/challenge3.py', eport])
- returncode = proc.wait()
- if returncode == 0:
- break
- time.sleep(3)
- return redirect(f'http://localhost:{eport}')
- @app.route('/challenge4')
- def challenge4():
- while True:
- eport = str(random.choice(range(52000, 52500))) #zelf range bepalen
- proc = subprocess.Popen(['python3', '/var/www/apache-flask/scripts/challenge4.py', eport])
- returncode = proc.wait()
- if returncode == 0:
- break
- time.sleep(3)
- return redirect(f'http://localhost:{eport}')
- @app.route('/challenge5')
- def challenge5():
- while True:
- eport = str(random.choice(range(52500, 53000))) #zelf range bepalen
- proc = subprocess.Popen(['python3', '/var/www/apache-flask/scripts/challenge5.py', eport])
- returncode = proc.wait()
- if returncode == 0:
- break
- time.sleep(3)
- return redirect(f'http://localhost:{eport}')
- @app.route('/challenge6')
- def challenge6():
- while True:
- eport = str(random.choice(range(50500, 51000))) #zelf range bepalen
- proc = subprocess.Popen(['python3', '/var/www/apache-flask/scripts/challenge6.py', eport])
- returncode = proc.wait()
- if returncode == 0:
- break
- time.sleep(3)
- return redirect(f'http://localhost:{eport}')
- @app.route('/nonoflag')
- def flag():
- return render_template('flag_page.html')
- if __name__ == "__main__":
- app.run(ssl_context=('certificate.pem', 'key.pem'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement