BinaryONe commited on
Commit
8d515de
·
1 Parent(s): d865ce5
Files changed (4) hide show
  1. Dockerfile +9 -14
  2. WebSSH/Dockerfile +0 -79
  3. init.sh +68 -4
  4. start.sh +32 -87
Dockerfile CHANGED
@@ -30,15 +30,13 @@ RUN apt-get update && \
30
  #echo "root:toor" | chpasswd &&\
31
 
32
  RUN useradd -m -u 1000 admin && \
33
- echo "admin:password" | chpasswd &&\
34
  usermod -aG sudo admin && \
35
- echo "admin ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
36
 
37
  COPY . /app
38
-
39
-
40
 
41
- # sudo ufw allow 2222/tcp
42
 
43
  #RUN sudo ufw disable
44
  RUN cp /app/sshd_config /etc/ssh/sshd_config # && cat /etc/ssh/sshd_config
@@ -52,13 +50,10 @@ RUN cp /app/sshd_config /etc/ssh/sshd_config # && cat /etc/ssh/sshd_config
52
  RUN touch /etc/ssh/ssh_known_hosts &&\
53
  touch /etc/ssh/known_hosts
54
 
55
- RUN chmod -R 777 /etc/ssh/* && \
56
- chown -R admin:admin /etc/ssh
57
-
58
-
59
-
60
- RUN echo " Public Key $(cat /etc/ssh/ssh_host_rsa_key.pub)" && \
61
- echo " Private Key $(cat /etc/ssh/ssh_host_rsa_key)"
62
 
63
 
64
 
@@ -110,8 +105,8 @@ EXPOSE 2222
110
 
111
  EXPOSE 7860
112
 
113
- RUN chown -R admin:admin /home/admin &&\
114
- chmod -R 777 /app /usr/sbin/sshd
115
 
116
  RUN echo "* Changing User to Admin :$(echo "password" || su - admin)"
117
 
 
30
  #echo "root:toor" | chpasswd &&\
31
 
32
  RUN useradd -m -u 1000 admin && \
33
+ echo "admin:password" | chpasswd && \
34
  usermod -aG sudo admin && \
35
+ echo "admin ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
36
 
37
  COPY . /app
 
 
38
 
39
+ RUN sudo ufw allow 2222/tcp
40
 
41
  #RUN sudo ufw disable
42
  RUN cp /app/sshd_config /etc/ssh/sshd_config # && cat /etc/ssh/sshd_config
 
50
  RUN touch /etc/ssh/ssh_known_hosts &&\
51
  touch /etc/ssh/known_hosts
52
 
53
+ RUN chmod -R 777 /etc/ssh/*
54
+
55
+ #RUN echo " Public Key $(cat /etc/ssh/ssh_host_rsa_key.pub)" && \
56
+ # echo " Private Key $(cat /etc/ssh/ssh_host_rsa_key)"
 
 
 
57
 
58
 
59
 
 
105
 
106
  EXPOSE 7860
107
 
108
+ #RUN chown -R admin:admin /home/admin &&\
109
+ # chmod -R 777 /app /usr/sbin/sshd
110
 
111
  RUN echo "* Changing User to Admin :$(echo "password" || su - admin)"
112
 
WebSSH/Dockerfile DELETED
@@ -1,79 +0,0 @@
1
- FROM alpine:latest
2
-
3
-
4
-
5
- ENV DEBIAN_FRONTEND=noninteractive
6
-
7
- # Install necessary packages
8
- RUN apk update && apk add --no-cache openssh openrc openssh-keygen bash sudo python3 py3-pip net-tools
9
-
10
- # Create a non-root user (admin)
11
- RUN adduser -D admin
12
- RUN echo "admin:password" | chpasswd
13
- RUN addgroup admin wheel
14
- RUN adduser admin wheel
15
-
16
- # SSH Configuration
17
- RUN mkdir -p /var/run/sshd /app /app/ssh
18
- RUN chmod -R 777 /app
19
-
20
- # SSH Configuration
21
- #RUN mkdir /app
22
-
23
- # Copy all the contents of the /app folder
24
- COPY . /app
25
-
26
-
27
- # Generate SSH keys and print them
28
- #RUN ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N "" && \
29
- # cat /etc/ssh/ssh_host_rsa_key && \
30
- # ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N "" && \
31
- # cat /etc/ssh/ssh_host_ecdsa_key && \
32
- # ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" && \
33
- # cat /etc/ssh/ssh_host_ed25519_key
34
-
35
- # Generate SSH keys
36
- RUN ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N "" && \
37
- ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N "" && \
38
- ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
39
-
40
- # Secure SSH Configuration
41
- RUN sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config && \
42
- sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config && \
43
- sed -i 's/#ChallengeResponseAuthentication yes/ChallengeResponseAuthentication no/' /etc/ssh/sshd_config && \
44
- sed -i 's/#UsePAM yes/UsePAM no/' /etc/ssh/sshd_config && \
45
- sed -i 's/#Port 22/Port 2222/' /etc/ssh/sshd_config && \
46
- echo "AllowUsers admin" >> /etc/ssh/sshd_config
47
-
48
-
49
-
50
- # Set the permissions for the SSH keys
51
- RUN chmod 777 /etc/ssh/ssh_host_* && \
52
- touch /app/ssh/ssh_known_hosts && \
53
- chmod 777 /app/ssh/ssh_* && \
54
- chmod 777 /home && \
55
- && cp -r /etc/ssh/* /app/ssh
56
-
57
- # List contents of /etc/ssh and /app/ssh
58
- RUN ls -l /etc/ssh/ && \
59
- ls -l /app/ssh/
60
-
61
- # Install WebSSH
62
- RUN python3 -m venv /app/venv && \
63
- /app/venv/bin/pip install --no-cache-dir --upgrade pip && \
64
- /app/venv/bin/pip install --no-cache-dir -r /app/WebSSH/requirements.txt && \
65
- /app/venv/bin/pip list
66
-
67
- # Expose the new SSH port
68
- EXPOSE 2222
69
-
70
- RUN chmod -R 777 /app
71
-
72
- # Copy the start.sh script
73
- #RUN chmod 777 /app/venv/lib/python3.12/site-packages/
74
- #RUN touch /app/venv/lib/python3.12/site-packages/known_hosts
75
- #RUN chmod 777 /app/venv/lib/python3.12/site-packages/known_hosts
76
-
77
- EXPOSE 7860
78
-
79
- CMD ["/app/start.sh"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
init.sh CHANGED
@@ -1,5 +1,69 @@
1
  #!/bin/bash
2
-
3
-
4
- echo "Hi There I am Booting up $(whoami)"
5
- /app/start.sh
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  #!/bin/bash
2
+ echo "* Hi There I am Booting up : $(whoami)"
3
+ # Function to print the current hostname and user details
4
+ print_host_details() {
5
+ #echo "0.0.0.0 abc" | sudo tee -a /etc/hosts
6
+ echo "* The hostname of this container is: $(cat /etc/hostname). PWD $(pwd)"
7
+ echo "* The host File of this container is: $(cat /etc/hosts)"
8
+ echo "* The Sudoers of this container is: $(sudo cat /etc/sudoers)"
9
+ echo "* ID of the user running the script: $(whoami) *ID : $(id -u) * Group: $(id -g)"
10
+ echo "* Changing User to Admin :$(echo "password" || su - admin)"
11
+ echo "* Current User WHO AM I $(whoami)"
12
+ echo "* Status of Admin : $(id admin 2>/dev/null || echo 'Admin user not found')"
13
+ }
14
+ # Function to generate SSH host keys if missing
15
+ generate_ssh_keys() {
16
+
17
+ sudo rm -rf /etc/ssh/ssh_host_rsa_*
18
+ sudo rm -rf /etc/ssh/ssh_host_ecdsa_*
19
+ sudo rm -rf /etc/ssh/ssh_host_ed25519_*
20
+ sudo rm -rf /etc/ssh/ssh_known_*
21
+
22
+ echo "* Generating SSH host keys *"
23
+ yes y | ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N "" || { echo "Failed to generate RSA key"; exit 1; }
24
+ yes y | ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N "" || { echo "Failed to generate ECDSA key"; exit 1; }
25
+ yes y | ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" || { echo "Failed to generate ED25519 key"; exit 1; }
26
+ ssh-keyscan -p 2222 127.0.0.1 >> /etc/ssh/ssh_known_hosts
27
+ cp /etc/ssh/ssh_known_hosts /etc/ssh/known_host
28
+ #ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ""
29
+ #ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N ""
30
+ #ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
31
+ echo "* Public Key:"
32
+ cat /etc/ssh/ssh_host_rsa_key.pub
33
+ echo "* Private Key :"
34
+ cat /etc/ssh/ssh_host_rsa_key
35
+ echo "* Host Files :"
36
+ cat /etc/ssh/ssh_known_host
37
+
38
+ }
39
+ # Function to activate virtual environment
40
+ activate_python_and_run() {
41
+ if [ -d "/app/WebSSHEnv" ]; then
42
+ echo "* Activating virtual environment *"
43
+ source /app/WebSSHEnv/bin/activate || { echo "Failed to activate virtual environment"; exit 1; }
44
+ echo "* Starting WebSSH application *"
45
+ python3 -u -m /app/WebSSH || { echo "Failed to start WebSSH application"; exit 1; }
46
+ else
47
+ echo "* Virtual environment not found, please check setup *"
48
+ fi
49
+ }
50
+ # Function to start SSH service if not running
51
+ start_ssh_service_with_status() {
52
+ if ! pgrep -x "sshd" >/dev/null; then
53
+ #service ssh restart
54
+ #service sshd restart
55
+ echo "* Starting SSH server at port 2222 *"
56
+ /usr/sbin/sshd -p 2222 || { echo "Failed to start SSH server"; exit 1; }
57
+ #/usr/sbin/sshd -D -e -ddd|| { echo "Failed to start SSH server"; exit 1; }
58
+ echo "* Status of SSH service: *"
59
+ netstat -tuln
60
+ else
61
+ echo "* SSH server is already running *"
62
+ fi
63
+ }
64
+
65
+ print_host_details
66
+ generate_ssh_keys
67
+ start_ssh_service_with_status
68
+ activate_python_and_run
69
+ /app/start.sh
start.sh CHANGED
@@ -1,66 +1,5 @@
1
  #!/bin/bash
2
 
3
- # Function to print the current hostname and user details
4
- print_host_details() {
5
- #echo "0.0.0.0 abc" | sudo tee -a /etc/hosts
6
- echo "* The hostname of this container is: $(cat /etc/hostname). PWD $(pwd)"
7
- echo "* The host File of this container is: $(cat /etc/hosts)"
8
- echo "* The Sudoers of this container is: $(cat /etc/sudoers)"
9
- echo "* ID of the user running the script:$(whoami) *ID : $(id -u) * Group: $(id -g)"
10
- echo "* Changing User to Admin :$(echo "password" || su - admin)"
11
- echo "* Current User WHO AM I $(whoami)"
12
- echo "* Status of Admin: $(id admin 2>/dev/null || echo 'Admin user not found')"
13
- }
14
- # Function to generate SSH host keys if missing
15
- generate_ssh_keys() {
16
-
17
- rm -rf /etc/ssh/ssh_host_rsa_*
18
- rm -rf /etc/ssh/ssh_host_ecdsa_*
19
- rm -rf /etc/ssh/ssh_host_ed25519_*
20
- rm -rf /etc/ssh/ssh_known_*
21
-
22
-
23
- echo "* Generating SSH host keys *"
24
- yes y | ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N "" || { echo "Failed to generate RSA key"; exit 1; }
25
- yes y | ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N "" || { echo "Failed to generate ECDSA key"; exit 1; }
26
- yes y | ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" || { echo "Failed to generate ED25519 key"; exit 1; }
27
- ssh-keyscan -p 2222 127.0.0.1 >> /etc/ssh/ssh_known_hosts
28
- cp /etc/ssh/ssh_known_hosts /etc/ssh/known_host
29
- #ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ""
30
- #ssh-keygen -t ecdsa -b 256 -f /etc/ssh/ssh_host_ecdsa_key -N ""
31
- #ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
32
- echo " Public Key:"
33
- cat /etc/ssh/ssh_host_rsa_key.pub
34
- echo " Private Key :"
35
- cat /etc/ssh/ssh_host_rsa_key
36
- echo " Host Files :"
37
- cat /etc/ssh/ssh_known_host
38
-
39
- }
40
- # Function to start SSH service if not running
41
- start_ssh_service() {
42
- if ! pgrep -x "sshd" >/dev/null; then
43
- service ssh restart
44
- service sshd restart
45
- echo "* Starting SSH server at port 2222 *"
46
- #/usr/sbin/sshd -p 2222 || { echo "Failed to start SSH server"; exit 1; }
47
- #/usr/sbin/sshd -D -e -ddd|| { echo "Failed to start SSH server"; exit 1; }
48
- else
49
- echo "* SSH server is already running *"
50
- fi
51
- }
52
-
53
-
54
- # Function to activate virtual environment
55
- activate_virtual_env() {
56
- if [ -d "/app/WebSSHEnv" ]; then
57
- echo "* Activating virtual environment *"
58
- source /app/WebSSHEnv/bin/activate || { echo "Failed to activate virtual environment"; exit 1; }
59
- else
60
- echo "* Virtual environment not found, please check setup *"
61
- fi
62
- }
63
-
64
  # Function to set working directory
65
  set_working_directory() {
66
  cd /app || { echo "Failed to change directory to /app"; exit 1; }
@@ -72,36 +11,42 @@ print_working_directory_contents() {
72
  ls -la /app
73
  }
74
 
75
- # Function to run the WebSSH application
76
- run_webssh_application() {
77
- echo "* Starting WebSSH application *"
78
- python3 -u -m WebSSH || { echo "Failed to start WebSSH application"; exit 1; }
79
- }
80
 
81
  # Function to keep the container running
82
  keep_container_running() {
83
  tail -f /var/log/auth.log
84
  }
85
 
86
- # Main script execution
87
- print_host_details
88
- generate_ssh_keys
89
- start_ssh_service
90
- #sshd -T | grep -i 'known hosts'
91
- echo "* Status of SSH service: *"
92
- netstat -tuln
93
-
94
- echo "* Testing admin's SSH login locally *"
95
- #echo "password" | ssh -v -o /etc/ssh/ssh_host_rsa_key.pub admin@localhost -p 2222
96
- #ssh admin@localhost -p 2222
97
- #sshpass -ppassword ssh -v -o StrictHostKeyChecking=no -i /etc/ssh/ssh_host_rsa_key.pub admin@127.0.0.1 -p 2222
98
- sshpass -p "password" ssh -v -t -o StrictHostKeyChecking=no admin@127.0.0.1 -p 2222 "command ; bash"
99
-
100
-
101
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
- activate_virtual_env
104
- set_working_directory
105
- print_working_directory_contents
106
- run_webssh_application
107
- keep_container_running
 
1
  #!/bin/bash
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  # Function to set working directory
4
  set_working_directory() {
5
  cd /app || { echo "Failed to change directory to /app"; exit 1; }
 
11
  ls -la /app
12
  }
13
 
14
+
 
 
 
 
15
 
16
  # Function to keep the container running
17
  keep_container_running() {
18
  tail -f /var/log/auth.log
19
  }
20
 
21
+ generate_admin_ssh_keys() {
22
+ if [ ! -f /home/admin/.ssh/id_rsa ]; then
23
+ echo "* Generating SSH keys for 'admin' user *"
24
+ mkdir -p /home/admin/.ssh || { echo "Failed to create .ssh directory"; exit 1; }
25
+ #ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -q -N ""
26
+ #yes y | ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -N "" || { echo "Failed to generate admin SSH key"; exit 1; }
27
+ yes y | ssh-keygen -t rsa -b 2048 -f /home/admin/.ssh/id_rsa -N "" || { echo "Failed to generate admin SSH key"; exit 1; }
28
+ ssh-keyscan -p 2222 127.0.0.1 >> /home/admin/.ssh/known_hosts
29
+ cp /home/admin/.ssh/known_hosts /home/admin/.ssh/ssh_known_hosts
30
+ #ssh-copy-id -i ~/.ssh/id_rsa.pub -p 2222 admin@localhost
31
+ chown -R admin:admin /home/admin
32
+ #chmod -R 777 /home/admin/.ssh
33
+ chmod 700 /home/admin/.ssh || { echo "Failed to set .ssh directory permissions"; exit 1; }
34
+ chmod 600 /home/admin/.ssh/id_rsa || { echo "Failed to set private key permissions"; exit 1; }
35
+ chmod 644 /home/admin/.ssh/id_rsa.pub || { echo "Failed to set public key permissions"; exit 1; }
36
+ fi
37
+ }
38
+ add_admin_authorized_keys() {
39
+ echo "* Adding public key to authorized_keys for 'admin' user *"
40
+ echo "* Admin Public Key :$(cat /home/admin/.ssh/id_rsa.pub)"
41
+ echo "* Admin Private Key :$(cat /home/admin/.ssh/id_rsa.pub)"
42
+ cat /home/admin/.ssh/id_rsa.pub >> /home/admin/.ssh/authorized_keys || { echo "Failed to add public key to authorized_keys"; exit 1; }
43
+ echo "* Authorised Keys: $(cat /home/admin/.ssh/authorized_keys)"
44
+ echo "* Known HOST File :$(cat /home/admin/.ssh/known_hosts)"
45
+ #chmod 600 /home/admin/.ssh/authorized_keys || { echo "Failed to set authorized_keys permissions"; exit 1; }
46
+ }
47
+ echo "password" || su - admin
48
+ echo "I am : $(whoami)"
49
+ generate_admin_ssh_keys
50
+ add_admin_authorized_keys
51
+ tail -f /var/log/auth.log
52