forked from maliceio/malice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugins.go
264 lines (222 loc) · 7.33 KB
/
plugins.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package plugins
import (
"bytes"
"fmt"
"io/ioutil"
"path"
"strings"
"sync"
"os"
"github.com/BurntSushi/toml"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types/strslice"
runconfigopts "github.com/docker/docker/runconfig/opts"
"github.com/maliceio/go-plugin-utils/utils"
"github.com/maliceio/malice/config"
"github.com/maliceio/malice/malice/docker/client"
"github.com/maliceio/malice/malice/docker/client/container"
"github.com/maliceio/malice/malice/docker/client/image"
er "github.com/maliceio/malice/malice/errors"
"github.com/maliceio/malice/malice/maldirs"
"github.com/parnurzeal/gorequest"
)
// StartPlugin starts plugin
func (plugin Plugin) StartPlugin(docker *client.Docker, arg string, scanID string, logs bool, wg *sync.WaitGroup) {
defer wg.Done()
cmd := plugin.buildCmd(arg, logs)
binds := []string{config.Conf.Docker.Binds} // []string{maldirs.GetSampledsDir() + ":/malware:ro"},
env := plugin.getPluginEnv()
env = append(env, "MALICE_SCANID="+scanID)
// env = append(env, "MALICE_ELASTICSEARCH="+utils.Getopt("MALICE_ELASTICSEARCH", getDbAddr()))
contJSON, err := container.Start(
docker, // docker *client.Docker,
cmd, // cmd strslice.StrSlice,
plugin.Name, // name string,
plugin.Image, // image string,
logs, // logs bool,
binds, // binds []string,
nil, // portBindings nat.PortMap,
[]string{config.Conf.Docker.Links}, // links []string,
env, // env []string,
)
log.WithFields(log.Fields{
"name": contJSON.Name,
"env": config.Conf.Environment.Run,
}).Debug("Plugin Container Started")
defer func() {
er.CheckError(container.Remove(docker, contJSON.ID, true, false, true))
log.WithFields(log.Fields{
"name": contJSON.Name,
"env": config.Conf.Environment.Run,
}).Debug("Plugin Container Removed")
}()
er.CheckError(err)
}
// getDbAddr gets address of DB server
func getDbAddr() string {
return "localhost"
}
// buildCmd creates plugin run command
func (plugin Plugin) buildCmd(args string, logs bool) strslice.StrSlice {
cmdStr := strslice.StrSlice{}
if logs {
cmdStr = append(cmdStr, "-t")
}
if plugin.Cmd != "" {
cmdStr = append(cmdStr, plugin.Cmd)
}
cmdStr = append(cmdStr, args)
return cmdStr
}
// RunIntelPlugins run all Intel plugins
func RunIntelPlugins(docker *client.Docker, hash string, scanID string, logs bool) {
hashType, _ := utils.GetHashType(hash)
log.Debug("Looking for Intel plugins...")
intelPlugins := GetIntelPlugins(hashType, true)
log.Debug("Found these plugins: ")
for _, plugin := range intelPlugins {
log.Debugf(" - %v", plugin.Name)
}
var wg sync.WaitGroup
wg.Add(len(intelPlugins))
for _, plugin := range intelPlugins {
go plugin.StartPlugin(docker, hash, scanID, logs, &wg)
}
wg.Wait()
}
func (plugin *Plugin) getPluginEnv() []string {
var env []string
for _, pluginEnv := range plugin.Env {
env = append(env, fmt.Sprintf("%s=%s", pluginEnv, os.Getenv(pluginEnv)))
}
return env
}
func printStatus(resp gorequest.Response, body string, errs []error) {
fmt.Println(resp.Status)
}
// PostResults post plugin results to Malice Webhook
func PostResults(url string, resultJSON []byte, taskID string) {
request := gorequest.New()
if config.Conf.Proxy.Enable {
request = gorequest.New().Proxy(config.Conf.Proxy.HTTP)
}
request.Post(url).
Set("Task", taskID).
Send(resultJSON).
End(printStatus)
}
//InstallPlugin installs a new malice plugin
func InstallPlugin(plugin *Plugin) (err error) {
var newPlugin = Configuration{
[]Plugin{
Plugin{
Name: plugin.Name,
Enabled: plugin.Enabled,
Category: plugin.Category,
Description: plugin.Description,
Image: plugin.Image,
Mime: plugin.Mime,
},
},
}
buf := new(bytes.Buffer)
er.CheckError(toml.NewEncoder(buf).Encode(newPlugin))
// fmt.Println(buf.String())
// open plugin config file
configPath := path.Join(maldirs.GetBaseDir(), "./plugins.toml")
f, err := os.OpenFile(configPath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
// write new plugin to installed plugin config
if _, err = f.WriteString("\n" + buf.String()); err != nil {
panic(err)
}
return
}
// DeletePlugin deletes a plugin
func DeletePlugin(name string) error {
for i, plugin := range Plugs.Plugins {
if strings.EqualFold(plugin.Name, name) {
Plugs.Plugins = append(Plugs.Plugins[:i], Plugs.Plugins[i+1:]...)
break
}
}
buf := new(bytes.Buffer)
er.CheckError(toml.NewEncoder(buf).Encode(Plugs))
// open plugin config file
configPath := path.Join(maldirs.GetBaseDir(), "./plugins.toml")
err := ioutil.WriteFile(configPath, buf.Bytes(), 0644)
return err
}
// InstalledPluginsCheck checks that all enabled plugins are installed
func InstalledPluginsCheck(docker *client.Docker) bool {
for _, plugin := range getEnabled(Plugs.Plugins) {
if _, exists, _ := image.Exists(docker, plugin.Image); !exists {
return false
}
}
return true
}
// UpdatePlugin performs a docker pull on all registered plugins checking for updates
func (plugin Plugin) UpdatePlugin(docker *client.Docker) {
image.Pull(docker, plugin.Image, "latest")
}
// UpdatePluginFromRepository performs a docker build on a plugins remote repository
func (plugin Plugin) UpdatePluginFromRepository(docker *client.Docker) {
log.Info("[Building Plugin from Source] ===> ", plugin.Name)
var buildArgs map[string]string
var quiet = false
tags := []string{"malice/" + plugin.Name + ":latest"}
if config.Conf.Proxy.Enable {
buildArgs = runconfigopts.ConvertKVStringsToMap([]string{
"HTTP_PROXY=" + config.Conf.Proxy.HTTP,
"HTTPS_PROXY=" + config.Conf.Proxy.HTTPS,
})
} else {
buildArgs = nil
}
labels := runconfigopts.ConvertKVStringsToMap([]string{"io.malice.plugin.installed.from=repository"})
image.Build(docker, plugin.Repository, tags, buildArgs, labels, quiet)
}
// UpdateEnabledPlugins performs a docker pull on all enabled plugins checking for updates
func UpdateEnabledPlugins(docker *client.Docker) {
// Pull busybox (used to copy samples to malice volume)
image.Pull(docker, "busybox", "latest")
// Pull blacktop/elk (used to store malice scan results data)
image.Pull(docker, config.Conf.DB.Image, "latest")
// Pull all enabled malice plugin images
for _, plugin := range GetEnabledPlugins() {
fmt.Println("[Updating Plugin] ===> ", plugin.Name)
if plugin.Build {
plugin.UpdatePluginFromRepository(docker)
} else {
image.Pull(docker, plugin.Image, "latest")
}
}
}
// UpdateAllPlugins performs a docker pull on all registered plugins checking for updates
func UpdateAllPlugins(docker *client.Docker) {
// Pull busybox (used to copy samples to malice volume)
image.Pull(docker, "busybox", "latest")
// Pull blacktop/elk (used to store malice scan results data)
image.Pull(docker, config.Conf.DB.Image, "latest")
plugins := Plugs.Plugins
for _, plugin := range plugins {
fmt.Println("[Updating Plugin] ===> ", plugin.Name)
if plugin.Build {
plugin.UpdatePluginFromRepository(docker)
} else {
image.Pull(docker, plugin.Image, "latest")
}
}
}
// UpdateAllPluginsFromSource performs a docker build on a plugins remote repository on all registered plugins
func UpdateAllPluginsFromSource(docker *client.Docker) {
plugins := Plugs.Plugins
for _, plugin := range plugins {
fmt.Println("[Updating Plugin from Source] ===> ", plugin.Name)
plugin.UpdatePluginFromRepository(docker)
}
}