-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (107 loc) · 3.56 KB
/
main.go
File metadata and controls
126 lines (107 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"log"
"os"
"syscall"
"github.com/akamensky/argparse"
"github.com/shirou/gopsutil/process"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
)
func injectorRoutine(injectProcessNameList []string, injectionDLLPath string, injectionFunctionName string, injectionFunctionArg string, refreshInterval int, maxInjectionRetry int, quit <-chan struct{}) {
// initial process injection (for already running processes)
processes, error := process.Processes()
if error != nil {
logMessage(LOGLEVEL_DEBUG, fmt.Sprintf("Error getting processes: %v", error))
}
for _, proc := range processes {
processName, error := proc.Name()
if error != nil {
logMessage(LOGLEVEL_DEBUG, fmt.Sprintf("Error getting process name for PID %d: %v", proc.Pid, error))
continue
}
if isProcessNameInList(processName, injectProcessNameList) {
go func() {
HandleProcessInjection(processName, uint32(proc.Pid), injectionDLLPath, injectionFunctionName, injectionFunctionArg, refreshInterval, maxInjectionRetry)
}()
}
}
// communication channels for WMI service
wmiRequests := make(chan wmiServiceRequest)
wmiResponses := make(chan wmiServiceResponse)
// start WMI service goroutine
go wmiService(wmiRequests, wmiResponses)
logMessage(LOGLEVEL_INFO, "Process injector service started.")
for {
select {
case <-quit:
logMessage(LOGLEVEL_INFO, "Shutting down process injector service.")
close(wmiRequests)
return
case resp := <-wmiResponses:
if resp.err != nil {
logMessage(LOGLEVEL_ERROR, fmt.Sprintf("WMI service error: %v", resp.err))
if hresult, ok := resp.err.(syscall.Errno); ok {
if hresult != 0x80041006 { // ignore "not available" error which can be temporary
return
}
}
continue
}
event := resp.event
logMessage(LOGLEVEL_DEBUG, fmt.Sprintf("New process detected: Name=%s, PID=%d", event.Name, event.PID))
// If the process name is in the list, attempt injection
if isProcessNameInList(event.Name, injectProcessNameList) {
go func() {
HandleProcessInjection(event.Name, event.PID, injectionDLLPath, injectionFunctionName, injectionFunctionArg, refreshInterval, maxInjectionRetry)
}()
}
}
}
}
func main() {
// config file argument parsing
parser := argparse.NewParser("Go Process Injector", "Process Injector Service for Windows")
configFilePath := parser.String("c", "config", &argparse.Options{Required: true, Help: "YAML configuration file"})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
}
// load yaml configuration
err = LoadConfig(*configFilePath)
if err != nil {
log.Fatalln(fmt.Errorf("error loading configuration: %v", err))
}
// initialize logger
var APP_LOGLEVEL int
switch AppConfig.InjectorLogLevel {
case "LOGLEVEL_DEBUG":
APP_LOGLEVEL = LOGLEVEL_DEBUG
case "LOGLEVEL_WARNING":
APP_LOGLEVEL = LOGLEVEL_WARNING
case "LOGLEVEL_ERROR":
APP_LOGLEVEL = LOGLEVEL_ERROR
default:
APP_LOGLEVEL = LOGLEVEL_INFO
}
InitLogger(APP_LOGLEVEL)
if AppConfig.InjectorLogFile != "" {
SetLogToFile(AppConfig.InjectorLogFile)
}
// start in interactive mode or as a Windows service
isWindowsSerice, err := svc.IsWindowsService()
if err != nil {
log.Fatalln(fmt.Errorf("error checking if running as a Windows service: %v", err))
}
if isWindowsSerice {
runService("pInjectService", false)
} else {
logMessage(LOGLEVEL_INFO, "Running in interactive mode.")
err = debug.Run("pInjectService", &pInjectService{})
if err != nil {
log.Fatalf("Error running service in interactive mode: %v", err)
}
return
}
}