Skip to content

Commit d3b85bf

Browse files
tetragon/windows: add uid to exec events in Windows
This commit consumes the changes made in process_monitor.c program to send the user's login id as a part of exec event. This id is also enumerated for existing processes during enumeration when tetragon starts. This uid is a unique Login Identifier extracted from user's token, and is same as Token AuthenticationId field of TOKEN_STATISTICS structure, available when token is queried with TokenStatistics information class. This Authetication Id can be used to retrieve back the token, sid, user name etc. fields using the win32 API LsaGetLogonSessionData() Since the Login ID as a uint64 is unique per user, and is different between privileged and non-privileged sessions of the same user, it seems to be a good proxy for uid field in exec event. Signed-off-by: Anadi Anadi<[email protected]>
1 parent 419ff0b commit d3b85bf

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

pkg/api/processapi/processapi_windows.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type MsgCreateProcessEvent struct {
99
ParentProcessID uint32
1010
CreatingProcessID uint32
1111
CreatingThreadID uint32
12+
UserLUID uint64
1213
CreationTime uint64
1314
}
1415

pkg/reader/proc/proc_windows.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package proc
66
import (
77
"errors"
88
"fmt"
9+
"strconv"
910
"strings"
1011
"syscall"
1112
"unsafe"
@@ -19,6 +20,19 @@ type TokenGroups struct {
1920
Groups []syscall.SIDAndAttributes
2021
}
2122

23+
type TokenStatistics struct {
24+
TokenId windows.LUID
25+
AuthenticationId windows.LUID
26+
ExpirationTime int64
27+
TokenType uint32
28+
ImpersonationLevel uint32
29+
DynamicCharged uint32
30+
DynamicAvailable uint32
31+
GroupCount uint32
32+
PrivilegeCount uint32
33+
ModifiedId windows.LUID
34+
}
35+
2236
func getIDFromSID(str_sid string) (string, error) {
2337
tokens := strings.Split(str_sid, "-")
2438
if len(tokens) <= 1 {
@@ -27,24 +41,40 @@ func getIDFromSID(str_sid string) (string, error) {
2741
return tokens[len(tokens)-1], nil
2842
}
2943

30-
// fillStatus returns the content of /proc/pid/status as Status
31-
func fillStatus(hProc windows.Handle, status *Status) error {
32-
var token syscall.Token
33-
err := syscall.OpenProcessToken(syscall.Handle(hProc), syscall.TOKEN_QUERY, &token)
34-
if err != nil {
35-
return err
44+
func getStrLuidFromToken(token windows.Token) (string, error) {
45+
46+
var size uint32
47+
err := windows.GetTokenInformation(token, windows.TokenStatistics, nil, 0, &size)
48+
if err != syscall.ERROR_INSUFFICIENT_BUFFER {
49+
return "", fmt.Errorf("GetTokenInformation (size query) failed: %v\n", err)
3650
}
3751

38-
defer token.Close()
39-
tokenUser, err := token.GetTokenUser()
52+
// Allocate buffer and retrieve TokenStatistics
53+
buffer := make([]byte, size)
54+
err = windows.GetTokenInformation(token, windows.TokenStatistics, &buffer[0], size, &size)
4055
if err != nil {
41-
return err
56+
return "", fmt.Errorf("GetTokenInformation (size query) failed: %v\n", err)
4257
}
43-
sid_string, err := tokenUser.User.Sid.String()
58+
59+
// Cast buffer to TOKEN_STATISTICS
60+
stats := (*TokenStatistics)(unsafe.Pointer(&buffer[0]))
61+
62+
luid := *(*uint64)(unsafe.Pointer(&stats.AuthenticationId))
63+
strLUID := strconv.FormatUint(luid, 10)
64+
return strLUID, nil
65+
66+
}
67+
68+
// fillStatus returns the content of /proc/pid/status as Status
69+
func fillStatus(hProc windows.Handle, status *Status) error {
70+
var token windows.Token
71+
err := windows.OpenProcessToken(hProc, windows.TOKEN_QUERY, &token)
4472
if err != nil {
4573
return err
4674
}
47-
str_uid, err := getIDFromSID(sid_string)
75+
76+
defer token.Close()
77+
str_uid, err := getStrLuidFromToken(token)
4878
if err != nil {
4979
return err
5080
}
@@ -53,7 +83,7 @@ func fillStatus(hProc windows.Handle, status *Status) error {
5383
if err != nil {
5484
return err
5585
}
56-
str_groupid, err := tokenGroup.PrimaryGroup.String()
86+
str_groupid := tokenGroup.PrimaryGroup.String()
5787
if err != nil {
5888
return err
5989
}

pkg/sensors/exec/exec_windows.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func msgToExecveUnix(m *processapi.MsgCreateProcessEvent) *exec.MsgExecveEventUn
2828
PID: m.ProcessID,
2929
TID: m.ProcessID,
3030
NSPID: 0,
31+
UID: uint32(m.UserLUID),
3132
Flags: 1,
3233
Size: 0,
3334
Ktime: m.CreationTime,

0 commit comments

Comments
 (0)