/
optionsparser.go
762 lines (697 loc) · 22.2 KB
/
optionsparser.go
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
package ffuf
import (
"bufio"
"context"
"fmt"
"io"
"net/textproto"
"net/url"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/pelletier/go-toml"
)
type ConfigOptions struct {
Filter FilterOptions `json:"filters"`
General GeneralOptions `json:"general"`
HTTP HTTPOptions `json:"http"`
Input InputOptions `json:"input"`
Matcher MatcherOptions `json:"matchers"`
Output OutputOptions `json:"output"`
}
type HTTPOptions struct {
Cookies []string `json:"-"` // this is appended in headers
Data string `json:"data"`
FollowRedirects bool `json:"follow_redirects"`
Headers []string `json:"headers"`
IgnoreBody bool `json:"ignore_body"`
Method string `json:"method"`
ProxyURL string `json:"proxy_url"`
Recursion bool `json:"recursion"`
RecursionDepth int `json:"recursion_depth"`
RecursionStrategy string `json:"recursion_strategy"`
ReplayProxyURL string `json:"replay_proxy_url"`
SNI string `json:"sni"`
Timeout int `json:"timeout"`
URL string `json:"url"`
Http2 bool `json:"http2"`
ClientCert string `json:"client-cert"`
ClientKey string `json:"client-key"`
}
type GeneralOptions struct {
AutoCalibration bool `json:"autocalibration"`
AutoCalibrationKeyword string `json:"autocalibration_keyword"`
AutoCalibrationPerHost bool `json:"autocalibration_per_host"`
AutoCalibrationStrategy string `json:"autocalibration_strategy"`
AutoCalibrationStrings []string `json:"autocalibration_strings"`
Colors bool `json:"colors"`
ConfigFile string `toml:"-" json:"config_file"`
Delay string `json:"delay"`
Json bool `json:"json"`
MaxTime int `json:"maxtime"`
MaxTimeJob int `json:"maxtime_job"`
Noninteractive bool `json:"noninteractive"`
Quiet bool `json:"quiet"`
Rate int `json:"rate"`
ScraperFile string `json:"scraperfile"`
Scrapers string `json:"scrapers"`
Searchhash string `json:"-"`
ShowVersion bool `toml:"-" json:"-"`
StopOn403 bool `json:"stop_on_403"`
StopOnAll bool `json:"stop_on_all"`
StopOnErrors bool `json:"stop_on_errors"`
Threads int `json:"threads"`
Verbose bool `json:"verbose"`
}
type InputOptions struct {
DirSearchCompat bool `json:"dirsearch_compat"`
Encoders []string `json:"encoders"`
Extensions string `json:"extensions"`
IgnoreWordlistComments bool `json:"ignore_wordlist_comments"`
InputMode string `json:"input_mode"`
InputNum int `json:"input_num"`
InputShell string `json:"input_shell"`
Inputcommands []string `json:"input_commands"`
Request string `json:"request_file"`
RequestProto string `json:"request_proto"`
Wordlists []string `json:"wordlists"`
}
type OutputOptions struct {
DebugLog string `json:"debug_log"`
OutputDirectory string `json:"output_directory"`
OutputFile string `json:"output_file"`
OutputFormat string `json:"output_format"`
OutputSkipEmptyFile bool `json:"output_skip_empty"`
}
type FilterOptions struct {
Mode string `json:"mode"`
Lines string `json:"lines"`
Regexp string `json:"regexp"`
Size string `json:"size"`
Status string `json:"status"`
Time string `json:"time"`
Words string `json:"words"`
}
type MatcherOptions struct {
Mode string `json:"mode"`
Lines string `json:"lines"`
Regexp string `json:"regexp"`
Size string `json:"size"`
Status string `json:"status"`
Time string `json:"time"`
Words string `json:"words"`
}
// NewConfigOptions returns a newly created ConfigOptions struct with default values
func NewConfigOptions() *ConfigOptions {
c := &ConfigOptions{}
c.Filter.Mode = "or"
c.Filter.Lines = ""
c.Filter.Regexp = ""
c.Filter.Size = ""
c.Filter.Status = ""
c.Filter.Time = ""
c.Filter.Words = ""
c.General.AutoCalibration = false
c.General.AutoCalibrationKeyword = "FUZZ"
c.General.AutoCalibrationStrategy = "basic"
c.General.Colors = false
c.General.Delay = ""
c.General.Json = false
c.General.MaxTime = 0
c.General.MaxTimeJob = 0
c.General.Noninteractive = false
c.General.Quiet = false
c.General.Rate = 0
c.General.Searchhash = ""
c.General.ScraperFile = ""
c.General.Scrapers = "all"
c.General.ShowVersion = false
c.General.StopOn403 = false
c.General.StopOnAll = false
c.General.StopOnErrors = false
c.General.Threads = 40
c.General.Verbose = false
c.HTTP.Data = ""
c.HTTP.FollowRedirects = false
c.HTTP.IgnoreBody = false
c.HTTP.Method = ""
c.HTTP.ProxyURL = ""
c.HTTP.Recursion = false
c.HTTP.RecursionDepth = 0
c.HTTP.RecursionStrategy = "default"
c.HTTP.ReplayProxyURL = ""
c.HTTP.Timeout = 10
c.HTTP.SNI = ""
c.HTTP.URL = ""
c.HTTP.Http2 = false
c.Input.DirSearchCompat = false
c.Input.Encoders = []string{}
c.Input.Extensions = ""
c.Input.IgnoreWordlistComments = false
c.Input.InputMode = "clusterbomb"
c.Input.InputNum = 100
c.Input.Request = ""
c.Input.RequestProto = "https"
c.Matcher.Mode = "or"
c.Matcher.Lines = ""
c.Matcher.Regexp = ""
c.Matcher.Size = ""
c.Matcher.Status = "200,204,301,302,307,401,403,405,500"
c.Matcher.Time = ""
c.Matcher.Words = ""
c.Output.DebugLog = ""
c.Output.OutputDirectory = ""
c.Output.OutputFile = ""
c.Output.OutputFormat = "json"
c.Output.OutputSkipEmptyFile = false
return c
}
// ConfigFromOptions parses the values in ConfigOptions struct, ensures that the values are sane,
// and creates a Config struct out of them.
func ConfigFromOptions(parseOpts *ConfigOptions, ctx context.Context, cancel context.CancelFunc) (*Config, error) {
//TODO: refactor in a proper flag library that can handle things like required flags
errs := NewMultierror()
conf := NewConfig(ctx, cancel)
var err error
var err2 error
if len(parseOpts.HTTP.URL) == 0 && parseOpts.Input.Request == "" {
errs.Add(fmt.Errorf("-u flag or -request flag is required"))
}
// prepare extensions
if parseOpts.Input.Extensions != "" {
extensions := strings.Split(parseOpts.Input.Extensions, ",")
conf.Extensions = extensions
}
// Convert cookies to a header
if len(parseOpts.HTTP.Cookies) > 0 {
parseOpts.HTTP.Headers = append(parseOpts.HTTP.Headers, "Cookie: "+strings.Join(parseOpts.HTTP.Cookies, "; "))
}
//Prepare inputproviders
conf.InputMode = parseOpts.Input.InputMode
validmode := false
for _, mode := range []string{"clusterbomb", "pitchfork", "sniper"} {
if conf.InputMode == mode {
validmode = true
}
}
if !validmode {
errs.Add(fmt.Errorf("Input mode (-mode) %s not recognized", conf.InputMode))
}
template := ""
// sniper mode needs some additional checking
if conf.InputMode == "sniper" {
template = "§"
if len(parseOpts.Input.Wordlists) > 1 {
errs.Add(fmt.Errorf("sniper mode only supports one wordlist"))
}
if len(parseOpts.Input.Inputcommands) > 1 {
errs.Add(fmt.Errorf("sniper mode only supports one input command"))
}
}
tmpEncoders := make(map[string]string)
for _, e := range parseOpts.Input.Encoders {
if strings.Contains(e, ":") {
key := strings.Split(e, ":")[0]
val := strings.Split(e, ":")[1]
tmpEncoders[key] = val
}
}
tmpWordlists := make([]string, 0)
for _, v := range parseOpts.Input.Wordlists {
var wl []string
if runtime.GOOS == "windows" {
// Try to ensure that Windows file paths like C:\path\to\wordlist.txt:KEYWORD are treated properly
if FileExists(v) {
// The wordlist was supplied without a keyword parameter
wl = []string{v}
} else {
filepart := v
if strings.Contains(filepart, ":") {
filepart = v[:strings.LastIndex(filepart, ":")]
}
if FileExists(filepart) {
wl = []string{filepart, v[strings.LastIndex(v, ":")+1:]}
} else {
// The file was not found. Use full wordlist parameter value for more concise error message down the line
wl = []string{v}
}
}
} else {
wl = strings.SplitN(v, ":", 2)
}
// Try to use absolute paths for wordlists
fullpath := ""
if wl[0] != "-" {
fullpath, err = filepath.Abs(wl[0])
} else {
fullpath = wl[0]
}
if err == nil {
wl[0] = fullpath
}
if len(wl) == 2 {
if conf.InputMode == "sniper" {
errs.Add(fmt.Errorf("sniper mode does not support wordlist keywords"))
} else {
newp := InputProviderConfig{
Name: "wordlist",
Value: wl[0],
Keyword: wl[1],
}
// Add encoders if set
enc, ok := tmpEncoders[wl[1]]
if ok {
newp.Encoders = enc
}
conf.InputProviders = append(conf.InputProviders, newp)
}
} else {
newp := InputProviderConfig{
Name: "wordlist",
Value: wl[0],
Keyword: "FUZZ",
Template: template,
}
// Add encoders if set
enc, ok := tmpEncoders["FUZZ"]
if ok {
newp.Encoders = enc
}
conf.InputProviders = append(conf.InputProviders, newp)
}
tmpWordlists = append(tmpWordlists, strings.Join(wl, ":"))
}
conf.Wordlists = tmpWordlists
for _, v := range parseOpts.Input.Inputcommands {
ic := strings.SplitN(v, ":", 2)
if len(ic) == 2 {
if conf.InputMode == "sniper" {
errs.Add(fmt.Errorf("sniper mode does not support command keywords"))
} else {
newp := InputProviderConfig{
Name: "command",
Value: ic[0],
Keyword: ic[1],
}
enc, ok := tmpEncoders[ic[1]]
if ok {
newp.Encoders = enc
}
conf.InputProviders = append(conf.InputProviders, newp)
conf.CommandKeywords = append(conf.CommandKeywords, ic[0])
}
} else {
newp := InputProviderConfig{
Name: "command",
Value: ic[0],
Keyword: "FUZZ",
Template: template,
}
enc, ok := tmpEncoders["FUZZ"]
if ok {
newp.Encoders = enc
}
conf.InputProviders = append(conf.InputProviders, newp)
conf.CommandKeywords = append(conf.CommandKeywords, "FUZZ")
}
}
if len(conf.InputProviders) == 0 {
errs.Add(fmt.Errorf("Either -w or --input-cmd flag is required"))
}
// Prepare the request using body
if parseOpts.Input.Request != "" {
err := parseRawRequest(parseOpts, &conf)
if err != nil {
errmsg := fmt.Sprintf("Could not parse raw request: %s", err)
errs.Add(fmt.Errorf(errmsg))
}
}
//Prepare URL
if parseOpts.HTTP.URL != "" {
conf.Url = parseOpts.HTTP.URL
}
// Prepare SNI
if parseOpts.HTTP.SNI != "" {
conf.SNI = parseOpts.HTTP.SNI
}
// prepare cert
if parseOpts.HTTP.ClientCert != "" {
conf.ClientCert = parseOpts.HTTP.ClientCert
}
if parseOpts.HTTP.ClientKey != "" {
conf.ClientKey = parseOpts.HTTP.ClientKey
}
//Prepare headers and make canonical
for _, v := range parseOpts.HTTP.Headers {
hs := strings.SplitN(v, ":", 2)
if len(hs) == 2 {
// trim and make canonical
// except if used in custom defined header
var CanonicalNeeded = true
for _, a := range conf.CommandKeywords {
if strings.Contains(hs[0], a) {
CanonicalNeeded = false
}
}
// check if part of InputProviders
if CanonicalNeeded {
for _, b := range conf.InputProviders {
if strings.Contains(hs[0], b.Keyword) {
CanonicalNeeded = false
}
}
}
if CanonicalNeeded {
var CanonicalHeader = textproto.CanonicalMIMEHeaderKey(strings.TrimSpace(hs[0]))
conf.Headers[CanonicalHeader] = strings.TrimSpace(hs[1])
} else {
conf.Headers[strings.TrimSpace(hs[0])] = strings.TrimSpace(hs[1])
}
} else {
errs.Add(fmt.Errorf("Header defined by -H needs to have a value. \":\" should be used as a separator"))
}
}
//Prepare delay
d := strings.Split(parseOpts.General.Delay, "-")
if len(d) > 2 {
errs.Add(fmt.Errorf("Delay needs to be either a single float: \"0.1\" or a range of floats, delimited by dash: \"0.1-0.8\""))
} else if len(d) == 2 {
conf.Delay.IsRange = true
conf.Delay.HasDelay = true
conf.Delay.Min, err = strconv.ParseFloat(d[0], 64)
conf.Delay.Max, err2 = strconv.ParseFloat(d[1], 64)
if err != nil || err2 != nil {
errs.Add(fmt.Errorf("Delay range min and max values need to be valid floats. For example: 0.1-0.5"))
}
} else if len(parseOpts.General.Delay) > 0 {
conf.Delay.IsRange = false
conf.Delay.HasDelay = true
conf.Delay.Min, err = strconv.ParseFloat(parseOpts.General.Delay, 64)
if err != nil {
errs.Add(fmt.Errorf("Delay needs to be either a single float: \"0.1\" or a range of floats, delimited by dash: \"0.1-0.8\""))
}
}
// Verify proxy url format
if len(parseOpts.HTTP.ProxyURL) > 0 {
u, err := url.Parse(parseOpts.HTTP.ProxyURL)
if err != nil || u.Opaque != "" || (u.Scheme != "http" && u.Scheme != "https" && u.Scheme != "socks5") {
errs.Add(fmt.Errorf("Bad proxy url (-x) format. Expected http, https or socks5 url"))
} else {
conf.ProxyURL = parseOpts.HTTP.ProxyURL
}
}
// Verify replayproxy url format
if len(parseOpts.HTTP.ReplayProxyURL) > 0 {
u, err := url.Parse(parseOpts.HTTP.ReplayProxyURL)
if err != nil || u.Opaque != "" || (u.Scheme != "http" && u.Scheme != "https" && u.Scheme != "socks5" && u.Scheme != "socks5h") {
errs.Add(fmt.Errorf("Bad replay-proxy url (-replay-proxy) format. Expected http, https or socks5 url"))
} else {
conf.ReplayProxyURL = parseOpts.HTTP.ReplayProxyURL
}
}
//Check the output file format option
if parseOpts.Output.OutputFile != "" {
//No need to check / error out if output file isn't defined
outputFormats := []string{"all", "json", "ejson", "html", "md", "csv", "ecsv"}
found := false
for _, f := range outputFormats {
if f == parseOpts.Output.OutputFormat {
conf.OutputFormat = f
found = true
}
}
if !found {
errs.Add(fmt.Errorf("Unknown output file format (-of): %s", parseOpts.Output.OutputFormat))
}
}
// Auto-calibration strings
if len(parseOpts.General.AutoCalibrationStrings) > 0 {
conf.AutoCalibrationStrings = parseOpts.General.AutoCalibrationStrings
}
// Using -acc implies -ac
if len(parseOpts.General.AutoCalibrationStrings) > 0 {
conf.AutoCalibration = true
}
if parseOpts.General.Rate < 0 {
conf.Rate = 0
} else {
conf.Rate = int64(parseOpts.General.Rate)
}
if conf.Method == "" {
if parseOpts.HTTP.Method == "" {
// Only set if defined on command line, because we might be reparsing the CLI after
// populating it through raw request in the first iteration
conf.Method = "GET"
} else {
conf.Method = parseOpts.HTTP.Method
}
} else {
if parseOpts.HTTP.Method != "" {
// Method overridden in CLI
conf.Method = parseOpts.HTTP.Method
}
}
if parseOpts.HTTP.Data != "" {
// Only set if defined on command line, because we might be reparsing the CLI after
// populating it through raw request in the first iteration
conf.Data = parseOpts.HTTP.Data
}
// Common stuff
conf.IgnoreWordlistComments = parseOpts.Input.IgnoreWordlistComments
conf.DirSearchCompat = parseOpts.Input.DirSearchCompat
conf.Colors = parseOpts.General.Colors
conf.InputNum = parseOpts.Input.InputNum
conf.InputShell = parseOpts.Input.InputShell
conf.OutputFile = parseOpts.Output.OutputFile
conf.OutputDirectory = parseOpts.Output.OutputDirectory
conf.OutputSkipEmptyFile = parseOpts.Output.OutputSkipEmptyFile
conf.IgnoreBody = parseOpts.HTTP.IgnoreBody
conf.Quiet = parseOpts.General.Quiet
conf.ScraperFile = parseOpts.General.ScraperFile
conf.Scrapers = parseOpts.General.Scrapers
conf.StopOn403 = parseOpts.General.StopOn403
conf.StopOnAll = parseOpts.General.StopOnAll
conf.StopOnErrors = parseOpts.General.StopOnErrors
conf.FollowRedirects = parseOpts.HTTP.FollowRedirects
conf.Recursion = parseOpts.HTTP.Recursion
conf.RecursionDepth = parseOpts.HTTP.RecursionDepth
conf.RecursionStrategy = parseOpts.HTTP.RecursionStrategy
conf.AutoCalibration = parseOpts.General.AutoCalibration
conf.AutoCalibrationPerHost = parseOpts.General.AutoCalibrationPerHost
conf.AutoCalibrationStrategy = parseOpts.General.AutoCalibrationStrategy
conf.Threads = parseOpts.General.Threads
conf.Timeout = parseOpts.HTTP.Timeout
conf.MaxTime = parseOpts.General.MaxTime
conf.MaxTimeJob = parseOpts.General.MaxTimeJob
conf.Noninteractive = parseOpts.General.Noninteractive
conf.Verbose = parseOpts.General.Verbose
conf.Json = parseOpts.General.Json
conf.Http2 = parseOpts.HTTP.Http2
// Check that fmode and mmode have sane values
valid_opmodes := []string{"and", "or"}
fmode_found := false
mmode_found := false
for _, v := range valid_opmodes {
if v == parseOpts.Filter.Mode {
fmode_found = true
}
if v == parseOpts.Matcher.Mode {
mmode_found = true
}
}
if !fmode_found {
errmsg := fmt.Sprintf("Unrecognized value for parameter fmode: %s, valid values are: and, or", parseOpts.Filter.Mode)
errs.Add(fmt.Errorf(errmsg))
}
if !mmode_found {
errmsg := fmt.Sprintf("Unrecognized value for parameter mmode: %s, valid values are: and, or", parseOpts.Matcher.Mode)
errs.Add(fmt.Errorf(errmsg))
}
conf.FilterMode = parseOpts.Filter.Mode
conf.MatcherMode = parseOpts.Matcher.Mode
if conf.AutoCalibrationPerHost {
// AutoCalibrationPerHost implies AutoCalibration
conf.AutoCalibration = true
}
// Handle copy as curl situation where POST method is implied by --data flag. If method is set to anything but GET, NOOP
if len(conf.Data) > 0 &&
conf.Method == "GET" &&
//don't modify the method automatically if a request file is being used as input
len(parseOpts.Input.Request) == 0 {
conf.Method = "POST"
}
conf.CommandLine = strings.Join(os.Args, " ")
for _, provider := range conf.InputProviders {
if provider.Template != "" {
if !templatePresent(provider.Template, &conf) {
errmsg := fmt.Sprintf("Template %s defined, but not found in pairs in headers, method, URL or POST data.", provider.Template)
errs.Add(fmt.Errorf(errmsg))
}
} else {
if !keywordPresent(provider.Keyword, &conf) {
errmsg := fmt.Sprintf("Keyword %s defined, but not found in headers, method, URL or POST data.", provider.Keyword)
errs.Add(fmt.Errorf(errmsg))
}
}
}
// If sniper mode, ensure there is no FUZZ keyword
if conf.InputMode == "sniper" {
if keywordPresent("FUZZ", &conf) {
errs.Add(fmt.Errorf("FUZZ keyword defined, but we are using sniper mode."))
}
}
// Do checks for recursion mode
if parseOpts.HTTP.Recursion {
if !strings.HasSuffix(conf.Url, "FUZZ") {
errmsg := "When using -recursion the URL (-u) must end with FUZZ keyword."
errs.Add(fmt.Errorf(errmsg))
}
}
// Make verbose mutually exclusive with json
if parseOpts.General.Verbose && parseOpts.General.Json {
errs.Add(fmt.Errorf("Cannot have -json and -v"))
}
return &conf, errs.ErrorOrNil()
}
func parseRawRequest(parseOpts *ConfigOptions, conf *Config) error {
conf.RequestFile = parseOpts.Input.Request
conf.RequestProto = parseOpts.Input.RequestProto
file, err := os.Open(parseOpts.Input.Request)
if err != nil {
return fmt.Errorf("could not open request file: %s", err)
}
defer file.Close()
r := bufio.NewReader(file)
s, err := r.ReadString('\n')
if err != nil {
return fmt.Errorf("could not read request: %s", err)
}
parts := strings.Split(s, " ")
if len(parts) < 3 {
return fmt.Errorf("malformed request supplied")
}
// Set the request Method
conf.Method = parts[0]
for {
line, err := r.ReadString('\n')
line = strings.TrimSpace(line)
if err != nil || line == "" {
break
}
p := strings.SplitN(line, ":", 2)
if len(p) != 2 {
continue
}
if strings.EqualFold(p[0], "content-length") {
continue
}
conf.Headers[strings.TrimSpace(p[0])] = strings.TrimSpace(p[1])
}
// Handle case with the full http url in path. In that case,
// ignore any host header that we encounter and use the path as request URL
if strings.HasPrefix(parts[1], "http") {
parsed, err := url.Parse(parts[1])
if err != nil {
return fmt.Errorf("could not parse request URL: %s", err)
}
conf.Url = parts[1]
conf.Headers["Host"] = parsed.Host
} else {
// Build the request URL from the request
conf.Url = parseOpts.Input.RequestProto + "://" + conf.Headers["Host"] + parts[1]
}
// Set the request body
b, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("could not read request body: %s", err)
}
conf.Data = string(b)
// Remove newline (typically added by the editor) at the end of the file
//nolint:gosimple // we specifically want to remove just a single newline, not all of them
if strings.HasSuffix(conf.Data, "\r\n") {
conf.Data = conf.Data[:len(conf.Data)-2]
} else if strings.HasSuffix(conf.Data, "\n") {
conf.Data = conf.Data[:len(conf.Data)-1]
}
return nil
}
func keywordPresent(keyword string, conf *Config) bool {
//Search for keyword from HTTP method, URL and POST data too
if strings.Contains(conf.Method, keyword) {
return true
}
if strings.Contains(conf.Url, keyword) {
return true
}
if strings.Contains(conf.Data, keyword) {
return true
}
for k, v := range conf.Headers {
if strings.Contains(k, keyword) {
return true
}
if strings.Contains(v, keyword) {
return true
}
}
return false
}
func templatePresent(template string, conf *Config) bool {
// Search for input location identifiers, these must exist in pairs
sane := false
if c := strings.Count(conf.Method, template); c > 0 {
if c%2 != 0 {
return false
}
sane = true
}
if c := strings.Count(conf.Url, template); c > 0 {
if c%2 != 0 {
return false
}
sane = true
}
if c := strings.Count(conf.Data, template); c > 0 {
if c%2 != 0 {
return false
}
sane = true
}
for k, v := range conf.Headers {
if c := strings.Count(k, template); c > 0 {
if c%2 != 0 {
return false
}
sane = true
}
if c := strings.Count(v, template); c > 0 {
if c%2 != 0 {
return false
}
sane = true
}
}
return sane
}
func ReadConfig(configFile string) (*ConfigOptions, error) {
conf := NewConfigOptions()
configData, err := os.ReadFile(configFile)
if err == nil {
err = toml.Unmarshal(configData, conf)
}
return conf, err
}
func ReadDefaultConfig() (*ConfigOptions, error) {
// Try to create configuration directory, ignore the potential error
_ = CheckOrCreateConfigDir()
conffile := filepath.Join(CONFIGDIR, "ffufrc")
if !FileExists(conffile) {
userhome, err := os.UserHomeDir()
if err == nil {
conffile = filepath.Join(userhome, ".ffufrc")
}
}
return ReadConfig(conffile)
}