-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathflags.go
97 lines (84 loc) · 3.05 KB
/
flags.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
package protocol
import (
"flag"
"github.com/vulncheck-oss/go-exploit/output"
)
// All arguments used by flag packages use this structure.
type flagArgs struct {
Variable any
Default any
Description string
}
// The internalProtocols datastructure is a map based on protocols that then utilizes the interface
// versions of the flag arguments, which should make it generic enough for any of the flag package
// function types that we utilize. This also implies that the protocol variables that are set are
// utilized elsewhere in go-exploit and not freeform CreateStringFlag style flags. If you want to
// add more protocol flags this is where they go.
var internalProtocols = map[string]map[string]flagArgs{
"HTTP": {
// allow the user to use their own HTTP user-agent if they so wish
"user-agent": flagArgs{
Variable: GlobalUA,
Default: GlobalUA,
Description: "The User-Agent to use in HTTP requests",
},
},
}
// AddProtocolFlags uses the internal supported protocol variables and manually adds the protocol
// flags to the exploit. If your exploit uses multiple protocols and you would like to expose the
// protocol flags, it should be added with a `protocol.AddProtocolFlags("HTTP").
func AddProtocolFlags(protocol string) bool {
_, exists := internalProtocols[protocol]
if !exists {
output.PrintfFrameworkError("Protocol '%s' has no flags specified", protocol)
return false
}
return CreateProtocolFlags(protocol)
}
// CreateProtocolFlags takes a protocol string and matches it to the internalProtocols map and then
// uses the type assertions to make them match the flag package functions. Be sure to check that
// the internalProtocols types for Variable and Default match the flag function signatures or this
// will bail out before the flag setting silently to basic type errors.
func CreateProtocolFlags(protocol string) bool {
flags, exists := internalProtocols[protocol]
if !exists {
// No flags for this protocol enabled, this is not fatal, it just means that no
// flags have set for the protocol
return true
}
for name, args := range flags {
switch v := args.Default.(type) {
case int:
a, ok := args.Variable.(int)
if !ok {
output.PrintFrameworkError("Internal protocol variable was not the expected type (int)")
return false
}
flag.IntVar(&a, name, v, args.Description)
case uint:
a, ok := args.Variable.(uint)
if !ok {
output.PrintFrameworkError("Internal protocol variable was not the expected type (uint)")
return false
}
flag.UintVar(&a, name, v, args.Description)
case string:
a, ok := args.Variable.(string)
if !ok {
output.PrintFrameworkError("Internal protocol variable was not the expected type (string)")
return false
}
flag.StringVar(&a, name, v, args.Description)
case bool:
a, ok := args.Variable.(bool)
if !ok {
output.PrintFrameworkError("Internal protocol variable was not the expected type (bool)")
return false
}
flag.BoolVar(&a, name, v, args.Description)
default:
// Type not found, how did this happen?
}
}
return true
}