From 9530a70ce99b40f740babc0d64bd78ba13a81b0a Mon Sep 17 00:00:00 2001 From: Michael Douglas Date: Tue, 10 Nov 2026 09:05:01 -0300 Subject: [PATCH] feat: v0.6.0 - major service detection upgrade - Add 88 unit tests across banner, tls, output, ports, and sigs modules - Show extra field in terminal output and fix all output writers (XML, grep, NDJSON, normal) - Reorder probes: NULL (1s) -> HTTP -> Redis/PostgreSQL for faster detection - Fix false Redis detection on non-Redis ports (validate RESP format) - TLS: add ALPN negotiation (h2/http1.1), SAN extraction from certificates, TLS 1.3 cipher suites - Add 6 new protocol probes: Memcached, MongoDB, MQTT, AMQP, RDP, SMB - Add HTTP fingerprints for Elasticsearch and Docker API - Enhanced -sC scripts: OpenAPI, docs, 404 fingerprint, reverse proxy detection - Data-driven signatures via @embedFile: 5 banner sigs + 30 HTTP app sigs (comptime parsed) - Auto-probe /openapi.json on FastAPI services to extract app title and version - FastAPI detection now shows actual service name (e.g. "ProStaff ML 2.0.0") --- .gitignore | 3 + build.zig | 5 + src/banner.zig | 939 +++++++++++++++++++++++++++++++++++++--- src/data/service-probes | 56 +++ src/main.zig | 18 +- src/output.zig | 57 ++- src/scripts.zig | 200 ++++++++- src/sigs.zig | 216 +++++++++ src/tls.zig | 193 ++++++++- 9 files changed, 1593 insertions(+), 94 deletions(-) create mode 100644 src/data/service-probes create mode 100644 src/sigs.zig diff --git a/.gitignore b/.gitignore index 79aa23a..acd00f5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ zig-out/ .zig-cache/ zig-cache/ +# Dev docs (local only) +devdocs/ + # Scan reports and internal docs (may contain target data) docs/reports/ docs/archive/ diff --git a/build.zig b/build.zig index 00888f7..4f4eaef 100644 --- a/build.zig +++ b/build.zig @@ -30,6 +30,11 @@ pub fn build(b: *std.Build) void { "src/cookie.zig", "src/blackrock.zig", "src/throttle.zig", + "src/banner.zig", + "src/tls.zig", + "src/output.zig", + "src/ports.zig", + "src/sigs.zig", }; for (test_modules) |mod| { diff --git a/src/banner.zig b/src/banner.zig index 71c982f..b8b21ef 100644 --- a/src/banner.zig +++ b/src/banner.zig @@ -1,6 +1,7 @@ const std = @import("std"); const rx_mod = @import("rx.zig"); const tls_mod = @import("tls.zig"); +const sigs = @import("sigs.zig"); const SO_RCVTIMEO = 20; const SO_SNDTIMEO = 21; @@ -73,6 +74,12 @@ pub fn detect(ip: u32, port: u16) ServiceInfo { .redis => probeRedis(ip, port), .postgresql => probePostgresql(ip, port), .tls => probeTls(ip, port), + .memcached => probeMemcached(ip, port), + .mongodb => probeMongodb(ip, port), + .mqtt => probeMqtt(ip, port), + .amqp => probeAmqp(ip, port), + .rdp => probeRdp(ip, port), + .smb => probeSmb(ip, port), .generic => probeGeneric(ip, port), }; } @@ -136,7 +143,23 @@ pub const grab = detect; // Port classification // --------------------------------------------------------------------------- -const Protocol = enum { http, ssh, ftp, smtp, mysql, redis, postgresql, tls, generic }; +const Protocol = enum { + http, + ssh, + ftp, + smtp, + mysql, + redis, + postgresql, + tls, + memcached, + mongodb, + mqtt, + amqp, + rdp, + smb, + generic, +}; fn classifyPort(port: u16) Protocol { return switch (port) { @@ -148,6 +171,12 @@ fn classifyPort(port: u16) Protocol { 3306, 33060 => .mysql, 6379...6399 => .redis, 5432...5434 => .postgresql, + 11211 => .memcached, + 27017...27019 => .mongodb, + 1883, 8883 => .mqtt, + 5672 => .amqp, + 3389 => .rdp, + 445 => .smb, else => .generic, }; } @@ -196,9 +225,49 @@ fn probeHttp(ip: u32, port: u16) ServiceInfo { fingerprintHttp(data, &info); + if (std.mem.eql(u8, info.name(), "uvicorn") and std.mem.eql(u8, info.extra(), "FastAPI")) { + probeFastapiOpenapi(ip, port, &info); + } + + if (std.mem.eql(u8, info.name(), "http")) { + if (sigs.matchHttp(data)) |m| { + info.setName(m.name); + info.version_len = 0; + } + } + + if (info.extra_len == 0 and std.mem.eql(u8, info.name(), "http")) { + storeHttpEvidence(data, &info); + } + return info; } +fn storeHttpEvidence(data: []const u8, info: *ServiceInfo) void { + var buf: [128]u8 = undefined; + var len: usize = 0; + + if (data.len >= 12 and std.mem.startsWith(u8, data, "HTTP/")) { + const written = std.fmt.bufPrint(buf[len..], "{s}", .{data[9..12]}) catch return; + len += written.len; + } + if (findHeaderValue(data, "content-type:")) |ct| { + const semi = std.mem.indexOfScalar(u8, ct, ';') orelse ct.len; + const short = ct[0..@min(semi, 32)]; + const written = std.fmt.bufPrint(buf[len..], ", {s}", .{short}) catch return; + len += written.len; + } + const body_start = if (std.mem.indexOf(u8, data, "\r\n\r\n")) |p| p + 4 else data.len; + if (body_start < data.len) { + const body_len = data.len - body_start; + const written = std.fmt.bufPrint(buf[len..], ", {d}B", .{body_len}) catch return; + len += written.len; + } + if (len > 0) { + info.setExtra(buf[0..len]); + } +} + fn probeSsh(ip: u32, port: u16) ServiceInfo { var info = ServiceInfo{}; const fd = openSocket(ip, port) orelse return info; @@ -382,14 +451,20 @@ fn probeRedis(ip: u32, port: u16) ServiceInfo { const data = buf[0..n]; - if (data[0] == '-') { + if (std.mem.startsWith(u8, data, "-NOAUTH") or std.mem.startsWith(u8, data, "-DENIED")) { + info.setName("Redis"); + info.setExtra("auth required"); + return info; + } + + if (std.mem.startsWith(u8, data, "-ERR")) { info.setName("Redis"); - if (std.mem.indexOf(u8, data, "NOAUTH")) |_| { - info.setExtra("auth required"); - } return info; } + if (data[0] != '$' or !std.mem.startsWith(u8, data, "$")) return info; + if (std.mem.indexOf(u8, data, "redis_version:") == null) return info; + info.setName("Redis"); if (std.mem.indexOf(u8, data, "redis_version:")) |pos| { @@ -506,6 +581,8 @@ fn probeTls(ip: u32, port: u16) ServiceInfo { const cipher = tls_info.cipherStr(); const expiry = tls_info.notAfter(); + const alpn_str = tls_info.alpn(); + var extra: [256]u8 = undefined; var elen: usize = 0; @@ -513,6 +590,10 @@ fn probeTls(ip: u32, port: u16) ServiceInfo { const written = std.fmt.bufPrint(extra[elen..], "{s}", .{ver}) catch ""; elen += written.len; } + if (alpn_str.len > 0) { + const written = std.fmt.bufPrint(extra[elen..], ", {s}", .{alpn_str}) catch ""; + elen += written.len; + } if (cipher.len > 0) { const written = std.fmt.bufPrint(extra[elen..], " {s}", .{cipher}) catch ""; elen += written.len; @@ -540,98 +621,494 @@ fn probeTls(ip: u32, port: u16) ServiceInfo { return info; } -fn probeGeneric(ip: u32, port: u16) ServiceInfo { +fn probeMemcached(ip: u32, port: u16) ServiceInfo { var info = ServiceInfo{}; const fd = openSocket(ip, port) orelse return info; defer std.posix.close(fd); - var buf: [1024]u8 = undefined; - const n = readWithTimeout(fd, &buf, 500_000_000); - if (n > 0) { - const line_end = std.mem.indexOfAny(u8, buf[0..n], "\r\n") orelse @min(n, 128); - const line = buf[0..line_end]; + _ = std.posix.write(fd, "version\r\n") catch return info; + var buf: [256]u8 = undefined; + const n = std.posix.read(fd, &buf) catch return info; + if (n < 8) return info; + + const data = buf[0..n]; + if (std.mem.startsWith(u8, data, "VERSION ")) { + info.setName("Memcached"); + const end = std.mem.indexOfAny(u8, data[8..], "\r\n") orelse (n - 8); + info.setVersion(data[8 .. 8 + end]); + } + _ = std.posix.write(fd, "quit\r\n") catch {}; + return info; +} + +fn probeMongodb(ip: u32, port: u16) ServiceInfo { + var info = ServiceInfo{}; + const fd = openSocket(ip, port) orelse return info; + defer std.posix.close(fd); + + // OP_QUERY: isMaster command on admin.$cmd + const bson_doc = [_]u8{ + 0x15, 0x00, 0x00, 0x00, // doc size = 21 + 0x10, // type: int32 + 'i', 's', 'M', 'a', 's', 't', 'e', 'r', 0x00, // key + 0x01, 0x00, 0x00, 0x00, // value = 1 + 0x00, // doc terminator + }; + const coll = "admin.$cmd\x00"; + const payload_len = 4 + coll.len + 4 + 4 + bson_doc.len; + const msg_len: u32 = @intCast(16 + payload_len); - if (std.mem.startsWith(u8, line, "SSH-")) { - return probeSshFromBanner(line); + var msg: [128]u8 = .{0} ** 128; + var pos: usize = 0; + std.mem.writeInt(u32, msg[pos..][0..4], msg_len, .little); + pos += 4; + std.mem.writeInt(u32, msg[pos..][0..4], 1, .little); + pos += 4; // requestID + pos += 4; // responseTo = 0 + std.mem.writeInt(u32, msg[pos..][0..4], 2004, .little); + pos += 4; // OP_QUERY + pos += 4; // flags = 0 + @memcpy(msg[pos .. pos + coll.len], coll); + pos += coll.len; + pos += 4; // numberToSkip = 0 + std.mem.writeInt(u32, msg[pos..][0..4], @as(u32, 0xFFFFFFFF), .little); + pos += 4; // numberToReturn + @memcpy(msg[pos .. pos + bson_doc.len], &bson_doc); + pos += bson_doc.len; + + _ = std.posix.write(fd, msg[0..pos]) catch return info; + + var buf: [4096]u8 = undefined; + const n = std.posix.read(fd, &buf) catch return info; + if (n < 36) return info; + + const op = std.mem.readInt(u32, buf[12..16], .little); + if (op != 1) return info; + + info.setName("MongoDB"); + if (findBsonString(buf[36..n], "version")) |ver| { + info.setVersion(ver); + } + return info; +} + +fn findBsonString(data: []const u8, key: []const u8) ?[]const u8 { + if (data.len < 5) return null; + var pos: usize = 4; + while (pos < data.len) { + const btype = data[pos]; + if (btype == 0) break; + pos += 1; + const key_end = std.mem.indexOfScalarPos(u8, data, pos, 0) orelse break; + const found_key = data[pos..key_end]; + pos = key_end + 1; + + if (btype == 0x02 and std.mem.eql(u8, found_key, key)) { + if (pos + 4 > data.len) break; + const str_len = std.mem.readInt(u32, data[pos..][0..4], .little); + pos += 4; + if (str_len == 0 or pos + str_len > data.len) break; + return data[pos .. pos + str_len - 1]; + } else { + pos = skipBsonValue(data, pos, btype) orelse break; } + } + return null; +} - if (line.len > 4 and isDigit(line[0]) and isDigit(line[1]) and isDigit(line[2]) and (line[3] == ' ' or line[3] == '-')) { - info.setVersion(line[4..]); - return info; +fn skipBsonValue(data: []const u8, start: usize, btype: u8) ?usize { + const pos = start; + switch (btype) { + 0x01 => return if (pos + 8 <= data.len) pos + 8 else null, // double + 0x02, 0x0D, 0x0E => { // string, JS, symbol + if (pos + 4 > data.len) return null; + const len = std.mem.readInt(u32, data[pos..][0..4], .little); + return if (pos + 4 + len <= data.len) pos + 4 + len else null; + }, + 0x03, 0x04 => { // document, array + if (pos + 4 > data.len) return null; + const len = std.mem.readInt(u32, data[pos..][0..4], .little); + return if (pos + len <= data.len) pos + len else null; + }, + 0x05 => { // binary + if (pos + 5 > data.len) return null; + const len = std.mem.readInt(u32, data[pos..][0..4], .little); + return if (pos + 5 + len <= data.len) pos + 5 + len else null; + }, + 0x07 => return if (pos + 12 <= data.len) pos + 12 else null, // ObjectId + 0x08 => return if (pos + 1 <= data.len) pos + 1 else null, // bool + 0x09 => return if (pos + 8 <= data.len) pos + 8 else null, // datetime + 0x0A => return pos, // null + 0x10 => return if (pos + 4 <= data.len) pos + 4 else null, // int32 + 0x11, 0x12 => return if (pos + 8 <= data.len) pos + 8 else null, // timestamp, int64 + else => return null, + } +} + +fn probeMqtt(ip: u32, port: u16) ServiceInfo { + var info = ServiceInfo{}; + const fd = openSocket(ip, port) orelse return info; + defer std.posix.close(fd); + + const connect_pkt = [_]u8{ + 0x10, 0x10, // CONNECT, remaining length 16 + 0x00, 0x04, 'M', 'Q', 'T', 'T', // protocol name + 0x04, // protocol level 4 (MQTT 3.1.1) + 0x02, // connect flags (clean session) + 0x00, 0x3C, // keepalive 60s + 0x00, 0x04, 'o', 'h', 'm', 'z', // client ID + }; + _ = std.posix.write(fd, &connect_pkt) catch return info; + + var buf: [64]u8 = undefined; + const n = std.posix.read(fd, &buf) catch return info; + if (n < 2) return info; + + if (buf[0] == 0x20) { + info.setName("MQTT"); + if (n >= 4) { + const rc = buf[3]; + const status: []const u8 = switch (rc) { + 0 => "connected", + 4 => "bad-credentials", + 5 => "not-authorized", + else => "refused", + }; + info.setExtra(status); } + } + _ = std.posix.write(fd, &[_]u8{ 0xE0, 0x00 }) catch {}; + return info; +} - // MySQL greeting: packet starts with protocol version byte 10 (0x0a) - if (n >= 5 and buf[3] == 0 and buf[4] == 0x0a) { - return probeMysql(ip, port); +fn probeAmqp(ip: u32, port: u16) ServiceInfo { + var info = ServiceInfo{}; + const fd = openSocket(ip, port) orelse return info; + defer std.posix.close(fd); + + _ = std.posix.write(fd, "AMQP\x00\x00\x09\x01") catch return info; + + var buf: [512]u8 = undefined; + const n = std.posix.read(fd, &buf) catch return info; + if (n < 8) return info; + + // Connection.Start: frame type=1 (method), class=10, method=10 + if (buf[0] == 1) { + info.setName("AMQP"); + // Check for RabbitMQ in the response payload + if (n > 20 and std.mem.indexOf(u8, buf[0..n], "RabbitMQ") != null) { + info.setExtra("RabbitMQ"); } + } + return info; +} - if (line.len > 0) { - const trimmed = std.mem.trim(u8, line, " \t\r\n"); - if (trimmed.len > 0) { - info.setVersion(trimmed); - return info; - } +fn probeRdp(ip: u32, port: u16) ServiceInfo { + var info = ServiceInfo{}; + const fd = openSocket(ip, port) orelse return info; + defer std.posix.close(fd); + + const cr_pkt = [_]u8{ + 0x03, 0x00, 0x00, 0x0B, // TPKT header + 0x06, // X.224 length + 0xE0, // Connection Request + 0x00, 0x00, // dst-ref + 0x00, 0x00, // src-ref + 0x00, // class 0 + }; + _ = std.posix.write(fd, &cr_pkt) catch return info; + + var buf: [64]u8 = undefined; + const n = std.posix.read(fd, &buf) catch return info; + if (n < 7) return info; + + if (buf[0] == 0x03 and buf[1] == 0x00 and buf[5] == 0xD0) { + info.setName("RDP"); + info.setExtra("Microsoft Terminal Services"); + } + return info; +} + +fn probeSmb(ip: u32, port: u16) ServiceInfo { + var info = ServiceInfo{}; + const fd = openSocket(ip, port) orelse return info; + defer std.posix.close(fd); + + // SMB2 Negotiate: NetBIOS(4) + SMB2 header(64) + negotiate body + var pkt: [128]u8 = .{0} ** 128; + var pos: usize = 4; // skip NetBIOS header + + // SMB2 header + @memcpy(pkt[pos .. pos + 4], "\xfeSMB"); + pos += 4; + std.mem.writeInt(u16, pkt[pos..][0..2], 64, .little); + pos += 2; // structure size + pos += 2; // credit charge + pos += 4; // status + // command = NEGOTIATE (0) + pos += 2; + std.mem.writeInt(u16, pkt[pos..][0..2], 1, .little); + pos += 2; // credit request + pos += 4; // flags + pos += 4; // next command + pos += 8; // message ID + pos += 4; // reserved + pos += 4; // tree ID + pos += 8; // session ID + pos += 16; // signature + + // Negotiate body + std.mem.writeInt(u16, pkt[pos..][0..2], 36, .little); + pos += 2; // structure size + std.mem.writeInt(u16, pkt[pos..][0..2], 2, .little); + pos += 2; // dialect count + pos += 2; // security mode + pos += 2; // reserved + pos += 4; // capabilities + pos += 16; // client GUID + pos += 8; // negotiate context + // dialects + std.mem.writeInt(u16, pkt[pos..][0..2], 0x0202, .little); + pos += 2; + std.mem.writeInt(u16, pkt[pos..][0..2], 0x0311, .little); + pos += 2; + + // NetBIOS length + const nb_len: u32 = @intCast(pos - 4); + std.mem.writeInt(u32, pkt[0..4], nb_len, .big); + pkt[0] = 0; + + _ = std.posix.write(fd, pkt[0..pos]) catch return info; + + var buf: [512]u8 = undefined; + const n = std.posix.read(fd, &buf) catch return info; + if (n < 68) return info; + + if (n >= 8 and std.mem.eql(u8, buf[4..8], "\xfeSMB")) { + info.setName("SMB"); + if (n >= 72) { + const dialect = std.mem.readInt(u16, buf[68..70], .little); + var ver_buf: [16]u8 = undefined; + const ver_str: []const u8 = switch (dialect) { + 0x0202 => "2.0.2", + 0x0210 => "2.1", + 0x0300 => "3.0", + 0x0302 => "3.0.2", + 0x0311 => "3.1.1", + else => std.fmt.bufPrint(&ver_buf, "dialect:0x{X:0>4}", .{dialect}) catch "unknown", + }; + info.setVersion(ver_str); } + } else if (n >= 8 and std.mem.eql(u8, buf[4..8], "\xffSMB")) { + info.setName("SMB"); + info.setVersion("1.0"); } + return info; +} - // Try HTTP first (most common on unknown ports) +fn probeGeneric(ip: u32, port: u16) ServiceInfo { + // Phase 1: NULL probe - wait for service to speak first { - var ip_buf: [16]u8 = undefined; - const ip_str = formatIp(&ip_buf, ip); - var req_buf: [128]u8 = undefined; - const request = std.fmt.bufPrint(&req_buf, "GET / HTTP/1.0\r\nHost: {s}\r\n\r\n", .{ip_str}) catch return info; - _ = std.posix.write(fd, request) catch return info; - - const hn = std.posix.read(fd, &buf) catch return info; - if (hn > 0) { - const data = buf[0..hn]; - if (std.mem.startsWith(u8, data, "HTTP/")) { - if (findHeaderValue(data, "server:")) |server| { - parseServerHeader(server, &info); - } - if (info.name_len == 0) { - if (findHeaderValue(data, "uwebsockets:")) |val| { - info.setName("uWebSockets"); - info.setVersion(val); - } else { - if (extractTitle(data)) |title| { - info.setVersion(title); - } - info.setName("http"); - } - } - if (findHeaderValue(data, "x-powered-by:")) |val| { - info.setExtra(val); - } - fingerprintHttp(data, &info); - return info; + const fd = openSocket(ip, port) orelse return ServiceInfo{}; + defer std.posix.close(fd); + + var buf: [1024]u8 = undefined; + const n = readWithTimeout(fd, &buf, 1_000_000_000); + if (n > 0) { + const data = buf[0..n]; + const result = classifyBanner(data, ip, port); + if (result.name_len > 0) return result; + if (sigs.matchBanner(data)) |m| { + var sig_info = ServiceInfo{}; + sig_info.setName(m.name); + if (m.version.len > 0) sig_info.setVersion(m.version); + return sig_info; } + if (!result.empty()) return result; + } + } - // Response to HTTP wasn't HTTP - check for Redis RESP error - if (std.mem.startsWith(u8, data, "-ERR") or - std.mem.startsWith(u8, data, "-NOAUTH") or - std.mem.startsWith(u8, data, "-DENIED")) - { - info.setName("Redis"); - if (std.mem.indexOf(u8, data, "NOAUTH")) |_| { - info.setExtra("auth required"); - } - return info; + // Phase 2: HTTP GET (most common on unknown ports, fresh connection) + if (!isKnownNonHttpPort(port)) { + const http_info = probeHttpGeneric(ip, port); + if (!http_info.empty()) return http_info; + } + + // Phase 3: Protocol-specific probes (last resort, each on a fresh connection) + { + const redis_info = probeRedis(ip, port); + if (!redis_info.empty()) return redis_info; + } + { + const pg_info = probePostgresql(ip, port); + if (!pg_info.empty()) return pg_info; + } + + return ServiceInfo{}; +} + +fn classifyBanner(data: []const u8, ip: u32, port: u16) ServiceInfo { + var info = ServiceInfo{}; + const line_end = std.mem.indexOfAny(u8, data, "\r\n") orelse @min(data.len, 128); + const line = data[0..line_end]; + + if (std.mem.startsWith(u8, line, "SSH-")) { + return probeSshFromBanner(line); + } + + // FTP/SMTP-style numeric banner (e.g. "220 ...") + if (line.len > 4 and isDigit(line[0]) and isDigit(line[1]) and isDigit(line[2]) and (line[3] == ' ' or line[3] == '-')) { + info.setVersion(line[4..]); + return info; + } + + // MySQL greeting packet + if (data.len >= 5 and data[3] == 0 and data[4] == 0x0a) { + return probeMysql(ip, port); + } + + // Redis RESP response + if (std.mem.startsWith(u8, data, "-ERR") or + std.mem.startsWith(u8, data, "-NOAUTH") or + std.mem.startsWith(u8, data, "-DENIED") or + std.mem.startsWith(u8, data, "+PONG") or + (data[0] == '$' and data.len >= 4 and (isDigit(data[1]) or data[1] == '-'))) + { + info.setName("Redis"); + if (std.mem.indexOf(u8, data, "NOAUTH")) |_| { + info.setExtra("auth required"); + } + return info; + } + + // HTTP response (some servers send HTTP without a request) + if (std.mem.startsWith(u8, data, "HTTP/")) { + info.setName("http"); + if (findHeaderValue(data, "server:")) |server| { + parseServerHeader(server, &info); + } + fingerprintHttp(data, &info); + return info; + } + + // Generic banner text + const trimmed = std.mem.trim(u8, line, " \t\r\n"); + if (trimmed.len > 0) { + info.setVersion(trimmed); + } + + return info; +} + +fn probeHttpGeneric(ip: u32, port: u16) ServiceInfo { + var info = ServiceInfo{}; + const fd = openSocket(ip, port) orelse return info; + defer std.posix.close(fd); + + var ip_buf: [16]u8 = undefined; + const ip_str = formatIp(&ip_buf, ip); + var req_buf: [128]u8 = undefined; + const request = std.fmt.bufPrint(&req_buf, "GET / HTTP/1.0\r\nHost: {s}\r\n\r\n", .{ip_str}) catch return info; + _ = std.posix.write(fd, request) catch return info; + + var buf: [4096]u8 = undefined; + const n = std.posix.read(fd, &buf) catch return info; + if (n == 0) return info; + + const data = buf[0..n]; + if (!std.mem.startsWith(u8, data, "HTTP/")) return info; + + if (findHeaderValue(data, "server:")) |server| { + parseServerHeader(server, &info); + } + if (info.name_len == 0) { + if (findHeaderValue(data, "uwebsockets:")) |val| { + info.setName("uWebSockets"); + info.setVersion(val); + } else { + if (extractTitle(data)) |title| { + info.setVersion(title); } + info.setName("http"); } } + if (findHeaderValue(data, "x-powered-by:")) |val| { + info.setExtra(val); + } + fingerprintHttp(data, &info); + + if (std.mem.eql(u8, info.name(), "uvicorn") and std.mem.eql(u8, info.extra(), "FastAPI")) { + probeFastapiOpenapi(ip, port, &info); + } - // Try Redis on a new connection - const redis_info = probeRedis(ip, port); - if (!redis_info.empty()) return redis_info; + if (std.mem.eql(u8, info.name(), "http")) { + if (sigs.matchHttp(data)) |m| { + info.setName(m.name); + info.version_len = 0; + } + } - // Try PostgreSQL on a new connection - const pg_info = probePostgresql(ip, port); - if (!pg_info.empty()) return pg_info; + if (info.extra_len == 0 and std.mem.eql(u8, info.name(), "http")) { + storeHttpEvidence(data, &info); + } return info; } +fn probeFastapiOpenapi(ip: u32, port: u16, info: *ServiceInfo) void { + const fd = openSocket(ip, port) orelse return; + defer std.posix.close(fd); + + var ip_buf: [16]u8 = undefined; + const ip_str = formatIp(&ip_buf, ip); + var req_buf: [128]u8 = undefined; + const request = std.fmt.bufPrint(&req_buf, "GET /openapi.json HTTP/1.0\r\nHost: {s}\r\n\r\n", .{ip_str}) catch return; + _ = std.posix.write(fd, request) catch return; + + var buf: [4096]u8 = undefined; + const n = std.posix.read(fd, &buf) catch return; + if (n == 0) return; + const data = buf[0..n]; + + if (!std.mem.startsWith(u8, data, "HTTP/1.")) return; + if (std.mem.indexOf(u8, data[0..@min(data.len, 32)], " 200 ") == null) return; + + const body_start = (std.mem.indexOf(u8, data, "\r\n\r\n") orelse return) + 4; + if (body_start >= data.len) return; + const body = data[body_start..]; + + if (findJsonValue(body, "\"title\":\"")) |title| { + if (findJsonValue(body, "\"version\":\"")) |ver| { + var extra_buf: [128]u8 = undefined; + const extra = std.fmt.bufPrint(&extra_buf, "{s} {s}", .{ title, ver }) catch return; + info.setExtra(extra); + } else { + info.setExtra(title); + } + } +} + +fn isKnownNonHttpPort(port: u16) bool { + return switch (port) { + 22, 2222 => true, + 21 => true, + 25, 587 => true, + 110 => true, + 143 => true, + 3306, 33060 => true, + 5432...5434 => true, + 6379...6399 => true, + 6660...6669, 6697 => true, + 11211 => true, + 27017...27019 => true, + 1883, 8883 => true, + 5672 => true, + 3389 => true, + 445 => true, + else => false, + }; +} + fn probeSshFromBanner(line: []const u8) ServiceInfo { var info = ServiceInfo{}; var dash_count: u8 = 0; @@ -758,9 +1235,35 @@ fn fingerprintHttp(data: []const u8, info: *ServiceInfo) void { return; } } + + // Elasticsearch: JSON with "tagline":"You Know, for Search" + if (std.mem.indexOf(u8, body, "You Know, for Search") != null) { + info.setName("Elasticsearch"); + if (findJsonValue(body, "\"number\":\"")) |ver| { + info.setVersion(ver); + } + return; + } + + // Docker API: JSON with "ApiVersion" + if (std.mem.indexOf(u8, body, "\"ApiVersion\"") != null) { + info.setName("Docker API"); + if (findJsonValue(body, "\"ApiVersion\":\"")) |ver| { + info.setVersion(ver); + } + return; + } } } +fn findJsonValue(data: []const u8, prefix: []const u8) ?[]const u8 { + const pos = std.mem.indexOf(u8, data, prefix) orelse return null; + const start = pos + prefix.len; + if (start >= data.len) return null; + const end = std.mem.indexOfScalarPos(u8, data, start, '"') orelse return null; + return data[start..end]; +} + fn extractTitle(data: []const u8) ?[]const u8 { const open = findCaseTag(data, "") orelse return null; const after = open + 7; @@ -821,3 +1324,297 @@ fn formatIp(buf: []u8, ip: u32) []const u8 { }; return std.fmt.bufPrint(buf, "{d}.{d}.{d}.{d}", .{ b[0], b[1], b[2], b[3] }) catch "?.?.?.?"; } + +// --------------------------------------------------------------------------- +// Tests +// --------------------------------------------------------------------------- + +test "parseServerHeader - nginx with version" { + var info = ServiceInfo{}; + parseServerHeader("nginx/1.25.3", &info); + try std.testing.expectEqualStrings("nginx", info.name()); + try std.testing.expectEqualStrings("1.25.3", info.version()); +} + +test "parseServerHeader - Apache complex version" { + var info = ServiceInfo{}; + parseServerHeader("Apache/2.4.57 (Debian)", &info); + try std.testing.expectEqualStrings("Apache", info.name()); + try std.testing.expectEqualStrings("2.4.57 (Debian)", info.version()); +} + +test "parseServerHeader - name only, no slash" { + var info = ServiceInfo{}; + parseServerHeader("cloudflare", &info); + try std.testing.expectEqualStrings("cloudflare", info.name()); + try std.testing.expectEqual(@as(u8, 0), info.version_len); +} + +test "probeSshFromBanner - OpenSSH" { + const info = probeSshFromBanner("SSH-2.0-OpenSSH_9.6p1 Ubuntu-3ubuntu0.4"); + try std.testing.expectEqualStrings("OpenSSH", info.name()); + try std.testing.expectEqualStrings("9.6p1 Ubuntu-3ubuntu0.4", info.version()); +} + +test "probeSshFromBanner - unknown SSH software" { + const info = probeSshFromBanner("SSH-2.0-libssh_0.10.5"); + try std.testing.expectEqualStrings("libssh_0.10.5", info.version()); +} + +test "probeSshFromBanner - bare SSH" { + const info = probeSshFromBanner("SSH-2.0"); + try std.testing.expectEqualStrings("SSH", info.name()); +} + +test "findHeaderValue - case insensitive match" { + const data = "HTTP/1.1 200 OK\r\nServer: nginx/1.25\r\nContent-Type: text/html\r\n\r\n"; + const val = findHeaderValue(data, "server:"); + try std.testing.expect(val != null); + try std.testing.expectEqualStrings("nginx/1.25", val.?); +} + +test "findHeaderValue - lowercase header" { + const data = "HTTP/1.1 200 OK\r\nserver: uvicorn\r\ncontent-type: application/json\r\n\r\n"; + const val = findHeaderValue(data, "server:"); + try std.testing.expect(val != null); + try std.testing.expectEqualStrings("uvicorn", val.?); +} + +test "findHeaderValue - not found" { + const data = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; + const val = findHeaderValue(data, "server:"); + try std.testing.expect(val == null); +} + +test "findHeaderValue - no mid-line match" { + const data = "HTTP/1.1 200 OK\r\nX-Server: fake\r\n\r\n"; + const val = findHeaderValue(data, "server:"); + try std.testing.expect(val == null); +} + +test "fingerprintHttp - uvicorn with FastAPI body" { + var info = ServiceInfo{}; + info.setName("uvicorn"); + const data = "HTTP/1.1 404 Not Found\r\nserver: uvicorn\r\ncontent-type: application/json\r\n\r\n{\"detail\":\"Not Found\"}"; + fingerprintHttp(data, &info); + try std.testing.expectEqualStrings("FastAPI", info.extra()); +} + +test "fingerprintHttp - uvicorn without FastAPI body" { + var info = ServiceInfo{}; + info.setName("uvicorn"); + const data = "HTTP/1.1 200 OK\r\nserver: uvicorn\r\n\r\n<html>Hello</html>"; + fingerprintHttp(data, &info); + try std.testing.expectEqual(@as(u16, 0), info.extra_len); +} + +test "fingerprintHttp - Go net/http" { + var info = ServiceInfo{}; + info.setName("http"); + const data = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain; charset=utf-8\r\nX-Content-Type-Options: nosniff\r\n\r\n404 page not found\n"; + fingerprintHttp(data, &info); + try std.testing.expectEqualStrings("Go net/http", info.name()); +} + +test "fingerprintHttp - Node.js http" { + var info = ServiceInfo{}; + info.setName("http"); + const data = "HTTP/1.1 200 OK\r\nKeep-Alive: timeout=5\r\nTransfer-Encoding: chunked\r\nContent-Type: text/html\r\n\r\nHello"; + fingerprintHttp(data, &info); + try std.testing.expectEqualStrings("Node.js http", info.name()); +} + +test "extractTitle - basic" { + const data = "HTTP/1.1 200 OK\r\n\r\n<html><head><title>My Page"; + const title = extractTitle(data); + try std.testing.expect(title != null); + try std.testing.expectEqualStrings("My Page", title.?); +} + +test "extractTitle - case insensitive" { + const data = "Test"; + const title = extractTitle(data); + try std.testing.expect(title != null); + try std.testing.expectEqualStrings("Test", title.?); +} + +test "extractTitle - no title" { + const data = "No title here"; + const title = extractTitle(data); + try std.testing.expect(title == null); +} + +test "findCaseTag - case insensitive" { + const pos = findCaseTag("Hello World", "HELLO"); + try std.testing.expect(pos != null); + try std.testing.expectEqual(@as(usize, 0), pos.?); +} + +test "findCaseTag - not found" { + const pos = findCaseTag("Hello World", "xyz"); + try std.testing.expect(pos == null); +} + +test "extractVersionNumber - basic" { + var info = ServiceInfo{}; + extractVersionNumber("vsftpd 3.0.5", &info); + try std.testing.expectEqualStrings("3.0.5", info.version()); +} + +test "extractVersionNumber - embedded in text" { + var info = ServiceInfo{}; + extractVersionNumber("ProFTPD 1.3.7c Server ready", &info); + try std.testing.expectEqualStrings("1.3.7", info.version()); +} + +test "classifyPort - known ports" { + try std.testing.expectEqual(Protocol.http, classifyPort(80)); + try std.testing.expectEqual(Protocol.http, classifyPort(8080)); + try std.testing.expectEqual(Protocol.tls, classifyPort(443)); + try std.testing.expectEqual(Protocol.ssh, classifyPort(22)); + try std.testing.expectEqual(Protocol.ftp, classifyPort(21)); + try std.testing.expectEqual(Protocol.smtp, classifyPort(25)); + try std.testing.expectEqual(Protocol.mysql, classifyPort(3306)); + try std.testing.expectEqual(Protocol.redis, classifyPort(6379)); + try std.testing.expectEqual(Protocol.redis, classifyPort(6382)); + try std.testing.expectEqual(Protocol.postgresql, classifyPort(5432)); + try std.testing.expectEqual(Protocol.generic, classifyPort(9999)); +} + +test "ServiceInfo - setName truncates at 32 bytes" { + var info = ServiceInfo{}; + const long = "a" ** 64; + info.setName(long); + try std.testing.expectEqual(@as(u8, 32), info.name_len); +} + +test "ServiceInfo - setVersion trims whitespace" { + var info = ServiceInfo{}; + info.setVersion(" 1.2.3 \r\n"); + try std.testing.expectEqualStrings("1.2.3", info.version()); +} + +test "ServiceInfo - empty checks" { + var info = ServiceInfo{}; + try std.testing.expect(info.empty()); + info.setName("test"); + try std.testing.expect(!info.empty()); +} + +test "ServiceInfo - str prefers version" { + var info = ServiceInfo{}; + info.setName("nginx"); + info.setVersion("1.25.3"); + try std.testing.expectEqualStrings("1.25.3", info.str()); +} + +test "ServiceInfo - str falls back to name" { + var info = ServiceInfo{}; + info.setName("http"); + try std.testing.expectEqualStrings("http", info.str()); +} + +test "classifyBanner - SSH" { + const info = classifyBanner("SSH-2.0-OpenSSH_9.6p1\r\n", 0, 0); + try std.testing.expectEqualStrings("OpenSSH", info.name()); +} + +test "classifyBanner - FTP greeting" { + const info = classifyBanner("220 (vsFTPd 3.0.5)\r\n", 0, 0); + try std.testing.expectEqualStrings("(vsFTPd 3.0.5)", info.version()); +} + +test "classifyBanner - Redis RESP error" { + const info = classifyBanner("-NOAUTH Authentication required.\r\n", 0, 0); + try std.testing.expectEqualStrings("Redis", info.name()); + try std.testing.expectEqualStrings("auth required", info.extra()); +} + +test "classifyBanner - generic text" { + const info = classifyBanner("* OK Dovecot ready.\r\n", 0, 0); + try std.testing.expectEqualStrings("* OK Dovecot ready.", info.version()); +} + +test "isKnownNonHttpPort" { + try std.testing.expect(isKnownNonHttpPort(22)); + try std.testing.expect(isKnownNonHttpPort(6379)); + try std.testing.expect(isKnownNonHttpPort(3306)); + try std.testing.expect(!isKnownNonHttpPort(80)); + try std.testing.expect(!isKnownNonHttpPort(8080)); + try std.testing.expect(!isKnownNonHttpPort(9999)); +} + +test "storeHttpEvidence - status, content-type, body size" { + var info = ServiceInfo{}; + info.setName("http"); + const data = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\nNot Found"; + storeHttpEvidence(data, &info); + try std.testing.expectEqualStrings("404, text/plain, 9B", info.extra()); +} + +test "storeHttpEvidence - strips content-type params" { + var info = ServiceInfo{}; + info.setName("http"); + const data = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n

Hi

"; + storeHttpEvidence(data, &info); + try std.testing.expectEqualStrings("200, text/html, 11B", info.extra()); +} + +test "classifyPort - new protocols" { + try std.testing.expectEqual(Protocol.memcached, classifyPort(11211)); + try std.testing.expectEqual(Protocol.mongodb, classifyPort(27017)); + try std.testing.expectEqual(Protocol.mqtt, classifyPort(1883)); + try std.testing.expectEqual(Protocol.amqp, classifyPort(5672)); + try std.testing.expectEqual(Protocol.rdp, classifyPort(3389)); + try std.testing.expectEqual(Protocol.smb, classifyPort(445)); +} + +test "isKnownNonHttpPort - new protocols" { + try std.testing.expect(isKnownNonHttpPort(11211)); + try std.testing.expect(isKnownNonHttpPort(27017)); + try std.testing.expect(isKnownNonHttpPort(1883)); + try std.testing.expect(isKnownNonHttpPort(5672)); + try std.testing.expect(isKnownNonHttpPort(3389)); + try std.testing.expect(isKnownNonHttpPort(445)); +} + +test "fingerprintHttp - Elasticsearch" { + var info = ServiceInfo{}; + info.setName("http"); + const data = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"name\":\"node1\",\"version\":{\"number\":\"8.12.0\"},\"tagline\":\"You Know, for Search\"}"; + fingerprintHttp(data, &info); + try std.testing.expectEqualStrings("Elasticsearch", info.name()); + try std.testing.expectEqualStrings("8.12.0", info.version()); +} + +test "fingerprintHttp - Docker API" { + var info = ServiceInfo{}; + info.setName("http"); + const data = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"ApiVersion\":\"1.45\",\"Version\":\"27.0.3\"}"; + fingerprintHttp(data, &info); + try std.testing.expectEqualStrings("Docker API", info.name()); + try std.testing.expectEqualStrings("1.45", info.version()); +} + +test "findJsonValue" { + const data = "{\"name\":\"test\",\"version\":\"1.2.3\"}"; + const val = findJsonValue(data, "\"version\":\""); + try std.testing.expect(val != null); + try std.testing.expectEqualStrings("1.2.3", val.?); +} + +test "findBsonString - basic" { + // BSON: {"version": "7.0.5"} + // doc_size(4) + type(1) + key\0 + str_len(4) + val\0 + terminator(1) + const bson = [_]u8{ + 0x1C, 0x00, 0x00, 0x00, // doc size = 28 + 0x02, // type: string + 'v', 'e', 'r', 's', 'i', 'o', 'n', 0x00, // key + 0x06, 0x00, 0x00, 0x00, // string length = 6 (including null) + '7', '.', '0', '.', '5', 0x00, // value + 0x00, // doc terminator + }; + const result = findBsonString(&bson, "version"); + try std.testing.expect(result != null); + try std.testing.expectEqualStrings("7.0.5", result.?); +} diff --git a/src/data/service-probes b/src/data/service-probes new file mode 100644 index 0000000..6f6b9b9 --- /dev/null +++ b/src/data/service-probes @@ -0,0 +1,56 @@ +# OhmZ Service Probes v1.0 +# Parsed at compile time via @embedFile - zero runtime cost. +# +# Format: name|type|pattern|version_skip +# type: p = prefix match, c = contains match +# version_skip: N = skip N bytes from match start, extract to CRLF +# -1 = no version extraction +# +# Sections: +# [null] - matched against NULL probe response (services that speak first) +# [http] - matched against full HTTP GET response (headers + body) +# +# Lines starting with # and empty lines are ignored. +# Signatures are tried in file order; first match wins. + +[null] +# Standard protocols not covered by classifyBanner +POP3|p|+OK |4 +IMAP|p|* OK |-1 +VNC|p|RFB |4 +NATS|p|INFO {|-1 +Rsync|p|@RSYNCD: |9 + +[http] +# Header-based detection +Jenkins|c|X-Jenkins:|-1 +Kibana|c|kbn-name|-1 +# Title-based detection - common self-hosted services +GitLab|c|GitLab|-1 +Grafana|c|<title>Grafana|-1 +Prometheus|c|<title>Prometheus|-1 +Portainer|c|<title>Portainer|-1 +Nextcloud|c|<title>Nextcloud|-1 +Jupyter|c|<title>Jupyter|-1 +pgAdmin|c|<title>pgAdmin|-1 +MinIO|c|<title>MinIO|-1 +Gitea|c|<title>Gitea|-1 +phpMyAdmin|c|<title>phpMyAdmin|-1 +Adminer|c|<title>Adminer|-1 +RabbitMQ|c|<title>RabbitMQ|-1 +Vault|c|<title>Vault|-1 +Consul|c|<title>Consul|-1 +ArgoCD|c|<title>Argo CD|-1 +Harbor|c|<title>Harbor|-1 +Rancher|c|<title>Rancher|-1 +Proxmox|c|<title>Proxmox|-1 +SonarQube|c|<title>SonarQube|-1 +Keycloak|c|<title>Keycloak|-1 +Mattermost|c|<title>Mattermost|-1 +Zabbix|c|<title>Zabbix|-1 +Netdata|c|<title>Netdata|-1 +Graylog|c|<title>Graylog|-1 +Redmine|c|<title>Redmine|-1 +Cockpit|c|<title>Cockpit|-1 +Webmin|c|<title>Webmin|-1 +Pi-hole|c|<title>Pi-hole|-1 diff --git a/src/main.zig b/src/main.zig index 74acc62..fda24f4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -15,7 +15,7 @@ const os_mod = @import("os.zig"); const traceroute_mod = @import("traceroute.zig"); const scripts_mod = @import("scripts.zig"); -const VERSION = "0.5.2"; +const VERSION = "0.6.0"; pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -230,7 +230,7 @@ pub fn main() !void { const workers: usize = @min(open_ports.len, 16); try stderr.print("Script scanning on {d} open ports ({d} workers)...\n", .{ open_ports.len, workers }); const sr = try allocator.alloc(scripts_mod.HostScripts, open_ports.len); - scripts_mod.runAll(open_ports, sr, workers); + scripts_mod.runAllEx(open_ports, sr, workers, config.script_scan); script_results = sr; } @@ -335,8 +335,13 @@ pub fn main() !void { } else serviceName(r.port); if (has_versions) { const ver = banners.?[i].version(); - if (ver.len > 0) { + const ext = banners.?[i].extra(); + if (ver.len > 0 and ext.len > 0) { + try stderr.print("{s:<9} \x1b[32mopen\x1b[0m {s:<14} {s} ({s})\n", .{ pstr, svc, ver, ext }); + } else if (ver.len > 0) { try stderr.print("{s:<9} \x1b[32mopen\x1b[0m {s:<14} {s}\n", .{ pstr, svc, ver }); + } else if (ext.len > 0) { + try stderr.print("{s:<9} \x1b[32mopen\x1b[0m {s:<14} ({s})\n", .{ pstr, svc, ext }); } else { try stderr.print("{s:<9} \x1b[32mopen\x1b[0m {s}\n", .{ pstr, svc }); } @@ -381,8 +386,13 @@ pub fn main() !void { } else serviceName(r.port); if (has_versions) { const ver = banners.?[i].version(); - if (ver.len > 0) { + const ext = banners.?[i].extra(); + if (ver.len > 0 and ext.len > 0) { + try stderr.print("{s:<17} {s:<9} \x1b[32mopen\x1b[0m {s:<14} {s} ({s})\n", .{ hstr, pstr, svc, ver, ext }); + } else if (ver.len > 0) { try stderr.print("{s:<17} {s:<9} \x1b[32mopen\x1b[0m {s:<14} {s}\n", .{ hstr, pstr, svc, ver }); + } else if (ext.len > 0) { + try stderr.print("{s:<17} {s:<9} \x1b[32mopen\x1b[0m {s:<14} ({s})\n", .{ hstr, pstr, svc, ext }); } else { try stderr.print("{s:<17} {s:<9} \x1b[32mopen\x1b[0m {s}\n", .{ hstr, pstr, svc }); } diff --git a/src/output.zig b/src/output.zig index 6c2222b..c344d8a 100644 --- a/src/output.zig +++ b/src/output.zig @@ -47,11 +47,16 @@ pub const NormalWriter = struct { const w = self.bw.writer(); var pbuf: [12]u8 = undefined; const pstr = std.fmt.bufPrint(&pbuf, "{d}/{s}", .{ rec.port, rec.protocol }) catch "?/tcp"; - w.print("{s:<9} {s:<7} {s}\n", .{ - pstr, - rec.status, - rec.service orelse "unknown", - }) catch {}; + const svc = rec.service orelse "unknown"; + if (rec.version) |ver| { + if (rec.extra) |ext| { + w.print("{s:<9} {s:<7} {s:<14} {s} ({s})\n", .{ pstr, rec.status, svc, ver, ext }) catch {}; + } else { + w.print("{s:<9} {s:<7} {s:<14} {s}\n", .{ pstr, rec.status, svc, ver }) catch {}; + } + } else { + w.print("{s:<9} {s:<7} {s}\n", .{ pstr, rec.status, svc }) catch {}; + } } pub fn finish(self: *NormalWriter) void { @@ -162,11 +167,12 @@ pub const GrepWriter = struct { w.writeAll(", ") catch {}; } - w.print("{d}/{s}/{s}//{s}///", .{ + w.print("{d}/{s}/{s}//{s}//{s}/", .{ rec.port, rec.status, rec.protocol, rec.service orelse "", + rec.version orelse "", }) catch {}; } @@ -201,7 +207,7 @@ pub const XmlWriter = struct { }; const w = xw.bw.writer(); w.writeAll("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") catch {}; - w.writeAll("<ohmzscan scanner=\"ohmz\" version=\"0.4.0\">\n") catch {}; + w.writeAll("<ohmzscan scanner=\"ohmz\" version=\"0.6.0\">\n") catch {}; return xw; } @@ -222,11 +228,24 @@ pub const XmlWriter = struct { w.print(" <port protocol=\"{s}\" portid=\"{d}\">\n", .{ rec.protocol, rec.port }) catch {}; w.print(" <state state=\"{s}\"/>\n", .{rec.status}) catch {}; if (rec.service) |svc| { + w.writeAll(" <service name=\"") catch {}; + w.writeAll(svc) catch {}; + w.writeByte('"') catch {}; if (rec.version) |ver| { - w.print(" <service name=\"{s}\" product=\"{s}\"/>\n", .{ svc, ver }) catch {}; - } else { - w.print(" <service name=\"{s}\"/>\n", .{svc}) catch {}; + w.print(" product=\"{s}\"", .{ver}) catch {}; + } + if (rec.extra) |ext| { + w.print(" extrainfo=\"{s}\"", .{ext}) catch {}; + } + if (rec.tls) { + w.writeAll(" tunnel=\"ssl\"") catch {}; } + w.writeAll("/>\n") catch {}; + } + if (rec.scripts) |scr| { + w.writeAll(" <script output=\"") catch {}; + w.writeAll(scr) catch {}; + w.writeAll("\"/>\n") catch {}; } w.writeAll(" </port>\n") catch {}; } @@ -282,6 +301,24 @@ pub const NdjsonWriter = struct { if (rec.service) |svc| { w.print(",\"service\":\"{s}\"", .{svc}) catch return; } + if (rec.version) |ver| { + w.writeAll(",\"version\":\"") catch return; + writeJsonEscaped(w, ver); + w.writeByte('"') catch return; + } + if (rec.extra) |ext| { + w.writeAll(",\"extra\":\"") catch return; + writeJsonEscaped(w, ext); + w.writeByte('"') catch return; + } + if (rec.tls) { + w.writeAll(",\"tls\":true") catch return; + } + if (rec.scripts) |scr| { + w.writeAll(",\"scripts\":\"") catch return; + writeJsonEscaped(w, scr); + w.writeByte('"') catch return; + } w.print(",\"rtt_us\":{d},\"timestamp\":{d}}}\n", .{ rec.rtt_us, rec.timestamp }) catch return; } diff --git a/src/scripts.zig b/src/scripts.zig index ae6b69a..ffcb672 100644 --- a/src/scripts.zig +++ b/src/scripts.zig @@ -34,11 +34,11 @@ pub const ScriptResult = struct { }; pub const HostScripts = struct { - results: [8]ScriptResult = .{ScriptResult{}} ** 8, + results: [12]ScriptResult = .{ScriptResult{}} ** 12, count: u8 = 0, pub fn add(self: *HostScripts, script_id: []const u8, output_text: []const u8) void { - if (self.count >= 8) return; + if (self.count >= 12) return; self.results[self.count].setId(script_id); self.results[self.count].setOutput(output_text); self.count += 1; @@ -50,16 +50,35 @@ pub const HostScripts = struct { // --------------------------------------------------------------------------- pub fn runDefault(ip: u32, port: u16) HostScripts { + return runScripts(ip, port, false); +} + +pub fn runScripts(ip: u32, port: u16, extended: bool) HostScripts { var hs = HostScripts{}; switch (port) { 80, 8080, 8888, 3000, 5000, 8000, 9090 => { scriptHttpTitle(ip, port, &hs); scriptHttpRobots(ip, port, &hs); + if (extended) { + scriptHttpOpenapi(ip, port, &hs); + scriptHttpDocs(ip, port, &hs); + scriptHttp404(ip, port, &hs); + scriptHttpProxy(ip, port, &hs); + } }, 22, 2222 => scriptSshHostkey(ip, port, &hs), 25, 587 => scriptSmtpCommands(ip, port, &hs), 3306, 33060 => scriptMysqlInfo(ip, port, &hs), - else => {}, + else => { + if (extended) { + // Try HTTP scripts on non-standard ports too + scriptHttpTitle(ip, port, &hs); + if (hs.count > 0) { + scriptHttpOpenapi(ip, port, &hs); + scriptHttp404(ip, port, &hs); + } + } + }, } return hs; } @@ -68,12 +87,21 @@ pub fn runAll( ports: []const rx_mod.OpenPort, results: []HostScripts, num_workers: usize, +) void { + runAllEx(ports, results, num_workers, false); +} + +pub fn runAllEx( + ports: []const rx_mod.OpenPort, + results: []HostScripts, + num_workers: usize, + extended: bool, ) void { if (ports.len == 0) return; if (ports.len == 1 or num_workers <= 1) { for (ports, 0..) |p, i| { - results[i] = runDefault(p.ip, p.port); + results[i] = runScripts(p.ip, p.port, extended); } return; } @@ -84,12 +112,14 @@ pub fn runAll( ports: []const rx_mod.OpenPort, results: []HostScripts, next: *std.atomic.Value(usize), + ext: bool, }; var ctx = Ctx{ .ports = ports, .results = results, .next = &next_idx, + .ext = extended, }; const actual = @min(num_workers, @min(ports.len, MAX_WORKERS)); @@ -101,7 +131,7 @@ pub fn runAll( while (true) { const i = c.next.fetchAdd(1, .monotonic); if (i >= c.ports.len) break; - c.results[i] = runDefault(c.ports[i].ip, c.ports[i].port); + c.results[i] = runScripts(c.ports[i].ip, c.ports[i].port, c.ext); } } }.run, .{&ctx}) catch null; @@ -398,6 +428,166 @@ fn scriptMysqlInfo(ip: u32, port: u16, hs: *HostScripts) void { hs.add("mysql-info", out_buf[0..out_len]); } +// --------------------------------------------------------------------------- +// Script: http-openapi (extended) +// --------------------------------------------------------------------------- + +fn scriptHttpOpenapi(ip: u32, port: u16, hs: *HostScripts) void { + const fd = openSocket(ip, port) orelse return; + defer std.posix.close(fd); + + var ip_buf: [16]u8 = undefined; + const ip_str = formatIp(&ip_buf, ip); + var req_buf: [160]u8 = undefined; + const request = std.fmt.bufPrint(&req_buf, "GET /openapi.json HTTP/1.1\r\nHost: {s}\r\nConnection: close\r\n\r\n", .{ip_str}) catch return; + _ = std.posix.write(fd, request) catch return; + + var buf: [4096]u8 = undefined; + var total: usize = 0; + while (total < buf.len) { + const n = std.posix.read(fd, buf[total..]) catch break; + if (n == 0) break; + total += n; + } + if (total < 15) return; + + const data = buf[0..total]; + if (!std.mem.startsWith(u8, data, "HTTP/1.")) return; + if (!std.mem.eql(u8, data[9..12], "200")) return; + + if (std.mem.indexOf(u8, data, "\"openapi\"") != null or std.mem.indexOf(u8, data, "\"swagger\"") != null) { + var out_buf: [128]u8 = undefined; + if (findJsonStringValue(data, "\"title\"")) |title| { + const out = std.fmt.bufPrint(&out_buf, "OpenAPI: {s}", .{title}) catch "OpenAPI spec found"; + hs.add("http-openapi", out); + } else { + hs.add("http-openapi", "OpenAPI spec found"); + } + } +} + +fn findJsonStringValue(data: []const u8, key_with_quote: []const u8) ?[]const u8 { + // Find "key":"value" pattern + const key_pos = std.mem.indexOf(u8, data, key_with_quote) orelse return null; + var pos = key_pos + key_with_quote.len; + // Skip whitespace and colon + while (pos < data.len and (data[pos] == ':' or data[pos] == ' ' or data[pos] == '"')) : (pos += 1) {} + if (pos == 0) return null; + pos -= 1; // back to opening quote + if (pos >= data.len or data[pos] != '"') return null; + pos += 1; + const end = std.mem.indexOfScalarPos(u8, data, pos, '"') orelse return null; + if (end <= pos) return null; + return data[pos..end]; +} + +// --------------------------------------------------------------------------- +// Script: http-docs (extended) +// --------------------------------------------------------------------------- + +fn scriptHttpDocs(ip: u32, port: u16, hs: *HostScripts) void { + const fd = openSocket(ip, port) orelse return; + defer std.posix.close(fd); + + var ip_buf: [16]u8 = undefined; + const ip_str = formatIp(&ip_buf, ip); + var req_buf: [160]u8 = undefined; + const request = std.fmt.bufPrint(&req_buf, "GET /docs HTTP/1.1\r\nHost: {s}\r\nConnection: close\r\n\r\n", .{ip_str}) catch return; + _ = std.posix.write(fd, request) catch return; + + var buf: [4096]u8 = undefined; + var total: usize = 0; + while (total < buf.len) { + const n = std.posix.read(fd, buf[total..]) catch break; + if (n == 0) break; + total += n; + } + if (total < 15) return; + + const data = buf[0..total]; + if (!std.mem.startsWith(u8, data, "HTTP/1.")) return; + if (!std.mem.eql(u8, data[9..12], "200")) return; + + if (findCaseInsensitive(data, "swagger-ui") != null) { + hs.add("http-docs", "Swagger UI"); + } else if (findCaseInsensitive(data, "redoc") != null) { + hs.add("http-docs", "ReDoc"); + } +} + +// --------------------------------------------------------------------------- +// Script: http-404 (extended) - framework fingerprinting via error page +// --------------------------------------------------------------------------- + +fn scriptHttp404(ip: u32, port: u16, hs: *HostScripts) void { + const fd = openSocket(ip, port) orelse return; + defer std.posix.close(fd); + + var ip_buf: [16]u8 = undefined; + const ip_str = formatIp(&ip_buf, ip); + var req_buf: [160]u8 = undefined; + const request = std.fmt.bufPrint(&req_buf, "GET /asd404ohmz HTTP/1.1\r\nHost: {s}\r\nConnection: close\r\n\r\n", .{ip_str}) catch return; + _ = std.posix.write(fd, request) catch return; + + var buf: [4096]u8 = undefined; + var total: usize = 0; + while (total < buf.len) { + const n = std.posix.read(fd, buf[total..]) catch break; + if (n == 0) break; + total += n; + } + if (total < 15) return; + + const data = buf[0..total]; + if (!std.mem.startsWith(u8, data, "HTTP/1.")) return; + + const body_start = if (std.mem.indexOf(u8, data, "\r\n\r\n")) |p| p + 4 else total; + const body = if (body_start < total) data[body_start..] else ""; + + if (std.mem.indexOf(u8, body, "Cannot GET") != null) { + hs.add("http-404", "Express.js"); + } else if (std.mem.indexOf(u8, body, "{\"detail\":\"Not Found\"}") != null) { + hs.add("http-404", "FastAPI"); + } else if (std.mem.indexOf(u8, body, "Whitelabel Error Page") != null) { + hs.add("http-404", "Spring Boot"); + } else if (findCaseInsensitive(body, "<address>apache") != null) { + hs.add("http-404", "Apache"); + } else if (findCaseInsensitive(body, "nginx") != null and std.mem.indexOf(u8, body, "404") != null) { + hs.add("http-404", "nginx"); + } +} + +// --------------------------------------------------------------------------- +// Script: http-proxy (extended) - reverse proxy detection +// --------------------------------------------------------------------------- + +fn scriptHttpProxy(ip: u32, port: u16, hs: *HostScripts) void { + const fd = openSocket(ip, port) orelse return; + defer std.posix.close(fd); + + var req_buf: [128]u8 = undefined; + const request = std.fmt.bufPrint(&req_buf, "GET / HTTP/1.1\r\nHost: invalid.ohmz.test\r\nConnection: close\r\n\r\n", .{}) catch return; + _ = std.posix.write(fd, request) catch return; + + var buf: [2048]u8 = undefined; + const n = std.posix.read(fd, &buf) catch return; + if (n < 15) return; + + const data = buf[0..n]; + if (!std.mem.startsWith(u8, data, "HTTP/1.")) return; + + // A reverse proxy often returns 404/502 with its own headers for invalid Host + if (findCaseInsensitive(data, "traefik") != null) { + hs.add("http-proxy", "Traefik reverse proxy"); + } else if (findCaseInsensitive(data, "haproxy") != null) { + hs.add("http-proxy", "HAProxy"); + } else if (findCaseInsensitive(data, "envoy") != null) { + hs.add("http-proxy", "Envoy proxy"); + } else if (findCaseInsensitive(data, "caddy") != null) { + hs.add("http-proxy", "Caddy"); + } +} + // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- diff --git a/src/sigs.zig b/src/sigs.zig new file mode 100644 index 0000000..80ddf8a --- /dev/null +++ b/src/sigs.zig @@ -0,0 +1,216 @@ +const std = @import("std"); + +const probes_raw = @embedFile("data/service-probes"); + +const MatchKind = enum { prefix, contains }; + +const Sig = struct { + name: []const u8, + kind: MatchKind, + pattern: []const u8, + ver_skip: i8, +}; + +pub const Match = struct { + name: []const u8, + version: []const u8, +}; + +const Category = enum { null_probe, http_body, none }; + +const null_sigs = parseSigs(.null_probe); +const http_sigs = parseSigs(.http_body); + +pub fn matchBanner(data: []const u8) ?Match { + return matchAgainst(data, &null_sigs); +} + +pub fn matchHttp(data: []const u8) ?Match { + return matchAgainst(data, &http_sigs); +} + +fn matchAgainst(data: []const u8, sigs: []const Sig) ?Match { + for (sigs) |sig| { + const maybe_pos: ?usize = switch (sig.kind) { + .prefix => if (std.mem.startsWith(u8, data, sig.pattern)) @as(?usize, 0) else null, + .contains => std.mem.indexOf(u8, data, sig.pattern), + }; + const pos = maybe_pos orelse continue; + var version: []const u8 = ""; + if (sig.ver_skip >= 0) { + const start = pos + @as(usize, @intCast(sig.ver_skip)); + if (start < data.len) { + const rest = data[start..]; + const end = std.mem.indexOfAny(u8, rest, "\r\n\x00") orelse rest.len; + version = rest[0..end]; + } + } + return Match{ .name = sig.name, .version = version }; + } + return null; +} + +// --------------------------------------------------------------------------- +// Comptime parsing +// --------------------------------------------------------------------------- + +fn countSigs(comptime cat: Category) comptime_int { + @setEvalBranchQuota(4000); + var section: Category = .none; + var count: usize = 0; + var iter = std.mem.splitScalar(u8, probes_raw, '\n'); + while (iter.next()) |raw| { + const line = std.mem.trim(u8, raw, " \t\r"); + if (line.len == 0 or line[0] == '#') continue; + if (std.mem.eql(u8, line, "[null]")) { + section = .null_probe; + continue; + } + if (std.mem.eql(u8, line, "[http]")) { + section = .http_body; + continue; + } + if (section == cat) count += 1; + } + return count; +} + +fn parseSigs(comptime cat: Category) [countSigs(cat)]Sig { + @setEvalBranchQuota(50000); + const N = countSigs(cat); + var result: [N]Sig = undefined; + var idx: usize = 0; + var section: Category = .none; + var iter = std.mem.splitScalar(u8, probes_raw, '\n'); + while (iter.next()) |raw| { + const line = std.mem.trim(u8, raw, " \t\r"); + if (line.len == 0 or line[0] == '#') continue; + if (std.mem.eql(u8, line, "[null]")) { + section = .null_probe; + continue; + } + if (std.mem.eql(u8, line, "[http]")) { + section = .http_body; + continue; + } + if (section != cat) continue; + result[idx] = parseLine(line); + idx += 1; + } + return result; +} + +fn parseLine(comptime line: []const u8) Sig { + const sep1 = std.mem.indexOfScalar(u8, line, '|') orelse @compileError("bad sig: missing first |"); + const name = line[0..sep1]; + + const after_name = line[sep1 + 1 ..]; + const sep2 = std.mem.indexOfScalar(u8, after_name, '|') orelse @compileError("bad sig: missing second |"); + const kind_str = after_name[0..sep2]; + + const sep_last = std.mem.lastIndexOfScalar(u8, line, '|') orelse @compileError("bad sig: missing last |"); + const pattern = line[sep1 + 1 + sep2 + 1 .. sep_last]; + const ver_str = line[sep_last + 1 ..]; + + return .{ + .name = name, + .kind = if (std.mem.eql(u8, kind_str, "p")) .prefix else if (std.mem.eql(u8, kind_str, "c")) .contains else @compileError("bad match type"), + .pattern = pattern, + .ver_skip = parseI8(ver_str), + }; +} + +fn parseI8(comptime s: []const u8) i8 { + if (s.len == 0) return -1; + var start: usize = 0; + var negative = false; + if (s[0] == '-') { + negative = true; + start = 1; + } + var val: i8 = 0; + for (s[start..]) |c| { + if (c < '0' or c > '9') @compileError("bad digit in version_skip"); + val = val * 10 + @as(i8, @intCast(c - '0')); + } + return if (negative) -val else val; +} + +// --------------------------------------------------------------------------- +// Tests +// --------------------------------------------------------------------------- + +test "null probe signature count" { + try std.testing.expect(null_sigs.len == 5); +} + +test "http signature count" { + try std.testing.expect(http_sigs.len >= 30); +} + +test "matchBanner - POP3" { + const m = matchBanner("+OK Dovecot ready.\r\n") orelse return error.TestExpectedMatch; + try std.testing.expectEqualStrings("POP3", m.name); + try std.testing.expectEqualStrings("Dovecot ready.", m.version); +} + +test "matchBanner - POP3 short" { + const m = matchBanner("+OK \r\n") orelse return error.TestExpectedMatch; + try std.testing.expectEqualStrings("POP3", m.name); + try std.testing.expectEqualStrings("", m.version); +} + +test "matchBanner - IMAP" { + const m = matchBanner("* OK [CAPABILITY IMAP4rev1] ready.\r\n") orelse return error.TestExpectedMatch; + try std.testing.expectEqualStrings("IMAP", m.name); + try std.testing.expectEqualStrings("", m.version); +} + +test "matchBanner - VNC" { + const m = matchBanner("RFB 003.008\n") orelse return error.TestExpectedMatch; + try std.testing.expectEqualStrings("VNC", m.name); + try std.testing.expectEqualStrings("003.008", m.version); +} + +test "matchBanner - NATS" { + const m = matchBanner("INFO {\"server_id\":\"abc\"}\r\n") orelse return error.TestExpectedMatch; + try std.testing.expectEqualStrings("NATS", m.name); + try std.testing.expectEqualStrings("", m.version); +} + +test "matchBanner - Rsync" { + const m = matchBanner("@RSYNCD: 31.0\n") orelse return error.TestExpectedMatch; + try std.testing.expectEqualStrings("Rsync", m.name); + try std.testing.expectEqualStrings("31.0", m.version); +} + +test "matchBanner - no match" { + try std.testing.expect(matchBanner("UNKNOWN PROTOCOL\r\n") == null); +} + +test "matchBanner - empty" { + try std.testing.expect(matchBanner("") == null); +} + +test "matchHttp - Grafana title" { + const resp = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><head><title>Grafana"; + const m = matchHttp(resp) orelse return error.TestExpectedMatch; + try std.testing.expectEqualStrings("Grafana", m.name); +} + +test "matchHttp - Jenkins header" { + const resp = "HTTP/1.1 200 OK\r\nX-Jenkins: 2.401.1\r\n\r\n"; + const m = matchHttp(resp) orelse return error.TestExpectedMatch; + try std.testing.expectEqualStrings("Jenkins", m.name); +} + +test "matchHttp - Kibana header" { + const resp = "HTTP/1.1 200 OK\r\nkbn-name: kibana\r\n\r\n"; + const m = matchHttp(resp) orelse return error.TestExpectedMatch; + try std.testing.expectEqualStrings("Kibana", m.name); +} + +test "matchHttp - no match" { + const resp = "HTTP/1.1 200 OK\r\n\r\nMy App"; + try std.testing.expect(matchHttp(resp) == null); +} diff --git a/src/tls.zig b/src/tls.zig index 282dba2..8ae3bcf 100644 --- a/src/tls.zig +++ b/src/tls.zig @@ -15,6 +15,10 @@ pub const TlsInfo = struct { issuer_cn_len: u8 = 0, not_after_buf: [24]u8 = .{0} ** 24, not_after_len: u8 = 0, + san_buf: [256]u8 = .{0} ** 256, + san_len: u16 = 0, + alpn_buf: [16]u8 = .{0} ** 16, + alpn_len: u8 = 0, tls_minor: u8 = 0, valid: bool = false, @@ -38,11 +42,25 @@ pub const TlsInfo = struct { return self.not_after_buf[0..self.not_after_len]; } + pub fn sans(self: *const TlsInfo) []const u8 { + return self.san_buf[0..self.san_len]; + } + + pub fn alpn(self: *const TlsInfo) []const u8 { + return self.alpn_buf[0..self.alpn_len]; + } + fn setStr(buf: []u8, text: []const u8) u8 { const len: u8 = @intCast(@min(text.len, buf.len)); @memcpy(buf[0..len], text[0..len]); return len; } + + fn setStr16(buf: []u8, text: []const u8) u16 { + const len: u16 = @intCast(@min(text.len, buf.len)); + @memcpy(buf[0..len], text[0..len]); + return len; + } }; pub fn probe(ip: u32, port: u16) TlsInfo { @@ -168,6 +186,15 @@ fn parseServerHello(data: []const u8, info: *TlsInfo) void { actual_major = data[epos]; actual_minor = data[epos + 1]; } + if (etype == 0x0010 and elen >= 4) { + const proto_start = epos + 2; + if (proto_start < ext_end) { + const proto_len: usize = data[proto_start]; + if (proto_start + 1 + proto_len <= ext_end) { + info.alpn_len = TlsInfo.setStr(&info.alpn_buf, data[proto_start + 1 .. proto_start + 1 + proto_len]); + } + } + } epos += elen; } } @@ -238,6 +265,21 @@ fn parseCertDer(der: []const u8, info: *TlsInfo) void { const subject_data_end = derSkip(tbs, pos) orelse return; const subject_data = tbs[pos..subject_data_end]; extractCn(subject_data, &info.subject_cn_buf, &info.cn_len); + + // Skip subjectPublicKeyInfo + pos = subject_data_end; + pos = derSkip(tbs, pos) orelse return; + + // Look for extensions (context tag [3] EXPLICIT) + while (pos < tbs.len) { + if (tbs[pos] == 0xA3) { + const ext_wrapper = derContent(tbs, pos) orelse break; + const ext_seq = derEnter(ext_wrapper, 0x30) orelse break; + extractSans(ext_seq, &info.san_buf, &info.san_len); + break; + } + pos = derSkip(tbs, pos) orelse break; + } } fn parseValidity(data: []const u8, info: *TlsInfo) void { @@ -270,6 +312,55 @@ fn parseValidity(data: []const u8, info: *TlsInfo) void { } } +fn extractSans(extensions: []const u8, san_buf: *[256]u8, san_len: *u16) void { + const san_oid = [_]u8{ 0x55, 0x1D, 0x11 }; // 2.5.29.17 + var pos: usize = 0; + while (pos < extensions.len) { + if (extensions[pos] != 0x30) { + pos = derSkip(extensions, pos) orelse return; + continue; + } + const ext_seq = derSlice(extensions, pos, 0x30) orelse return; + pos = derSkip(extensions, pos) orelse return; + + if (ext_seq.len < 2) continue; + const oid_data = derContent(ext_seq, 0) orelse continue; + if (oid_data.len != san_oid.len or !std.mem.eql(u8, oid_data, &san_oid)) continue; + + // Found SAN extension - get the OCTET STRING value + var epos = derSkip(ext_seq, 0) orelse continue; + // Skip optional BOOLEAN (critical flag) + if (epos < ext_seq.len and ext_seq[epos] == 0x01) { + epos = derSkip(ext_seq, epos) orelse continue; + } + const octet = derSlice(ext_seq, epos, 0x04) orelse continue; + const names_seq = derEnter(octet, 0x30) orelse continue; + + var npos: usize = 0; + var slen: usize = 0; + while (npos < names_seq.len) { + const tag = names_seq[npos]; + const li = derReadLen(names_seq, npos + 1) orelse break; + if (li.offset + li.length > names_seq.len) break; + const val = names_seq[li.offset .. li.offset + li.length]; + + // tag 0x82 = dNSName (context [2] IMPLICIT) + if (tag == 0x82 and val.len > 0) { + if (slen > 0 and slen + 1 < san_buf.len) { + san_buf[slen] = ','; + slen += 1; + } + const copy_len = @min(val.len, san_buf.len - slen); + @memcpy(san_buf[slen .. slen + copy_len], val[0..copy_len]); + slen += copy_len; + } + npos = li.offset + li.length; + } + san_len.* = @intCast(@min(slen, san_buf.len)); + return; + } +} + fn extractCn(name_data: []const u8, buf: *[128]u8, len: *u8) void { const cn_oid = [_]u8{ 0x55, 0x04, 0x03 }; const seq = derEnter(name_data, 0x30) orelse return; @@ -351,10 +442,10 @@ fn readU24(b: []const u8) usize { return (@as(usize, b[0]) << 16) | (@as(usize, b[1]) << 8) | @as(usize, b[2]); } -const HelloMsg = struct { data: [256]u8, len: usize }; +const HelloMsg = struct { data: [384]u8, len: usize }; fn buildClientHello() HelloMsg { - var buf: [256]u8 = .{0} ** 256; + var buf: [384]u8 = .{0} ** 384; var pos: usize = 0; buf[pos] = 22; @@ -385,14 +476,17 @@ fn buildClientHello() HelloMsg { pos += 1; const cipher_suites = [_][2]u8{ + .{ 0x13, 0x01 }, // TLS_AES_128_GCM_SHA256 (TLS 1.3) + .{ 0x13, 0x02 }, // TLS_AES_256_GCM_SHA384 (TLS 1.3) + .{ 0x13, 0x03 }, // TLS_CHACHA20_POLY1305_SHA256 (TLS 1.3) .{ 0xC0, 0x2F }, // ECDHE_RSA_AES_128_GCM_SHA256 .{ 0xC0, 0x30 }, // ECDHE_RSA_AES_256_GCM_SHA384 .{ 0xC0, 0x2B }, // ECDHE_ECDSA_AES_128_GCM_SHA256 .{ 0xC0, 0x2C }, // ECDHE_ECDSA_AES_256_GCM_SHA384 + .{ 0xCC, 0xA8 }, // ECDHE_RSA_CHACHA20_POLY1305 + .{ 0xCC, 0xA9 }, // ECDHE_ECDSA_CHACHA20_POLY1305 .{ 0x00, 0x9C }, // RSA_AES_128_GCM_SHA256 .{ 0x00, 0x9D }, // RSA_AES_256_GCM_SHA384 - .{ 0x00, 0x2F }, // RSA_AES_128_CBC_SHA - .{ 0x00, 0x35 }, // RSA_AES_256_CBC_SHA .{ 0x00, 0xFF }, // RENEGOTIATION_INFO_SCSV }; const suites_len: u16 = cipher_suites.len * 2; @@ -468,6 +562,38 @@ fn buildClientHello() HelloMsg { pos += 2; } + // Extension: ALPN (0x0010) - advertise h2 and http/1.1 + buf[pos] = 0x00; + buf[pos + 1] = 0x10; + pos += 2; + std.mem.writeInt(u16, buf[pos..][0..2], 14, .big); + pos += 2; // extension data length + std.mem.writeInt(u16, buf[pos..][0..2], 12, .big); + pos += 2; // protocol list length + buf[pos] = 2; + pos += 1; + @memcpy(buf[pos .. pos + 2], "h2"); + pos += 2; + buf[pos] = 8; + pos += 1; + @memcpy(buf[pos .. pos + 8], "http/1.1"); + pos += 8; + + // Extension: supported_versions (0x002B) - offer TLS 1.3 + 1.2 + buf[pos] = 0x00; + buf[pos + 1] = 0x2B; + pos += 2; + std.mem.writeInt(u16, buf[pos..][0..2], 5, .big); + pos += 2; // extension data length + buf[pos] = 4; + pos += 1; // supported versions list length + buf[pos] = 0x03; + buf[pos + 1] = 0x04; + pos += 2; // TLS 1.3 + buf[pos] = 0x03; + buf[pos + 1] = 0x03; + pos += 2; // TLS 1.2 + const ext_len: u16 = @intCast(pos - ext_len_pos - 2); std.mem.writeInt(u16, buf[ext_len_pos..][0..2], ext_len, .big); @@ -522,3 +648,62 @@ fn cipherSuiteName(suite: u16) []const u8 { else => "unknown", }; } + +// --------------------------------------------------------------------------- +// Tests +// --------------------------------------------------------------------------- + +test "tlsVersionName" { + try std.testing.expectEqualStrings("TLS 1.3", tlsVersionName(0x03, 0x04)); + try std.testing.expectEqualStrings("TLS 1.2", tlsVersionName(0x03, 0x03)); + try std.testing.expectEqualStrings("TLS 1.0", tlsVersionName(0x03, 0x01)); + try std.testing.expectEqualStrings("SSL 3.0", tlsVersionName(0x03, 0x00)); + try std.testing.expectEqualStrings("unknown", tlsVersionName(0x02, 0x00)); +} + +test "cipherSuiteName - known suites" { + try std.testing.expectEqualStrings("TLS-AES-128-GCM-SHA256", cipherSuiteName(0x1301)); + try std.testing.expectEqualStrings("ECDHE-RSA-AES128-GCM-SHA256", cipherSuiteName(0xC02F)); + try std.testing.expectEqualStrings("unknown", cipherSuiteName(0xFFFF)); +} + +test "derReadLen - short form" { + const data = [_]u8{ 0x30, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05 }; + const info = derReadLen(&data, 1); + try std.testing.expect(info != null); + try std.testing.expectEqual(@as(usize, 2), info.?.offset); + try std.testing.expectEqual(@as(usize, 5), info.?.length); +} + +test "derReadLen - long form 1 byte" { + const data = [_]u8{ 0x30, 0x81, 0x80 } ++ ([_]u8{0} ** 128); + const info = derReadLen(&data, 1); + try std.testing.expect(info != null); + try std.testing.expectEqual(@as(usize, 3), info.?.offset); + try std.testing.expectEqual(@as(usize, 128), info.?.length); +} + +test "buildClientHello - valid TLS record" { + const hello = buildClientHello(); + try std.testing.expectEqual(@as(u8, 22), hello.data[0]); // handshake + try std.testing.expectEqual(@as(u8, 0x03), hello.data[1]); // major + try std.testing.expectEqual(@as(u8, 0x01), hello.data[2]); // minor (compat) + try std.testing.expectEqual(@as(u8, 1), hello.data[5]); // ClientHello type + try std.testing.expect(hello.len > 100); + try std.testing.expect(hello.len < 384); +} + +test "TlsInfo - setStr truncates" { + var buf: [8]u8 = .{0} ** 8; + const len = TlsInfo.setStr(&buf, "abcdefghijklmnop"); + try std.testing.expectEqual(@as(u8, 8), len); + try std.testing.expectEqualStrings("abcdefgh", buf[0..len]); +} + +test "TlsInfo - defaults" { + const info = TlsInfo{}; + try std.testing.expect(!info.valid); + try std.testing.expectEqual(@as(u8, 0), info.version_len); + try std.testing.expectEqual(@as(u16, 0), info.san_len); + try std.testing.expectEqual(@as(u8, 0), info.alpn_len); +}