Library comm
Common communication functions for network discovery tasks like banner grabbing and data exchange.
The functions in this module return values appropriate for use with
exception handling via nmap.new_try.
These functions may be passed a table of options, but it's not required. The
keys for the options table are "bytes", "lines",
"proto", and "timeout". "bytes" sets
a minimum number of bytes to read. "lines" does the same for
lines. "proto" sets the protocol to communicate with,
defaulting to "tcp" if not provided. "timeout"
sets the socket timeout (see the socket function set_timeout
for details).
If both "bytes" and "lines" are provided,
"lines" takes precedence. If neither are given, the functions
read as many bytes as possible.
Author:
| Kris Katterjohn 04/2008 |
Copyright© Same as Nmap--See http://nmap.org/book/man-legal.html
Source: http://nmap.org/svn/nselib/comm.lua
Functions
| bestoption (port) |
This function returns best protocol order for trying to open a conenction based on port and service information |
| exchange (host, port, data, opts) |
This function connects to the specified port number on the specified host, sends data, then waits for and returns the response, if any. |
| get_banner (host, port, opts) |
This function simply connects to the specified port number on the specified host and returns any data received. |
| is_ssl (port_number) |
This function just checks if the provided port number is on a list of ports that usually provide services with ssl |
| opencon (host, port, protocol, data, opts) |
This function opens a connection, sends the first data payload and check if a response is correctly received (what means that the protocol used is fine) |
| tryssl (host, port, data, opts) |
This function tries to open a connection based on the best option about which is the correct protocol |
Functions
- bestoption (port)
-
This function returns best protocol order for trying to open a conenction based on port and service information
The first value is the best option, the second is the worst
Parameters
- port: The port table
Return values:
- Best option ("tcp" or "ssl")
- Worst option ("tcp" or "ssl")
- exchange (host, port, data, opts)
-
This function connects to the specified port number on the specified host, sends data, then waits for and returns the response, if any.
The first return value is true to signal success or false to signal failure. On success the second return value is the response from the remote host. On failure the second return value is an error message.
Parameters
- host: The host to connect to.
- port: The port on the host.
- data: The data to send initially.
- opts: The options. See the module description.
Return values:
- Status (true or false).
- Data (if status is true) or error string (if status is false).
- get_banner (host, port, opts)
-
This function simply connects to the specified port number on the specified host and returns any data received.
The first return value is true to signal success or false to signal failure. On success the second return value is the response from the remote host. On failure the second return value is an error message.
Parameters
- host: The host to connect to.
- port: The port on the host.
- opts: The options. See the module description.
Return values:
- Status (true or false).
- Data (if status is true) or error string (if status is false).
- is_ssl (port_number)
-
This function just checks if the provided port number is on a list of ports that usually provide services with ssl
Parameters
- port_number: The number of the port to check
Return value:
bool True if port is usually ssl, otherwise false - opencon (host, port, protocol, data, opts)
-
This function opens a connection, sends the first data payload and check if a response is correctly received (what means that the protocol used is fine)
Possible options: timeout: generic timeout value connect_timeout: especific timeout for connection request_timeout: especific timeout for requests recv_before: receive data before sending first payload
Default timeout is set to 8000.
Parameters
- host: The destination host IP
- port: The destination host port
- protocol: The protocol for the connection
- data: The first data payload of the connection
- opts:
Return values:
- sd The socket descriptor, nil if no connection is estabilished
- response The response received for the payload
- early_resp If opt recv_before is true, returns the value of the first receive (before sending data)
- tryssl (host, port, data, opts)
-
This function tries to open a connection based on the best option about which is the correct protocol
If the best option fails, the function tries the other option
This function allows writing nse scripts in a way that the API will take care of ssl issues, making failure detection transparent to the programmer
Parameters
- host: The host table
- port: The port table
- data: The first data payload of the connection
- opts: Options, such as timeout
Return values:
- sd The socket descriptor
- response The response received for the payload
- correctOpt Correct option for connection guess
- earlyResp If opt recv_before is true, returns the value of the first receive (before sending data)




