From 7ada635d782e5e7db6f404918b68da29f75e2e1d Mon Sep 17 00:00:00 2001 From: chauhand2463 Date: Thu, 29 Oct 2026 19:21:03 +0000 Subject: [PATCH] Fix MariaDB unauthorized version detection --- Makefile.in | 4 +- nmap-service-probes | 5 +- tests/service_probe_match_test.cc | 139 ++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 tests/service_probe_match_test.cc diff --git a/Makefile.in b/Makefile.in index 48f39f5f8..6b18911c5 100644 --- a/Makefile.in +++ b/Makefile.in @@ -271,7 +271,7 @@ clean-nping: -cd $(NPINGDIR) && $(MAKE) clean clean-tests: - @rm -f tests/nmap_dns_test tests/expr_match_test + @rm -f tests/nmap_dns_test tests/expr_match_test tests/service_probe_match_test distclean-pcap: -cd $(LIBPCAPDIR) && $(MAKE) distclean @@ -461,7 +461,7 @@ check-nsock: check-zenmap: @cd $(ZENMAPDIR)/test && $(PYTHON) run_tests.py -check-nmap: tests/nmap_dns_test tests/expr_match_test tests/cstring_unescape_test +check-nmap: tests/nmap_dns_test tests/expr_match_test tests/cstring_unescape_test tests/service_probe_match_test for test in $^; do ./$$test; done check: @NCAT_CHECK@ @NSOCK_CHECK@ @ZENMAP_CHECK@ @NSE_CHECK@ @NDIFF_CHECK@ check-nmap diff --git a/nmap-service-probes b/nmap-service-probes index bb057afcd..c02d3ad62 100644 --- a/nmap-service-probes +++ b/nmap-service-probes @@ -2231,9 +2231,8 @@ match mysqlx m|^\x05\0\0\0\x0b\x08\x05\x1a\0| p/MySQL X protocol listener/ cpe:/ # MySQL Handshake packet ( .\0\0\0\x0a ) reference - http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::Handshake # Error packet ( .\0\0\0\xff ) reference - http://dev.mysql.com/doc/internals/en/packet-ERR_Packet.html#cs-packet-err-header match mysql m|^.?\0\0\0\xff..Host .* is not allowed to connect to this MySQL server$|s p/MySQL/ i/unauthorized/ cpe:/a:mysql:mysql/ -match mysql m|^.\0\0\0\xff..Host .* is not allowed to connect to this MariaDB server$|s p/MariaDB/ v/10.3.23 or earlier/ i/unauthorized/ cpe:/a:mariadb:mariadb/ -# https://jira.mariadb.org/browse/MDEV-21101 -match mysql m|^.\0\0\x01\xff..Host .* is not allowed to connect to this MariaDB server$|s p/MariaDB/ v/10.3.24 or later/ i/unauthorized/ cpe:/a:mariadb:mariadb/ +match mysql m|^.\0\0\0\xff..Host .* is not allowed to connect to this MariaDB server$|s p/MariaDB/ i/unauthorized/ cpe:/a:mariadb:mariadb/ +match mysql m|^.\0\0\x01\xff..Host .* is not allowed to connect to this MariaDB server$|s p/MariaDB/ i/unauthorized/ cpe:/a:mariadb:mariadb/ match mysql m|^.\0\0\0\xff..Too many connections|s p/MySQL/ i/Too many connections/ cpe:/a:mysql:mysql/ match mysql m|^.\0\0\0\xff..Host .* is blocked because of many connection errors|s p/MySQL/ i/blocked - too many connection errors/ cpe:/a:mysql:mysql/ match mysql m|^.\0\0\x01\xff..Host .* is blocked because of many connection errors|s p/MariaDB/ v/10.3.24 or later/ i/blocked - too many connection errors/ cpe:/a:mariadb:mariadb/ diff --git a/tests/service_probe_match_test.cc b/tests/service_probe_match_test.cc new file mode 100644 index 000000000..0d6d7666d --- /dev/null +++ b/tests/service_probe_match_test.cc @@ -0,0 +1,139 @@ +/*************************************************************************** + * service_probe_match_test.cc -- Tests MariaDB unauthorized probe matches * + * * + ***********************IMPORTANT NMAP LICENSE TERMS************************ + * + * The Nmap Security Scanner is (C) 1996-2026 Nmap Software LLC ("The Nmap + * Project"). Nmap is also a registered trademark of the Nmap Project. + * + * This program is distributed under the terms of the Nmap Public Source + * License (NPSL). The exact license text applying to a particular Nmap + * release or source code control revision is contained in the LICENSE + * file distributed with that version of Nmap or source code control + * revision. More Nmap copyright/legal information is available from + * https://nmap.org/book/man-legal.html, and further information on the + * NPSL license itself can be found at https://nmap.org/npsl/ . This + * header summarizes some key points from the Nmap license, but is no + * substitute for the actual license text. + * + * Nmap is generally free for end users to download and use themselves, + * including commercial use. It is available from https://nmap.org. + * + * The Nmap license generally prohibits companies from using and + * redistributing Nmap in commercial products, but we sell a special Nmap + * OEM Edition with a more permissive license and special features for + * this purpose. See https://nmap.org/oem/ + * + * If you have received a written Nmap license agreement or contract + * stating terms other than these (such as an Nmap OEM license), you may + * choose to use and redistribute Nmap under those terms instead. + * + * The official Nmap Windows builds include the Npcap software + * (https://npcap.com) for packet capture and transmission. It is under + * separate license terms which forbid redistribution without special + * permission. So the official Nmap Windows builds may not be redistributed + * without special permission (such as an Nmap OEM license). + * + * Source is provided to this software because we believe users have a + * right to know exactly what a program is going to do before they run it. + * This also allows you to audit the software for security holes. + * + * Source code also allows you to port Nmap to new platforms, fix bugs, and + * add new features. You are highly encouraged to submit your changes as a + * Github PR or by email to the dev@nmap.org mailing list for possible + * incorporation into the main distribution. Unless you specify otherwise, it + * is understood that you are offering us very broad rights to use your + * submissions as described in the Nmap Public Source License Contributor + * Agreement. This is important because we fund the project by selling licenses + * with various terms, and also because the inability to relicense code has + * caused devastating problems for other Free Software projects (such as KDE + * and NASM). + * + * The free version of Nmap is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties, + * indemnification and commercial support are all available through the + * Npcap OEM program--see https://nmap.org/oem/ + * + ***************************************************************************/ + +#include "../nmap.h" +#include "../service_scan.h" + +#include +#include +#include + +namespace { +int failures = 0; + +void expect(bool condition, const char *description) { + if (!condition) { + std::cout << "FAIL: " << description << std::endl; + failures++; + } +} + +std::string error_packet(unsigned char sequence, unsigned int error_code, + const char *message) { + const size_t message_len = std::strlen(message); + const size_t payload_len = 3 + message_len; + std::string packet(4 + payload_len, '\0'); + + packet[0] = static_cast(payload_len & 0xff); + packet[1] = static_cast((payload_len >> 8) & 0xff); + packet[2] = static_cast((payload_len >> 16) & 0xff); + packet[3] = static_cast(sequence); + packet[4] = static_cast(0xff); + packet[5] = static_cast(error_code & 0xff); + packet[6] = static_cast((error_code >> 8) & 0xff); + std::memcpy(&packet[7], message, message_len); + return packet; +} + +void test_unauthorized(ServiceProbe *probe, unsigned char sequence) { + const char *message = + "Host '127.0.0.1' is not allowed to connect to this MariaDB server"; + const std::string packet = error_packet(sequence, 1130, message); + const MatchDetails *match = probe->testMatch( + reinterpret_cast(packet.data()), + static_cast(packet.size()), 0); + + expect(match != NULL, "unauthorized response should match"); + if (match == NULL) { + return; + } + expect(match->serviceName != NULL && + std::strcmp(match->serviceName, "mysql") == 0, + "unauthorized response should match the mysql probe"); + expect(match->product != NULL && + std::strcmp(match->product, "MariaDB") == 0, + "unauthorized response should identify MariaDB"); + expect(match->info != NULL && + std::strcmp(match->info, "unauthorized") == 0, + "unauthorized response should report unauthorized"); + expect(match->version == NULL || match->version[0] == '\0', + "unauthorized response should not report a version"); + expect(match->cpe_a != NULL && + std::strncmp(match->cpe_a, "cpe:/a:mariadb:mariadb", + sizeof("cpe:/a:mariadb:mariadb") - 1) == 0, + "unauthorized response should report the MariaDB CPE"); +} +} // namespace + +int main(int argc, char **argv) { + const char *probes = argc > 1 ? argv[1] : "nmap-service-probes"; + AllProbes all_probes; + parse_nmap_service_probe_file(&all_probes, probes); + ServiceProbe *probe = all_probes.getProbeByName("NULL", IPPROTO_TCP); + if (probe == NULL) { + std::cout << "FAIL: NULL probe not found" << std::endl; + return 1; + } + + test_unauthorized(probe, 0x00); + test_unauthorized(probe, 0x01); + + std::cout << "Ran 2 tests. " << failures << " failures." << std::endl; + return failures == 0 ? 0 : 1; +}