forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 4
/
script_builtins.go
160 lines (135 loc) · 3.84 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
package modules
import (
"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 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 %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
}
// 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
}
}