/
script_builtins.go
248 lines (206 loc) · 6.19 KB
/
script_builtins.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
package utils
import (
"bytes"
"compress/gzip"
"encoding/base64"
"io/ioutil"
"github.com/bettercap/bettercap/log"
"github.com/bettercap/bettercap/session"
"github.com/evilsocket/islazy/plugin"
"github.com/robertkrimen/otto"
)
var nullOtto = otto.Value{}
func errOtto(format string, args ...interface{}) otto.Value {
log.Error(format, args...)
return nullOtto
}
func init() {
// used to read a directory (returns string array)
plugin.Defines["readDir"] = func(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc != 1 {
return errOtto("readDir: expected 1 argument, %d given instead.", argc)
}
path := argv[0].String()
dir, err := ioutil.ReadDir(path)
if err != nil {
return errOtto("Could not read directory %s: %s", path, err)
}
entry_list := []string{}
for _, file := range dir {
entry_list = append( entry_list, file.Name() )
}
v, err := otto.Otto.ToValue(*call.Otto, entry_list)
if err != nil {
return errOtto("Could not convert to array: %s", err)
}
return v
}
// used to read a file ... doh
plugin.Defines["readFile"] = func(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc != 1 {
return errOtto("readFile: expected 1 argument, %d given instead.", argc)
}
filename := argv[0].String()
raw, err := ioutil.ReadFile(filename)
if err != nil {
return errOtto("Could not read file %s: %s", filename, err)
}
v, err := otto.ToValue(string(raw))
if err != nil {
return errOtto("Could not convert to string: %s", err)
}
return v
}
plugin.Defines["writeFile"] = func(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc != 2 {
return errOtto("writeFile: expected 2 arguments, %d given instead.", argc)
}
filename := argv[0].String()
data := argv[1].String()
err := ioutil.WriteFile(filename, []byte(data), 0644)
if err != nil {
return errOtto("Could not write %d bytes to %s: %s", len(data), filename, err)
}
return otto.NullValue()
}
// log something
plugin.Defines["log"] = func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Info("%s", v.String())
}
return otto.Value{}
}
// log debug
plugin.Defines["log_debug"] = func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Debug("%s", v.String())
}
return otto.Value{}
}
// log info
plugin.Defines["log_info"] = func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Info("%s", v.String())
}
return otto.Value{}
}
// log warning
plugin.Defines["log_warn"] = func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Warning("%s", v.String())
}
return otto.Value{}
}
// log error
plugin.Defines["log_error"] = func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Error("%s", v.String())
}
return otto.Value{}
}
// log fatal
plugin.Defines["log_fatal"] = func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
log.Fatal("%s", v.String())
}
return otto.Value{}
}
// javascript btoa function
plugin.Defines["btoa"] = func(call otto.FunctionCall) otto.Value {
varValue := base64.StdEncoding.EncodeToString([]byte(call.Argument(0).String()))
v, err := otto.ToValue(varValue)
if err != nil {
return errOtto("Could not convert to string: %s", varValue)
}
return v
}
// javascript atob function
plugin.Defines["atob"] = func(call otto.FunctionCall) otto.Value {
varValue, err := base64.StdEncoding.DecodeString(call.Argument(0).String())
if err != nil {
return errOtto("Could not decode string: %s", call.Argument(0).String())
}
v, err := otto.ToValue(string(varValue))
if err != nil {
return errOtto("Could not convert to string: %s", varValue)
}
return v
}
// compress data with gzip
plugin.Defines["gzipCompress"] = func(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc != 1 {
return errOtto("gzipCompress: expected 1 argument, %d given instead.", argc)
}
uncompressed_bytes := []byte( argv[0].String() )
var writer_buffer bytes.Buffer
gzip_writer := gzip.NewWriter(&writer_buffer)
_, err := gzip_writer.Write(uncompressed_bytes)
if err != nil {
return errOtto( "gzipCompress: could not compress data: %s", err.Error() )
}
gzip_writer.Close()
compressed_bytes := writer_buffer.Bytes()
v, err := otto.ToValue( string(compressed_bytes) )
if err != nil {
return errOtto( "Could not convert to string: %s", err.Error() )
}
return v
}
// decompress data with gzip
plugin.Defines["gzipDecompress"] = func(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc != 1 {
return errOtto("gzipDecompress: expected 1 argument, %d given instead.", argc)
}
compressed_bytes := []byte( argv[0].String() )
reader_buffer := bytes.NewBuffer(compressed_bytes)
gzip_reader, err := gzip.NewReader(reader_buffer)
if err != nil {
return errOtto( "gzipDecompress: could not create gzip reader: %s", err.Error() )
}
var decompressed_buffer bytes.Buffer
_, err = decompressed_buffer.ReadFrom(gzip_reader)
if err != nil {
return errOtto( "gzipDecompress: could not decompress data: %s", err.Error() )
}
decompressed_bytes := decompressed_buffer.Bytes()
v, err := otto.ToValue( string(decompressed_bytes) )
if err != nil {
return errOtto( "Could not convert to string: %s", err.Error() )
}
return v
}
// read or write environment variable
plugin.Defines["env"] = func(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc == 1 {
// get
varName := call.Argument(0).String()
if found, varValue := session.I.Env.Get(varName); found {
v, err := otto.ToValue(varValue)
if err != nil {
return errOtto("Could not convert to string: %s", varValue)
}
return v
}
} else if argc == 2 {
// set
varName := call.Argument(0).String()
varValue := call.Argument(1).String()
session.I.Env.Set(varName, varValue)
} else {
return errOtto("env: expected 1 or 2 arguments, %d given instead.", argc)
}
return nullOtto
}
}