<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://commons.oreilly.com/wiki/skins/common/feed.css?97"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap - Revision history</title>
		<link>http://commons.oreilly.com/wiki/index.php?title=Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Extending_Hydra_and_Nmap&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.11.0</generator>
		<lastBuildDate>Sat, 25 May 2013 11:51:28 GMT</lastBuildDate>
		<item>
			<title>Docbook2Wiki: Initial conversion from Docbook</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Extending_Hydra_and_Nmap&amp;diff=9245&amp;oldid=prev</link>
			<description>&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;</description>
			<pubDate>Tue, 11 Mar 2008 22:54:01 GMT</pubDate>			<dc:creator>Docbook2Wiki</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Extending_Hydra_and_Nmap</comments>		</item>
		<item>
			<title>Docbook2Wiki: Initial conversion from Docbook</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Extending_Hydra_and_Nmap&amp;diff=8331&amp;oldid=prev</link>
			<description>&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;
Many security tools do not support a plug-in architecture, making extending these tools somewhat challenging. However, if your security tool uses a modular architecture, or if it uses a configurable database for specifying its behavior, you can extend it more easily. In this chapter we demonstrate how to extend the popular open source security tool, Hydra, to support an additional protocol, as well as adding signatures to the service signature file for the popular port scanner, Nmap, to recognize additional services.&lt;br /&gt;
&lt;br /&gt;
By extending existing tools to support additional protocols and services, you can test for security vulnerabilities in networks where nonstandard or proprietary protocols and services exist, without creating an entirely new tool from scratch.&lt;br /&gt;
&lt;br /&gt;
== Extending Hydra ==&lt;br /&gt;
&lt;br /&gt;
Hydra is a popular tool written by Van Hauser (''http://www.thc.org/'') for testing networked services for weak username and password combinations. This technique, commonly known as brute-force testing, is valuable for ensuring that network services and systems are not vulnerable to password-guessing attacks due to weak username and password combinations.&lt;br /&gt;
&lt;br /&gt;
Although Hydra supports a number of different protocols for testing, most likely you'll want to test services available on your network that Hydra doesn't support. In this section we will demonstrate how to add a module for testing Simple Mail Transport Protocol (SMTP) authentication. You could use this to determine if weak passwords exist in your email user base and close this potential exposure before a spammer takes advantage of it.&lt;br /&gt;
&lt;br /&gt;
Hydra is freely available for noncommercial use and for commercial use with proper acknowledgment. You can download it from ''http://www.thc.org/thc-hydra/''. The module described in this section is included in Hydra Version 4.2.&lt;br /&gt;
&lt;br /&gt;
=== Overview of Hydra ===&lt;br /&gt;
&lt;br /&gt;
Hydra is a very popular tool primarily because of the wide variety of protocols it supports and because its parallel nature divides password-testing tasks among a user-definable number of tasks.&lt;br /&gt;
&lt;br /&gt;
As of Version 4.4, Hydra supports the following protocols:&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
| telnet || ftp || http&lt;br /&gt;
|-&lt;br /&gt;
| https || http proxy || ldap&lt;br /&gt;
|-&lt;br /&gt;
| SMB || SMBNT || Microsoft SQL&lt;br /&gt;
|-&lt;br /&gt;
| mysql || rexec || socks5&lt;br /&gt;
|-&lt;br /&gt;
| VNC || pop3 || imap&lt;br /&gt;
|-&lt;br /&gt;
| nntp || pcnfs || icq&lt;br /&gt;
|-&lt;br /&gt;
| SAP/R3 || Cisco auth || Cisco enable&lt;br /&gt;
|-&lt;br /&gt;
| SMTP auth || ssh2 || snmp&lt;br /&gt;
|-&lt;br /&gt;
| cvs || Cisco AAA || &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Hydra is primarily a command-line security-testing tool, and as such you can call it from within recent versions of Nessus to perform login (username) and password testing on services identified by Nessus. In addition to using the tool through Nessus, recent versions of Hydra also come with a graphical GTK user interface for platforms supporting the GTK toolkit.&lt;br /&gt;
&lt;br /&gt;
=== Overview of SMTP Authentication ===&lt;br /&gt;
&lt;br /&gt;
In this section we will demonstrate how to add SMTP authentication protocol support to Hydra. Mail servers commonly use SMTP authentication to identify a user as being valid prior to accepting email for delivery.&lt;br /&gt;
&lt;br /&gt;
A number of different standards for SMTP authentication exist, many of which are not RFC standards. We are demonstrating an authentication method using the &amp;lt;tt&amp;gt;AUTH&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;LOGIN&amp;lt;/tt&amp;gt; method, as shown in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-EX-1|Example 3-1]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-EX-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-1. An SMTP AUTH session'''&lt;br /&gt;
&lt;br /&gt;
 220-mail.xxxxxxxx.com ESMTP Exim 4.34 #1 Wed, 23 Jun 2004 17:35:13 -0700 &lt;br /&gt;
 EHLO mail.myserver.com&lt;br /&gt;
 250-mail.xxxxxxxx.com Hello mail.myserver.com [192.168.0.156]&lt;br /&gt;
 250-SIZE 52428800&lt;br /&gt;
 250-PIPELINING&lt;br /&gt;
 250-AUTH PLAIN LOGIN&lt;br /&gt;
 250-STARTTLS&lt;br /&gt;
 250 HELP&lt;br /&gt;
 AUTH LOGIN&lt;br /&gt;
 334 VXNlcm5hbWU6&lt;br /&gt;
 bXl1c2VybmFtZQ==&lt;br /&gt;
 334 UGFzc3dvcmQ6&lt;br /&gt;
 bXlwYXNzd29yZA==&lt;br /&gt;
 235 Authentication succeeded&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;AUTH&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;LOGIN&amp;lt;/tt&amp;gt; authentication method is well supported by many common SMTP servers, and as such, it is a good protocol to use. The protocol is a simple process that uses unencrypted credentials. Even though the protocol is insecure, a number of mail servers support it in their default configurations as a lowest-common-denominator protocol for SMTP authentication.&lt;br /&gt;
&lt;br /&gt;
The protocol can be demonstrated by using the &amp;lt;tt&amp;gt;telnet&amp;lt;/tt&amp;gt; command to port 25 on an available mail server. The mail server then responds with a connection message:&lt;br /&gt;
&lt;br /&gt;
 220-mail.xxxxxxxx.com ESMTP Exim 4.34 #1 Wed, 23 Jun 2004 17:35:13 -0700&lt;br /&gt;
&lt;br /&gt;
The mail server responds with a header containing the SMTP response code 220. Similar to the HTTP protocol, SMTP uses a numbered response code system, as shown in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-1|Table 3-1]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-TABLE-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-1. SMTP 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;
| 2xx; e.g., 220 (service ready) || Command accepted and processed&lt;br /&gt;
|-&lt;br /&gt;
| 3xx; e.g., 354 (start mail input) || Flow control message&lt;br /&gt;
|-&lt;br /&gt;
| 4xx; e.g., 421 (service not available) || Critical failure or transfer failure&lt;br /&gt;
|-&lt;br /&gt;
| 5xx; e.g., 500 (syntax error) || Errors with command&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In this case, the mail server (or more accurately, the MTA, or Mail Transfer Agent) is running the open source Exim service. Then we need to start an email session with the mail server by using the &amp;lt;tt&amp;gt;EHLO&amp;lt;/tt&amp;gt; command with our Internet hostname, as shown here:&lt;br /&gt;
&lt;br /&gt;
 250-mail.xxxxxxxx.com Hello mail.myserver.com [192.168.0.156]&lt;br /&gt;
 250-SIZE 52428800&lt;br /&gt;
 250-PIPELINING&lt;br /&gt;
 250-AUTH PLAIN LOGIN&lt;br /&gt;
 250-STARTTLS&lt;br /&gt;
 250 HELP&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;EHLO&amp;lt;/tt&amp;gt; command informs the server that we want to use the Extended Simple Mail Transfer Protocol (ESMTP) and determines the SMTP extensions supported by the mail server, including the types of authentication (if any) supported by the server we are interrogating. The &amp;lt;tt&amp;gt;AUTH&amp;lt;/tt&amp;gt; keyword is followed by two different types of authentication, indicating that this server supports both the &amp;lt;tt&amp;gt;PLAIN&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;LOGIN&amp;lt;/tt&amp;gt; authentication methods. This command is important, as RFC-compliant mail servers should respond with an error message such as &amp;lt;tt&amp;gt;503&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;AUTH&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;command&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;used&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;when&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;not&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;advertised&amp;lt;/tt&amp;gt; if the &amp;lt;tt&amp;gt;AUTH&amp;lt;/tt&amp;gt; keyword is used without a preceding &amp;lt;tt&amp;gt;EHLO&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Then we send the mail server an &amp;lt;tt&amp;gt;AUTH&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;LOGIN&amp;lt;/tt&amp;gt; command to start the authentication process with the server:&lt;br /&gt;
&lt;br /&gt;
 AUTH LOGIN&lt;br /&gt;
 334 VXNlcm5hbWU6&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;AUTH LOGIN&amp;lt;/tt&amp;gt; command instructs the server that the client wants to begin SMTP authentication using the &amp;lt;tt&amp;gt;LOGIN&amp;lt;/tt&amp;gt; method. The server has responded with the 334 status code, and a Base64-encoded representation of the string &amp;lt;tt&amp;gt;Username&amp;lt;/tt&amp;gt;: to prompt the client to supply the username. The client supplies the username for authentication encoded using Base64 encoding. The username used here is &amp;lt;tt&amp;gt;myusername&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 bXl1c2VybmFtZQ==&lt;br /&gt;
 334 UGFzc3dvcmQ6&lt;br /&gt;
&lt;br /&gt;
Then the server responds with a Base64-encoded representation of the string &amp;lt;tt&amp;gt;Password&amp;lt;/tt&amp;gt;: to prompt the client to supply the password. The client supplies the password encoded using Base64 encoding. The password used in this example is &amp;lt;tt&amp;gt;mypassword&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 bXlwYXNzd29yZA==&lt;br /&gt;
 235 Authentication succeeded&lt;br /&gt;
&lt;br /&gt;
Providing the username and password supplied are correct, the server responds with a 2xx status code. If the username and password combination is incorrect the server responds with a 5xx response code.&lt;br /&gt;
&lt;br /&gt;
=== Adding Additional Protocols to Hydra ===&lt;br /&gt;
&lt;br /&gt;
Hydra is structured in a very modular way, and therefore adding support for an additional protocol requires that Hydra support the defined module interface.&lt;br /&gt;
&lt;br /&gt;
Each protocol is implemented in a file called ''hydra-'' &amp;lt;tt&amp;gt;''&amp;lt;service name&amp;gt;''&amp;lt;/tt&amp;gt; ''.c'' containing a function prototype:&lt;br /&gt;
&lt;br /&gt;
 void service_&amp;lt;service name&amp;gt; (unsigned long int ip, int sp, unsigned char options,&lt;br /&gt;
             char *miscptr, FILE *fp, int port);&lt;br /&gt;
&lt;br /&gt;
The options passed to the service function are outlined in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-2|Table 3-2]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-TABLE-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-2. Service function parameters'''&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;
 ip&lt;br /&gt;
| &amp;lt;tt&amp;gt;ip&amp;lt;/tt&amp;gt; is the IP address of the target host.&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 sp&lt;br /&gt;
| &amp;lt;tt&amp;gt;sp&amp;lt;/tt&amp;gt; is a socket used to read login (username) and password pairs for this task.&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 options&lt;br /&gt;
| &amp;lt;tt&amp;gt;options&amp;lt;/tt&amp;gt; is for user options. Currently this is &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;OPTION_SSL&amp;lt;/tt&amp;gt; if the user has specified to use Secure Sockets Layer (SSL).&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 miscptr&lt;br /&gt;
| &amp;lt;tt&amp;gt;miscptr&amp;lt;/tt&amp;gt; is a user-supplied additional parameter. This is for services that require more information than is supplied by default. Example modules using this parameter include the &amp;lt;tt&amp;gt;http&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;https&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;http-proxy&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;smbnt&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;ldap&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;cisco-enable&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;SAP/R3&amp;lt;/tt&amp;gt; modules.&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 fp&lt;br /&gt;
| &amp;lt;tt&amp;gt;fp&amp;lt;/tt&amp;gt; is a socket used to report found login (username) and password pairs for this task.&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 port&lt;br /&gt;
| If the user has defined a port to connect to, it is contained in &amp;lt;tt&amp;gt;port&amp;lt;/tt&amp;gt;. This is used when services are run on nonstandard ports.&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once the service file has been written, integrating the modules into Hydra is simple:&lt;br /&gt;
&lt;br /&gt;
* Add the new ''hydra-'' &amp;lt;tt&amp;gt;''&amp;lt;service&amp;gt;''&amp;lt;/tt&amp;gt; into the relevant areas within the ''Makefile.am'' file.&lt;br /&gt;
* Edit the ''hydra.c'' file to add a reference to the new module. You can determine where to add this reference by searching for the string &amp;lt;tt&amp;gt;ADD&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;NEW&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;SERVICES&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;HERE&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Add default service ports into ''hydra.h''.&lt;br /&gt;
&lt;br /&gt;
Note that this will not add the new module into the &amp;lt;tt&amp;gt;xhydra&amp;lt;/tt&amp;gt; graphical interface. Also note that you will need to patch this to support the ability to call the new module.&lt;br /&gt;
&lt;br /&gt;
=== Implementing SMTP-AUTH in Hydra ===&lt;br /&gt;
&lt;br /&gt;
Every protocol Hydra supports needs to define the following variables and include files:&lt;br /&gt;
&lt;br /&gt;
 #include &amp;quot;hydra-mod.h&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 extern char *HYDRA_EXIT;&lt;br /&gt;
 &lt;br /&gt;
 char *buf;&lt;br /&gt;
&lt;br /&gt;
The ''hydra-mod.h'' include file defines the functions the module accesses while running. The &amp;lt;tt&amp;gt;HYDRA_EXIT&amp;lt;/tt&amp;gt; string is a value returned by some Hydra functions. The &amp;lt;tt&amp;gt;buf&amp;lt;/tt&amp;gt; pointer is used in ''hydra-smtpauth.c'' as a temporary buffer for data received.&lt;br /&gt;
&lt;br /&gt;
 void&lt;br /&gt;
 service_smtpauth(unsigned long int ip, int sp, unsigned char options,&lt;br /&gt;
         char *miscptr, FILE * fp, int port)&lt;br /&gt;
 {&lt;br /&gt;
   int run = 1, next_run, sock = -1;&lt;br /&gt;
   int myport = PORT_SMTPAUTH, mysslport = PORT_SMTPAUTH_SSL;&lt;br /&gt;
   char *buffer = &amp;quot;EHLO hydra\r\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;run&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;next_run&amp;lt;/tt&amp;gt; variables are used to control the state of the testing session. &amp;lt;tt&amp;gt;service_smtpauth&amp;lt;/tt&amp;gt; follows a convention similar to many of the other text-based protocols supported in Hydra, whereby it is possible to connect and try multiple sets of credentials. The &amp;lt;tt&amp;gt;run&amp;lt;/tt&amp;gt; values are specified in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-3|Table 3-3]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-TABLE-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-3. run values'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! run values !! Description&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt; || Connect or reconnect to the service port.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt; || Run the password-testing function on the established connection. You can run this multiple times for one connection for this protocol.&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt; || Close the connection and exit gracefully.&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;sock&amp;lt;/tt&amp;gt; variable is used to track the status of the connection to the service. The &amp;lt;tt&amp;gt;PORT_SMTPAUTH&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;PORT_SMTPAUTH_SSL&amp;lt;/tt&amp;gt; values have been added to the ''hydra.h'' file, and they are the ports SMTP runs on normally and when run over SSL (ports 25 and 465, respectively). The string buffer is the SMTP &amp;lt;tt&amp;gt;EHLO&amp;lt;/tt&amp;gt; command to be sent to the server.&lt;br /&gt;
&lt;br /&gt;
   /* keep track of socket for login/password */&lt;br /&gt;
   hydra_register_socket(sp);&lt;br /&gt;
 &lt;br /&gt;
   /* get the next login/password pair to test */&lt;br /&gt;
   if (memcmp(hydra_get_next_pair( ), &amp;amp;HYDRA_EXIT, sizeof(HYDRA_EXIT)) == 0)&lt;br /&gt;
     return;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;hydra_register_socket()&amp;lt;/tt&amp;gt; function is required to register the socket &amp;lt;tt&amp;gt;sp&amp;lt;/tt&amp;gt; supplied to the module with the Hydra functions used to obtain the login (username) and password pairs for testing. Due to the parallelized structure of Hydra, each running task obtains separate login (username) and password combinations to optimize testing.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;hydra_get_next_pair( )&amp;lt;/tt&amp;gt; function is used to obtain the next pair of credentials for testing. This function returns &amp;lt;tt&amp;gt;HYDRA_EXIT&amp;lt;/tt&amp;gt; on failure. These credentials are later obtained as strings using the functions &amp;lt;tt&amp;gt;hydra_get_next_login()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;hydra_get_next_password()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
   /* permanent loop keyed on the run variable */&lt;br /&gt;
   while (1) {&lt;br /&gt;
     switch (run) {&lt;br /&gt;
     case 1:                    /* connect and service init function */&lt;br /&gt;
       /* if we are already connected */&lt;br /&gt;
       if (sock &amp;gt;= 0)&lt;br /&gt;
         sock = hydra_disconnect(sock);&lt;br /&gt;
       usleep(300000);&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;run&amp;lt;/tt&amp;gt; variable is used here in a &amp;lt;tt&amp;gt;switch&amp;lt;/tt&amp;gt; statement to control the state of the connection to the server. The values used for the &amp;lt;tt&amp;gt;run&amp;lt;/tt&amp;gt; variable are shown in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-3|Table 3-3]]. This functionality ensures that if a connection to the server is already in place, it is disconnected with &amp;lt;tt&amp;gt;hydra_disconnect()&amp;lt;/tt&amp;gt; . In this way, the module can ensure that a new connection is made if an error occurs by ensuring the &amp;lt;tt&amp;gt;run&amp;lt;/tt&amp;gt; variable is set to &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
       /* determine port to connect to */&lt;br /&gt;
       if ((options &amp;amp; OPTION_SSL) == 0) {&lt;br /&gt;
         if (port != 0)&lt;br /&gt;
           myport = port;&lt;br /&gt;
         sock = hydra_connect_tcp(ip, myport);&lt;br /&gt;
         port = myport;&lt;br /&gt;
       } else {&lt;br /&gt;
         if (port != 0)&lt;br /&gt;
           mysslport = port;&lt;br /&gt;
         sock = hydra_connect_ssl(ip, mysslport);&lt;br /&gt;
         port = myport;&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
If the user has not specified the use of SSL, the module connects to the default port for the service, or it connects to the user-defined port if it has been supplied using &amp;lt;tt&amp;gt;hydra_connect_tcp()&amp;lt;/tt&amp;gt; . If SSL has been specified, the default SSL port is used unless the user has specified a custom port, and the connection is made using &amp;lt;tt&amp;gt;hydra_connect_ssl()&amp;lt;/tt&amp;gt; . For protocols using UDP, such as SNMP, Hydra also supports the &amp;lt;tt&amp;gt;hydra_connect_udp()&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
       /* see if connect succeeded */&lt;br /&gt;
       if (sock &amp;lt; 0) {&lt;br /&gt;
         hydra_report(stderr, &amp;quot;Error: Child with pid %d can't connect\n&amp;quot;,&lt;br /&gt;
              (int) getpid( ));&lt;br /&gt;
         hydra_child_exit(1);&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
If the connection did not succeed, Hydra will print an error to &amp;lt;tt&amp;gt;STDERR&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;hydra_report( )&amp;lt;/tt&amp;gt; function is a synonym for &amp;lt;tt&amp;gt;fprintf&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;hydra_child_exit()&amp;lt;/tt&amp;gt; function reports the exit status of the child task, as in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-4|Table 3-4]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-TABLE-4&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-4. hydra_child_exit( ) values'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Value !! Description&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; || Normal exit&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt; || Could not connect to the service&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt; || Application protocol error or service shutdown&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once the connection is made, many protocols send some form of data as a banner or to begin authentication.&lt;br /&gt;
&lt;br /&gt;
       /* consume any data waiting in buffer */&lt;br /&gt;
       while (hydra_data_ready(sock)) {&lt;br /&gt;
         if((buf = hydra_receive_line(sock)) == NULL)&lt;br /&gt;
           exit(-1);&lt;br /&gt;
         free(buf);&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;hydra_data_ready()&amp;lt;/tt&amp;gt; function returns regardless of whether data is to be read from the connected socket. If data is to be read, &amp;lt;tt&amp;gt;hydra_receive_line()&amp;lt;/tt&amp;gt; reads the data in the receive buffer from the socket, and the data is thrown away. This is done to ensure that any banner messages are consumed from the buffer prior to any other actions. Note that we free the buffer that was read. It is important to perform this step on all data reads to avoid memory leaks.&lt;br /&gt;
&lt;br /&gt;
In addition to the &amp;lt;tt&amp;gt;hydra_receive_line( )&amp;lt;/tt&amp;gt; function, Hydra also has the simpler &amp;lt;tt&amp;gt;hydra_recv( )&amp;lt;/tt&amp;gt; function that is useful if using a binary protocol.&lt;br /&gt;
&lt;br /&gt;
       /* send EHLO command */&lt;br /&gt;
       if (hydra_send(sock, buffer, strlen(buffer), 0) &amp;lt; 0)&lt;br /&gt;
         exit(-1);&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;hydra_send( )&amp;lt;/tt&amp;gt; function is used to send the &amp;lt;tt&amp;gt;EHLO&amp;lt;/tt&amp;gt; command to the server.&lt;br /&gt;
&lt;br /&gt;
       /* see if there was any response */&lt;br /&gt;
       if ((buf = hydra_receive_line(sock)) == NULL)&lt;br /&gt;
         exit(-1);&lt;br /&gt;
 &lt;br /&gt;
       /* see if the LOGIN keyword is in the response */&lt;br /&gt;
       if (strstr(buf, &amp;quot;LOGIN&amp;quot;) == NULL) {       /* check AUTH LOGIN supported */&lt;br /&gt;
         hydra_report(stderr, &amp;quot;Error: SMTP AUTH LOGIN not supported: %s\n&amp;quot;, buf);&lt;br /&gt;
         hydra_child_exit(2);&lt;br /&gt;
         exit(-1);&lt;br /&gt;
       }&lt;br /&gt;
       free(buf);&lt;br /&gt;
       next_run = 2;    /* run crack next */&lt;br /&gt;
       break;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;buf&amp;lt;/tt&amp;gt; buffer received in response to the &amp;lt;tt&amp;gt;EHLO&amp;lt;/tt&amp;gt; command is checked to see if it contains the word &amp;lt;tt&amp;gt;LOGIN&amp;lt;/tt&amp;gt; . This is done to validate whether the server advertises the presence of the &amp;lt;tt&amp;gt;AUTH LOGIN&amp;lt;/tt&amp;gt; command. If the command is present, the &amp;lt;tt&amp;gt;next_run&amp;lt;/tt&amp;gt; value (and therefore the next value of the &amp;lt;tt&amp;gt;run&amp;lt;/tt&amp;gt; variable) is set to &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt;, which initiates the testing process on the next cycle through the loop.&lt;br /&gt;
&lt;br /&gt;
     case 2:                    /* run the cracking function */&lt;br /&gt;
       next_run = start_smtpauth(sock, ip, port, options, miscptr, fp);&lt;br /&gt;
       break;&lt;br /&gt;
&lt;br /&gt;
Where the &amp;lt;tt&amp;gt;run&amp;lt;/tt&amp;gt; variable is &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt;, the connection has been established and the testing function is started, as per [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-3|Table 3-3]].&lt;br /&gt;
&lt;br /&gt;
     case 3:                    /* clean exit */&lt;br /&gt;
       /* if connected */&lt;br /&gt;
       if (sock &amp;gt;= 0)&lt;br /&gt;
         sock = hydra_disconnect(sock);&lt;br /&gt;
       hydra_child_exit(0);&lt;br /&gt;
       return;&lt;br /&gt;
&lt;br /&gt;
Where the &amp;lt;tt&amp;gt;run&amp;lt;/tt&amp;gt; variable is &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt;, the socket is disconnected and the task exits cleanly, as per [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-3|Table 3-3]].&lt;br /&gt;
&lt;br /&gt;
     default:&lt;br /&gt;
       hydra_report(stderr, &amp;quot;Caught unknown return code, exiting!\n&amp;quot;);&lt;br /&gt;
       hydra_child_exit(0);&lt;br /&gt;
       exit(-1);&lt;br /&gt;
     }&lt;br /&gt;
     run = next_run;   /* next step dependant on return from cracking function */&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;service_smtpauth()&amp;lt;/tt&amp;gt; function exits if the &amp;lt;tt&amp;gt;start_smtpauth()&amp;lt;/tt&amp;gt; function returns a value other than &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;3&amp;lt;/tt&amp;gt;. This ensures that the simple state machine controlled by the &amp;lt;tt&amp;gt;run&amp;lt;/tt&amp;gt; variable is always in one of the three defined states—connecting/reconnecting, testing, or disconnecting.&lt;br /&gt;
&lt;br /&gt;
Where the connection has been established successfully, and the &amp;lt;tt&amp;gt;run&amp;lt;/tt&amp;gt; variable is set to &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt;, the &amp;lt;tt&amp;gt;service_smtpauth( )&amp;lt;/tt&amp;gt; function calls the &amp;lt;tt&amp;gt;start_smtpauth( )&amp;lt;/tt&amp;gt; function to perform a single testing instance.&lt;br /&gt;
&lt;br /&gt;
 int&lt;br /&gt;
 start_smtpauth(int s, unsigned long int ip, int port, unsigned char options,&lt;br /&gt;
         char *miscptr, FILE *fp)&lt;br /&gt;
&lt;br /&gt;
Here the &amp;lt;tt&amp;gt;start_smtpauth( )&amp;lt;/tt&amp;gt; function is passed the same values as those passed to the &amp;lt;tt&amp;gt;service_smtpauth( )&amp;lt;/tt&amp;gt; function. This function is not called from outside of this module; however, the naming and structure throughout the existing protocols supported in Hydra largely follow this convention.&lt;br /&gt;
&lt;br /&gt;
   char *empty = &amp;quot;&amp;quot;;&lt;br /&gt;
   char *login, *pass, buffer[300], buffer2[300];&lt;br /&gt;
 &lt;br /&gt;
   /* get login and password from the pair fetched */&lt;br /&gt;
   if (strlen(login = hydra_get_next_login( )) == 0)&lt;br /&gt;
     login = empty;&lt;br /&gt;
   if (strlen(pass = hydra_get_next_password( )) == 0)&lt;br /&gt;
     pass = empty;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;hydra_get_next_login()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;hydra_get_next_password()&amp;lt;/tt&amp;gt; functions are used to obtain the login (username) and password pair to be used for this instance of testing. These functions rely on the &amp;lt;tt&amp;gt;hydra_get_next_pair()&amp;lt;/tt&amp;gt; function having been run to first read the login and password pair from the internal socket.&lt;br /&gt;
&lt;br /&gt;
   /* consume any remaining data in the buffer */&lt;br /&gt;
   while (hydra_data_ready(s) &amp;gt; 0) {&lt;br /&gt;
     if ((buf = hydra_receive_line(s)) == NULL)&lt;br /&gt;
       return (1);&lt;br /&gt;
     free(buf);         /* make sure we free memory we use */&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
Any data returned from the server remaining in the buffer is read and thrown away. If an error occurs while reading data, the function returns &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;, which causes the &amp;lt;tt&amp;gt;service_smtpauth( )&amp;lt;/tt&amp;gt; function to attempt to reconnect to the server.&lt;br /&gt;
&lt;br /&gt;
   /* send AUTH LOGIN command */&lt;br /&gt;
   sprintf(buffer, &amp;quot;AUTH LOGIN\r\n&amp;quot;);&lt;br /&gt;
   if (hydra_send(s, buffer, strlen(buffer), 0) &amp;lt; 0) {&lt;br /&gt;
     return 1;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;AUTH LOGIN&amp;lt;/tt&amp;gt; command is sent to start an authentication attempt. If this fails, you should try to reconnect again.&lt;br /&gt;
&lt;br /&gt;
   /* if no response received */&lt;br /&gt;
   if ((buf = hydra_receive_line(s)) == NULL)&lt;br /&gt;
     return 1;&lt;br /&gt;
 &lt;br /&gt;
   /* make sure we got a 334 response code (asking for username) */&lt;br /&gt;
   if (strstr(buf, &amp;quot;334&amp;quot;) == NULL) {&lt;br /&gt;
     hydra_report(stderr, &amp;quot;Error: SMTP AUTH LOGIN error: %s\n&amp;quot;, buf);&lt;br /&gt;
     free(buf);&lt;br /&gt;
     return 3;&lt;br /&gt;
   }&lt;br /&gt;
   free(buf);&lt;br /&gt;
&lt;br /&gt;
If the response from the mail server is something other than &amp;lt;tt&amp;gt;334&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;VXNlcm5hbWU6&amp;lt;/tt&amp;gt;, you have experienced a protocol error, so you should exit. This might occur if the mail server does not support the authentication method you are attempting.&lt;br /&gt;
&lt;br /&gt;
   /* base64 encode the username - also making sure string is &amp;lt; 250 */&lt;br /&gt;
   sprintf(buffer2, &amp;quot;%.250s&amp;quot;, login);  &lt;br /&gt;
   hydra_tobase64((unsigned char *) buffer2);&lt;br /&gt;
   sprintf(buffer, &amp;quot;%.250s\r\n&amp;quot;, buffer2);&lt;br /&gt;
 &lt;br /&gt;
   /* send the username */&lt;br /&gt;
   if (hydra_send(s, buffer, strlen(buffer), 0) &amp;lt; 0) {&lt;br /&gt;
     return 1;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
Send the login (username) obtained from &amp;lt;tt&amp;gt;hydra_get_next_login()&amp;lt;/tt&amp;gt; . This is Base64-encoded using &amp;lt;tt&amp;gt;hydra_tobase64()&amp;lt;/tt&amp;gt; . A &amp;lt;tt&amp;gt;hydra_conv64( )&amp;lt;/tt&amp;gt; function exists for Base64-encoding single characters, if required. Note that we are ensuring that the user-supplied data is cut off at 250 characters to avoid a potential buffer overflow issue.&lt;br /&gt;
&lt;br /&gt;
   /* if no response received */&lt;br /&gt;
   if ((buf = hydra_receive_line(s)) == NULL)&lt;br /&gt;
     return (1);&lt;br /&gt;
 &lt;br /&gt;
   /* make sure we get a 334 - asking for password */&lt;br /&gt;
   if (strstr(buf, &amp;quot;334&amp;quot;) == NULL) {&lt;br /&gt;
     hydra_report(stderr, &amp;quot;Error: SMTP AUTH LOGIN error: %s\n&amp;quot;, buf);&lt;br /&gt;
     free(buf);&lt;br /&gt;
     return (3);&lt;br /&gt;
   }&lt;br /&gt;
   free(buf);&lt;br /&gt;
 &lt;br /&gt;
   /* base64 encode the password */&lt;br /&gt;
   sprintf(buffer2, &amp;quot;%.250s&amp;quot;, pass);&lt;br /&gt;
   hydra_tobase64((unsigned char *) buffer2);&lt;br /&gt;
   sprintf(buffer, &amp;quot;%.250s\r\n&amp;quot;, buffer2);&lt;br /&gt;
 &lt;br /&gt;
   /* send the password */&lt;br /&gt;
   if (hydra_send(s, buffer, strlen(buffer), 0) &amp;lt; 0) {&lt;br /&gt;
     return 1;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   /* if no response received */&lt;br /&gt;
   if ((buf = hydra_receive_line(s)) == NULL)&lt;br /&gt;
     return (1);&lt;br /&gt;
&lt;br /&gt;
The password received from &amp;lt;tt&amp;gt;hydra_get_next_password()&amp;lt;/tt&amp;gt; is sent to the mail server the same way in which the username was sent.&lt;br /&gt;
&lt;br /&gt;
   /* if authentication was successful */&lt;br /&gt;
   if (strstr(buf, &amp;quot;235&amp;quot;) != NULL) {&lt;br /&gt;
     /* report the found credentials */&lt;br /&gt;
     hydra_report_found_host(port, ip, &amp;quot;smtpauth&amp;quot;, fp);&lt;br /&gt;
     hydra_completed_pair_found( );&lt;br /&gt;
     free(buf);&lt;br /&gt;
     if (memcmp(hydra_get_next_pair( ), &amp;amp;HYDRA_EXIT, sizeof(HYDRA_EXIT)) == 0)&lt;br /&gt;
       return 3;&lt;br /&gt;
     return 1;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
If the &amp;lt;tt&amp;gt;235 Authentication succeeded&amp;lt;/tt&amp;gt; response is received from the mail server, the successful login (username) and password combination is reported using the &amp;lt;tt&amp;gt;hydra_report_found_host()&amp;lt;/tt&amp;gt; function. The &amp;lt;tt&amp;gt;hydra_completed_pair_found()&amp;lt;/tt&amp;gt; function is used to communicate on the internal socket that the current credentials were successful. Then the &amp;lt;tt&amp;gt;hydra_get_next_pair()&amp;lt;/tt&amp;gt; function fetches the next pair of credentials for use and causes the module to exit cleanly if no credential pairs remain.&lt;br /&gt;
&lt;br /&gt;
   free(buf);&lt;br /&gt;
   &lt;br /&gt;
   /* otherwise, we're finished with this pair anyway */&lt;br /&gt;
   hydra_completed_pair( );&lt;br /&gt;
   if (memcmp(hydra_get_next_pair( ), &amp;amp;HYDRA_EXIT, sizeof(HYDRA_EXIT)) == 0)&lt;br /&gt;
     return 3;&lt;br /&gt;
 &lt;br /&gt;
   return 2;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If the authentication attempt was not successful, the completed status of the pair is communicated using the &amp;lt;tt&amp;gt;hydra_completed_pair()&amp;lt;/tt&amp;gt; function. Then the &amp;lt;tt&amp;gt;hydra_get_next_pair( )&amp;lt;/tt&amp;gt; function is used to fetch the next pair, and causes the module to exit cleanly if no credential pairs remain to be tested by this task.&lt;br /&gt;
&lt;br /&gt;
=== Complete Source to hydra-smtpauth.c ===&lt;br /&gt;
&lt;br /&gt;
The complete source to the SMTP authentication module as described earlier in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#Implementing SMTP-AUTH in Hydra|Section 3.1.4]] is contained in the ''src/hydra-smtpauth.c'' file in the Hydra distribution in Versions 4.2 and above.&lt;br /&gt;
&lt;br /&gt;
=== Quick Reference to Hydra Functions ===&lt;br /&gt;
&lt;br /&gt;
Although the SMTP authentication module highlighted most of the functionality Hydra supplies for use in modules, we have not yet covered all of Hydra's functionality. Because developer documentation of the functions is not available for Hydra modules, this section provides a quick reference to the Hydra functions available as of Version 4.4.&lt;br /&gt;
&lt;br /&gt;
In addition to the functions described next, Hydra also contains files for supporting the MD4 and DES algorithms. These files are not part of the Hydra module structure, and as such are not covered here.&lt;br /&gt;
&lt;br /&gt;
====''void hydra_child_exit(int code)''====&lt;br /&gt;
&lt;br /&gt;
Exits the child task while signaling the exit status to Hydra&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
Valid values for &amp;lt;tt&amp;gt;code&amp;lt;/tt&amp;gt; are shown in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-4|Table 3-4]]. Supply the value for &amp;lt;tt&amp;gt;code&amp;lt;/tt&amp;gt; as &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; for normal exit, &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt; for no connection possible, and &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt; for protocol or service error.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''void hydra_register_socket(int sock)''====&lt;br /&gt;
&lt;br /&gt;
Registers the internal socket passed in by Hydra&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;hydra_register_socket( )&amp;lt;/tt&amp;gt; should be called with the &amp;lt;tt&amp;gt;sp&amp;lt;/tt&amp;gt; variable passed into the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''char *hydra_get_next_pair( )''====&lt;br /&gt;
&lt;br /&gt;
Fetches the next pair of credentials for testing to an internal Hydra variable&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;hydra_get_next_pair( )&amp;lt;/tt&amp;gt; function returns a pointer to the next credential pair with the pair formatted as &amp;lt;tt&amp;gt;login\0password&amp;lt;/tt&amp;gt;. These can then be fetched cleanly using &amp;lt;tt&amp;gt;hydra_get_next_login( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;hydra_get_next_password( )&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;hydra_get_next_pair( )&amp;lt;/tt&amp;gt; function returns &amp;lt;tt&amp;gt;HYDRA_EXIT&amp;lt;/tt&amp;gt; on failure, and &amp;lt;tt&amp;gt;HYDRA_EMPTY&amp;lt;/tt&amp;gt; where no value was supplied (for example, when testing for blank passwords).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''char *hydra_get_next_login( )''====&lt;br /&gt;
&lt;br /&gt;
Fetches the next login (username) string&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function returns a pointer to the login value fetched by the &amp;lt;tt&amp;gt;hydra_get_next_pair( )&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''char *hydra_get_next_password( )''====&lt;br /&gt;
&lt;br /&gt;
Fetches the next password string&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function returns a pointer to the password value fetched by the &amp;lt;tt&amp;gt;hydra_get_next_pair( )&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''void hydra_completed_pair( )''====&lt;br /&gt;
&lt;br /&gt;
Updates the status of the current pair to Hydra as not valid to the internal socket&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This is run when the current pair does not appear to be a valid login/password combination on the service being tested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''void hydra_completed_pair_found( )''====&lt;br /&gt;
&lt;br /&gt;
Updates Hydra with the status that the current pair is valid to the internal socket&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This is run when the current pair has been found to be a valid login/password combination on the service being tested.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''void hydra_report_found(int port, char *svc, FILE *fp)''====&lt;br /&gt;
&lt;br /&gt;
Used to supply the credentials found for display&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function is used to output the found credentials to the user. &amp;lt;tt&amp;gt;port&amp;lt;/tt&amp;gt; is the port the service was tested on, &amp;lt;tt&amp;gt;svc&amp;lt;/tt&amp;gt; is the name of the service (commonly a literal string such as &amp;lt;tt&amp;gt;smtpauth&amp;lt;/tt&amp;gt;), and &amp;lt;tt&amp;gt;fp&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;fp&amp;lt;/tt&amp;gt; value Hydra supplied to the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''void hydra_report_found_host (int port, unsigned int ip, char *svc, FILE *fp)''====&lt;br /&gt;
&lt;br /&gt;
Used to supply the credentials found for display, including the host IP address&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function is similar to &amp;lt;tt&amp;gt;hydra_report_found( )&amp;lt;/tt&amp;gt;, except the IP address of the server tested is displayed. It is used to output the found credentials to the user. &amp;lt;tt&amp;gt;port&amp;lt;/tt&amp;gt; is the port the service was tested on, &amp;lt;tt&amp;gt;ip&amp;lt;/tt&amp;gt; is the IP address, &amp;lt;tt&amp;gt;svc&amp;lt;/tt&amp;gt; is the name of the service (commonly a literal string such as &amp;lt;tt&amp;gt;smtpauth&amp;lt;/tt&amp;gt;), and &amp;lt;tt&amp;gt;fp&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;fp&amp;lt;/tt&amp;gt; value Hydra supplied to the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''void hydra_report_found_host_msg (int port, unsigned int ip, char *svc, FILE *fp, char *msg)''====&lt;br /&gt;
&lt;br /&gt;
Used to supply the credentials found for display, including the host IP address and a message to be displayed to the user&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function is similar to &amp;lt;tt&amp;gt;hydra_report_found_host()&amp;lt;/tt&amp;gt;, with the addition of a message to be displayed. It is used to output the found credentials to the user. &amp;lt;tt&amp;gt;port&amp;lt;/tt&amp;gt; is the port the service was tested on, &amp;lt;tt&amp;gt;ip&amp;lt;/tt&amp;gt; is the IP address, &amp;lt;tt&amp;gt;svc&amp;lt;/tt&amp;gt; is the name of the service (commonly a literal string such as &amp;lt;tt&amp;gt;smtpauth&amp;lt;/tt&amp;gt;), &amp;lt;tt&amp;gt;fp&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;fp&amp;lt;/tt&amp;gt; value Hydra supplied to the module, and &amp;lt;tt&amp;gt;msg&amp;lt;/tt&amp;gt; is a message to be displayed to the user.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int hydra_connect_tcp(unsigned long int host, int port)''====&lt;br /&gt;
&lt;br /&gt;
Used to make a connection to a service using TCP&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function makes a connection to the host defined by the IP address &amp;lt;tt&amp;gt;host&amp;lt;/tt&amp;gt;, on port &amp;lt;tt&amp;gt;port&amp;lt;/tt&amp;gt;, using TCP. &amp;lt;tt&amp;gt;host&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;ip&amp;lt;/tt&amp;gt; value passed into the module, and the &amp;lt;tt&amp;gt;port&amp;lt;/tt&amp;gt; value usually is a standard port for the service; however, it also can be user-defined. The function returns a socket value used in sending and receiving operations, or &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt; on error.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int hydra_connect_ssl(unsigned long int host, int port)''====&lt;br /&gt;
&lt;br /&gt;
Used to make a connection to a service using SSL.&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function makes a connection to the host defined by the IP address &amp;lt;tt&amp;gt;host&amp;lt;/tt&amp;gt;, on port &amp;lt;tt&amp;gt;port&amp;lt;/tt&amp;gt;, using SSL. &amp;lt;tt&amp;gt;host&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;ip&amp;lt;/tt&amp;gt; value passed into the module, and the &amp;lt;tt&amp;gt;port&amp;lt;/tt&amp;gt; value is either the standard SSL port for the service, or user-defined. The function returns a socket value used in sending and receiving operations, or &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt; on error.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int hydra_connect_udp(unsigned long int host, int port)''====&lt;br /&gt;
&lt;br /&gt;
Used to make a connection to a service using UDP&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function sets up a socket for communicating to the host defined by the IP address &amp;lt;tt&amp;gt;host&amp;lt;/tt&amp;gt; on port &amp;lt;tt&amp;gt;port&amp;lt;/tt&amp;gt;, using UDP. &amp;lt;tt&amp;gt;host&amp;lt;/tt&amp;gt; is the &amp;lt;tt&amp;gt;ip&amp;lt;/tt&amp;gt; value passed into the module, and the &amp;lt;tt&amp;gt;port&amp;lt;/tt&amp;gt; value is either the standard port for the service, or user-defined. The function returns a socket value used in sending and receiving operations, or &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt; on error.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int hydra_disconnect(int socket)''====&lt;br /&gt;
&lt;br /&gt;
Disconnects a socket opened by one of the Hydra connection functions&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function closes the socket supplied and returns &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int hydra_data_ready_writing_timed(int socket, long sec, long usec)''====&lt;br /&gt;
&lt;br /&gt;
Checks whether the socket is ready to have data written to it&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function waits up to &amp;lt;tt&amp;gt;sec&amp;lt;/tt&amp;gt; seconds and &amp;lt;tt&amp;gt;usec&amp;lt;/tt&amp;gt; microseconds to see if the socket &amp;lt;tt&amp;gt;socket&amp;lt;/tt&amp;gt; is available for writing. This function returns a value greater than zero if the socket is ready for writing, &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; if the socket is not ready for writing, and &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt; on error.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int hydra_data_ready_writing(int socket)''====&lt;br /&gt;
&lt;br /&gt;
Checks whether the socket is ready to have data written to it&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function calls &amp;lt;tt&amp;gt;hydra_data_ready_writing_timed()&amp;lt;/tt&amp;gt; to see if the socket &amp;lt;tt&amp;gt;socket&amp;lt;/tt&amp;gt; is available for writing. This function returns a value greater than zero if the socket is ready for writing, &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; if the socket is not ready for writing, and &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt; on error.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int hydra_data_ready_timed(int socket, long sec, long usec)''====&lt;br /&gt;
&lt;br /&gt;
Checks whether the socket has data ready to be read&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function waits up to &amp;lt;tt&amp;gt;sec&amp;lt;/tt&amp;gt; seconds and &amp;lt;tt&amp;gt;usec&amp;lt;/tt&amp;gt; microseconds to see if the socket &amp;lt;tt&amp;gt;socket&amp;lt;/tt&amp;gt; has data available for reading. This function returns a value greater than zero if the socket has data for reading, &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; if no data is available, and &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt; on error.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int hydra_data_ready(int socket)''====&lt;br /&gt;
&lt;br /&gt;
Checks whether the socket has data ready to be read&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function calls &amp;lt;tt&amp;gt;hydra_data_ready_timed( )&amp;lt;/tt&amp;gt; to see if the socket &amp;lt;tt&amp;gt;socket&amp;lt;/tt&amp;gt; has data to be read. This function returns a value greater than zero if the socket has data for reading, &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; if no data is available, and &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt; on error.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int hydra_recv(int socket, char *buf, int length)''====&lt;br /&gt;
&lt;br /&gt;
Receives data from the supplied socket&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function reads up to &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; data from the socket &amp;lt;tt&amp;gt;socket&amp;lt;/tt&amp;gt; into the buffer &amp;lt;tt&amp;gt;buf&amp;lt;/tt&amp;gt;. The function returns the amount of data read, or &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt; on error. No translation of any type is done to the data received. This function should be used for binary protocols, as &amp;lt;tt&amp;gt;hydra_receive_line( )&amp;lt;/tt&amp;gt; performs some translation on data read.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''char *hydra_receive_line(int socket)''====&lt;br /&gt;
&lt;br /&gt;
Receives data in a line-oriented mode from the supplied socket&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function attempts to read all data available from the socket &amp;lt;tt&amp;gt;socket&amp;lt;/tt&amp;gt;. It returns a pointer to a buffer which is allocated within the function. These buffers should be deallocated using a &amp;lt;tt&amp;gt;free( )&amp;lt;/tt&amp;gt; call after use to conserve memory usage. All NULL characters in the data received are translated into space characters (0x20).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int hydra_send(int socket, char *buf, int size, int options)''====&lt;br /&gt;
&lt;br /&gt;
Sends the supplied data on the supplied socket&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function sends the data in the buffer &amp;lt;tt&amp;gt;buf&amp;lt;/tt&amp;gt;, of length &amp;lt;tt&amp;gt;size&amp;lt;/tt&amp;gt;, out on the socket defined by &amp;lt;tt&amp;gt;socket&amp;lt;/tt&amp;gt;. The &amp;lt;tt&amp;gt;options&amp;lt;/tt&amp;gt; variable is not commonly used (it is set to &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;), but is the &amp;lt;tt&amp;gt;flags&amp;lt;/tt&amp;gt; variable for the underlying socket's API &amp;lt;tt&amp;gt;send( )&amp;lt;/tt&amp;gt; command. This function returns the amount of data sent, or &amp;lt;tt&amp;gt;-1&amp;lt;/tt&amp;gt; on error.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''int make_to_lower(char *buf)''====&lt;br /&gt;
&lt;br /&gt;
Converts the supplied buffer to lowercase&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function converts the buffer pointed to by &amp;lt;tt&amp;gt;buf&amp;lt;/tt&amp;gt; to lowercase. The function always returns &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''unsigned char hydra_conv64(unsigned char in)''====&lt;br /&gt;
&lt;br /&gt;
Converts a single character to Base64 encoding&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function returns the Base64-encoded representation of the character supplied to the function in the &amp;lt;tt&amp;gt;in&amp;lt;/tt&amp;gt; parameter, or &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; on error.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''void hydra_tobase64(unsigned char *buf)''====&lt;br /&gt;
&lt;br /&gt;
Converts a string to Base64 encoding&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function converts the string pointed to by &amp;lt;tt&amp;gt;buf&amp;lt;/tt&amp;gt; to Base64 encoding. If an error occurs during encoding, the value pointed to by &amp;lt;tt&amp;gt;buf&amp;lt;/tt&amp;gt; is in an undefined state.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====''void hydra_dump_asciihex(unsigned char *string, int length)''====&lt;br /&gt;
&lt;br /&gt;
Prints a hex and ASCII dump&lt;br /&gt;
&lt;br /&gt;
=====Synopsis=====&lt;br /&gt;
&lt;br /&gt;
This function takes the data in &amp;lt;tt&amp;gt;string&amp;lt;/tt&amp;gt;, of length &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt;, and prints a hex and ASCII table to standard output. This can be very useful for debugging a module under development .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Adding Service Signatures to Nmap ==&lt;br /&gt;
&lt;br /&gt;
Recent versions of the popular port scanner Nmap can detect the type and version of services running on a network, as illustrated in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-EX-2|Example 3-2]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-EX-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-2. Example Nmap version scan'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&amp;gt;nmap -sV 127.0.0.1  &lt;br /&gt;
&lt;br /&gt;
Starting nmap 3.50 ( http://www.insecure.org/nmap/ ) at 2003-07-05 17:12 EDT&lt;br /&gt;
Interesting ports on localhost (127.0.0.1):&lt;br /&gt;
(The 1658 ports scanned but not shown below are in state: closed)&lt;br /&gt;
PORT   STATE SERVICE VERSION&lt;br /&gt;
22/tcp open  ssh     OpenSSH 3.8.1p1 (protocol 2.0)&lt;br /&gt;
&lt;br /&gt;
Nmap run completed -- 1 IP address (1 host up) scanned in 1.104 seconds&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This scan is implemented as a series of probes and responses in the file ''nmap-service-probes'' . This file defines the probes that will be sent to the service to elicit some response, as well as a series of regular expressions against which to match responses to determine which services are running and, where possible, their versions.&lt;br /&gt;
&lt;br /&gt;
At a high level, the version-scanning methodology follows this process:&lt;br /&gt;
&lt;br /&gt;
* If the port is a TCP port, connect to it and listen. This is called the NULL probe. Many services will return a banner on connection. If a match is made, processing stops.&lt;br /&gt;
* If no match is given, or if the protocol is UDP, probes defined in the ''nmap-service-probes''file will be attempted if the protocol and the port ranges in the file match. If a response matching a probe is found, processing stops. If a soft match occurs (whereby a service is recognized, but not its type or version), follow-on probes will be limited to relevant ones.&lt;br /&gt;
* If no match is found, each probe in the ''nmap-service-probes'' file will be tried, regardless of the ports on which the service usually runs. This will be limited where a soft match has already occurred.&lt;br /&gt;
* If SSL was found, Nmap will connect using SSL (if available) to run the version-detection process again.&lt;br /&gt;
&lt;br /&gt;
If a service responds to a probe sent during this process, but Nmap does not recognize the response, Nmap prints a fingerprint for the service that you can use to report the signature to the Nmap developers, as shown in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-EX-3|Example 3-3]]. You can use this, together with the version and service information, to include a signature that recognizes this service in the ''nmap-service-probes'' file in the future.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-EX-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 3-3. Nmap unrecognized service'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&amp;gt;nmap -sV -p 4738 127.0.0.1&lt;br /&gt;
&lt;br /&gt;
Starting nmap 3.50 ( http://www.insecure.org/nmap/ ) at 2003-07-05 17:39 EDT&lt;br /&gt;
Interesting ports on localhost (127.0.0.1):&lt;br /&gt;
PORT     STATE SERVICE VERSION&lt;br /&gt;
4738/tcp open  unknown&lt;br /&gt;
1 service unrecognized despite returning data. If you know the service/version, &lt;br /&gt;
please submit the following fingerprint at http://www.insecure.org/cgi-bin/&lt;br /&gt;
servicefp-submit.cgi :&lt;br /&gt;
SF-Port4738-TCP:V=3.50%D=7/5%Time=40E9CA80%P=i686-pc-linux-gnu%r(NULL,59,&amp;quot;&lt;br /&gt;
SF:Login\x20with\x20USER\x20&amp;lt;name&amp;gt;\x20followed\x20by\x20PASS\x20&amp;lt;password&amp;gt;&lt;br /&gt;
SF:\x20or\x20ANON\r\nCheck\x20privileges\x20with\x20PRIVS\r\n&amp;quot;)%r(GenericL&lt;br /&gt;
SF:ines,59,&amp;quot;Login\x20with\x20USER\x20&amp;lt;name&amp;gt;\x20followed\x20by\x20PASS\x20&amp;lt;&lt;br /&gt;
SF:password&amp;gt;\x20or\x20ANON\r\nCheck\x20privileges\x20with\x20PRIVS\r\n&amp;quot;)%r&lt;br /&gt;
SF:(GetRequest,59,&amp;quot;Login\x20with\x20USER\x20&amp;lt;name&amp;gt;\x20followed\x20by\x20PA&lt;br /&gt;
SF:SS\x20&amp;lt;password&amp;gt;\x20or\x20ANON\r\nCheck\x20privileges\x20with\x20PRIVS\&lt;br /&gt;
SF:r\n&amp;quot;)%r(HTTPOptions,59,&amp;quot;Login\x20with\x20USER\x20&amp;lt;name&amp;gt;\x20followed\x20 &lt;br /&gt;
SF:by\x20PASS\x20&amp;lt;password&amp;gt;\x20or\x20ANON\r\nCheck\x20privileges\x20with\x&lt;br /&gt;
&amp;lt;cut&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Nmap run completed -- 1 IP address (1 host up) scanned in 75.504 seconds&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At this point we have several options:&lt;br /&gt;
&lt;br /&gt;
* Submit the signature to the URL provided and wait for the next version of Nmap. If responses were received from the probes sent, and the service is something that could be expected to be running on someone else's environment, this might be the best choice.&lt;br /&gt;
* Create a working match and/or probe statement, and submit that to Fyodor at ''fyodor@insecure.org''. For services that require a custom probe and can be expected to be found in another environment, this might be the best choice.&lt;br /&gt;
* Create a working match and/or probe statement for your own use. You might choose this option if your environment contains custom-written software running proprietary services or protocols. In this case it is necessary to know how to write the probes and matches to detect these proprietary services running on the environment being tested.&lt;br /&gt;
&lt;br /&gt;
Regardless of which option you choose, it is very useful to know how to write your own probe and match signatures.&lt;br /&gt;
&lt;br /&gt;
=== The nmap-service-probes File ===&lt;br /&gt;
&lt;br /&gt;
The keywords contained in the ''nmap-service-probes'' file are listed in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-5|Table 3-5]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-TABLE-5&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-5. nmap-service-probes keywords'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Keyword !! Format&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 Probe&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 Probe &amp;lt;protocol&amp;gt; &amp;lt;probe name&amp;gt; &amp;lt;probe string&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 match&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 match &amp;lt;service&amp;gt; &amp;lt;pattern&amp;gt; [version info]&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 softmatch&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 softmatch &amp;lt;service&amp;gt; &amp;lt;pattern&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 ports&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 ports &amp;lt;portlist&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 sslports&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 sslports &amp;lt;portlist&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 Totalwaitms&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
 totalwaitms &amp;lt;milliseconds&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Probes ====&lt;br /&gt;
&lt;br /&gt;
A probe entry consists of the values shown in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-6|Table 3-6]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-TABLE-6&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-6. Probe values'''&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;
| Protocol || TCP or UDP.&lt;br /&gt;
|-&lt;br /&gt;
| Probe name || Name of the probe (human-readable).&lt;br /&gt;
|-&lt;br /&gt;
| Probe string || String starting with a &amp;lt;tt&amp;gt;q&amp;lt;/tt&amp;gt;, then a delimiter that will start and end the string sent. The string can consist of printable characters, as well as quoted unprintable characters and control characters in standard C or Perl notation.&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here are some example probe strings:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;Probe TCP NULL q||&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Send nothing, waiting the amount of time specified in &amp;lt;tt&amp;gt;totalwaitms&amp;lt;/tt&amp;gt;.&lt;br /&gt;
;&amp;lt;tt&amp;gt;Probe TCP GenericLines q|\r\n\r\n|&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Send carriage return, newline, carriage return, newline.&lt;br /&gt;
;&amp;lt;tt&amp;gt;Probe UDP DNSStatusRequest q|\0\0\x10\0\0\0\0\0\0\0\0\0|&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Send the binary string &amp;lt;tt&amp;gt;0x00&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;0x00&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;0x10&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;0x00&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;0x00&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;0x00&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;0x00&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;0x00&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;0x00&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;0x00&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;0x00 0x00&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Matches ====&lt;br /&gt;
&lt;br /&gt;
A match entry consists of the values defined in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-7|Table 3-7]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-TABLE-7&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-7. Match values'''&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;
| Service || Name of the service the pattern matches.&lt;br /&gt;
|-&lt;br /&gt;
| Pattern || A Perl-compatible regular expression to match the expected response for this service. This is of the format &amp;lt;tt&amp;gt;m/regex/opts&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|-&lt;br /&gt;
| Version info || A field specifying additional version information. This is of the format &amp;lt;tt&amp;gt;v/product&amp;lt;/tt&amp;gt;&amp;lt;tt&amp;gt;name/version/info/&amp;lt;/tt&amp;gt;. This can contain variables matched from the matching pattern, such as &amp;lt;tt&amp;gt;$1&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;$2&amp;lt;/tt&amp;gt;, where the matching pattern contains &amp;lt;tt&amp;gt;( )&amp;lt;/tt&amp;gt; matches. Any or all entries can be empty.&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Nmap uses the Perl Compatible Regular Expressions (''libpcre'') library for evaluating regular expressions. Perl regular expressions are documented at ''http://www.perldoc.com/perl5.8.0/pod/perlre.html''.&lt;br /&gt;
&lt;br /&gt;
Here are some example match strings:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;match ssh m/^SSH-([.\d]+)-OpenSSH[_-](\S+)/ v/OpenSSH/$2/protocol $1/&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Match strings such as &amp;lt;tt&amp;gt;SSH-1.5-OpenSSH-3.4p1&amp;lt;/tt&amp;gt;, reading the version string (3.4p1) and protocol (1.5) into the &amp;lt;tt&amp;gt;$2&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;$1&amp;lt;/tt&amp;gt; variables, respectively.&lt;br /&gt;
;&amp;lt;tt&amp;gt;match ftp m/^220[- ].*FTP server \(Version&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;(wu-[-.\w]+)/s v/WU-FTPD/$1//&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Match strings such as &amp;lt;tt&amp;gt;220&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;FTP&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;(Version&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;wu-2.6.0)&amp;lt;/tt&amp;gt; and extract the version wu-2.6.0.&lt;br /&gt;
;&amp;lt;tt&amp;gt;match mysql m/^.\0\0\0\n(4\.[-.\w]+)\0...\0/s v/MySQL/$1//&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Match the version of MySQL 4.x from the binary response.&lt;br /&gt;
&lt;br /&gt;
==== Soft matches ====&lt;br /&gt;
&lt;br /&gt;
A ''soft match'' occurs when a service can be identified, but no additional information can be derived. A soft-match entry consists of the values defined in [[Network Security Tools/Modifying and Hacking Security Tools/Extending Hydra and Nmap#networkst-CHP-3-TABLE-8|Table 3-8]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;networkst-CHP-3-TABLE-8&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 3-8. Soft-match values'''&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;
| Service || Name of the service the pattern matches.&lt;br /&gt;
|-&lt;br /&gt;
| Pattern || A Perl-compatible regular expression to match the expected response for this service. This is of the format &amp;lt;tt&amp;gt;m/regex/opts&amp;lt;/tt&amp;gt;.&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here are some example soft-match strings:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;tt&amp;gt;softmatch ftp m/^220[- ].*ftp server.*\r\n/i&amp;lt;/tt&amp;gt;&lt;br /&gt;
* &amp;lt;tt&amp;gt;softmatch imap m/^\* OK [-.\w,:+ ]+imap[-.\w,:+ ]+\r\n$/i&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== ports ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;ports&amp;lt;/tt&amp;gt; is a comma-separated list of ports, as well as port ranges (e.g., 35067-35090) on which the service will commonly run. This is used to ensure that probing is done efficiently, and therefore the &amp;lt;tt&amp;gt;ports&amp;lt;/tt&amp;gt; entry should follow the &amp;lt;tt&amp;gt;Probe&amp;lt;/tt&amp;gt; entry in &amp;lt;tt&amp;gt;nmap-service-probes&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== sslports ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;sslports&amp;lt;/tt&amp;gt; is a comma-separated list of ports, as well as port ranges (e.g., 55522-55525) on which the service will commonly run over SSL. This is used to ensure that probing is done efficiently, and therefore the &amp;lt;tt&amp;gt;sslports&amp;lt;/tt&amp;gt; entry should follow the &amp;lt;tt&amp;gt;Probe&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;ports&amp;lt;/tt&amp;gt; entries in &amp;lt;tt&amp;gt;nmap-service-probes&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== totalwaitms ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;totalwaitms&amp;lt;/tt&amp;gt; is used to specify the timeout for a &amp;lt;tt&amp;gt;Probe&amp;lt;/tt&amp;gt;. It is not needed unless the service you are probing does not respond immediately. If it is used, it should follow the &amp;lt;tt&amp;gt;Probe&amp;lt;/tt&amp;gt; entry.&lt;/div&gt;</description>
			<pubDate>Tue, 11 Mar 2008 21:39:00 GMT</pubDate>			<dc:creator>Docbook2Wiki</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:Network_Security_Tools/Modifying_and_Hacking_Security_Tools/Extending_Hydra_and_Nmap</comments>		</item>
	</channel>
</rss>