Skip to content
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

Fallback to Direct Connection when HTTP Proxy Connection Fails #1295

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions client/command/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func ParseWGc2(args string) ([]*clientpb.ImplantC2, error) {
func hasValidC2AdvancedOptions(options url.Values) (bool, error) {
for key, value := range options {
if len(value) > 1 {
return false, fmt.Errorf("too many values specified for advanced option %s. Only one value for %s can be specified.", key, key)
return false, fmt.Errorf("too many values specified for advanced option %s. Only one value for %s can be specified", key, key)
}
testValue := value[0]

Expand Down Expand Up @@ -588,13 +588,35 @@ func hasValidC2AdvancedOptions(options url.Values) (bool, error) {
return true, nil
}

func checkOptionValue(c2Options url.Values, option string, value string) bool {
if !c2Options.Has(option) {
return false
} else {
optionValue := c2Options.Get(option)
return strings.ToLower(optionValue) == value
}
}

func uriWithoutProxyOptions(uri *url.URL) {
options := uri.Query()
// If any of the options do not exist, there is no error
options.Del("proxy")
options.Del("proxy-username")
options.Del("proxy-password")
options.Del("ask-proxy-creds")
options.Del("fallback")

uri.RawQuery = options.Encode()
}

// ParseHTTPc2 - Parse HTTP connection string arg
func ParseHTTPc2(args string) ([]*clientpb.ImplantC2, error) {
c2s := []*clientpb.ImplantC2{}
if args == "" {
return c2s, nil
}
for index, arg := range strings.Split(args, ",") {
allArguments := strings.Split(args, ",")
for index, arg := range allArguments {
var uri *url.URL
var err error
if cmp := strings.ToLower(arg); strings.HasPrefix(cmp, "http://") || strings.HasPrefix(cmp, "https://") {
Expand All @@ -619,6 +641,16 @@ func ParseHTTPc2(args string) ([]*clientpb.ImplantC2, error) {
Priority: uint32(index),
URL: uri.String(),
})
/* If a proxy is defined and the operator wants to fallback to connecting directly, add
a C2 that connects directly without the proxy settings.
*/
if checkOptionValue(uri.Query(), "fallback", "true") && uri.Query().Has("proxy") && !checkOptionValue(uri.Query(), "driver", "wininet") {
uriWithoutProxyOptions(uri)
c2s = append(c2s, &clientpb.ImplantC2{
Priority: uint32(index + len(allArguments)),
URL: uri.String(),
})
}
}
return c2s, nil
}
Expand Down
Loading