Secure you communication through SSH pipes. [2/4]

Secure you communication through SSH pipes. [2/4]

Chapter 4 will introduce us remote targets using knowledge from previous chapters. First of all we will create target host in our local Lab which will be used to connect with. We know that we created image from Dockerfile of Centos system with ssh enabled and generating all keys. We will list images:
$ docker images
centos_ssh latest 631487ba8064 44 hours ago 523MB

That’s the one we will use to generate target system TARGET1 the same like JUMPBOX1 but now we will remember about port 22 😉
$ docker run -d -P -p 22:22 --name TARGET1 centos_ssh:latest

We will receive id of container we ran.

In this exercise we will try to communicate with TARGET1 using JUMPBOX1. First we need to connect to TARGET1 one using docker, let us list dockers and find TARGET1 container id or we can use TARGET1 name. No we are creating the same user and we copy public keys as shown in previous chapter. We can list contianers:
└─$ docker ps -a
f3f18d566cc1 centos_ssh:latest "/usr/sbin/sshd -D" About an hour ago Up 5 minutes>22/tcp JUMPBOX1
5256f5bf1f93 centos_ssh:latest "/usr/sbin/sshd -D" 2 days ago Up 2 hours>22/tcp TARGET1`

There is two ways to login, by container id or by name:

└─$ docker exec -it TARGET1 bash
[[email protected] /]# exit

└─$ docker exec -it 525 bash
[[email protected] /]# adduser nemo
# su nemo

Create .ssh and add public keys from Kali authorized_keys
1 mkdir ~/.ssh
2 vim ~/.ssh/authorized_keys
3 vi ~/.ssh/authorized_keys
4 cat ~/.ssh/authorized_keys
5 chmod 700 ~/.ssh/
6 chmod 600 ~/.ssh/authorized_keys
7 exit

Check if everything work properly. We should ssh from Kali to our TARGER1 as nemo:
└─$ ssh -p 22 [email protected]
[[email protected] ~]$

Create Netcat chat from the papers. We ssh to the TARGET1 from Kali and start nc server:

└─$ ssh -p 22 [email protected]
[[email protected] ~]$ nc -nv -l 2222
Ncat: Version 7.50 ( )
Ncat: Listening on`

Forward port to remote target from new terminal:
└─$ ssh -p 22 [email protected] -L 130
[[email protected] ~]$

Last thing is to run nc from Kali:

└─$ nc 2000 130
Hi Kali !`

Send Hi TARGET! and from TARGET we typed Hi Kali !

└─$ ssh -p 22 [email protected]
[[email protected] ~]$ nc -nv -l 2222
Ncat: Version 7.50 ( )
Ncat: Listening on
Ncat: Connection from
Ncat: Connection from
Hi Kali !`

You can notice that nothing special happens on JUMPBOX1:

└─$ ssh -p 22 [email protected] -L 130
[[email protected] ~]$`

but you can type -vvv and see debug and logs.

If you get to this point we can move forward to SSH to Linux Target and use the connection described before to ssh into TARGET1 through JUMPBOX1. We will be able to run bash on TARGET1. First we need to do little change, change port 2222 to 22:
└─$ ssh -p 22 [email protected] -L 255
[[email protected] ~]$

Now we can run this command (it will ask us to add fingerprint while first access):

└─$ ssh -p 2000 [email protected]
[[email protected] ~]$`

We are in! You can notice container id starts with 525, that’s our TARGET1 machine. In the papers they instruct us to use ifconfig to see machine ip address, on my machine we will use different command to display ip:
$ hostname -I

This is the ip of our TARGET1 machine.

Next step id to use double tunnels together. For this exercise I will modify Dockerfile to have more automatic image.

Here is image that will create ready JUMPBOX1 for us:

└─$ cat Dockerfile 1 ⚙
FROM centos/systemd:latest

RUN yum update -y && yum -y install openssh openssh-server openssh-clients sudo initscripts

RUN ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -q -N "" -t rsa -f /root/.ssh/id_rsa
RUN adduser nemo
RUN mkdir /home/nemo/.ssh
RUN chmod 700 /home/nemo/.ssh
RUN chown nemo /home/nemo/.ssh/
RUN chgrp nemo /home/nemo/.ssh
RUN touch /home/nemo/.ssh/authorized_keys
RUN chmod 600 /home/nemo/.ssh/authorized_keys
RUN chown nemo /home/nemo/.ssh/authorized_keys
RUN chgrp nemo /home/nemo/.ssh/authorized_keys


CMD ["/usr/sbin/sshd", "-D"]`

We can now save Dockerfile and run build:
└─$ docker build -t ready_centos_ssh .

Here we have results how image build looks like:
└─$ docker build -t ready_centos_ssh . 1 ⚙
Sending build context to Docker daemon 2.56kB
Step 1/15 : FROM centos/systemd:latest
---> 05d3c1e2d0c1
Step 2/15 : RUN yum update -y && yum -y install openssh openssh-server openssh-clients sudo initscripts
---> Using cache
---> c9a84358517c
Step 3/15 : RUN ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key
---> Using cache
---> b1d24455552f
Step 4/15 : RUN ssh-keygen -q -N "" -t rsa -f /root/.ssh/id_rsa
---> Using cache
---> 2352efdcb371
Step 5/15 : RUN adduser nemo
---> Running in 2d4528b66310
Removing intermediate container 2d4528b66310
---> 3f7748750341
Step 6/15 : RUN mkdir /home/nemo/.ssh
---> Running in daa6364267f6
Removing intermediate container daa6364267f6
---> 59339ba72742
Step 7/15 : RUN chmod 700 /home/nemo/.ssh
---> Running in 9eea213cb1cc
Removing intermediate container 9eea213cb1cc
---> bcd1e8eaca8c
Step 8/15 : RUN chown nemo /home/nemo/.ssh/
---> Running in bb90969852ca
Removing intermediate container bb90969852ca
---> ac84458934f5
Step 9/15 : RUN chgrp nemo /home/nemo/.ssh
---> Running in 636c361348b0
Removing intermediate container 636c361348b0
---> 5c6cdb3ad134
Step 10/15 : RUN touch /home/nemo/.ssh/authorized_keys
---> Running in cf2c56177955
Removing intermediate container cf2c56177955
---> 83a4646f2a24
Step 11/15 : RUN chmod 600 /home/nemo/.ssh/authorized_keys
---> Running in d96045371c99
Removing intermediate container d96045371c99
---> b853ba4078ce
Step 12/15 : RUN chown nemo /home/nemo/.ssh/authorized_keys
---> Running in ec3bfffd644f
Removing intermediate container ec3bfffd644f
---> 2149ad463725
Step 13/15 : RUN chgrp nemo /home/nemo/.ssh/authorized_keys

---> Running in 749d96f207ee
Removing intermediate container 749d96f207ee
---> 8d425721fe05
Step 14/15 : EXPOSE 22
---> Running in 3b23146029f2
Removing intermediate container 3b23146029f2
---> 0cdad8836360
Step 15/15 : CMD ["/usr/sbin/sshd", "-D"]
---> Running in 61757a8571fc
Removing intermediate container 61757a8571fc
---> 8fd761404691
Successfully built 8fd761404691
Successfully tagged ready_centos_ssh:latest

Next step we can run our image to build JUMPBOX2:
└─$ docker run -d -P -p 22:22 --name JUMPBOX2 ready_centos_ssh:latest 1 ⚙

Copy Kali public key to our JUMPBOX2 using docker exec and bash command:
└─$ docker exec -it JUMPBOX2 bash -c "echo '$(cat .ssh/' >> /home/nemo/.ssh/authorized_keys"

Ssh into JUMPBOX2 (ip address you can found out with docker inspect)
└─$ ssh [email protected] 1 ⚙
[[email protected] ~]$

Create another two JUMPBOXes and move one with the rest of the chapter.
Preparing the whole infrastructure (without windows for now) should look like this:

└─$ docker run -d -P -p 2200:22 --net plumbernet --ip --name TARGET1 ready_centos_ssh:latest

└─$ docker run -d -P -p 2220:22 --net plumbernet --ip --name JUMPBOX1 ready_centos_ssh:latest

└─$ docker run -d -P -p 2221:22 --net plumbernet --ip --name JUMPBOX2 ready_centos_ssh:latest

└─$ docker run -d -P -p 2222:22 --net plumbernet --ip --name JUMPBOX3 ready_centos_ssh:latest

└─$ docker run -d -P -p 2223:22 --net plumbernet --ip --name JUMPBOX4 ready_centos_ssh:latest

Let’s check created and running containers:
└─$ docker ps -a
fd6f583492e1 ready_centos_ssh:latest "/usr/sbin/sshd -D" 11 seconds ago Up 10 seconds>22/tcp JUMPBOX4
57d1ff913f3a ready_centos_ssh:latest "/usr/sbin/sshd -D" 31 seconds ago Up 31 seconds>22/tcp JUMPBOX3
9638750b56c4 ready_centos_ssh:latest "/usr/sbin/sshd -D" 43 seconds ago Up 42 seconds>22/tcp JUMPBOX2
4f627cb01f6b ready_centos_ssh:latest "/usr/sbin/sshd -D" 59 seconds ago Up 59 seconds>22/tcp JUMPBOX1
7931295c3fcf ready_centos_ssh:latest "/usr/sbin/sshd -D" About a minute ago Up About a minute>22/tcp TARGET1
f3f18d566cc1 centos_ssh:latest "/usr/sbin/sshd -D" 27 hours ago Exited (0) 19 hours ago JUMPBOX1_old
5256f5bf1f93 centos_ssh:latest "/usr/sbin/sshd -D" 3 days ago Exited (0) 19 hours ago TARGET1_old

Copy public key to every container:
└─$ docker exec -it TARGET1 bash -c "echo '$(cat .ssh/' >> /home/nemo/.ssh/authorized_keys"

└─$ docker exec -it JUMPBOX1 bash -c "echo '$(cat .ssh/' >> /home/nemo/.ssh/authorized_keys"

└─$ docker exec -it JUMPBOX2 bash -c "echo '$(cat .ssh/' >> /home/nemo/.ssh/authorized_keys"

└─$ docker exec -it JUMPBOX3 bash -c "echo '$(cat .ssh/' >> /home/nemo/.ssh/authorized_keys"

└─$ docker exec -it JUMPBOX4 bash -c "echo '$(cat .ssh/' >> /home/nemo/.ssh/authorized_keys"

In the next step we follow from chapter 4 point 4.5.
While we have running JUMPBOX1…4 and TARGET1 we will shh into JUMPBOX1:
└─$ ssh -p 22 [email protected] -L 130 ⨯
[[email protected] ~]$

We can notice that we are in JUMPBOX1 as hostname is the same as container id of JUMPBOX1. Now we will open new terminal in Kali and jump into JUMPBOX2 through tunnel 1111:

└─$ ssh -p 1111 [email protected] -L
[[email protected] ~]$

and next 2222, 3333 and 4444:
└─$ ssh -p 2222 [email protected] -L
[[email protected] ~]$

└─$ ssh -p 3333 [email protected] -L
[[email protected] ~]$

now we will open next new terminal and access TARGET1:
└─$ ssh -p 4444 [email protected]
[[email protected] ~]$

TARGET1 container id is 7931295c3fcf. We are in! You can open new terminal and look all ports open:
└─$ netstat -nat

This is one way described in handbook, let’s do it with another one. We will take all port forwards into one string. We will gather all [email protected]:port from JUMPHOSTs and TARGET1 separated by comma:

└─$ ssh -J [email protected]:22,[email protected]:22,[email protected]:22,[email protected]:22,[email protected]`200
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
[-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
[-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
[-i identity_file] [-J [[email protected]]host[:port]] [-L address]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-Q query_option] [-R address] [-S ctl_path] [-W host:port]
[-w local_tun[:remote_tun]] destination [command]

If we take example from handbook it should work but you have to remember that versions of ssh might differ on OS and versions itself. So I tried to execute one JUMPBOX and it was the same message about parameters of ssh. I tried without commas and it worked.
└─$ ssh -J 255 ⨯
[email protected]'s password:

If we take the shortest example we will see that Kali user is not found, that’s correct as we need to use nemo user. Let’s take extended version without commas:

└─$ ssh -J [email protected]:22 [email protected]:22 [email protected]:22 [email protected]:22 `[email protected]`
channel 0: open failed: administratively prohibited: open failed
stdio forwarding failed
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535`

We get connection administratively prohibited. I managed to enable this connection changing ssh_config and went into another errors regarding user permission, requesting root user for creating tunnel. I think that this is blocked because port 22 is automatically selected and it’s lover that 1024 which requires root. 

Examples how to use Jumpbox in real situations can be found in last subjects of the chapter.

Making those tests I came up with scripts that will automate creations of our environment, they will be available on github.

 Those information might be very useful for learning  


Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany.