Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

beacon_ingest: serve the named pipe on Windows

Claudee3ce0067aecd
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension.go
index 8124ab2a..00b85853 100644
--- a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension.go
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension.go
@@ -1,6 +1,8 @@
// Package beaconingestextension is the collector side of the restricted log access mode: it
-// listens on the local ingest socket and appends the events hooks send there to the runtime log,
-// so a system-mode endpoint can keep that log 0640 root:beacon-log instead of world-writable.
+// listens on the local ingest socket (a named pipe on Windows) and appends the events hooks send
+// there to the runtime log, so a system-mode endpoint can keep that log 0640 root:beacon-log, or on
+// Windows closed to everyone but SYSTEM, Administrators and the readers group, instead of writable
+// by every user.
//
// Each event is stamped with the sender's kernel-verified account (localingest.StampLine) and
// written through the same lock, rotation and file-mode handling as the beaconjson exporter
@@ -51,9 +53,10 @@ func newIngest(cfg *Config, logger *zap.Logger) *ingest {
}
}
-// Start creates the socket and serves it in the background. A socket another running collector
-// still answers on, or a non-socket at the path, fails the start: two writers serving one log, or
-// a path someone put something else at, is a configuration problem to surface, not to work around.
+// Start creates the socket or pipe and serves it in the background. A socket another running
+// collector still answers on, a non-socket at the path, or a pipe that already exists (another
+// collector, or an account that took the name first) fails the start: two writers serving one log,
+// or a name someone else holds, is a problem to surface, not to work around.
func (e *ingest) Start(_ context.Context, host component.Host) error {
if !asymptoteobserve.RuntimeLogAccessRestricted(e.cfg.LogPath) {
// Harmless -- the log is simply also writable directly -- but not what install sets up,
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension_windows_test.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension_windows_test.go
new file mode 100644
index 00000000..6095cd83
--- /dev/null
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension_windows_test.go
@@ -0,0 +1,156 @@
+//go:build windows
+
+package beaconingestextension
+
+import (
+ "bufio"
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "os"
+ "os/user"
+ "path/filepath"
+ "testing"
+ "time"
+
+ "go.uber.org/zap"
+ "golang.org/x/sys/windows"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/localingest"
+)
+
+func currentSID(t *testing.T) string {
+ t.Helper()
+ u, err := windows.GetCurrentProcessToken().GetTokenUser()
+ if err != nil {
+ t.Fatal(err)
+ }
+ return u.User.Sid.String()
+}
+
+type winFixture struct {
+ ext *ingest
+ client localingest.Client
+ log string
+ pipe string
+}
+
+func startOnPipe(t *testing.T) *winFixture {
+ t.Helper()
+ cfg := createDefaultConfig()
+ cfg.SocketPath = fmt.Sprintf(`\\.\pipe\beacon-ingest-ext-test-%d-%d`, os.Getpid(), time.Now().UnixNano())
+ cfg.LogPath = filepath.Join(t.TempDir(), "logs", "runtime.jsonl")
+ if err := os.MkdirAll(filepath.Dir(cfg.LogPath), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ ext := newIngest(cfg, zap.NewNop())
+ if err := ext.Start(context.Background(), nil); err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = ext.Shutdown(context.Background()) })
+ return &winFixture{ext: ext, log: cfg.LogPath, pipe: cfg.SocketPath,
+ client: localingest.Client{SocketPath: cfg.SocketPath, TrustedSID: currentSID(t), LogPath: cfg.LogPath, Timeout: 5 * time.Second}}
+}
+
+func winEvent(t *testing.T, i int) []byte {
+ t.Helper()
+ data, err := json.Marshal(map[string]interface{}{
+ "timestamp": fmt.Sprintf("2026-10-30T09:03:%02d.000000000Z", i%60),
+ "vendor": asymptoteobserve.Vendor,
+ "product": asymptoteobserve.Product,
+ "schema_version": asymptoteobserve.SchemaVersion,
+ "event": map[string]interface{}{"action": "command.executed", "category": "command"},
+ "session": map[string]interface{}{"id": fmt.Sprintf("s-%d", i)},
+ "user": map[string]interface{}{"name": `NT AUTHORITY\SYSTEM`, "uid": "S-1-5-18"},
+ "message": fmt.Sprintf("event %d", i),
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ return data
+}
+
+func TestEventsSentToThePipeLandInTheLogUnderTheSendersAccount(t *testing.T) {
+ f := startOnPipe(t)
+ resp, err := f.client.Send([][]byte{winEvent(t, 1), winEvent(t, 2)})
+ if err != nil || resp.Accepted != 2 {
+ t.Fatalf("Send = %+v, %v", resp, err)
+ }
+ file, err := os.Open(f.log)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer file.Close()
+ sid := currentSID(t)
+ me, _ := user.Current()
+ var n int
+ scanner := bufio.NewScanner(file)
+ for scanner.Scan() {
+ var e map[string]interface{}
+ if err := json.Unmarshal(scanner.Bytes(), &e); err != nil {
+ t.Fatalf("log line is not one JSON object: %q", scanner.Text())
+ }
+ u := e["user"].(map[string]interface{})
+ if u["uid"] != sid {
+ t.Fatalf("user.uid = %v; the event claimed SYSTEM and must carry the sender's SID %s", u["uid"], sid)
+ }
+ if me != nil && u["name"] != me.Username {
+ t.Fatalf("user.name = %v, want %q", u["name"], me.Username)
+ }
+ n++
+ }
+ if n != 2 {
+ t.Fatalf("log has %d lines, want 2", n)
+ }
+}
+
+func TestASecondCollectorCannotServeTheSamePipe(t *testing.T) {
+ f := startOnPipe(t)
+ cfg := *f.ext.cfg
+ second := newIngest(&cfg, zap.NewNop())
+ if err := second.Start(context.Background(), nil); err == nil {
+ _ = second.Shutdown(context.Background())
+ t.Fatal("a second collector started on a pipe another one serves")
+ }
+}
+
+func TestShutdownLeavesNoPipe(t *testing.T) {
+ f := startOnPipe(t)
+ if err := f.client.Probe(); err != nil {
+ t.Fatalf("Probe = %v", err)
+ }
+ if err := f.ext.Shutdown(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ if err := f.client.Probe(); !errors.Is(err, localingest.ErrNoSocket) {
+ t.Fatalf("Probe after Shutdown = %v, want ErrNoSocket", err)
+ }
+}
+
+func TestResolveNameNamesTheCurrentAccount(t *testing.T) {
+ me, err := user.Current()
+ if err != nil {
+ t.Skip(err)
+ }
+ if got := resolveName(currentSID(t)); got != me.Username {
+ t.Fatalf("resolveName = %q, want %q", got, me.Username)
+ }
+ if got := resolveName("S-1-5-21-1-2-3-999999"); got != "" {
+ t.Fatalf("resolveName of an unknown SID = %q", got)
+ }
+}
+
+func TestValidWindowsName(t *testing.T) {
+ for _, ok := range []string{`CONTOSO\Jane Doe`, `DESKTOP-1\runneradmin`, `NT AUTHORITY\SYSTEM`} {
+ if !validWindowsName(ok) {
+ t.Errorf("validWindowsName(%q) = false", ok)
+ }
+ }
+ for _, bad := range []string{"", " lead", "trail ", "nl\nx", "esc\x1b[31m", "del\x7f", string(make([]byte, 257))} {
+ if validWindowsName(bad) {
+ t.Errorf("validWindowsName(%q) = true", bad)
+ }
+ }
+}
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names.go
index bcedd1a8..f4d224aa 100644
--- a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names.go
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names.go
@@ -1,19 +1,14 @@
package beaconingestextension
import (
- "context"
- "os"
- "os/exec"
- "os/user"
- "strings"
"sync"
"time"
)
const (
- // nameLookupTimeout bounds one id(1) call. The name is resolved once per connection, inside the
- // server's budget for it, so a directory service that does not answer costs one connection
- // half a second and leaves the event named by uid only.
+ // nameLookupTimeout bounds one id(1) call on Unix. The name is resolved once per connection,
+ // inside the server's budget for it, so a directory service that does not answer costs one
+ // connection half a second and leaves the event named by its account id only.
nameLookupTimeout = 500 * time.Millisecond
// nameCacheTTL bounds how long a uid's name is reused. Accounts are renamed rarely; a miss is
// retried after the same interval, so a directory service that was briefly unreachable does
@@ -21,12 +16,8 @@ const (
nameCacheTTL = 10 * time.Minute
)
-// nameCache resolves the account name stamped on each event from the peer's uid.
-//
-// The collector is built with CGO_ENABLED=0, whose os/user reads /etc/passwd and never asks NSS
-// or Directory Services, so accounts from LDAP, SSSD, AD or macOS's own directory would come back
-// unnamed. `id -nu <uid>` asks the system's own resolver on both Linux and macOS and is the
-// fallback.
+// nameCache resolves the account name stamped on each event from the peer's account: a uid on
+// Unix, a SID on Windows (see resolveName for each).
type nameCache struct {
mu sync.Mutex
entries map[string]nameEntry
@@ -58,42 +49,6 @@ func (c *nameCache) lookup(uid string) string {
return name
}
-// idCommands are the absolute paths tried for id(1). The collector runs as root, so PATH is not
-// consulted.
-var idCommands = []string{"/usr/bin/id", "/bin/id"}
-
-// firstExecutable is the first of paths that exists as an executable file.
-func firstExecutable(paths []string) string {
- for _, path := range paths {
- if info, err := os.Stat(path); err == nil && info.Mode().IsRegular() && info.Mode().Perm()&0o111 != 0 {
- return path
- }
- }
- return ""
-}
-
-func resolveName(id string) string {
- if u, err := user.LookupId(id); err == nil && validName(u.Username) {
- return u.Username
- }
- // id(1) is tried once: on merged-/usr systems both paths are the same binary, and running it
- // again behind a directory service that did not answer would only double the wait.
- path := firstExecutable(idCommands)
- if path == "" {
- return ""
- }
- ctx, cancel := context.WithTimeout(context.Background(), nameLookupTimeout)
- out, err := exec.CommandContext(ctx, path, "-nu", id).Output()
- cancel()
- if err != nil {
- return ""
- }
- if name := strings.TrimSpace(string(out)); validName(name) {
- return name
- }
- return ""
-}
-
// validName accepts what an account name can be and nothing that could disturb a log line or a
// reader: no control characters or whitespace, bounded length.
func validName(name string) bool {
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_test.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_test.go
index df5bef75..3ab83e35 100644
--- a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_test.go
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_test.go
@@ -81,21 +81,3 @@ func TestValidName(t *testing.T) {
}
}
}
-
-func TestFirstExecutable(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("no execute bits")
- }
- dir := t.TempDir()
- plain := dir + "/plain"
- exe := dir + "/exe"
- _ = os.WriteFile(plain, nil, 0o644)
- _ = os.WriteFile(exe, nil, 0o755)
- _ = os.Chmod(exe, 0o755)
- if got := firstExecutable([]string{dir + "/missing", plain, exe}); got != exe {
- t.Fatalf("firstExecutable = %q, want %q", got, exe)
- }
- if got := firstExecutable([]string{dir + "/missing"}); got != "" {
- t.Fatalf("firstExecutable = %q, want none", got)
- }
-}
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_unix.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_unix.go
new file mode 100644
index 00000000..4e13050d
--- /dev/null
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_unix.go
@@ -0,0 +1,52 @@
+//go:build !windows
+
+package beaconingestextension
+
+import (
+ "context"
+ "os"
+ "os/exec"
+ "os/user"
+ "strings"
+)
+
+// resolveName names a uid. The collector is built with CGO_ENABLED=0, whose os/user reads /etc/passwd and never asks NSS or
+// Directory Services, so accounts from LDAP, SSSD, AD or macOS's own directory would come back
+// unnamed. `id -nu <uid>` asks the system's own resolver on both Linux and macOS and is the
+// fallback.
+
+// idCommands are the absolute paths tried for id(1). The collector runs as root, so PATH is not
+// consulted.
+var idCommands = []string{"/usr/bin/id", "/bin/id"}
+
+// firstExecutable is the first of paths that exists as an executable file.
+func firstExecutable(paths []string) string {
+ for _, path := range paths {
+ if info, err := os.Stat(path); err == nil && info.Mode().IsRegular() && info.Mode().Perm()&0o111 != 0 {
+ return path
+ }
+ }
+ return ""
+}
+
+func resolveName(id string) string {
+ if u, err := user.LookupId(id); err == nil && validName(u.Username) {
+ return u.Username
+ }
+ // id(1) is tried once: on merged-/usr systems both paths are the same binary, and running it
+ // again behind a directory service that did not answer would only double the wait.
+ path := firstExecutable(idCommands)
+ if path == "" {
+ return ""
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), nameLookupTimeout)
+ out, err := exec.CommandContext(ctx, path, "-nu", id).Output()
+ cancel()
+ if err != nil {
+ return ""
+ }
+ if name := strings.TrimSpace(string(out)); validName(name) {
+ return name
+ }
+ return ""
+}
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_unix_test.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_unix_test.go
new file mode 100644
index 00000000..5aec5bc3
--- /dev/null
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_unix_test.go
@@ -0,0 +1,23 @@
+//go:build !windows
+
+package beaconingestextension
+
+import (
+ "os"
+ "testing"
+)
+
+func TestFirstExecutable(t *testing.T) {
+ dir := t.TempDir()
+ plain := dir + "/plain"
+ exe := dir + "/exe"
+ _ = os.WriteFile(plain, nil, 0o644)
+ _ = os.WriteFile(exe, nil, 0o755)
+ _ = os.Chmod(exe, 0o755)
+ if got := firstExecutable([]string{dir + "/missing", plain, exe}); got != exe {
+ t.Fatalf("firstExecutable = %q, want %q", got, exe)
+ }
+ if got := firstExecutable([]string{dir + "/missing"}); got != "" {
+ t.Fatalf("firstExecutable = %q, want none", got)
+ }
+}
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_windows.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_windows.go
new file mode 100644
index 00000000..deb5c698
--- /dev/null
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_windows.go
@@ -0,0 +1,33 @@
+//go:build windows
+
+package beaconingestextension
+
+import (
+ "os/user"
+ "strings"
+)
+
+// resolveName names a SID: DOMAIN\user, the form Windows hooks write to user.name. os/user asks
+// LookupAccountSid, which consults the domain on a domain-joined machine; Windows has no id(1) to
+// fall back to.
+func resolveName(sid string) string {
+ u, err := user.LookupId(sid)
+ if err != nil || !validWindowsName(u.Username) {
+ return ""
+ }
+ return u.Username
+}
+
+// validWindowsName is validName with spaces allowed inside the name, as Windows account names
+// allow them ("CONTOSO\Jane Doe"); still no control characters, nothing at either end, bounded.
+func validWindowsName(name string) bool {
+ if name == "" || len(name) > 256 || strings.TrimSpace(name) != name {
+ return false
+ }
+ for _, r := range name {
+ if r < ' ' || r == 0x7f {
+ return false
+ }
+ }
+ return true
+}