Initial upload
This commit is contained in:
1 parent
b0bf3b7167
commit
a022f29e34
65 files changed
+46484
-4339
No files matched your search
+2287
File diff suppressed because it is too large.
Load diff
Executable
BIN
Binary file not shown.
@@ -0,0 +1,108 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func mkKey(t *testing.T) ssh.PublicKey {
|
||||
t.Helper()
|
||||
pub, _, err := ed25519.GenerateKey(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
k, err := ssh.NewPublicKey(pub)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return k
|
||||
}
|
||||
|
||||
func TestTOFUPerUserPinning(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
store := filepath.Join(dir, "data", "ssh_known_hosts.json")
|
||||
addr := &net.TCPAddr{IP: net.IPv4(10, 0, 0, 9), Port: 22}
|
||||
|
||||
real := mkKey(t)
|
||||
trap := mkKey(t)
|
||||
|
||||
userA := tofuHostKeyCallback(store, "userA", false)
|
||||
|
||||
// userA first use pins real key.
|
||||
if err := userA("srv.example:22", addr, real); err != nil {
|
||||
t.Fatalf("userA first-use should pin, got: %v", err)
|
||||
}
|
||||
// userA reconnect with same key is fine.
|
||||
if err := userA("srv.example:22", addr, real); err != nil {
|
||||
t.Fatalf("userA same key should be allowed, got: %v", err)
|
||||
}
|
||||
// userA presented a DIFFERENT key on reconnect => MITM, rejected.
|
||||
if err := userA("srv.example:22", addr, trap); err == nil {
|
||||
t.Fatal("userA mismatched key MUST be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
// Core of the reported attack: a malicious user pre-pins a TRAP key; the victim
|
||||
// must NOT inherit it. The victim either pins the real key for themselves, or
|
||||
// (if the attacker also intercepts the victim) sees a divergence rejection.
|
||||
func TestTOFUPrePinningDoesNotPoisonVictim(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
store := filepath.Join(dir, "data", "ssh_known_hosts.json")
|
||||
addr := &net.TCPAddr{IP: net.IPv4(10, 0, 0, 9), Port: 22}
|
||||
|
||||
real := mkKey(t)
|
||||
trap := mkKey(t)
|
||||
|
||||
attacker := tofuHostKeyCallback(store, "attacker", false)
|
||||
victim := tofuHostKeyCallback(store, "victim", false)
|
||||
|
||||
// Attacker pre-pins a trap key for the host (their own store only).
|
||||
if err := attacker("newsrv:22", addr, trap); err != nil {
|
||||
t.Fatalf("attacker pre-pin setup failed: %v", err)
|
||||
}
|
||||
|
||||
// Victim connects to the REAL server (sees the real key). Because the
|
||||
// attacker recorded a different key globally, the victim must be WARNED
|
||||
// (divergence) rather than silently trusting anything.
|
||||
if err := victim("newsrv:22", addr, real); err == nil {
|
||||
t.Fatal("victim must be warned about divergence, not silently allowed")
|
||||
} else if !strings.Contains(strings.ToLower(err.Error()), "divergence") {
|
||||
t.Fatalf("expected divergence error, got: %v", err)
|
||||
}
|
||||
|
||||
// Critically: the victim is NEVER bound to the attacker's trap key.
|
||||
st := loadKnownHosts(store)
|
||||
if got := st.Pins[userHostKey("victim", "newsrv:22")]; got == hostKeyFingerprint(trap) {
|
||||
t.Fatal("victim must NOT inherit the attacker's pinned key")
|
||||
}
|
||||
|
||||
// After explicit, informed acceptance, the victim pins the REAL key only.
|
||||
victimAccept := tofuHostKeyCallback(store, "victim", true)
|
||||
if err := victimAccept("newsrv:22", addr, real); err != nil {
|
||||
t.Fatalf("victim explicit-accept of real key failed: %v", err)
|
||||
}
|
||||
st = loadKnownHosts(store)
|
||||
if got := st.Pins[userHostKey("victim", "newsrv:22")]; got != hostKeyFingerprint(real) {
|
||||
t.Fatalf("victim should be pinned to the REAL key, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTOFUFailsClosedOnUnwritableStore(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
blocker := filepath.Join(dir, "blocker")
|
||||
if err := os.WriteFile(blocker, []byte("x"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := filepath.Join(blocker, "data", "known.json")
|
||||
cb := tofuHostKeyCallback(store, "u", false)
|
||||
addr := &net.TCPAddr{IP: net.IPv4(10, 0, 0, 9), Port: 22}
|
||||
if err := cb("h:22", addr, mkKey(t)); err == nil {
|
||||
t.Fatal("must fail closed when the key store is unavailable")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user