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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
package interactive
import (
"bufio"
"fmt"
"strconv"
"strings"
"time"
"github.com/ffuf/ffuf/v2/pkg/ffuf"
)
type interactive struct {
Job *ffuf.Job
paused bool
}
func Handle(job *ffuf.Job) error {
i := interactive{job, false}
tty, err := termHandle()
if err != nil {
return err
}
defer tty.Close()
inreader := bufio.NewScanner(tty)
inreader.Split(bufio.ScanLines)
for inreader.Scan() {
i.handleInput(inreader.Bytes())
}
return nil
}
func (i *interactive) handleInput(in []byte) {
instr := string(in)
args := strings.Split(strings.TrimSpace(instr), " ")
if len(args) == 1 && args[0] == "" {
// Enter pressed - toggle interactive state
i.paused = !i.paused
if i.paused {
i.Job.Pause()
time.Sleep(500 * time.Millisecond)
i.printBanner()
} else {
i.Job.Resume()
}
} else {
switch args[0] {
case "?":
i.printHelp()
case "help":
i.printHelp()
case "resume":
i.paused = false
i.Job.Resume()
case "restart":
i.Job.Reset(false)
i.paused = false
i.Job.Output.Info("Restarting the current ffuf job!")
i.Job.Resume()
case "show":
for _, r := range i.Job.Output.GetCurrentResults() {
i.Job.Output.PrintResult(r)
}
case "savejson":
if len(args) < 2 {
i.Job.Output.Error("Please define the filename")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"savejson\"")
} else {
err := i.Job.Output.SaveFile(args[1], "json")
if err != nil {
i.Job.Output.Error(fmt.Sprintf("%s", err))
} else {
i.Job.Output.Info("Output file successfully saved!")
}
}
case "fc":
if len(args) < 2 {
i.Job.Output.Error("Please define a value for status code filter, or \"none\" for removing it")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"fc\"")
} else {
i.updateFilter("status", args[1], true)
i.Job.Output.Info("New status code filter value set")
}
case "afc":
if len(args) < 2 {
i.Job.Output.Error("Please define a value to append to status code filter")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"afc\"")
} else {
i.appendFilter("status", args[1])
i.Job.Output.Info("New status code filter value set")
}
case "fl":
if len(args) < 2 {
i.Job.Output.Error("Please define a value for line count filter, or \"none\" for removing it")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"fl\"")
} else {
i.updateFilter("line", args[1], true)
i.Job.Output.Info("New line count filter value set")
}
case "afl":
if len(args) < 2 {
i.Job.Output.Error("Please define a value to append to line count filter")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"afl\"")
} else {
i.appendFilter("line", args[1])
i.Job.Output.Info("New line count filter value set")
}
case "fw":
if len(args) < 2 {
i.Job.Output.Error("Please define a value for word count filter, or \"none\" for removing it")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"fw\"")
} else {
i.updateFilter("word", args[1], true)
i.Job.Output.Info("New word count filter value set")
}
case "afw":
if len(args) < 2 {
i.Job.Output.Error("Please define a value to append to word count filter")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"afw\"")
} else {
i.appendFilter("word", args[1])
i.Job.Output.Info("New word count filter value set")
}
case "fs":
if len(args) < 2 {
i.Job.Output.Error("Please define a value for response size filter, or \"none\" for removing it")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"fs\"")
} else {
i.updateFilter("size", args[1], true)
i.Job.Output.Info("New response size filter value set")
}
case "afs":
if len(args) < 2 {
i.Job.Output.Error("Please define a value to append to size filter")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"afs\"")
} else {
i.appendFilter("size", args[1])
i.Job.Output.Info("New response size filter value set")
}
case "ft":
if len(args) < 2 {
i.Job.Output.Error("Please define a value for response time filter, or \"none\" for removing it")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"ft\"")
} else {
i.updateFilter("time", args[1], true)
i.Job.Output.Info("New response time filter value set")
}
case "aft":
if len(args) < 2 {
i.Job.Output.Error("Please define a value to append to response time filter")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"aft\"")
} else {
i.appendFilter("time", args[1])
i.Job.Output.Info("New response time filter value set")
}
case "queueshow":
i.printQueue()
case "queuedel":
if len(args) < 2 {
i.Job.Output.Error("Please define the index of a queued job to remove. Use \"queueshow\" for listing of jobs.")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"queuedel\"")
} else {
i.deleteQueue(args[1])
}
case "queueskip":
i.Job.SkipQueue()
i.Job.Output.Info("Skipping to the next queued job")
case "rate":
if len(args) < 2 {
i.Job.Output.Error("Please define the new rate")
} else if len(args) > 2 {
i.Job.Output.Error("Too many arguments for \"rate\"")
} else {
newrate, err := strconv.Atoi(args[1])
if err != nil {
i.Job.Output.Error(fmt.Sprintf("Could not adjust rate: %s", err))
} else {
i.Job.Rate.ChangeRate(newrate)
}
}
default:
if i.paused {
i.Job.Output.Warning(fmt.Sprintf("Unknown command: \"%s\". Enter \"help\" for a list of available commands", args[0]))
} else {
i.Job.Output.Error("NOPE")
}
}
}
if i.paused {
i.printPrompt()
}
}
func (i *interactive) refreshResults() {
results := make([]ffuf.Result, 0)
filters := i.Job.Config.MatcherManager.GetFilters()
for _, filter := range filters {
for _, res := range i.Job.Output.GetCurrentResults() {
fakeResp := &ffuf.Response{
StatusCode: res.StatusCode,
ContentLines: res.ContentLength,
ContentWords: res.ContentWords,
ContentLength: res.ContentLength,
}
filterOut, _ := filter.Filter(fakeResp)
if !filterOut {
results = append(results, res)
}
}
}
i.Job.Output.SetCurrentResults(results)
}
func (i *interactive) updateFilter(name, value string, replace bool) {
if value == "none" {
i.Job.Config.MatcherManager.RemoveFilter(name)
} else {
_ = i.Job.Config.MatcherManager.AddFilter(name, value, replace)
}
i.refreshResults()
}
func (i *interactive) appendFilter(name, value string) {
i.updateFilter(name, value, false)
}
func (i *interactive) printQueue() {
if len(i.Job.QueuedJobs()) > 0 {
i.Job.Output.Raw("Queued jobs:\n")
for index, job := range i.Job.QueuedJobs() {
postfix := ""
if index == 0 {
postfix = " (active job)"
}
i.Job.Output.Raw(fmt.Sprintf(" [%d] : %s%s\n", index, job.Url, postfix))
}
} else {
i.Job.Output.Info("Job queue is empty")
}
}
func (i *interactive) deleteQueue(in string) {
index, err := strconv.Atoi(in)
if err != nil {
i.Job.Output.Warning(fmt.Sprintf("Not a number: %s", in))
} else {
if index < 0 || index > len(i.Job.QueuedJobs())-1 {
i.Job.Output.Warning("No such queued job. Use \"queueshow\" to list the jobs in queue")
} else if index == 0 {
i.Job.Output.Warning("Cannot delete the currently running job. Use \"queueskip\" to advance to the next one")
} else {
i.Job.DeleteQueueItem(index)
i.Job.Output.Info("Job successfully deleted!")
}
}
}
func (i *interactive) printBanner() {
i.Job.Output.Raw("entering interactive mode\ntype \"help\" for a list of commands, or ENTER to resume.\n")
}
func (i *interactive) printPrompt() {
i.Job.Output.Raw("> ")
}
func (i *interactive) printHelp() {
var fc, fl, fs, ft, fw string
for name, filter := range i.Job.Config.MatcherManager.GetFilters() {
switch name {
case "status":
fc = "(active: " + filter.Repr() + ")"
case "line":
fl = "(active: " + filter.Repr() + ")"
case "word":
fw = "(active: " + filter.Repr() + ")"
case "size":
fs = "(active: " + filter.Repr() + ")"
case "time":
ft = "(active: " + filter.Repr() + ")"
}
}
rate := fmt.Sprintf("(active: %d)", i.Job.Config.Rate)
help := `
available commands:
afc [value] - append to status code filter %s
fc [value] - (re)configure status code filter %s
afl [value] - append to line count filter %s
fl [value] - (re)configure line count filter %s
afw [value] - append to word count filter %s
fw [value] - (re)configure word count filter %s
afs [value] - append to size filter %s
fs [value] - (re)configure size filter %s
aft [value] - append to time filter %s
ft [value] - (re)configure time filter %s
rate [value] - adjust rate of requests per second %s
queueshow - show job queue
queuedel [number] - delete a job in the queue
queueskip - advance to the next queued job
restart - restart and resume the current ffuf job
resume - resume current ffuf job (or: ENTER)
show - show results for the current job
savejson [filename] - save current matches to a file
help - you are looking at it
`
i.Job.Output.Raw(fmt.Sprintf(help, fc, fc, fl, fl, fw, fw, fs, fs, ft, ft, rate))
}
|