| 1 | package input |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "os" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/ffuf/ffuf/v2/pkg/ffuf" |
| 10 | ) |
| 11 | |
| 12 | type WordlistInput struct { |
| 13 | active bool |
| 14 | config *ffuf.Config |
| 15 | data [][]byte |
| 16 | position int |
| 17 | keyword string |
| 18 | } |
| 19 | |
| 20 | func NewWordlistInput(keyword string, value string, conf *ffuf.Config) (*WordlistInput, error) { |
| 21 | var wl WordlistInput |
| 22 | wl.active = true |
| 23 | wl.keyword = keyword |
| 24 | wl.config = conf |
| 25 | wl.position = 0 |
| 26 | var valid bool |
| 27 | var err error |
| 28 | // stdin? |
| 29 | if value == "-" { |
| 30 | // yes |
| 31 | valid = true |
| 32 | } else { |
| 33 | // no |
| 34 | valid, err = wl.validFile(value) |
| 35 | } |
| 36 | if err != nil { |
| 37 | return &wl, err |
| 38 | } |
| 39 | if valid { |
| 40 | err = wl.readFile(value) |
| 41 | } |
| 42 | return &wl, err |
| 43 | } |
| 44 | |
| 45 | // Position will return the current position in the input list |
| 46 | func (w *WordlistInput) Position() int { |
| 47 | return w.position |
| 48 | } |
| 49 | |
| 50 | // SetPosition sets the current position of the inputprovider |
| 51 | func (w *WordlistInput) SetPosition(pos int) { |
| 52 | w.position = pos |
| 53 | } |
| 54 | |
| 55 | // ResetPosition resets the position back to beginning of the wordlist. |
| 56 | func (w *WordlistInput) ResetPosition() { |
| 57 | w.position = 0 |
| 58 | } |
| 59 | |
| 60 | // Keyword returns the keyword assigned to this InternalInputProvider |
| 61 | func (w *WordlistInput) Keyword() string { |
| 62 | return w.keyword |
| 63 | } |
| 64 | |
| 65 | // Next will return a boolean telling if there's words left in the list |
| 66 | func (w *WordlistInput) Next() bool { |
| 67 | return w.position < len(w.data) |
| 68 | } |
| 69 | |
| 70 | // IncrementPosition will increment the current position in the inputprovider data slice |
| 71 | func (w *WordlistInput) IncrementPosition() { |
| 72 | w.position += 1 |
| 73 | } |
| 74 | |
| 75 | // Value returns the value from wordlist at current cursor position |
| 76 | func (w *WordlistInput) Value() []byte { |
| 77 | return w.data[w.position] |
| 78 | } |
| 79 | |
| 80 | // Total returns the size of wordlist |
| 81 | func (w *WordlistInput) Total() int { |
| 82 | return len(w.data) |
| 83 | } |
| 84 | |
| 85 | // Active returns boolean if the inputprovider is active |
| 86 | func (w *WordlistInput) Active() bool { |
| 87 | return w.active |
| 88 | } |
| 89 | |
| 90 | // Enable sets the inputprovider as active |
| 91 | func (w *WordlistInput) Enable() { |
| 92 | w.active = true |
| 93 | } |
| 94 | |
| 95 | // Disable disables the inputprovider |
| 96 | func (w *WordlistInput) Disable() { |
| 97 | w.active = false |
| 98 | } |
| 99 | |
| 100 | // validFile checks that the wordlist file exists and can be read |
| 101 | func (w *WordlistInput) validFile(path string) (bool, error) { |
| 102 | _, err := os.Stat(path) |
| 103 | if err != nil { |
| 104 | return false, err |
| 105 | } |
| 106 | f, err := os.Open(path) |
| 107 | if err != nil { |
| 108 | return false, err |
| 109 | } |
| 110 | f.Close() |
| 111 | return true, nil |
| 112 | } |
| 113 | |
| 114 | // readFile reads the file line by line to a byte slice |
| 115 | func (w *WordlistInput) readFile(path string) error { |
| 116 | var file *os.File |
| 117 | var err error |
| 118 | if path == "-" { |
| 119 | file = os.Stdin |
| 120 | } else { |
| 121 | file, err = os.Open(path) |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | } |
| 126 | defer file.Close() |
| 127 | |
| 128 | var data [][]byte |
| 129 | var ok bool |
| 130 | reader := bufio.NewScanner(file) |
| 131 | re := regexp.MustCompile(`(?i)%ext%`) |
| 132 | for reader.Scan() { |
| 133 | if w.config.DirSearchCompat && len(w.config.Extensions) > 0 { |
| 134 | text := []byte(reader.Text()) |
| 135 | if re.Match(text) { |
| 136 | for _, ext := range w.config.Extensions { |
| 137 | contnt := re.ReplaceAll(text, []byte(ext)) |
| 138 | data = append(data, []byte(contnt)) |
| 139 | } |
| 140 | } else { |
| 141 | text := reader.Text() |
| 142 | |
| 143 | if w.config.IgnoreWordlistComments { |
| 144 | text, ok = stripComments(text) |
| 145 | if !ok { |
| 146 | continue |
| 147 | } |
| 148 | } |
| 149 | data = append(data, []byte(text)) |
| 150 | } |
| 151 | } else { |
| 152 | text := reader.Text() |
| 153 | |
| 154 | if w.config.IgnoreWordlistComments { |
| 155 | text, ok = stripComments(text) |
| 156 | if !ok { |
| 157 | continue |
| 158 | } |
| 159 | } |
| 160 | data = append(data, []byte(text)) |
| 161 | if w.keyword == "FUZZ" && len(w.config.Extensions) > 0 { |
| 162 | for _, ext := range w.config.Extensions { |
| 163 | data = append(data, []byte(text+ext)) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | w.data = data |
| 169 | return reader.Err() |
| 170 | } |
| 171 | |
| 172 | // stripComments removes all kind of comments from the word |
| 173 | func stripComments(text string) (string, bool) { |
| 174 | // If the line starts with a # ignoring any space on the left, |
| 175 | // return blank. |
| 176 | if strings.HasPrefix(strings.TrimLeft(text, " "), "#") { |
| 177 | return "", false |
| 178 | } |
| 179 | |
| 180 | // If the line has # later after a space, that's a comment. |
| 181 | // Only send the word upto space to the routine. |
| 182 | index := strings.Index(text, " #") |
| 183 | if index == -1 { |
| 184 | return text, true |
| 185 | } |
| 186 | return text[:index], true |
| 187 | } |