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