-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwazuh-container-exec.go
223 lines (199 loc) · 4.59 KB
/
wazuh-container-exec.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"sync"
"time"
)
const EOF = 0
var listenPath = getEnvOrDef("WAZUH_AGENT_CONTAINER_EXEC_LISTEN_PATH", path.Join(os.Getenv("WAZUH_AGENT_HOST_DIR"), "/var/ossec/container-exec.sock"))
var connectPath = getEnvOrDef("WAZUH_AGENT_CONTAINER_EXEC_CONNECT_PATH", "/var/ossec/container-exec.sock")
var logFile = getEnvOrDef("WAZUH_AGENT_CONTAINER_EXEC_LOG_FILE", "/var/ossec/logs/active-responses.log")
func getEnvOrDef(name, def string) string {
v, ok := os.LookupEnv(name)
if !ok {
return def
}
return v
}
func client() {
conn, err := net.Dial("unix", connectPath)
if err != nil {
panic(err)
}
defer conn.Close()
stdout := os.Stdout
if logFile != "" {
stdout, err = os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer stdout.Close()
}
go func() {
if _, err := io.Copy(conn, os.Stdin); err != nil {
fmt.Fprintln(stdout, "Error copying to stdin: ", err)
return
}
if _, err := conn.Write([]byte{EOF, '\n'}); err != nil {
fmt.Fprintln(stdout, "Error sending EOF: ", err)
return
}
}()
if _, err := io.Copy(stdout, conn); err != nil {
fmt.Fprintln(stdout, "Error copying to stdout: ", err)
os.Exit(1)
}
}
type syslogWrapper struct {
hostname string
cmd *exec.Cmd
name string
w io.Writer
}
func newSyslogWrapper(cmd *exec.Cmd, w io.Writer) io.Writer {
h, err := os.Hostname()
if err != nil {
h = "unknown"
}
return syslogWrapper{
hostname: h,
cmd: cmd,
name: filepath.Base(cmd.Path),
w: w,
}
}
func (l syslogWrapper) Write(p []byte) (n int, err error) {
h := time.Now().Format(time.Stamp) + " " +
l.hostname + " " +
l.name + "[" + strconv.Itoa(l.cmd.Process.Pid) + "]" +
": "
n, err = l.w.Write(append([]byte(h), p...))
if n = n - len(h); n < 0 {
n = 0
}
return
}
type activeResponseData struct {
Parameters struct {
ExtraArgs []string `json:"extra_args"`
} `json:"parameters"`
}
func server() {
listener, err := net.Listen("unix", listenPath)
if err != nil {
panic(err)
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Error accepting connection:", err)
continue
}
go func(conn net.Conn) {
defer conn.Close()
done := false
r := bufio.NewReader(conn)
line, err := r.ReadBytes('\n')
if err != nil {
log.Println("Error reading from connection:", err)
return
}
var args []string
ard := &activeResponseData{}
if err := json.Unmarshal(line, ard); err == nil {
args = ard.Parameters.ExtraArgs
if len(args) == 0 {
conn.Write([]byte("No arguments specified"))
return
}
} else {
ard = nil
args = []string{"bash"}
}
cmd := exec.Command(args[0], args[1:]...)
var writter io.Writer = conn
if ard != nil {
writter = newSyslogWrapper(cmd, conn)
}
stdin, err := cmd.StdinPipe()
if err != nil {
conn.Write([]byte(err.Error()))
log.Println("Error getting stdin pipe:", err)
return
}
stdout, err := cmd.StdoutPipe()
if err != nil {
conn.Write([]byte(err.Error()))
log.Println("Error getting stdout pipe:", err)
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
conn.Write([]byte(err.Error()))
log.Println("Error getting stderr pipe:", err)
return
}
var wg sync.WaitGroup
wg.Add(3)
defer wg.Wait()
go func() {
defer wg.Done()
defer stdin.Close()
for len(line) > 1 && !(len(line) == 2 && line[0] == EOF) {
_, err := io.WriteString(stdin, string(line))
if err != nil {
conn.Write([]byte(err.Error()))
log.Println("Error writing to stdin:", err)
return
}
line, err = r.ReadBytes('\n')
if err != nil && !errors.Is(err, io.EOF) && !done {
log.Println("Error reading from connection:", err)
}
if err != nil {
cmd.Process.Kill()
}
}
}()
go func() {
defer wg.Done()
defer stdout.Close()
if _, err := io.Copy(writter, stdout); err != nil && !done {
log.Println("Error copying stdout:", err)
}
}()
go func() {
defer wg.Done()
defer stderr.Close()
if _, err := io.Copy(writter, stderr); err != nil && !done {
log.Println("Error copying stderr:", err)
}
}()
defer conn.Close()
if err := cmd.Run(); err != nil {
conn.Write([]byte(err.Error()))
log.Println("Error executing command:", err)
}
done = true
}(conn)
}
}
func main() {
if len(os.Args) > 1 && os.Args[1] == "server" {
server()
return
}
client()
}