Debian Codegraph @

pkg/interactive/termhandler.go

raw symbols SCIP
1 package interactive
2
3 import (
4 "bufio"
5 "fmt"
6 "strconv"
7 "strings"
8 "time"
9
10 "github.com/ffuf/ffuf/v2/pkg/ffuf"
11 )
12
13 type interactive struct {
14 Job *ffuf.Job
15 paused bool
16 }
17
18 func Handle(job *ffuf.Job) error {
19 i := interactive{job, false}
20 tty, err := termHandle()
21 if err != nil {
22 return err
23 }
24 defer tty.Close()
25 inreader := bufio.NewScanner(tty)
26 inreader.Split(bufio.ScanLines)
27 for inreader.Scan() {
28 i.handleInput(inreader.Bytes())
29 }
30 return nil
31 }
32
33 func (i *interactive) handleInput(in []byte) {
34 instr := string(in)
35 args := strings.Split(strings.TrimSpace(instr), " ")
36 if len(args) == 1 && args[0] == "" {
37 // Enter pressed - toggle interactive state
38 i.paused = !i.paused
39 if i.paused {
40 i.Job.Pause()
41 time.Sleep(500 * time.Millisecond)
42 i.printBanner()
43 } else {
44 i.Job.Resume()
45 }
46 } else {
47 switch args[0] {
48 case "?":
49 i.printHelp()
50 case "help":
51 i.printHelp()
52 case "resume":
53 i.paused = false
54 i.Job.Resume()
55 case "restart":
56 i.Job.Reset(false)
57 i.paused = false
58 i.Job.Output.Info("Restarting the current ffuf job!")
59 i.Job.Resume()
60 case "show":
61 for _, r := range i.Job.Output.GetCurrentResults() {
62 i.Job.Output.PrintResult(r)
63 }
64 case "savejson":
65 if len(args) < 2 {
66 i.Job.Output.Error("Please define the filename")
67 } else if len(args) > 2 {
68 i.Job.Output.Error("Too many arguments for \"savejson\"")
69 } else {
70 err := i.Job.Output.SaveFile(args[1], "json")
71 if err != nil {
72 i.Job.Output.Error(fmt.Sprintf("%s", err))
73 } else {
74 i.Job.Output.Info("Output file successfully saved!")
75 }
76 }
77 case "fc":
78 if len(args) < 2 {
79 i.Job.Output.Error("Please define a value for status code filter, or \"none\" for removing it")
80 } else if len(args) > 2 {
81 i.Job.Output.Error("Too many arguments for \"fc\"")
82 } else {
83 i.updateFilter("status", args[1], true)
84 i.Job.Output.Info("New status code filter value set")
85 }
86 case "afc":
87 if len(args) < 2 {
88 i.Job.Output.Error("Please define a value to append to status code filter")
89 } else if len(args) > 2 {
90 i.Job.Output.Error("Too many arguments for \"afc\"")
91 } else {
92 i.appendFilter("status", args[1])
93 i.Job.Output.Info("New status code filter value set")
94 }
95 case "fl":
96 if len(args) < 2 {
97 i.Job.Output.Error("Please define a value for line count filter, or \"none\" for removing it")
98 } else if len(args) > 2 {
99 i.Job.Output.Error("Too many arguments for \"fl\"")
100 } else {
101 i.updateFilter("line", args[1], true)
102 i.Job.Output.Info("New line count filter value set")
103 }
104 case "afl":
105 if len(args) < 2 {
106 i.Job.Output.Error("Please define a value to append to line count filter")
107 } else if len(args) > 2 {
108 i.Job.Output.Error("Too many arguments for \"afl\"")
109 } else {
110 i.appendFilter("line", args[1])
111 i.Job.Output.Info("New line count filter value set")
112 }
113 case "fw":
114 if len(args) < 2 {
115 i.Job.Output.Error("Please define a value for word count filter, or \"none\" for removing it")
116 } else if len(args) > 2 {
117 i.Job.Output.Error("Too many arguments for \"fw\"")
118 } else {
119 i.updateFilter("word", args[1], true)
120 i.Job.Output.Info("New word count filter value set")
121 }
122 case "afw":
123 if len(args) < 2 {
124 i.Job.Output.Error("Please define a value to append to word count filter")
125 } else if len(args) > 2 {
126 i.Job.Output.Error("Too many arguments for \"afw\"")
127 } else {
128 i.appendFilter("word", args[1])
129 i.Job.Output.Info("New word count filter value set")
130 }
131 case "fs":
132 if len(args) < 2 {
133 i.Job.Output.Error("Please define a value for response size filter, or \"none\" for removing it")
134 } else if len(args) > 2 {
135 i.Job.Output.Error("Too many arguments for \"fs\"")
136 } else {
137 i.updateFilter("size", args[1], true)
138 i.Job.Output.Info("New response size filter value set")
139 }
140 case "afs":
141 if len(args) < 2 {
142 i.Job.Output.Error("Please define a value to append to size filter")
143 } else if len(args) > 2 {
144 i.Job.Output.Error("Too many arguments for \"afs\"")
145 } else {
146 i.appendFilter("size", args[1])
147 i.Job.Output.Info("New response size filter value set")
148 }
149 case "ft":
150 if len(args) < 2 {
151 i.Job.Output.Error("Please define a value for response time filter, or \"none\" for removing it")
152 } else if len(args) > 2 {
153 i.Job.Output.Error("Too many arguments for \"ft\"")
154 } else {
155 i.updateFilter("time", args[1], true)
156 i.Job.Output.Info("New response time filter value set")
157 }
158 case "aft":
159 if len(args) < 2 {
160 i.Job.Output.Error("Please define a value to append to response time filter")
161 } else if len(args) > 2 {
162 i.Job.Output.Error("Too many arguments for \"aft\"")
163 } else {
164 i.appendFilter("time", args[1])
165 i.Job.Output.Info("New response time filter value set")
166 }
167 case "queueshow":
168 i.printQueue()
169 case "queuedel":
170 if len(args) < 2 {
171 i.Job.Output.Error("Please define the index of a queued job to remove. Use \"queueshow\" for listing of jobs.")
172 } else if len(args) > 2 {
173 i.Job.Output.Error("Too many arguments for \"queuedel\"")
174 } else {
175 i.deleteQueue(args[1])
176 }
177 case "queueskip":
178 i.Job.SkipQueue()
179 i.Job.Output.Info("Skipping to the next queued job")
180 case "rate":
181 if len(args) < 2 {
182 i.Job.Output.Error("Please define the new rate")
183 } else if len(args) > 2 {
184 i.Job.Output.Error("Too many arguments for \"rate\"")
185 } else {
186 newrate, err := strconv.Atoi(args[1])
187 if err != nil {
188 i.Job.Output.Error(fmt.Sprintf("Could not adjust rate: %s", err))
189 } else {
190 i.Job.Rate.ChangeRate(newrate)
191 }
192 }
193
194 default:
195 if i.paused {
196 i.Job.Output.Warning(fmt.Sprintf("Unknown command: \"%s\". Enter \"help\" for a list of available commands", args[0]))
197 } else {
198 i.Job.Output.Error("NOPE")
199 }
200 }
201 }
202
203 if i.paused {
204 i.printPrompt()
205 }
206 }
207
208 func (i *interactive) refreshResults() {
209 results := make([]ffuf.Result, 0)
210 filters := i.Job.Config.MatcherManager.GetFilters()
211 for _, filter := range filters {
212 for _, res := range i.Job.Output.GetCurrentResults() {
213 fakeResp := &ffuf.Response{
214 StatusCode: res.StatusCode,
215 ContentLines: res.ContentLength,
216 ContentWords: res.ContentWords,
217 ContentLength: res.ContentLength,
218 }
219 filterOut, _ := filter.Filter(fakeResp)
220 if !filterOut {
221 results = append(results, res)
222 }
223 }
224 }
225 i.Job.Output.SetCurrentResults(results)
226 }
227
228 func (i *interactive) updateFilter(name, value string, replace bool) {
229 if value == "none" {
230 i.Job.Config.MatcherManager.RemoveFilter(name)
231 } else {
232 _ = i.Job.Config.MatcherManager.AddFilter(name, value, replace)
233 }
234 i.refreshResults()
235 }
236
237 func (i *interactive) appendFilter(name, value string) {
238 i.updateFilter(name, value, false)
239 }
240
241 func (i *interactive) printQueue() {
242 if len(i.Job.QueuedJobs()) > 0 {
243 i.Job.Output.Raw("Queued jobs:\n")
244 for index, job := range i.Job.QueuedJobs() {
245 postfix := ""
246 if index == 0 {
247 postfix = " (active job)"
248 }
249 i.Job.Output.Raw(fmt.Sprintf(" [%d] : %s%s\n", index, job.Url, postfix))
250 }
251 } else {
252 i.Job.Output.Info("Job queue is empty")
253 }
254 }
255
256 func (i *interactive) deleteQueue(in string) {
257 index, err := strconv.Atoi(in)
258 if err != nil {
259 i.Job.Output.Warning(fmt.Sprintf("Not a number: %s", in))
260 } else {
261 if index < 0 || index > len(i.Job.QueuedJobs())-1 {
262 i.Job.Output.Warning("No such queued job. Use \"queueshow\" to list the jobs in queue")
263 } else if index == 0 {
264 i.Job.Output.Warning("Cannot delete the currently running job. Use \"queueskip\" to advance to the next one")
265 } else {
266 i.Job.DeleteQueueItem(index)
267 i.Job.Output.Info("Job successfully deleted!")
268 }
269 }
270 }
271 func (i *interactive) printBanner() {
272 i.Job.Output.Raw("entering interactive mode\ntype \"help\" for a list of commands, or ENTER to resume.\n")
273 }
274
275 func (i *interactive) printPrompt() {
276 i.Job.Output.Raw("> ")
277 }
278
279 func (i *interactive) printHelp() {
280 var fc, fl, fs, ft, fw string
281 for name, filter := range i.Job.Config.MatcherManager.GetFilters() {
282 switch name {
283 case "status":
284 fc = "(active: " + filter.Repr() + ")"
285 case "line":
286 fl = "(active: " + filter.Repr() + ")"
287 case "word":
288 fw = "(active: " + filter.Repr() + ")"
289 case "size":
290 fs = "(active: " + filter.Repr() + ")"
291 case "time":
292 ft = "(active: " + filter.Repr() + ")"
293 }
294 }
295 rate := fmt.Sprintf("(active: %d)", i.Job.Config.Rate)
296 help := `
297 available commands:
298 afc [value] - append to status code filter %s
299 fc [value] - (re)configure status code filter %s
300 afl [value] - append to line count filter %s
301 fl [value] - (re)configure line count filter %s
302 afw [value] - append to word count filter %s
303 fw [value] - (re)configure word count filter %s
304 afs [value] - append to size filter %s
305 fs [value] - (re)configure size filter %s
306 aft [value] - append to time filter %s
307 ft [value] - (re)configure time filter %s
308 rate [value] - adjust rate of requests per second %s
309 queueshow - show job queue
310 queuedel [number] - delete a job in the queue
311 queueskip - advance to the next queued job
312 restart - restart and resume the current ffuf job
313 resume - resume current ffuf job (or: ENTER)
314 show - show results for the current job
315 savejson [filename] - save current matches to a file
316 help - you are looking at it
317 `
318 i.Job.Output.Raw(fmt.Sprintf(help, fc, fc, fl, fl, fw, fw, fs, fs, ft, ft, rate))
319 }