Compare commits

..

No commits in common. "cfcbbb09625aec3a800e8ad40d1b417364037688" and "e311a94e98fd807167681ac214deda55ecde9eed" have entirely different histories.

4 changed files with 120 additions and 142 deletions

View File

@ -27,7 +27,8 @@ func dropPrivileges(userToSwitchTo string) {
// Check supplementary groups.
groups, err := syscall.Getgroups()
if err != nil {
fatalError(err)
fmt.Println(err)
os.Exit(1)
}
for _, groupID := range groups {
if groupID == 0 {
@ -42,53 +43,60 @@ func dropPrivileges(userToSwitchTo string) {
fmt.Println("When running as root, use the -u option to switch to an unprivileged user.")
os.Exit(1)
} else if rootPrimaryGroup || rootSupplementaryGroup {
fatalError("The user running the program is in the root group;\n" +
"use the -u option to switch to an unprivileged user.")
fmt.Println("The user running the program is in the root group;")
fmt.Println("use the -u option to switch to an unprivileged user.")
os.Exit(1)
}
} else { // userToSwitchTo != ""
} else { // userToSwitchTo != ""
// Get user and group IDs for the user we want to switch to.
userInfo, err := user.Lookup(userToSwitchTo)
if err != nil {
fatalError(err)
fmt.Println(err)
os.Exit(1)
}
// Convert group id and user id from string to int.
gid, err := strconv.Atoi(userInfo.Gid)
if err != nil {
fatalError(err)
fmt.Println(err)
os.Exit(1)
}
uid, err := strconv.Atoi(userInfo.Uid)
if err != nil {
fatalError(err)
fmt.Println(err)
os.Exit(1)
}
// If the user we want to switch to has root privileges, stop execution.
if uid == 0 || gid == 0 {
fatalError("Running as root is not allowed.")
fmt.Println("Running as root is not allowed.")
os.Exit(1)
}
// Unset supplementary group IDs.
err = syscall.Setgroups([]int{})
if err != nil {
fmt.Fprintln(os.Stderr,
"Failed to unset supplementary group IDs: "+err.Error())
fmt.Println("Failed to unset supplementary group IDs: " + err.Error())
if rootSupplementaryGroup {
fatalError("Failed to drop root privileges. Exiting...")
fmt.Println("Failed to drop root privileges. Exiting...")
os.Exit(1)
}
}
// Set group ID (real and effective).
err = syscall.Setgid(gid)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to set group ID: "+err.Error())
fmt.Println("Failed to set group ID: " + err.Error())
if rootPrimaryGroup {
fatalError("Failed to drop root privileges. Exiting...")
fmt.Println("Failed to drop root privileges. Exiting...")
os.Exit(1)
}
}
// Set user ID (real and effective).
err = syscall.Setuid(uid)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to set user ID: "+err.Error())
fmt.Println("Failed to set user ID: " + err.Error())
if rootUser {
fatalError("Failed to drop root privileges. Exiting...")
fmt.Println("Failed to drop root privileges. Exiting...")
os.Exit(1)
}
}

View File

@ -1,22 +0,0 @@
# TLS Client Hello Mirror
=> /json/v1 Your browser's TLS Client Hello, reflected as JSON
This test:
* reflects the complete Client Hello message, preserving the order in which TLS parameters and extensions are sent;
* can be used to check for TLS privacy pitfalls (session resumption, TLS fingerprinting, system time exposure);
* supports multiple protocols (currently HTTP and Gemini);
* is free as in freedom and trivial to self-host.
JSON only, for now. The API is largely stable - fields may be added, but existing fields will not be modified or removed. IANA-assigned codes for TLS parameters and extensions are available at:
=> https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml TLS parameters
=> https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml TLS extensions
Note that these lists do not include draft extensions and GREASE values. Missing values will be documented here as the project evolves.
_____________________
=> https://nervuri.net/ Author: nervuri
=> https://tildegit.org/nervuri/client-hello-mirror Source (contributions welcome)
=> https://www.gnu.org/licenses/agpl-3.0.en.html License: AGPL-3.0-or-later

View File

@ -1,62 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#000">
<meta name="referrer" content="no-referrer">
<title>TLS Client Hello Mirror</title>
<style>
:root {
color-scheme: dark;
}
::selection {
color: #FFF;
background-color: #070;
}
body {
color: #DDD;
background-color: #000;
margin: 1em auto;
max-width: 38em;
padding: 0 .62em;
font: 1.1em/1.62 sans-serif;
}
@media print{
body{
max-width: none;
}
}
a:link {color:#EEE;}
a:visited {color:#EEE;}
a:hover {color:#FFF;}
a:active {color:#FFF;}
</style>
<main>
<center>
<h1>TLS Client Hello Mirror</h1>
</center>
<h3><a href="/json/v1">Your browser's TLS Client Hello, reflected as JSON</a></h3>
<p>This test:
<ul>
<li>reflects the complete Client Hello message, preserving the order in which TLS parameters and extensions are sent;</li>
<li>can be used to check for TLS privacy pitfalls (<a href="https://svs.informatik.uni-hamburg.de/publications/2018/2018-12-06-Sy-ACSAC-Tracking_Users_across_the_Web_via_TLS_Session_Resumption.pdf">session resumption</a>, <a href="https://tlsfingerprint.io/">TLS fingerprinting</a>, <a href="https://datatracker.ietf.org/doc/html/draft-mathewson-no-gmtunixtime">system time exposure</a>);</li>
<li>supports both HTTP and <a href="https://gemini.circumlunar.space/">Gemini</a>;</li>
<li>is <a href="https://www.gnu.org/philosophy/free-sw.en.html">free as in freedom</a> and trivial to self-host.</li>
</ul>
</p>
<p>JSON only, for now, but a UI is on <a href="https://tildegit.org/nervuri/client-hello-mirror#roadmap">the roadmap</a>.</p>
<p>The API is largely stable - fields may be added, but existing fields will not be modified or removed. IANA-assigned codes for TLS parameters and extensions are documented at:
<ul>
<li><a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml">TLS parameters</a></li>
<li><a href="https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml">TLS extensions</a></li>
</ul>
Note that these lists do not include draft extensions and <a href="https://datatracker.ietf.org/doc/html/rfc8701">GREASE</a> values. Missing values will be documented here as the project evolves.
</p>
</main>
<hr>
<footer>
Author: <a href="https://nervuri.net/">nervuri</a><br>
<a href="https://tildegit.org/nervuri/client-hello-mirror">Source</a> (contributions welcome)<br>
License: <a href="https://www.gnu.org/licenses/agpl-3.0.en.html">AGPL-3.0-or-later</a>
</footer>
</html>

140
server.go
View File

@ -6,22 +6,18 @@ import (
"bufio"
"bytes"
"crypto/tls"
_ "embed"
"encoding/binary"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net"
"net/url"
"os"
"strings"
"time"
)
const readTimeout = 10 // seconds
const writeTimeout = 10 // seconds
type tlsConnectionInfo struct {
TlsVersion uint16 `json:"tls_version"`
CipherSuite uint16 `json:"cipher_suite"`
@ -29,9 +25,6 @@ type tlsConnectionInfo struct {
NegotiatedProtocol string `json:"alpn_negotiated_protocol"` // ALPN
}
// Connection wrapper that enables exposing the Client Hello to the
// request handler.
// See https://github.com/FiloSottile/mostly-harmless/tree/main/talks/asyncnet
type prefixConn struct {
net.Conn
io.Reader
@ -41,19 +34,91 @@ func (c prefixConn) Read(p []byte) (int, error) {
return c.Reader.Read(p)
}
// Output to stderr and exit with error code 1.
// Like log.Fatal, but without the date&time prefix.
// Used before starting the server loop.
func fatalError(err ...any) {
logger := log.New(os.Stderr, "", 0)
logger.Fatal(err...)
const html = `<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#000">
<meta name="referrer" content="no-referrer">
<title>TLS Client Hello Mirror</title>
<style>
:root {
color-scheme: dark;
}
::selection {
color: #FFF;
background-color: #070;
}
body {
color: #DDD;
background-color: #000;
margin: 1em auto;
max-width: 38em;
padding: 0 .62em;
font: 1.1em/1.62 sans-serif;
}
@media print{
body{
max-width: none;
}
}
a:link {color:#EEE;}
a:visited {color:#EEE;}
a:hover {color:#FFF;}
a:active {color:#FFF;}
</style>
<main>
<center>
<h1>TLS Client Hello Mirror</h1>
</center>
<h3><a href="/json/v1">Your browser's TLS Client Hello, reflected as JSON</a></h3>
<p>This test:
<ul>
<li>reflects the complete Client Hello message, preserving the order in which TLS parameters and extensions are sent;</li>
<li>can be used to check for TLS privacy pitfalls (<a href="https://svs.informatik.uni-hamburg.de/publications/2018/2018-12-06-Sy-ACSAC-Tracking_Users_across_the_Web_via_TLS_Session_Resumption.pdf">session resumption</a>, <a href="https://tlsfingerprint.io/">TLS fingerprinting</a>, <a href="https://datatracker.ietf.org/doc/html/draft-mathewson-no-gmtunixtime">system time exposure</a>);</li>
<li>supports both HTTP and <a href="https://gemini.circumlunar.space/">Gemini</a>;</li>
<li>is <a href="https://www.gnu.org/philosophy/free-sw.en.html">free as in freedom</a> and trivial to self-host.</li>
</ul>
</p>
<p>JSON only, for now, but a UI is on <a href="https://tildegit.org/nervuri/client-hello-mirror#roadmap">the roadmap</a>.</p>
<p>The API is largely stable - fields may be added, but existing fields will not be modified or removed. IANA-assigned codes for TLS parameters and extensions are documented at:
<ul>
<li><a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml">TLS parameters</a></li>
<li><a href="https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml">TLS extensions</a></li>
</ul>
Note that these lists do not include draft extensions and <a href="https://datatracker.ietf.org/doc/html/rfc8701">GREASE</a> values. Missing values will be documented here as the project evolves.
</p>
</main>
<hr>
<footer>
Author: <a href="https://nervuri.net/">nervuri</a><br>
<a href="https://tildegit.org/nervuri/client-hello-mirror">Source</a> (contributions welcome)<br>
License: <a href="https://www.gnu.org/licenses/agpl-3.0.en.html">AGPL-3.0-or-later</a>
</footer>
</html>`
//go:embed index.html
var html string
const gemtext = `# TLS Client Hello Mirror
//go:embed index.gmi
var gemtext string
=> /json/v1 Your browser's TLS Client Hello, reflected as JSON
This test:
* reflects the complete Client Hello message, preserving the order in which TLS parameters and extensions are sent;
* can be used to check for TLS privacy pitfalls (session resumption, TLS fingerprinting, system time exposure);
* supports multiple protocols (currently HTTP and Gemini);
* is free as in freedom and trivial to self-host.
JSON only, for now. The API is largely stable - fields may be added, but existing fields will not be modified or removed. IANA-assigned codes for TLS parameters and extensions are available at:
=> https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml TLS parameters
=> https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml TLS extensions
Note that these lists do not include draft extensions and GREASE values. Missing values will be documented here as the project evolves.
_____________________
=> https://nervuri.net/ Author: nervuri
=> https://tildegit.org/nervuri/client-hello-mirror Source (contributions welcome)
=> https://www.gnu.org/licenses/agpl-3.0.en.html License: AGPL-3.0-or-later`
// Copy the Client Hello message before starting the TLS handshake.
func peek(conn net.Conn, tlsConfig *tls.Config) {
@ -79,24 +144,21 @@ func peek(conn net.Conn, tlsConfig *tls.Config) {
}
rawClientHello := buf.Bytes()
// "Put back" the Client Hello bytes we just read, so that they can be
// used in the TLS handshake. Concatenate the read bytes with the
// unread bytes using a MultiReader, inside a connection wrapper.
pConn := prefixConn{
Conn: conn,
Reader: io.MultiReader(&buf, conn),
}
tlsConnection := tls.Server(pConn, tlsConfig)
err = tlsConnection.Handshake()
server := tls.Server(pConn, tlsConfig)
err = server.Handshake()
if err != nil {
log.Println(err)
return
}
requestHandler(tlsConnection, rawClientHello)
tlsHandler(server, rawClientHello)
}
func requestHandler(conn *tls.Conn, rawClientHello []byte) {
func tlsHandler(conn *tls.Conn, rawClientHello []byte) {
defer conn.Close()
scanner := bufio.NewScanner(conn)
@ -107,6 +169,7 @@ func requestHandler(conn *tls.Conn, rawClientHello []byte) {
log.Println(err)
return
}
//log.Println(line)
var protocol string
var path string // requested page
@ -212,25 +275,28 @@ func main() {
flag.Parse()
hostAndPort = flag.Arg(0)
if certFile == "" || keyFile == "" || hostAndPort == "" {
fatalError("usage: client-hello-mirror -c cert.pem -k key.pem [-u user] host:port")
fmt.Println("usage: client-hello-mirror -c cert.pem -k key.pem [-u user] host:port")
return
}
// Load cert
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
fatalError(err)
log.Fatal(err)
return
}
// TLS config
tlsConfig := tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS10,
NextProtos: []string{"http/1.1"},
//MaxVersion: tls.VersionTLS12,
NextProtos: []string{"http/1.1"},
}
// Listen for connections
ln, err := net.Listen("tcp", hostAndPort)
if err != nil {
fatalError(err)
log.Println(err)
return
}
defer ln.Close()
@ -239,24 +305,12 @@ func main() {
log.Println("Server started")
for {
// Wait for a connection.
conn, err := ln.Accept()
if err != nil {
log.Println("Error accepting: ", err.Error())
continue
}
// Set connection timeouts.
err = conn.SetReadDeadline(time.Now().Add(readTimeout * time.Second))
if err != nil {
log.Println("SetReadDeadline error: ", err.Error())
continue
}
err = conn.SetWriteDeadline(time.Now().Add(writeTimeout * time.Second))
if err != nil {
log.Println("SetWriteDeadline error: ", err.Error())
continue
}
// Process the request.
conn.SetReadDeadline(time.Now().Add(5 * time.Second)) // timeout
go peek(conn, &tlsConfig)
}
}