| 1 | package input |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/ffuf/ffuf/v2/pkg/ffuf" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/ffuf/pencode/pkg/pencode" |
| 9 | ) |
| 10 | |
| 11 | type MainInputProvider struct { |
| 12 | Providers []ffuf.InternalInputProvider |
| 13 | Encoders map[string]*pencode.Chain |
| 14 | Config *ffuf.Config |
| 15 | position int |
| 16 | msbIterator int |
| 17 | } |
| 18 | |
| 19 | func NewInputProvider(conf *ffuf.Config) (ffuf.InputProvider, ffuf.Multierror) { |
| 20 | validmode := false |
| 21 | errs := ffuf.NewMultierror() |
| 22 | for _, mode := range []string{"clusterbomb", "pitchfork", "sniper"} { |
| 23 | if conf.InputMode == mode { |
| 24 | validmode = true |
| 25 | } |
| 26 | } |
| 27 | if !validmode { |
| 28 | errs.Add(fmt.Errorf("Input mode (-mode) %s not recognized", conf.InputMode)) |
| 29 | return &MainInputProvider{}, errs |
| 30 | } |
| 31 | mainip := MainInputProvider{Config: conf, msbIterator: 0, Encoders: make(map[string]*pencode.Chain)} |
| 32 | // Initialize the correct inputprovider |
| 33 | for _, v := range conf.InputProviders { |
| 34 | err := mainip.AddProvider(v) |
| 35 | if err != nil { |
| 36 | errs.Add(err) |
| 37 | } |
| 38 | } |
| 39 | return &mainip, errs |
| 40 | } |
| 41 | |
| 42 | func (i *MainInputProvider) AddProvider(provider ffuf.InputProviderConfig) error { |
| 43 | if provider.Name == "command" { |
| 44 | newcomm, _ := NewCommandInput(provider.Keyword, provider.Value, i.Config) |
| 45 | i.Providers = append(i.Providers, newcomm) |
| 46 | } else { |
| 47 | // Default to wordlist |
| 48 | newwl, err := NewWordlistInput(provider.Keyword, provider.Value, i.Config) |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | i.Providers = append(i.Providers, newwl) |
| 53 | } |
| 54 | if len(provider.Encoders) > 0 { |
| 55 | chain := pencode.NewChain() |
| 56 | err := chain.Initialize(strings.Split(strings.TrimSpace(provider.Encoders), " ")) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | i.Encoders[provider.Keyword] = chain |
| 61 | } |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | // ActivateKeywords enables / disables wordlists based on list of active keywords |
| 66 | func (i *MainInputProvider) ActivateKeywords(kws []string) { |
| 67 | for _, p := range i.Providers { |
| 68 | if ffuf.StrInSlice(p.Keyword(), kws) { |
| 69 | p.Active() |
| 70 | } else { |
| 71 | p.Disable() |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Position will return the current position of progress |
| 77 | func (i *MainInputProvider) Position() int { |
| 78 | return i.position |
| 79 | } |
| 80 | |
| 81 | // SetPosition will reset the MainInputProvider to a specific position |
| 82 | func (i *MainInputProvider) SetPosition(pos int) { |
| 83 | if i.Config.InputMode == "clusterbomb" || i.Config.InputMode == "sniper" { |
| 84 | i.setclusterbombPosition(pos) |
| 85 | } else { |
| 86 | i.setpitchforkPosition(pos) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Keywords returns a slice of all keywords in the inputprovider |
| 91 | func (i *MainInputProvider) Keywords() []string { |
| 92 | kws := make([]string, 0) |
| 93 | for _, p := range i.Providers { |
| 94 | kws = append(kws, p.Keyword()) |
| 95 | } |
| 96 | return kws |
| 97 | } |
| 98 | |
| 99 | // Next will increment the cursor position, and return a boolean telling if there's inputs left |
| 100 | func (i *MainInputProvider) Next() bool { |
| 101 | if i.position >= i.Total() { |
| 102 | return false |
| 103 | } |
| 104 | i.position++ |
| 105 | return true |
| 106 | } |
| 107 | |
| 108 | // Value returns a map of inputs for keywords |
| 109 | func (i *MainInputProvider) Value() map[string][]byte { |
| 110 | retval := make(map[string][]byte) |
| 111 | if i.Config.InputMode == "clusterbomb" || i.Config.InputMode == "sniper" { |
| 112 | retval = i.clusterbombValue() |
| 113 | } |
| 114 | if i.Config.InputMode == "pitchfork" { |
| 115 | retval = i.pitchforkValue() |
| 116 | } |
| 117 | if len(i.Encoders) > 0 { |
| 118 | for key, val := range retval { |
| 119 | chain, ok := i.Encoders[key] |
| 120 | if ok { |
| 121 | tmpVal, err := chain.Encode([]byte(val)) |
| 122 | if err != nil { |
| 123 | fmt.Printf("ERROR: %s\n", err) |
| 124 | } |
| 125 | retval[key] = tmpVal |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return retval |
| 130 | } |
| 131 | |
| 132 | // Reset resets all the inputproviders and counters |
| 133 | func (i *MainInputProvider) Reset() { |
| 134 | for _, p := range i.Providers { |
| 135 | p.ResetPosition() |
| 136 | } |
| 137 | i.position = 0 |
| 138 | i.msbIterator = 0 |
| 139 | } |
| 140 | |
| 141 | // pitchforkValue returns a map of keyword:value pairs including all inputs. |
| 142 | // This mode will iterate through wordlists in lockstep. |
| 143 | func (i *MainInputProvider) pitchforkValue() map[string][]byte { |
| 144 | values := make(map[string][]byte) |
| 145 | for _, p := range i.Providers { |
| 146 | if !p.Active() { |
| 147 | // The inputprovider is disabled |
| 148 | continue |
| 149 | } |
| 150 | if !p.Next() { |
| 151 | // Loop to beginning if the inputprovider has been exhausted |
| 152 | p.ResetPosition() |
| 153 | } |
| 154 | values[p.Keyword()] = p.Value() |
| 155 | p.IncrementPosition() |
| 156 | } |
| 157 | return values |
| 158 | } |
| 159 | |
| 160 | func (i *MainInputProvider) setpitchforkPosition(pos int) { |
| 161 | for _, p := range i.Providers { |
| 162 | p.SetPosition(pos) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // clusterbombValue returns map of keyword:value pairs including all inputs. |
| 167 | // this mode will iterate through all possible combinations. |
| 168 | func (i *MainInputProvider) clusterbombValue() map[string][]byte { |
| 169 | values := make(map[string][]byte) |
| 170 | // Should we signal the next InputProvider in the slice to increment |
| 171 | signalNext := false |
| 172 | first := true |
| 173 | index := 0 |
| 174 | for _, p := range i.Providers { |
| 175 | if !p.Active() { |
| 176 | continue |
| 177 | } |
| 178 | if signalNext { |
| 179 | p.IncrementPosition() |
| 180 | signalNext = false |
| 181 | } |
| 182 | if !p.Next() { |
| 183 | // No more inputs in this inputprovider |
| 184 | if index == i.msbIterator { |
| 185 | // Reset all previous wordlists and increment the msb counter |
| 186 | i.msbIterator += 1 |
| 187 | i.clusterbombIteratorReset() |
| 188 | // Start again |
| 189 | return i.clusterbombValue() |
| 190 | } |
| 191 | p.ResetPosition() |
| 192 | signalNext = true |
| 193 | } |
| 194 | values[p.Keyword()] = p.Value() |
| 195 | if first { |
| 196 | p.IncrementPosition() |
| 197 | first = false |
| 198 | } |
| 199 | index += 1 |
| 200 | } |
| 201 | return values |
| 202 | } |
| 203 | |
| 204 | func (i *MainInputProvider) setclusterbombPosition(pos int) { |
| 205 | i.Reset() |
| 206 | if pos > i.Total() { |
| 207 | // noop |
| 208 | return |
| 209 | } |
| 210 | for i.position < pos-1 { |
| 211 | i.Next() |
| 212 | i.Value() |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | func (i *MainInputProvider) clusterbombIteratorReset() { |
| 217 | index := 0 |
| 218 | for _, p := range i.Providers { |
| 219 | if !p.Active() { |
| 220 | continue |
| 221 | } |
| 222 | if index < i.msbIterator { |
| 223 | p.ResetPosition() |
| 224 | } |
| 225 | if index == i.msbIterator { |
| 226 | p.IncrementPosition() |
| 227 | } |
| 228 | index += 1 |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // Total returns the amount of input combinations available |
| 233 | func (i *MainInputProvider) Total() int { |
| 234 | count := 0 |
| 235 | if i.Config.InputMode == "pitchfork" { |
| 236 | for _, p := range i.Providers { |
| 237 | if !p.Active() { |
| 238 | continue |
| 239 | } |
| 240 | if p.Total() > count { |
| 241 | count = p.Total() |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | if i.Config.InputMode == "clusterbomb" || i.Config.InputMode == "sniper" { |
| 246 | count = 1 |
| 247 | for _, p := range i.Providers { |
| 248 | if !p.Active() { |
| 249 | continue |
| 250 | } |
| 251 | count = count * p.Total() |
| 252 | } |
| 253 | } |
| 254 | return count |
| 255 | } |
| 256 |