2022-04-09 22:30:36 +08:00
|
|
|
package sniffer
|
2022-03-30 23:54:52 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
2022-04-09 22:30:36 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/constant/sniffer"
|
2022-03-30 23:54:52 +08:00
|
|
|
)
|
|
|
|
|
2022-04-09 22:30:36 +08:00
|
|
|
var (
|
|
|
|
errNotTLS = errors.New("not TLS header")
|
|
|
|
errNotClientHello = errors.New("not client hello")
|
|
|
|
)
|
2022-03-30 23:54:52 +08:00
|
|
|
|
2023-01-23 13:16:25 +08:00
|
|
|
var _ sniffer.Sniffer = (*TLSSniffer)(nil)
|
|
|
|
|
2022-04-09 22:30:36 +08:00
|
|
|
type TLSSniffer struct {
|
2023-01-23 13:16:25 +08:00
|
|
|
*BaseSniffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTLSSniffer(snifferConfig SnifferConfig) (*TLSSniffer, error) {
|
2023-06-04 11:51:30 +08:00
|
|
|
ports := snifferConfig.Ports
|
|
|
|
if len(ports) == 0 {
|
|
|
|
ports = utils.IntRanges[uint16]{utils.NewRange[uint16](443, 443)}
|
2023-01-23 13:16:25 +08:00
|
|
|
}
|
|
|
|
return &TLSSniffer{
|
|
|
|
BaseSniffer: NewBaseSniffer(ports, C.TCP),
|
|
|
|
}, nil
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
|
2022-04-09 22:30:36 +08:00
|
|
|
func (tls *TLSSniffer) Protocol() string {
|
2022-03-30 23:54:52 +08:00
|
|
|
return "tls"
|
|
|
|
}
|
|
|
|
|
2022-04-09 22:30:36 +08:00
|
|
|
func (tls *TLSSniffer) SupportNetwork() C.NetWork {
|
|
|
|
return C.TCP
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
|
2023-10-19 18:30:20 +08:00
|
|
|
func (tls *TLSSniffer) SniffData(bytes []byte) (string, error) {
|
2022-04-09 22:30:36 +08:00
|
|
|
domain, err := SniffTLS(bytes)
|
|
|
|
if err == nil {
|
|
|
|
return *domain, nil
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2022-03-30 23:54:52 +08:00
|
|
|
|
|
|
|
func IsValidTLSVersion(major, minor byte) bool {
|
|
|
|
return major == 3
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadClientHello returns server name (if any) from TLS client hello message.
|
|
|
|
// https://github.com/golang/go/blob/master/src/crypto/tls/handshake_messages.go#L300
|
2022-04-09 22:30:36 +08:00
|
|
|
func ReadClientHello(data []byte) (*string, error) {
|
2022-03-30 23:54:52 +08:00
|
|
|
if len(data) < 42 {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, ErrNoClue
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
sessionIDLen := int(data[38])
|
|
|
|
if sessionIDLen > 32 || len(data) < 39+sessionIDLen {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, ErrNoClue
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
data = data[39+sessionIDLen:]
|
|
|
|
if len(data) < 2 {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, ErrNoClue
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
// cipherSuiteLen is the number of bytes of cipher suite numbers. Since
|
|
|
|
// they are uint16s, the number must be even.
|
|
|
|
cipherSuiteLen := int(data[0])<<8 | int(data[1])
|
|
|
|
if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
data = data[2+cipherSuiteLen:]
|
|
|
|
if len(data) < 1 {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, ErrNoClue
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
compressionMethodsLen := int(data[0])
|
|
|
|
if len(data) < 1+compressionMethodsLen {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, ErrNoClue
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
data = data[1+compressionMethodsLen:]
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
if len(data) < 2 {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extensionsLength := int(data[0])<<8 | int(data[1])
|
|
|
|
data = data[2:]
|
|
|
|
if extensionsLength != len(data) {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for len(data) != 0 {
|
|
|
|
if len(data) < 4 {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
extension := uint16(data[0])<<8 | uint16(data[1])
|
|
|
|
length := int(data[2])<<8 | int(data[3])
|
|
|
|
data = data[4:]
|
|
|
|
if len(data) < length {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if extension == 0x00 { /* extensionServerName */
|
|
|
|
d := data[:length]
|
|
|
|
if len(d) < 2 {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
namesLen := int(d[0])<<8 | int(d[1])
|
|
|
|
d = d[2:]
|
|
|
|
if len(d) != namesLen {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
for len(d) > 0 {
|
|
|
|
if len(d) < 3 {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
nameType := d[0]
|
|
|
|
nameLen := int(d[1])<<8 | int(d[2])
|
|
|
|
d = d[3:]
|
|
|
|
if len(d) < nameLen {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
if nameType == 0 {
|
|
|
|
serverName := string(d[:nameLen])
|
|
|
|
// An SNI value may not include a
|
|
|
|
// trailing dot. See
|
|
|
|
// https://tools.ietf.org/html/rfc6066#section-3.
|
|
|
|
if strings.HasSuffix(serverName, ".") {
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotClientHello
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
2022-04-09 22:30:36 +08:00
|
|
|
|
|
|
|
return &serverName, nil
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
2022-04-09 22:30:36 +08:00
|
|
|
|
2022-03-30 23:54:52 +08:00
|
|
|
d = d[nameLen:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data = data[length:]
|
|
|
|
}
|
|
|
|
|
2022-04-09 22:30:36 +08:00
|
|
|
return nil, errNotTLS
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
|
2022-04-09 22:30:36 +08:00
|
|
|
func SniffTLS(b []byte) (*string, error) {
|
2022-03-30 23:54:52 +08:00
|
|
|
if len(b) < 5 {
|
|
|
|
return nil, ErrNoClue
|
|
|
|
}
|
|
|
|
|
|
|
|
if b[0] != 0x16 /* TLS Handshake */ {
|
|
|
|
return nil, errNotTLS
|
|
|
|
}
|
|
|
|
if !IsValidTLSVersion(b[1], b[2]) {
|
|
|
|
return nil, errNotTLS
|
|
|
|
}
|
|
|
|
headerLen := int(binary.BigEndian.Uint16(b[3:5]))
|
|
|
|
if 5+headerLen > len(b) {
|
|
|
|
return nil, ErrNoClue
|
|
|
|
}
|
|
|
|
|
2022-04-09 22:30:36 +08:00
|
|
|
domain, err := ReadClientHello(b[5 : 5+headerLen])
|
2022-03-30 23:54:52 +08:00
|
|
|
if err == nil {
|
2022-04-09 22:30:36 +08:00
|
|
|
return domain, nil
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|