From cc40823e5c2ecd74871f627b836a4661b849eb6b Mon Sep 17 00:00:00 2001 From: Robert David Graham Date: Thu, 16 Nov 2023 01:47:48 -0500 Subject: [PATCH 1/4] Windows cleanup --- src/in-binary.c | 4 ++++ src/main-conf.c | 5 ++++- src/massip-rangesv4.c | 4 ++++ src/proto-dns.c | 3 --- src/proto-ftp.c | 1 + src/proto-isakmp.c | 1 - src/proto-mc.c | 6 ++++-- src/proto-ssl.c | 1 + src/stack-tcp-core.c | 2 +- src/string_s.h | 2 -- 10 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/in-binary.c b/src/in-binary.c index adfab175..4c139bd3 100644 --- a/src/in-binary.c +++ b/src/in-binary.c @@ -18,6 +18,10 @@ #include #include +#ifdef _MSC_VER +#pragma warning(disable:4996) +#endif + static const size_t BUF_MAX = 1024*1024; struct MasscanRecord { diff --git a/src/main-conf.c b/src/main-conf.c index d3b525fb..f0651219 100644 --- a/src/main-conf.c +++ b/src/main-conf.c @@ -2332,7 +2332,10 @@ static int SET_tcp_sackok(struct Masscan *masscan, const char *name, const char static int SET_debug_tcp(struct Masscan *masscan, const char *name, const char *value) { - extern int is_tcp_debug; + extern int is_tcp_debug; /* global */ + UNUSEDPARM(name); + UNUSEDPARM(masscan); + if (value == 0 || value[0] == '\0') is_tcp_debug = 1; else diff --git a/src/massip-rangesv4.c b/src/massip-rangesv4.c index c66cc4d3..fa6fda96 100644 --- a/src/massip-rangesv4.c +++ b/src/massip-rangesv4.c @@ -39,6 +39,10 @@ #include #include +#ifdef _MSC_VER +#pragma warning(disable:4204) +#endif + #define BUCKET_COUNT 16 #define REGRESS(x) if (!(x)) return (fprintf(stderr, "regression failed %s:%d\n", __FILE__, __LINE__)|1) diff --git a/src/proto-dns.c b/src/proto-dns.c index 287c7871..9f391333 100644 --- a/src/proto-dns.c +++ b/src/proto-dns.c @@ -446,7 +446,4 @@ handle_dns(struct Output *out, time_t timestamp, px + offset, txtlen); return 1; } - - - return 0; } diff --git a/src/proto-ftp.c b/src/proto-ftp.c index d7b494c4..45dc9bd5 100644 --- a/src/proto-ftp.c +++ b/src/proto-ftp.c @@ -127,6 +127,7 @@ ftp_parse( const struct Banner1 *banner1, static void * ftp_init(struct Banner1 *banner1) { + UNUSEDPARM(banner1); //banner1->payloads.tcp[21] = &banner_ftp; return 0; } diff --git a/src/proto-isakmp.c b/src/proto-isakmp.c index 0ed0f335..9e610ca9 100644 --- a/src/proto-isakmp.c +++ b/src/proto-isakmp.c @@ -76,7 +76,6 @@ isakmp_parse(struct Output *out, time_t timestamp, unsigned port_me = parsed->port_dst; uint64_t cookie; uint64_t resp_cookie; - unsigned char i; /* All responses will be at least 8 bytes */ if (length < 16) diff --git a/src/proto-mc.c b/src/proto-mc.c index ac0a3a88..90e7be64 100644 --- a/src/proto-mc.c +++ b/src/proto-mc.c @@ -54,7 +54,8 @@ mc_parse( const struct Banner1 *banner1, { size_t i; struct MCSTUFF *mc = &pstate->sub.mc; - + UNUSEDPARM(banner1_private); + UNUSEDPARM(banner1); for(i = 0; i < length; i++) { if(px[i] == '{') mc->brackcount++; @@ -76,7 +77,8 @@ mc_parse( const struct Banner1 *banner1, if(mc->imgstart) mc->imgstart-=(size_t)mc->banmem; } else { // we found start but not the end - if((mc->imgend = (size_t)memchr(mc->banmem+mc->imgstart,'\"',mc->totalLen-mc->imgstart))){ // we found the end + mc->imgend = (size_t)memchr(mc->banmem+mc->imgstart,'\"',mc->totalLen-mc->imgstart); + if(mc->imgend){ // we found the end mc->imgend-=(size_t)mc->banmem; memcpy(mc->banmem+mc->imgstart,mc->banmem+mc->imgend,(mc->totalLen-mc->imgend)+1); // copy data after B64 mc->totalLen=mc->imgstart+(mc->totalLen-mc->imgend); // shrink length to subtract B64 image diff --git a/src/proto-ssl.c b/src/proto-ssl.c index 7d27f341..f884e2b2 100644 --- a/src/proto-ssl.c +++ b/src/proto-ssl.c @@ -392,6 +392,7 @@ parse_server_cert( UNUSEDPARM(banner1); UNUSEDPARM(banner1_private); + UNUSEDPARM(socket); for (i=0; iseqno_them; if (payload_offset < 0) { /* This is a retrnasmission that we've already acknowledged */ - if (payload_offset <= 0 - payload_length) { + if (payload_offset <= 0 - (int)payload_length) { /* Both begin and end are old, so simply discard it */ return TCB__okay; } else { diff --git a/src/string_s.h b/src/string_s.h index e489eda8..9cbc8cdd 100644 --- a/src/string_s.h +++ b/src/string_s.h @@ -50,8 +50,6 @@ #undef itoa #define itoa ITOA_FUNCTION_IS_BAD -#undef strerror -#define strerror STRERROR_FUNCTION_IS_BAD const char *strerror_x(int x); From 0e9f8110db24702f7ee19b689f3cc1a7e2e2caf3 Mon Sep 17 00:00:00 2001 From: Robert David Graham Date: Thu, 16 Nov 2023 04:37:50 -0500 Subject: [PATCH 2/4] cleaned up portability for MingGW compiles --- README.md | 2 +- src/crypto-blackrock2.c | 2 +- src/crypto-lcg.c | 2 +- src/in-binary.c | 10 ++-- src/main-conf.c | 61 ++++++++++++------------ src/main-ptrace.c | 36 +++++++------- src/main-status.c | 2 +- src/main.c | 10 ++-- src/masscan-app.c | 4 +- src/masscan.h | 2 +- src/massip-parse.c | 10 ++-- src/out-binary.c | 6 +-- src/out-certs.c | 2 +- src/out-grepable.c | 6 +-- src/out-json.c | 2 +- src/out-ndjson.c | 2 +- src/out-redis.c | 14 +++--- src/out-xml.c | 6 +-- src/output.c | 16 +++---- src/proto-coap.c | 6 +-- src/proto-http.c | 2 +- src/proto-ntlmssp.c | 4 +- src/proto-ntp.c | 6 +-- src/proto-smb.c | 10 ++-- src/proto-snmp.c | 6 +-- src/proto-ssl.c | 10 ++-- src/proto-tcp-rdp.c | 2 +- src/proto-tcp-telnet.c | 2 +- src/proto-vnc.c | 8 ++-- src/proto-zeroaccess.c | 4 +- src/rawsock-getif.c | 6 +-- src/rawsock-getip.c | 4 +- src/rawsock-getip6.c | 2 +- src/rawsock-getmac.c | 4 +- src/rawsock-getroute.c | 2 +- src/rawsock.c | 16 +++---- src/rte-ring.c | 2 +- src/rte-ring.h | 2 +- src/stack-arpv4.c | 2 +- src/stack-tcp-core.c | 8 ++-- src/stub-pfring.c | 2 +- src/syn-cookie.c | 17 ++++--- src/templ-pkt.c | 2 +- src/util-logger.c | 6 +-- src/{string_s.c => util-safefunc.c} | 58 ++++++---------------- src/{string_s.h => util-safefunc.h} | 37 +++++++------- src/vulncheck.c | 2 +- src/xring.c | 2 +- xcode4/masscan.xcodeproj/project.pbxproj | 16 +++---- 49 files changed, 206 insertions(+), 239 deletions(-) rename src/{string_s.c => util-safefunc.c} (56%) rename src/{string_s.h => util-safefunc.h} (70%) diff --git a/README.md b/README.md index a0c44622..ed61e2f1 100644 --- a/README.md +++ b/README.md @@ -504,7 +504,7 @@ probably faster than you want anyway. A bounty is offered for vulnerabilities, see the VULNINFO.md file for more information. -This project uses safe functions like `strcpy_s()` instead of unsafe functions +This project uses safe functions like `safe_strcpy()` instead of unsafe functions like `strcpy()`. This project has automated unit regression tests (`make regress`). diff --git a/src/crypto-blackrock2.c b/src/crypto-blackrock2.c index 4131d6a5..db807c88 100644 --- a/src/crypto-blackrock2.c +++ b/src/crypto-blackrock2.c @@ -2,7 +2,7 @@ #include "pixie-timer.h" #include "unusedparm.h" #include "util-malloc.h" -#include "string_s.h" +#include "util-safefunc.h" #include #include #include diff --git a/src/crypto-lcg.c b/src/crypto-lcg.c index e06ec431..8082daab 100644 --- a/src/crypto-lcg.c +++ b/src/crypto-lcg.c @@ -5,7 +5,7 @@ #include "crypto-lcg.h" #include "crypto-primegen.h" /* DJB's prime factoring code */ -#include "string_s.h" +#include "util-safefunc.h" #include "util-malloc.h" #include /* for 'sqrt()', may need -lm for gcc */ diff --git a/src/in-binary.c b/src/in-binary.c index 4c139bd3..865a6f99 100644 --- a/src/in-binary.c +++ b/src/in-binary.c @@ -9,7 +9,7 @@ #include "masscan-status.h" #include "main-globals.h" #include "output.h" -#include "string_s.h" +#include "util-safefunc.h" #include "in-filter.h" #include "in-report.h" #include "util-malloc.h" @@ -478,15 +478,15 @@ _binaryfile_parse(struct Output *out, const char *filename, unsigned char *buf = 0; size_t bytes_read; uint64_t total_records = 0; - int x; /* Allocate a buffer of up to one megabyte per record */ buf = MALLOC(BUF_MAX); /* Open the file */ - x = fopen_s(&fp, filename, "rb"); - if (x != 0 || fp == NULL) { - perror(filename); + fp = fopen(filename, "rb"); + if (fp == NULL) { + fprintf(stderr, "[-] FAIL: --readscan\n"); + fprintf(stderr, "[-] %s: %s\n", filename, strerror(errno)); goto end; } diff --git a/src/main-conf.c b/src/main-conf.c index f0651219..dc120879 100644 --- a/src/main-conf.c +++ b/src/main-conf.c @@ -14,7 +14,7 @@ #include "masscan.h" #include "massip-addr.h" #include "masscan-version.h" -#include "string_s.h" +#include "util-safefunc.h" #include "util-logger.h" #include "proto-banner1.h" #include "templ-payloads.h" @@ -296,7 +296,7 @@ masscan_echo_nic(struct Masscan *masscan, FILE *fp, unsigned i) if (masscan->nic_count <= 1) idx_str[0] = '\0'; else - sprintf_s(idx_str, sizeof(idx_str), "[%u]", i); + snprintf(idx_str, sizeof(idx_str), "[%u]", i); if (masscan->nic[i].ifname[0]) fprintf(fp, "adapter%s = %s\n", idx_str, masscan->nic[i].ifname); @@ -391,17 +391,16 @@ masscan_save_state(struct Masscan *masscan) { char filename[512]; FILE *fp; - int err; - - strcpy_s(filename, sizeof(filename), "paused.conf"); + safe_strcpy(filename, sizeof(filename), "paused.conf"); fprintf(stderr, " " " \r"); fprintf(stderr, "saving resume file to: %s\n", filename); - err = fopen_s(&fp, filename, "wt"); - if (err) { - perror(filename); + fp = fopen(filename, "wt"); + if (fp == NULL) { + fprintf(stderr, "[-] FAIL: saving resume file\n"); + fprintf(stderr, "[-] %s: %s\n", filename, strerror(errno)); return; } @@ -1205,7 +1204,6 @@ static int SET_hello_file(struct Masscan *masscan, const char *name, const char { unsigned index; FILE *fp; - int x; char buf[16384]; char buf2[16384]; size_t bytes_read; @@ -1225,10 +1223,10 @@ static int SET_hello_file(struct Masscan *masscan, const char *name, const char } /* When connecting via TCP, send this file */ - x = fopen_s(&fp, value, "rb"); - if (x != 0) { - LOG(0, "[FAILED] could not read hello file\n"); - perror(value); + fp = fopen(value, "rb"); + if (fp == NULL) { + LOG(0, "[-] [FAILED] --hello-file\n"); + LOG(0, "[-] %s: %s\n", value, strerror(errno)); return CONF_ERR; } @@ -1244,7 +1242,7 @@ static int SET_hello_file(struct Masscan *masscan, const char *name, const char bytes_encoded = base64_encode(buf2, sizeof(buf2)-1, buf, bytes_read); buf2[bytes_encoded] = '\0'; - sprintf_s(foo, sizeof(foo), "hello-string[%u]", (unsigned)index); + snprintf(foo, sizeof(foo), "hello-string[%u]", (unsigned)index); masscan_set_parameter(masscan, foo, buf2); @@ -1636,7 +1634,7 @@ static int SET_output_filename(struct Masscan *masscan, const char *name, const } if (masscan->output.format == 0) masscan->output.format = Output_XML; /*TODO: Why is the default XML?*/ - strcpy_s(masscan->output.filename, + safe_strcpy(masscan->output.filename, sizeof(masscan->output.filename), value); return CONF_OK; @@ -1806,7 +1804,7 @@ static int SET_pcap_filename(struct Masscan *masscan, const char *name, const ch return 0; } if (value) - strcpy_s(masscan->pcap_filename, sizeof(masscan->pcap_filename), value); + safe_strcpy(masscan->pcap_filename, sizeof(masscan->pcap_filename), value); return CONF_OK; } @@ -1960,7 +1958,7 @@ static int SET_rotate_directory(struct Masscan *masscan, const char *name, const } return 0; } - strcpy_s( masscan->output.rotate.directory, + safe_strcpy( masscan->output.rotate.directory, sizeof(masscan->output.rotate.directory), value); /* strip trailing slashes */ @@ -2087,7 +2085,7 @@ static int SET_output_stylesheet(struct Masscan *masscan, const char *name, cons if (masscan->output.format == 0) masscan->output.format = Output_XML; - strcpy_s(masscan->output.stylesheet, sizeof(masscan->output.stylesheet), value); + safe_strcpy(masscan->output.stylesheet, sizeof(masscan->output.stylesheet), value); return CONF_OK; } @@ -2462,7 +2460,7 @@ masscan_set_parameter(struct Masscan *masscan, } if (masscan->nic_count < index + 1) masscan->nic_count = index + 1; - sprintf_s( masscan->nic[index].ifname, + snprintf( masscan->nic[index].ifname, sizeof(masscan->nic[index].ifname), "%s", value); @@ -2751,7 +2749,7 @@ masscan_set_parameter(struct Masscan *masscan, /* The timeout for banners TCP connections */ masscan->tcp_connection_timeout = (unsigned)parseInt(value); } else if (EQUALS("datadir", name)) { - strcpy_s(masscan->nmap.datadir, sizeof(masscan->nmap.datadir), value); + safe_strcpy(masscan->nmap.datadir, sizeof(masscan->nmap.datadir), value); } else if (EQUALS("data-length", name)) { unsigned x = (unsigned)strtoul(value, 0, 0); if (x >= 1514 - 14 - 40) { @@ -2901,7 +2899,7 @@ masscan_set_parameter(struct Masscan *masscan, masscan->redis.port = port; masscan->output.format = Output_Redis; - strcpy_s(masscan->output.filename, + safe_strcpy(masscan->output.filename, sizeof(masscan->output.filename), ""); } else if(EQUALS("redis-pwd", name)) { @@ -3156,17 +3154,16 @@ masscan_load_database_files(struct Masscan *masscan) } /* - * "nmap-payloads" + * `--nmap-payloads` */ filename = masscan->payloads.nmap_payloads_filename; if (filename) { FILE *fp; - int err; - - err = fopen_s(&fp, filename, "rt"); - if (err || fp == NULL) { - perror(filename); + fp = fopen(filename, "rt"); + if (fp == NULL) { + fprintf(stderr, "[-] FAIL: --nmap-payloads\n"); + fprintf(stderr, "[-] %s:%s\n", filename, strerror(errno)); } else { if (masscan->payloads.udp == NULL) masscan->payloads.udp = payloads_udp_create(); @@ -3869,14 +3866,16 @@ void masscan_read_config_file(struct Masscan *masscan, const char *filename) { FILE *fp; - errno_t err; char line[65536]; - err = fopen_s(&fp, filename, "rt"); - if (err) { + fp = fopen(filename, "rt"); + if (fp == NULL) { char dir[512]; char *x; - perror(filename); + + fprintf(stderr, "[-] FAIL: reading configuration file\n"); + fprintf(stderr, "[-] %s: %s\n", filename, strerror(errno)); + x = getcwd(dir, sizeof(dir)); if (x) fprintf(stderr, "[-] cwd = %s\n", dir); diff --git a/src/main-ptrace.c b/src/main-ptrace.c index b14d839e..22eb1299 100644 --- a/src/main-ptrace.c +++ b/src/main-ptrace.c @@ -1,7 +1,7 @@ #include "main-ptrace.h" #include "proto-preprocess.h" #include "pixie-timer.h" -#include "string_s.h" +#include "util-safefunc.h" /*************************************************************************** @@ -35,10 +35,10 @@ packet_trace(FILE *fp, double pt_start, const unsigned char *px, size_t length, /* format the IP addresses into fixed-width fields */ fmt = ipaddress_fmt(parsed.src_ip); - sprintf_s(from, sizeof(from), "[%s]:%u", fmt.string, parsed.port_src); + snprintf(from, sizeof(from), "[%s]:%u", fmt.string, parsed.port_src); fmt = ipaddress_fmt(parsed.dst_ip); - sprintf_s(to, sizeof(to), "[%s]:%u", fmt.string, parsed.port_dst); + snprintf(to, sizeof(to), "[%s]:%u", fmt.string, parsed.port_dst); switch (parsed.found) { case FOUND_ARP: @@ -46,9 +46,9 @@ packet_trace(FILE *fp, double pt_start, const unsigned char *px, size_t length, *strchr(to, ':') = '\0'; *strchr(from, ':') = '\0'; switch (type) { - case 1:strcpy_s(sz_type, sizeof(sz_type), "request"); break; - case 2:strcpy_s(sz_type, sizeof(sz_type), "response"); break; - default: sprintf_s(sz_type, sizeof(sz_type), "unknown(%u)", type); break; + case 1:safe_strcpy(sz_type, sizeof(sz_type), "request"); break; + case 2:safe_strcpy(sz_type, sizeof(sz_type), "response"); break; + default: snprintf(sz_type, sizeof(sz_type), "unknown(%u)", type); break; } fprintf(fp, "%s (%5.4f) ARP %-21s > %-21s %s\n", direction, timestamp - pt_start, from, to, sz_type); @@ -65,19 +65,19 @@ packet_trace(FILE *fp, double pt_start, const unsigned char *px, size_t length, case FOUND_TCP: type = px[offset+13]; switch (type) { - case 0x00: strcpy_s(sz_type, sizeof(sz_type), "NULL"); break; - case 0x01: strcpy_s(sz_type, sizeof(sz_type), "FIN"); break; - case 0x11: strcpy_s(sz_type, sizeof(sz_type), "FIN-ACK"); break; - case 0x19: strcpy_s(sz_type, sizeof(sz_type), "FIN-ACK-PSH"); break; - case 0x02: strcpy_s(sz_type, sizeof(sz_type), "SYN"); break; - case 0x12: strcpy_s(sz_type, sizeof(sz_type), "SYN-ACK"); break; - case 0x04: strcpy_s(sz_type, sizeof(sz_type), "RST"); break; - case 0x14: strcpy_s(sz_type, sizeof(sz_type), "RST-ACK"); break; - case 0x15: strcpy_s(sz_type, sizeof(sz_type), "RST-FIN-ACK"); break; - case 0x10: strcpy_s(sz_type, sizeof(sz_type), "ACK"); break; - case 0x18: strcpy_s(sz_type, sizeof(sz_type), "ACK-PSH"); break; + case 0x00: safe_strcpy(sz_type, sizeof(sz_type), "NULL"); break; + case 0x01: safe_strcpy(sz_type, sizeof(sz_type), "FIN"); break; + case 0x11: safe_strcpy(sz_type, sizeof(sz_type), "FIN-ACK"); break; + case 0x19: safe_strcpy(sz_type, sizeof(sz_type), "FIN-ACK-PSH"); break; + case 0x02: safe_strcpy(sz_type, sizeof(sz_type), "SYN"); break; + case 0x12: safe_strcpy(sz_type, sizeof(sz_type), "SYN-ACK"); break; + case 0x04: safe_strcpy(sz_type, sizeof(sz_type), "RST"); break; + case 0x14: safe_strcpy(sz_type, sizeof(sz_type), "RST-ACK"); break; + case 0x15: safe_strcpy(sz_type, sizeof(sz_type), "RST-FIN-ACK"); break; + case 0x10: safe_strcpy(sz_type, sizeof(sz_type), "ACK"); break; + case 0x18: safe_strcpy(sz_type, sizeof(sz_type), "ACK-PSH"); break; default: - sprintf_s(sz_type, sizeof(sz_type), + snprintf(sz_type, sizeof(sz_type), "%s%s%s%s%s%s%s%s", (type&0x01)?"FIN":"", (type&0x02)?"SYN":"", diff --git a/src/main-status.c b/src/main-status.c index fe07aaa0..2cd15623 100644 --- a/src/main-status.c +++ b/src/main-status.c @@ -12,7 +12,7 @@ #include "pixie-timer.h" #include "unusedparm.h" #include "main-globals.h" -#include "string_s.h" +#include "util-safefunc.h" #include "util-bool.h" #include diff --git a/src/main.c b/src/main.c index e0113c61..751f2fad 100644 --- a/src/main.c +++ b/src/main.c @@ -687,7 +687,7 @@ receive_thread(void *v) if (masscan->tcp_connection_timeout) { char foo[64]; - sprintf_s(foo, sizeof(foo), "%u", masscan->tcp_connection_timeout); + snprintf(foo, sizeof(foo), "%u", masscan->tcp_connection_timeout); tcpcon_set_parameter( tcpcon, "timeout", strlen(foo), @@ -695,7 +695,7 @@ receive_thread(void *v) } if (masscan->tcp_hello_timeout) { char foo[64]; - sprintf_s(foo, sizeof(foo), "%u", masscan->tcp_hello_timeout); + snprintf(foo, sizeof(foo), "%u", masscan->tcp_hello_timeout); tcpcon_set_parameter( tcpcon, "hello-timeout", strlen(foo), @@ -726,7 +726,7 @@ receive_thread(void *v) for (pay = masscan->payloads.tcp; pay; pay = pay->next) { char name[64]; - sprintf_s(name, sizeof(name), "hello-string[%u]", pay->port); + snprintf(name, sizeof(name), "hello-string[%u]", pay->port); tcpcon_set_parameter( tcpcon, name, strlen(pay->payload_base64), @@ -1347,7 +1347,7 @@ main_scan(struct Masscan *masscan) struct tm x; now = time(0); - gmtime_s(&x, &now); + safe_gmtime(&x, &now); strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S GMT", &x); LOG(0, "Starting masscan " MASSCAN_VERSION " (http://bit.ly/14GZzcT) at %s\n", buffer); @@ -1604,7 +1604,7 @@ int main(int argc, char *argv[]) masscan->redis.password = NULL; masscan->payloads.udp = payloads_udp_create(); masscan->payloads.oproto = payloads_oproto_create(); - strcpy_s( masscan->output.rotate.directory, + safe_strcpy( masscan->output.rotate.directory, sizeof(masscan->output.rotate.directory), "."); masscan->is_capture_cert = 1; diff --git a/src/masscan-app.c b/src/masscan-app.c index 37749b60..903015fb 100644 --- a/src/masscan-app.c +++ b/src/masscan-app.c @@ -1,5 +1,5 @@ #include "masscan-app.h" -#include "string_s.h" +#include "util-safefunc.h" /****************************************************************************** * When outputting results, we call this function to print out the type of @@ -51,7 +51,7 @@ masscan_app_to_string(enum ApplicationProtocol proto) case PROTO_ERROR: return "error"; default: - sprintf_s(tmp, sizeof(tmp), "(%u)", proto); + snprintf(tmp, sizeof(tmp), "(%u)", proto); return tmp; } } diff --git a/src/masscan.h b/src/masscan.h index 5e0854d4..c132a911 100644 --- a/src/masscan.h +++ b/src/masscan.h @@ -1,7 +1,7 @@ #ifndef MASSCAN_H #define MASSCAN_H #include "massip-addr.h" -#include "string_s.h" +#include "util-safefunc.h" #include "stack-src.h" #include "massip.h" #include "util-bool.h" diff --git a/src/massip-parse.c b/src/massip-parse.c index 25a3148c..f3c77ed8 100644 --- a/src/massip-parse.c +++ b/src/massip-parse.c @@ -14,7 +14,7 @@ #include "util-logger.h" #include "util-bool.h" #include "util-malloc.h" -#include "string_s.h" +#include "util-safefunc.h" #include "unusedparm.h" #include @@ -844,10 +844,10 @@ massip_parse_file(struct MassIP *massip, const char *filename) if (strcmp(filename, "-") == 0) { fp = stdin; } else { - int err; - err = fopen_s(&fp, filename, "rb"); - if (err || fp == NULL) { - perror(filename); + fp = fopen(filename, "rb"); + if (fp == NULL) { + fprintf(stderr, "[-] FAIL: parsing IP addresses\n"); + fprintf(stderr, "[-] %s: %s\n", filename, strerror(errno)); exit(1); } } diff --git a/src/out-binary.c b/src/out-binary.c index 2372cd6a..d0dfac5b 100644 --- a/src/out-binary.c +++ b/src/out-binary.c @@ -2,7 +2,7 @@ #include "masscan-app.h" #include "masscan-status.h" #include "out-record.h" -#include "string_s.h" +#include "util-safefunc.h" #include /**************************************************************************** @@ -17,7 +17,7 @@ binary_out_open(struct Output *out, FILE *fp) memset(firstrecord, 0, 2+'a'); - sprintf_s(firstrecord, 2+'a', "masscan/1.1\ns:%u\n", + snprintf(firstrecord, 2+'a', "masscan/1.1\ns:%u\n", (unsigned)out->when_scan_started); bytes_written = fwrite(firstrecord, 1, 2+'a', fp); if (bytes_written != 2+'a') { @@ -40,7 +40,7 @@ binary_out_close(struct Output *out, FILE *fp) UNUSEDPARM(out); memset(firstrecord, 0, 2+'a'); - sprintf_s(firstrecord, 2+'a', "masscan/1.1"); + snprintf(firstrecord, 2+'a', "masscan/1.1"); bytes_written = fwrite(firstrecord, 1, 2+'a', fp); if (bytes_written != 2+'a') { perror("output"); diff --git a/src/out-certs.c b/src/out-certs.c index 214eb587..5e0705ac 100644 --- a/src/out-certs.c +++ b/src/out-certs.c @@ -1,7 +1,7 @@ #include "output.h" #include "masscan-app.h" #include "masscan-status.h" -#include "string_s.h" +#include "util-safefunc.h" #include diff --git a/src/out-grepable.c b/src/out-grepable.c index 69cdfe8b..0a4887f3 100644 --- a/src/out-grepable.c +++ b/src/out-grepable.c @@ -4,7 +4,7 @@ #include "masscan-status.h" #include "out-tcp-services.h" #include "massip-port.h" -#include "string_s.h" +#include "util-safefunc.h" /**************************************************************************** @@ -75,7 +75,7 @@ grepable_out_open(struct Output *out, FILE *fp) unsigned count; - gmtime_s(&tm, &out->when_scan_started); + safe_gmtime(&tm, &out->when_scan_started); //Tue Jan 21 20:23:22 2014 //%a %b %d %H:%M:%S %Y @@ -122,7 +122,7 @@ grepable_out_close(struct Output *out, FILE *fp) UNUSEDPARM(out); - gmtime_s(&tm, &now); + safe_gmtime(&tm, &now); //Tue Jan 21 20:23:22 2014 //%a %b %d %H:%M:%S %Y diff --git a/src/out-json.c b/src/out-json.c index 5d91e436..21b2b83d 100644 --- a/src/out-json.c +++ b/src/out-json.c @@ -1,7 +1,7 @@ #include "output.h" #include "masscan-app.h" #include "masscan-status.h" -#include "string_s.h" +#include "util-safefunc.h" #include diff --git a/src/out-ndjson.c b/src/out-ndjson.c index 56fd3211..de040cf2 100644 --- a/src/out-ndjson.c +++ b/src/out-ndjson.c @@ -1,7 +1,7 @@ #include "output.h" #include "masscan-app.h" #include "masscan-status.h" -#include "string_s.h" +#include "util-safefunc.h" #include diff --git a/src/out-redis.c b/src/out-redis.c index 9aad9ace..e06ddd1f 100644 --- a/src/out-redis.c +++ b/src/out-redis.c @@ -157,7 +157,7 @@ redis_out_open(struct Output *out, FILE *fp) UNUSEDPARM(out); if (out->redis.password != NULL) { - sprintf_s(line, sizeof(line), + snprintf(line, sizeof(line), "*2\r\n" "$4\r\nAUTH\r\n" "$%u\r\n%s\r\n", @@ -233,8 +233,8 @@ redis_out_status(struct Output *out, FILE *fp, time_t timestamp, int values_length; ipaddress_formatted_t fmt = ipaddress_fmt(ip); - ip_string_length = sprintf_s(ip_string, sizeof(ip_string), "%s", fmt.string); - port_string_length = sprintf_s(port_string, sizeof(port_string), "%u/%s", port, name_from_ip_proto(ip_proto)); + ip_string_length = snprintf(ip_string, sizeof(ip_string), "%s", fmt.string); + port_string_length = snprintf(port_string, sizeof(port_string), "%u/%s", port, name_from_ip_proto(ip_proto)); /**3 $3 @@ -249,7 +249,7 @@ myvalue * KEY: "host" * VALUE: ip */ - sprintf_s(line, sizeof(line), + snprintf(line, sizeof(line), "*3\r\n" "$4\r\nSADD\r\n" "$%d\r\n%s\r\n" @@ -270,7 +270,7 @@ myvalue * KEY: ip * VALUE: port */ - sprintf_s(line, sizeof(line), + snprintf(line, sizeof(line), "*3\r\n" "$4\r\nSADD\r\n" "$%d\r\n%s\r\n" @@ -291,9 +291,9 @@ myvalue * KEY: ip:port * VALUE: timestamp:status:reason:ttl */ - values_length = sprintf_s(values, sizeof(values), "%u:%u:%u:%u", + values_length = snprintf(values, sizeof(values), "%u:%u:%u:%u", (unsigned)timestamp, status, reason, ttl); - line_length = sprintf_s(line, sizeof(line), + line_length = snprintf(line, sizeof(line), "*3\r\n" "$4\r\nSADD\r\n" "$%d\r\n%s:%s\r\n" diff --git a/src/out-xml.c b/src/out-xml.c index fd73305a..72a6581d 100644 --- a/src/out-xml.c +++ b/src/out-xml.c @@ -1,7 +1,7 @@ #include "output.h" #include "masscan-app.h" #include "masscan-status.h" -#include "string_s.h" +#include "util-safefunc.h" @@ -39,9 +39,9 @@ xml_out_close(struct Output *out, FILE *fp) struct tm tm; if (out->is_gmt) - gmtime_s(&tm, &now); + safe_gmtime(&tm, &now); else - localtime_s(&tm, &now); + safe_localtime(&tm, &now); strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm); fprintf(fp, diff --git a/src/output.c b/src/output.c index ccaa5c76..5a1fa669 100644 --- a/src/output.c +++ b/src/output.c @@ -34,7 +34,7 @@ #include "output.h" #include "masscan.h" #include "masscan-status.h" -#include "string_s.h" +#include "util-safefunc.h" #include "proto-banner1.h" #include "masscan-app.h" #include "main-globals.h" @@ -103,7 +103,7 @@ status_string(enum PortStatus status) const char * reason_string(int x, char *buffer, size_t sizeof_buffer) { - sprintf_s(buffer, sizeof_buffer, "%s%s%s%s%s%s%s%s", + snprintf(buffer, sizeof_buffer, "%s%s%s%s%s%s%s%s", (x&0x01)?"fin-":"", (x&0x02)?"syn-":"", (x&0x04)?"rst-":"", @@ -357,7 +357,7 @@ indexed_filename(const char *filename, unsigned index) /* format the new name */ - sprintf_s(new_filename, new_length, "%.*s.%02u%s", + snprintf(new_filename, new_length, "%.*s.%02u%s", (unsigned)ext, filename, index, filename+ext); @@ -550,9 +550,9 @@ output_do_rotate(struct Output *out, int is_closing) /* Get the proper timestamp for the file */ if (out->is_gmt) { - err = gmtime_s(&tm, &out->rotate.last); + err = safe_gmtime(&tm, &out->rotate.last); } else { - err = localtime_s(&tm, &out->rotate.last); + err = safe_localtime(&tm, &out->rotate.last); } if (err != 0) { free(new_filename); @@ -575,7 +575,7 @@ output_do_rotate(struct Output *out, int is_closing) x_off = strlen(filename); x_len = 0; } - sprintf_s(new_filename, new_filename_size, + snprintf(new_filename, new_filename_size, "%s/%.*s-%05u%.*s", dir, (unsigned)x_off, filename, @@ -583,7 +583,7 @@ output_do_rotate(struct Output *out, int is_closing) (unsigned)x_len, filename + x_off ); } else { - sprintf_s(new_filename, new_filename_size, + snprintf(new_filename, new_filename_size, "%s/%02u%02u%02u-%02u%02u%02u" "-%s", dir, tm.tm_year % 100, @@ -636,7 +636,7 @@ output_do_rotate(struct Output *out, int is_closing) fp = open_rotate(out, filename); if (fp == NULL) { - LOG(0, "rotate: %s: failed: %s\n", filename, strerror_x(errno)); + LOG(0, "rotate: %s: failed: %s\n", filename, strerror(errno)); } else { close_rotate(out, out->fp); out->fp = fp; diff --git a/src/proto-coap.c b/src/proto-coap.c index c97bbd0e..ea2bfdc3 100644 --- a/src/proto-coap.c +++ b/src/proto-coap.c @@ -40,7 +40,7 @@ #include "syn-cookie.h" #include "massip-port.h" #include "util-malloc.h" -#include "string_s.h" +#include "util-safefunc.h" #include "util-bool.h" #include #include @@ -352,7 +352,7 @@ coap_parse(const unsigned char *px, size_t length, struct BannerOutput *banout, /* Response code */ { char buf[64]; - sprintf_s(buf, sizeof(buf), "rsp=%u.%u(%s)", code>>5, code&0x1F, response_code(code)); + snprintf(buf, sizeof(buf), "rsp=%u.%u(%s)", code>>5, code&0x1F, response_code(code)); banout_append(banout, PROTO_COAP, buf, AUTO_LEN); //code >>= 5; } @@ -361,7 +361,7 @@ coap_parse(const unsigned char *px, size_t length, struct BannerOutput *banout, /* If there was a token, the print it. */ if (token) { char buf[64]; - sprintf_s(buf, sizeof(buf), " token=0x%llu", token); + snprintf(buf, sizeof(buf), " token=0x%llu", token); banout_append(banout, PROTO_COAP, buf, AUTO_LEN); } diff --git a/src/proto-http.c b/src/proto-http.c index b25188a2..671b5099 100644 --- a/src/proto-http.c +++ b/src/proto-http.c @@ -3,7 +3,7 @@ #include "stack-tcp-api.h" #include "smack.h" #include "unusedparm.h" -#include "string_s.h" +#include "util-safefunc.h" #include "masscan-app.h" #include "util-malloc.h" #include "util-bool.h" diff --git a/src/proto-ntlmssp.c b/src/proto-ntlmssp.c index 37a99b65..1a5a53e4 100644 --- a/src/proto-ntlmssp.c +++ b/src/proto-ntlmssp.c @@ -1,7 +1,7 @@ #include "proto-ntlmssp.h" #include "masscan-app.h" #include "proto-banout.h" -#include "string_s.h" +#include "util-safefunc.h" #include "util-malloc.h" #include #include @@ -169,7 +169,7 @@ ntlmssp_decode(struct NtlmsspDecode *x, /* Version field */ { char buf[64]; - sprintf_s(buf, sizeof(buf), " version=%u.%u.%u ntlm-ver=%u", + snprintf(buf, sizeof(buf), " version=%u.%u.%u ntlm-ver=%u", px[48], px[49], px[50] | px[51]<<8, diff --git a/src/proto-ntp.c b/src/proto-ntp.c index 20c0ef2e..72ab04e5 100644 --- a/src/proto-ntp.c +++ b/src/proto-ntp.c @@ -5,7 +5,7 @@ #include #include #include "smack.h" -#include "string_s.h" +#include "util-safefunc.h" #include "output.h" #include "masscan-app.h" #include "proto-preprocess.h" @@ -122,7 +122,7 @@ ntp_modlist_parse(const unsigned char *px, const char *errmsg = val2string_lookup(error_codes, errcode); if (errmsg == 0) errmsg = "Bogus Error Code"; - sprintf_s(foo, sizeof(foo), "%u", errcode); + snprintf(foo, sizeof(foo), "%u", errcode); banout_append(banout, PROTO_NTP, "Response was NTP Error Code ", AUTO_LEN); banout_append(banout, PROTO_NTP, foo, AUTO_LEN); banout_append(banout, PROTO_NTP, " - \"", AUTO_LEN); @@ -145,7 +145,7 @@ ntp_modlist_parse(const unsigned char *px, { char msg[128]; - sprintf_s(msg, sizeof(msg), " response-size=%u-bytes more=%s", + snprintf(msg, sizeof(msg), " response-size=%u-bytes more=%s", record_count * record_size, ((px[0]>>6)&1)?"true":"false"); banout_append(banout, PROTO_NTP, msg, AUTO_LEN); diff --git a/src/proto-smb.c b/src/proto-smb.c index 73ef9632..a811f6a5 100644 --- a/src/proto-smb.c +++ b/src/proto-smb.c @@ -7,7 +7,7 @@ #include "unusedparm.h" #include "masscan-app.h" #include "crypto-siphash24.h" -#include "string_s.h" +#include "util-safefunc.h" #include "unusedparm.h" #include #include @@ -1006,7 +1006,7 @@ smb2_parse_negotiate(struct SMBSTUFF *smb, const unsigned char *px, size_t offse struct tm tm = {0}; size_t len; - gmtime_s(&tm, ×tamp); + safe_gmtime(&tm, ×tamp); len = strftime(str, sizeof(str), " time=%Y-%m-%d %H:%M:%S ", &tm); banout_append(banout, PROTO_SMB, str, len); smb->is_printed_time = 1; @@ -1022,7 +1022,7 @@ smb2_parse_negotiate(struct SMBSTUFF *smb, const unsigned char *px, size_t offse struct tm tm = {0}; size_t len; - gmtime_s(&tm, ×tamp); + safe_gmtime(&tm, ×tamp); len = strftime(str, sizeof(str), " boottime=%Y-%m-%d %H:%M:%S ", &tm); banout_append(banout, PROTO_SMB, str, len); smb->is_printed_boottime = 1; @@ -1439,11 +1439,11 @@ smb_parse_smb(struct SMBSTUFF *smb, const unsigned char *px, size_t max, struct time_t timestamp = convert_windows_time(smb->parms.negotiate.SystemTime); struct tm tm = {0}; - gmtime_s(&tm, ×tamp); + safe_gmtime(&tm, ×tamp); len = strftime(str, sizeof(str), " time=%Y-%m-%d %H:%M:%S", &tm); banout_append(banout, PROTO_SMB, str, len); - sprintf_s(str, sizeof(str), " TZ=%+d ", (short)smb->parms.negotiate.ServerTimeZone); + snprintf(str, sizeof(str), " TZ=%+d ", (short)smb->parms.negotiate.ServerTimeZone); banout_append(banout, PROTO_SMB, str, AUTO_LEN); smb->is_printed_time = 1; diff --git a/src/proto-snmp.c b/src/proto-snmp.c index 1c49c9c0..53a869e2 100644 --- a/src/proto-snmp.c +++ b/src/proto-snmp.c @@ -41,7 +41,7 @@ #include #include #include "smack.h" -#include "string_s.h" +#include "util-safefunc.h" #include "output.h" #include "masscan-app.h" #include "proto-preprocess.h" @@ -245,7 +245,7 @@ snmp_banner_oid(const unsigned char *oid, size_t oid_length, if (x == 0 && i >= oid_length) break; - sprintf_s(foo, sizeof(foo), ".%" PRIu64 "", x); + snprintf(foo, sizeof(foo), ".%" PRIu64 "", x); banout_append(banout, PROTO_SNMP, foo, strlen(foo)); } } @@ -275,7 +275,7 @@ snmp_banner(const unsigned char *oid, size_t oid_length, uint64_t result = 0; for (i=0; i #include @@ -72,7 +72,7 @@ BANNER_CIPHER(struct BannerOutput *banout, unsigned cipher_suite) { //const char *notes = ""; char foo[64]; - sprintf_s(foo, sizeof(foo), "cipher:0x%x", cipher_suite); + snprintf(foo, sizeof(foo), "cipher:0x%x", cipher_suite); banout_append(banout, PROTO_SSL3, foo, AUTO_LEN); /*switch (cipher_suite) { @@ -110,7 +110,7 @@ BANNER_VERSION(struct BannerOutput *banout, unsigned version_major, banout_append(banout, PROTO_SSL3, "TLS/1.3 ", AUTO_LEN); break; default: - sprintf_s(foo, sizeof(foo), "SSLver[%u,%u] ", + snprintf(foo, sizeof(foo), "SSLver[%u,%u] ", version_major, version_minor); banout_append(banout, PROTO_SSL3, foo, strlen(foo)); @@ -837,7 +837,7 @@ parse_alert( default: banout_append(banout, PROTO_SAFE, "poodle[no-SSLv3] ", AUTO_LEN); - sprintf_s(foo, sizeof(foo), " ALERT(0x%02x%02x) ", + snprintf(foo, sizeof(foo), " ALERT(0x%02x%02x) ", ssl->x.server_alert.level, ssl->x.server_alert.description ); @@ -847,7 +847,7 @@ parse_alert( } } else { char foo[64]; - sprintf_s(foo, sizeof(foo), " ALERT(0x%02x%02x) ", + snprintf(foo, sizeof(foo), " ALERT(0x%02x%02x) ", ssl->x.server_alert.level, ssl->x.server_alert.description ); diff --git a/src/proto-tcp-rdp.c b/src/proto-tcp-rdp.c index 5ad7b895..8ea2ae96 100644 --- a/src/proto-tcp-rdp.c +++ b/src/proto-tcp-rdp.c @@ -8,7 +8,7 @@ #include "assert.h" #include #include -#include "string_s.h" +#include "util-safefunc.h" /*************************************************************************** * @param length diff --git a/src/proto-tcp-telnet.c b/src/proto-tcp-telnet.c index 91416a21..cdb066ed 100644 --- a/src/proto-tcp-telnet.c +++ b/src/proto-tcp-telnet.c @@ -6,7 +6,7 @@ #include "util-malloc.h" #include #include -#include "string_s.h" +#include "util-safefunc.h" struct TelnetOptions { unsigned num; diff --git a/src/proto-vnc.c b/src/proto-vnc.c index c261056e..a2c1359e 100644 --- a/src/proto-vnc.c +++ b/src/proto-vnc.c @@ -3,7 +3,7 @@ #include "stack-tcp-api.h" #include "unusedparm.h" #include "masscan-app.h" -#include "string_s.h" +#include "util-safefunc.h" #include "smack.h" #include @@ -82,7 +82,7 @@ vnc_append_sectype(struct BannerOutput *banout, unsigned sectype) banout_append(banout, PROTO_VNC_INFO, " Apple35", AUTO_LEN); break; default: - sprintf_s(foo, sizeof(foo), " %u", sectype); + snprintf(foo, sizeof(foo), " %u", sectype); banout_append(banout, PROTO_VNC_INFO, foo, AUTO_LEN); break; } @@ -266,7 +266,7 @@ vnc_parse( const struct Banner1 *banner1, case RFB_SERVERINIT+1: pstate->sub.vnc.width <<= 8; pstate->sub.vnc.width |= px[i]; - sprintf_s(foo, sizeof(foo), " width=%u", pstate->sub.vnc.width); + snprintf(foo, sizeof(foo), " width=%u", pstate->sub.vnc.width); banout_append(banout, PROTO_VNC_RFB, foo, AUTO_LEN); state++; break; @@ -277,7 +277,7 @@ vnc_parse( const struct Banner1 *banner1, case RFB_SERVERINIT+3: pstate->sub.vnc.height <<= 8; pstate->sub.vnc.height |= px[i]; - sprintf_s(foo, sizeof(foo), " height=%u", pstate->sub.vnc.height); + snprintf(foo, sizeof(foo), " height=%u", pstate->sub.vnc.height); banout_append(banout, PROTO_VNC_RFB, foo, AUTO_LEN); state++; break; diff --git a/src/proto-zeroaccess.c b/src/proto-zeroaccess.c index f0c51edc..f267b886 100644 --- a/src/proto-zeroaccess.c +++ b/src/proto-zeroaccess.c @@ -11,7 +11,7 @@ #include "proto-preprocess.h" #include "output.h" #include "proto-banner1.h" -#include "string_s.h" +#include "util-safefunc.h" #include #include @@ -233,7 +233,7 @@ handle_zeroaccess( struct Output *out, time_t timestamp, | buf[16 + i*8 + 2] << 8 | buf[16 + i*8 + 3] << 0; - sprintf_s(szaddr, sizeof(szaddr), "%u.%u.%u.%u ", + snprintf(szaddr, sizeof(szaddr), "%u.%u.%u.%u ", (unsigned char)(ip_found>>24), (unsigned char)(ip_found>>16), (unsigned char)(ip_found>> 8), diff --git a/src/rawsock-getif.c b/src/rawsock-getif.c index 01ff6c03..860cb2f2 100755 --- a/src/rawsock-getif.c +++ b/src/rawsock-getif.c @@ -5,7 +5,7 @@ This works on both Linux and windows. */ #include "rawsock.h" -#include "string_s.h" +#include "util-safefunc.h" #include "util-malloc.h" #include "util-logger.h" @@ -380,7 +380,7 @@ int rawsock_get_default_interface(char *ifname, size_t sizeof_ifname) ipv4 = ntohl(rtInfo->gateWay.s_addr); if (ipv4 == 0) continue; - strcpy_s(ifname, sizeof_ifname, rtInfo->ifName); + safe_strcpy(ifname, sizeof_ifname, rtInfo->ifName); } } @@ -473,7 +473,7 @@ int rawsock_get_default_interface(char *ifname, size_t sizeof_ifname) * we'll use that one */ if (ipv4) { - sprintf_s(ifname, sizeof_ifname, "\\Device\\NPF_%s", pAdapter->AdapterName); + snprintf(ifname, sizeof_ifname, "\\Device\\NPF_%s", pAdapter->AdapterName); } } diff --git a/src/rawsock-getip.c b/src/rawsock-getip.c index 33c0a310..ef0ad4a1 100644 --- a/src/rawsock-getip.c +++ b/src/rawsock-getip.c @@ -12,7 +12,7 @@ I think it'll work the same on any BSD system. */ #include "rawsock.h" -#include "string_s.h" +#include "util-safefunc.h" #include "massip-parse.h" /***************************************************************************** @@ -37,7 +37,7 @@ rawsock_get_adapter_ip(const char *ifname) fd = socket(AF_INET, SOCK_DGRAM, 0); ifr.ifr_addr.sa_family = AF_INET; - strcpy_s(ifr.ifr_name, IFNAMSIZ, ifname); + safe_strcpy(ifr.ifr_name, IFNAMSIZ, ifname); x = ioctl(fd, SIOCGIFADDR, &ifr); if (x < 0) { diff --git a/src/rawsock-getip6.c b/src/rawsock-getip6.c index 931b4a07..cea4975f 100644 --- a/src/rawsock-getip6.c +++ b/src/rawsock-getip6.c @@ -12,7 +12,7 @@ I think it'll work the same on any BSD system. */ #include "rawsock.h" -#include "string_s.h" +#include "util-safefunc.h" #include "massip-parse.h" /***************************************************************************** diff --git a/src/rawsock-getmac.c b/src/rawsock-getmac.c index a816de3d..15ef70f9 100644 --- a/src/rawsock-getmac.c +++ b/src/rawsock-getmac.c @@ -10,7 +10,7 @@ I think it'll work the same on any BSD system. */ #include "rawsock.h" -#include "string_s.h" +#include "util-safefunc.h" #include "util-logger.h" /***************************************************************************** @@ -38,7 +38,7 @@ rawsock_get_adapter_mac(const char *ifname, unsigned char *mac) goto end; } - strcpy_s(ifr.ifr_name, IFNAMSIZ, ifname); + safe_strcpy(ifr.ifr_name, IFNAMSIZ, ifname); x = ioctl(fd, SIOCGIFHWADDR, (char *)&ifr); if (x < 0) { perror("ioctl"); diff --git a/src/rawsock-getroute.c b/src/rawsock-getroute.c index 67afbcb1..e9b1b538 100644 --- a/src/rawsock-getroute.c +++ b/src/rawsock-getroute.c @@ -5,7 +5,7 @@ This works on both Linux and windows. */ #include "rawsock.h" -#include "string_s.h" +#include "util-safefunc.h" #include "util-malloc.h" #include "massip-parse.h" #include "util-logger.h" diff --git a/src/rawsock.c b/src/rawsock.c index 75b6be7a..721d850d 100755 --- a/src/rawsock.c +++ b/src/rawsock.c @@ -8,7 +8,7 @@ #include "templ-pkt.h" #include "util-logger.h" #include "main-ptrace.h" -#include "string_s.h" +#include "util-safefunc.h" #include "stub-pcap.h" #include "stub-pfring.h" #include "pixie-timer.h" @@ -145,15 +145,15 @@ rawsock_init(void) if (name == NULL || addr == NULL) exit(1); - sprintf_s(name, name_len, "\\Device\\NPF_%s", pAdapter->AdapterName); + snprintf(name, name_len, "\\Device\\NPF_%s", pAdapter->AdapterName); //printf("\tAdapter Desc: \t%s\n", pAdapter->Description); //printf("\tAdapter Addr: \t"); for (i = 0; i < pAdapter->AddressLength; i++) { if (i == (pAdapter->AddressLength - 1)) - sprintf_s(addr+i*3, addr_len-i*3, "%.2X", pAdapter->Address[i]); + snprintf(addr+i*3, addr_len-i*3, "%.2X", pAdapter->Address[i]); else - sprintf_s(addr+i*3, addr_len-i*3, "%.2X-", pAdapter->Address[i]); + snprintf(addr+i*3, addr_len-i*3, "%.2X-", pAdapter->Address[i]); } //printf("%s -> %s\n", addr, name); adapter_names[adapter_name_count].easy_name = addr; @@ -170,8 +170,8 @@ rawsock_init(void) char *addr = (char*)malloc(addr_len); if (name == NULL || addr == NULL) exit(1); - sprintf_s(name, name_len, "\\Device\\NPF_%s", pAdapter->AdapterName); - sprintf_s(addr, addr_len, "%s", pAdapter->IpAddressList.IpAddress.String); + snprintf(name, name_len, "\\Device\\NPF_%s", pAdapter->AdapterName); + snprintf(addr, addr_len, "%s", pAdapter->IpAddressList.IpAddress.String); //printf("%s -> %s\n", addr, name); adapter_names[adapter_name_count].easy_name = addr; adapter_names[adapter_name_count].hard_name = name; @@ -665,7 +665,7 @@ rawsock_init_adapter(const char *adapter_name, adapter->link_type = 1; if (adapter->ring == NULL) { LOG(0, "pfring:'%s': OPEN ERROR: %s\n", - adapter_name, strerror_x(errno)); + adapter_name, strerror(errno)); return 0; } else LOG(1, "pfring:'%s': successfully opened\n", adapter_name); @@ -697,7 +697,7 @@ rawsock_init_adapter(const char *adapter_name, err = PFRING.enable_ring(adapter->ring); if (err != 0) { LOG(0, "pfring: '%s': ENABLE ERROR: %s\n", - adapter_name, strerror_x(errno)); + adapter_name, strerror(errno)); PFRING.close(adapter->ring); adapter->ring = 0; return 0; diff --git a/src/rte-ring.c b/src/rte-ring.c index 7f79a233..0d8d370d 100644 --- a/src/rte-ring.c +++ b/src/rte-ring.c @@ -69,7 +69,7 @@ * POSSIBILITY OF SUCH DAMAGE. * ***************************************************************************/ -#include "string_s.h" +#include "util-safefunc.h" #include #include #include diff --git a/src/rte-ring.h b/src/rte-ring.h index b2864ff0..9590c86c 100644 --- a/src/rte-ring.h +++ b/src/rte-ring.h @@ -95,7 +95,7 @@ #ifndef EDQUOT #define EDQUOT EOVERFLOW #endif -#define rte_snprintf sprintf_s +#define rte_snprintf snprintf #define PRIu32 "u" #endif diff --git a/src/stack-arpv4.c b/src/stack-arpv4.c index 99cd8471..a48005a4 100755 --- a/src/stack-arpv4.c +++ b/src/stack-arpv4.c @@ -17,7 +17,7 @@ #include "stack-src.h" #include "stack-arpv4.h" #include "stack-queue.h" -#include "string_s.h" +#include "util-safefunc.h" #include "util-logger.h" #include "pixie-timer.h" #include "proto-preprocess.h" diff --git a/src/stack-tcp-core.c b/src/stack-tcp-core.c index cfa3beea..f50467c2 100644 --- a/src/stack-tcp-core.c +++ b/src/stack-tcp-core.c @@ -75,7 +75,7 @@ #include "proto-smb.h" #include "proto-versioning.h" #include "output.h" -#include "string_s.h" +#include "util-safefunc.h" #include "main-globals.h" #include "crypto-base64.h" #include "util-malloc.h" @@ -232,7 +232,7 @@ state_to_string(int state) case STATE_ESTABLISHED_RECV:return "ESTABLISHED_RECV"; default: - sprintf_s(buf, sizeof(buf), "%d", state); + snprintf(buf, sizeof(buf), "%d", state); return buf; } } @@ -404,7 +404,7 @@ tcpcon_set_parameter(struct TCP_ConnectionTable *tcpcon, if (name_equals(name, "http-payload")) { char lenstr[64]; - sprintf_s(lenstr, sizeof(lenstr), "%u", (unsigned)value_length); + snprintf(lenstr, sizeof(lenstr), "%u", (unsigned)value_length); banner_http.hello_length = http_change_requestline( (unsigned char**)&banner_http.hello, @@ -1241,7 +1241,7 @@ what_to_string(enum TCP_What state) case TCP_WHAT_DATA: return "DATA"; case TCP_WHAT_CLOSE: return "CLOSE"; default: - sprintf_s(buf, sizeof(buf), "%d", state); + snprintf(buf, sizeof(buf), "%d", state); return buf; } } diff --git a/src/stub-pfring.c b/src/stub-pfring.c index 2a4663ae..00e39a2c 100644 --- a/src/stub-pfring.c +++ b/src/stub-pfring.c @@ -7,7 +7,7 @@ */ #include "stub-pfring.h" -#include "string_s.h" +#include "util-safefunc.h" #include "util-logger.h" struct PFRING PFRING; diff --git a/src/syn-cookie.c b/src/syn-cookie.c index c64a5ed7..4c9eb10a 100644 --- a/src/syn-cookie.c +++ b/src/syn-cookie.c @@ -1,6 +1,6 @@ #include "syn-cookie.h" #include "pixie-timer.h" -#include "string_s.h" +#include "util-safefunc.h" #include "crypto-siphash24.h" #include #include @@ -20,7 +20,6 @@ get_entropy(void) { uint64_t entropy[2] = {0,0}; unsigned i; - int err; /* * Gather some random bits @@ -32,24 +31,25 @@ get_entropy(void) entropy[0] ^= __rdtsc(); #endif time(0); - err = fopen_s(&fp, "/", "r"); + fp = fopen("/", "r"); entropy[1] <<= 1; entropy[1] |= entropy[0]>>63; entropy[0] <<= 1; - if (err == 0 && fp) { + if (fp) { fclose(fp); } } entropy[0] ^= time(0); -#if defined(__linux__) + /* Always try to open this file, even on platforms like Windows + * where it's not going to exist. */ { FILE *fp; - err = fopen_s(&fp, "/dev/urandom", "r"); - if (err == 0 && fp) { - int x; + fp = fopen("/dev/urandom", "r"); + if (fp) { + size_t x; uint64_t urand = 0; x = fread(&urand, 1, sizeof(urand), fp); entropy[0] ^= urand; @@ -61,7 +61,6 @@ get_entropy(void) } entropy[0] ^= pixie_nanotime(); } -#endif return entropy[0] ^ entropy[1]; } diff --git a/src/templ-pkt.c b/src/templ-pkt.c index fcb93ed3..db65724e 100644 --- a/src/templ-pkt.c +++ b/src/templ-pkt.c @@ -13,7 +13,7 @@ #include "massip-port.h" #include "proto-preprocess.h" #include "proto-sctp.h" -#include "string_s.h" +#include "util-safefunc.h" #include "pixie-timer.h" #include "util-logger.h" #include "templ-payloads.h" diff --git a/src/util-logger.c b/src/util-logger.c index 03c1c1f8..d133d0b6 100644 --- a/src/util-logger.c +++ b/src/util-logger.c @@ -9,7 +9,7 @@ be redirected to a file. */ #include "util-logger.h" -#include "string_s.h" +#include "util-safefunc.h" #include #include @@ -52,7 +52,7 @@ vLOGnet(unsigned port_me, ipaddress ip_them, const char *fmt, va_list marker) char sz_ip[64]; ipaddress_formatted_t fmt1 = ipaddress_fmt(ip_them); - sprintf_s(sz_ip, sizeof(sz_ip), "%s", fmt1.string); + snprintf(sz_ip, sizeof(sz_ip), "%s", fmt1.string); fprintf(stderr, "%u:%s: ", port_me, sz_ip); vfprintf(stderr, fmt, marker); fflush(stderr); @@ -78,7 +78,7 @@ vLOGip(int level, ipaddress ip, unsigned port, const char *fmt, va_list marker) char sz_ip[64]; ipaddress_formatted_t fmt1 = ipaddress_fmt(ip); - sprintf_s(sz_ip, sizeof(sz_ip), "%s:%u: ", fmt1.string, port); + snprintf(sz_ip, sizeof(sz_ip), "%s:%u: ", fmt1.string, port); fprintf(stderr, "%s ", sz_ip); vfprintf(stderr, fmt, marker); fflush(stderr); diff --git a/src/string_s.c b/src/util-safefunc.c similarity index 56% rename from src/string_s.c rename to src/util-safefunc.c index 10dfac0d..73d32863 100644 --- a/src/string_s.c +++ b/src/util-safefunc.c @@ -2,32 +2,16 @@ safe C library functions This upgrades unsafe C functions like "strcpy()" to safer equivalents, - like "strcpy_s()". + like "safe_strcpy()". NOTE: This is for maintaining a policy of "no unsafe functions" */ -#include "string_s.h" +#include "util-safefunc.h" #include #include #include #include -/** - * fopen_s - */ -#if !defined(WIN32) || _MSC_VER == 1200 -errno_t fopen_s(FILE **pFile, const char *filename, const char *mode) -{ - if (pFile == NULL || filename == NULL || mode == NULL) - return EINVAL; - *pFile = fopen(filename, mode); - if (*pFile != NULL) - return 0; - else - return errno; -} -#endif - /** * Case-insensitive memcmp() */ @@ -45,44 +29,43 @@ memcasecmp(const void *lhs, const void *rhs, size_t length) #endif /** - * strcpy + * Safe version of `strcpy()` */ -#if !defined(WIN32) -errno_t strcpy_s(char *dst, size_t sizeof_dst, const char *src) +void +safe_strcpy(char *dst, size_t sizeof_dst, const char *src) { size_t i; if (sizeof_dst == 0) - return ERANGE; + return; if (dst == NULL) - return EINVAL; + return; if (src == NULL) { dst[0] = 0; - return EINVAL; + return; } for (i=0; src[i]; i++) { if (i >= sizeof_dst) { dst[0] = 0; - return ERANGE; + return; } else dst[i] = src[i]; } if (i >= sizeof_dst) { dst[0] = 0; - return ERANGE; + return ; } else dst[i] = src[i]; - return 0; + return; } -#endif -#ifdef __GNUC__ -errno_t localtime_s(struct tm* _tm, const time_t *time) + +errno_t safe_localtime(struct tm* _tm, const time_t *time) { struct tm *x; @@ -95,7 +78,7 @@ errno_t localtime_s(struct tm* _tm, const time_t *time) return 0; } -errno_t gmtime_s(struct tm* _tm, const time_t *time) +errno_t safe_gmtime(struct tm* _tm, const time_t *time) { struct tm *x; @@ -108,18 +91,5 @@ errno_t gmtime_s(struct tm* _tm, const time_t *time) return 0; } -#endif -/* - * I don't understand why Microsoft says this function is unsafe, so - * do it anyway - */ -const char *strerror_x(int x) -{ -#ifdef _MSC_VER -#pragma warning(disable: 4996) -#endif -#undef strerror - return strerror(x); -} diff --git a/src/string_s.h b/src/util-safefunc.h similarity index 70% rename from src/string_s.h rename to src/util-safefunc.h index 9cbc8cdd..3f06ed8c 100644 --- a/src/string_s.h +++ b/src/util-safefunc.h @@ -1,15 +1,19 @@ /* - safe "string" functions, like Microsoft's + safe "string" functions This is for the "safe" clib functions, where things like "strcpy()" is - replaced with a safer version of the function, like "strcpy_s()". Since - these things are non-standard, compilers deal with them differently. + replaced with a safer version of the function, like "safe_strcpy()". + + NOTE: I tried to based these on Microosft's `..._s()` functions proposed + in Annex K, but it's become too hard trying to deal with both my own + version and existing versions. Therefore, I've changed this code to + use names not used by others. Reference: http://msdn.microsoft.com/en-us/library/bb288454.aspx */ -#ifndef STRCPY_S -#define STRCPY_S +#ifndef UTIL_SAFEFUNC_H +#define UTIL_SAFEFUNC_H #include #include #include @@ -50,8 +54,14 @@ #undef itoa #define itoa ITOA_FUNCTION_IS_BAD +/** + * A bounds checking version of strcpy, like `strcpy_s()` on Windows or + * `strlcpy()` in glibc. + */ +void safe_strcpy(char *dst, size_t sizeof_dst, const char *src); +errno_t safe_localtime(struct tm* _tm, const time_t *time); +errno_t safe_gmtime(struct tm* _tm, const time_t *time); -const char *strerror_x(int x); #if defined(_MSC_VER) && (_MSC_VER >= 1900) /*Visual Studio 2015 and 2017*/ @@ -80,27 +90,16 @@ const char *strerror_x(int x); #elif defined(_MSC_VER) && (_MSC_VER == 1200) /* Visual Studio 6.0 */ -# define sprintf_s _snprintf +# define snprintf _snprintf # define strcasecmp _stricmp # define memcasecmp _memicmp -# define vsprintf_s _vsnprintf +# define vsnprintf _vsnprintf typedef int errno_t; -errno_t fopen_s(FILE **fp, const char *filename, const char *mode); #elif defined(__GNUC__) && (__GNUC__ >= 4) #include -/* GCC 4 */ -# define sprintf_s snprintf -# define vsprintf_s vsnprintf int memcasecmp(const void *lhs, const void *rhs, size_t length); typedef int errno_t; -#if !defined(WIN32) /* mingw */ -errno_t fopen_s(FILE **fp, const char *filename, const char *mode); -errno_t strcpy_s(char *dst, size_t sizeof_dst, const char *src); -errno_t localtime_s(struct tm* _tm, const time_t *time); -errno_t gmtime_s(struct tm* _tm, const time_t *time); -#endif -#undef strerror #else # warning unknown compiler diff --git a/src/vulncheck.c b/src/vulncheck.c index a5c6d6c3..7efe5af3 100644 --- a/src/vulncheck.c +++ b/src/vulncheck.c @@ -1,5 +1,5 @@ #include "vulncheck.h" -#include "string_s.h" +#include "util-safefunc.h" extern struct MassVulnCheck vuln_ntp_monlist; diff --git a/src/xring.c b/src/xring.c index 888fdade..3a729b4a 100644 --- a/src/xring.c +++ b/src/xring.c @@ -1,7 +1,7 @@ #include "xring.h" #include "pixie-threads.h" #include "pixie-timer.h" -#include "string_s.h" +#include "util-safefunc.h" #include diff --git a/xcode4/masscan.xcodeproj/project.pbxproj b/xcode4/masscan.xcodeproj/project.pbxproj index 8e5f1692..533c5ddc 100644 --- a/xcode4/masscan.xcodeproj/project.pbxproj +++ b/xcode4/masscan.xcodeproj/project.pbxproj @@ -82,7 +82,7 @@ 118D67F52B02DD6F00271F7F /* smack1.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921CA17DBCC7E00DDFD32 /* smack1.c */; }; 118D67F62B02DD6F00271F7F /* massip-rangesv4.c in Sources */ = {isa = PBXBuildFile; fileRef = 118B9B2B25A00FA800F5FB0B /* massip-rangesv4.c */; }; 118D67F72B02DD6F00271F7F /* smackqueue.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921CB17DBCC7E00DDFD32 /* smackqueue.c */; }; - 118D67F82B02DD6F00271F7F /* string_s.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921CD17DBCC7E00DDFD32 /* string_s.c */; }; + 118D67F82B02DD6F00271F7F /* util-safefunc.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921CD17DBCC7E00DDFD32 /* util-safefunc.c */; }; 118D67F92B02DD6F00271F7F /* stack-ndpv6.c in Sources */ = {isa = PBXBuildFile; fileRef = 11B6297625995AA100D4786F /* stack-ndpv6.c */; }; 118D67FA2B02DD6F00271F7F /* syn-cookie.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921CF17DBCC7E00DDFD32 /* syn-cookie.c */; }; 118D67FB2B02DD6F00271F7F /* stack-arpv4.c in Sources */ = {isa = PBXBuildFile; fileRef = 1124DD6D25B4FF3C00EEFC2C /* stack-arpv4.c */; }; @@ -193,7 +193,7 @@ 11A921F417DBCC7E00DDFD32 /* rte-ring.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921C717DBCC7E00DDFD32 /* rte-ring.c */; }; 11A921F517DBCC7E00DDFD32 /* smack1.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921CA17DBCC7E00DDFD32 /* smack1.c */; }; 11A921F617DBCC7E00DDFD32 /* smackqueue.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921CB17DBCC7E00DDFD32 /* smackqueue.c */; }; - 11A921F717DBCC7E00DDFD32 /* string_s.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921CD17DBCC7E00DDFD32 /* string_s.c */; }; + 11A921F717DBCC7E00DDFD32 /* util-safefunc.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921CD17DBCC7E00DDFD32 /* util-safefunc.c */; }; 11A921F817DBCC7E00DDFD32 /* syn-cookie.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921CF17DBCC7E00DDFD32 /* syn-cookie.c */; }; 11A921F917DBCC7E00DDFD32 /* templ-pkt.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921D117DBCC7E00DDFD32 /* templ-pkt.c */; }; 11A921FA17DBCC7E00DDFD32 /* xring.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A921D317DBCC7E00DDFD32 /* xring.c */; }; @@ -397,8 +397,8 @@ 11A921CA17DBCC7E00DDFD32 /* smack1.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = smack1.c; sourceTree = ""; }; 11A921CB17DBCC7E00DDFD32 /* smackqueue.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = smackqueue.c; sourceTree = ""; }; 11A921CC17DBCC7E00DDFD32 /* smackqueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = smackqueue.h; sourceTree = ""; }; - 11A921CD17DBCC7E00DDFD32 /* string_s.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = string_s.c; sourceTree = ""; }; - 11A921CE17DBCC7E00DDFD32 /* string_s.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = string_s.h; sourceTree = ""; }; + 11A921CD17DBCC7E00DDFD32 /* util-safefunc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "util-safefunc.c"; sourceTree = ""; }; + 11A921CE17DBCC7E00DDFD32 /* util-safefunc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "util-safefunc.h"; sourceTree = ""; }; 11A921CF17DBCC7E00DDFD32 /* syn-cookie.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "syn-cookie.c"; sourceTree = ""; }; 11A921D017DBCC7E00DDFD32 /* syn-cookie.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "syn-cookie.h"; sourceTree = ""; }; 11A921D117DBCC7E00DDFD32 /* templ-pkt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "templ-pkt.c"; sourceTree = ""; }; @@ -551,6 +551,8 @@ 118E114F25859CFD00618314 /* util */ = { isa = PBXGroup; children = ( + 11A921CD17DBCC7E00DDFD32 /* util-safefunc.c */, + 11A921CE17DBCC7E00DDFD32 /* util-safefunc.h */, 118D67C22B02CBA000271F7F /* util-errormsg.c */, 118D67C12B02CBA000271F7F /* util-errormsg.h */, 115C0CAA18035BC5004E6CD7 /* unusedparm.h */, @@ -806,8 +808,6 @@ 11A921CA17DBCC7E00DDFD32 /* smack1.c */, 11A921CB17DBCC7E00DDFD32 /* smackqueue.c */, 11A921CC17DBCC7E00DDFD32 /* smackqueue.h */, - 11A921CD17DBCC7E00DDFD32 /* string_s.c */, - 11A921CE17DBCC7E00DDFD32 /* string_s.h */, 11A9219317DBCC7E00DDFD32 /* event-timeout.c */, 11A9219417DBCC7E00DDFD32 /* event-timeout.h */, 11A9219517DBCC7E00DDFD32 /* util-logger.c */, @@ -950,7 +950,7 @@ 118D67F52B02DD6F00271F7F /* smack1.c in Sources */, 118D67F62B02DD6F00271F7F /* massip-rangesv4.c in Sources */, 118D67F72B02DD6F00271F7F /* smackqueue.c in Sources */, - 118D67F82B02DD6F00271F7F /* string_s.c in Sources */, + 118D67F82B02DD6F00271F7F /* util-safefunc.c in Sources */, 118D67F92B02DD6F00271F7F /* stack-ndpv6.c in Sources */, 118D67FA2B02DD6F00271F7F /* syn-cookie.c in Sources */, 118D67FB2B02DD6F00271F7F /* stack-arpv4.c in Sources */, @@ -1071,7 +1071,7 @@ 11A921F517DBCC7E00DDFD32 /* smack1.c in Sources */, 118B9B3225A00FA900F5FB0B /* massip-rangesv4.c in Sources */, 11A921F617DBCC7E00DDFD32 /* smackqueue.c in Sources */, - 11A921F717DBCC7E00DDFD32 /* string_s.c in Sources */, + 11A921F717DBCC7E00DDFD32 /* util-safefunc.c in Sources */, 11B6297A25995AA100D4786F /* stack-ndpv6.c in Sources */, 11A921F817DBCC7E00DDFD32 /* syn-cookie.c in Sources */, 1124DD7025B4FF3C00EEFC2C /* stack-arpv4.c in Sources */, From e5c501edc629d6b228cabcc121c513a196333c4a Mon Sep 17 00:00:00 2001 From: Robert David Graham Date: Thu, 16 Nov 2023 05:46:08 -0500 Subject: [PATCH 3/4] cleanup for mingw portability --- Makefile | 7 +++++++ src/main-conf.c | 5 ++--- src/pixie-threads.c | 3 ++- src/rawsock-getip6.c | 27 ++++++++++----------------- src/rawsock.c | 1 - src/stack-tcp-core.c | 15 +++++---------- src/stub-pfring.c | 9 ++++----- src/util-errormsg.c | 2 +- src/util-safefunc.c | 8 ++++++-- src/util-safefunc.h | 10 +++++----- vs10/masscan.vcxproj | 4 ++-- vs10/masscan.vcxproj.filters | 12 ++++++------ 12 files changed, 50 insertions(+), 53 deletions(-) diff --git a/Makefile b/Makefile index 9c94cfe7..c93c8d50 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,10 @@ +# If Windows, then assume the compiler is `gcc` for the +# MinGW environment. I can't figure out how to tell if it's +# actually MingGW. FIXME TODO +ifeq ($(OS),Windows_NT) + CC = gcc +endif + # Try to figure out the default compiler. I dont know the best # way to do this with `gmake`. If you have better ideas, please # submit a pull request on github. diff --git a/src/main-conf.c b/src/main-conf.c index dc120879..307ba857 100644 --- a/src/main-conf.c +++ b/src/main-conf.c @@ -427,11 +427,10 @@ static void ranges_from_file(struct RangeList *ranges, const char *filename) { FILE *fp; - errno_t err; unsigned line_number = 0; - err = fopen_s(&fp, filename, "rt"); - if (err) { + fp = fopen(filename, "rt"); + if (fp) { perror(filename); exit(1); /* HARD EXIT: because if it's an exclusion file, we don't * want to continue. We don't want ANY chance of diff --git a/src/pixie-threads.c b/src/pixie-threads.c index 48283a1b..f085d230 100644 --- a/src/pixie-threads.c +++ b/src/pixie-threads.c @@ -5,7 +5,8 @@ #include #include #endif -#if defined(__GNUC__) + +#if defined(__GNUC__) && !defined(WIN32) #include #include #include diff --git a/src/rawsock-getip6.c b/src/rawsock-getip6.c index cea4975f..f44dc973 100644 --- a/src/rawsock-getip6.c +++ b/src/rawsock-getip6.c @@ -81,10 +81,18 @@ rawsock_get_adapter_ipv6(const char *ifname) /***************************************************************************** *****************************************************************************/ #elif defined(WIN32) +/* From: + * https://stackoverflow.com/questions/10972794/undefined-reference-to-getadaptersaddresses20-but-i-included-liphlpapi + */ +#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x501 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x501 +#endif #define WIN32_LEAN_AND_MEAN #include #include #include + #ifdef _MSC_VER #pragma comment(lib, "IPHLPAPI.lib") #endif @@ -158,24 +166,9 @@ rawsock_get_adapter_ipv6(const char *ifname) if (addr->Flags == IP_ADAPTER_ADDRESS_TRANSIENT) continue; -#if 0 -/* My first attempt was to use the underlying approved operating system - * APIs to format this, rather than making assumptions about the - * structure of how internals might hold an IPv6 address. - * But it turns out, "inet_ntop()" has some portability - * issues. Thus, I'm not going to follow this path */ - /* Format as a string */ - inet_ntop(sa->sin6_family, &sa->sin6_addr, buf, sizeof(buf)); - - /* Convert to internal format */ - ipv6 = massip_parse_ipv6(buf); - if (ipv6address_is_invalid(ipv6)) { - fprintf(stderr, "[-] corrupt IPv6 address: %s\n", buf); - continue; - } -#else + /* Don't use operating-system function for this, because they aren't + * really portable or safe */ ipv6 = ipv6address_from_bytes((const unsigned char *)&sa->sin6_addr); -#endif if (addr->PrefixOrigin == IpPrefixOriginWellKnown) { /* This value applies to an IPv6 link-local address or an IPv6 loopback address */ diff --git a/src/rawsock.c b/src/rawsock.c index 721d850d..2d82d9f1 100755 --- a/src/rawsock.c +++ b/src/rawsock.c @@ -110,7 +110,6 @@ rawsock_init(void) /* variables used to print DHCP time info */ //struct tm newtime; //char buffer[32]; - //errno_t error; ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); pAdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof (IP_ADAPTER_INFO)); diff --git a/src/stack-tcp-core.c b/src/stack-tcp-core.c index f50467c2..911056de 100644 --- a/src/stack-tcp-core.c +++ b/src/stack-tcp-core.c @@ -240,7 +240,7 @@ state_to_string(int state) static void vLOGtcb(const struct TCP_Control_Block *tcb, int dir, const char *fmt, va_list marker) { - char sz[80]; + char sz[256]; ipaddress_formatted_t fmt1 = ipaddress_fmt(tcb->ip_them); snprintf(sz, sizeof(sz), "[%s:%u %4u,%4u] %s:%5u [%4u,%4u] {%s} ", @@ -872,8 +872,8 @@ tcpcon_destroy_tcb( * banners. */ { - struct stack_handle_t socket[1] = {tcpcon, tcb, 0, 0}; - banner_flush(socket); + struct stack_handle_t socket = {tcpcon, tcb, 0, 0}; + banner_flush(&socket); } LOGtcb(tcb, 2, "--DESTROYED--\n"); @@ -1168,11 +1168,6 @@ tcpcon_send_packet( * from a receive-thread. Therefore, instead of transmiting ourselves, * we hae to queue it up for later transmission. */ stack_transmit_packetbuffer(tcpcon->stack, response); - - if ((tcp_flags & 0x01) == 0x10) { - LOGtcb(tcb, 0, "xmit FIN myseqno=%u\n", tcb->seqno_me-tcb->seqno_me_first + payload_length); - } - } /*************************************************************************** @@ -1326,10 +1321,10 @@ application_notify(struct TCP_ConnectionTable *tcpcon, { struct Banner1 *banner1 = tcpcon->banner1; const struct ProtocolParserStream *stream = tcb->stream; - struct stack_handle_t socket[1] = { + struct stack_handle_t socket = { tcpcon, tcb, secs, usecs}; - return application_event(socket, + return application_event(&socket, tcb->app_state, event, stream, banner1, payload, payload_length diff --git a/src/stub-pfring.c b/src/stub-pfring.c index 00e39a2c..bf415e12 100644 --- a/src/stub-pfring.c +++ b/src/stub-pfring.c @@ -27,12 +27,11 @@ PFRING_is_installed(void) { #if defined(__linux__) FILE *fp; - int err; char line[256]; int found = 0; - err = fopen_s(&fp, "/proc/modules", "rb"); - if (err) + fp = fopen("/proc/modules", "rb"); + if (fp == NULL) return 0; while (fgets(line, sizeof(line), fp)) { @@ -67,12 +66,12 @@ PFRING_init(void) LOG(6, "pfring: looking for 'libpfring.so'\n"); h = dlopen("libpfring.so", RTLD_LAZY); if (h == NULL) { - LOG(2, "pfring: error: dlopen('libpfring.so'): %s\n", strerror_x(errno)); + LOG(2, "pfring: error: dlopen('libpfring.so'): %s\n", strerror(errno)); return 0; } else LOG(2, "pfring: found 'libpfring.so'!\n"); -#define LOADSYM(name) if ((PFRING.name = dlsym(h, "pfring_"#name)) == 0) {LOG(2, "pfring_%s: not found in 'libpfring.so': %s\n", #name, strerror_x(errno));err=1;} +#define LOADSYM(name) if ((PFRING.name = dlsym(h, "pfring_"#name)) == 0) {LOG(2, "pfring_%s: not found in 'libpfring.so': %s\n", #name, strerror(errno));err=1;} LOADSYM(open); LOADSYM(close); LOADSYM(enable_ring); diff --git a/src/util-errormsg.c b/src/util-errormsg.c index 3552f817..6d32156a 100644 --- a/src/util-errormsg.c +++ b/src/util-errormsg.c @@ -9,7 +9,7 @@ #endif -static unsigned long long _entropy = (unsigned long long)&_entropy; +static unsigned long long _entropy; void errmsg_init(unsigned long long in_entropy) { _entropy = in_entropy; diff --git a/src/util-safefunc.c b/src/util-safefunc.c index 73d32863..b500befd 100644 --- a/src/util-safefunc.c +++ b/src/util-safefunc.c @@ -65,7 +65,8 @@ safe_strcpy(char *dst, size_t sizeof_dst, const char *src) -errno_t safe_localtime(struct tm* _tm, const time_t *time) +int +safe_localtime(struct tm* _tm, const time_t *time) { struct tm *x; @@ -78,7 +79,10 @@ errno_t safe_localtime(struct tm* _tm, const time_t *time) return 0; } -errno_t safe_gmtime(struct tm* _tm, const time_t *time) + + +int +safe_gmtime(struct tm* _tm, const time_t *time) { struct tm *x; diff --git a/src/util-safefunc.h b/src/util-safefunc.h index 3f06ed8c..84d6cdb6 100644 --- a/src/util-safefunc.h +++ b/src/util-safefunc.h @@ -59,8 +59,8 @@ * `strlcpy()` in glibc. */ void safe_strcpy(char *dst, size_t sizeof_dst, const char *src); -errno_t safe_localtime(struct tm* _tm, const time_t *time); -errno_t safe_gmtime(struct tm* _tm, const time_t *time); +int safe_localtime(struct tm* _tm, const time_t *time); +int safe_gmtime(struct tm* _tm, const time_t *time); #if defined(_MSC_VER) && (_MSC_VER >= 1900) @@ -79,6 +79,8 @@ errno_t safe_gmtime(struct tm* _tm, const time_t *time); /*Visual Studio 2010*/ # include # include +#pragma warning(disable: 4996) +#define snprintf _snprintf # define strcasecmp _stricmp # define memcasecmp _memicmp # ifndef PRIu64 @@ -94,12 +96,10 @@ errno_t safe_gmtime(struct tm* _tm, const time_t *time); # define strcasecmp _stricmp # define memcasecmp _memicmp # define vsnprintf _vsnprintf - typedef int errno_t; #elif defined(__GNUC__) && (__GNUC__ >= 4) #include - int memcasecmp(const void *lhs, const void *rhs, size_t length); - typedef int errno_t; + int memcasecmp(const void *lhs, const void *rhs, size_t length);; #else # warning unknown compiler diff --git a/vs10/masscan.vcxproj b/vs10/masscan.vcxproj index ced92e99..24bd0379 100644 --- a/vs10/masscan.vcxproj +++ b/vs10/masscan.vcxproj @@ -115,7 +115,6 @@ - @@ -128,6 +127,7 @@ + @@ -215,7 +215,6 @@ - @@ -230,6 +229,7 @@ + diff --git a/vs10/masscan.vcxproj.filters b/vs10/masscan.vcxproj.filters index 9919a7f1..a0b6d84d 100644 --- a/vs10/masscan.vcxproj.filters +++ b/vs10/masscan.vcxproj.filters @@ -144,9 +144,6 @@ Source Files\misc - - Source Files\misc - Source Files\misc @@ -396,6 +393,9 @@ Source Files\util + + Source Files\util + @@ -470,9 +470,6 @@ Source Files\misc - - Source Files\misc - Source Files\proto @@ -689,6 +686,9 @@ Source Files\util + + Source Files\util + From 09ff4df9fdb13e435b89fdd2cdb678d182701362 Mon Sep 17 00:00:00 2001 From: Robert David Graham Date: Thu, 16 Nov 2023 18:28:41 -0800 Subject: [PATCH 4/4] Possible fix for issue #734, fixing MinGW problems on Windows --- .gitignore | 1 + Makefile | 2 +- src/main-conf.c | 4 ++++ src/out-binary.c | 4 ++-- src/out-grepable.c | 2 +- src/output.c | 4 +++- src/pixie-timer.c | 2 +- src/rawsock-getif.c | 9 +++++++++ src/rawsock-getip.c | 9 +++++++++ src/rawsock-getmac.c | 8 ++++++++ src/rawsock-getroute.c | 9 +++++++++ src/read-service-probes.c | 5 ++++- src/stub-lua.c | 5 ++++- src/util-safefunc.h | 8 ++++++-- 14 files changed, 62 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 642828d0..94dc49c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ paused.conf .Makefile.swp .vscode +vs10/.vs/ diff --git a/Makefile b/Makefile index c93c8d50..866be22d 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ endif ifneq (, $(findstring mingw, $(SYS))) INCLUDES = -Ivs10/include LIBS = -L vs10/lib -lIPHLPAPI -lWs2_32 -FLAGS2 = -march=i686 +#FLAGS2 = -march=i686 endif # Cygwin diff --git a/src/main-conf.c b/src/main-conf.c index 307ba857..f0aaa5a1 100644 --- a/src/main-conf.c +++ b/src/main-conf.c @@ -100,6 +100,10 @@ print_version() ); printf("Compiled on: %s %s\n", __DATE__, __TIME__); +#if defined(__x86_64) || defined(__x86_64__) + cpu = "x86"; +#endif + #if defined(_MSC_VER) #if defined(_M_AMD64) || defined(_M_X64) cpu = "x86"; diff --git a/src/out-binary.c b/src/out-binary.c index d0dfac5b..31a03e03 100644 --- a/src/out-binary.c +++ b/src/out-binary.c @@ -103,8 +103,8 @@ static void binary_out_status_ipv6(struct Output *out, FILE *fp, time_t timestamp, int status, ipaddress ip, unsigned ip_proto, unsigned port, unsigned reason, unsigned ttl) { - unsigned char buf[256]; - size_t max = sizeof(buf); + unsigned char buf[256+1]; + size_t max = sizeof(buf-1); size_t offset = 0; size_t bytes_written; diff --git a/src/out-grepable.c b/src/out-grepable.c index 0a4887f3..30d89fc3 100644 --- a/src/out-grepable.c +++ b/src/out-grepable.c @@ -154,7 +154,7 @@ grepable_out_status(struct Output *out, FILE *fp, time_t timestamp, else service = oproto_service_name(ip_proto); - fprintf(fp, "Timestamp: %lu", timestamp); + fprintf(fp, "Timestamp: %llu", (unsigned long long)timestamp); fmt = ipaddress_fmt(ip); fprintf(fp, "\tHost: %s ()", fmt.string); diff --git a/src/output.c b/src/output.c index 5a1fa669..f563a4c9 100644 --- a/src/output.c +++ b/src/output.c @@ -34,7 +34,6 @@ #include "output.h" #include "masscan.h" #include "masscan-status.h" -#include "util-safefunc.h" #include "proto-banner1.h" #include "masscan-app.h" #include "main-globals.h" @@ -48,6 +47,9 @@ #include #include +/* Put this at the bottom of the include lists because of warnings */ +#include "util-safefunc.h" + /***************************************************************************** *****************************************************************************/ diff --git a/src/pixie-timer.c b/src/pixie-timer.c index d08afebd..cccd9c58 100644 --- a/src/pixie-timer.c +++ b/src/pixie-timer.c @@ -55,7 +55,7 @@ getFILETIMEoffset(void) } int -clock_gettime(int X, struct timeval *tv) +win_clock_gettime(int X, struct timeval *tv) { LARGE_INTEGER t; FILETIME f; diff --git a/src/rawsock-getif.c b/src/rawsock-getif.c index 860cb2f2..d30da401 100755 --- a/src/rawsock-getif.c +++ b/src/rawsock-getif.c @@ -394,6 +394,15 @@ int rawsock_get_default_interface(char *ifname, size_t sizeof_ifname) #if defined(WIN32) +/* From: + * https://stackoverflow.com/questions/10972794/undefined-reference-to-getadaptersaddresses20-but-i-included-liphlpapi + * I think this fixed issue #734 + */ +#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x501 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x501 +#endif + #include #include #include "massip-parse.h" diff --git a/src/rawsock-getip.c b/src/rawsock-getip.c index ef0ad4a1..9a759997 100644 --- a/src/rawsock-getip.c +++ b/src/rawsock-getip.c @@ -57,6 +57,15 @@ rawsock_get_adapter_ip(const char *ifname) /***************************************************************************** *****************************************************************************/ #elif defined(WIN32) + /* From: + * https://stackoverflow.com/questions/10972794/undefined-reference-to-getadaptersaddresses20-but-i-included-liphlpapi + * I think this fixes issue #734 + */ +#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x501 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x501 +#endif + #include #include #ifdef _MSC_VER diff --git a/src/rawsock-getmac.c b/src/rawsock-getmac.c index 15ef70f9..bc44d139 100644 --- a/src/rawsock-getmac.c +++ b/src/rawsock-getmac.c @@ -76,6 +76,14 @@ rawsock_get_adapter_mac(const char *ifname, unsigned char *mac) /***************************************************************************** *****************************************************************************/ #elif defined(WIN32) + /* From: + * https://stackoverflow.com/questions/10972794/undefined-reference-to-getadaptersaddresses20-but-i-included-liphlpapi + * I think this fixes issue #734 + */ +#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x501 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x501 +#endif #include #include #ifdef _MSC_VER diff --git a/src/rawsock-getroute.c b/src/rawsock-getroute.c index e9b1b538..639233ac 100644 --- a/src/rawsock-getroute.c +++ b/src/rawsock-getroute.c @@ -424,6 +424,15 @@ int rawsock_get_default_gateway(const char *ifname, unsigned *ipv4) #if defined(WIN32) +/* From: + * https://stackoverflow.com/questions/10972794/undefined-reference-to-getadaptersaddresses20-but-i-included-liphlpapi + * I think this fixes issue #734 + */ +#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x501 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x501 +#endif + #include #include #ifdef _MSC_VER diff --git a/src/read-service-probes.c b/src/read-service-probes.c index 1a18b896..f0cd96a7 100644 --- a/src/read-service-probes.c +++ b/src/read-service-probes.c @@ -8,8 +8,11 @@ #include #include -#if defined(WIN32) +#ifdef _MSC_VER #pragma warning(disable:4996) +#endif + +#if defined(WIN32) #define strncasecmp _strnicmp #endif diff --git a/src/stub-lua.c b/src/stub-lua.c index 8059c888..c4cef6d8 100644 --- a/src/stub-lua.c +++ b/src/stub-lua.c @@ -1,10 +1,13 @@ #define LUAAPI #include "stub-lua.h" +#ifdef _MSC_VER +#pragma warning(disable: 4133 4113 4047) +#endif + #if defined(WIN32) #define WIN32_LEAN_AND_MEAN #include -#pragma warning(disable: 4133 4113 4047) #else #include #endif diff --git a/src/util-safefunc.h b/src/util-safefunc.h index 84d6cdb6..80662c04 100644 --- a/src/util-safefunc.h +++ b/src/util-safefunc.h @@ -21,11 +21,15 @@ #include #include +#ifdef _MSC_VER +#pragma warning(disable: 4996) +#endif + #undef strcpy #define strcpy STRCPY_FUNCTION_IS_BAD -#undef strncpy -#define strncpy STRNCPY_FUNCTION_IS_BAD +/*#undef strncpy +#define strncpy STRNCPY_FUNCTION_IS_BAD*/ #undef strcat #define strcat STRCAT_FUNCTION_IS_BAD