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 cli
import (
"context"
"errors"
"fmt"
"math"
"os"
"strings"
"sync"
"time"
"github.com/OJ/gobuster/v3/libgobuster"
)
const (
ruler = "==============================================================="
cliProgressUpdate = 500 * time.Millisecond
)
// resultWorker outputs the results as they come in. This needs to be a range and should not handle
// the context so the channel always has a receiver and libgobuster will not block.
func resultWorker(g *libgobuster.Gobuster, filename string, wg *sync.WaitGroup) {
defer wg.Done()
var f *os.File
var err error
if filename != "" {
f, err = os.Create(filename)
if err != nil {
g.Logger.Fatalf("error on creating output file: %v", err)
}
defer f.Close()
}
for r := range g.Progress.ResultChan {
s, err := r.ResultToString()
if err != nil {
g.Logger.Fatal(err)
}
if s != "" {
s = strings.TrimSpace(s)
if g.Opts.NoProgress || g.Opts.Quiet {
_, _ = fmt.Printf("%s\n", s) // nolint forbidigo
} else {
// only print the clear line when progress output is enabled
_, _ = fmt.Printf("%s%s\n", TerminalClearLine, s) // nolint forbidigo
}
if f != nil {
err = writeToFile(f, s)
if err != nil {
g.Logger.Fatalf("error on writing output file: %v", err)
}
}
}
}
}
// errorWorker outputs the errors as they come in. This needs to be a range and should not handle
// the context so the channel always has a receiver and libgobuster will not block.
func errorWorker(g *libgobuster.Gobuster, wg *sync.WaitGroup) {
defer wg.Done()
for e := range g.Progress.ErrorChan {
if !g.Opts.Quiet && !g.Opts.NoError {
g.Logger.Error(e.Error())
g.Logger.Debugf("%#v", e)
}
}
}
// messageWorker outputs messages as they come in. This needs to be a range and should not handle
// the context so the channel always has a receiver and libgobuster will not block.
func messageWorker(g *libgobuster.Gobuster, wg *sync.WaitGroup) {
defer wg.Done()
for msg := range g.Progress.MessageChan {
if !g.Opts.Quiet {
switch msg.Level {
case libgobuster.LevelDebug:
g.Logger.Debug(msg.Message)
case libgobuster.LevelError:
g.Logger.Error(msg.Message)
case libgobuster.LevelWarn:
g.Logger.Warn(msg.Message)
case libgobuster.LevelInfo:
g.Logger.Info(msg.Message)
default:
panic(fmt.Sprintf("invalid level %d", msg.Level))
}
}
}
}
func printProgress(g *libgobuster.Gobuster) {
requestsIssued := g.Progress.RequestsIssued()
requestsExpected := g.Progress.RequestsExpected()
if requestsExpected == 0 {
requestsExpected = 1 // avoid division by zero
}
percent := float32(requestsIssued) * 100.0 / float32(requestsExpected)
if math.IsNaN(float64(percent)) {
percent = 0.0
}
s := fmt.Sprintf("%sProgress: %d / %d (%3.2f%%)", TerminalClearLine, requestsIssued, requestsExpected, percent)
_, _ = fmt.Fprint(os.Stderr, s)
}
// progressWorker outputs the progress every tick. It will stop once cancel() is called
// on the context
func progressWorker(ctx context.Context, g *libgobuster.Gobuster, wg *sync.WaitGroup) {
defer wg.Done()
tick := time.NewTicker(cliProgressUpdate)
for {
select {
case <-tick.C:
printProgress(g)
case <-ctx.Done():
// print the final progress so we end at 100%
printProgress(g)
fmt.Println() // nolint:forbidigo
return
}
}
}
func writeToFile(f *os.File, output string) error {
_, err := fmt.Fprintf(f, "%s\n", output)
if err != nil {
return fmt.Errorf("[!] Unable to write to file %w", err)
}
return nil
}
// Gobuster is the main entry point for the CLI
func Gobuster(ctx context.Context, opts *libgobuster.Options, plugin libgobuster.GobusterPlugin, log *libgobuster.Logger) error {
// Sanity checks
if opts == nil {
return errors.New("please provide valid options")
}
if plugin == nil {
return errors.New("please provide a valid plugin")
}
ctxCancel, cancel := context.WithCancel(ctx)
defer cancel()
gobuster, err := libgobuster.NewGobuster(opts, plugin, log)
if err != nil {
return err
}
if !opts.Quiet {
log.Println(ruler)
log.Printf("Gobuster v%s\n", libgobuster.VERSION)
log.Println("by OJ Reeves (@TheColonial) & Christian Mehlmauer (@firefart)")
log.Println(ruler)
c, err := gobuster.GetConfigString()
if err != nil {
return fmt.Errorf("error on creating config string: %w", err)
}
log.Println(c)
log.Println(ruler)
gobuster.Logger.Printf("Starting gobuster in %s mode", plugin.Name())
if opts.WordlistOffset > 0 {
gobuster.Logger.Printf("Skipping the first %d elements...", opts.WordlistOffset)
}
log.Println(ruler)
}
fi, err := os.Stdout.Stat()
if err != nil {
return err
}
// check if we are not in a terminal. If so, disable output
if (fi.Mode() & os.ModeCharDevice) != os.ModeCharDevice {
opts.NoProgress = true
}
// our waitgroup for all goroutines
// this ensures all goroutines are finished
// when we call wg.Wait()
var wg sync.WaitGroup
wg.Add(1)
go resultWorker(gobuster, opts.OutputFilename, &wg)
wg.Add(1)
go errorWorker(gobuster, &wg)
wg.Add(1)
go messageWorker(gobuster, &wg)
if !opts.Quiet && !opts.NoProgress {
// if not quiet add a new workgroup entry and start the goroutine
wg.Add(1)
go progressWorker(ctxCancel, gobuster, &wg)
}
err = gobuster.Run(ctxCancel)
// call cancel func so progressWorker will exit (the only goroutine in this
// file using the context) and to free resources
cancel()
// wait for all spun up goroutines to finish (all have to call wg.Done())
wg.Wait()
// Late error checking to finish all threads
if err != nil {
return err
}
if !opts.Quiet {
log.Println(ruler)
gobuster.Logger.Println("Finished")
log.Println(ruler)
}
return nil
}
|