PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/nselib/nmap.luadoc

https://gitlab.com/g10h4ck/nmap-gsoc2015
Unknown | 825 lines | 776 code | 49 blank | 0 comment | 0 complexity | bbc397819ac41ca89136fa39db3c7894 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-2.0, LGPL-2.1, MIT
  1. ---
  2. -- Interface with Nmap internals.
  3. --
  4. -- The <code>nmap</code> module is an interface with Nmap's internal functions
  5. -- and data structures. The API provides target host details such as port
  6. -- states and version detection results. It also offers an interface to the
  7. -- Nsock library for efficient network I/O.
  8. -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html
  9. module "nmap"
  10. --- Returns the debugging level as a non-negative integer.
  11. --
  12. -- The debugging level can be set with the <code>-d</code> option.
  13. -- @return The debugging level.
  14. -- @usage if nmap.debugging() > 0 then ... end
  15. function debugging()
  16. --- Determines whether Nmap was compiled with SSL support.
  17. --
  18. -- This can be used to avoid sending SSL probes when SSL is not available.
  19. -- @return True if Nmap was compiled with SSL support, false otherwise.
  20. function have_ssl()
  21. --- Returns the version intensity as a non-negative integer.
  22. --
  23. -- The version intensity can be set for all version probes with the
  24. -- <code>--version-intensity</code> option. The intensity for version scripts
  25. -- can be overridden with the <code>script-intensity</code> script argument.
  26. -- If overridden, nmap.version_intensity() returns the overridden value
  27. -- automatically. If neither <code>--version-intensity</code> nor the script
  28. -- argument <code>script-intensity</code> are used, the version intensity
  29. -- defaults to 7.
  30. -- When a version script is given by name with the <code>--script</code>
  31. -- option, as opposed to being selected automatically due to <code>-sV</code>,
  32. -- its version intensity is automatically set to maximum (9).
  33. -- @return The version intensity.
  34. -- @usage
  35. -- portrule = function(host, port)
  36. -- return ...
  37. -- ...
  38. -- and nmap.version_intensity() >= 7
  39. -- end
  40. function version_intensity()
  41. --- Returns the verbosity level as a non-negative integer.
  42. --
  43. -- The verbosity level can be set with the <code>-v</code> option. When
  44. -- a script is given by name with the <code>--script</code> option, as
  45. -- opposed to being selected by default or by category, its verbosity
  46. -- level is automatically increased by one.
  47. -- @return The verbosity level.
  48. -- @usage if nmap.verbosity() > 0 then ... end
  49. function verbosity()
  50. --- Returns whether a script should be able to perform privileged operations
  51. --
  52. -- @return True if Nmap is running privileged, false otherwise.
  53. function is_privileged()
  54. --- Resolves the specified host name using the optional address family and
  55. -- returns a table containing all of the matching addresses.
  56. --
  57. -- If no address family is given, resolve() will return all addresses for the
  58. -- name.
  59. --
  60. -- @param host Host name to resolve
  61. -- @param family Address family string (such as "inet") to specify the type
  62. -- of addresses returned
  63. -- @see address_family
  64. -- @return Status (true or false)
  65. -- @return Table containing addresses resolved from the host name if status
  66. -- is true, or an error string if status is false
  67. -- @usage local status, t = nmap.resolve("www.kame.net", nmap.address_family())
  68. function resolve(host, family)
  69. --- Returns the address family Nmap is using.
  70. --
  71. -- For example, if Nmap is run with the -6 option, then "inet6" is returned.
  72. --
  73. -- @return The address family as a string ("inet" or "inet6")
  74. -- @usage local family = nmap.address_family()
  75. function address_family()
  76. --- Returns the interface name (dnet-style) that Nmap is using.
  77. --
  78. -- For example in the pre-scanning (<code>"prerule"</code> scripts) phase
  79. -- if Nmap is run with the <code>-e eth0</code>, then "eth0" can be
  80. -- returned, however Nmap can return an other interface name since it
  81. -- can determine the best interface suited for the job.
  82. -- Other <code>"hostrule"</code> and <code>"portrule"</code> scripts
  83. -- should use the interface field of the <code>host</code> table:
  84. -- <code>host.interface</code>.
  85. --
  86. -- The result of this function can be used to get the interface information
  87. -- table, example: <code>nmap.get_interface_info("eth0")</code>.
  88. --
  89. -- @return A string containing the interface name (dnet-style) on
  90. -- success, or a nil value on failures.
  91. -- @usage local interface_name = nmap.get_interface()
  92. function get_interface()
  93. --- Gets the interface network information.
  94. --
  95. -- This function takes a dnet-style interface name and returns a table
  96. -- containing the network information of the interface.
  97. --
  98. -- Keys of the returned table:
  99. -- * <code>device</code> The interface name, can be an interface alias.
  100. -- * <code>shortname</code> A simple short name of the device.
  101. -- * <code>netmask</code> The netmask bits (CIDR) of the interface.
  102. -- * <code>address</code> The string representing the IP address assigned to the interface.
  103. -- * <code>link</code> The string representing the hardware type of the interface. Possible values are: <code>"ethernet"</code>, <code>"loopback"</code>, <code>"p2p"</code> or <code>"other"</code>.
  104. -- * <code>mac</code> MAC address (six-byte-long binary string) of the interface if the type of the interface is <code>"ethernet"</code>, otherwise it is <code>nil</code>.
  105. -- * <code>broadcast</code> The string representing the broadcast address assigned to the interface if the interface type is <code>"ethernet"</code> and if the used address is IPv4, otherwise it is <code>nil</code>.
  106. -- * <code>up</code> The state of the interface, possible values are <code>"up"</code> or <code>"down"</code>.
  107. -- * <code>mtu</code> The MTU size of the interface.
  108. --
  109. -- @param interface_name The name of the interface.
  110. -- @return Table containing the network information of the interface on
  111. -- success, or nil and an error message on failures.
  112. -- @usage local iface, err = nmap.get_interface_info("eth0")
  113. function get_interface_info(interface_name)
  114. --- Lists network interfaces
  115. --
  116. -- This script enumerates all network interfaces and returns a list of tables
  117. -- containing information about every interface.
  118. --
  119. -- Keys of each table:
  120. -- * <code>device</code> The interface name, can be an interface alias.
  121. -- * <code>shortname</code> A simple short name of the device.
  122. -- * <code>netmask</code> The netmask bits (CIDR) of the interface.
  123. -- * <code>address</code> The string representing the IP address assigned to the interface.
  124. -- * <code>link</code> The string representing the hardware type of the interface. Possible values are: <code>"ethernet"</code>, <code>"loopback"</code>, <code>"p2p"</code> or <code>"other"</code>.
  125. -- * <code>mac</code> MAC address (six-byte-long binary string) of the interface if the type of the interface is <code>"ethernet"</code>, otherwise it is <code>nil</code>.
  126. -- * <code>broadcast</code> The string representing the broadcast address assigned to the interface if the interface type is <code>"ethernet"</code> and if the used address is IPv4, otherwise it is <code>nil</code>.
  127. -- * <code>up</code> The state of the interface, possible values are <code>"up"</code> or <code>"down"</code>.
  128. -- * <code>mtu</code> The MTU size of the interface.
  129. --
  130. -- @return Array of tables containing information about every discovered interface.
  131. -- @usage local interfaces, err = nmap.list_interfaces()
  132. function list_interfaces()
  133. --- Returns the TTL (time to live) value selected by the --ttl option
  134. --
  135. -- If there is no value specified or if the value specified with the --ttl
  136. -- option is out of the range 0 to 255 (inclusive) this function returns 64,
  137. -- which is the default TTL for an IP packet. This function would be most
  138. -- useful in crafting packets, which we want to comply with the selected
  139. -- Nmap TTL value.
  140. --
  141. -- @return A number containing the TTL value
  142. -- @usage local ttl = nmap.get_ttl()
  143. function get_ttl()
  144. --- Returns the payload data length selected with the --data-length option
  145. --
  146. -- Used when a script is crafting ICMP packets and needs to comply with the
  147. -- selected payload data length. If there is no value specified this function
  148. -- returns 0 which is the default length of the ICMP payload for Nmap.
  149. --
  150. -- @return A number containing the value of the payload length
  151. -- @usage local payload_length = nmap.get_payload_length
  152. function get_payload_length()
  153. --- Searches for the specified file relative to Nmap's search paths and returns
  154. -- a string containing its path if it is found and readable (to the process).
  155. -- Absolute paths and paths relative to the current directory will not be
  156. -- searched.
  157. --
  158. -- If the file is not found, not readable, or is a directory, <code>nil</code>
  159. -- is returned.
  160. -- @usage
  161. -- nmap.fetchfile("nmap-rpc") --> "/usr/local/share/nmap/nmap-rpc"
  162. -- @param filename Filename to search for.
  163. -- @return String representing the full path to the file or <code>nil</code>.
  164. function fetchfile(filename)
  165. --- Returns the timing level as a non-negative integer.
  166. --
  167. -- Possible return values vary from <code>0</code> to <code>5</code>,
  168. -- corresponding to the six built-in Nmap timing templates. The timing level
  169. -- can be set with the <code>-T</code> option.
  170. -- @return The timing level.
  171. function timing_level()
  172. --- Gets a port table for a port on a given host.
  173. --
  174. -- This function takes a host table and a port table and returns a port table
  175. -- for the queried port. The port table returned is similar in structure to the
  176. -- ones passed to the <code>hostrule</code>, <code>portrule</code>, and
  177. -- <code>action</code> functions. If the given port was not scanned the function
  178. -- returns <code>nil</code>.
  179. --
  180. -- You can of course reuse the host and port tables passed to a script's rule
  181. -- function. The purpose of this call is to be able to match scripts against
  182. -- more than one open port. For example if the target host has an open port 22
  183. -- and a running identd server, then you can write a script which will only fire
  184. -- if both ports are open and there is an identification server on port 113.
  185. -- While it is possible to specify IP addresses different to the currently
  186. -- scanned target, the result will only be correct if the target is in the
  187. -- currently scanned group of hosts.
  188. -- @param host Host table, containing an <code>ip</code> field.
  189. -- @param port Port table, containing <code>number</code> and
  190. -- <code>protocol</code> fields.
  191. -- @return A new port table holding the status and information for the port, or <code>nil</code>.
  192. -- @usage p = nmap.get_port_state({ip="127.0.0.1"}, {number="80", protocol="tcp"})
  193. function get_port_state(host, port)
  194. --- Iterates over port tables matching protocol and state for a given host
  195. --
  196. -- This function takes a host table, previous port table, port protocol and
  197. -- port state to return matching port tables on a host.
  198. --
  199. -- The first time you call this function, pass <code>nil</code> for the port
  200. -- parameter to get the first matching port table. From then on, pass the
  201. -- previous port table returned by this function to the port parameter for the
  202. -- next matching port table.
  203. --
  204. -- @param host Host table, containing an <code>ip</code> field
  205. -- @param port Port table, containing a <code>number</code> field; or <code>nil</code>
  206. -- for first port
  207. -- @param proto Port protocol, such as <code>"tcp"</code>
  208. -- @param state Port state, such as <code>"open"</code>
  209. -- @return Next port table for host, or <code>nil</code> when exhausted
  210. -- @usage port = nmap.get_ports(host, port, "tcp", "open")
  211. function get_ports(host, port, proto, state)
  212. --- Sets the state of a port on a given host.
  213. --
  214. -- Using this function, the final port state, reflected in Nmap's results, can
  215. -- be changed for a target. This is useful when Nmap detects a port as
  216. -- <code>open|filtered</code>, but the script successfully connects to that
  217. -- port. In this case, the script can set the port state to <code>open</code>.
  218. -- This function doesn't change the original port table passed a script's
  219. -- action function.
  220. -- @param host Host table, containing an <code>ip</code> field.
  221. -- @param port Port table, containing <code>number</code> and
  222. -- <code>protocol</code> fields.
  223. -- @param state Port state, like <code>"open"</code> or <code>"closed"</code>.
  224. function set_port_state(host, port, state)
  225. --- Sets version information on a port.
  226. --
  227. -- NSE scripts are sometimes able to determine the service name and application
  228. -- version listening on a port. A whole script category (<code>version</code>)
  229. -- was designed for this purpose. This function is used to record version
  230. -- information when it is discovered.
  231. --
  232. -- The host and port arguments to this function should either be the tables
  233. -- passed to the action method or they should have the same structure. The port
  234. -- argument specifies the port to operate on through its <code>number</code>
  235. -- and <code>protocol</code> fields. and also contains the new version
  236. -- information to set. The version detection fields this function looks at are
  237. -- <code>name</code>, <code>product</code>, <code>version</code>,
  238. -- <code>extrainfo</code>, <code>hostname</code>, <code>ostype</code>,
  239. -- <code>devicetype</code>, <code>service_tunnel</code>, and <code>cpe</code>.
  240. -- All these keys are optional.
  241. --
  242. -- The <code>probestate</code> argument describes the state in which the script
  243. -- completed. It is a string, one of: <code>"hardmatched"</code>,
  244. -- <code>"softmatched"</code>, <code>"nomatch"</code>,
  245. -- <code>"tcpwrapped"</code>, or <code>"incomplete"</code>.
  246. -- <code>"hardmatched"</code> is almost always used (and is the default),
  247. -- as it signifies a
  248. -- successful match. The other possible states are generally only used for
  249. -- standard version detection rather than the NSE enhancement.
  250. -- @param host Host table, containing an <code>ip</code> field.
  251. -- @param port Port table, containing <code>number</code> and
  252. -- <code>protocol</code> fields, as well as any additional version information
  253. -- fields.
  254. -- @param probestate The state of the probe: <code>"hardmatched"</code>,
  255. -- <code>"softmatched"</code>, <code>"nomatch"</code>,
  256. -- <code>"tcpwrapped"</code>, or <code>"incomplete"</code>.
  257. function set_port_version(host, port, probestate)
  258. --- Returns the current date and time in seconds.
  259. -- @return The number of seconds since the epoch (on most systems this is
  260. -- 01/01/1970) as a floating point value.
  261. -- @usage local now = nmap.clock()
  262. function clock()
  263. --- Returns the current date and time in milliseconds.
  264. -- @return The number of milliseconds since the epoch (on most systems this is
  265. -- 01/01/1970).
  266. -- @usage local now = nmap.clock_ms()
  267. function clock_ms()
  268. --- Create a mutex on an object.
  269. --
  270. -- This function returns another function that works as a mutex on the object
  271. -- passed. This object can be any Lua data type except <code>nil</code>,
  272. -- Booleans, and Numbers. The Mutex (the returned function) allows you to lock,
  273. -- try to lock, and release the mutex. The Mutex function takes only one
  274. -- argument, which must be one of
  275. -- * <code>"lock"</code>: makes a blocking lock on the mutex. If the mutex is busy then the thread will yield and wait. The function returns with the mutex locked.
  276. -- * <code>"trylock"</code>: makes a non-blocking lock on the mutex. If the mutex is busy then it immediately returns a false value. Otherwise, the mutex locks the mutex and returns true.
  277. -- * <code>"done"</code>: releases the mutex and allows another thread to lock it. If the thread does not have a lock on the mutex, an error will be raised.
  278. -- * <code>"running"</code>: returns the thread locked on the mutex or <code>nil</code> if no thread is locked. This should only be used for debugging as it interferes with finished threads from being collected.
  279. --
  280. -- NSE maintains a weak reference to the Mutex function so other calls to
  281. -- nmap.mutex with the same object will return the same function (Mutex);
  282. -- however, if you discard your reference to the Mutex then it may be collected
  283. -- and subsequent calls to nmap.mutex with the object will return a different
  284. -- Mutex!
  285. -- @param object Object to create a mutex for.
  286. -- @return Mutex function which takes one of the following arguments:
  287. -- <code>"lock"</code>, <code>"trylock"</code>, <code>"done"</code>, or
  288. -- <code>"running"</code>.
  289. -- @usage
  290. -- id = "My Script's Unique ID"
  291. --
  292. -- local mutex = nmap.mutex(id)
  293. -- function action(host, port)
  294. -- mutex "lock"
  295. -- -- do stuff
  296. -- mutex "done"
  297. -- return script_output
  298. -- end
  299. function mutex(object)
  300. --- Create a condition variable for an object.
  301. --
  302. -- This function returns a function that works as a Condition Variable for the
  303. -- given object parameter. The object can be any Lua data type except
  304. -- <code>nil</code>, Booleans, and Numbers. The Condition Variable (returned
  305. -- function) allows you wait, signal, and broadcast on the condition variable.
  306. -- The Condition Variable function takes only one argument, which must be one of
  307. -- * <code>"wait"</code>: Wait on the condition variable until another thread wakes us.
  308. -- * <code>"signal"</code>: Wake up a single thread from the waiting set of threads for this condition variable.
  309. -- * <code>"broadcast"</code>: Wake up all threads in the waiting set of threads for this condition variable.
  310. --
  311. -- NSE maintains a weak reference to the Condition Variable so other calls to
  312. -- nmap.condvar with the same object will return the same function (Condition
  313. -- Variable); however, if you discard your reference to the Condition
  314. -- Variable then it may be collected; and, subsequent calls to nmap.condvar with
  315. -- the object will return a different Condition Variable function!
  316. --
  317. -- In NSE, Condition Variables are typically used to coordinate with threads
  318. -- created using the stdnse.new_thread facility. The worker threads must
  319. -- wait until work is available that the master thread (the actual running
  320. -- script) will provide. Once work is created, the master thread will awaken
  321. -- one or more workers so that the work can be done.
  322. --
  323. -- It is important to check the predicate (the test to see if your worker
  324. -- thread should "wait" or not) BEFORE and AFTER the call to wait. You are
  325. -- not guaranteed spurious wakeups will not occur (that is, there is no
  326. -- guarantee your thread will not be awakened when no thread called
  327. -- <code>"signal"</code> or <code>"broadcast"</code> on the condition variable).
  328. -- One important check for your worker threads, before and after waiting,
  329. -- should be to check that the master script thread is still alive.
  330. -- (To check that the master script thread is alive, obtain the "base" thread
  331. -- using stdnse.base and use coroutine.status). You do not want your worker
  332. -- threads to continue when the script has ended for reasons unknown to your
  333. -- worker thread. You are guaranteed that all threads waiting on a
  334. -- condition variable will be awakened if any thread that has accessed
  335. -- the condition variable via <code>nmap.condvar</code> ends for any
  336. -- reason. This is essential to prevent deadlock with threads
  337. -- waiting for another thread to awaken
  338. -- them that has ended unexpectedly.
  339. -- @see stdnse.new_thread
  340. -- @see stdnse.base
  341. -- @param object Object to create a condition variable for.
  342. -- @return ConditionVariable Condition variable function.
  343. -- @usage
  344. -- local myobject = {}
  345. -- local cv = nmap.condvar(myobject)
  346. -- cv "wait" -- waits until another thread calls cv "signal"
  347. function condvar(object)
  348. --- Creates a new exception handler.
  349. --
  350. -- This function returns an exception handler function. The exception handler is
  351. -- meant to be wrapped around other function calls that may raise an exception.
  352. -- A function raises an exception by making its first return value false and its
  353. -- second return value a message describing the error. When an exception occurs,
  354. -- the exception handler optionally calls a user-provided cleanup function, then
  355. -- terminates the script. When an exception does not occur (the wrapped
  356. -- function's first return value is true), the exception handler strips off the
  357. -- first return value and returns the rest.
  358. --
  359. -- The optional cleanup function is passed as the sole argument to
  360. -- <code>new_try</code>. It can be used to release sockets or other resources
  361. -- before the script terminates.
  362. --
  363. -- A function that may raise an exception must follow the return protocol
  364. -- understood by this function: on an exception its return values are
  365. -- <code>false</code> or <code>nil</code> followed by an error message; on
  366. -- success its return values are any true value followed by any other results.
  367. -- @param handler User cleanup function (optional).
  368. -- @usage
  369. -- local result, socket, try, catch
  370. --
  371. -- result = ""
  372. -- socket = nmap.new_socket()
  373. -- catch = function()
  374. -- socket:close()
  375. -- end
  376. -- try = nmap.new_try(catch)
  377. -- try(socket:connect(host, port))
  378. -- result = try(socket:receive_lines(1))
  379. -- try(socket:send(result))
  380. function new_try(handler)
  381. --- Returns a new NSE socket object.
  382. --
  383. -- To allow for efficient and parallelizable network I/O, NSE provides an
  384. -- interface to Nsock, the Nmap socket library. The smart callback mechanism
  385. -- Nsock uses is fully transparent to NSE scripts. The main benefit of NSE's
  386. -- sockets is that they never block on I/O operations, allowing many scripts to
  387. -- be run in parallel. The I/O parallelism is fully transparent to authors of
  388. -- NSE scripts. In NSE you can either program as if you were using a single
  389. -- non-blocking socket or you can program as if your connection is blocking.
  390. -- Seemingly blocking I/O calls still return once a specified timeout has been
  391. -- exceeded.
  392. --
  393. -- NSE sockets are the recommended way to do network I/O. They support
  394. -- <code>connect</code>-style sending and receiving over TCP and UDP (and SSL),
  395. -- as well as raw socket receiving.
  396. -- @param protocol a protocol string (optional, defaults to <code>"tcp"</code>).
  397. -- @param af an address family string (optional, defaults to <code>"inet"</code>).
  398. -- @return A new NSE socket.
  399. -- @see pcap_open
  400. -- @usage local socket = nmap.new_socket()
  401. function new_socket(protocol, af)
  402. --- Sets the local address of a socket.
  403. --
  404. -- This socket method sets the local address and port of a socket. It must be
  405. -- called before <code>connect</code>. The address set by <code>bind</code>
  406. -- overrides Nmap's source address and port set by the <code>-S</code> and
  407. -- <code>-g</code> options.
  408. -- @param addr Address string or <code>nil</code> (optional).
  409. -- @param port Port number or <code>nil</code> (optional).
  410. -- @return Status (true or false).
  411. -- @return Error string (if status is false).
  412. -- @usage
  413. -- try = nmap.new_try()
  414. -- try(socket:bind(nil, 53))
  415. -- try(socket:bind("1.2.3.4"))
  416. -- try(socket:bind("2001:db8::1"))
  417. -- try(socket:bind("1.2.3.4", 53))
  418. function bind(addr, port)
  419. --- Establishes a connection.
  420. --
  421. -- This method puts a socket in a state ready for communication. It takes as
  422. -- arguments a host descriptor (a host table, IP address, or hostname), a port
  423. -- descriptor (a port table or number), and optionally a protocol. If given, the
  424. -- protocol must be one of <code>"tcp"</code>, <code>"udp"</code> or
  425. -- <code>"ssl"</code>. The default value for the protocol is
  426. -- <code>port.protocol</code> if <code>port</code> is a port table, otherwise
  427. -- <code>"tcp"</code>.
  428. --
  429. -- If <code>host</code> is a host table, it must contain at least one of the
  430. -- keys <code>ip</code> or <code>name</code>. If <code>name</code>
  431. -- is given, it is used to request the correct certificate in SSL connections.
  432. -- Passing a string instead of a host table acts like <code>host.ip</code> and
  433. -- <code>host.name</code> were set to the same value. If <code>port</code>
  434. -- is a table, it must contain the <code>number</code> key.
  435. --
  436. -- On success the function returns a true value. On failure it returns a false
  437. -- value (<code>false</code> or <code>nil</code>) and an error string. Those
  438. -- strings are taken from the <code>gai_strerror</code> C function. They are
  439. -- (with the error code in parentheses):
  440. -- * <code>"Address family for hostname not supported"</code> (<code>EAI_ADDRFAMILY</code>)
  441. -- * <code>"Temporary failure in name resolution"</code> (<code>EAI_AGAIN</code>)
  442. -- * <code>"Bad value for ai_flags"</code> (<code>EAI_BADFLAGS</code>)
  443. -- * <code>"Non-recoverable failure in name resolution"</code> (<code>EAI_FAIL</code>)
  444. -- * <code>"ai_family not supported"</code> (<code>EAI_FAMILY</code>)
  445. -- * <code>"Memory allocation failure"</code> (<code>EAI_MEMORY</code>)
  446. -- * <code>"No address associated with hostname"</code> (<code>EAI_NODATA</code>)
  447. -- * <code>"Name or service not known"</code> (<code>EAI_NONAME</code>)
  448. -- * <code>"Servname not supported for ai_socktype"</code> (<code>EAI_SERVICE</code>)
  449. -- * <code>"ai_socktype not supported"</code> (<code>EAI_SOCKTYPE</code>)
  450. -- * <code>"System error"</code> (<code>EAI_SYSTEM</code>)
  451. -- In addition to these standard system error messages there are two
  452. -- NSE-specific errors:
  453. -- * <code>"Sorry, you don't have OpenSSL"</code>: The protocol is <code>"ssl"</code> but Nmap was compiled without OpenSSL support.
  454. -- * <code>"invalid connection method"</code>: The second parameter is not one of <code>"tcp"</code>, <code>"udp"</code>, and <code>"ssl"</code>.
  455. -- @param host Host table, hostname or IP address.
  456. -- @param port Port table or number.
  457. -- @param protocol <code>"tcp"</code>, <code>"udp"</code>, or
  458. -- <code>"ssl"</code> (default <code>"tcp"</code>, or whatever was set in
  459. -- <code>new_socket</code>).
  460. -- @return Status (true or false).
  461. -- @return Error code (if status is false).
  462. -- @see new_socket
  463. -- @usage
  464. -- local status, err = socket:connect(host, port)
  465. -- if not status then
  466. -- return string.format("Can't connect: %s", err)
  467. -- end
  468. function connect(host, port, protocol)
  469. --- Reconnect the open (connected) socket with SSL.
  470. --
  471. -- It is sometimes desirable to request SSL over an established connection.
  472. -- The internal buffers for the socket are cleared when the reconnection is
  473. -- made. Any received data that has not yet been read through a call to receive
  474. -- is lost.
  475. -- @usage
  476. -- local status, err = socket:reconnect_ssl()
  477. -- if not status then
  478. -- return string.format("Can't reconnect with ssl: %s", err)
  479. -- end
  480. function reconnect_ssl()
  481. --- Sends data on an open socket.
  482. --
  483. -- This socket method sends the data contained in the data string through an
  484. -- open connection. On success the function returns a true value. If the send
  485. -- operation fails, the function returns a false value (<code>false</code> or
  486. -- <code>nil</code>) along with an error string. The error strings are
  487. -- * <code>"Trying to send through a closed socket"</code>: There was no call to <code>socket:connect</code> before the send operation.
  488. -- * <code>"TIMEOUT"</code>: The operation took longer than the specified timeout for the socket.
  489. -- * <code>"ERROR"</code>: An error occurred inside the underlying Nsock library.
  490. -- * <code>"CANCELLED"</code>: The operation was cancelled.
  491. -- * <code>"KILL"</code>: For example the script scan is aborted due to a faulty script.
  492. -- * <code>"EOF"</code>: An EOF was read (probably will not occur for a send operation).
  493. -- @param data The data to send.
  494. -- @return Status (true or false).
  495. -- @return Error code (if status is false).
  496. -- @see new_socket
  497. -- @usage local status, err = socket:send(data)
  498. function send(data)
  499. --- Sends data on an unconnected socket to a given destination.
  500. --
  501. -- Sockets that have not been connected do not have an implicit
  502. -- destination address, so the <code>send</code> function doesn't work. Instead
  503. -- the destination must be given with each send using this function. The
  504. -- protocol and address family of the socket must have been set in
  505. -- <code>new_socket</code>. On
  506. -- success the function returns a true value. If the send operation fails, the
  507. -- function returns a false value (<code>false</code> or <code>nil</code>) along
  508. -- with an error string. The error strings are
  509. -- * <code>"TIMEOUT"</code>: The operation took longer than the specified timeout for the socket.
  510. -- * <code>"ERROR"</code>: An error occurred inside the underlying Nsock library.
  511. -- * <code>"CANCELLED"</code>: The operation was cancelled.
  512. -- * <code>"KILL"</code>: For example the script scan is aborted due to a faulty script.
  513. -- * <code>"EOF"</code>: An EOF was read (probably will not occur for a send operation).
  514. -- @param host The hostname or IP address to send to.
  515. -- @param port The port number to send to.
  516. -- @param data The data to send.
  517. -- @return Status (true or false).
  518. -- @return Error code (if status is false).
  519. -- @usage local status, err = socket:sendto(host, port, data)
  520. function sendto(host, port, data)
  521. --- Receives data from an open socket.
  522. --
  523. -- The receive method does a non-blocking receive operation on an open socket.
  524. -- On success the function returns true along with the received data. On
  525. -- failure the function returns a false value (<code>false</code> or
  526. -- <code>nil</code>) along with an error string. A failure occurs for example if
  527. -- <code>receive</code> is called on a closed socket. The receive call returns
  528. -- to the NSE script all the data currently stored in the receive buffer of the
  529. -- socket. Error conditions are the same as for <code>send</code>.
  530. -- @return Status (true or false).
  531. -- @return Data (if status is true) or error string (if status is false).
  532. -- @see new_socket
  533. -- @usage local status, data = socket:receive()
  534. function receive()
  535. --- Receives lines from an open connection.
  536. --
  537. -- Tries to receive at least <code>n</code> lines from an open connection. A
  538. -- line is a string delimited with <code>\n</code> characters. If no data was
  539. -- was received before the operation times out a <code>"TIMEOUT"</code> error
  540. -- occurs. If even one character was received then it is returned with success.
  541. -- On the other hand, if more than <code>n</code> lines were received, all are
  542. -- returned, not just <code>n</code>. Use <code>stdnse.make_buffer</code> to
  543. -- guarantee only one line is returned per call.
  544. --
  545. -- The return values and error codes are the same as for <code>send</code>.
  546. -- @param n Minimum number of lines to read.
  547. -- @return Status (true or false).
  548. -- @return Data (if status is true) or error string (if status is false).
  549. -- @see new_socket
  550. -- @usage local status, lines = socket:receive_lines(1)
  551. function receive_lines(n)
  552. --- Receives bytes from an open connection.
  553. --
  554. -- Tries to receive at least <code>n</code> bytes from an open connection. Like
  555. -- in <code>receive_lines</code>, <code>n</code> is the minimum amount of
  556. -- characters we would like to receive. If more arrive, we get all of them. If
  557. -- even one is received then it is returned. If no characters arrive before the
  558. -- operation times out, a <code>"TIMEOUT"</code> error occurs.
  559. --
  560. -- The return values and error codes are the same as for <code>send</code>.
  561. -- @param n Minimum number of bytes to read.
  562. -- @return Status (true or false).
  563. -- @return Data (if status is true) or error string (if status is false).
  564. -- @see new_socket
  565. -- @usage local status, bytes = socket:receive_bytes(1)
  566. function receive_bytes(n)
  567. --- Reads from a socket using a buffer and an arbitrary delimiter.
  568. --
  569. -- This method reads data from the network until it encounters the given
  570. -- delimiter string (or matches the function passed in). This function
  571. -- continues to read from the network until the delimiter is found or the
  572. -- function times out. If data is read beyond the delimiter, that data is
  573. -- saved in a buffer for the next call to <code>receive_buf</code>.
  574. --
  575. -- The first argument may be either a pattern or a function. If a pattern, that
  576. -- pattern is used to separate the data. If a function, it must take exactly
  577. -- one parameter (the buffer) and its return values must be in the same format
  578. -- as those of <code>string.find</code> (offsets to the start and the end of
  579. -- the delimiter inside the buffer, or <code>nil</code> if the delimiter is not
  580. -- found). The nselib <code>match.lua</code> module provides functions for
  581. -- matching against regular expressions or byte counts. These functions are
  582. -- suitable as arguments to <code>receive_buf</code>.
  583. --
  584. -- The second argument to <code>receive_buf</code> is a Boolean value
  585. -- controlling whether the delimiting string is returned along with the
  586. -- received data (true) or discarded (false).
  587. --
  588. -- On success the function returns true along with the received data. On failure
  589. -- the function returns <code>false</code> or <code>nil</code> along with an
  590. -- receive error string. This function may also throw errors for incorrect usage.
  591. -- @param delimiter A Lua pattern or a function with return values like those of
  592. -- <code>string.find</code>.
  593. -- @param keeppattern Whether to return the delimiter string with any returned
  594. -- data.
  595. -- @return Status (true or false).
  596. -- @return Data (if status is true) or error string (if status is false).
  597. -- @see new_socket
  598. -- @see match
  599. -- @usage local status, line = socket:receive_buf("\r?\n", false)
  600. function receive_buf(delimiter, keeppattern)
  601. --- Closes an open connection.
  602. --
  603. -- On success the function returns true. If the close fails, the function
  604. -- returns <code>false</code> or <code>nil</code> and an error string. Currently
  605. -- the only error message is <code>"Trying to close a closed socket"</code>,
  606. -- which is issued if the socket has already been closed.
  607. --
  608. -- Sockets are subject to garbage collection. Should you forget to close a
  609. -- socket, it will get closed before it gets deleted (on the next occasion Lua's
  610. -- garbage collector is run). However since garbage collection cycles are
  611. -- difficult to predict, it is considered good practice to close opened sockets.
  612. -- @return Status (true or false).
  613. -- @return Error code (if status is false).
  614. -- @see new_socket
  615. -- @usage socket:close()
  616. function close()
  617. --- Gets information about a socket.
  618. --
  619. -- This function returns information about a socket object. It returns five
  620. -- values. If an error occurred, the first value is <code>false</code> or
  621. -- <code>nil</code> and the second value is an error string. Otherwise the first
  622. -- value is true and the remaining 4 values describe both endpoints of the TCP
  623. -- connection. If you put the call inside an exception handler created by
  624. -- <code>new_try</code> the status value is consumed. The call can be used for
  625. -- example if you want to query an authentication server.
  626. -- @return Status (true or false).
  627. -- @return Local IP address (if status is true) or error string (if status is
  628. -- false).
  629. -- @return Local port number (if status is true).
  630. -- @return Remote IP address (if status is true).
  631. -- @return Remote port number (if status is true).
  632. -- @see new_socket
  633. -- @usage local status, lhost, lport, rhost, rport = socket:get_info()
  634. function get_info()
  635. --- Sets a timeout for socket input and output operations.
  636. --
  637. -- After this time, given in milliseconds, socket operations will time out and
  638. -- return. The default value is 30,000 (30 seconds). The lowest allowed value is
  639. -- 10 ms, since this is the granularity of NSE network I/O.
  640. -- @param t Timeout in milliseconds.
  641. -- @see new_socket
  642. -- @usage socket:set_timeout(10000)
  643. function set_timeout(t)
  644. --- Opens a socket for raw packet capture.
  645. --
  646. -- @param device The dnet-style interface name of the device you want to capture
  647. -- from.
  648. -- @param snaplen The length of each packet you want to capture (similar to the
  649. -- <code>-s</code> option to tcpdump)
  650. -- @param promisc Boolean value for whether the interface should activate
  651. -- promiscuous mode.
  652. -- @param bpf A string describing a Berkeley Packet Filter expression (like
  653. -- those provided to tcpdump).
  654. -- @see new_socket, pcap_receive
  655. -- @usage
  656. -- local socket = nmap.new_socket()
  657. -- socket:pcap_open("eth0", 64, false, "tcp")
  658. function pcap_open(device, snaplen, promisc, bpf)
  659. --- Receives a captured packet.
  660. --
  661. -- If an error or timeout occurs, the function returns false and an error
  662. -- message. Otherwise, the function returns true followed by the packet length,
  663. -- layer two header, layer three header and packet capture time.
  664. -- @return Status (true or false).
  665. -- @return The length of the captured packet (this may be smaller than the
  666. -- actual packet length since packets are truncated when the
  667. -- libpcap snaplen parameter is smaller than the total packet length).
  668. -- @return Data from the second OSI layer (e.g. ethernet headers).
  669. -- @return Data from the third OSI layer (e.g. IPv4 headers).
  670. -- @return Packet capture time, as floating point seconds since the epoch
  671. -- @see pcap_open
  672. -- @usage status, plen, l2_data, l3_data, time = socket:pcap_receive()
  673. function pcap_receive()
  674. --- Closes a pcap device.
  675. -- @see close, pcap_close
  676. -- @usage socket:pcap_close()
  677. function pcap_close()
  678. ---
  679. -- Retrieves the SSL certificate of the peer. The returned value can be accessed
  680. -- like a table and has the following members:
  681. --
  682. -- <code>
  683. -- subject = { commonName = "...", countryName = "...",
  684. -- { "2", "5", "4", "15" } = "...", ... },
  685. -- issuer = { commonName = "...", ... },
  686. -- pubkey = { type = "rsa", bits = 1024 },
  687. -- validity = { notBefore = { year = 2020, month = 5, day = 5,
  688. -- hour = 0, min = 0, sec = 0 },
  689. -- notAfter = { year = 2021, month = 5, day = 5,
  690. -- hour = 0, min = 0, sec = 0 } },
  691. -- pem = "-----BEGIN CERTIFICATE-----\nMIIFxzCCBK+gAwIBAgIQX02QuADDB7CVj..."
  692. -- </code>
  693. --
  694. -- It also has the following member functions:
  695. --
  696. -- * <code>digest(algorithm)</code> returns the digest of the certificate using the given digest algorithm, which is any of the strings returned by <code>openssl.supported_digests</code>, typically something like <code>"md5"</code> or <code>"sha1"</code>.
  697. --
  698. -- The <code>"subject"</code> and <code>"issuer"</code> fields hold each
  699. -- distinguished name. Fields with an unknown OID are represented as an array
  700. -- whose elements are the numeric components of the OID, encoded as strings.
  701. --
  702. -- The <code>"validity"</code> table has the members <code>"notBefore"</code>
  703. -- and <code>"notAfter"</code>. Each of these is a table as returned by
  704. -- <code>os.date("!*t")</code> if the date in the certificate could be parsed,
  705. -- except that they lack the <code>"wday"</code> and <code>"yday"</code>
  706. -- members. If the date could not be parsed, the value will be a string
  707. -- containing the raw byte values of the field. If absent, the value will be
  708. -- <code>nil</code>.
  709. --
  710. -- The <code>"pem"</code> field contains a PEM-encoded string of the entire
  711. -- contents of the certificate.
  712. -- @return A table as described above.
  713. -- @usage
  714. -- local s = nmap.new_socket()
  715. -- local status, error = s:connect(host, port, "ssl")
  716. -- if status then
  717. -- local cert = s:get_ssl_certificate()
  718. -- local digest = cert:digest("md5")
  719. -- end
  720. function get_ssl_certificate()
  721. --- Creates a new dnet object, used to send raw packets.
  722. -- @usage local dnet = nmap.new_dnet()
  723. function new_dnet()
  724. --- Opens an ethernet interface for raw packet sending.
  725. --
  726. -- An error (<code>"device is not valid ethernet interface"</code>) is thrown
  727. -- in case the provided argument is not valid.
  728. -- @param interface_name The dnet-style name of the interface to open.
  729. -- @see new_dnet
  730. -- @usage dnet:ethernet_open("eth0")
  731. function ethernet_open(interface_name)
  732. --- Sends a raw ethernet frame.
  733. --
  734. -- The dnet object must be associated with a previously opened interface. The
  735. -- packet must include the IP and ethernet headers. If there was no previous
  736. -- valid call to <code>ethernet_open</code> an error is thrown
  737. -- (<code>"dnet is not valid opened ethernet interface"</code>).
  738. -- @param packet An ethernet frame to send.
  739. -- @see new_dnet
  740. -- @usage dnet:ethernet_send(packet)
  741. function ethernet_send(packet)
  742. --- Closes an ethernet interface.
  743. --
  744. -- An error (<code>"device is not valid ethernet interface"</code>) is thrown
  745. -- in case the provided argument is not valid.
  746. -- @see new_dnet, ethernet_open
  747. -- @usage dnet:ethernet_close()
  748. function ethernet_close()
  749. --- Opens a socket for raw IPv4 packet sending.
  750. -- @see new_dnet
  751. -- @usage dnet:ip_open()
  752. function ip_open()
  753. --- Sends a raw IPv4 or IPv6 packet.
  754. --
  755. -- The dnet object must be associated with a previously opened socket. The
  756. -- packet must begin with an IP header. If there was no previous valid call
  757. -- to <code>ip_open</code> an error is thrown.
  758. -- @param packet An IP packet to send.
  759. -- @param dst A destination address, as a host table or string. If omitted, the
  760. -- destination address is read from the packet; however this is deprecated, because
  761. -- the packet does not contain the scope ID required to send to certain IPv6
  762. -- addresses.
  763. -- @see new_dnet
  764. -- @usage dnet:ip_send(packet, dst)
  765. function ip_send(packet, dst)
  766. --- Closes a raw IPv4 socket.
  767. -- @see new_dnet, ip_open
  768. -- @usage dnet:ip_close()
  769. function ip_close()
  770. --- Writes to a log file.
  771. --
  772. -- Writes <code>string</code> to <code>file</code> ("stdout" or "stderr").
  773. -- Use stdnse.debug to print debug information based on the
  774. -- debugging level.
  775. -- @see stdnse.debug
  776. function log_write(file, string)