-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathargs.go
163 lines (143 loc) · 4.11 KB
/
args.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
package main
import (
"flag"
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/glebarez/padre/pkg/color"
"github.com/glebarez/padre/pkg/encoder"
"github.com/glebarez/padre/pkg/util"
)
func init() {
// a custom usage message
flag.Usage = func() {
fmt.Fprint(stderr, usage)
}
}
const (
defaultConcurrency = 30
defaultTerminalWidth = 80
maxConcurrency = 256
)
// Args - CLI flags
type Args struct {
BlockLen *int
Parallel *int
TargetURL *string
Encoder encoder.Encoder
PaddingErrorPattern *string
ProxyURL *url.URL
POSTdata *string
ContentType *string
Cookies []*http.Cookie
EncryptMode *bool
Input *string
}
func parseArgs() (*Args, *argErrors) {
// container for storing errors and warnings
argErrs := newArgErrors()
args := &Args{}
// simple flags that go in as-is
args.PaddingErrorPattern = flag.String("err", "", "")
args.BlockLen = flag.Int("b", 0, "")
args.Parallel = flag.Int("p", defaultConcurrency, "")
args.POSTdata = flag.String("post", "", "")
args.ContentType = flag.String("ct", "", "")
args.EncryptMode = flag.Bool("enc", false, "")
args.TargetURL = flag.String("u", "", "")
// flags that need additional processing
proxyURL := flag.String("proxy", "", "")
encoding := flag.String("e", "b64", "")
replacements := flag.String("r", "", "")
cookies := flag.String("cookie", "", "")
// parse flags
flag.Parse()
// general check on URL, POSTdata or Cookies for having the $ placeholder
match1, err := regexp.MatchString(`\$`, *args.TargetURL)
if err != nil {
argErrs.flagError("-u", err)
}
match2, err := regexp.MatchString(`\$`, *args.POSTdata)
if err != nil {
argErrs.flagError("-post", err)
}
match3, err := regexp.MatchString(`\$`, *cookies)
if err != nil {
argErrs.flagError("-cookie", err)
}
if !(match1 || match2 || match3) {
argErrs.flagErrorf("-u, -post, -cookie", "Either URL, POST data or Cookie must contain the $ placeholder")
}
// Target URL
if *args.TargetURL == "" {
argErrs.flagErrorf("-u", "Must be specified")
} else {
_, err = url.Parse(*args.TargetURL)
if err != nil {
argErrs.flagError("-u", fmt.Errorf("failed to parse URL: %w", err))
}
}
// Proxy URL
if *proxyURL != "" {
args.ProxyURL, err = url.Parse(*proxyURL)
if err != nil {
argErrs.flagError("-proxy", fmt.Errorf("failed to parse URL: %w", err))
}
}
// Encoder (With replacements)
if len(*replacements)%2 == 1 {
argErrs.flagErrorf("-r", "String must be of even length (0,2,4, etc.)")
} else {
switch strings.ToLower(*encoding) {
case "b64":
args.Encoder = encoder.NewB64encoder(*replacements)
case "lhex":
args.Encoder = encoder.NewLHEXencoder(*replacements)
default:
argErrs.flagErrorf("-e", "Unsupported encoding specified")
}
}
// block length
switch *args.BlockLen {
case 0: // = not set
case 8:
case 16:
case 32:
default:
argErrs.flagErrorf("-b", "Unsupported value passed. Omit, or specify one of: 8, 16, 32")
}
// Cookies
if *cookies != "" {
args.Cookies, err = util.ParseCookies(*cookies)
if err != nil {
argErrs.flagError("-cookie", fmt.Errorf("failed to parse cookies: %s", err))
}
}
// Concurrency
if *args.Parallel < 1 {
argErrs.flagWarningf("-p", "Cannot be less than 1, value corrected to default value (%d)", defaultConcurrency)
*args.Parallel = defaultConcurrency
} else if *args.Parallel > maxConcurrency {
argErrs.flagWarningf("-p", "Value reduced to maximum allowed value (%d)", maxConcurrency)
*args.Parallel = maxConcurrency
}
// content-type auto-detection
if *args.POSTdata != "" && *args.ContentType == "" {
*args.ContentType = util.DetectContentType(*args.POSTdata)
argErrs.warningf("HTTP Content-Type detected automatically as %s", color.Yellow(*args.ContentType))
}
// decide on input source
switch flag.NArg() {
case 0:
// no input passed, STDIN will be used
case 1:
// input is passed
args.Input = &flag.Args()[0]
default:
// too many positional arguments
argErrs.flagErrorf("[INPUT]", "Specify exactly one input string, or pipe into STDIN")
}
return args, argErrs
}