Spaces:
Paused
Paused
File size: 5,950 Bytes
6059389 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
#!/bin/bash
# Function to print the current hostname and user details
print_host_details() {
echo "* The hostname of this container is: $(cat /etc/hostname)"
#echo "* The host of this container is: $(cat /etc/hosts)"
echo "* ID of the user running the script: $(id -u) * Group: $(id -g) * Status of Admin: $(id admin 2>/dev/null || echo 'Admin user not found')"
}
# Function to generate SSH host keys if missing
generate_ssh_keys() {
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
echo "* Generating SSH host keys *"
ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N "" || { echo "Failed to generate RSA key"; exit 1; }
ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N "" || { echo "Failed to generate ECDSA key"; exit 1; }
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" || { echo "Failed to generate ED25519 key"; exit 1; }
fi
}
# Function to start SSH service if not running
start_ssh_service() {
if ! pgrep -x "sshd" >/dev/null; then
echo "* Starting SSH server on port 2222 *"
/usr/sbin/sshd -p 2222 || { echo "Failed to start SSH server"; exit 1; }
else
echo "* SSH server is already running *"
fi
}
# Function to create 'admin' user if missing
create_admin_user() {
if ! id -u admin >/dev/null 2>&1; then
echo "* Creating 'admin' user *"
useradd -m -s /bin/bash -G sudo admin || { echo "Failed to create admin user"; exit 1; }
echo "admin:password" | chpasswd || { echo "Failed to set admin password"; exit 1; }
echo "%sudo ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers || { echo "Failed to update sudoers"; exit 1; }
fi
}
# Function to fix permissions for 'admin' user
fix_admin_permissions() {
echo "* Fixing permissions for 'admin' user *"
chown -R admin:admin /home/admin || { echo "Failed to change ownership"; exit 1; }
chmod -R 700 /home/admin || { echo "Failed to set permissions"; exit 1; }
}
# Function to generate SSH keys for 'admin' user if missing
generate_admin_ssh_keys() {
if [ ! -f /home/admin/.ssh/id_rsa ]; then
echo "* Generating SSH keys for 'admin' user *"
mkdir -p /home/admin/.ssh || { echo "Failed to create .ssh directory"; exit 1; }
ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -q -N "" || { echo "Failed to generate admin SSH key"; exit 1; }
chmod 700 /home/admin/.ssh || { echo "Failed to set .ssh directory permissions"; exit 1; }
chmod 600 /home/admin/.ssh/id_rsa || { echo "Failed to set private key permissions"; exit 1; }
chmod 644 /home/admin/.ssh/id_rsa.pub || { echo "Failed to set public key permissions"; exit 1; }
fi
}
# Function to add public key to authorized_keys for 'admin' user
add_admin_authorized_keys() {
if [ ! -f /home/admin/.ssh/authorized_keys ]; then
echo "* Adding public key to authorized_keys for 'admin' user *"
bash -c "cat /home/admin/.ssh/id_rsa.pub >> /home/admin/.ssh/authorized_keys" || { echo "Failed to add public key to authorized_keys"; exit 1; }
chmod 600 /home/admin/.ssh/authorized_keys || { echo "Failed to set authorized_keys permissions"; exit 1; }
fi
}
# Function to add SSH private key to the SSH agent
add_ssh_key_to_agent() {
if ! ssh-add -l | grep -q '/home/admin/.ssh/id_rsa'; then
echo "* Adding SSH private key to the SSH agent *"
ssh-add /home/admin/.ssh/id_rsa || { echo "Failed to add SSH private key to agent"; exit 1; }
fi
}
# Function to activate virtual environment
activate_virtual_env() {
if [ -d "/app/WebSSHEnv" ]; then
echo "* Activating virtual environment *"
source /app/WebSSHEnv/bin/activate || { echo "Failed to activate virtual environment"; exit 1; }
else
echo "* Virtual environment not found, please check setup *"
fi
}
# Function to set working directory
set_working_directory() {
cd /app || { echo "Failed to change directory to /app"; exit 1; }
}
# Function to print the contents of the working directory
print_working_directory_contents() {
echo "* Contents of /app directory: *"
ls -la /app
}
# Function to run the WebSSH application
run_webssh_application() {
echo "* Starting WebSSH application *"
python3 -u -m WebSSH || { echo "Failed to start WebSSH application"; exit 1; }
}
# Function to change account when username and password are given as parameters
change_account() {
local username=$1
local password=$2
if id -u "$username" >/dev/null 2>&1; then
echo "* Trying Switching to user: $username *"
expect -c "
spawn su -p $username -c 'echo \"* Now running as: \$(whoami) *\"'
expect \"Password:\"
send \"$password\r\"
interact
"
expect -c "
spawn su -p $username
expect \"Password:\"
send \"$password\r\"
interact
"
echo "$password" | su -p "$username"
echo "* Now running as 2: $(whoami) *"
else
echo "User $username does not exist"
fi
}
# Function to keep the container running
keep_container_running() {
tail -f /dev/null
}
# Main script execution
print_host_details
generate_ssh_keys
start_ssh_service
#echo "password" | su - admin -c "whoami"
#create_admin_user
#change_account "admin" "password"
fix_admin_permissions
generate_admin_ssh_keys
add_admin_authorized_keys
add_ssh_key_to_agent
netstat -tuln
#Test if admin's credentials are correct
echo "* Testing admin's SSH login locally *"
ssh -o StrictHostKeyChecking=no -i /home/admin/.ssh/id_rsa admin@127.0.0.1 -p 2222 exit
if [ $? -eq 0 ]; then
echo "* Admin credentials are valid."
else
echo "* Admin login failed! Check the password for 'admin' user." >&2
fi
echo "* Contents of id_rsa of Admin:"
cat /home/admin/.ssh/id_rsa
activate_virtual_env
set_working_directory
print_working_directory_contents
run_webssh_application
keep_container_running |