refactoring in flask-soc-site-v2
This commit is contained in:
parent
079da00a53
commit
79d3323804
159
app.py
159
app.py
|
@ -1,115 +1,44 @@
|
||||||
# from dotenv import load_dotenv
|
import secrets
|
||||||
import secrets
|
|
||||||
import socket
|
# TODO
|
||||||
import uuid
|
# FIX
|
||||||
from logging.config import dictConfig
|
# WARNING
|
||||||
from pprint import pprint
|
|
||||||
from urllib.parse import urlparse
|
from flask import Flask, flash, redirect, render_template, request, session, url_for
|
||||||
|
from markupsafe import escape
|
||||||
from flask import Flask, flash, redirect, render_template, request, session, url_for
|
|
||||||
from markupsafe import escape
|
import host_lookup
|
||||||
|
import upload
|
||||||
# from io import StringIO
|
|
||||||
from validators import domain, email, ipv4, ipv6, url
|
app = Flask(__name__)
|
||||||
|
generate_secret = secrets.token_urlsafe(16)
|
||||||
from constants import *
|
app.secret_key = generate_secret
|
||||||
from host_lookup import abuseipdb, metadata, spf_dmarc, virustotal_api_test
|
|
||||||
from upload import csv_parse
|
|
||||||
|
@app.route("/")
|
||||||
# import csv
|
def index():
|
||||||
|
return redirect(url_for("lookup"))
|
||||||
dictConfig(LOGCONF)
|
|
||||||
|
|
||||||
# put this in a .flaskenv file: https://dev.to/kubona_my/dealing-with-environment-variables-in-flask-o1
|
@app.route("/lookup", methods=["GET", "POST"])
|
||||||
app = Flask(__name__)
|
def lookup():
|
||||||
generate_secret = secrets.token_urlsafe(16)
|
hosts = []
|
||||||
app.secret_key = generate_secret
|
if request.method == "GET":
|
||||||
# app.debug = True
|
return render_template("lookup.html")
|
||||||
|
elif request.method == "POST" and "host" in request.form:
|
||||||
|
user_input = escape(request.form.get("host").strip())
|
||||||
class Info(object):
|
if len(user_input) >= 1:
|
||||||
def __init__(self, host):
|
hosts = host_lookup.process_input(user_input)
|
||||||
self.host = host
|
return render_template("results.html", hosts=hosts)
|
||||||
self.ip_address = None
|
else:
|
||||||
self.host_type = metadata.check(self.host)
|
flash("YA DONE FUCKED UP", "error")
|
||||||
self.metadata = metadata.lookup(self.host)
|
return render_template("lookup.html")
|
||||||
self.emailsec = ()
|
elif request.method == "POST" and "file" in request.files:
|
||||||
self.vt = {}
|
file = request.files["file"]
|
||||||
self.abuseipdb = {}
|
extracted = upload.extract(file)
|
||||||
|
hosts = host_lookup.process_file(extracted)
|
||||||
def lookup(host):
|
return render_template("results.html", hosts=hosts)
|
||||||
result = Info(host)
|
|
||||||
if result.host_type == DOMAIN:
|
|
||||||
result.ip_address = socket.gethostbyname(host)
|
if __name__ == "__main__":
|
||||||
result.emailsec = spf_dmarc.lookup(host)
|
app.run(debug=True)
|
||||||
result.vt = virustotal_api_test.analyse(result.host, result.host_type)
|
|
||||||
result.abuseipdb = abuseipdb.analyse(result.ip_address)
|
|
||||||
print("[DEBUGGING]")
|
|
||||||
pprint(result.emailsec)
|
|
||||||
elif result.host_type == URL:
|
|
||||||
result.domain = urlparse(host).netloc
|
|
||||||
result.ip_address = socket.gethostbyname(result.domain)
|
|
||||||
result.vt = virustotal_api_test.analyse(result.host, result.host_type)
|
|
||||||
result.abuseipdb = abuseipdb.analyse(result.ip_address)
|
|
||||||
elif result.host_type == IPV4 or IPV6:
|
|
||||||
result.vt = virustotal_api_test.analyse(result.host, result.host_type)
|
|
||||||
result.abuseipdb = abuseipdb.analyse(host)
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def index():
|
|
||||||
# logging example taken from https://betterstack.com/community/guides/logging/how-to-start-logging-with-flask/
|
|
||||||
session["ctx"] = {"request_id": str(uuid.uuid4())}
|
|
||||||
app.logger.info("A user visited the home page >>> %s", session["ctx"])
|
|
||||||
|
|
||||||
return redirect(url_for("lookup"))
|
|
||||||
|
|
||||||
|
|
||||||
# refactor to handle form requests better: https://www.digitalocean.com/community/tutorials/how-to-use-web-forms-in-a-flask-application
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/lookup", methods=["GET", "POST"])
|
|
||||||
def lookup():
|
|
||||||
host = ""
|
|
||||||
host = escape(request.form.get("host"))
|
|
||||||
session["ctx"] = {"request_id": str(uuid.uuid4())}
|
|
||||||
# figure out how to start a session, maybe with a variable?
|
|
||||||
# variable = session.get('something')
|
|
||||||
if request.method == "GET":
|
|
||||||
return render_template("lookup_options.html")
|
|
||||||
elif request.method == "POST" and "host" in request.form:
|
|
||||||
host = ""
|
|
||||||
host = escape(request.form.get("host"))
|
|
||||||
session["ctx"] = {"request_id": str(uuid.uuid4())}
|
|
||||||
app.logger.info(
|
|
||||||
"A user submitted a host to look up. | host: %s >>> %s",
|
|
||||||
host,
|
|
||||||
session["ctx"],
|
|
||||||
)
|
|
||||||
if not host:
|
|
||||||
flash("Try again", "error")
|
|
||||||
return render_template("lookup_options.html")
|
|
||||||
elif host:
|
|
||||||
result = Info.lookup(host)
|
|
||||||
return render_template(
|
|
||||||
"lookup_options.html",
|
|
||||||
host=result.host,
|
|
||||||
host_type=result.host_type,
|
|
||||||
result=result,
|
|
||||||
)
|
|
||||||
elif request.method == "POST" and "file" in request.files:
|
|
||||||
file = request.files["file"]
|
|
||||||
extracted = csv_parse.extract(file)
|
|
||||||
results = []
|
|
||||||
for host in extracted:
|
|
||||||
results.append(Info.lookup(host))
|
|
||||||
print(results)
|
|
||||||
return render_template("lookup_options.html")
|
|
||||||
else:
|
|
||||||
flash("No file!", "error")
|
|
||||||
return render_template("lookup_options.html")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run(debug=True)
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user