Files
SimpleRemoter/server/go/server/tls.go
T
yuanyuanxianganddeepseek-v4-pro a447452521 Fix: Silence SOCKET_DLLLOADER log flood and tighten device-port logging
SOCKET_DLLLOADER (210) is the shellcode / memory-DLL loader asking the
server for the full DLL payload before it can load the client. The MFC
master console serves it via SendServerDll; the Go web device port has no
payload to hand out, so every memory-DLL client startup/reconnect fell
into the default dispatch branch and logged "Unhandled command 210",
flooding the log. Add a protocol constant and drop the command silently.

While auditing the device-port logging surface:

- Downgrade wrapTLS's per-connection hex-dump lines and the screen-spy
  "[timing]" cold-start line from Info to Debug (they fire once per TCP
  connection / screen view and are diagnostics, not operational events).

- Distinguish a sub-connection hijack attempt from a plain orphan: when a
  screen/terminal sub-conn claims a MasterID from a source IP other than
  the device's login conn, log a dedicated Warn instead of mislabeling it
  as "orphan ... unknown device".

Co-Authored-By: deepseek-v4-pro
2026-10-16 12:26:35 +02:00

114 lines
4.1 KiB
Go

package server
import (
"bufio"
"crypto/tls"
"fmt"
"net"
"time"
)
// loadTLSConfig builds the server's one-way TLS configuration from the
// configured cert/key paths, leaving s.tlsCfg nil when TLS is disabled. A cert
// path set without its key (or vice versa) is a misconfiguration: TLS is left
// disabled and a warning is logged rather than guessing. A load failure is
// returned to the caller so startup fails loudly instead of silently serving
// plaintext.
func (s *Server) loadTLSConfig() error {
if s.config.TLSCertFile == "" && s.config.TLSKeyFile == "" {
return nil
}
if s.config.TLSCertFile == "" || s.config.TLSKeyFile == "" {
s.logger.Warn("TLS disabled: both YAMA_TLS_CERT and YAMA_TLS_KEY must be set (cert=%q key=%q)",
s.config.TLSCertFile, s.config.TLSKeyFile)
return nil
}
cert, err := tls.LoadX509KeyPair(s.config.TLSCertFile, s.config.TLSKeyFile)
if err != nil {
return fmt.Errorf("load TLS cert/key: %w", err)
}
s.tlsCfg = &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
// One-way TLS: no ClientAuth / ClientCAs. Client identity is still
// established by the in-tunnel login (V1 PwdHmac / V2 ECDSA).
}
s.logger.Info("TLS enabled (one-way): cert=%s", s.config.TLSCertFile)
return nil
}
// isTLSClientHello reports whether b begins with a TLS handshake record whose
// first message is a ClientHello:
//
// 0x16 — handshake record type
// 0x03 — TLS major version (all TLS 1.x / 1.3 records)
// 0x01 (b[5])— handshake message type ClientHello
//
// The first two bytes alone are insufficient: the C++ client obfuscates its
// "HELL" header (common/header.h GetHead) with a per-connection key, so a
// plaintext client can in principle emit 0x16 0x03 as its first two bytes. For
// that obfuscated header byte 5 is always 0x00, and for the plain "Shine"/
// "POST" variants it is an ASCII byte — never 0x01. Requiring b[5]==0x01 makes
// a false positive effectively impossible.
func isTLSClientHello(b []byte) bool {
return len(b) >= 6 && b[0] == 0x16 && b[1] == 0x03 && b[5] == 0x01
}
// bufferedConn replays bytes already peeked from a net.Conn, so the caller can
// sniff a prefix and then hand the connection (peeked bytes included) to the
// TLS or plaintext read loop. Read delegates to the buffered reader; everything
// else delegates to the embedded net.Conn.
type bufferedConn struct {
net.Conn
r *bufio.Reader
}
func (c *bufferedConn) Read(p []byte) (int, error) {
return c.r.Read(p)
}
// wrapTLS sniffs the first bytes of a freshly accepted connection and, when TLS
// is enabled and the peer is speaking TLS, returns a tls.Conn wrapper. The TLS
// handshake is lazy: it runs on the first Read, which handleConnection already
// bounds with its ReadTimeout. Plaintext peers are returned unchanged (wrapped
// in a bufferedConn so the sniffed prefix is replayed).
func (s *Server) wrapTLS(conn net.Conn) net.Conn {
// NewContext only disables Nagle on a *net.TCPConn; once we wrap the conn
// (bufferedConn / tls.Conn) that assertion no longer fires, so disable it
// here on the raw conn to preserve screen-handshake responsiveness.
if tcp, ok := conn.(*net.TCPConn); ok {
_ = tcp.SetNoDelay(true)
}
if s.tlsCfg == nil {
return conn
}
br := bufio.NewReader(conn)
if s.config.ReadTimeout > 0 {
_ = conn.SetReadDeadline(time.Now().Add(s.config.ReadTimeout))
}
hdr, err := br.Peek(6)
_ = conn.SetReadDeadline(time.Time{})
// Peek failed (timeout / EOF): the peer never delivered enough bytes to
// identify itself. Fall through with the raw conn so the read loop surfaces
// EOF/timeout exactly as it does for the plaintext path today. Any <6
// partial bytes already buffered are discarded, which only affects peers
// that were already broken (real clients send ≥13 bytes immediately).
if err != nil {
s.logger.Warn("wrapTLS peek failed for %s: %v", conn.RemoteAddr(), err)
return conn
}
bc := &bufferedConn{Conn: conn, r: br}
if isTLSClientHello(hdr) {
s.logger.Debug("wrapTLS: TLS peer %s first6=%x", conn.RemoteAddr(), hdr)
return tls.Server(bc, s.tlsCfg)
}
s.logger.Debug("wrapTLS: plaintext peer %s first6=%x", conn.RemoteAddr(), hdr)
return bc
}