This commit is contained in:
Ali 2026-11-25 04:09:23 +00:00 • committed by GitHub
commit 2b33a2620a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 46 additions and 4 deletions

View file

@ -227,8 +227,13 @@ void NmapOps::Initialize() {
isr00t = 1;
else if (getenv("NMAP_UNPRIVILEGED"))
isr00t = 0;
else
isr00t = !(geteuid());
else {
#ifdef __linux__
isr00t = (geteuid() == 0) || have_net_capabilities();
#else
isr00t = (geteuid() == 0);
#endif
}
#endif
have_pcap = true;
debugging = 0;

View file

@ -590,6 +590,12 @@ typedef bitvector_t octet_bitvector[(256 - 1) / (sizeof(unsigned long) * CHAR_BI
#include "nbase_ipv6.h"
#ifdef __linux__
/* Returns non-zero if the process has CAP_NET_RAW effective.
* See nbase_misc.c for details. */
int have_net_capabilities(void);
#endif
#ifdef __cplusplus
}
#endif

View file

@ -858,3 +858,29 @@ const char *sockaddr_storage_iptop(const struct sockaddr_storage * addr, char *
return NULL;
}}
}
/* Returns non-zero if the process has CAP_NET_RAW in its effective capability
* set. Handles setcap deployments where the binary runs as a non-root UID:
* sudo setcap cap_net_raw=ep /usr/bin/nmap
* CAP_NET_RAW is sufficient for the raw sockets and pcap packet capture nmap
* performs (promiscuous mode via PACKET_MR_PROMISC on an AF_PACKET socket
* requires only CAP_NET_RAW).
* Uses /proc/self/status to avoid a libcap dependency. */
#ifdef __linux__
int have_net_capabilities(void) {
FILE *f = fopen("/proc/self/status", "r");
if (!f)
return 0;
char line[256];
uint64_t capeff = 0;
while (fgets(line, sizeof(line), f)) {
if (strncmp(line, "CapEff:", 7) == 0) {
sscanf(line + 7, "%" SCNx64, &capeff);
break;
}
}
fclose(f);
/* CAP_NET_RAW=13 */
return (capeff & (UINT64_C(1) << 13)) != 0;
}
#endif /* __linux__ */

View file

@ -136,8 +136,13 @@ NpingOps::NpingOps() {
isr00t=true;
else if (getenv("NMAP_UNPRIVILEGED") || getenv("NPING_UNPRIVILEGED"))
isr00t=false;
else
isr00t = !(geteuid());
else {
#ifdef __linux__
isr00t = (geteuid() == 0) || have_net_capabilities();
#else
isr00t = (geteuid() == 0);
#endif
}
#endif
/* Payloads */