/
executor.go
222 lines (186 loc) · 3.99 KB
/
executor.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
package main
import (
"fmt"
"io"
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/chzyer/readline"
)
func (p *fiestaPrompt) Executor(line string) {
line = strings.TrimSpace(line)
line = strings.ToLower(line)
vLine := strings.Split(line, " ")
switch {
// EXIT
case line == "exit" || line == "quit":
p.exit = true
// USE
case strings.HasPrefix(line, "use "):
if len(p.Attack) > 0 { // ignore when attack was already selected
return
}
attack := line[4:]
for _, v := range p.listAttacks() {
if v == attack {
// Prompt
p.Attack = attack
p.setPrompt(p.Attack)
p.I.Config.AutoComplete = p.CompleterUse
// Core
p.c.SetAttack(attack)
return
}
}
fmt.Println(p.Usage("use"))
// BACK
case line == "back":
if len(p.Attack) == 0 { // ignore when no attack selected
return
}
p.Attack = ""
p.setPrompt(p.Attack)
p.I.Config.AutoComplete = p.CompleterRoot
// SHOW
case line == "show attacks":
attacks := p.listAttacks()
for _, v := range attacks {
fmt.Println(v)
}
case line == "show options":
if len(p.Attack) == 0 { // not shown when no attack selected
return
}
fmt.Printf("\nModule options (%s):\n\n", p.Attack)
fmt.Println("\tName Current Setting Description")
fmt.Println("\t---- --------------- -----------")
for _, v := range p.c.Options() {
fmt.Printf("\t%-10s %-15s %s\n", v.Param, v.Value, v.Descr)
}
fmt.Printf("\n")
case strings.HasPrefix(line, "show "):
fmt.Println(p.Usage("show"))
// SET / UNSET
case strings.HasPrefix(line, "set "):
if len(p.Attack) == 0 { // ignore when no attack selected
return
}
if len(vLine) < 3 {
fmt.Println(p.Usage("set"))
return
}
option := strings.ToUpper(vLine[1])
n := len(vLine[0]) + len(vLine[1]) + 2
value := line[n:]
p.c.SetOption(option, value)
fmt.Printf("%s = %s", option, value)
case strings.HasPrefix(line, "unset "):
if len(p.Attack) == 0 { // ignore when no attack selected
return
}
if len(vLine) < 2 {
fmt.Println(p.Usage("unset"))
return
}
option := strings.ToUpper(vLine[1])
p.c.UnsetOption(option)
// EXPLOIT
case line == "exploit" || line == "run":
if len(p.Attack) == 0 { // ignore when no attack selected
return
}
statusCh, msgCh, resCh, errorCh := p.c.Exploit()
// close channel if CTRL+C
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go p.stopIfCtrlC(sigs)
// Exit if we receive an error
go func() {
e, ok := <-errorCh
p.c.Break()
if ok {
p.magic.NewLine()
p.magic.PrintError(e)
}
}()
// Print status if received
go func() {
for {
s, ok := <-statusCh
if !ok {
break
}
p.magic.PrintStatus(s)
}
}()
// Print result if received
go func() {
for {
s, ok := <-resCh
if !ok {
break
}
p.magic.PrintResult(s)
}
}()
// print results until CTRL+C or finish
for m, ok := <-msgCh; ok; m, ok = <-msgCh {
p.magic.Print(m)
}
p.magic.NewLine()
// NO COMMAND
case line == "":
return
// USAGE
case len(p.Usage(vLine[0])) > 0:
fmt.Println(p.Usage(vLine[0]))
/*
default: // execute OS command
out, _ := exec.Command("sh", "-c", line).Output()
if len(out) > 9 {
fmt.Println(string(out))
}
*/
}
}
func (p *fiestaPrompt) stopIfCtrlC(signal chan os.Signal) {
<-signal
p.c.Break()
}
func (p *fiestaPrompt) Run() {
// set default Prompt
p.setPrompt("")
log.SetOutput(p.I.Stderr())
p.exit = false
for !p.exit {
line, e := p.I.Readline()
if e == readline.ErrInterrupt {
if len(line) == 0 {
break
} else {
continue
}
} else if e == io.EOF {
break
}
p.Executor(line)
}
}
func (p *fiestaPrompt) Usage(cmd string) string {
var usage string
switch cmd {
case "use":
usage = "Usage: use [attack]"
case "show":
usage = "Usage: show [attacks|options]"
case "set":
usage = "Usage: set [option] [value]"
case "unset":
usage = "Usage: unset [option]"
default:
usage = ""
}
return usage
}