|
import os |
|
import streamlit as st |
|
import pandas as pd |
|
import time |
|
from getmac import get_mac_address as gma |
|
from requests import get |
|
import platform, uuid, psutil |
|
import json |
|
|
|
|
|
|
|
import hashlib |
|
|
|
|
|
sourceFileDir = os.path.dirname(os.path.abspath(__file__)) |
|
os.chdir(sourceFileDir) |
|
|
|
def make_hashes(password): |
|
return hashlib.sha256(str.encode(password)).hexdigest() |
|
|
|
def check_hashes(password, hashed_text): |
|
if make_hashes(password) == hashed_text: |
|
return hashed_text |
|
return False |
|
|
|
|
|
import sqlite3 |
|
conn = sqlite3.connect('data.db') |
|
c = conn.cursor() |
|
|
|
|
|
def create_user_table(): |
|
c.execute('CREATE TABLE IF NOT EXISTS users(user_id INTEGER PRIMARY KEY AUTOINCREMENT,\ |
|
username TEXT NOT NULL, password TEXT NOT NULL)') |
|
|
|
def add_user_data(username, password): |
|
c.execute('INSERT INTO users(username, password) VALUES (?,?)',(username,password)) |
|
conn.commit() |
|
|
|
def login_user(username, password): |
|
c.execute('SELECT * FROM users WHERE username =? AND password = ?',(username,password)) |
|
data = c.fetchall() |
|
return data |
|
|
|
def view_all_users(): |
|
c.execute('SELECT * FROM users') |
|
data = c.fetchall() |
|
return data |
|
|
|
def create_login_table(): |
|
|
|
|
|
c.execute('CREATE TABLE IF NOT EXISTS login(login_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL,\ |
|
login_time TEXT NOT NULL, login_duration TEXT NOT NULL,\ |
|
device_name TEXT, device_uuid TEXT, mac_address TEXT, device_vendor TEXT, device_version TEXT, device_model TEXT, device_ram TEXT,\ |
|
ip_v6 TEXT, ip_v4 TEXT, ip_country TEXT, ip_region TEXT, ip_city TEXT, ip_lat TEXT, ip_lon TEXT, ip_timezone TEXT, isp_name TEXT, isp_org TEXT, isp_as TEXT)') |
|
|
|
def add_login_data(username, login_time, login_duration,\ |
|
device_name, device_uuid, mac_address, device_vendor, device_version, device_model, device_ram,\ |
|
ip_v6, ip_v4, ip_country, ip_region, ip_city, ip_lat, ip_lon, ip_timezone, isp_name, isp_org, isp_as): |
|
|
|
|
|
|
|
|
|
|
|
c.execute('INSERT INTO login(username, login_time, login_duration,\ |
|
device_name, device_uuid, mac_address, device_vendor, device_version, device_model, device_ram,\ |
|
ip_v6, ip_v4, ip_country, ip_region, ip_city, ip_lat, ip_lon, ip_timezone, isp_name, isp_org, isp_as)\ |
|
VALUES (?, ?, ?, \ |
|
?, ?, ?, ?, ?, ?, ?, \ |
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',\ |
|
(username, login_time, login_duration,\ |
|
device_name, device_uuid, mac_address, device_vendor, device_version, device_model, device_ram,\ |
|
ip_v6, ip_v4, ip_country, ip_region, ip_city, ip_lat, ip_lon, ip_timezone, isp_name, isp_org, isp_as)) |
|
|
|
conn.commit() |
|
|
|
|
|
def export_csv(): |
|
|
|
|
|
db_df = pd.read_sql_query('SELECT * FROM login', conn) |
|
db_df.to_csv('login.csv', index=False) |
|
|
|
|
|
db_df = pd.read_sql_query('SELECT * FROM users', conn) |
|
db_df.to_csv('users.csv', index=False) |
|
|
|
def get_from_api(url, value=""): |
|
|
|
|
|
response = get(url + value) |
|
if response.status_code != 200: |
|
raise Exception("[!] Invalid request!") |
|
|
|
return response.content.decode() |
|
|
|
def get_ip_info(ip_v4): |
|
|
|
|
|
isp = get_from_api("http://ip-api.com/json/", ip_v4) |
|
|
|
|
|
isp = json.loads(isp) |
|
|
|
|
|
ip_country = isp["country"] |
|
ip_region = isp["regionName"] |
|
ip_city = isp["city"] |
|
ip_lat = isp["lat"] |
|
ip_lon = isp["lon"] |
|
ip_timezone = isp["timezone"] |
|
isp_name = isp["isp"] |
|
isp_org = isp["org"] |
|
isp_as = isp["as"] |
|
|
|
return ip_country, ip_region, ip_city, ip_lat, ip_lon, ip_timezone, isp_name, isp_org, isp_as |
|
|
|
def main(): |
|
"""Banking Advanced Authentication Module""" |
|
|
|
menu = ["Home", "SignUp", "User Login", "Testing Tool"] |
|
choice = st.sidebar.selectbox("Menu",menu) |
|
|
|
if choice == "Home": |
|
st.subheader("Banking Advanced Authentication Module") |
|
|
|
elif choice == "SignUp": |
|
st.subheader("Create New Account") |
|
new_user = st.text_input("Username") |
|
new_password = st.text_input("Password",type='password') |
|
|
|
if st.button("Signup"): |
|
create_user_table() |
|
add_user_data(new_user,make_hashes(new_password)) |
|
st.success("You have successfully created a valid Account") |
|
st.info("Go to Login Menu to login") |
|
|
|
elif choice == "User Login": |
|
|
|
col1, col2, col3 = st.columns(3) |
|
|
|
with col1: |
|
st.image("img/blank.png") |
|
|
|
with col2: |
|
st.image("img/blank.png") |
|
|
|
with col3: |
|
st.image("img/Standard_Chartered.png") |
|
|
|
with st.container(): |
|
st.subheader("Login") |
|
time_start = time.time() |
|
username = st.text_input("Username") |
|
password = st.text_input("Password",type='password') |
|
login = st.button("Login") |
|
|
|
if login: |
|
|
|
login_time = time.time() |
|
hashed_pswd = make_hashes(password) |
|
result = login_user(username,check_hashes(password,hashed_pswd)) |
|
|
|
if result: |
|
|
|
login_duration = login_time - time_start |
|
|
|
|
|
device_name = platform.node() |
|
device_uuid = uuid.getnode() |
|
mac_address = gma() |
|
device_vendor = get_from_api("https://api.macvendors.com/", mac_address) |
|
device_version = platform.version() |
|
device_model = platform.platform() |
|
device_ram = str(round(psutil.virtual_memory().total / (1024.0 **3)))+" GB" |
|
|
|
|
|
ip_v6 = get_from_api('https://ident.me') |
|
ip_v4 = get_from_api('https://api.ipify.org') |
|
ip_country, ip_region, ip_city, ip_lat, ip_lon, ip_timezone, isp_name, isp_org, isp_as = get_ip_info(ip_v4) |
|
|
|
|
|
create_login_table() |
|
|
|
verification = False |
|
|
|
|
|
|
|
|
|
|
|
|
|
verification = True |
|
|
|
if verification: |
|
|
|
add_login_data(username, login_time, login_duration,\ |
|
device_name, device_uuid, mac_address, device_vendor, device_version, device_model, device_ram,\ |
|
ip_v6, ip_v4, ip_country, ip_region, ip_city, ip_lat, ip_lon, ip_timezone, isp_name, isp_org, isp_as) |
|
|
|
|
|
|
|
|
|
st.success("Logged In as {}".format(username)) |
|
|
|
task = st.selectbox("Task",["Add Post","Analytics","Profiles"]) |
|
if task == "Add Post": |
|
st.subheader("Add Your Post") |
|
|
|
elif task == "Analytics": |
|
st.subheader("Analytics") |
|
elif task == "Profiles": |
|
st.subheader("User Profiles") |
|
user_result = view_all_users() |
|
clean_db = pd.DataFrame(user_result,columns=["Username","Password"]) |
|
st.dataframe(clean_db) |
|
else: |
|
st.warning("Not the real user => Fail to login.") |
|
|
|
else: |
|
st.warning("Incorrect Username/Password") |
|
|
|
else: |
|
st.subheader("Testing tool") |
|
|
|
if __name__ == '__main__': |
|
main() |
|
|