diff options
| author | Bryan McNulty <bryanmcnulty@protonmail.com> | 2025-03-12 10:36:38 -0500 | 
|---|---|---|
| committer | Bryan McNulty <bryanmcnulty@protonmail.com> | 2025-03-12 10:36:38 -0500 | 
| commit | c29e70df5434a82ee43fa59826c67037d07d7b3a (patch) | |
| tree | adea65dcb7c7f2d3c461e0b98de444519c03bb42 /cmd/root.go | |
| parent | 8a2631d9348c81a724e30b0e2913f3e7bb1bb56f (diff) | |
| download | goexec-c29e70df5434a82ee43fa59826c67037d07d7b3a.tar.gz goexec-c29e70df5434a82ee43fa59826c67037d07d7b3a.zip | |
+Proxy support +Dockerfile
Diffstat (limited to 'cmd/root.go')
| -rw-r--r-- | cmd/root.go | 181 | 
1 files changed, 101 insertions, 80 deletions
| diff --git a/cmd/root.go b/cmd/root.go index f596c75..b44c50e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,104 +1,125 @@  package cmd  import ( -	"context" -	"fmt" -	"github.com/RedTeamPentesting/adauth" -	"github.com/rs/zerolog" -	"github.com/spf13/cobra" -	"os" -	"regexp" -	"strings" +  "context" +  "fmt" +  "github.com/RedTeamPentesting/adauth" +  "github.com/rs/zerolog" +  "github.com/spf13/cobra" +  "net/url" +  "os" +  "regexp" +  "strings"  )  var ( -	//logFile string -	log      zerolog.Logger -	ctx      context.Context -	authOpts *adauth.Options +  //logFile string +  log      zerolog.Logger +  ctx      context.Context +  authOpts *adauth.Options -	hostname string +  hostname string +  proxyStr string +  proxyUrl *url.URL -	// Root flags -	debug bool +  // Root flags +  debug bool -	// Generic flags -	command          string -	executable       string -	executablePath   string -	executableArgs   string -	workingDirectory string -	windowState      string +  // Generic flags +  command          string +  executable       string +  executablePath   string +  executableArgs   string +  workingDirectory string +  windowState      string -	rootCmd = &cobra.Command{ -		Use: "goexec", -		PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { -			// For modules that require a full executable path -			if executablePath != "" && !regexp.MustCompile(`^([a-zA-Z]:)?\\`).MatchString(executablePath) { -				return fmt.Errorf("executable path (-e) must be an absolute Windows path, i.e. C:\\Windows\\System32\\cmd.exe") -			} -			if command != "" { -				p := strings.SplitN(command, " ", 2) -				executable = p[0] -				if len(p) > 1 { -					executableArgs = p[1] -				} -			} -			log = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).Level(zerolog.InfoLevel).With().Timestamp().Logger() -			if debug { -				log = log.Level(zerolog.DebugLevel) -			} -			return -		}, -	} +  rootCmd = &cobra.Command{ +    Use: "goexec", +    PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { +      // For modules that require a full executable path +      if executablePath != "" && !regexp.MustCompile(`^([a-zA-Z]:)?\\`).MatchString(executablePath) { +        return fmt.Errorf("executable path (-e) must be an absolute Windows path, i.e. C:\\Windows\\System32\\cmd.exe") +      } +      if command != "" { +        p := strings.SplitN(command, " ", 2) +        executable = p[0] +        if len(p) > 1 { +          executableArgs = p[1] +        } +      } +      log = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).Level(zerolog.InfoLevel).With().Timestamp().Logger() +      if debug { +        log = log.Level(zerolog.DebugLevel) +      } +      return +    }, +  }  ) +func needs(reqs ...func(*cobra.Command, []string) error) (fn func(*cobra.Command, []string) error) { +  return func(cmd *cobra.Command, args []string) (err error) { +    for _, req := range reqs { +      if err = req(cmd, args); err != nil { +        return +      } +    } +    return +  } +} +  func needsTarget(proto string) func(cmd *cobra.Command, args []string) error { -	return func(cmd *cobra.Command, args []string) (err error) { -		if len(args) != 1 { -			return fmt.Errorf("command require exactly one positional argument: [target]") -		} -		if creds, target, err = authOpts.WithTarget(ctx, proto, args[0]); err != nil { -			return fmt.Errorf("failed to parse target: %w", err) -		} -		if creds == nil { -			return fmt.Errorf("no credentials supplied") -		} -		if target == nil { -			return fmt.Errorf("no target supplied") -		} -		if hostname, err = target.Hostname(ctx); err != nil { -			log.Debug().Err(err).Msg("Could not get target hostname") -		} -		return -	} + +  return func(cmd *cobra.Command, args []string) (err error) { +    if proxyStr != "" { +      if proxyUrl, err = url.Parse(proxyStr); err != nil { +        return fmt.Errorf("failed to parse proxy URL %q: %w", proxyStr, err) +      } +    } +    if len(args) != 1 { +      return fmt.Errorf("command require exactly one positional argument: [target]") +    } +    if creds, target, err = authOpts.WithTarget(ctx, proto, args[0]); err != nil { +      return fmt.Errorf("failed to parse target: %w", err) +    } +    if creds == nil { +      return fmt.Errorf("no credentials supplied") +    } +    if target == nil { +      return fmt.Errorf("no target supplied") +    } +    if hostname, err = target.Hostname(ctx); err != nil { +      log.Debug().Err(err).Msg("Could not get target hostname") +    } +    return +  }  }  func init() { -	ctx = context.Background() +  ctx = context.Background() -	cobra.EnableCommandSorting = false +  cobra.EnableCommandSorting = false -	rootCmd.InitDefaultVersionFlag() -	rootCmd.InitDefaultHelpCmd() -	rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Enable debug logging") +  rootCmd.InitDefaultVersionFlag() +  rootCmd.InitDefaultHelpCmd() +  rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Enable debug logging") +  rootCmd.PersistentFlags().StringVarP(&proxyStr, "proxy", "x", "", "Proxy URL") -	authOpts = &adauth.Options{Debug: log.Debug().Msgf} -	authOpts.RegisterFlags(rootCmd.PersistentFlags()) +  authOpts = &adauth.Options{Debug: log.Debug().Msgf} +  authOpts.RegisterFlags(rootCmd.PersistentFlags()) -	scmrCmdInit() -	rootCmd.AddCommand(scmrCmd) -	tschCmdInit() -	rootCmd.AddCommand(tschCmd) -	wmiCmdInit() -	rootCmd.AddCommand(wmiCmd) -	dcomCmdInit() -	rootCmd.AddCommand(dcomCmd) +  scmrCmdInit() +  rootCmd.AddCommand(scmrCmd) +  tschCmdInit() +  rootCmd.AddCommand(tschCmd) +  wmiCmdInit() +  rootCmd.AddCommand(wmiCmd) +  dcomCmdInit() +  rootCmd.AddCommand(dcomCmd)  }  func Execute() { -	if err := rootCmd.Execute(); err != nil { -		fmt.Println(err) -		os.Exit(1) -	} +  if err := rootCmd.Execute(); err != nil { +    fmt.Println(err) +    os.Exit(1) +  }  } |