Initial commit: LAN RDP with keylogger support
This commit is contained in:
commit
c4adcbbe0f
33 files changed
+4335
No files matched your search
Executable
+105
@@ -0,0 +1,105 @@
|
||||
// Package tlsutil handles certificate generation and trust for lanrdp.
|
||||
//
|
||||
// There is no CA and no public PKI involved: the host generates a
|
||||
// self-signed certificate once and keeps it. Clients pin the host's
|
||||
// certificate fingerprint on first connect (trust-on-first-use, the same
|
||||
// model SSH uses for host keys) and refuse to proceed silently if it ever
|
||||
// changes — that would mean either the host was reinstalled or someone is
|
||||
// intercepting the connection.
|
||||
package tlsutil
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const certValidity = 10 * 365 * 24 * time.Hour
|
||||
|
||||
// LoadOrCreateServerCert loads a persisted self-signed certificate/key pair
|
||||
// from dir, generating a fresh one on first run.
|
||||
func LoadOrCreateServerCert(dir string) (tls.Certificate, error) {
|
||||
certPath := filepath.Join(dir, "host.crt")
|
||||
keyPath := filepath.Join(dir, "host.key")
|
||||
|
||||
if cert, err := tls.LoadX509KeyPair(certPath, keyPath); err == nil {
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
|
||||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
|
||||
serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
||||
if err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
|
||||
tmpl := &x509.Certificate{
|
||||
SerialNumber: serial,
|
||||
Subject: pkix.Name{CommonName: "lanrdp-host"},
|
||||
NotBefore: time.Now().Add(-time.Hour),
|
||||
NotAfter: time.Now().Add(certValidity),
|
||||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: true,
|
||||
}
|
||||
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &priv.PublicKey, priv)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
|
||||
certOut, err := os.OpenFile(certPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
defer certOut.Close()
|
||||
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: der}); err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
|
||||
keyBytes, err := x509.MarshalECPrivateKey(priv)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
|
||||
if err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
defer keyOut.Close()
|
||||
if err := pem.Encode(keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: keyBytes}); err != nil {
|
||||
return tls.Certificate{}, err
|
||||
}
|
||||
|
||||
return tls.LoadX509KeyPair(certPath, keyPath)
|
||||
}
|
||||
|
||||
// Fingerprint returns a SHA-256 fingerprint of a DER-encoded certificate,
|
||||
// formatted like an SSH key fingerprint (colon-separated hex) for easy
|
||||
// out-of-band comparison.
|
||||
func Fingerprint(der []byte) string {
|
||||
sum := sha256.Sum256(der)
|
||||
parts := make([]string, len(sum))
|
||||
for i, b := range sum {
|
||||
parts[i] = fmt.Sprintf("%02x", b)
|
||||
}
|
||||
return strings.Join(parts, ":")
|
||||
}
|
||||
Executable
+108
@@ -0,0 +1,108 @@
|
||||
package tlsutil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TrustedDevice is one entry in the trust store: a certificate fingerprint
|
||||
// this client has approved, plus a human-friendly label so multiple
|
||||
// distinct devices (a laptop, a desktop, a tablet, ...) stay identifiable
|
||||
// instead of being a wall of hex fingerprints.
|
||||
type TrustedDevice struct {
|
||||
Label string `json:"label"`
|
||||
FirstSeen time.Time `json:"first_seen"`
|
||||
LastSeen time.Time `json:"last_seen"`
|
||||
}
|
||||
|
||||
// TrustStore is a client-side trust-on-first-use record of which
|
||||
// certificate fingerprints have been approved, keyed by fingerprint
|
||||
// rather than by network address or listen endpoint.
|
||||
//
|
||||
// Trust has to travel with the certificate's identity, not the connection
|
||||
// path: the same device can be reached at different addresses over time
|
||||
// (DHCP renewal, roaming), and — since multiple different devices can all
|
||||
// call back to the same -listen endpoint — several distinct fingerprints
|
||||
// legitimately need to be trusted under what used to be a single
|
||||
// address-keyed slot. Keying by fingerprint is also just the same model
|
||||
// SSH's known_hosts uses: it records which keys you've approved, not
|
||||
// which address you approved them at.
|
||||
type TrustStore struct {
|
||||
path string
|
||||
entries map[string]TrustedDevice // key: fingerprint
|
||||
}
|
||||
|
||||
func LoadTrustStore(path string) (*TrustStore, error) {
|
||||
ts := &TrustStore{path: path, entries: map[string]TrustedDevice{}}
|
||||
data, err := os.ReadFile(path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return ts, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal(data, &ts.entries); err != nil {
|
||||
// Most likely an older known_hosts.json from before multi-device
|
||||
// support (address-keyed, plain fingerprint-string values, which
|
||||
// won't unmarshal into TrustedDevice). Rather than fail to start,
|
||||
// treat it as empty — every device just gets re-trusted once on
|
||||
// its next connection.
|
||||
ts.entries = map[string]TrustedDevice{}
|
||||
}
|
||||
return ts, nil
|
||||
}
|
||||
|
||||
func (ts *TrustStore) save() error {
|
||||
if err := os.MkdirAll(filepath.Dir(ts.path), 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.MarshalIndent(ts.entries, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(ts.path, data, 0o600)
|
||||
}
|
||||
|
||||
// Lookup returns the trusted entry for fingerprint, if any.
|
||||
func (ts *TrustStore) Lookup(fingerprint string) (TrustedDevice, bool) {
|
||||
d, ok := ts.entries[fingerprint]
|
||||
return d, ok
|
||||
}
|
||||
|
||||
// Trust records fingerprint as approved under label, persisting to disk.
|
||||
// Calling it again for an already-trusted fingerprint updates the label
|
||||
// and LastSeen without touching FirstSeen.
|
||||
func (ts *TrustStore) Trust(fingerprint, label string) error {
|
||||
d, existed := ts.entries[fingerprint]
|
||||
now := time.Now()
|
||||
if !existed {
|
||||
d.FirstSeen = now
|
||||
}
|
||||
d.Label = label
|
||||
d.LastSeen = now
|
||||
ts.entries[fingerprint] = d
|
||||
return ts.save()
|
||||
}
|
||||
|
||||
// Touch updates LastSeen for an already-trusted fingerprint. Intended to
|
||||
// be non-fatal to callers — losing a last-seen timestamp isn't worth
|
||||
// aborting a connection over.
|
||||
func (ts *TrustStore) Touch(fingerprint string) error {
|
||||
d, ok := ts.entries[fingerprint]
|
||||
if !ok {
|
||||
return fmt.Errorf("tlsutil: %s is not a trusted fingerprint", fingerprint)
|
||||
}
|
||||
d.LastSeen = time.Now()
|
||||
ts.entries[fingerprint] = d
|
||||
return ts.save()
|
||||
}
|
||||
|
||||
// All returns every trusted device, keyed by fingerprint, for listing or
|
||||
// management. Callers must not mutate the returned map.
|
||||
func (ts *TrustStore) All() map[string]TrustedDevice {
|
||||
return ts.entries
|
||||
}
|
||||
Reference in new issue
Block a user