<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://commons.oreilly.com/wiki/skins/common/feed.css?97"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://commons.oreilly.com/wiki/index.php?title=Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Developing_Dissectors_and_Plug-ins_for_the_Ettercap_Network_Sniffer&amp;action=history&amp;feed=atom</id>
		<title>Network Security Tools/Modifying and Hacking Security Tools/Developing Dissectors and Plug-ins for the Ettercap Network Sniffer - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://commons.oreilly.com/wiki/index.php?title=Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Developing_Dissectors_and_Plug-ins_for_the_Ettercap_Network_Sniffer&amp;action=history&amp;feed=atom"/>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Developing_Dissectors_and_Plug-ins_for_the_Ettercap_Network_Sniffer&amp;action=history"/>
		<updated>2013-05-25T01:12:44Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.11.0</generator>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Developing_Dissectors_and_Plug-ins_for_the_Ettercap_Network_Sniffer&amp;diff=9244&amp;oldid=prev</id>
		<title>Docbook2Wiki: Initial conversion from Docbook</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Developing_Dissectors_and_Plug-ins_for_the_Ettercap_Network_Sniffer&amp;diff=9244&amp;oldid=prev"/>
				<updated>2008-03-11T22:54:01Z</updated>
		
		<summary type="html">&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 22:54, 11 March 2008&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;</summary>
		<author><name>Docbook2Wiki</name></author>	</entry>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Developing_Dissectors_and_Plug-ins_for_the_Ettercap_Network_Sniffer&amp;diff=8330&amp;oldid=prev</id>
		<title>Docbook2Wiki: Initial conversion from Docbook</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Developing_Dissectors_and_Plug-ins_for_the_Ettercap_Network_Sniffer&amp;diff=8330&amp;oldid=prev"/>
				<updated>2008-03-11T21:39:00Z</updated>
		
		<summary type="html">&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Network Security Tools/TOC}}&lt;br /&gt;
Ettercap is a network analyzer that is free and open source. Advanced features such as ARP poisoning, packet filtering, and OS fingerprinting, along with support for password dissectors and plug-ins make Ettercap a powerful tool and a favorite among many network administrators. Ettercap has been known to compile on various Unix and Linux flavors, and has been successfully ported to run on Microsoft Windows operating systems.&lt;br /&gt;
&lt;br /&gt;
This chapter introduces the concept of writing dissectors and plug-ins for Ettercap. Dissectors allow you to grab important information, such as usernames and passwords, that are transmitted over a network. For the purposes of understanding how to write a dissector, we will step through a dissector that captures and displays FTP usernames and passwords. Then, to demonstrate how to write an Ettercap plug-in, we will step through a plug-in that alerts the user when one host on the network attempts to establish a new TCP connection with another host.&lt;br /&gt;
&lt;br /&gt;
== Installing and Using Ettercap ==&lt;br /&gt;
&lt;br /&gt;
The latest Ettercap source code is available from ''http://ettercap.sourceforge.net/download.php''. Grab the latest tarball and compile Ettercap:&lt;br /&gt;
&lt;br /&gt;
 [notroot]$ '''tar zxvf ettercap-NG-x.y.z.tar.gz'''&lt;br /&gt;
 [notroot]$ '''cd ettercap-NG-x.y.z'''&lt;br /&gt;
 [notroot]$ '''./configure'''&lt;br /&gt;
 [notroot]$''' make'''&lt;br /&gt;
 [root]#''' make install'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;warning&amp;quot;&amp;gt;&lt;br /&gt;
'''Warning'''&lt;br /&gt;
&lt;br /&gt;
Make sure you obtain and install an Ettercap version that is equal to or greater than 0.7.0. Ettercap APIs of versions older than 0.7.0 differ significantly, and are no longer supported.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can run Ettercap in console mode, curses mode, or GTK mode, the latter of which is shown in [[Network Security Tools/Modifying and Hacking Security Tools/Developing Dissectors and Plug-ins for the Ettercap Network Sniffer#networkst-CHP-2-FIG-1|Figure 2-1]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-2-FIG-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 2-1. Ettercap in GTK mode'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Network Security Tools_I_2_tt84.png|Ettercap in GTK mode]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run &amp;lt;tt&amp;gt;ettercap -h&amp;lt;/tt&amp;gt; to discover the plethora of options and features Ettercap provides. See the &amp;lt;tt&amp;gt;ettercap&amp;lt;/tt&amp;gt; manpage for more details on available options and features.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
The Ettercap web site consists of a publicly available message board dedicated to providing support in case you experience problems. Access the message board by visiting ''http://ettercap.sourceforge.net/forum/''.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Writing an Ettercap Dissector ==&lt;br /&gt;
&lt;br /&gt;
A ''dissector'' captures protocol-specific information from the network. Most Ettercap dissectors are designed to capture usernames and passwords transmitted over the network in real time. Here is an example of how to run Ettercap in console mode to sniff passwords:&lt;br /&gt;
&lt;br /&gt;
 [root]# '''ettercap --text --quiet'''&lt;br /&gt;
 &lt;br /&gt;
 ettercap NG-0.7.0 copyright 2001-2004 ALoR &amp;amp; NaGA&lt;br /&gt;
 &lt;br /&gt;
 Listening on en0... (Ethernet)&lt;br /&gt;
    eth0 -&amp;gt;       00:0B:25:30:11:B      192.168.1.1     255.255.255.0&lt;br /&gt;
 &lt;br /&gt;
 Privileges dropped to UID 65534 GID 65534...&lt;br /&gt;
    0 plugins&lt;br /&gt;
   39 protocol dissectors&lt;br /&gt;
   53 ports monitored&lt;br /&gt;
 6312 mac vendor fingerprint&lt;br /&gt;
 1633 tcp OS fingerprint&lt;br /&gt;
 2183 known services&lt;br /&gt;
 &lt;br /&gt;
 Starting Unified sniffing...&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 Text only Interface activated...&lt;br /&gt;
 Hit 'h' for inline help&lt;br /&gt;
 &lt;br /&gt;
 FTP : 10.0.0.1:21 -&amp;gt; USER: john  PASS: try4ndgu355m3!!&lt;br /&gt;
&lt;br /&gt;
In the preceding example, the FTP dissector successfully sniffed the FTP password &amp;lt;tt&amp;gt;try4ndgu355m3!!&amp;lt;/tt&amp;gt; of user &amp;lt;tt&amp;gt;john&amp;lt;/tt&amp;gt; logged on to an FTP server running on host &amp;lt;tt&amp;gt;10.0.0.1&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
In the following paragraphs, we will discuss the dissector responsible for capturing FTP usernames and passwords. First we will discuss the FTP authentication mechanism, followed by a detailed analysis of the FTP dissector source code.&lt;br /&gt;
&lt;br /&gt;
=== Overview of FTP Authentication ===&lt;br /&gt;
&lt;br /&gt;
This section discusses how FTP performs authentication. We need to understand this before we step through FTP dissector source code for Ettercap.&lt;br /&gt;
&lt;br /&gt;
FTP is a plain-text protocol, and it uses no encryption. FTP servers listen on TCP port 21 by default. To authenticate with an FTP server, the client establishes a connection to TCP port 21 and expects a banner that is preceded with &amp;lt;tt&amp;gt;220&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 220 Welcome to ftp.example.com&lt;br /&gt;
&lt;br /&gt;
The banner string is irrelevant and can be changed by the FTP server administrator. By default, banner strings of some FTP servers provide the FTP server name and version number. With respect to the Ettercap dissector, we are concerned with only the &amp;lt;tt&amp;gt;220&amp;lt;/tt&amp;gt; response code, which signifies that the FTP server is ready to serve further requests.&lt;br /&gt;
&lt;br /&gt;
To authenticate with the FTP server, a client sends the &amp;lt;tt&amp;gt;USER&amp;lt;/tt&amp;gt; command followed by the user's username:&lt;br /&gt;
&lt;br /&gt;
 USER john&lt;br /&gt;
&lt;br /&gt;
If the FTP server is ready to authenticate the user, it responds with a &amp;lt;tt&amp;gt;331&amp;lt;/tt&amp;gt; response code:&lt;br /&gt;
&lt;br /&gt;
 331 Please specify the password.&lt;br /&gt;
&lt;br /&gt;
Next, the FTP client sends the &amp;lt;tt&amp;gt;PASS&amp;lt;/tt&amp;gt; command followed by the user's password:&lt;br /&gt;
&lt;br /&gt;
 PASS try4ndgu355m3!!&lt;br /&gt;
&lt;br /&gt;
If the supplied password is correct, the FTP server responds with a &amp;lt;tt&amp;gt;230&amp;lt;/tt&amp;gt; response code:&lt;br /&gt;
&lt;br /&gt;
 230- Welcome to ftp.example.com&lt;br /&gt;
 230 Login successful.&lt;br /&gt;
&lt;br /&gt;
The outcome of a request to an FTP server depends mainly on the first digit of the three-digit response code. [[Network Security Tools/Modifying and Hacking Security Tools/Developing Dissectors and Plug-ins for the Ettercap Network Sniffer#networkst-CHP-2-TABLE-1|Table 2-1]] lists FTP response codes and their meanings, based on the first digit of the code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-2-TABLE-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 2-1. FTP response codes'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Response code !! Description&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 1yz&lt;br /&gt;
| Positive preliminary reply&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 2yz&lt;br /&gt;
| Positive completion reply&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 3yz&lt;br /&gt;
| Positive intermediate reply&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 4yz&lt;br /&gt;
| Transient negative completion reply&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Because FTP is a plain-text protocol, you can use a telnet client to connect to the FTP server and test the authentication mechanism. Here is an example:&lt;br /&gt;
&lt;br /&gt;
 [notroot]$ '''telnet ftp.example.com 21'''&lt;br /&gt;
 Trying 192.168.1.2...&lt;br /&gt;
 Connected to ftp.example.com.&lt;br /&gt;
 Escape character is '^]'.&lt;br /&gt;
 220 Welcome to ftp.example.com.&lt;br /&gt;
 &lt;br /&gt;
 '''                     USER john'''&lt;br /&gt;
                   &lt;br /&gt;
 331 Please specify the password.&lt;br /&gt;
 '''PASS try4ndgu355m3!!'''&lt;br /&gt;
 230- Welcome to ftp.example.com&lt;br /&gt;
 230 Login successful.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
For more details on the FTP protocol, see RFC 959, available at ''http://www.faqs.org/rfcs/rfc959.html''.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The FTP Password Dissector ===&lt;br /&gt;
&lt;br /&gt;
The FTP dissector's goal is to analyze FTP traffic on the network to obtain and display FTP usernames and passwords. The dissector, ''ec_ftp.c'', is located in the ''src/dissectors'' directory of the Ettercap source tree. The first few lines of the code use the &amp;lt;tt&amp;gt;include&amp;lt;/tt&amp;gt; directive to include required header files for writing dissectors:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;ec.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;ec_decode.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;ec_dissect.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;ec_session.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Prototypes for defined functions are declared next. We will discuss these functions in the next few paragraphs.&lt;br /&gt;
&lt;br /&gt;
 FUNC_DECODER(dissector_ftp);&lt;br /&gt;
 void ftp_init(void);&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;ftp_init( )&amp;lt;/tt&amp;gt; function adds an entry into appropriate Ettercap data structures by invoking the &amp;lt;tt&amp;gt;dissect_add( )&amp;lt;/tt&amp;gt; function:&lt;br /&gt;
&lt;br /&gt;
 void _  _init ftp_init(void)&lt;br /&gt;
 {&lt;br /&gt;
     dissect_add(&amp;quot;ftp&amp;quot;, APP_LAYER_TCP, 21, dissector_ftp);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Note that the &amp;lt;tt&amp;gt;_ _init&amp;lt;/tt&amp;gt; macro is defined in &amp;lt;tt&amp;gt;ec.h&amp;lt;/tt&amp;gt; as:&lt;br /&gt;
&lt;br /&gt;
 #define _ _init _ _attribute_  _ ((constructor))&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;_ _attribute_ _((constructor))&amp;lt;/tt&amp;gt; directive causes all functions to be invoked before &amp;lt;tt&amp;gt;main( )&amp;lt;/tt&amp;gt;. Therefore, the &amp;lt;tt&amp;gt;ftp_init( )&amp;lt;/tt&amp;gt; function is automatically invoked when the &amp;lt;tt&amp;gt;ettercap&amp;lt;/tt&amp;gt; executable is run. The &amp;lt;tt&amp;gt;dissect_add( )&amp;lt;/tt&amp;gt; function should be called by every dissector because it is used to add an entry into &amp;lt;tt&amp;gt;dissect_list&amp;lt;/tt&amp;gt;, a structure used by Ettercap to manage enabled dissectors. The function prototype for &amp;lt;tt&amp;gt;dissect_add( )&amp;lt;/tt&amp;gt; is:&lt;br /&gt;
&lt;br /&gt;
 void dissect_add(char *name, u_int8 level, u_int32 port, FUNC_DECODER_PTR(decoder))&lt;br /&gt;
&lt;br /&gt;
Parameters accepted by &amp;lt;tt&amp;gt;dissect_add( )&amp;lt;/tt&amp;gt; are described in [[Network Security Tools/Modifying and Hacking Security Tools/Developing Dissectors and Plug-ins for the Ettercap Network Sniffer#networkst-CHP-2-TABLE-2|Table 2-2]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-2-TABLE-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 2-2. Parameters for dissect_add( )'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Parameter !! Description&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 Name&lt;br /&gt;
| Name of dissector. This name is also used in the Ettercap configuration file located in ''share/etter.conf'' to enable or disable dissectors upon startup.&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 Level&lt;br /&gt;
| Layer on which the dissector operates. Possible values are &amp;lt;tt&amp;gt;IFACE_LAYER&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;LINK_LAYER&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NET_LAYER&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;PROTO_LAYER&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;APP_LAYER&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;APP_LAYER_TCP&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;APP_LAYER_UDP&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 Port&lt;br /&gt;
| Port number on which the dissector operates.&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 FUNC_DECODER_PTR(decoder)&lt;br /&gt;
| Pointer to &amp;quot;main&amp;quot; function of the dissector.&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that the last parameter to &amp;lt;tt&amp;gt;dissect_add( )&amp;lt;/tt&amp;gt; is &amp;lt;tt&amp;gt;dissector_ftp&amp;lt;/tt&amp;gt;. This designates the &amp;lt;tt&amp;gt;dissector_ftp( )&amp;lt;/tt&amp;gt; function as the entry point to the dissector code whenever traffic on TCP port 21 is captured. The &amp;lt;tt&amp;gt;FUNC_DECODER( )&amp;lt;/tt&amp;gt; macro is used to define &amp;lt;tt&amp;gt;dissector_ftp&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 FUNC_DECODER(dissector_ftp)&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;FUNC_DECODER&amp;lt;/tt&amp;gt; macro is just a wrapper around &amp;lt;tt&amp;gt;dissector_ftp&amp;lt;/tt&amp;gt; that defines it as a pointer. This is useful because, as we previously noted, &amp;lt;tt&amp;gt;dissector_ftp&amp;lt;/tt&amp;gt; is passed to &amp;lt;tt&amp;gt;dissect_add( )&amp;lt;/tt&amp;gt;, whose last parameter accepts only a pointer to a function.&lt;br /&gt;
&lt;br /&gt;
Because &amp;lt;tt&amp;gt;dissector_ftp( )&amp;lt;/tt&amp;gt; is invoked every time a packet on TCP port 21 is captured, &amp;lt;tt&amp;gt;DECLARE_DISP_PTR_END( )&amp;lt;/tt&amp;gt; is called to set &amp;lt;tt&amp;gt;ptr&amp;lt;/tt&amp;gt; to point to the beginning of the data buffer, and &amp;lt;tt&amp;gt;end&amp;lt;/tt&amp;gt; to point to the end of the buffer:&lt;br /&gt;
&lt;br /&gt;
 DECLARE_DISP_PTR_END(ptr, end);&lt;br /&gt;
&lt;br /&gt;
Dissectors in Ettercap need to keep track of individual TCP connections. You initiate a TCP connection by sending a TCP packet with the &amp;lt;tt&amp;gt;SYN&amp;lt;/tt&amp;gt; flag set, followed by a response TCP packet from the server that contains the &amp;lt;tt&amp;gt;SYN&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;ACK&amp;lt;/tt&amp;gt; flags set. Therefore, the FTP dissector calls &amp;lt;tt&amp;gt;CREATE_SESSION_ON_SYN_ACK()&amp;lt;/tt&amp;gt; , which creates a new session for the connection as soon as a packet with the &amp;lt;tt&amp;gt;SYN&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;ACK&amp;lt;/tt&amp;gt; flags set is captured:&lt;br /&gt;
&lt;br /&gt;
 CREATE_SESSION_ON_SYN_ACK(&amp;quot;ftp&amp;quot;, s, dissector_ftp);&lt;br /&gt;
&lt;br /&gt;
The first parameter to &amp;lt;tt&amp;gt;CREATE_SESSION_ON_SYN_ACK( )&amp;lt;/tt&amp;gt; indicates the name of the dissector, which in our case is &amp;lt;tt&amp;gt;ftp&amp;lt;/tt&amp;gt;. The second parameter to &amp;lt;tt&amp;gt;CREATE_SESSION_SYN&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;_ACK( )&amp;lt;/tt&amp;gt; is &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt;, which is a pointer to the &amp;lt;tt&amp;gt;ec_session&amp;lt;/tt&amp;gt; structure defined in ''ec_session.h''. This structure holds individual session data, and is therefore used to keep track of individual TCP connections.&lt;br /&gt;
&lt;br /&gt;
The first TCP packet sent from the FTP server most likely contains the banner, including the 220 response code, and this is analyzed by calling the &amp;lt;tt&amp;gt;IF_FIRST_PACKET_FROM_SERVER()&amp;lt;/tt&amp;gt; function. The &amp;lt;tt&amp;gt;IF_FIRST_PACKET_FROM_SERVER( )&amp;lt;/tt&amp;gt; macro expects the block to end with &amp;lt;tt&amp;gt;ENDIF_FIRST_PACKET_FROM_SERVER( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 IF_FIRST_PACKET_FROM_SERVER(&amp;quot;ftp&amp;quot;, s, ident, dissector_ftp)&lt;br /&gt;
 {            &lt;br /&gt;
     DEBUG_MSG(&amp;quot;\tdissector_ftp BANNER&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
     if (!strncmp(ptr, &amp;quot;220&amp;quot;, 3)) &lt;br /&gt;
     {&lt;br /&gt;
         PACKET-&amp;gt;DISSECTOR.banner = strdup(ptr + 4);&lt;br /&gt;
          &lt;br /&gt;
         if ( (ptr = strchr(PACKET-&amp;gt;DISSECTOR.banner, '\r')) != NULL )&lt;br /&gt;
             *ptr = '\0';&lt;br /&gt;
     }&lt;br /&gt;
 } ENDIF_FIRST_PACKET_FROM_SERVER(s, ident)&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;ident&amp;lt;/tt&amp;gt; parameter is a &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; pointer, and is assigned to a new session identifier of type &amp;lt;tt&amp;gt;struct dissect_ident&amp;lt;/tt&amp;gt; . As the name suggests, &amp;lt;tt&amp;gt;ident&amp;lt;/tt&amp;gt; is used to identify sessions. &amp;lt;tt&amp;gt;PACKET&amp;lt;/tt&amp;gt; is a global structure of type &amp;lt;tt&amp;gt;struct packet_object&amp;lt;/tt&amp;gt; . It holds the actual network packet data. (See ''ec_packet.h'' for the definition of &amp;lt;tt&amp;gt;packet_object&amp;lt;/tt&amp;gt;.) Using &amp;lt;tt&amp;gt;strncmp()&amp;lt;/tt&amp;gt; , the FTP dissector code looks for the string &amp;lt;tt&amp;gt;220&amp;lt;/tt&amp;gt; within the first three characters pointed to by &amp;lt;tt&amp;gt;ptr&amp;lt;/tt&amp;gt; because &amp;lt;tt&amp;gt;220&amp;lt;/tt&amp;gt; is sent by an FTP server upon connect, followed by the FTP server banner. &amp;lt;tt&amp;gt;PACKET-&amp;gt;DISSECTOR.banner&amp;lt;/tt&amp;gt; is then set to the banner of the FTP server, which is basically everything after the &amp;lt;tt&amp;gt;220&amp;lt;/tt&amp;gt; string. Next, &amp;lt;tt&amp;gt;strchr()&amp;lt;/tt&amp;gt; is used to point &amp;lt;tt&amp;gt;ptr&amp;lt;/tt&amp;gt; to the end of the banner by searching for the &amp;lt;tt&amp;gt;\r&amp;lt;/tt&amp;gt; character.&lt;br /&gt;
&lt;br /&gt;
The dissector makes sure to skip packets that contain no data. These packets are mainly &amp;lt;tt&amp;gt;ACK&amp;lt;/tt&amp;gt; TCP packets that serve only as acknowledgments:&lt;br /&gt;
&lt;br /&gt;
 if (PACKET-&amp;gt;DATA.len == 0)  &lt;br /&gt;
       return NULL;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;FROM_SERVER&amp;lt;/tt&amp;gt; macro is used to skip all subsequent packets from the server. After having obtained the &amp;lt;tt&amp;gt;220&amp;lt;/tt&amp;gt; server string and banner, we do not care about any data coming from the FTP server. From there on, the dissector is concerned only with username and password data that is transmitted to the server:&lt;br /&gt;
&lt;br /&gt;
 if (FROM_SERVER(&amp;quot;ftp&amp;quot;, PACKET))&lt;br /&gt;
       return NULL;&lt;br /&gt;
&lt;br /&gt;
Whitespace in the beginning of packet data is skipped:&lt;br /&gt;
&lt;br /&gt;
 while(*ptr == ' ' &amp;amp;&amp;amp; ptr != end) ptr++;&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;tt&amp;gt;ptr&amp;lt;/tt&amp;gt; points to &amp;lt;tt&amp;gt;end&amp;lt;/tt&amp;gt;, there is no more data to analyze, so the dissector returns:&lt;br /&gt;
&lt;br /&gt;
 if (ptr == end)&lt;br /&gt;
     return NULL;&lt;br /&gt;
&lt;br /&gt;
The dissector uses &amp;lt;tt&amp;gt;strncasecmp()&amp;lt;/tt&amp;gt; to look for the &amp;lt;tt&amp;gt;USER&amp;lt;/tt&amp;gt; command sent by the FTP client to the server to capture the FTP username:&lt;br /&gt;
&lt;br /&gt;
 if (!strncasecmp(ptr, &amp;quot;USER &amp;quot;, 5))&lt;br /&gt;
 {&lt;br /&gt;
     DEBUG_MSG(&amp;quot;\tDissector_FTP USER&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
     dissect_create_session(&amp;amp;s, PACKET, DISSECT_CODE(dissector_ftp));&lt;br /&gt;
 &lt;br /&gt;
     ptr += 5;&lt;br /&gt;
 &lt;br /&gt;
     SAFE_FREE(s-&amp;gt;data);&lt;br /&gt;
 &lt;br /&gt;
     s-&amp;gt;data = strdup(ptr);&lt;br /&gt;
     s-&amp;gt;data_len = strlen(ptr);&lt;br /&gt;
 &lt;br /&gt;
     if ( (ptr = strchr(s-&amp;gt;data,'\r')) != NULL )&lt;br /&gt;
         *ptr = '\0';&lt;br /&gt;
 &lt;br /&gt;
     session_put(s);&lt;br /&gt;
 &lt;br /&gt;
     return NULL;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;DEBUG_MSG( )&amp;lt;/tt&amp;gt; macro prints the given string to a designated debug file if Ettercap is compiled with the &amp;lt;tt&amp;gt;--enable-debug&amp;lt;/tt&amp;gt; option. If Ettercap is unable to write to the debug file, the message is printed to &amp;lt;tt&amp;gt;stderr&amp;lt;/tt&amp;gt;, which causes most Unix and Linux shells to output to the console by default.&lt;br /&gt;
&lt;br /&gt;
Note that the FTP dissector uses the session pointer(&amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt;) returned by &amp;lt;tt&amp;gt;CREATE_SESSION_SYN_ACK()&amp;lt;/tt&amp;gt; to invoke &amp;lt;tt&amp;gt;IF_FIRST_PACKET_FROM_SERVER()&amp;lt;/tt&amp;gt; , which requires a session pointer as its second parameter. However, the dissector creates a brand-new session in the preceding block when Ettercap is started after the FTP connection is established, in which case the banner and SYN+ACK packet would have already been sent and never been seen by the dissector.&lt;br /&gt;
&lt;br /&gt;
The dissector advances &amp;lt;tt&amp;gt;ptr&amp;lt;/tt&amp;gt; by 5 to skip the &amp;lt;tt&amp;gt;USER&amp;lt;/tt&amp;gt; command followed by whitespace, so now &amp;lt;tt&amp;gt;ptr&amp;lt;/tt&amp;gt; points to the username sent by the FTP client. The &amp;lt;tt&amp;gt;SAFE_FREE( )&amp;lt;/tt&amp;gt; macro invokes &amp;lt;tt&amp;gt;free( )&amp;lt;/tt&amp;gt; to free data only if the data is not &amp;lt;tt&amp;gt;null&amp;lt;/tt&amp;gt;. The session pointer's &amp;lt;tt&amp;gt;data&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;data_len&amp;lt;/tt&amp;gt; items are set to the username string contained in &amp;lt;tt&amp;gt;ptr&amp;lt;/tt&amp;gt;, and its length. Next, &amp;lt;tt&amp;gt;session_put( )&amp;lt;/tt&amp;gt; is invoked to store the session pointed to by &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt;. This session is retrieved by the following &amp;lt;tt&amp;gt;if&amp;lt;/tt&amp;gt; block, which attempts to capture the password sent by the FTP client. The &amp;lt;tt&amp;gt;strncasecmp()&amp;lt;/tt&amp;gt; function compares the first five characters of &amp;lt;tt&amp;gt;ptr&amp;lt;/tt&amp;gt; with the &amp;lt;tt&amp;gt;PASS&amp;lt;/tt&amp;gt; string, which signifies that the FTP client has sent the user password to the server:&lt;br /&gt;
&lt;br /&gt;
 if ( !strncasecmp(ptr, &amp;quot;PASS &amp;quot;, 5) )&lt;br /&gt;
 {&lt;br /&gt;
     DEBUG_MSG(&amp;quot;\tDissector_FTP PASS&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
     ptr += 5;&lt;br /&gt;
 &lt;br /&gt;
     dissect_create_ident(&amp;amp;ident, PACKET, DISSECT_CODE(dissector_ftp));&lt;br /&gt;
 &lt;br /&gt;
     if (session_get_and_del(&amp;amp;s, ident, DISSECT_IDENT_LEN) == -ENOTFOUND)&lt;br /&gt;
     {&lt;br /&gt;
         SAFE_FREE(ident);&lt;br /&gt;
         return NULL;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     if (s-&amp;gt;data == NULL) &lt;br /&gt;
     {&lt;br /&gt;
         SAFE_FREE(ident);&lt;br /&gt;
         return NULL;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     PACKET-&amp;gt;DISSECTOR.user = strdup(s-&amp;gt;data);&lt;br /&gt;
     PACKET-&amp;gt;DISSECTOR.pass = strdup(ptr);&lt;br /&gt;
 &lt;br /&gt;
     if ( (ptr = strchr(PACKET-&amp;gt;DISSECTOR.pass, '\r')) != NULL )&lt;br /&gt;
         *ptr = '\0';&lt;br /&gt;
 &lt;br /&gt;
     session_free(s);&lt;br /&gt;
     SAFE_FREE(ident);&lt;br /&gt;
 &lt;br /&gt;
     DISSECT_MSG(&amp;quot;FTP : %s:%d -&amp;gt; USER: %s  PASS: %s\n&amp;quot;, ip_addr_ntoa(&amp;amp;PACKET-&amp;gt;L3.dst, tmp),&lt;br /&gt;
 ntohs(PACKET-&amp;gt;L4.dst), PACKET-&amp;gt;DISSECTOR.user,&lt;br /&gt;
 &lt;br /&gt;
     return NULL;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the preceding code block, &amp;lt;tt&amp;gt;ptr&amp;lt;/tt&amp;gt; is incremented by 5 to point to the password sent by the FTP client, which occurs after the string &amp;lt;tt&amp;gt;PASS&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;dissect_create_ident( )&amp;lt;/tt&amp;gt; function is used to create a session identifier, &amp;lt;tt&amp;gt;ident&amp;lt;/tt&amp;gt;, which is used to invoke &amp;lt;tt&amp;gt;session_get_and_del( )&amp;lt;/tt&amp;gt; . The &amp;lt;tt&amp;gt;session_get_and_del()&amp;lt;/tt&amp;gt; function obtains the previous session into &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt;, and deletes the session from memory because the dissector no longer needs the session after the current code block. If a previous session is not available, the dissector cannot proceed, and therefore returns after freeing &amp;lt;tt&amp;gt;ident&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;PACKET-&amp;gt;DISSECTOR.user&amp;lt;/tt&amp;gt; is set to the data stored in &amp;lt;tt&amp;gt;s-&amp;gt;data&amp;lt;/tt&amp;gt;, which contains the FTP username as set in the &amp;lt;tt&amp;gt;if (!strncasecmp(ptr&amp;lt;/tt&amp;gt;, &amp;quot;&amp;lt;tt&amp;gt;USER&amp;lt;/tt&amp;gt; &amp;quot;, &amp;lt;tt&amp;gt;5))&amp;lt;/tt&amp;gt; block. If &amp;lt;tt&amp;gt;s-&amp;gt;data&amp;lt;/tt&amp;gt; is not set (&amp;lt;tt&amp;gt;null&amp;lt;/tt&amp;gt;), the dissector returns because we cannot proceed without the FTP username being available. &amp;lt;tt&amp;gt;PACKET-&amp;gt;DISSECTOR.pass&amp;lt;/tt&amp;gt; is set to the password sent by the FTP server as pointed to by &amp;lt;tt&amp;gt;ptr&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;strchr( )&amp;lt;/tt&amp;gt; function is used to parse until the end of the password by looking for &amp;lt;tt&amp;gt;\r&amp;lt;/tt&amp;gt;. Next, &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;ident&amp;lt;/tt&amp;gt; are set free because the dissector no longer needs them. The &amp;lt;tt&amp;gt;DISSECT_MSG&amp;lt;/tt&amp;gt; macro is used to display the FTP server IP address and the username and password sent by the FTP client to the FTP server. Once this is done, the dissector simply returns.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
The source code for the FTP dissector is available in the ''src/dissectors/ec_ftp.c'' file in the Ettercap source tree. It is written by ALoR and NaGA, authors and maintainers of Ettercap.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Writing an Ettercap Plug-in ==&lt;br /&gt;
&lt;br /&gt;
You can enable or disable Ettercap plug-ins on the fly, and therefore you can use them to extend Ettercap functionality on demand. Ettercap comes bundled with a variety of plug-ins that you can find in the ''plug-ins'' directory of the Ettercap source tree. The following sections show you how to write &amp;lt;tt&amp;gt;find_tcp_conn&amp;lt;/tt&amp;gt;, a plug-in that detects the initiation of a new TCP connection on the network.&lt;br /&gt;
&lt;br /&gt;
=== The find_tcp_conn Plug-in ===&lt;br /&gt;
&lt;br /&gt;
To establish a TCP connection with a remote host, the source host sends a TCP packet with the &amp;lt;tt&amp;gt;SYN&amp;lt;/tt&amp;gt; flag set to the remote host. If the remote host is listening on a particular port, it responds with a TCP packet with the &amp;lt;tt&amp;gt;SYN&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;ACK&amp;lt;/tt&amp;gt; flags set. The source host then sends a TCP packet with the &amp;lt;tt&amp;gt;ACK&amp;lt;/tt&amp;gt; bit set to formally establish the TCP connection. This sequence is known as the ''three-way TCP handshake'' . Therefore, to detect new TCP connections with other hosts, our plug-in has to analyze the network traffic for TCP packets that have the &amp;lt;tt&amp;gt;SYN&amp;lt;/tt&amp;gt; flag set. The &amp;lt;tt&amp;gt;find_tcp_conn&amp;lt;/tt&amp;gt; plug-in described in the following paragraphs analyzes TCP packets for the &amp;lt;tt&amp;gt;SYN&amp;lt;/tt&amp;gt; flag, and if one is found, it alerts the Ettercap user that a host on the network is attempting to establish a new TCP connection with another host.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;find_tcp_conn&amp;lt;/tt&amp;gt; plug-in alerts the Ettercap user whenever a TCP packet with the &amp;lt;tt&amp;gt;SYN&amp;lt;/tt&amp;gt; flag set is captured. Therefore, the plug-in alerts the Ettercap user even if the server host does not respond to the connection attempt. This plug-in can be useful for noticing when a SYN port-scan is being performed on a network.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;warning&amp;quot;&amp;gt;&lt;br /&gt;
'''Warning'''&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;find_tcp_conn&amp;lt;/tt&amp;gt; plug-in will not detect new TCP connections when the host running Ettercap is on a network switch because network switches attempt to segregate network traffic. Therefore, the &amp;lt;tt&amp;gt;find_tcp_conn&amp;lt;/tt&amp;gt; plug-in will detect SYN packets from other hosts only when the host running Ettercap is on a network hub, or when Ettercap is instructed to perform ARP poisoning.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Every Ettercap plug-in needs to include ''ec.h'' and ''ec_plugins.h''. These files contain required global variables and plug-in APIs. The plug-in uses the &amp;lt;tt&amp;gt;packet_object&amp;lt;/tt&amp;gt; structure defined in ''ec_packet.h'' along with various functions defined in ''ec_hook.h'' , so these header files need to be included as well:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;ec.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;ec_plugins.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;ec_packet.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;ec_hook.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All Ettercap plug-ins should declare &amp;lt;tt&amp;gt;plugin_load()&amp;lt;/tt&amp;gt; , which serves as the entry point of a plug-in. Following is its prototype:&lt;br /&gt;
&lt;br /&gt;
 int plugin_load(void *);&lt;br /&gt;
&lt;br /&gt;
Following is the prototype of &amp;lt;tt&amp;gt;find_tcp_conn_init( )&amp;lt;/tt&amp;gt; , which is called when the plug-in is enabled, and &amp;lt;tt&amp;gt;find_tcp_conn_fini( )&amp;lt;/tt&amp;gt;, which is called when the plug-in is disabled:&lt;br /&gt;
&lt;br /&gt;
 static int find_tcp_conn_init(void *);&lt;br /&gt;
 static int find_tcp_conn_fini(void *);&lt;br /&gt;
&lt;br /&gt;
The plug-in invokes &amp;lt;tt&amp;gt;parse_tcp()&amp;lt;/tt&amp;gt; when a TCP packet is received. Here is its prototype:&lt;br /&gt;
&lt;br /&gt;
 static void parse_tcp(struct packet_object *po);&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;plugin_register( )&amp;lt;/tt&amp;gt; function in &amp;lt;tt&amp;gt;plugin_load()&amp;lt;/tt&amp;gt; accepts a structure of type &amp;lt;tt&amp;gt;plugin_ops&amp;lt;/tt&amp;gt; . Following is the definition of &amp;lt;tt&amp;gt;find_tcp_conn_ops&amp;lt;/tt&amp;gt; , which is an instance of &amp;lt;tt&amp;gt;plugin_ops&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 struct plugin_ops find_tcp_conn_ops = {&lt;br /&gt;
    /* ettercap version MUST be the global EC_VERSION */&lt;br /&gt;
    ettercap_version: EC_VERSION,&lt;br /&gt;
    /* the name of the plugin */&lt;br /&gt;
    name:             &amp;quot;find_tcp_conn&amp;quot;,&lt;br /&gt;
     /* a short description of the plugin (max 50 chars) */&lt;br /&gt;
    info:             &amp;quot;Detect TCP connections&amp;quot;,&lt;br /&gt;
    /* the plugin version. */&lt;br /&gt;
    version:          &amp;quot;1.0&amp;quot;,&lt;br /&gt;
    /* activation function */&lt;br /&gt;
    init:             &amp;amp;find_tcp_conn_init,&lt;br /&gt;
    /* deactivation function */&lt;br /&gt;
    fini:             &amp;amp;find_tcp_conn_fini,&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
Most of the items defined by &amp;lt;tt&amp;gt;find_tcp_conn_ops&amp;lt;/tt&amp;gt; are self-explanatory. Note that &amp;lt;tt&amp;gt;ettercap_version&amp;lt;/tt&amp;gt; must be set to &amp;lt;tt&amp;gt;EC_VERSION&amp;lt;/tt&amp;gt;. Ettercap uses this value to prevent a plug-in compiled for a different version of Ettercap from attempting to load. The &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; item declares the function that is called when the user enables the plug-in, and the &amp;lt;tt&amp;gt;fini&amp;lt;/tt&amp;gt; item declares the function that is called when the user disables the plug-in. For example, in the Ettercap GTK frontend, you can enable or disable plug-ins by selecting &amp;quot;Manage the plugins&amp;quot; from the Plugins menu and double-clicking the plug-in names.&lt;br /&gt;
&lt;br /&gt;
Following is the definition of &amp;lt;tt&amp;gt;plugin_load( )&amp;lt;/tt&amp;gt; , which is called when the plug-in is loaded. Users can load a plug-in by pressing Ctrl-O from the GTK frontend and selecting the appropriate plug-in file.&lt;br /&gt;
&lt;br /&gt;
 int plugin_load(void *handle)&lt;br /&gt;
 {&lt;br /&gt;
     return plugin_register(handle, &amp;amp;find_tcp_conn_ops);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Every plug-in must be assigned a unique handle, which the Ettercap engine generates when it invokes &amp;lt;tt&amp;gt;plugin_load( )&amp;lt;/tt&amp;gt;. As a plug-in author, you simply need to pass &amp;lt;tt&amp;gt;handle&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;plugin_register( )&amp;lt;/tt&amp;gt; as its first parameter. The second parameter, &amp;lt;tt&amp;gt;find_tcp_conn_ops&amp;lt;/tt&amp;gt;, is the structure we declared in the previous paragraphs. As we already have seen, this structure defines plug-in details as well as the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;fini&amp;lt;/tt&amp;gt; items.&lt;br /&gt;
&lt;br /&gt;
Following is the definition of &amp;lt;tt&amp;gt;find_tcp_conn_init( )&amp;lt;/tt&amp;gt; , which is defined as our &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; function and is called when the Ettercap user enables the plug-in:&lt;br /&gt;
&lt;br /&gt;
 static int find_tcp_conn_init(void *dummy)&lt;br /&gt;
 {&lt;br /&gt;
     USER_MSG(&amp;quot;find_tcp_conn: plugin running...\n&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
     hook_add(HOOK_PACKET_TCP, &amp;amp;parse_tcp);&lt;br /&gt;
 &lt;br /&gt;
     return PLUGIN_RUNNING;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;USER_MSG( )&amp;lt;/tt&amp;gt; macro displays the given string to the Ettercap user. In the case of the GTK frontend, the string is displayed in the lower section of the GUI. In this case, the plug-in displays the string &amp;lt;tt&amp;gt;find_tcp_conn&amp;lt;/tt&amp;gt;: &amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;running..&amp;lt;/tt&amp;gt;. to let the user know the plug-in has been enabled. The &amp;lt;tt&amp;gt;hook_add()&amp;lt;/tt&amp;gt; function takes in two parameters. Following is its prototype:&lt;br /&gt;
&lt;br /&gt;
 void hook_add(int point, void (*func)(struct packet_object *po))&lt;br /&gt;
&lt;br /&gt;
You use the &amp;lt;tt&amp;gt;point&amp;lt;/tt&amp;gt; parameter to decide when the plug-in hook function is to be called. We pass &amp;lt;tt&amp;gt;HOOK_PACKET_TCP&amp;lt;/tt&amp;gt; as the &amp;lt;tt&amp;gt;point&amp;lt;/tt&amp;gt; parameter to &amp;lt;tt&amp;gt;hook_add( )&amp;lt;/tt&amp;gt; to indicate that we want &amp;lt;tt&amp;gt;parse_tcp( )&amp;lt;/tt&amp;gt; to be called every time Ettercap captures a TCP packet on the network. (For an explanation of other types of hooking points, see the ''doc/plugins'' text file in the Ettercap source tree.) The &amp;lt;tt&amp;gt;find_tcp_conn_init( )&amp;lt;/tt&amp;gt; function returns &amp;lt;tt&amp;gt;PLUGIN_RUNNING&amp;lt;/tt&amp;gt;, which indicates to the Ettercap engine that the plug-in has initialized successfully.&lt;br /&gt;
&lt;br /&gt;
Here is a definition of &amp;lt;tt&amp;gt;find_tcp_conn_fini( )&amp;lt;/tt&amp;gt; , which is invoked when the Ettercap user disables the plug-in:&lt;br /&gt;
&lt;br /&gt;
 static int find_tcp_conn_fini(void *dummy)&lt;br /&gt;
 {&lt;br /&gt;
     USER_MSG(&amp;quot;find_tcp_conn: plugin terminated...\n&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
     hook_del(HOOK_PACKET_TCP, &amp;amp;parse_tcp); &lt;br /&gt;
 &lt;br /&gt;
     return PLUGIN_FINISHED;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;hook_del( )&amp;lt;/tt&amp;gt; function removes the &amp;lt;tt&amp;gt;parse_tcp( )&amp;lt;/tt&amp;gt; function as the hook function. After &amp;lt;tt&amp;gt;hook_del( )&amp;lt;/tt&amp;gt; returns, the Ettercap engine no longer invokes &amp;lt;tt&amp;gt;parse_tcp( )&amp;lt;/tt&amp;gt; when a TCP packet is received. The &amp;lt;tt&amp;gt;find_tcp_conn_fini( )&amp;lt;/tt&amp;gt; function returns &amp;lt;tt&amp;gt;PLUGIN_FINISHED&amp;lt;/tt&amp;gt; to indicate to the Ettercap engine that the plug-in finished and can be deallocated.&lt;br /&gt;
&lt;br /&gt;
Following is the definition of the &amp;lt;tt&amp;gt;parse_tcp( )&amp;lt;/tt&amp;gt; function, which is called whenever Ettercap receives a TCP packet:&lt;br /&gt;
&lt;br /&gt;
 static void parse_tcp(struct packet_object *po)&lt;br /&gt;
 {&lt;br /&gt;
    char tmp1[MAX_ASCII_ADDR_LEN];&lt;br /&gt;
    char tmp2[MAX_ASCII_ADDR_LEN];&lt;br /&gt;
 &lt;br /&gt;
    if ( po-&amp;gt;L4.flags != TH_SYN )&lt;br /&gt;
       return;&lt;br /&gt;
    &lt;br /&gt;
    USER_MSG(&amp;quot;find_tcp_conn: Probable connection attempt %s -&amp;gt; %s [%d]\n&amp;quot;,&lt;br /&gt;
 ip_addr_ntoa(&amp;amp;po-&amp;gt;L3.src, tmp1), ip_addr_ntoa(&amp;amp;po-&amp;gt;L3.dst, tmp2), ntohs(po-&amp;gt;L4.dst));&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;if&amp;lt;/tt&amp;gt; block inspects &amp;lt;tt&amp;gt;po&amp;lt;/tt&amp;gt;, which contains the TCP packet captured by Ettercap. If the packet does not have the &amp;lt;tt&amp;gt;SYN&amp;lt;/tt&amp;gt; flag set, &amp;lt;tt&amp;gt;L4.flags&amp;lt;/tt&amp;gt; will not be equal to &amp;lt;tt&amp;gt;TH_SYN&amp;lt;/tt&amp;gt;, and the function simply returns. The &amp;lt;tt&amp;gt;L4&amp;lt;/tt&amp;gt; structure signifies &amp;quot; Layer 4,&amp;quot; also known as the ''Transport Layer'' of the OSI model where TCP operates. &amp;lt;tt&amp;gt;L3&amp;lt;/tt&amp;gt; signifies &amp;quot; Layer 3,&amp;quot; also known as the ''Network Layer''&amp;quot; where the IP operates.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;USER_MSG&amp;lt;/tt&amp;gt; is invoked only if the previous &amp;lt;tt&amp;gt;if&amp;lt;/tt&amp;gt; block did not return, in which case we can be certain that the captured TCP packet has the &amp;lt;tt&amp;gt;SYN&amp;lt;/tt&amp;gt; flag set. Therefore, we call &amp;lt;tt&amp;gt;USER_MSG()&amp;lt;/tt&amp;gt; to alert the user that an attempt to establish a new TCP connection was detected, as shown in [[Network Security Tools/Modifying and Hacking Security Tools/Developing Dissectors and Plug-ins for the Ettercap Network Sniffer#networkst-CHP-2-FIG-2|Figure 2-2]]. The &amp;lt;tt&amp;gt;ip_addr_ntoa()&amp;lt;/tt&amp;gt; function accepts an IP address of type &amp;lt;tt&amp;gt;ip_addr&amp;lt;/tt&amp;gt; as the first parameter and returns a string representation when given a &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; pointer as its second parameter. Because &amp;lt;tt&amp;gt;po-&amp;gt;L3.src&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;po-&amp;gt;L3.dst&amp;lt;/tt&amp;gt; contain the source and destination IP addresses of the packet and are of type &amp;lt;tt&amp;gt;ip_addr&amp;lt;/tt&amp;gt;, the plug-in invokes &amp;lt;tt&amp;gt;ip_addr_ntoa( )&amp;lt;/tt&amp;gt; to convert them to strings to display them to the user via &amp;lt;tt&amp;gt;USER_MSG( )&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;ntohs( )&amp;lt;/tt&amp;gt; function is passed &amp;lt;tt&amp;gt;po-&amp;gt;L4.dst&amp;lt;/tt&amp;gt; as the parameter, which contains the destination port. The &amp;lt;tt&amp;gt;ntohs( )&amp;lt;/tt&amp;gt; function converts a given value from network byte order to host byte order. This is useful in preserving portability because different CPUs use different byte orders.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-2-FIG-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 2-2. The find_tcp_conn plug-in in action'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Network Security Tools_I_2_tt125.png|The find_tcp_conn plug-in in action]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== find_tcp_conn.c ===&lt;br /&gt;
&lt;br /&gt;
The easiest way to compile this plug-in is to make a new directory called ''find_tcp_conn'' in the ''plug-ins''directory in the Ettercap source tree. Then, copy over the ''Makefile'' from another plug-in called &amp;lt;tt&amp;gt;find_conn&amp;lt;/tt&amp;gt;, and replace all occurrences of &amp;lt;tt&amp;gt;find_conn&amp;lt;/tt&amp;gt; with &amp;lt;tt&amp;gt;find_tcp_conn&amp;lt;/tt&amp;gt;. Run &amp;lt;tt&amp;gt;make&amp;lt;/tt&amp;gt;, and you will end up with ''ec_find_tcp_conn.so'' in the ''.libs/'' directory. To load this plug-in from the GTK frontend, press Ctrl-O and select this file. Press Ctrl-P to go to the plug-in management section, and double-click the &amp;quot;find_tcp_conn&amp;quot; entry to enable the plug-in. Here is the complete source code for &amp;lt;tt&amp;gt;find_tcp_conn.c&amp;lt;/tt&amp;gt; for easy reference:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;ec.h&amp;gt;                        /* required for global variables */&lt;br /&gt;
 #include &amp;lt;ec_plugins.h&amp;gt;                /* required for plugin ops */&lt;br /&gt;
 #include &amp;lt;ec_packet.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;ec_hook.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 /* prototypes */&lt;br /&gt;
 int plugin_load(void *);&lt;br /&gt;
 static int find_tcp_conn_init(void *);&lt;br /&gt;
 static int find_tcp_conn_fini(void *);&lt;br /&gt;
 static void parse_tcp(struct packet_object *po);&lt;br /&gt;
 &lt;br /&gt;
 /* plugin operations */&lt;br /&gt;
 struct plugin_ops find_tcp_conn_ops = {&lt;br /&gt;
     /* ettercap version MUST be the global EC_VERSION */&lt;br /&gt;
     ettercap_version: EC_VERSION,&lt;br /&gt;
     /* the name of the plugin */&lt;br /&gt;
     name:             &amp;quot;find_tcp_conn&amp;quot;,&lt;br /&gt;
     /* a short description of the plugin (max 50 chars) */&lt;br /&gt;
     info:             &amp;quot;Detect TCP connections&amp;quot;,&lt;br /&gt;
     /* the plugin version. */&lt;br /&gt;
     version:          &amp;quot;1.0&amp;quot;,&lt;br /&gt;
     /* activation function */&lt;br /&gt;
     init:             &amp;amp;find_tcp_conn_init,&lt;br /&gt;
     /* deactivation function */&lt;br /&gt;
     fini:             &amp;amp;find_tcp_conn_fini,&lt;br /&gt;
 };&lt;br /&gt;
 /* this function is called on plugin load */&lt;br /&gt;
 int plugin_load(void *handle)&lt;br /&gt;
 {  &lt;br /&gt;
     return plugin_register(handle, &amp;amp;find_tcp_conn_ops);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 static int find_tcp_conn_init(void *dummy)&lt;br /&gt;
 {&lt;br /&gt;
     USER_MSG(&amp;quot;find_tcp_conn: plugin running...\n&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
     hook_add(HOOK_PACKET_TCP, &amp;amp;parse_tcp);&lt;br /&gt;
 &lt;br /&gt;
     return PLUGIN_RUNNING;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 static int find_tcp_conn_fini(void *dummy)&lt;br /&gt;
 {&lt;br /&gt;
     USER_MSG(&amp;quot;find_tcp_conn: plugin terminated...\n&amp;quot;);&lt;br /&gt;
   &lt;br /&gt;
     hook_del(HOOK_PACKET_TCP, &amp;amp;parse_tcp);&lt;br /&gt;
 &lt;br /&gt;
     return PLUGIN_FINISHED;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 /* Parse the TCP request */&lt;br /&gt;
 static void parse_tcp(struct packet_object *po)&lt;br /&gt;
 {&lt;br /&gt;
     char tmp1[MAX_ASCII_ADDR_LEN];&lt;br /&gt;
     char tmp2[MAX_ASCII_ADDR_LEN];&lt;br /&gt;
    &lt;br /&gt;
     if ( po-&amp;gt;L4.flags != TH_SYN )&lt;br /&gt;
         return;&lt;br /&gt;
 &lt;br /&gt;
     USER_MSG(&amp;quot;find_tcp_conn: Probable connection attempt %s -&amp;gt; %s [%d]\n&amp;quot;,&lt;br /&gt;
 ip_addr_ntoa(&amp;amp;po-&amp;gt;L3.src, tmp1), ip_addr_ntoa(&amp;amp;po-&amp;gt;L3.dst, tmp2),&lt;br /&gt;
 ntohs(po-&amp;gt;L4.dst));&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
See the ''doc/plugins'' text file within the Ettercap source tree for a listing and description of other useful plug-in-related function calls.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>Docbook2Wiki</name></author>	</entry>

	</feed>