-
Notifications
You must be signed in to change notification settings - Fork 0
/
stenoquery.go
204 lines (177 loc) · 6.06 KB
/
stenoquery.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
// Copyright 2019 Jason Ertel (jertel). All rights reserved.
//
// This program is distributed under the terms of version 2 of the
// GNU General Public License. See LICENSE for further details.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
package stenoquery
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/apex/log"
"github.com/kennygrant/sanitize"
"github.com/khulnasoft/khulnasoft-soc/agent"
"github.com/khulnasoft/khulnasoft-soc/model"
"github.com/khulnasoft/khulnasoft-soc/module"
)
const DEFAULT_EXECUTABLE_PATH = "stenoread"
const DEFAULT_PCAP_OUTPUT_PATH = "/nsm/pcapout"
const DEFAULT_PCAP_INPUT_PATH = "/nsm/pcap"
const DEFAULT_EPOCH_REFRESH_MS = 120000
const DEFAULT_TIMEOUT_MS = 1200000
const DEFAULT_DATA_LAG_MS = 120000
type StenoQuery struct {
config module.ModuleConfig
executablePath string
pcapOutputPath string
pcapInputPath string
agent *agent.Agent
epochTimeTmp time.Time
epochTime time.Time
epochRefreshTime time.Time
epochRefreshMs int
timeoutMs int
dataLagMs int
}
func NewStenoQuery(agt *agent.Agent) *StenoQuery {
return &StenoQuery{
agent: agt,
}
}
func (lag *StenoQuery) PrerequisiteModules() []string {
return nil
}
func (steno *StenoQuery) Init(cfg module.ModuleConfig) error {
var err error
steno.config = cfg
steno.executablePath = module.GetStringDefault(cfg, "executablePath", DEFAULT_EXECUTABLE_PATH)
steno.pcapOutputPath = module.GetStringDefault(cfg, "pcapOutputPath", DEFAULT_PCAP_OUTPUT_PATH)
steno.pcapInputPath = module.GetStringDefault(cfg, "pcapInputPath", DEFAULT_PCAP_INPUT_PATH)
steno.epochRefreshMs = module.GetIntDefault(cfg, "epochRefreshMs", DEFAULT_EPOCH_REFRESH_MS)
steno.timeoutMs = module.GetIntDefault(cfg, "timeoutMs", DEFAULT_TIMEOUT_MS)
steno.dataLagMs = module.GetIntDefault(cfg, "dataLagMs", DEFAULT_DATA_LAG_MS)
if steno.agent == nil {
err = errors.New("Unable to invoke JobMgr.AddJobProcessor due to nil agent")
} else {
steno.agent.JobMgr.AddJobProcessor(steno)
}
return err
}
func (steno *StenoQuery) Start() error {
return nil
}
func (steno *StenoQuery) Stop() error {
return nil
}
func (steno *StenoQuery) IsRunning() bool {
return false
}
func (steno *StenoQuery) getDataLagDate() time.Time {
return time.Now().Add(time.Duration(-steno.dataLagMs) * time.Millisecond)
}
func (steno *StenoQuery) ProcessJob(job *model.Job, reader io.ReadCloser) (io.ReadCloser, error) {
var err error
if job.GetKind() != "pcap" {
log.WithFields(log.Fields{
"jobId": job.Id,
"kind": job.GetKind(),
}).Debug("Skipping steno processor due to unsupported job")
return reader, nil
}
if len(job.Filter.ImportId) > 0 {
log.WithFields(log.Fields{
"jobId": job.Id,
"importId": job.Filter.ImportId,
}).Debug("Skipping steno processor due to presence of importId")
return reader, nil
} else if job.Filter == nil || job.Filter.EndTime.Before(steno.GetDataEpoch()) || job.Filter.BeginTime.After(steno.getDataLagDate()) {
log.WithFields(log.Fields{
"jobId": job.Id,
"availableDataBeginDate": steno.GetDataEpoch(),
"availableDataEndDate": steno.getDataLagDate(),
"jobBeginDate": job.Filter.BeginTime,
"jobEndDate": job.Filter.EndTime,
}).Info("Skipping steno processor due to date range conflict")
err = errors.New("No data available for the requested dates")
} else {
job.FileExtension = "pcap"
query := steno.CreateQuery(job)
pcapFilepath := fmt.Sprintf("%s/%d.%s", steno.pcapOutputPath, job.Id, job.FileExtension)
log.WithField("jobId", job.Id).Info("Processing pcap export for job")
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(steno.timeoutMs)*time.Millisecond)
defer cancel()
cmd := exec.CommandContext(ctx, steno.executablePath, query, "-w", pcapFilepath)
var output []byte
output, err = cmd.CombinedOutput()
log.WithFields(log.Fields{
"executablePath": steno.executablePath,
"query": query,
"output": string(output),
"pcapFilepath": pcapFilepath,
"err": err,
}).Debug("Executed stenoread")
if err == nil {
var file *os.File
file, err = os.Open(pcapFilepath)
if err == nil {
reader = file
}
}
}
return reader, err
}
func (steno *StenoQuery) CleanupJob(job *model.Job) {
pcapOutputFilepath := fmt.Sprintf("%s/%d.%s", steno.pcapOutputPath, job.Id, sanitize.Name(job.FileExtension))
os.Remove(pcapOutputFilepath)
}
func (steno *StenoQuery) CreateQuery(job *model.Job) string {
beginTime := job.Filter.BeginTime.Format(time.RFC3339)
endTime := job.Filter.EndTime.Format(time.RFC3339)
query := fmt.Sprintf("before %s and after %s", endTime, beginTime)
if len(job.Filter.SrcIp) > 0 {
query = fmt.Sprintf("%s and host %s", query, job.Filter.SrcIp)
}
if len(job.Filter.DstIp) > 0 {
query = fmt.Sprintf("%s and host %s", query, job.Filter.DstIp)
}
if job.Filter.SrcPort > 0 {
query = fmt.Sprintf("%s and port %d", query, job.Filter.SrcPort)
}
if job.Filter.DstPort > 0 {
query = fmt.Sprintf("%s and port %d", query, job.Filter.DstPort)
}
return query
}
func (steno *StenoQuery) GetDataEpoch() time.Time {
now := time.Now()
refreshDuration := time.Duration(steno.epochRefreshMs) * time.Millisecond
if now.Sub(steno.epochRefreshTime) > refreshDuration {
steno.epochTimeTmp = now
err := filepath.Walk(steno.pcapInputPath, steno.updateEpochTimeTmp)
if err != nil {
log.WithError(err).WithField("pcapInputPath", steno.pcapInputPath)
} else {
steno.epochTime = steno.epochTimeTmp
}
steno.epochRefreshTime = now
}
return steno.epochTime
}
func (steno *StenoQuery) updateEpochTimeTmp(path string, info os.FileInfo, err error) error {
if err != nil {
log.WithError(err).WithField("path", path).Error("Unable to access path while updating epoch")
return err
}
if !info.IsDir() && info.Size() > 0 && info.ModTime().Before(steno.epochTimeTmp) {
steno.epochTimeTmp = info.ModTime()
}
return nil
}