From 21f383a9efc3aaed7c3427b3bdcdfdd0bbac8f0d Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 18 Mar 2025 11:22:22 -0600 Subject: [PATCH 001/127] Bringing over John's CyberCafe prototype work. --- Backend/CyberCafe_Daemon.sh | 204 ---------------- Backend/Cybercafe_commandLine.sh | 146 ++++++++++++ Backend/Cybercafe_daemon.sh | 49 ++++ Backend/Cybercafe_internetSessionFunctions.sh | 221 ++++++++++++++++++ Backend/Cybercafe_setupFunctions.sh | 134 +++++++++++ Backend/Cybercafe_testbed.sh | 52 +++++ Backend/archive/CyberCafe_UpdateUserUsage.sh | 7 + .../UserModify_IptablesRules_AtomicScript.sh | 46 ++++ Database/CyberCafe_Database.db | Bin 0 -> 28672 bytes Database/CyberCafe_Database_Schema.sql | 51 ++++ Database/CyberCafe_DropAllTables.sql | 4 + Database/CyberCafe_Example.db | Bin 61440 -> 0 bytes Database/CyberCafe_Schema.sql | 89 ------- Database/RMIT_CyberCafe_Schema.sql | 47 ---- Database/createDatabaseEntries.sh | 58 +++++ Website_ASU2024/about/index.php | 10 + Website_ASU2024/createaccount/index.php | 10 + Website_ASU2024/home/index.php | 84 +++++++ Website_ASU2024/index.php | 12 + Website_ASU2024/login/index.php | 141 +++++++++++ 20 files changed, 1025 insertions(+), 340 deletions(-) delete mode 100644 Backend/CyberCafe_Daemon.sh create mode 100644 Backend/Cybercafe_commandLine.sh create mode 100644 Backend/Cybercafe_daemon.sh create mode 100644 Backend/Cybercafe_internetSessionFunctions.sh create mode 100644 Backend/Cybercafe_setupFunctions.sh create mode 100644 Backend/Cybercafe_testbed.sh create mode 100644 Backend/archive/CyberCafe_UpdateUserUsage.sh create mode 100644 Backend/archive/UserModify_IptablesRules_AtomicScript.sh create mode 100644 Database/CyberCafe_Database.db create mode 100644 Database/CyberCafe_Database_Schema.sql create mode 100644 Database/CyberCafe_DropAllTables.sql delete mode 100644 Database/CyberCafe_Example.db delete mode 100644 Database/CyberCafe_Schema.sql delete mode 100644 Database/RMIT_CyberCafe_Schema.sql create mode 100644 Database/createDatabaseEntries.sh create mode 100644 Website_ASU2024/about/index.php create mode 100644 Website_ASU2024/createaccount/index.php create mode 100644 Website_ASU2024/home/index.php create mode 100644 Website_ASU2024/index.php create mode 100644 Website_ASU2024/login/index.php diff --git a/Backend/CyberCafe_Daemon.sh b/Backend/CyberCafe_Daemon.sh deleted file mode 100644 index a198f4c..0000000 --- a/Backend/CyberCafe_Daemon.sh +++ /dev/null @@ -1,204 +0,0 @@ -#!/bin/bash - -### GLOBAL VARIABLES ### -# The status of the hotspot functionality (not necessarily the CyberCafe -# infrastructure. Either 'up' or 'down' -HS_STATUS='down' - -# This should be set to the hotspot's IP once the hotspot has been enabled. When -# disabled, this variable should be blank again. -LOCAL_IP='' - -# Between increments of this value we will not perform a more expensive check of -# the status of the CyberCafe infrastructure. Once passed, we perform the checks -# then reset the counter. In seconds. -REFRESH_TIME=3600 - -# Path to the file used to indicate the time we last did an expensive check of -# the hotspot/CyberCafe infrastructure status. If the file doesn't exist, it's -# assumed we're not ready to act as a CyberCafe router. -STATUS_PATH="/data/data/com.android.myapplication/files/tmp/cybercafe.confirmed" - -# When 'true', we perform the expensive CyberCafe infrastructure check. -TIME_TO_REFRESH=false - - -### FUNCTIONS ### -function check_hotspot_status { - # Does it appear the hotspot is active? - ip add show dev wlan0 | grep 192\.168\.43\. > /dev/null - wlan_ip_status=$? - - if [[ $wlan_ip_status -eq 0 ]]; then - HS_STATUS='up' - - elif [[ $wlan_ip_status -ne 0 && $HS_STATUS == 'up' ]]; then - # Assume hotspot has recently gone done. - HS_STATUS='down' - TIME_TO_REFRESH=true - else - HS_STATUS='down' - TIME_TO_REFRESH=false - fi - - # HS_STATUS is down: TIME_TO_REFRESH is false, HS_STATUS=down - # HS_STATUS is up, but the STATUS_PATH does not exist: Indicate refresh - # HS_STATUS is up, and STATUS_PATH exists: Check STATUS_PATH age - # STATUS_PATH age > REFRESH_TIME: Indicate refresh - # STATUS_PATH age < REFRESH_TIME: Indicate no refresh - - - if [[ $HS_STATUS == 'up' && ! -e $STATUS_PATH ]]; then - TIME_TO_REFRESH=true - - elif [[ $HS_STATUS == 'up' && -e $STATUS_PATH ]]; then - # If the CyberCafe status file exists and the hotspot is up... - # ... calculate how old it is in seconds... - cf_status_path_age=$(echo "$(date +%s) - $(date -r ${STATUS_PATH} +%s)" | bc) - - # ... and if it's older than what we allow (REFRESH_TIME), indicate it's time to refresh - if [[ $cf_status_path_age -gt $REFRESH_TIME ]]; then - TIME_TO_REFRESH=true - else - TIME_TO_REFRESH=false - fi - fi -} - - -function setup_infra { - # We're in hotspot mode but unsure if we're properly configured for CyberCafe operation - LOCAL_IP=$(ip -4 addr show dev wlan0 | grep inet | awk '{print $2}' | cut -d '/' -f 1) - # iptmon tables? - ## iptmon_tx - iptables -t mangle -C FORWARD -j iptmon_tx > /dev/null 2>&1 - if [[ $? -ne 0 ]]; then - echo "Creating table ipmon_tx" - iptables -t mangle -N iptmon_tx - - echo "Adding iptmon_tx to FORWARD chain, 'mangle' table" - iptables -t mangle -A FORWARD -j iptmon_tx - fi - - ## iptmon_rx - iptables -t mangle -C FORWARD -j iptmon_rx > /dev/null 2>&1 - if [[ $? -ne 0 ]]; then - echo "Creating table ipmon_rx" - iptables -t mangle -N iptmon_rx - - echo "Adding iptmon_rx to FORWARD chain, 'mangle' table" - iptables -t mangle -A FORWARD -j iptmon_rx - fi - - # Default iptables redirect - - iptables -t nat -C PREROUTING -j DNAT --to-destination ${LOCAL_IP} > /dev/null 2>&1 - if [[ $? -ne 0 ]]; then - echo "Creating default redirect rule" - iptables -t nat -A PREROUTING -p udp -d ${LOCAL_IP} --dport 53 -j RETURN - iptables -t nat -A PREROUTING -p udp -s 0.0.0.0/32 -d 255.255.255.255/32 --dport 67 -j RETURN - iptables -t nat -A PREROUTING -j DNAT --to-destination ${LOCAL_IP} - fi - - # Traffic Control - tc qdisc show dev wlan0 | grep htb > /dev/null - if [[ $? -ne 0 ]]; then - echo "Creating tc qdisc HTB queues" - tc qdisc add dev wlan0 root handle 1: htb default 10 - tc class add dev wlan0 parent 1: classid 1:1 htb rate 15mbps ceil 15mbps - tc class add dev wlan0 parent 1:1 classid 1:10 htb rate 15mbps ceil 15mbps - tc class add dev wlan0 parent 1:1 classid 1:20 htb rate 100kbps ceil 100kbps - fi - - # Update the STATUS_PATH file - rm -f ${STATUS_PATH} && touch ${STATUS_PATH} - - # Check captive portal HTTPD server - if ! pgrep lighttpd > /dev/null; then - start_captive_webserver - fi -} - - -function shutdown_infra { - printf "%s" "$(date +%T)" && echo ": Shutdown, I suppose" - echo "Stopping captive portal webserver" - pkill lighttpd - - echo "Putting variables back to default values" - LOCAL_IP='' - TIME_TO_REFRESH=false - HS_STATUS='down' - - echo "Removing status path" - rm -f $STATUS_PATH - - echo "Cleaning up iptables" - iptables -t mangle -D FORWARD -j iptmon_tx - iptables -t mangle -D FORWARD -j iptmon_rx - while true; do - iptables -t mangle -D iptmon_rx 1 - if [[ $? == 1 ]]; then - break - fi - done - iptables -t mangle -X iptmon_rx - - while true; do - iptables -t mangle -D iptmon_tx 1 - if [[ $? == 1 ]]; then - break - fi - done - iptables -t mangle -X iptmon_tx - - while true; do - iptables -t nat -D PREROUTING 2 - if [[ $? == 1 ]]; then - break - fi - done - - echo "Cleaning up wlan0 qdisc" - tc qdisc delete dev wlan0 root - - echo - echo "Done." -} - - -function start_captive_webserver { - set -o allexport - source /data/data/com.android.myapplication/files/conf/lighttpd.env - /data/data/com.android.myapplication/files/bin/lighttpd -f /data/data/com.android.myapplication/files/conf/lighttpd.conf -} - -### START OF PROGRAM EXECUTION ### - -# I'm undecided if this is a hack or not. The problem was if this script terminated -# without cleaning up we'd have a current $STATUS_PATH which, as currently configured, -# would let the script go from "Hotspot down" to "Everything good." -rm $STATUS_PATH - -while true; do - check_hotspot_status - - if [[ $HS_STATUS == 'up' ]] && ! $TIME_TO_REFRESH; then - printf "%s" "$(date +%T)" \ - && echo ": Everything is fine, carry on." - elif [[ $HS_STATUS == 'up' ]] && $TIME_TO_REFRESH; then - # In hotspot mode, but status file has expired - printf "%s" "$(date +%T)" \ - && echo ": In hotspot mode, but status file has expired." \ - && echo "Checking CyberCafe infrastructure." - setup_infra - elif [[ $HS_STATUS == 'down' ]] && $TIME_TO_REFRESH; then - # Assume hotspot has recently gone down. Clean up. - shutdown_infra - else - printf "%s" "$(date +%T)" \ - && echo ": Hotspot down. We wait..." - fi - - sleep 60 -done diff --git a/Backend/Cybercafe_commandLine.sh b/Backend/Cybercafe_commandLine.sh new file mode 100644 index 0000000..6f20171 --- /dev/null +++ b/Backend/Cybercafe_commandLine.sh @@ -0,0 +1,146 @@ +#Organization: Grey-box +#Project: Cybercafe +#File: commandLine +#Description: Acts as the interface between the administrator and the Cybercafe backend. + +##INCLUDES## +. ./Cybercafe_setupFunctions.sh +. ./Cybercafe_internetSessionFunctions.sh + +##COMMANDS## +function command_run +{ + #test to see if daemon is already running + ps -eo name,cmdline | grep "bash ./Cybercafe_daemon.sh" | grep -v grep > /dev/null 2>&1 # if this returns 0 it implies that the script exists and is running + if [[ $? -eq 0 ]]; then + echo "Cybercafe infrastructure already running." + else + printf "%s" "$(date +%T)" \ + && echo " Running Cybercafe..." + nohup bash ./Cybercafe_daemon.sh & #run CyberCafe daemon as a seperate process + #$DAEMON_SCRIPT_PID = $(($(ps -eo pid,name,cmdline | grep "bash ./CyberCafe_Daemon.sh" | grep -v grep | awk '{print $1}'))) + printf "%s" "$(date +%T)" \ + && echo " Cybercafe started." + + #Send warning if hotspot is down + ip add show dev $HS_INTERFACE | grep UP > /dev/null # Does it appear the hotspot is active? + if [[ $? -ne 0 ]]; then + echo "Warning the designated hotspot interface is not currently up." + fi + fi +} + +function command_status +{ + echo "" + printf "%s" "$(date '+%Y-%m-%d %H:%M:%S')" + echo "" + ps -eo name,cmdline | grep "bash ./Cybercafe_daemon.sh" | grep -v grep > /dev/null 2>&1 # if this returns 0 it implies that the script exists and is running + if [[ $? -eq 0 ]]; then + echo "Status: Running" + echo "" + echo "Process Info:" + ps -eo user,pid,ppid,uid,stime,stat,name,cmdline | head -n 1 + ps -eo user,pid,ppid,uid,stime,stat,name,cmdline | grep "bash ./Cybercafe_daemon.sh" | grep -v grep + else + echo "Status: Stopped" + fi + echo "" + echo "Hotspot Interface Info:" + ip add show dev $HS_INTERFACE + echo "" +} + +function command_list_sessions +{ + DATABASE_PATH='../database/CyberCafe_Database.db' + sqlite3 $DATABASE_PATH -header "SELECT * FROM internet_sessions" +} + +function command_list_users +{ + DATABASE_PATH='../database/CyberCafe_Database.db' + sqlite3 $DATABASE_PATH -header "SELECT * FROM users" +} + +function command_list_lanes +{ + DATABASE_PATH='../database/CyberCafe_Database.db' + sqlite3 $DATABASE_PATH -header "SELECT * FROM data_lanes" +} + +function command_shutdown +{ + ps -eo stat,name,cmdline | grep "bash ./Cybercafe_daemon.sh" | grep -v grep > /dev/null 2>&1 # if this returns 0 it implies that the script exists and is running + if [[ $? -eq 0 ]]; then + printf "%s" "$(date +%T)" + echo "" + echo "Shutting down" + touch shutdown.confirmed + while true; do + sleep 1 + ps -eo stat,name,cmdline | grep "bash ./Cybercafe_daemon.sh" | grep -v grep > /dev/null 2>&1 # if this returns 1 it implies that the script has stopped + if [[ $? -eq 1 ]]; then + rm shutdown.confirmed + printf "%s" "$(date +%T)" + echo " Shutdown complete" + break + fi + done + else + echo "Cybercafe Daemon isn't running" + fi +} + +function command_kill +{ + killall -9 Cybercafe_daemon.sh + clear_internet_sessions + shutdown_infrastructure +} + +function command_exit +{ + loop=false +} + +function display_help +{ + echo "Valid commands:" + echo "run - Starts Cybercafe infrastructure." + echo "status - Show status info on Cybercafe infrastructure." + echo "list (table) - List database info such as (sessions,users,lanes)" + echo "shutdown - Sends the signal to system to shutdown cleanly." + echo "kill - Kills all Cybercafe scripts (not recommended)." + echo "exit - Leave this command line." + echo "help - Display this page." +} + +##PROGRAM## +loop=true +while $loop; do + read -p "CyberCafe>> " user_command + if [[ $user_command == 'run' ]]; then + command_run + elif [[ $user_command == 'status' ]]; then + command_status + elif [[ $user_command == 'list' ]]; then + echo "Must include table to list." + elif [[ $user_command == 'list sessions' ]]; then + command_list_sessions + elif [[ $user_command == 'list users' ]]; then + command_list_users + elif [[ $user_command == 'list lanes' ]]; then + command_list_lanes + elif [[ $user_command == 'shutdown' ]]; then + command_shutdown + elif [[ $user_command == 'kill' ]]; then + command_kill + elif [[ $user_command == 'exit' ]]; then + command_exit + elif [[ $user_command == 'help' ]]; then + display_help + else + echo "Invalid command. Try 'help' for more options." + fi +done \ No newline at end of file diff --git a/Backend/Cybercafe_daemon.sh b/Backend/Cybercafe_daemon.sh new file mode 100644 index 0000000..5283b5e --- /dev/null +++ b/Backend/Cybercafe_daemon.sh @@ -0,0 +1,49 @@ +#!/bin/bash +#Organization: Grey-box +#Project: Cybercafe +#File: daemon +#Description: Main script that is run at start. This script is invoked by Cybercafe_commandLine.sh + +###INCLUDES### +. ./Cybercafe_setupFunctions.sh +. ./Cybercafe_internetSessionFunctions.sh + + +### START OF PROGRAM EXECUTION ### +while true; do + check_hotspot_status + + #Case 1: Hotspot up and was up in previous check + if [[ $HS_STATUS == 'up' ]] && [[ $HS_STATUS_PREV == 'up' ]]; then + printf "%s" "$(date +%T)" \ + && echo ": Status normal" + check_internet_sessions + + #Case 2: Hotspot up and was down in previous check + elif [[ $HS_STATUS == 'up' ]] && [[ $HS_STATUS_PREV == 'down' ]]; then + printf "%s" "$(date +%T)" \ + && echo ": Hotspot up" + setup_infrastructure + + #Case 3: Hotspot down and was up in previous check + elif [[ $HS_STATUS == 'down' ]] && [[ $HS_STATUS_PREV == 'up' ]]; then + # Assume hotspot has recently gone down. Clean up. + printf "%s" "$(date +%T)" \ + && echo ": Hotspot went down" + clear_internet_sessions + shutdown_infrastructure + + #Case 4: Hotspot down and was down in previous check + else + printf "%s" "$(date +%T)" \ + && echo ": Hotspot down" + fi + + if [[ -f ./shutdown.confirmed ]]; then + #clear_internet_sessions + shutdown_infrastructure + exit + fi + + sleep 2 +done diff --git a/Backend/Cybercafe_internetSessionFunctions.sh b/Backend/Cybercafe_internetSessionFunctions.sh new file mode 100644 index 0000000..bfaa234 --- /dev/null +++ b/Backend/Cybercafe_internetSessionFunctions.sh @@ -0,0 +1,221 @@ +#Organization: Grey-box +#Project: Cybercafe +#File: globalVariables +#Description: Contains all global variables and configuration values that are needed by other scripts of the backend. + +##INCLUDES## + +##VARIABLES## +DATABASE_PATH='../database/CyberCafe_Database.db' + +function clear_internet_sessions +{ + #Purpose: used to delete all existing internet sessions stored in the database for when the system needs to be shutdown (i.e. shutdown_infra) + # 1. Get user session data from sqlite database + # 2. Save data from internet_sessions table into user_data_usage table and format it + + I=$((0)) #iterator value + INDEX_LIMIT=$(($(sqlite3 $DATABASE_PATH "SELECT MAX(table_index) FROM internet_sessions;")+1)) #the maximum number of internet session entries + while [ $I -lt $INDEX_LIMIT ] + do + remove_session $I + I=$(($I+1)) #increment iterator + done +} + +#Desc: used along with remove_session to remove user access +function delete_user_iptable_rules +{ + # Argument1: $1 -> user_ip + iptables -t mangle -D iptmon_rx -d ${1} -i ${HS_INTERFACE} &> /dev/null # delete rules since this user is effectively logged out + iptables -t mangle -D iptmon_tx -s ${1} -i ${HS_INTERFACE} &> /dev/null + iptables -t nat -D PREROUTING -p all -s ${1} -i ${HS_INTERFACE} -j RETURN &> /dev/null #delete access outside of the captive portal using iptables +} + +#Desc: cleanly remove a given session and save all necessary data +function remove_session +{ + # Argument1: $1 -> internet_session table index (i.e. what internet session are we saving/deleting) + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT * FROM internet_sessions WHERE table_index=${1}") + if [[ $RESPONSE == '' ]]; then + return 1 #session doesn't exists + else + + #1. Get all the data + #Necessary data from internet_session row + DATETIME=$(date '+%Y-%m-%d %H:%M:%S') + SESSION_TX=$(echo $RESPONSE | cut -f 5 -d '|') + SESSION_RX=$(echo $RESPONSE | cut -f 6 -d '|') + USER_ID=$(echo $RESPONSE | cut -f 2 -d '|') + USER_IP=$(echo $RESPONSE | cut -f 4 -d '|') + # + + #Calculate what session number this will be saved under in user_data_usage for this user + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT MAX(session_number) FROM user_data_usage WHERE user_id='${USER_ID}'") + if [[ $RESPONSE == '' ]]; then #this implies that this is the very first session for this user + SESSION_NUMBER=$((0)) + ENTRY_INDEX=$((0)) + else + SESSION_NUMBER=$(($RESPONSE)) + fi + #Calculate what entry number this will be saved under in user_data_usage for this user on this session + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT MAX(session_entry_index) FROM user_data_usage WHERE user_id='${USER_ID}' AND session_number=${SESSION_NUMBER}") + if [[ $RESPONSE == '' ]]; then #this implies that this is the very first entry for this session + ENTRY_INDEX=$((0)) + INTERVAL_TX=$SESSION_TX + INTERVAL_RX=$SESSION_RX + else + ENTRY_INDEX=$(($RESPONSE+1)) + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT SUM(interval_bytes_tx) FROM user_data_usage WHERE user_id=${USER_ID} AND session_number=${SESSION_NUMBER}") + INTERVAL_TX=$(($SESSION_TX-$RESPONSE)) #The final data entry is the final interval which is the (total usage - sum of previous entries) + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT SUM(interval_bytes_rx) FROM user_data_usage WHERE user_id=${USER_ID} AND session_number=${SESSION_NUMBER}") + INTERVAL_RX=$(($SESSION_RX-$RESPONSE)) #same as above + fi + + #2. Create entry + sqlite3 $DATABASE_PATH "INSERT INTO user_data_usage (user_id,session_number,session_entry_index,entry_datetime,interval_bytes_tx,interval_bytes_rx) VALUES (${USER_ID},${SESSION_NUMBER},${ENTRY_INDEX},'${DATETIME}',${INTERVAL_TX},${INTERVAL_RX})" + #3. Delete internet session + sqlite3 $DATABASE_PATH "DELETE FROM internet_sessions WHERE table_index=${1}" # delete entry from internet_sessions so that the session doesn't exist anymore + #4. Delete iptable rules + delete_user_iptable_rules $USER_IP + fi +} + +function check_against_usage_limits +{ + # Argument1: $1 -> internet_session table index + USER_ID=$(sqlite3 $DATABASE_PATH "SELECT user_id FROM internet_sessions WHERE table_index=${I}") + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT user_level FROM users WHERE user_id=${USER_ID}") + if [[ $REPONSE == '0' ]]; then #if the user is an admin then skip all other checks and return 1 (true) + return 1 + else + #1. Sum of usage today, this week, this month + TOTAL_TX_DAY=$(($(sqlite3 $DATABASE_PATH "SELECT SUM(interval_bytes_tx) FROM user_data_usage WHERE user_id=${USER_ID} AND entry_datetime>=(SELECT CURRENT_DATE AS today)"))) + TOTAL_RX_DAY=$(($(sqlite3 $DATABASE_PATH "SELECT SUM(interval_bytes_rx) FROM user_data_usage WHERE user_id=${USER_ID} AND entry_datetime>=(SELECT CURRENT_DATE AS today)"))) + if [[ $TOTAL_TX_DAY == '' ]]; then + TOTAL_TX_DAY=$((0)) + fi + if [[ $TOTAL_RX_DAY == '' ]]; then + TOTAL_RX_DAY=$((0)) + fi + #2. Check the lane of the user + LANE_ID=$(sqlite3 $DATABASE_PATH "SELECT lane_id FROM users WHERE user_id=${USER_ID}") + if [[ $LANE_ID == '' ]]; then + LANE_ID=$((2)) + fi + #SELECT lane_id FROM users WHERE user_id=${USER_ID}; + #3. Do logical comparisions + LANE_DAILY_LIMIT=$(($(sqlite3 $DATABASE_PATH "SELECT bytelimit_daily FROM data_lanes WHERE lane_id=${LANE_ID}"))) + if [[ $(($TOTAL_TX_DAY+$TOTAL_RX_DAY)) -gt $LANE_DAILY_LIMIT ]]; then + return 1 + else + return 0 + fi + #4. Return boolean value 1 (over limits) 0 (not over limits) + fi +} + +function check_internet_sessions +{ + # Purpose: main function used to periodically update internet session data in the sqlite database and remove sessions based certain criteria. + I=$((0)) #iterator value + INDEX_LIMIT=$(($(sqlite3 $DATABASE_PATH "SELECT MAX(table_index) FROM internet_sessions;")+1)) #the maximum number of internet session entries + while [ $I -lt $INDEX_LIMIT ] + do + # 1. Get user session data from sqlite database if it doesn't exist then skip + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT * FROM internet_sessions WHERE table_index=${I}") + if [[ $RESPONSE == '' ]]; then + I=$(($I+1)) + continue + fi + + # 2. Check that a rule exists to record data usage (iptables) + USER_IP=$(echo $RESPONSE | cut -f 4 -d '|') + iptables -t mangle -C iptmon_rx -d ${USER_IP} -i ${HS_INTERFACE} &> /dev/null + if [[ $? -eq 1 ]] && [[ $RESPONSE != '' ]]; then + #rule doesn't exist but should + iptables -t mangle -A iptmon_rx -d ${USER_IP} -i ${HS_INTERFACE} > /dev/null + iptables -t mangle -A iptmon_tx -s ${USER_IP} -i ${HS_INTERFACE} > /dev/null + fi + + # 3. Update data usage according to ip table onto internet_session table + SESSION_ACCESS=$(echo $RESPONSE | cut -f 7 -d '|') + if [[ $SESSION_ACCESS == '1' ]]; then + SESSION_TX=$(($(iptables -t mangle -L iptmon_tx -vxn | grep $USER_IP | awk '{print $2}'))) + SESSION_RX=$(($(iptables -t mangle -L iptmon_rx -vxn | grep $USER_IP | awk '{print $2}'))) + sqlite3 $DATABASE_PATH "UPDATE internet_sessions SET session_tx=${SESSION_TX},session_rx=${SESSION_RX} WHERE table_index=${I}" + fi + + # 4. update idle time based on metrics from user_data_usage table + USER_ID=$(sqlite3 $DATABASE_PATH "SELECT user_id FROM internet_sessions WHERE table_index=${I}") + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT MAX(entry_datetime) FROM user_data_usage WHERE user_id=${USER_ID}") + if [[ $RESPONSE != '' ]]; then + sqlite3 $DATABASE_PATH "UPDATE internet_sessions SET datetime_sinceLastRequest='${RESPONSE}' WHERE table_index=${I}" + fi + + # 5. If session age or session idle time becomes to great then save that sessions data and delete the session + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT timediff((SELECT datetime_created FROM internet_sessions WHERE table_index=${I}),datetime(datetime(),'localtime'))") + SESSION_AGE=$((86400*$(echo $RESPONSE | awk '{print $1}' | cut -f 4 -d '-')+3600*$(echo $RESPONSE | awk '{print $2}' | cut -f 1 -d ':')+60*$(echo $RESPONSE | awk '{print $2}' | cut -f 2 -d ':')+$(echo $RESPONSE | awk '{print $2}' | cut -f 3 -d ':' | cut -f 1 -d '.'))) + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT timediff((SELECT datetime_sinceLastRequest FROM internet_sessions WHERE table_index=${I}),datetime(datetime(),'localtime'))") + SESSION_IDLETIME=$((10#86400*$(echo $RESPONSE | awk '{print $1}' | cut -f 4 -d '-')+10#3600*$(echo $RESPONSE | awk '{print $2}' | cut -f 1 -d ':')+10#60*$(echo $RESPONSE | awk '{print $2}' | cut -f 2 -d ':')+$(echo $RESPONSE | awk '{print $2}' | cut -f 3 -d ':' | cut -f 1 -d '.'))) + + if [[ ${SESSION_AGE} -gt 43200 ]] || [[ ${SESSION_IDLETIME} -gt 3600 ]]; then #if session is older than 12 hours or has been idle for more than 1hr then delete session + remove_session $I + delete_user_iptable_rules $USER_IP + fi + + #6. Send data entry for this check to user_data_usage + #Necessary data from internet_session row + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT * FROM internet_sessions WHERE table_index=${I}") + DATETIME=$(date '+%Y-%m-%d %H:%M:%S') + SESSION_TX=$(echo $RESPONSE | cut -f 5 -d '|') + SESSION_RX=$(echo $RESPONSE | cut -f 6 -d '|') + USER_ID=$(echo $RESPONSE | cut -f 2 -d '|') + USER_IP=$(echo $RESPONSE | cut -f 4 -d '|') + # + + #Calculate what session number this will be saved under in user_data_usage for this user + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT MAX(session_number) FROM user_data_usage WHERE user_id='${USER_ID}'") + if [[ $RESPONSE == '' ]]; then #this implies that this is the very first session for this user + SESSION_NUMBER=$((0)) + ENTRY_INDEX=$((0)) + else + SESSION_NUMBER=$(($RESPONSE)) + fi + #Calculate what entry number this will be saved under in user_data_usage for this user on this session + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT MAX(session_entry_index) FROM user_data_usage WHERE user_id='${USER_ID}' AND session_number=${SESSION_NUMBER}") + if [[ $RESPONSE == '' ]]; then #this implies that this is the very first entry for this session + ENTRY_INDEX=$((0)) + INTERVAL_TX=$SESSION_TX + INTERVAL_RX=$SESSION_RX + else + ENTRY_INDEX=$(($RESPONSE+1)) + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT SUM(interval_bytes_tx) FROM user_data_usage WHERE user_id=${USER_ID} AND session_number=${SESSION_NUMBER}") + INTERVAL_TX=$(($SESSION_TX-$RESPONSE)) #The next interval entry is [total usage for this session - sum of previous entries associated with this user's session] + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT SUM(interval_bytes_rx) FROM user_data_usage WHERE user_id=${USER_ID} AND session_number=${SESSION_NUMBER}") + INTERVAL_RX=$(($SESSION_RX-$RESPONSE)) #same as above + fi + #create entry for this check in database + sqlite3 $DATABASE_PATH "INSERT INTO user_data_usage (user_id,session_number,session_entry_index,entry_datetime,interval_bytes_tx,interval_bytes_rx) VALUES (${USER_ID},${SESSION_NUMBER},${ENTRY_INDEX},'${DATETIME}',${INTERVAL_TX},${INTERVAL_RX})" + + #7. Calculate current usage and determine if given user is outside their limits + check_against_usage_limits $I + if [[ $? -eq 1 ]]; then #user is over limits, so do necessary updates + sqlite3 $DATABASE_PATH "UPDATE internet_sessions SET session_access='0' WHERE table_index=${I}" + fi + + #8. Check access bit associated with user to see if iptable rules need to be updated + iptables -t nat -C PREROUTING -s ${USER_IP} -i ${HS_INTERFACE} -j RETURN &> /dev/null + if [[ $? -eq 0 && $SESSION_ACCESS == '0' ]]; then #rule exists but shouldn't + iptables -t nat -D PREROUTING -p all -s ${USER_IP} -i ${HS_INTERFACE} -j RETURN > /dev/null + fi + iptables -t nat -C PREROUTING -s ${USER_IP} -i ${HS_INTERFACE} -j RETURN &> /dev/null + if [[ $? -eq 1 && $SESSION_ACCESS == '1' ]]; then #rule doesn't exist but should + iptables -t nat -I PREROUTING 2 -p all -s ${USER_IP} -i ${HS_INTERFACE} -j RETURN > /dev/null + fi + + + I=$(($I+1)) #increment iterator + done +} +"$@" \ No newline at end of file diff --git a/Backend/Cybercafe_setupFunctions.sh b/Backend/Cybercafe_setupFunctions.sh new file mode 100644 index 0000000..03d9111 --- /dev/null +++ b/Backend/Cybercafe_setupFunctions.sh @@ -0,0 +1,134 @@ +#Organization: Grey-box +#Project: Cybercafe +#Description: Contains all the bash functions necessary to setup the Cybercafe architecture. This script is included by the daemon script which actually calls the setup. + +##INCLUDES## + +##VARIABLES## +LOCAL_IP='' + +# The status of the hotspot interface on the device +HS_STATUS='down' +HS_STATUS_PREV='down' + +# Interface of the hotspot +HS_INTERFACE='wlan1' + +# Base directory of lighttpd and important files +APP_PATH='/data/data/com.termux/files/usr' # application path + +function check_hotspot_status +#Checks whether the device's hotspot feature has been turned on or off +{ + HS_STATUS_PREV=$HS_STATUS #save previous status from last check + ip add show dev $HS_INTERFACE | grep UP > /dev/null # Does it appear the hotspot is active? + if [[ $? -eq 0 ]]; then + HS_STATUS='up' + else + HS_STATUS='down' + fi +} + + +function setup_infrastructure +#Setup any necessary infrastructure for the Cybercafe system +{ + # We're in hotspot mode but unsure if we're properly configured for CyberCafe operation + LOCAL_IP=$(ifconfig $HS_INTERFACE | grep 'inet addr' | awk '{print $2}' | cut -d: -f2) + echo $HS_INTERFACE IP: $LOCAL_IP + + #check if iptmon_tx table exists + iptables -t mangle -C FORWARD -j iptmon_tx > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + echo "Creating table ipmon_tx" + iptables -t mangle -N iptmon_tx + + echo "Adding iptmon_tx to FORWARD chain, 'mangle' table" + iptables -t mangle -A FORWARD -j iptmon_tx + fi + + ## iptmon_rx + iptables -t mangle -C FORWARD -j iptmon_rx > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + echo "Creating table iptmon_rx" + iptables -t mangle -N iptmon_rx + + echo "Adding iptmon_rx to FORWARD chain, 'mangle' table" + iptables -t mangle -A FORWARD -j iptmon_rx + fi + + # Default iptables redirect + + iptables -t nat -C PREROUTING -p tcp -i ${HS_INTERFACE} -j DNAT --to-destination ${LOCAL_IP}:80 > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + echo "Creating default redirect rule" + iptables -t nat -A PREROUTING -p udp -d ${LOCAL_IP} --dport 53 -j RETURN + iptables -t nat -A PREROUTING -p udp -s 0.0.0.0/32 -d 255.255.255.255/32 --dport 67 -j RETURN + iptables -t nat -A PREROUTING -p tcp -i ${HS_INTERFACE} -j DNAT --to-destination ${LOCAL_IP}:80 + fi + + # Traffic Control +# tc qdisc show dev $HS_INTERFACE | grep htb > /dev/null +# if [[ $? -ne 0 ]]; then +# echo "Creating tc qdisc HTB queues" +# tc qdisc add dev $HS_INTERFACE root handle 1: htb default 10 +# tc class add dev $HS_INTERFACE parent 1: classid 1:1 htb rate 15mbps ceil 15mbps +# tc class add dev $HS_INTERFACE parent 1:1 classid 1:10 htb rate 15mbps ceil 15mbps +# tc class add dev $HS_INTERFACE parent 1:1 classid 1:20 htb rate 100kbps ceil 100kbps +# fi + + # Check captive portal HTTPD server + if ! pgrep lighttpd > /dev/null; then + start_captive_webserver + fi +} + + +function shutdown_infrastructure +#remove any necessary infrastructure for running the CyberCafe system +{ + printf "%s" "$(date +%T)" + echo " Shutting down..." + pkill lighttpd + + LOCAL_IP='' + HS_STATUS='down' + + echo "Deleting iptmon tables." + iptables -t mangle -D FORWARD -j iptmon_tx > /dev/null 2>&1 + iptables -t mangle -D FORWARD -j iptmon_rx > /dev/null 2>&1 + while true; do + iptables -t mangle -D iptmon_rx 1 > /dev/null 2>&1 + if [[ $? == 1 ]]; then + break + fi + done + iptables -t mangle -X iptmon_rx > /dev/null 2>&1 + + while true; do + iptables -t mangle -D iptmon_tx 1 > /dev/null 2>&1 + if [[ $? == 1 ]]; then + break + fi + done + iptables -t mangle -X iptmon_tx > /dev/null 2>&1 + + echo "Removing prerouting rules." + while true; do + iptables -t nat -D PREROUTING 2 > /dev/null 2>&1 + if [[ $? == 1 ]]; then + break + fi + done + +# echo "Cleaning up ${HS_INTERFACE} qdisc" +# tc qdisc delete dev $HS_INTERFACE root + + echo "Done." +} + +function start_captive_webserver +#starts the lighttpd webserver that acts as a captive web portal for sign in and such +{ + $APP_PATH/bin/lighttpd -f $APP_PATH/etc/lighttpd/lighttpd.conf +} \ No newline at end of file diff --git a/Backend/Cybercafe_testbed.sh b/Backend/Cybercafe_testbed.sh new file mode 100644 index 0000000..4f901cb --- /dev/null +++ b/Backend/Cybercafe_testbed.sh @@ -0,0 +1,52 @@ +#!/bin/bash +#Organization: Grey-box +#Project: Cybercafe +#File: testbed +#Description: Used to test functions contained in the different Cybercafe libraries + +###INCLUDES### +. ./Cybercafe_setupFunctions.sh +. ./Cybercafe_internetSessionFunctions.sh + +PS4='Line[$LINENO]: ' + +display_information() +{ + echo "" + echo "---Stage ${1}---" + echo -e "\t---Iptables---" + iptables -t nat -L PREROUTING -vxn + iptables -t mangle -L iptmon_tx -vxn + iptables -t mangle -L iptmon_rx -vxn + echo -e "\t---sqlite3---" + echo -e "\t\t---internet_sessions---" + sqlite3 $DATABASE_PATH -header "SELECT * FROM internet_sessions" + echo -e "\t\t---user_data_usage---" + sqlite3 $DATABASE_PATH -header "SELECT * FROM user_data_usage" + read -p "Press any key to continue..." +} + +DATETIME=$(date '+%Y-%m-%d %H:%M:%S') +DATETIME_LASTREQUEST=$(date '+%Y-%m-%d %H:%M:%S') +sqlite3 $DATABASE_PATH "INSERT INTO internet_sessions VALUES (0,1,'a838fjdlkc908sdjfk3jnk2wjnef','192.168.1.45',100,100,1,'${DATETIME}','${DATETIME_LASTREQUEST}')" +sqlite3 $DATABASE_PATH "INSERT INTO internet_sessions VALUES (1,2,'b838fjdlkc908sdjfk3jnk2wjnef','192.168.1.46',100,100,0,'${DATETIME}','${DATETIME_LASTREQUEST}')" +sqlite3 $DATABASE_PATH "INSERT INTO internet_sessions VALUES (2,3,'c838fjdlkc908sdjfk3jnk2wjnef','192.168.1.47',100,100,0,'${DATETIME}','${DATETIME_LASTREQUEST}')" +echo "" +display_information '1' + +setup_infrastructure + +display_information '2' + +check_internet_sessions + +display_information '3' + +clear_internet_sessions + +display_information '4' + +shutdown_infrastructure + +sqlite3 $DATABASE_PATH "DELETE FROM internet_sessions WHERE 1=1" +sqlite3 $DATABASE_PATH "DELETE FROM user_data_usage WHERE 1=1" \ No newline at end of file diff --git a/Backend/archive/CyberCafe_UpdateUserUsage.sh b/Backend/archive/CyberCafe_UpdateUserUsage.sh new file mode 100644 index 0000000..baaab98 --- /dev/null +++ b/Backend/archive/CyberCafe_UpdateUserUsage.sh @@ -0,0 +1,7 @@ +echo "HI" + + + +while true; do + +done \ No newline at end of file diff --git a/Backend/archive/UserModify_IptablesRules_AtomicScript.sh b/Backend/archive/UserModify_IptablesRules_AtomicScript.sh new file mode 100644 index 0000000..51a7e5a --- /dev/null +++ b/Backend/archive/UserModify_IptablesRules_AtomicScript.sh @@ -0,0 +1,46 @@ +#Greybox - Cybercafe +#Script: UserModify_IptablesRules_AtomicScript.sh +#Desc: Add and remove user rules to the iptables of the device to allow outside access and record data usage + +DEFAULT_INSERT_LINE=2 +HS_INTERFACE='wlan1' +user_ip=$2 + +#Argument Validation +if [[ $# != 2 ]]; then + echo "Invalid number of arguments." + echo "Usage: ./UserModify_IptablesRules_AtomicScript [+/-] [user_ip]" + exit 1 +fi + +#Adding Rule for Use +if [[ $1 == + ]]; then + iptables -t nat -C PREROUTING -p tcp -s ${user_ip} -i ${HS_INTERFACE} -j RETURN > /dev/null 2>&1 + if [[ $? -ne 0 ]]; then + #rules to add + iptables -t nat -I PREROUTING $DEFAULT_INSERT_LINE -p tcp -s ${user_ip} -i ${HS_INTERFACE} -j RETURN > /dev/null 2>&1 + #record tcp packets incoming to this user + iptables -t mangle -A iptmon_rx -p tcp -d ${user_ip} -i ${HS_INTERFACE} > /dev/null 2>&1 + #record tcp packets outgoing from this user + iptables -t mangle -A iptmon_tx -p tcp -s ${user_ip} -i ${HS_INTERFACE} > /dev/null 2>&1 + fi + exit 0 +fi + +#Remove Rule for User +if [[ $1 == - ]]; then + iptables -t nat -C PREROUTING -p tcp -s ${user_ip} -i ${HS_INTERFACE} -j RETURN > /dev/null 2>&1 + if [[ $? -eq 0 ]]; then + #rules to remove + iptables -t nat -D PREROUTING -p tcp -s ${user_ip} -i ${HS_INTERFACE} -j RETURN > /dev/null 2>&1 + #record tcp packets incoming to this user + iptables -t mangle -D iptmon_rx -p tcp -d ${user_ip} -i ${HS_INTERFACE} > /dev/null 2>&1 + #record tcp packets outgoing from this user + iptables -t mangle -D iptmon_tx -p tcp -s ${user_ip} -i ${HS_INTERFACE} > /dev/null 2>&1 + fi + exit 0 +fi + +echo "Invalid Syntax" +echo "Usage: ./UserModify_IptablesRules_AtomicScript [+/-] [user_ip]" +exit 1 diff --git a/Database/CyberCafe_Database.db b/Database/CyberCafe_Database.db new file mode 100644 index 0000000000000000000000000000000000000000..0bbeada7556c1429740114f1dbad02483b75e730 GIT binary patch literal 28672 zcmeI)&2QUe90zbaX`RMkd6&t%AiN7ov_j*&IQ9XlY2{YYLJCPxFC)^cH%Z^+zKX5Xz2SWvHiTnd7jVnOC;y9^5C;x z5L4BkOvfZv8{939=ed1V^u zh;5GqqcioOd(2+8R`HzDukvfglQ4dAStWit=zh`}JW}slkJQSt7uD+d(RN9>caQ)2 zFdfS*qEq%}Y(TS!d`YilOKYRLQdtqxg8ZRHG>U>rXos`0L#KIz&sxwho}Sr3=+PH> z%@)uYWDTtTq4hvb z%A!hEQ=P}flA-(o&&x7D5`^%bAZh3JPm-8`TvtJ}zLN&H58~RrW>lZ=<|Z5R`{|*G>1v6xrHFQF0x>lYHGu$>vtFL4p7TAOHafKmY;| zfB*y_009U<;QuO+T>ls4?>YH5`DeBhSazE|lXd^qwSJaGf&c^{009U<00Izz00bZa z0SG|gO$yv82;BR{Vv*fT6DK!`)O+{$7mqvqdW|L@>@OZ4uuJ*(1VIQVAEx&%@vima z@i*+I{VRJh={VN^-{kb++z@~O1Rwwb2tWV=5P$##AOL~?Tp(HhFUVhT@(*l-1OW&@ z00Izz00bZa0SG_<0uX?}YY3ba#Ihtwx93usPyPp?*7Ob2)IHMH8%~>;y6I@Ox`=$=QMx~6$8t?n8v$Mk%|cM2!GxWi7U%%z<&@y0=DxVqWaY0GIf zwOUhaIUY4@hT}CoO0|~fJDSek$2Hmw-)%aMK@82;{W@`)O@o-4k*xn0K*)6v|sW%HsEfWc?ra|G)K9ivd6Y0uX=z T1Rwwb2tWV=5P$##ZWQ<%#6=C? literal 0 HcmV?d00001 diff --git a/Database/CyberCafe_Database_Schema.sql b/Database/CyberCafe_Database_Schema.sql new file mode 100644 index 0000000..faba4e7 --- /dev/null +++ b/Database/CyberCafe_Database_Schema.sql @@ -0,0 +1,51 @@ +-- Internet Sessions -- +CREATE TABLE internet_sessions( +table_index INTEGER NOT NULL, +user_id INTEGER NOT NULL, +session_id TEXT NOT NULL, +ip TEXT NOT NULL, +session_tx INTEGER NOT NULL, +session_rx INTEGER NOT NULL, +session_access INTEGER NOT NULL, +datetime_created TEXT NOT NULL, +datetime_sinceLastRequest TEXT NOT NULL, +PRIMARY KEY (table_index, user_id, session_id), +FOREIGN KEY (user_id) REFERENCES users(user_id)); + +-- Users -- +CREATE TABLE users( +user_id INTEGER NOT NULL, +name TEXT, +email TEXT, +phone TEXT, +username TEXT NOT NULL, +password TEXT NOT NULL, +user_level INTEGER NOT NULL, +lane_id INTEGER NOT NULL, +PRIMARY KEY (user_id, username)); + +-- User Data Usage -- +CREATE TABLE user_data_usage( +user_id INTEGER NOT NULL, +session_number INTEGER NOT NULL, +session_entry_index INTEGER NOT NULL, +entry_datetime TEXT NOT NULL, +interval_bytes_tx INTEGER NOT NULL, +interval_bytes_rx INTEGER NOT NULL, +FOREIGN KEY (user_id) REFERENCES users(user_id)); + +-- Data Lanes -- +CREATE TABLE data_lanes( +lane_id INTEGER NOT NULL, +lane_name TEXT, +bytelimit_daily BIGINT, +bytelimit_weekly BIGINT, +bytelimit_monthly BIGINT, +PRIMARY KEY (lane_id)); + +-- insert debug entries -- +INSERT INTO users VALUES('0','','','','admin','8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918','0','1'); +INSERT INTO users VALUES('1','','','','user','04f8996da763b7a969b1028ee3007569eaf3a635486ddab211d512c85b9df8fb','1','2'); +INSERT INTO data_lanes VALUES(0,'no-limit','100000000000000000','100000000000000000','100000000000000000'); +INSERT INTO data_lanes VALUES(1,'testlane1','1000000','1000000','1000000'); +INSERT INTO data_lanes VALUES(2,'testlane2','5000000','5000000','5000000'); \ No newline at end of file diff --git a/Database/CyberCafe_DropAllTables.sql b/Database/CyberCafe_DropAllTables.sql new file mode 100644 index 0000000..50300ff --- /dev/null +++ b/Database/CyberCafe_DropAllTables.sql @@ -0,0 +1,4 @@ +DROP TABLE internet_sessions; +DROP TABLE users; +DROP TABLE user_data_usage; +DROP TABLE data_lanes; \ No newline at end of file diff --git a/Database/CyberCafe_Example.db b/Database/CyberCafe_Example.db deleted file mode 100644 index 2152ca98c13190f0fed43e1495b0b6d019121dff..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 61440 zcmeI5Z)_WP8OQA=jbq35bsL-MwrIziDhW-t`|Cf4DlQG)7Bx%TG%c;P$vWHT#w@i{ z+gV!y9cfC1Kx$u10^Trq!=D#uh!>lX5TLz4fDmZFU?BEl8he2dlMwBTiXnyPcNg1t zmpf<5Dy<7&%aPB|@44?izt8jgbN9Pka{SnAwPo^^dgH9o;^okuP&gdYc|H^hU7{b3 ze%h}jZS=Rl&}Z1O9kh8V^ho1diRgSN+5c>ao8W$)yeD}hzLt1xXixl!!M)K32Kpnv zkIhpdULXJh|0e|2hW12Kk3JgyV!mY*m&|IdWj1PNtI#x?&1${Y++-MgjhTsF;e;oOS3VvgCRvGKLT ziAYM8!&eVl*|5Z#Eu*#4EG$-=t$O34k16TOh>ruy$wpk8_>~ro+8MJD$hT&mFE}La z{imPEPahuh7R}7@W1~~k3o|G4qZ9n-p{cpK{HLs~6SIfeXa3aDnR(lEbn3)$+8Sqc zmmB7@)jCx%;5feJ+dWvrG@A9ARSi0{E8`o8CU_5%ZMJ~ecs@ChIxUAo)mquS&^)(9 z(|f^KY1OUo&f2z_>Ln zAl3LlYG&Vi0qF#o;BLK_6Qi9w(64@ii=-we! zGl%2`2U1Ut26Yw}o`UH58ofNXJCZtZAbjPxMS0n{c-E}7+5?g2>wpWpr;vePUi6#j zgPIBn1T+Pr7nrM7y=5$UXZe7nSn+Ig4Dx2s&a3kjz%iY7b8&EBAa(3OP%b>o3Zlml zS$(=MlG?vNeEIWMKAeYeZuh$~;VfZku-SQgtaaW|s2M(|RCZC>Y?d0;1O>7pwzN)u(2iCvfHZ`C5-+SNE{##l|!J1F6INgYY;jQV^ZrcCX$Sj-(!XD7^Yv z3rwf)bT)RmkaWzw#sgm#tj904>%Y8MuXTn5M};~keX((I*FY-&P*9CJacoTVth@Ko z{4>brpalJdxnGCqjTZ=j00@8p2!H?xfB*=900@8p2!OzyPe2au9v(gLvFXJ|wJA!n z^~q1uT1%r|D>0+KvRtcPc%)Q6o5>1kRm~}~5Tkm9xtk$+;{^gB00JNY0w4eaAOHd& z00JNY0wA!12n_WNN876e_Wl1?Lfk7msDh{p2!H?xfB*=900@8p2!H?xfB*>WGy(_1 z1KhpGA`$TrJ?}VMKR>zLsMZ_R*2M|FxU%$2=UG|aD3#~|*-yS{189HR_y6AwaqsRl zFsKg*fB*=900@8p2!H?xfB*=900@8p5$NyZq9VKhPk;0eFAx9$5C8!X009sH0T2KI z5C8!X0D&D$fZhMc_5Th|FscRuAOHd&00JNY0w4eaAOHd&00Kn7e*XWB5cdXffB*=9 z00@8p2!H?xfB*=900@8p2z(d`JWT%(AS&-_|EWJokmN~GniOST%xZ!_@BWbe{Qr#* zcjLoYL&OXMAOHd&00JNY0w4eaAOHd&00JNoKp@^f9CiKu{|5(u8RCA*eU8(Tw~{X> zTS+PLuf&UqrNqbMe~*7BUXDLJ^ybhDLr)FeANzCctFeXH-O*R0*P^r0810A`2!H?x zfB*=900?|Q1oQ`Yj}FUnIxR>-Q7B15B`r8^d?=HGJSnNXBx`b3Qzg0=Vw=)D-3YNv z`U7nRu~JEkaz#}|KLtrsa+;ua6f9B8q~C8Vs6|ySRg|2ND=KaUaZ<|gVn&lPnwYi~ zY*W@Y>3h2hxok!$XNp;W1yR!EoLj*%=^t$?$Y#c<2tp;B-lULbF^D3)I|^jV+9o}< zp~%ZivTR|HXYDA+flax4Z}(L{w5 zI;N~`((mmmh^4Gis;EVO1xe8)kAh{=KjKixiK0m)0u)%(?ij3!==V4j34S(grb=hmr~!(sXE|eFCOzp;P)yk{WM|LnxMwsWNBMUv zSSCHuRVY^^(MXq^{lck0cQ!pS2+X9%9SVwUh*Ht?=OCz>knzO8O!`n)p=4Hy6}3_d zP>?l6_9!ru9_uO;%f*b4mV9fG)>Il{Trp&9lOA;_5UH{$`NlvBX^kRq!WOnStjAPFRqb&j+hcTx~F#g$EpftmDyu0pn$5e>8A+p1ZU z_K?ijCVjUS_;GdQ@58)m1PEb)_Wu#y}7hPrsmwF_RwNP{`yeS!&c-8u2&P z!n)lr3!2y+qsT-(jhWc*|L=|Whmvn|D))or^T}rtE6Kfd{r`)^3-RB@zsFsT|0jMj zv5$)>ZGqHQI+a4p0_!8I z|9I ziPj=0#xK9z+`^H5J)1ec_06Nb9oZ@m!}Hf4UE0EtE+YkU{Mzfn7LK%LlP1UOU)TZ< zTbjvG3cr25*jtgUz>wlU|NYb!ek!dNt&jZXPoLSsk5*GiZ|(cv^=o9EOw(m{j@C5S zN4~P^*L0kIv^1iS;PtOh^g1#LQi~vn*Z$ZuLh_?^4r)19uD&$bTac}AFxa=gw3Q=u z18QPFdF^;_LAEwwCFSi?y&P#(f|jAv>-O{i?}WH_-q#R + + + Cybercafe Demo + + +

Greybox - Cybercafe

+

Place holder

+ + \ No newline at end of file diff --git a/Website_ASU2024/createaccount/index.php b/Website_ASU2024/createaccount/index.php new file mode 100644 index 0000000..f2e6051 --- /dev/null +++ b/Website_ASU2024/createaccount/index.php @@ -0,0 +1,10 @@ + + + + Cybercafe Demo + + +

Greybox - Cybercafe

+

Place holder

+ + \ No newline at end of file diff --git a/Website_ASU2024/home/index.php b/Website_ASU2024/home/index.php new file mode 100644 index 0000000..ea71f54 --- /dev/null +++ b/Website_ASU2024/home/index.php @@ -0,0 +1,84 @@ + + + + Cybercafe Demo - Home + + + + +

Admin Page

+
+ +
+ + + '; +} +function userPage() +{ + echo ' + + + Cybercafe Demo - Home + + + + +

User Page

+ + + + '; +} +if(isset($_COOKIE['session_id'])) +{ + $db = new SQLite3('../../database/CyberCafe_Database.db'); + $response = $db->query("SELECT user_id FROM internet_sessions WHERE session_id='".$_COOKIE['session_id']."'"); + $responseArray = $response->fetchArray(); + #if there is an internet session found matching the query then load respective page + if($responseArray) + { + $user_id = (int)$responseArray['user_id']; + $response2 = $db->query("SELECT * FROM users WHERE user_id=".$user_id.""); + $responseArray2 = $response2->fetchArray(); + if($responseArray2['user_level']==0) + { + adminPage(); + if(array_key_exists('test',$_POST)) + { + runScript(); + } + } + else if($responseArray2['user_level']==1) + { + userPage(); + } + } + #if there is no internet session matching the cookie then return to login + else + { + setcookie('session_id', '', time()-3600, '/'); + header('Location: /login'); + } + $db->close(); +} +#if session_id is not set users should be redirected to login +else +{ + header('Location: /login'); +} +?> \ No newline at end of file diff --git a/Website_ASU2024/index.php b/Website_ASU2024/index.php new file mode 100644 index 0000000..b633be2 --- /dev/null +++ b/Website_ASU2024/index.php @@ -0,0 +1,12 @@ + + + + Cybercafe Demo + + +

Greybox - Cybercafe

+ Login Page
+ Create Account
+ About
+ + diff --git a/Website_ASU2024/login/index.php b/Website_ASU2024/login/index.php new file mode 100644 index 0000000..6ec20e3 --- /dev/null +++ b/Website_ASU2024/login/index.php @@ -0,0 +1,141 @@ +query("SELECT * FROM users WHERE username='".$username."'"); + $responseArray = $response->fetchArray(); + if($responseArray['password']==$password) + { + $user_level = (int)$responseArray['user_level']; + $user_id = (int)$responseArray['user_id']; + $lane_id = (int)$responseArray['lane_id']; + if($user_level=='0') + { + $sessionAccessBit = 1; + } + else if($user_level=='1') + { + $sessionAccessBit = 0; + } + #Create New Internet Session to write to database + $response = $db->query("SELECT MAX(table_index) FROM internet_sessions"); + $responseArray = $response->fetchArray(); + if($responseArray) + { + $internetSessionIndex = (int)$responseArray['table_index']+1; + } + else + { + $internetSessionIndex = 0; + } + #check to see if there are any sessions already active with this user_id + $response = $db->query("SELECT * FROM internet_sessions WHERE user_id='".$user_id."'"); + $responseArray = $response->fetchArray(); + #remove the old session associated with this user_id if it exists. + if($responseArray) + { + shell_exec("bash ../../backend/Cybercafe_internetSessionFunctions.sh remove_session ".$responseArray['table_index']); + } + #TODO: create method to check if this new session will already be out of range of their data limits + #get datetime info + $datetimeObj = new DateTime(); + $datetime = $datetimeObj->format('Y-m-d H:i:s'); + $db->exec("INSERT INTO internet_sessions ( + table_index, + user_id, + session_id, + ip, + session_tx, + session_rx, + session_access, + datetime_created, + datetime_sinceLastRequest) + VALUES( + ".$internetSessionIndex.", + ".$user_id.", + '".$session_id."', + '".$_SERVER['REMOTE_ADDR']."', + 0,0, + ".$sessionAccessBit.", + '".$datetime."', + '".$datetime."')"); + ##DEBUG## + echo "

\$db->exec(\"INSERT INTO internet_sessions (
table_index,
user_id,
session_id,
ip,
session_tx,
session_rx,
session_access,
datetime_created,
datetime_sinceLastRequest)
VALUES(
".$internetSessionIndex.",
".$user_id.",
'".$session_id."',
'".$_SERVER['REMOTE_ADDR']."',
0,0,
".$sessionAccessBit.",
'".$datetime."',
'".$datetime."')\");

"; + # + $db->close(); + return true; + } + $db->close(); + return false; +} +function hashPassword($passwordString) +{ + return hash('sha256', $passwordString); +} +function validSessionID($session_id) +{ + $db = new SQLite3('../../database/CyberCafe_Database.db'); + $response = $db->query("SELECT 1 FROM internet_sessions WHERE session_id='".$session_id."'"); + $result = $response->fetchArray(); + + if($result) + { + $db->close(); + return true; + } + else + { + $db->close(); + return false; + } +} + +if(isset($_COOKIE['session_id'])&&validSessionID($_COOKIE['session_id'])) +{ + header('Location: ../home'); + die(); +} +else +{ + if(isset($_POST['username'])&&isset($_POST['password'])) + { + $session_id = session_create_id(); + if(logIn($_POST['username'],$_POST['password'],$session_id)) + { + setcookie("session_id",$session_id,time()+(3600),"/"); + header('Location: ../home'); + die(); + } + else + { + echo "Login Failed!"; + } + } + else + { + echo ' + + + + Cybercafe Demo - Log-In + + + +

Log-In

+
+
+
+
+
+
+
+ + '; + } +} +?> From f05ff4c215b0f3febca6689f0f40d29ff37c840c Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Sun, 23 Mar 2025 23:32:07 -0700 Subject: [PATCH 002/127] Rename index.php to page.php --- Website_ASU2024/{index.php => page.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Website_ASU2024/{index.php => page.php} (100%) diff --git a/Website_ASU2024/index.php b/Website_ASU2024/page.php similarity index 100% rename from Website_ASU2024/index.php rename to Website_ASU2024/page.php From cebf6083cea3961baf479b41979dbdecd4fd1e1d Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Sun, 23 Mar 2025 23:32:20 -0700 Subject: [PATCH 003/127] Rename index.php to page.php --- Website_ASU2024/login/{index.php => page.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Website_ASU2024/login/{index.php => page.php} (100%) diff --git a/Website_ASU2024/login/index.php b/Website_ASU2024/login/page.php similarity index 100% rename from Website_ASU2024/login/index.php rename to Website_ASU2024/login/page.php From 3315092f74feeedbf722baabfc9adc8f1ce36021 Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Sun, 23 Mar 2025 23:32:30 -0700 Subject: [PATCH 004/127] Rename index.php to page.php --- Website_ASU2024/home/{index.php => page.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Website_ASU2024/home/{index.php => page.php} (99%) diff --git a/Website_ASU2024/home/index.php b/Website_ASU2024/home/page.php similarity index 99% rename from Website_ASU2024/home/index.php rename to Website_ASU2024/home/page.php index ea71f54..db624c4 100644 --- a/Website_ASU2024/home/index.php +++ b/Website_ASU2024/home/page.php @@ -81,4 +81,4 @@ function userPage() { header('Location: /login'); } -?> \ No newline at end of file +?> From 36812afa149eb58cfba1510f6b181478c61ab69a Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Sun, 23 Mar 2025 23:32:39 -0700 Subject: [PATCH 005/127] Rename index.php to page.php --- Website_ASU2024/createaccount/{index.php => page.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Website_ASU2024/createaccount/{index.php => page.php} (94%) diff --git a/Website_ASU2024/createaccount/index.php b/Website_ASU2024/createaccount/page.php similarity index 94% rename from Website_ASU2024/createaccount/index.php rename to Website_ASU2024/createaccount/page.php index f2e6051..13c70bf 100644 --- a/Website_ASU2024/createaccount/index.php +++ b/Website_ASU2024/createaccount/page.php @@ -7,4 +7,4 @@

Greybox - Cybercafe

Place holder

- \ No newline at end of file + From 551696d5f6a9635be1f8b89667f2d03544e05a1a Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Sun, 23 Mar 2025 23:32:53 -0700 Subject: [PATCH 006/127] Rename index.php to page.php --- Website_ASU2024/about/{index.php => page.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Website_ASU2024/about/{index.php => page.php} (94%) diff --git a/Website_ASU2024/about/index.php b/Website_ASU2024/about/page.php similarity index 94% rename from Website_ASU2024/about/index.php rename to Website_ASU2024/about/page.php index f2e6051..13c70bf 100644 --- a/Website_ASU2024/about/index.php +++ b/Website_ASU2024/about/page.php @@ -7,4 +7,4 @@

Greybox - Cybercafe

Place holder

- \ No newline at end of file + From e71f6416e29ec4b62ff37abb1e16c5d7a29ee079 Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Mon, 24 Mar 2025 00:36:39 -0700 Subject: [PATCH 007/127] removed some echo lines removed echo lines since daemon will now mostly be called from command line also uncommented clear_internet_sessions so that shutdown will clear internet sessions --- Backend/Cybercafe_daemon.sh | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Backend/Cybercafe_daemon.sh b/Backend/Cybercafe_daemon.sh index 5283b5e..6e007c3 100644 --- a/Backend/Cybercafe_daemon.sh +++ b/Backend/Cybercafe_daemon.sh @@ -15,32 +15,25 @@ while true; do #Case 1: Hotspot up and was up in previous check if [[ $HS_STATUS == 'up' ]] && [[ $HS_STATUS_PREV == 'up' ]]; then - printf "%s" "$(date +%T)" \ - && echo ": Status normal" check_internet_sessions #Case 2: Hotspot up and was down in previous check elif [[ $HS_STATUS == 'up' ]] && [[ $HS_STATUS_PREV == 'down' ]]; then - printf "%s" "$(date +%T)" \ - && echo ": Hotspot up" setup_infrastructure #Case 3: Hotspot down and was up in previous check elif [[ $HS_STATUS == 'down' ]] && [[ $HS_STATUS_PREV == 'up' ]]; then # Assume hotspot has recently gone down. Clean up. - printf "%s" "$(date +%T)" \ - && echo ": Hotspot went down" clear_internet_sessions shutdown_infrastructure #Case 4: Hotspot down and was down in previous check else - printf "%s" "$(date +%T)" \ - && echo ": Hotspot down" + : fi if [[ -f ./shutdown.confirmed ]]; then - #clear_internet_sessions + clear_internet_sessions shutdown_infrastructure exit fi From 73f48efee825c1535b6c0f4e68ce9501a699bbae Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Mon, 24 Mar 2025 00:38:22 -0700 Subject: [PATCH 008/127] debugging check_internet_sessions had error with session_age and resolving some of the variables as octal which would give a number outside of base error when '09' or '08' appeared changed some other stuff as well --- Backend/Cybercafe_internetSessionFunctions.sh | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/Backend/Cybercafe_internetSessionFunctions.sh b/Backend/Cybercafe_internetSessionFunctions.sh index bfaa234..8315fc5 100644 --- a/Backend/Cybercafe_internetSessionFunctions.sh +++ b/Backend/Cybercafe_internetSessionFunctions.sh @@ -6,7 +6,7 @@ ##INCLUDES## ##VARIABLES## -DATABASE_PATH='../database/CyberCafe_Database.db' +DATABASE_PATH='/data/data/com.termux/files/usr/var/www/database/CyberCafe_Database.db' function clear_internet_sessions { @@ -27,8 +27,8 @@ function clear_internet_sessions function delete_user_iptable_rules { # Argument1: $1 -> user_ip - iptables -t mangle -D iptmon_rx -d ${1} -i ${HS_INTERFACE} &> /dev/null # delete rules since this user is effectively logged out - iptables -t mangle -D iptmon_tx -s ${1} -i ${HS_INTERFACE} &> /dev/null + iptables -t mangle -D iptmon_rx -d ${1} &> /dev/null # delete rules since this user is effectively logged out + iptables -t mangle -D iptmon_tx -s ${1} &> /dev/null iptables -t nat -D PREROUTING -p all -s ${1} -i ${HS_INTERFACE} -j RETURN &> /dev/null #delete access outside of the captive portal using iptables } @@ -73,9 +73,9 @@ function remove_session fi #2. Create entry - sqlite3 $DATABASE_PATH "INSERT INTO user_data_usage (user_id,session_number,session_entry_index,entry_datetime,interval_bytes_tx,interval_bytes_rx) VALUES (${USER_ID},${SESSION_NUMBER},${ENTRY_INDEX},'${DATETIME}',${INTERVAL_TX},${INTERVAL_RX})" + sqlite3 $DATABASE_PATH "INSERT INTO user_data_usage (user_id,session_number,session_entry_index,entry_datetime,interval_bytes_tx,interval_bytes_rx) VALUES (${USER_ID},${SESSION_NUMBER},${ENTRY_INDEX},'${DATETIME}',${INTERVAL_TX},${INTERVAL_RX})" &> /dev/null #3. Delete internet session - sqlite3 $DATABASE_PATH "DELETE FROM internet_sessions WHERE table_index=${1}" # delete entry from internet_sessions so that the session doesn't exist anymore + sqlite3 $DATABASE_PATH "DELETE FROM internet_sessions WHERE table_index=${1}" &> /dev/null # delete entry from internet_sessions so that the session doesn't exist anymore #4. Delete iptable rules delete_user_iptable_rules $USER_IP fi @@ -101,7 +101,7 @@ function check_against_usage_limits #2. Check the lane of the user LANE_ID=$(sqlite3 $DATABASE_PATH "SELECT lane_id FROM users WHERE user_id=${USER_ID}") if [[ $LANE_ID == '' ]]; then - LANE_ID=$((2)) + LANE_ID=$((0)) fi #SELECT lane_id FROM users WHERE user_id=${USER_ID}; #3. Do logical comparisions @@ -131,11 +131,11 @@ function check_internet_sessions # 2. Check that a rule exists to record data usage (iptables) USER_IP=$(echo $RESPONSE | cut -f 4 -d '|') - iptables -t mangle -C iptmon_rx -d ${USER_IP} -i ${HS_INTERFACE} &> /dev/null + iptables -t mangle -C iptmon_rx -d ${USER_IP} &> /dev/null if [[ $? -eq 1 ]] && [[ $RESPONSE != '' ]]; then #rule doesn't exist but should - iptables -t mangle -A iptmon_rx -d ${USER_IP} -i ${HS_INTERFACE} > /dev/null - iptables -t mangle -A iptmon_tx -s ${USER_IP} -i ${HS_INTERFACE} > /dev/null + iptables -t mangle -A iptmon_rx -d ${USER_IP} &> /dev/null + iptables -t mangle -A iptmon_tx -s ${USER_IP} &> /dev/null fi # 3. Update data usage according to ip table onto internet_session table @@ -143,21 +143,21 @@ function check_internet_sessions if [[ $SESSION_ACCESS == '1' ]]; then SESSION_TX=$(($(iptables -t mangle -L iptmon_tx -vxn | grep $USER_IP | awk '{print $2}'))) SESSION_RX=$(($(iptables -t mangle -L iptmon_rx -vxn | grep $USER_IP | awk '{print $2}'))) - sqlite3 $DATABASE_PATH "UPDATE internet_sessions SET session_tx=${SESSION_TX},session_rx=${SESSION_RX} WHERE table_index=${I}" + sqlite3 $DATABASE_PATH "UPDATE internet_sessions SET session_tx=${SESSION_TX},session_rx=${SESSION_RX} WHERE table_index=${I}" &> /dev/null fi - # 4. update idle time based on metrics from user_data_usage table + # 4. update datetime since last request based on metrics from user_data_usage table USER_ID=$(sqlite3 $DATABASE_PATH "SELECT user_id FROM internet_sessions WHERE table_index=${I}") - RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT MAX(entry_datetime) FROM user_data_usage WHERE user_id=${USER_ID}") + RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT MAX(entry_datetime) FROM user_data_usage WHERE user_id=${USER_ID} AND interval_bytes_tx+interval_bytes_rx!=0") if [[ $RESPONSE != '' ]]; then - sqlite3 $DATABASE_PATH "UPDATE internet_sessions SET datetime_sinceLastRequest='${RESPONSE}' WHERE table_index=${I}" + sqlite3 $DATABASE_PATH "UPDATE internet_sessions SET datetime_sinceLastRequest='${RESPONSE}' WHERE table_index=${I}" &> /dev/null fi # 5. If session age or session idle time becomes to great then save that sessions data and delete the session RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT timediff((SELECT datetime_created FROM internet_sessions WHERE table_index=${I}),datetime(datetime(),'localtime'))") - SESSION_AGE=$((86400*$(echo $RESPONSE | awk '{print $1}' | cut -f 4 -d '-')+3600*$(echo $RESPONSE | awk '{print $2}' | cut -f 1 -d ':')+60*$(echo $RESPONSE | awk '{print $2}' | cut -f 2 -d ':')+$(echo $RESPONSE | awk '{print $2}' | cut -f 3 -d ':' | cut -f 1 -d '.'))) + SESSION_AGE=$((86400*$((10#$(echo $RESPONSE | awk '{print $1}' | cut -f 4 -d '-')))+3600*$((10#$(echo $RESPONSE | awk '{print $2}' | cut -f 1 -d ':')))+60*$((10#$(echo $RESPONSE | awk '{print $2}' | cut -f 2 -d ':')))+$((10#$(echo $RESPONSE | awk '{print $2}' | cut -f 3 -d ':' | cut -f 1 -d '.'))))) RESPONSE=$(sqlite3 $DATABASE_PATH "SELECT timediff((SELECT datetime_sinceLastRequest FROM internet_sessions WHERE table_index=${I}),datetime(datetime(),'localtime'))") - SESSION_IDLETIME=$((10#86400*$(echo $RESPONSE | awk '{print $1}' | cut -f 4 -d '-')+10#3600*$(echo $RESPONSE | awk '{print $2}' | cut -f 1 -d ':')+10#60*$(echo $RESPONSE | awk '{print $2}' | cut -f 2 -d ':')+$(echo $RESPONSE | awk '{print $2}' | cut -f 3 -d ':' | cut -f 1 -d '.'))) + SESSION_IDLETIME=$((86400*$((10#$(echo $RESPONSE | awk '{print $1}' | cut -f 4 -d '-')))+3600*$((10#$(echo $RESPONSE | awk '{print $2}' | cut -f 1 -d ':')))+60*$((10#$(echo $RESPONSE | awk '{print $2}' | cut -f 2 -d ':')))+$((10#$(echo $RESPONSE | awk '{print $2}' | cut -f 3 -d ':' | cut -f 1 -d '.'))))) if [[ ${SESSION_AGE} -gt 43200 ]] || [[ ${SESSION_IDLETIME} -gt 3600 ]]; then #if session is older than 12 hours or has been idle for more than 1hr then delete session remove_session $I @@ -196,22 +196,24 @@ function check_internet_sessions INTERVAL_RX=$(($SESSION_RX-$RESPONSE)) #same as above fi #create entry for this check in database - sqlite3 $DATABASE_PATH "INSERT INTO user_data_usage (user_id,session_number,session_entry_index,entry_datetime,interval_bytes_tx,interval_bytes_rx) VALUES (${USER_ID},${SESSION_NUMBER},${ENTRY_INDEX},'${DATETIME}',${INTERVAL_TX},${INTERVAL_RX})" + sqlite3 $DATABASE_PATH "INSERT INTO user_data_usage (user_id,session_number,session_entry_index,entry_datetime,interval_bytes_tx,interval_bytes_rx) VALUES (${USER_ID},${SESSION_NUMBER},${ENTRY_INDEX},'${DATETIME}',${INTERVAL_TX},${INTERVAL_RX})" &> /dev/null #7. Calculate current usage and determine if given user is outside their limits check_against_usage_limits $I if [[ $? -eq 1 ]]; then #user is over limits, so do necessary updates - sqlite3 $DATABASE_PATH "UPDATE internet_sessions SET session_access='0' WHERE table_index=${I}" + sqlite3 $DATABASE_PATH "UPDATE internet_sessions SET session_access='0' WHERE table_index=${I}" &> /dev/null + else + sqlite3 $DATABASE_PATH "UPDATE internet_sessions SET session_access='1' WHERE table_index=${I}" &> /dev/null fi - #8. Check access bit associated with user to see if iptable rules need to be updated + #8. Check user access entry for user to see if PREROUTING iptable rules need to be updated for this user iptables -t nat -C PREROUTING -s ${USER_IP} -i ${HS_INTERFACE} -j RETURN &> /dev/null if [[ $? -eq 0 && $SESSION_ACCESS == '0' ]]; then #rule exists but shouldn't - iptables -t nat -D PREROUTING -p all -s ${USER_IP} -i ${HS_INTERFACE} -j RETURN > /dev/null + iptables -t nat -D PREROUTING -p all -s ${USER_IP} -i ${HS_INTERFACE} -j RETURN &> /dev/null fi iptables -t nat -C PREROUTING -s ${USER_IP} -i ${HS_INTERFACE} -j RETURN &> /dev/null if [[ $? -eq 1 && $SESSION_ACCESS == '1' ]]; then #rule doesn't exist but should - iptables -t nat -I PREROUTING 2 -p all -s ${USER_IP} -i ${HS_INTERFACE} -j RETURN > /dev/null + iptables -t nat -I PREROUTING 2 -p all -s ${USER_IP} -i ${HS_INTERFACE} -j RETURN &> /dev/null fi From 82513a6de708892ad92e9ddea54845787ce94527 Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Mon, 24 Mar 2025 00:39:57 -0700 Subject: [PATCH 009/127] changed table from PREROUTING to POSTROUTING for iptmon_rx to work properly it needs to intercept the packet before it leaves the wlan1 interface which doesn't happen from the FORWARD mangle table also some misc changes --- Backend/Cybercafe_setupFunctions.sh | 53 +++++++++++------------------ 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/Backend/Cybercafe_setupFunctions.sh b/Backend/Cybercafe_setupFunctions.sh index 03d9111..21fb006 100644 --- a/Backend/Cybercafe_setupFunctions.sh +++ b/Backend/Cybercafe_setupFunctions.sh @@ -21,7 +21,7 @@ function check_hotspot_status #Checks whether the device's hotspot feature has been turned on or off { HS_STATUS_PREV=$HS_STATUS #save previous status from last check - ip add show dev $HS_INTERFACE | grep UP > /dev/null # Does it appear the hotspot is active? + ip add show dev $HS_INTERFACE | grep UP &> /dev/null # Does it appear the hotspot is active? if [[ $? -eq 0 ]]; then HS_STATUS='up' else @@ -35,42 +35,35 @@ function setup_infrastructure { # We're in hotspot mode but unsure if we're properly configured for CyberCafe operation LOCAL_IP=$(ifconfig $HS_INTERFACE | grep 'inet addr' | awk '{print $2}' | cut -d: -f2) - echo $HS_INTERFACE IP: $LOCAL_IP #check if iptmon_tx table exists - iptables -t mangle -C FORWARD -j iptmon_tx > /dev/null 2>&1 + iptables -t mangle -C FORWARD -j iptmon_tx > /dev/null &> /dev/null if [[ $? -ne 0 ]]; then - echo "Creating table ipmon_tx" - iptables -t mangle -N iptmon_tx + iptables -t mangle -N iptmon_tx > /dev/null &> /dev/null - echo "Adding iptmon_tx to FORWARD chain, 'mangle' table" - iptables -t mangle -A FORWARD -j iptmon_tx + iptables -t mangle -A FORWARD -j iptmon_tx &> /dev/null fi ## iptmon_rx - iptables -t mangle -C FORWARD -j iptmon_rx > /dev/null 2>&1 + iptables -t mangle -C POSTROUTING -j iptmon_rx &> /dev/null if [[ $? -ne 0 ]]; then - echo "Creating table iptmon_rx" - iptables -t mangle -N iptmon_rx + iptables -t mangle -N iptmon_rx &> /dev/null - echo "Adding iptmon_rx to FORWARD chain, 'mangle' table" - iptables -t mangle -A FORWARD -j iptmon_rx + iptables -t mangle -A POSTROUTING -j iptmon_rx &> /dev/null fi # Default iptables redirect - iptables -t nat -C PREROUTING -p tcp -i ${HS_INTERFACE} -j DNAT --to-destination ${LOCAL_IP}:80 > /dev/null 2>&1 + iptables -t nat -C PREROUTING -p tcp -i ${HS_INTERFACE} -j DNAT --to-destination ${LOCAL_IP}:80 &> /dev/null if [[ $? -ne 0 ]]; then - echo "Creating default redirect rule" - iptables -t nat -A PREROUTING -p udp -d ${LOCAL_IP} --dport 53 -j RETURN - iptables -t nat -A PREROUTING -p udp -s 0.0.0.0/32 -d 255.255.255.255/32 --dport 67 -j RETURN - iptables -t nat -A PREROUTING -p tcp -i ${HS_INTERFACE} -j DNAT --to-destination ${LOCAL_IP}:80 + iptables -t nat -A PREROUTING -p udp -d ${LOCAL_IP} --dport 53 -j RETURN > /dev/null &> /dev/null + iptables -t nat -A PREROUTING -p udp -s 0.0.0.0/32 -d 255.255.255.255/32 --dport 67 -j RETURN &> /dev/null + iptables -t nat -A PREROUTING -p tcp -i ${HS_INTERFACE} -j DNAT --to-destination ${LOCAL_IP}:80 &> /dev/null fi # Traffic Control # tc qdisc show dev $HS_INTERFACE | grep htb > /dev/null # if [[ $? -ne 0 ]]; then -# echo "Creating tc qdisc HTB queues" # tc qdisc add dev $HS_INTERFACE root handle 1: htb default 10 # tc class add dev $HS_INTERFACE parent 1: classid 1:1 htb rate 15mbps ceil 15mbps # tc class add dev $HS_INTERFACE parent 1:1 classid 1:10 htb rate 15mbps ceil 15mbps @@ -78,7 +71,7 @@ function setup_infrastructure # fi # Check captive portal HTTPD server - if ! pgrep lighttpd > /dev/null; then + if ! pgrep lighttpd &> /dev/null; then start_captive_webserver fi } @@ -87,48 +80,42 @@ function setup_infrastructure function shutdown_infrastructure #remove any necessary infrastructure for running the CyberCafe system { - printf "%s" "$(date +%T)" - echo " Shutting down..." pkill lighttpd LOCAL_IP='' HS_STATUS='down' - echo "Deleting iptmon tables." - iptables -t mangle -D FORWARD -j iptmon_tx > /dev/null 2>&1 - iptables -t mangle -D FORWARD -j iptmon_rx > /dev/null 2>&1 + iptables -t mangle -D FORWARD -j iptmon_tx &> /dev/null + iptables -t mangle -D PREROUTING -j iptmon_rx &> /dev/null while true; do - iptables -t mangle -D iptmon_rx 1 > /dev/null 2>&1 + iptables -t mangle -D iptmon_rx 1 &> /dev/null if [[ $? == 1 ]]; then break fi done - iptables -t mangle -X iptmon_rx > /dev/null 2>&1 + iptables -t mangle -X iptmon_rx &> /dev/null while true; do - iptables -t mangle -D iptmon_tx 1 > /dev/null 2>&1 + iptables -t mangle -D iptmon_tx 1 &> /dev/null if [[ $? == 1 ]]; then break fi done - iptables -t mangle -X iptmon_tx > /dev/null 2>&1 + iptables -t mangle -X iptmon_tx &> /dev/null - echo "Removing prerouting rules." while true; do - iptables -t nat -D PREROUTING 2 > /dev/null 2>&1 + iptables -t nat -D PREROUTING 2 &> /dev/null if [[ $? == 1 ]]; then break fi done -# echo "Cleaning up ${HS_INTERFACE} qdisc" # tc qdisc delete dev $HS_INTERFACE root - echo "Done." } function start_captive_webserver #starts the lighttpd webserver that acts as a captive web portal for sign in and such { - $APP_PATH/bin/lighttpd -f $APP_PATH/etc/lighttpd/lighttpd.conf + $APP_PATH/bin/lighttpd -f $APP_PATH/etc/lighttpd/lighttpd.conf &> /dev/null } \ No newline at end of file From aefcdf78e5e9901bd1336d99f81c68d427a8caa6 Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Mon, 24 Mar 2025 00:41:20 -0700 Subject: [PATCH 010/127] Delete Backend/archive directory not in use anymore and code is completely irrelevent --- Backend/archive/CyberCafe_UpdateUserUsage.sh | 7 --- .../UserModify_IptablesRules_AtomicScript.sh | 46 ------------------- 2 files changed, 53 deletions(-) delete mode 100644 Backend/archive/CyberCafe_UpdateUserUsage.sh delete mode 100644 Backend/archive/UserModify_IptablesRules_AtomicScript.sh diff --git a/Backend/archive/CyberCafe_UpdateUserUsage.sh b/Backend/archive/CyberCafe_UpdateUserUsage.sh deleted file mode 100644 index baaab98..0000000 --- a/Backend/archive/CyberCafe_UpdateUserUsage.sh +++ /dev/null @@ -1,7 +0,0 @@ -echo "HI" - - - -while true; do - -done \ No newline at end of file diff --git a/Backend/archive/UserModify_IptablesRules_AtomicScript.sh b/Backend/archive/UserModify_IptablesRules_AtomicScript.sh deleted file mode 100644 index 51a7e5a..0000000 --- a/Backend/archive/UserModify_IptablesRules_AtomicScript.sh +++ /dev/null @@ -1,46 +0,0 @@ -#Greybox - Cybercafe -#Script: UserModify_IptablesRules_AtomicScript.sh -#Desc: Add and remove user rules to the iptables of the device to allow outside access and record data usage - -DEFAULT_INSERT_LINE=2 -HS_INTERFACE='wlan1' -user_ip=$2 - -#Argument Validation -if [[ $# != 2 ]]; then - echo "Invalid number of arguments." - echo "Usage: ./UserModify_IptablesRules_AtomicScript [+/-] [user_ip]" - exit 1 -fi - -#Adding Rule for Use -if [[ $1 == + ]]; then - iptables -t nat -C PREROUTING -p tcp -s ${user_ip} -i ${HS_INTERFACE} -j RETURN > /dev/null 2>&1 - if [[ $? -ne 0 ]]; then - #rules to add - iptables -t nat -I PREROUTING $DEFAULT_INSERT_LINE -p tcp -s ${user_ip} -i ${HS_INTERFACE} -j RETURN > /dev/null 2>&1 - #record tcp packets incoming to this user - iptables -t mangle -A iptmon_rx -p tcp -d ${user_ip} -i ${HS_INTERFACE} > /dev/null 2>&1 - #record tcp packets outgoing from this user - iptables -t mangle -A iptmon_tx -p tcp -s ${user_ip} -i ${HS_INTERFACE} > /dev/null 2>&1 - fi - exit 0 -fi - -#Remove Rule for User -if [[ $1 == - ]]; then - iptables -t nat -C PREROUTING -p tcp -s ${user_ip} -i ${HS_INTERFACE} -j RETURN > /dev/null 2>&1 - if [[ $? -eq 0 ]]; then - #rules to remove - iptables -t nat -D PREROUTING -p tcp -s ${user_ip} -i ${HS_INTERFACE} -j RETURN > /dev/null 2>&1 - #record tcp packets incoming to this user - iptables -t mangle -D iptmon_rx -p tcp -d ${user_ip} -i ${HS_INTERFACE} > /dev/null 2>&1 - #record tcp packets outgoing from this user - iptables -t mangle -D iptmon_tx -p tcp -s ${user_ip} -i ${HS_INTERFACE} > /dev/null 2>&1 - fi - exit 0 -fi - -echo "Invalid Syntax" -echo "Usage: ./UserModify_IptablesRules_AtomicScript [+/-] [user_ip]" -exit 1 From 08b40093c3ff9d0283d6fafe74436e5b7e1d9ebe Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Mon, 24 Mar 2025 00:41:59 -0700 Subject: [PATCH 011/127] made /home the landing page --- Website_ASU2024/page.php | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Website_ASU2024/page.php b/Website_ASU2024/page.php index b633be2..c49cc21 100644 --- a/Website_ASU2024/page.php +++ b/Website_ASU2024/page.php @@ -1,12 +1,3 @@ - - - - Cybercafe Demo - - -

Greybox - Cybercafe

- Login Page
- Create Account
- About
- - + \ No newline at end of file From 21eddba27a57dd4a072ef749b8552c7ee11aa9e9 Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Mon, 24 Mar 2025 00:43:50 -0700 Subject: [PATCH 012/127] changed navigation, added remove old session functionality general debugging and making webpage easier to navigate --- Website_ASU2024/login/page.php | 95 ++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 33 deletions(-) diff --git a/Website_ASU2024/login/page.php b/Website_ASU2024/login/page.php index 6ec20e3..35a2273 100644 --- a/Website_ASU2024/login/page.php +++ b/Website_ASU2024/login/page.php @@ -1,12 +1,14 @@ query("SELECT * FROM users WHERE username='".$username."'"); $responseArray = $response->fetchArray(); if($responseArray['password']==$password) @@ -14,6 +16,16 @@ function logIn($username,$password,$session_id) $user_level = (int)$responseArray['user_level']; $user_id = (int)$responseArray['user_id']; $lane_id = (int)$responseArray['lane_id']; + #check to see if there are any sessions already active with this user_id + $response2 = $db->query("SELECT * FROM internet_sessions WHERE user_id='".$user_id."'"); + $responseArray2 = $response2->fetchArray(); + if($responseArray2) + { + #Must close database temporarily so process write mutex lock for the database can be given to bash script + $db->close(); + exec("bash '".$GLOBALS['internetSessionFunctionsShellScript_path']."' remove_session ".((int)$responseArray2['table_index'])); + $db = new SQLite3($GLOBALS['database_path']); + } if($user_level=='0') { $sessionAccessBit = 1; @@ -22,27 +34,18 @@ function logIn($username,$password,$session_id) { $sessionAccessBit = 0; } - #Create New Internet Session to write to database $response = $db->query("SELECT MAX(table_index) FROM internet_sessions"); $responseArray = $response->fetchArray(); - if($responseArray) + $response2 = $db->query("SELECT datetime_created FROM internet_sessions"); + $responseArray2 = $response2->fetchArray(); + if($responseArray2['datetime_created']==NULL) { - $internetSessionIndex = (int)$responseArray['table_index']+1; + $internetSessionIndex=0; } else { - $internetSessionIndex = 0; - } - #check to see if there are any sessions already active with this user_id - $response = $db->query("SELECT * FROM internet_sessions WHERE user_id='".$user_id."'"); - $responseArray = $response->fetchArray(); - #remove the old session associated with this user_id if it exists. - if($responseArray) - { - shell_exec("bash ../../backend/Cybercafe_internetSessionFunctions.sh remove_session ".$responseArray['table_index']); + $internetSessionIndex=((int)$responseArray[0])+1; } - #TODO: create method to check if this new session will already be out of range of their data limits - #get datetime info $datetimeObj = new DateTime(); $datetime = $datetimeObj->format('Y-m-d H:i:s'); $db->exec("INSERT INTO internet_sessions ( @@ -64,9 +67,6 @@ function logIn($username,$password,$session_id) ".$sessionAccessBit.", '".$datetime."', '".$datetime."')"); - ##DEBUG## - echo "

\$db->exec(\"INSERT INTO internet_sessions (
table_index,
user_id,
session_id,
ip,
session_tx,
session_rx,
session_access,
datetime_created,
datetime_sinceLastRequest)
VALUES(
".$internetSessionIndex.",
".$user_id.",
'".$session_id."',
'".$_SERVER['REMOTE_ADDR']."',
0,0,
".$sessionAccessBit.",
'".$datetime."',
'".$datetime."')\");

"; - # $db->close(); return true; } @@ -79,7 +79,7 @@ function hashPassword($passwordString) } function validSessionID($session_id) { - $db = new SQLite3('../../database/CyberCafe_Database.db'); + $db = new SQLite3($GLOBALS['database_path']); $response = $db->query("SELECT 1 FROM internet_sessions WHERE session_id='".$session_id."'"); $result = $response->fetchArray(); @@ -107,7 +107,7 @@ function validSessionID($session_id) $session_id = session_create_id(); if(logIn($_POST['username'],$_POST['password'],$session_id)) { - setcookie("session_id",$session_id,time()+(3600),"/"); + setcookie("session_id",$session_id,time()+43200,"/"); header('Location: ../home'); die(); } @@ -118,22 +118,51 @@ function validSessionID($session_id) } else { - echo ' - + echo ' - Cybercafe Demo - Log-In + Cybercafe Demo + + + - -

Log-In

-
-
-
-
-
-
-
+ + +

Log-In

+
+
+
+
+
+
+
'; } From e35b5689f1ee7193aa1364f00d14331e3a543ee3 Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Mon, 24 Mar 2025 00:44:56 -0700 Subject: [PATCH 013/127] changes to navigation, page style, verification structure lots of changes --- Website_ASU2024/home/page.php | 150 +++++++++++++++++++++++++++------- 1 file changed, 119 insertions(+), 31 deletions(-) diff --git a/Website_ASU2024/home/page.php b/Website_ASU2024/home/page.php index db624c4..8fa5198 100644 --- a/Website_ASU2024/home/page.php +++ b/Website_ASU2024/home/page.php @@ -1,52 +1,145 @@ - Cybercafe Demo - Home + Cybercafe Demo + + + - - -

Admin Page

-
- -
+ + +

Admin Page

'; } -function userPage() +function userHomePage() { echo ' - Cybercafe Demo - Home + Cybercafe Demo + + + + + +

User Page

-

User Page

- + '; +} +function defaultHomePage() +{ + echo ' + + + Cybercafe Demo + + + + + + + +

Greybox - Cybercafe

'; } if(isset($_COOKIE['session_id'])) { - $db = new SQLite3('../../database/CyberCafe_Database.db'); + $db = new SQLite3($GLOBALS['database_path']); $response = $db->query("SELECT user_id FROM internet_sessions WHERE session_id='".$_COOKIE['session_id']."'"); $responseArray = $response->fetchArray(); #if there is an internet session found matching the query then load respective page @@ -57,15 +150,11 @@ function userPage() $responseArray2 = $response2->fetchArray(); if($responseArray2['user_level']==0) { - adminPage(); - if(array_key_exists('test',$_POST)) - { - runScript(); - } + adminHomePage(); } else if($responseArray2['user_level']==1) { - userPage(); + userHomePage(); } } #if there is no internet session matching the cookie then return to login @@ -76,9 +165,8 @@ function userPage() } $db->close(); } -#if session_id is not set users should be redirected to login else { - header('Location: /login'); + defaultHomePage(); } -?> +?> \ No newline at end of file From 8967792d1860652fc617f4f86a25a30ca6f1242d Mon Sep 17 00:00:00 2001 From: Grey-box-johnfischer Date: Mon, 24 Mar 2025 00:45:23 -0700 Subject: [PATCH 014/127] New admin pages for managment --- Website_ASU2024/home/ManageLanes/page.php | 288 +++++++++++++++++++++ Website_ASU2024/home/ManageUsers/page.php | 292 ++++++++++++++++++++++ 2 files changed, 580 insertions(+) create mode 100644 Website_ASU2024/home/ManageLanes/page.php create mode 100644 Website_ASU2024/home/ManageUsers/page.php diff --git a/Website_ASU2024/home/ManageLanes/page.php b/Website_ASU2024/home/ManageLanes/page.php new file mode 100644 index 0000000..a39cdbc --- /dev/null +++ b/Website_ASU2024/home/ManageLanes/page.php @@ -0,0 +1,288 @@ +exec("UPDATE data_lanes SET bytelimit_daily=".$bytelimit." WHERE lane_id=".$lane_id); + $db->close(); +} + +function updateByteLimitWeekly($lane_id,$bytelimit) +{ + $bytelimit=$bytelimit*(10**6); + $db = new SQLite3($GLOBALS['database_path']); + $db->exec("UPDATE data_lanes SET bytelimit_weekly=".$bytelimit." WHERE lane_id=".$lane_id); + $db->close(); +} + +function updateByteLimitMonthly($lane_id,$bytelimit) +{ + $bytelimit=$bytelimit*(10**6); + $db = new SQLite3($GLOBALS['database_path']); + $db->exec("UPDATE data_lanes SET bytelimit_monthly=".$bytelimit." WHERE lane_id=".$lane_id); + $db->close(); +} + +function removeLane($lane_id) +{ + $db = new SQLite3($GLOBALS['database_path']); + $db->exec("DELETE FROM data_lanes WHERE lane_id=".$lane_id); + $db->close(); +} + +function newLane() +{ + $db = new SQLite3($GLOBALS['database_path']); + $response=$db->query("SELECT MAX(lane_id) FROM data_lanes"); + $responseArray=$response->fetchArray(); + if($response) + { + $nextLaneID=$responseArray[0]+1; + } + else + { + $nextLaneID=0; + } + $db->exec("INSERT INTO data_lanes (lane_id,lane_name,bytelimit_daily,bytelimit_weekly,bytelimit_monthly) VALUES (".$nextLaneID.",'new lane',0,0,0)"); + $db->close(); +} + +function renameLane($land_id,$newName) +{ + $db = new SQLite3($GLOBALS['database_path']); + $db->exec("UPDATE data_lanes SET lane_name='".$newName."' WHERE lane_id=".$lane_id); + $db->close(); +} + +function displayPage() +{ + $db = new SQLite3($GLOBALS['database_path']); + $response = $db->query("SELECT * FROM data_lanes"); + $table_entries=""; + $i=0; + while($responseArray=$response->fetchArray()) + { + $lane_id=$responseArray['lane_id']; + $lane_name=$responseArray['lane_name']; + $bytelimit_daily=$responseArray['bytelimit_daily']/(10**6); + $bytelimit_weekly=$responseArray['bytelimit_weekly']/(10**6); + $bytelimit_monthly=$responseArray['bytelimit_monthly']/(10**6); + $response2=$db->query("SELECT username FROM users WHERE lane_id=".$lane_id); + $responseArray2=$response2->fetchArray(SQLITE3_NUM); + if($responseArray2) + { + $usersInLane=sizeof($responseArray2); + } + else + { + $usersInLane=0; + } + $table_entries=$table_entries." + ".$lane_id." +
+
+
+ ".$usersInLane." +
+