@@ -20,9 +20,27 @@ package eventlog
2020
2121import (
2222 "fmt"
23+ "github.com/rabbitstack/fibratus/pkg/kevent/ktypes"
2324 "golang.org/x/sys/windows/registry"
2425)
2526
27+ const (
28+ // Source represents the event source that generates the alerts
29+ Source = "Fibratus"
30+ // Levels designates the supported eventlog levels
31+ Levels = uint32 (Info | Warn | Erro )
32+ // msgFile specifies the location of the eventlog message DLL
33+ msgFile = "%ProgramFiles%\\ Fibratus\\ fibratus.dll"
34+ // keyName represents the registry key under which the eventlog source is registered
35+ keyName = `SYSTEM\CurrentControlSet\Services\EventLog\Application`
36+ )
37+
38+ // ErrKeyExists signals that the registry key already exists
39+ var ErrKeyExists = fmt .Errorf ("%s\\ %s already exists" , keyName , Source )
40+
41+ // categoryCount indicates the number of current event categories
42+ var categoryCount = uint32 (len (ktypes .Categories ()))
43+
2644// Level is the type definition for the eventlog log level
2745type Level uint16
2846
@@ -35,77 +53,45 @@ const (
3553 Erro Level = 1
3654)
3755
38- // LevelFromString resolves the eventlog levle from string
39- func LevelFromString (s string ) Level {
40- switch s {
41- case "info" , "INFO" :
42- return Info
43- case "warn" , "warning" , "WARN" , "WARNING" :
44- return Warn
45- case "erro" , "error" , "ERRO" , "ERROR" :
46- return Erro
47- default :
48- panic (fmt .Sprintf ("unrecognized evtlog level: %s" , s ))
49- }
50- }
51-
52- // ErrKeyExists signals that the registry key already exists
53- type ErrKeyExists struct {
54- src string
55- key string
56- }
57-
58- func (e ErrKeyExists ) Error () string {
59- return fmt .Sprintf ("%s\\ %s already exists" , e .key , e .src )
60- }
61-
6256// Install modifies PC registry to allow logging with an event source src.
6357// It adds all required keys and values to the event log registry key.
6458// Install uses msgFile as the event message file. If useExpandKey is true,
6559// the event message file is installed as REG_EXPAND_SZ value,
66- // otherwise as REG_SZ. Use bitwise of log.Error, log.Warning and
67- // log.Info to specify events supported by the new event source.
68- func Install (src , f , key string , useExpandKey bool , eventsSupported , cats uint32 ) error {
69- appkey , err := registry .OpenKey (registry .LOCAL_MACHINE , key , registry .CREATE_SUB_KEY )
60+ // otherwise as REG_SZ. Use bitwise of Errr, Warn, and Info to specify events
61+ // supported by the new event source.
62+ func Install (eventsSupported uint32 ) error {
63+ key , err := registry .OpenKey (registry .LOCAL_MACHINE , keyName , registry .CREATE_SUB_KEY )
7064 if err != nil {
7165 return err
7266 }
73- defer appkey .Close ()
67+ defer key .Close ()
7468
75- sk , alreadyExist , err := registry .CreateKey (appkey , src , registry .SET_VALUE )
69+ sk , exists , err := registry .CreateKey (key , Source , registry .SET_VALUE )
7670 if err != nil {
7771 return err
7872 }
7973 defer sk .Close ()
80- if alreadyExist {
81- return ErrKeyExists { src , key }
74+ if exists {
75+ return ErrKeyExists
8276 }
8377
8478 err = sk .SetDWordValue ("CustomSource" , 1 )
8579 if err != nil {
8680 return err
8781 }
88- if useExpandKey {
89- err = sk .SetExpandStringValue ("EventMessageFile" , f )
90- } else {
91- err = sk .SetStringValue ("EventMessageFile" , f )
92- }
82+ err = sk .SetExpandStringValue ("EventMessageFile" , msgFile )
9383 if err != nil {
9484 return err
9585 }
96- if useExpandKey {
97- err = sk .SetExpandStringValue ("CategoryMessageFile" , f )
98- } else {
99- err = sk .SetStringValue ("CategoryMessageFile" , f )
100- }
86+ err = sk .SetExpandStringValue ("CategoryMessageFile" , msgFile )
10187 if err != nil {
10288 return err
10389 }
10490 err = sk .SetDWordValue ("TypesSupported" , eventsSupported )
10591 if err != nil {
10692 return err
10793 }
108- err = sk .SetDWordValue ("CategoryCount" , cats )
94+ err = sk .SetDWordValue ("CategoryCount" , categoryCount )
10995 if err != nil {
11096 return err
11197 }
0 commit comments