-
Notifications
You must be signed in to change notification settings - Fork 41
Add protocol based flag handling #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "flag" | ||
|
|
||
| "github.com/vulncheck-oss/go-exploit/output" | ||
| "github.com/vulncheck-oss/go-exploit/protocol" | ||
| ) | ||
|
|
||
| // 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: protocol.GlobalUA, | ||
| Default: protocol.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 `cli.AddProtocolFlags("HTTP"). | ||
| func AddProtocolFlags(protocol string) bool { | ||
| _, exists := internalProtocols[protocol] | ||
| if !exists { | ||
| output.PrintfFrameworkError("Protocol '%s' has no flags specified", protocol) | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| return protocolFlags(protocol) | ||
| } | ||
|
|
||
| // protocolFlags 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 protocolFlags(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 | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.