From 14fc84bd4978691b8f993d51de55beb4eb6f3d77 Mon Sep 17 00:00:00 2001 From: david Date: Tue, 4 Nov 2008 22:47:17 +0000 Subject: [PATCH] Update finger.nse in scripting.xml to match the latest version of the script. Also take the example script out of a sect2 and put it directly in the enclosing sect1 (the sect1 was empty except for the sect2). --- docs/scripting.xml | 82 ++++++++++++---------------------------------- scripts/finger.nse | 3 +- 2 files changed, 23 insertions(+), 62 deletions(-) diff --git a/docs/scripting.xml b/docs/scripting.xml index 52a29c7d5..bd7894b22 100644 --- a/docs/scripting.xml +++ b/docs/scripting.xml @@ -2082,10 +2082,8 @@ end Example Script - - - Finger-Test Script Finger Results” script + The finger script (finger.nse) is a perfect example of how short typical NSE scripts are. @@ -2095,9 +2093,10 @@ end printed in Nmap's output. A detailed description of what the script actually does should go in the description field. -id="Finger Results"id” script variable - -description="attempts to get a list of usernames via the finger service"description” script variable +id = "Finger Results"id” script variable +description = [[ +Attempts to get a list of usernames via the finger service. +]]description” script variable author = "Eddie Bell <ejlbell@gmail.com>"Bell, Eddieauthor” script variable @@ -2109,14 +2108,15 @@ containing all the categories the script belongs to—These are used for script selection through the option. -categories = {"discovery"} +categories = {"default", "discovery"} You can use the facilities provided by the nselib () with require. Here -we want to use shorter port rules. +we want to use common communication functions and shorter port rules. +require "comm" require "shortport" @@ -2135,65 +2135,25 @@ expect it, should the version detection information not be available. portrule = shortport.port_or_service(79, "finger")portrule” script variable - -action = function(host, port)action” script variable - local socket = nmap.new_socket() - local results = "" - local status = true -The function err_catch() will be called for -clean up, through NSE's exception handling mechanism. Here it only -closes the previously opened socket (which should be enough in most -cases). +First, the script uses nmap.new_try() to +create an exception handler that will quit the script in case of an +error. Next, it passes control to comm.exchange(), +which handles the network transaction. Here we have asked to receive no +more than around 100 lines, with a timeout of five seconds +(5000 ms). Any errors will be handled by the +try exception handler. The script returns a string +if the call to comm.exchange() was successful. -local err_catch = function() - socket:close() +action = function(host, port) + local try = nmap.new_try() + + return try(comm.exchange(host, port, "\r\n", + {lines=100, proto=port.protocol, timeout=5000})) end - -The clean up function gets registered for exception handling via -a call to nmap.new_try() - - - local try = nmap.new_try(err_catch()) - - -The script sets a timeout of 5000 (five seconds). -Should any operation require more time we'll receive a -TIMEOUT error message. - - - socket:set_timeout(5000) - - -To make use of the exception handling we need to wrap calls to those functions which might return an error, inside try() - - - try(socket:connect(host.ip, port.number, port.protocol)) - try(socket:send("\n\r")) - - -The call to receive_lines() is not wrapped -in try(), because we don't want to abort the script -just because we didn't receive the data we expected. Note that if -there is less data than requested (100 lines), we will still receive -it and the status will be true—subsequent -calls would yield a false status. - - - status, results = socket:receive_lines(100) - socket:close() - - -The script returns a string if the call to receive_lines() was successful, otherwise it returns nil. - - - return results - end - - diff --git a/scripts/finger.nse b/scripts/finger.nse index 1107c7098..793b35c7b 100644 --- a/scripts/finger.nse +++ b/scripts/finger.nse @@ -17,5 +17,6 @@ portrule = shortport.port_or_service(79, "finger") action = function(host, port) local try = nmap.new_try() - return try(comm.exchange(host, port, "\r\n", {lines=100, proto=port.protocol, timeout=5000})) + return try(comm.exchange(host, port, "\r\n", + {lines=100, proto=port.protocol, timeout=5000})) end