mirror of
https://github.com/nmap/nmap.git
synced 2026-08-04 06:40:48 +00:00
Adding the NSE nmap.list_interfaces() function that lists all interfaces available to Nmap.
This commit is contained in:
parent
2a0c839986
commit
abf2a20866
2 changed files with 106 additions and 12 deletions
|
|
@ -18,6 +18,7 @@ extern "C" {
|
|||
#include "nmap_dns.h"
|
||||
#include "osscan.h"
|
||||
#include "protocols.h"
|
||||
#include "libnetutil/netutil.h"
|
||||
|
||||
#include "nse_nmaplib.h"
|
||||
#include "nse_utility.h"
|
||||
|
|
@ -765,17 +766,89 @@ static int l_get_interface (lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
/* returns a list of tables where each table contains information about each
|
||||
* interface.
|
||||
*/
|
||||
static int l_list_interfaces (lua_State *L)
|
||||
{
|
||||
int numifs = 0, numroutes = 0;
|
||||
struct interface_info *iflist;
|
||||
char errstr[256];
|
||||
errstr[0]='\0';
|
||||
char ipstr[INET6_ADDRSTRLEN];
|
||||
struct addr src, bcast;
|
||||
|
||||
iflist = getinterfaces(&numifs, errstr, sizeof(errstr));
|
||||
|
||||
int i;
|
||||
|
||||
if (iflist==NULL || numifs<=0) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, errstr);
|
||||
return 2;
|
||||
} else {
|
||||
memset(ipstr, 0, INET6_ADDRSTRLEN);
|
||||
memset(&src, 0, sizeof(src));
|
||||
memset(&bcast, 0, sizeof(bcast));
|
||||
lua_newtable(L); //base table
|
||||
|
||||
for(i=0; i< numifs; i++) {
|
||||
lua_newtable(L); //interface table
|
||||
setsfield(L, -1, "device", iflist[i].devfullname);
|
||||
setsfield(L, -1, "shortname", iflist[i].devname);
|
||||
setnfield(L, -1, "netmask", iflist[i].netmask_bits);
|
||||
setsfield(L, -1, "address", inet_ntop_ez(&(iflist[i].addr),
|
||||
sizeof(iflist[i].addr) ));
|
||||
|
||||
switch (iflist[i].device_type){
|
||||
case devt_ethernet:
|
||||
setsfield(L, -1, "link", "ethernet");
|
||||
lua_pushlstring(L, (const char *) iflist[i].mac, 6);
|
||||
lua_setfield(L, -2, "mac");
|
||||
|
||||
/* calculate the broadcast address */
|
||||
if (iflist[i].addr.ss_family == AF_INET) {
|
||||
src.addr_type = ADDR_TYPE_IP;
|
||||
src.addr_bits = iflist[i].netmask_bits;
|
||||
src.addr_ip = ((struct sockaddr_in *)&(iflist[i].addr))->sin_addr.s_addr;
|
||||
addr_bcast(&src, &bcast);
|
||||
memset(ipstr, 0, INET6_ADDRSTRLEN);
|
||||
if (addr_ntop(&bcast, ipstr, INET6_ADDRSTRLEN) != NULL)
|
||||
setsfield(L, -1, "broadcast", ipstr);
|
||||
}
|
||||
break;
|
||||
case devt_loopback:
|
||||
setsfield(L, -1, "link", "loopback");
|
||||
break;
|
||||
case devt_p2p:
|
||||
setsfield(L, -1, "link", "p2p");
|
||||
break;
|
||||
case devt_other:
|
||||
default:
|
||||
setsfield(L, -1, "link", "other");
|
||||
}
|
||||
|
||||
setsfield(L, -1, "up", (iflist[i].device_up ? "up" : "down"));
|
||||
setnfield(L, -1, "mtu", iflist[i].mtu);
|
||||
|
||||
/* After setting the fields, add the interface table to the base table */
|
||||
lua_rawseti(L, -2, i);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* return the ttl (time to live) specified with the
|
||||
* --ttl command line option. If a wrong value is
|
||||
* specified it defaults to 64.
|
||||
*/
|
||||
static int l_get_ttl (lua_State *L)
|
||||
{
|
||||
if (o.ttl < 0 || o.ttl > 255)
|
||||
lua_pushnumber(L, 64); //default TTL
|
||||
else
|
||||
lua_pushnumber(L, o.ttl);
|
||||
return 1;
|
||||
if (o.ttl < 0 || o.ttl > 255)
|
||||
lua_pushnumber(L, 64); //default TTL
|
||||
else
|
||||
lua_pushnumber(L, o.ttl);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* return the payload length specified by the --data-length
|
||||
|
|
@ -784,11 +857,11 @@ static int l_get_ttl (lua_State *L)
|
|||
*/
|
||||
static int l_get_payload_length(lua_State *L)
|
||||
{
|
||||
if (o.extra_payload_length < 0)
|
||||
lua_pushnumber(L, 0); //default payload length
|
||||
else
|
||||
lua_pushnumber(L, o.extra_payload_length);
|
||||
return 1;
|
||||
if (o.extra_payload_length < 0)
|
||||
lua_pushnumber(L, 0); //default payload length
|
||||
else
|
||||
lua_pushnumber(L, o.extra_payload_length);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int luaopen_nmap (lua_State *L)
|
||||
|
|
@ -818,8 +891,9 @@ int luaopen_nmap (lua_State *L)
|
|||
{"address_family", l_address_family},
|
||||
{"get_interface", l_get_interface},
|
||||
{"get_interface_info", l_dnet_get_interface_info},
|
||||
{"get_ttl", l_get_ttl},
|
||||
{"get_payload_length",l_get_payload_length},
|
||||
{"list_interfaces", l_list_interfaces},
|
||||
{"get_ttl", l_get_ttl},
|
||||
{"get_payload_length",l_get_payload_length},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -101,6 +101,26 @@ function get_interface()
|
|||
-- @usage local iface, err = nmap.get_interface_info("eth0")
|
||||
function get_interface_info(interface_name)
|
||||
|
||||
--- Lists network interfaces
|
||||
--
|
||||
-- This script enumerates all network interfaces and returns a list of tables
|
||||
-- containing information about every interface.
|
||||
--
|
||||
-- Keys of each table:
|
||||
-- * <code>device</code> The interface name, can be an interface alias.
|
||||
-- * <code>shortname</code> A simple short name of the device.
|
||||
-- * <code>netmask</code> The netmask bits (CIDR) of the interface.
|
||||
-- * <code>address</code> The string representing the IP address assigned to the interface.
|
||||
-- * <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>.
|
||||
-- * <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>.
|
||||
-- * <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>.
|
||||
-- * <code>up</code> The state of the interface, possible values are <code>"up"</code> or <code>"down"</code>.
|
||||
-- * <code>mtu</code> The MTU size of the interface.
|
||||
--
|
||||
-- @return Array of tables containing information about every discovered interface.
|
||||
-- @usage local interfaces, err = nmap.list_interfaces()
|
||||
function list_interfaces()
|
||||
|
||||
--- Returns the TTL (time to live) value selected by the --ttl option
|
||||
--
|
||||
-- If there is no value specified or if the value specified with the --ttl
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue