1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 21:21:31 +00:00

Key service table on protocol number, not name

This commit is contained in:
dmiller
2022-09-16 01:10:19 +00:00
parent 6c6d4e33b5
commit 3a32543db0
4 changed files with 65 additions and 58 deletions

View File

@@ -149,7 +149,7 @@ void Port::getNmapServiceName(char *namebuf, int buflen) const {
} else { } else {
const struct nservent *service; const struct nservent *service;
service = nmap_getservbyport(portno, IPPROTO2STR(proto)); service = nmap_getservbyport(portno, proto);
if (service != NULL) if (service != NULL)
service_name = service->s_name; service_name = service->s_name;
else else
@@ -268,7 +268,7 @@ void PortList::getServiceDeductions(u16 portno, int protocol, struct serviceDedu
/* Look up the service name. */ /* Look up the service name. */
*sd = serviceDeductions(); *sd = serviceDeductions();
service = nmap_getservbyport(portno, IPPROTO2STR(protocol)); service = nmap_getservbyport(portno, protocol);
if (service != NULL) if (service != NULL)
sd->name = service->s_name; sd->name = service->s_name;
else else
@@ -337,7 +337,7 @@ void PortList::setServiceProbeResults(u16 portno, int protocol,
Just look up the service name if none is provided. */ Just look up the service name if none is provided. */
if (sname == NULL) { if (sname == NULL) {
const struct nservent *service; const struct nservent *service;
service = nmap_getservbyport(portno, IPPROTO2STR(protocol)); service = nmap_getservbyport(portno, protocol);
if (service != NULL) if (service != NULL)
sname = service->s_name; sname = service->s_name;
} }

View File

@@ -465,15 +465,15 @@ static void getpts_aux(const char *origexpr, int nested, u8 *porttbl, int range_
} else { } else {
if (nested) { if (nested) {
if ((range_type & SCAN_TCP_PORT) && if ((range_type & SCAN_TCP_PORT) &&
nmap_getservbyport(rangestart, "tcp")) { nmap_getservbyport(rangestart, IPPROTO_TCP)) {
porttbl[rangestart] |= SCAN_TCP_PORT; porttbl[rangestart] |= SCAN_TCP_PORT;
} }
if ((range_type & SCAN_UDP_PORT) && if ((range_type & SCAN_UDP_PORT) &&
nmap_getservbyport(rangestart, "udp")) { nmap_getservbyport(rangestart, IPPROTO_UDP)) {
porttbl[rangestart] |= SCAN_UDP_PORT; porttbl[rangestart] |= SCAN_UDP_PORT;
} }
if ((range_type & SCAN_SCTP_PORT) && if ((range_type & SCAN_SCTP_PORT) &&
nmap_getservbyport(rangestart, "sctp")) { nmap_getservbyport(rangestart, IPPROTO_SCTP)) {
porttbl[rangestart] |= SCAN_SCTP_PORT; porttbl[rangestart] |= SCAN_SCTP_PORT;
} }
if ((range_type & SCAN_PROTOCOLS) && if ((range_type & SCAN_PROTOCOLS) &&

View File

@@ -64,6 +64,7 @@
#include "scan_lists.h" #include "scan_lists.h"
#include "services.h" #include "services.h"
#include "protocols.h"
#include "NmapOps.h" #include "NmapOps.h"
#include "charpool.h" #include "charpool.h"
#include "nmap_error.h" #include "nmap_error.h"
@@ -75,17 +76,17 @@
/* This structure is the key for looking up services in the /* This structure is the key for looking up services in the
port/proto -> service map. */ port/proto -> service map. */
struct port_spec { struct port_spec {
int portno; union {
std::string proto; struct {
u16 portno;
u16 proto;
} p;
u32 compval;
} u;
/* Sort in the usual nmap-services order. */ /* Sort in the usual nmap-services order. */
bool operator<(const port_spec& other) const { bool operator<(const port_spec& other) const {
if (this->portno < other.portno) return this->u.compval < other.u.compval;
return true;
else if (this->portno > other.portno)
return false;
else
return this->proto < other.proto;
} }
}; };
@@ -105,7 +106,8 @@ extern NmapOps o;
static int numtcpports; static int numtcpports;
static int numudpports; static int numudpports;
static int numsctpports; static int numsctpports;
static std::map<port_spec, service_node> service_table; typedef std::map<port_spec, service_node> ServiceMap;
static ServiceMap service_table;
static std::list<service_node> services_by_ratio; static std::list<service_node> services_by_ratio;
static int services_initialized; static int services_initialized;
static int ratio_format; // 0 = /etc/services no-ratio format. 1 = new nmap format static int ratio_format; // 0 = /etc/services no-ratio format. 1 = new nmap format
@@ -192,55 +194,60 @@ static int nmap_services_init() {
continue; continue;
} }
port_spec ps; // lowercase in-place
ps.portno = portno; *(u32 *)proto = (*(u32 *)proto) | 0x20202020;
ps.proto = proto; if (proto[3] == 0x20) proto[3] = '\0';
const struct nprotoent *npe = nmap_getprotbyname(proto);
/* Now we make sure our service table doesn't have duplicates */ int *port_count = NULL;
std::map<port_spec, service_node>::const_iterator i; switch (npe ? npe->p_proto : -1) {
i = service_table.find(ps); case IPPROTO_TCP:
if (i != service_table.end()) { port_count = &numtcpports;
if (o.debugging) break;
error("Port %d proto %s is duplicated in services file %s", portno, proto, filename); case IPPROTO_UDP:
continue; port_count = &numudpports;
} break;
case IPPROTO_SCTP:
// caseless comparison of first 4 bytes port_count = &numsctpports;
u32 protocmp = (*(u32 *)proto) & ~0x20202020; break;
if (protocmp == *(u32 *)"TCP") { default:
numtcpports++;
}
else if (protocmp == *(u32 *)"UDP") {
numudpports++;
}
else if (protocmp == *(u32 *)"SCTP" && proto[4] == '\0') {
numsctpports++;
}
else {
if (o.debugging) {
// ignore a few known protos from system services files // ignore a few known protos from system services files
if (strncasecmp(proto, "ddp", 3) == 0 || if (o.debugging
&& strncasecmp(proto, "ddp", 3) != 0
/* ddp is some apple thing...we don't "do" that */ /* ddp is some apple thing...we don't "do" that */
strncasecmp(proto, "divert", 6) == 0 || && strncasecmp(proto, "divert", 6) != 0
/* divert sockets are for freebsd's natd */ /* divert sockets are for freebsd's natd */
strncasecmp(proto, "#", 1) == 0) { && proto[0] != '#') /* possibly misplaced comment, but who cares? */
/* possibly misplaced comment, but who cares? */ {
} else { error("Unknown protocol (%s) on line %d of services file %s.", proto, lineno, filename);
error("protocmp = %u, tcp = %u", protocmp, *(u32 *)"TCP");
fatal("Unknown protocol (%s) on line %d of services file %s.", proto, lineno, filename);
} }
} continue;
continue; break;
} }
port_spec ps;
ps.u.p.portno = portno;
ps.u.p.proto = npe->p_proto;
struct service_node sn; struct service_node sn;
sn.s_name = cp_strdup(servicename); sn.s_name = cp_strdup(servicename);
sn.s_port = portno; sn.s_port = portno;
sn.s_proto = cp_strdup(proto); sn.s_proto = npe->p_name;
sn.ratio = ratio; sn.ratio = ratio;
service_table[ps] = sn; std::pair<ServiceMap::iterator, bool> status = service_table.insert(
ServiceMap::value_type(ps, sn));
if (!status.second) {
if (o.debugging > 1) {
error("Port %d proto %s is duplicated in services file %s", portno, proto, filename);
}
continue;
}
/* Now we make sure our service table doesn't have duplicates */
*port_count += 1;
services_by_ratio.push_back(sn); services_by_ratio.push_back(sn);
} }
@@ -269,7 +276,7 @@ void free_services() {
*/ */
int addportsfromservmask(const char *mask, u8 *porttbl, int range_type) { int addportsfromservmask(const char *mask, u8 *porttbl, int range_type) {
std::map<port_spec, service_node>::const_iterator i; ServiceMap::const_iterator i;
int t = 0; int t = 0;
if (!services_initialized && nmap_services_init() == -1) if (!services_initialized && nmap_services_init() == -1)
@@ -298,15 +305,15 @@ int addportsfromservmask(const char *mask, u8 *porttbl, int range_type) {
const struct nservent *nmap_getservbyport(int port, const char *proto) { const struct nservent *nmap_getservbyport(u16 port, u16 proto) {
std::map<port_spec, service_node>::const_iterator i; ServiceMap::const_iterator i;
port_spec ps; port_spec ps;
if (nmap_services_init() == -1) if (nmap_services_init() == -1)
return NULL; return NULL;
ps.portno = port; ps.u.p.portno = port;
ps.proto = proto; ps.u.p.proto = proto;
i = service_table.find(ps); i = service_table.find(ps);
if (i != service_table.end()) if (i != service_table.end())
return &i->second; return &i->second;

View File

@@ -75,7 +75,7 @@ struct nservent {
}; };
int addportsfromservmask(const char *mask, u8 *porttbl, int range_type); int addportsfromservmask(const char *mask, u8 *porttbl, int range_type);
const struct nservent *nmap_getservbyport(int port, const char *proto); const struct nservent *nmap_getservbyport(u16 port, u16 proto);
void gettoppts(double level, const char *portlist, struct scan_lists * ports, const char *exclude_list = NULL); void gettoppts(double level, const char *portlist, struct scan_lists * ports, const char *exclude_list = NULL);
void free_services(); void free_services();