add tls key logging

This commit is contained in:
Page Fault
2020-05-26 15:55:43 +00:00
parent a37066dd4b
commit 958c2b64e7
4 changed files with 26 additions and 1 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
[![HitCounts](http://hits.dwyl.io/p4gefau1t/trojan-go.svg)](http://hits.dwyl.io/p4gefau1t/trojan-go)
[![Release](https://img.shields.io/github/v/release/p4gefau1t/trojan-go?include_prereleases)](https://img.shields.io/github/v/release/p4gefau1t/trojan-go?include_prereleases)
[![Release Date](https://img.shields.io/github/release-date-pre/p4gefau1t/trojan-go)](https://img.shields.io/github/release-date-pre/p4gefau1t/trojan-go)
[![Docker Image](https://images.microbadger.com/badges/image/p4gefau1t/trojan-go.svg)](https://microbadger.com/images/p4gefau1t/trojan-go)
[![Docker Image](https://images.microbadger.com/badges/image/p4gefau1t/trojan-go.svg)](https://hub.docker.com/r/p4gefau1t/trojan-go)
[![Commit](https://img.shields.io/github/last-commit/p4gefau1t/trojan-go)](https://img.shields.io/github/last-commit/p4gefau1t/trojan-go)
[![Commit Activity](https://img.shields.io/github/commit-activity/m/p4gefau1t/trojan-go)](https://img.shields.io/github/commit-activity/m/p4gefau1t/trojan-go)
+3
View File
@@ -3,6 +3,7 @@ package conf
import (
"crypto/tls"
"crypto/x509"
"io"
"github.com/p4gefau1t/trojan-go/common"
utls "github.com/refraction-networking/utls"
@@ -45,6 +46,7 @@ type TLSConfig struct {
Curves string `json:"curves"`
Fingerprint string `json:"fingerprint"`
ServePlainText bool `json:"serve_plain_text"`
KeyLogPath string `json:"key_log"`
ClientHelloID *utls.ClientHelloID
FallbackAddress *common.Address
@@ -56,6 +58,7 @@ type TLSConfig struct {
CipherSuiteTLS13 []uint16
SessionTicket bool
CurvePreferences []tls.CurveID
KeyLogger io.Writer
}
type TCPConfig struct {
+20
View File
@@ -18,7 +18,23 @@ import (
"golang.org/x/crypto/pbkdf2"
)
func setKeyLogger(tlsConfig *TLSConfig) error {
if tlsConfig.KeyLogPath != "" {
log.Warn("TLS key logging activated. USE OF KEY LOGGING COMPROMISES SECURITY. IT SHOULD ONLY BE USED FOR DEBUGGING.")
file, err := os.OpenFile(tlsConfig.KeyLogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return common.NewError("Failed to open key log file").Base(err)
}
tlsConfig.KeyLogger = file
}
return nil
}
func loadCert(tlsConfig *TLSConfig) error {
err := setKeyLogger(tlsConfig)
if err != nil {
return err
}
if tlsConfig.CertPath == "" {
log.Info("Cert of the remote server is unspecified. Using default CA list")
} else {
@@ -55,6 +71,10 @@ func loadCert(tlsConfig *TLSConfig) error {
}
func loadCertAndKey(tlsConfig *TLSConfig) error {
err := setKeyLogger(tlsConfig)
if err != nil {
return err
}
if tlsConfig.KeyPassword != "" {
keyFile, err := ioutil.ReadFile(tlsConfig.KeyPath)
if err != nil {
+2
View File
@@ -326,6 +326,7 @@ func (m *TLSManager) dialTLSWithFakeFingerprint() (*utls.UConn, error) {
RootCAs: m.config.TLS.CertPool,
ServerName: m.config.TLS.SNI,
InsecureSkipVerify: !m.config.TLS.Verify,
KeyLogWriter: m.config.TLS.KeyLogger,
}
if workingFingerprint != "" {
spec, err := m.genClientSpec(workingFingerprint)
@@ -414,6 +415,7 @@ func (m *TLSManager) DialToServer() (io.ReadWriteCloser, error) {
CurvePreferences: m.config.TLS.CurvePreferences,
NextProtos: m.config.TLS.ALPN,
ClientSessionCache: m.sessionCache,
KeyLogWriter: m.config.TLS.KeyLogger,
}
tlsConn := tls.Client(tcpConn, tlsConfig)
err = tlsConn.Handshake()