File: dir_test.go

package info (click to toggle)
gobuster 3.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 932 kB
  • sloc: makefile: 7
file content (97 lines) | stat: -rw-r--r-- 2,336 bytes parent folder | download
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
package dir

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"testing"
	"time"

	"github.com/OJ/gobuster/v3/cli"
	"github.com/OJ/gobuster/v3/gobusterdir"
	"github.com/OJ/gobuster/v3/libgobuster"
)

func httpServer(b *testing.B, content string) *httptest.Server {
	b.Helper()
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		if _, err := fmt.Fprint(w, content); err != nil {
			b.Fatalf("%v", err)
		}
	}))
	return ts
}

func BenchmarkDirMode(b *testing.B) {
	h := httpServer(b, "test")
	defer h.Close()

	u, err := url.Parse(h.URL)
	if err != nil {
		b.Fatalf("could not parse URL: %v", err)
	}
	pluginopts := gobusterdir.NewOptions()
	pluginopts.URL = u
	pluginopts.Timeout = 10 * time.Second

	pluginopts.Extensions = ".php,.csv"
	tmpExt, err := libgobuster.ParseExtensions(pluginopts.Extensions)
	if err != nil {
		b.Fatalf("could not parse extensions: %v", err)
	}
	pluginopts.ExtensionsParsed = tmpExt

	pluginopts.StatusCodes = "200,204,301,302,307,401,403"
	tmpStat, err := libgobuster.ParseCommaSeparatedInt(pluginopts.StatusCodes)
	if err != nil {
		b.Fatalf("could not parse status codes: %v", err)
	}
	pluginopts.StatusCodesParsed = tmpStat

	wordlist, err := os.CreateTemp(b.TempDir(), "")
	if err != nil {
		b.Fatalf("could not create tempfile: %v", err)
	}
	defer os.Remove(wordlist.Name())
	for w := range 1000 {
		_, _ = fmt.Fprintf(wordlist, "%d\n", w)
	}
	if err := wordlist.Close(); err != nil {
		b.Fatalf("%v", err)
	}

	globalopts := libgobuster.Options{
		Threads:    10,
		Wordlist:   wordlist.Name(),
		NoProgress: true,
	}

	ctx := b.Context()
	oldStdout := os.Stdout
	oldStderr := os.Stderr
	defer func(out, err *os.File) { os.Stdout = out; os.Stderr = err }(oldStdout, oldStderr)
	devnull, err := os.Open(os.DevNull)
	if err != nil {
		b.Fatalf("could not get devnull %v", err)
	}
	defer devnull.Close()
	log := libgobuster.NewLogger(false)

	// Run the real benchmark
	for b.Loop() {
		os.Stdout = devnull
		os.Stderr = devnull
		plugin, err := gobusterdir.New(&globalopts, pluginopts, log)
		if err != nil {
			b.Fatalf("error on creating gobusterdir: %v", err)
		}

		if err := cli.Gobuster(ctx, &globalopts, plugin, log); err != nil {
			b.Fatalf("error on running gobuster: %v", err)
		}
		os.Stdout = oldStdout
		os.Stderr = oldStderr
	}
}