Skip to content

Commit

Permalink
Merge pull request #1537 from BishopFox/v1.6.0/cli-changes
Browse files Browse the repository at this point in the history
Fix CLI changes
  • Loading branch information
rkervella authored Jan 4, 2024
2 parents 67f92d6 + 77fea4f commit bdd8390
Show file tree
Hide file tree
Showing 255 changed files with 5,686 additions and 5,301 deletions.
9 changes: 4 additions & 5 deletions client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import (
"os"
"path"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"

"github.com/bishopfox/sliver/client/console"
"github.com/bishopfox/sliver/client/version"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

const (
Expand All @@ -37,7 +36,7 @@ const (

var sliverServerVersion = fmt.Sprintf("v%s", version.FullVersion())

// Initialize logging
// Initialize logging.
func initLogging(appDir string) *os.File {
log.SetFlags(log.LstdFlags | log.Lshortfile)
logFile, err := os.OpenFile(path.Join(appDir, logFileName), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o600)
Expand Down Expand Up @@ -88,7 +87,7 @@ var rootCmd = &cobra.Command{
Long: ``,
}

// Execute - Execute root command
// Execute - Execute root command.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
1 change: 0 additions & 1 deletion client/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"sort"

"github.com/bishopfox/sliver/client/assets"

"gopkg.in/AlecAivazis/survey.v1"
)

Expand Down
11 changes: 5 additions & 6 deletions client/cli/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package cli
import (
"fmt"

"github.com/spf13/cobra"
"google.golang.org/grpc"

"github.com/bishopfox/sliver/client/assets"
"github.com/bishopfox/sliver/client/command"
"github.com/bishopfox/sliver/client/console"
"github.com/bishopfox/sliver/client/transport"
"github.com/bishopfox/sliver/protobuf/rpcpb"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

// consoleCmd generates the console with required pre/post runners
func consoleCmd(con *console.SliverConsoleClient) *cobra.Command {
// consoleCmd generates the console with required pre/post runners.
func consoleCmd(con *console.SliverClient) *cobra.Command {
consoleCmd := &cobra.Command{
Use: "console",
Short: "Start the sliver client console",
Expand All @@ -25,7 +24,7 @@ func consoleCmd(con *console.SliverConsoleClient) *cobra.Command {
return consoleCmd
}

func consoleRunnerCmd(con *console.SliverConsoleClient, run bool) (pre, post func(cmd *cobra.Command, args []string) error) {
func consoleRunnerCmd(con *console.SliverClient, run bool) (pre, post func(cmd *cobra.Command, args []string) error) {
var ln *grpc.ClientConn

pre = func(_ *cobra.Command, _ []string) error {
Expand Down
15 changes: 7 additions & 8 deletions client/cli/implant.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package cli
import (
"errors"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/bishopfox/sliver/client/command"
"github.com/bishopfox/sliver/client/command/use"
"github.com/bishopfox/sliver/client/console"
"github.com/bishopfox/sliver/client/constants"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func implantCmd(con *console.SliverConsoleClient) *cobra.Command {
func implantCmd(con *console.SliverClient) *cobra.Command {
con.IsCLI = true

makeCommands := command.SliverCommands(con)
Expand All @@ -34,7 +33,7 @@ func implantCmd(con *console.SliverConsoleClient) *cobra.Command {
return cmd
}

func makeRunners(implantCmd *cobra.Command, con *console.SliverConsoleClient) (pre, post func(cmd *cobra.Command, args []string) error) {
func makeRunners(implantCmd *cobra.Command, con *console.SliverClient) (pre, post func(cmd *cobra.Command, args []string) error) {
startConsole, closeConsole := consoleRunnerCmd(con, false)

// The pre-run function connects to the server and sets up a "fake" console,
Expand All @@ -59,15 +58,15 @@ func makeRunners(implantCmd *cobra.Command, con *console.SliverConsoleClient) (p
return pre, closeConsole
}

func makeCompleters(cmd *cobra.Command, con *console.SliverConsoleClient) {
func makeCompleters(cmd *cobra.Command, con *console.SliverClient) {
comps := carapace.Gen(cmd)

comps.PreRun(func(cmd *cobra.Command, args []string) {
cmd.PersistentPreRunE(cmd, args)
})

// Bind completers to flags (wrap them to use the same pre-runners)
command.FlagComps(cmd, func(comp *carapace.ActionMap) {
command.BindFlagCompletions(cmd, func(comp *carapace.ActionMap) {
(*comp)["use"] = carapace.ActionCallback(func(c carapace.Context) carapace.Action {
cmd.PersistentPreRunE(cmd, c.Args)
return use.SessionIDCompleter(con)
Expand Down
3 changes: 1 addition & 2 deletions client/cli/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import (
"fmt"
"os"

"github.com/bishopfox/sliver/client/assets"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"

"github.com/bishopfox/sliver/client/assets"
)

func importCmd() *cobra.Command {
Expand Down
3 changes: 1 addition & 2 deletions client/cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ package cli
import (
"fmt"

"github.com/spf13/cobra"

"github.com/bishopfox/sliver/client/version"
"github.com/spf13/cobra"
)

var cmdVersion = &cobra.Command{
Expand Down
15 changes: 7 additions & 8 deletions client/command/alias/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ import (
"github.com/bishopfox/sliver/client/assets"
"github.com/bishopfox/sliver/client/command/settings"
"github.com/bishopfox/sliver/client/console"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

// AliasesCmd - The alias command
func AliasesCmd(cmd *cobra.Command, con *console.SliverConsoleClient, args []string) error {
// AliasesCmd - The alias command.
func AliasesCmd(cmd *cobra.Command, con *console.SliverClient, args []string) error {
if 0 < len(loadedAliases) {
PrintAliases(con)
} else {
Expand All @@ -45,8 +44,8 @@ func AliasesCmd(cmd *cobra.Command, con *console.SliverConsoleClient, args []str
return nil
}

// PrintAliases - Print a list of loaded aliases
func PrintAliases(con *console.SliverConsoleClient) {
// PrintAliases - Print a list of loaded aliases.
func PrintAliases(con *console.SliverClient) {
tw := table.NewWriter()
tw.SetStyle(settings.GetTableStyle(con))
tw.AppendHeader(table.Row{
Expand Down Expand Up @@ -88,8 +87,8 @@ func PrintAliases(con *console.SliverConsoleClient) {
con.Println(tw.Render())
}

// AliasCommandNameCompleter - Completer for installed extensions command names
func AliasCommandNameCompleter(prefix string, args []string, con *console.SliverConsoleClient) []string {
// AliasCommandNameCompleter - Completer for installed extensions command names.
func AliasCommandNameCompleter(prefix string, args []string, con *console.SliverClient) []string {
results := []string{}
for name := range loadedAliases {
if strings.HasPrefix(name, prefix) {
Expand Down Expand Up @@ -129,7 +128,7 @@ func getInstalledManifests() map[string]*AliasManifest {
return installedManifests
}

// AliasCommandNameCompleter - Completer for installed extensions command names
// AliasCommandNameCompleter - Completer for installed extensions command names.
func AliasCompleter() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
results := []string{}
Expand Down
61 changes: 61 additions & 0 deletions client/command/alias/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package alias

import (
"github.com/bishopfox/sliver/client/command/flags"
"github.com/bishopfox/sliver/client/command/help"
"github.com/bishopfox/sliver/client/console"
consts "github.com/bishopfox/sliver/client/constants"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

// Commands returns the `alias` command and its child commands.
func Commands(con *console.SliverClient) []*cobra.Command {
aliasCmd := &cobra.Command{
Use: consts.AliasesStr,
Short: "List current aliases",
Long: help.GetHelpFor([]string{consts.AliasesStr}),
Run: func(cmd *cobra.Command, args []string) {
AliasesCmd(cmd, con, args)
},
GroupID: consts.GenericHelpGroup,
}

aliasLoadCmd := &cobra.Command{
Use: consts.LoadStr + " [ALIAS]",
Short: "Load a command alias",
Long: help.GetHelpFor([]string{consts.AliasesStr, consts.LoadStr}),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
AliasesLoadCmd(cmd, con, args)
},
}
aliasCmd.AddCommand(aliasLoadCmd)
flags.NewCompletions(aliasLoadCmd).PositionalCompletion(carapace.ActionDirectories().Tag("alias directory").Usage("path to the alias directory"))

aliasInstallCmd := &cobra.Command{
Use: consts.InstallStr + " [ALIAS]",
Short: "Install a command alias",
Long: help.GetHelpFor([]string{consts.AliasesStr, consts.InstallStr}),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
AliasesInstallCmd(cmd, con, args)
},
}
aliasCmd.AddCommand(aliasInstallCmd)
flags.NewCompletions(aliasInstallCmd).PositionalCompletion(carapace.ActionFiles().Tag("alias file"))

aliasRemove := &cobra.Command{
Use: consts.RmStr + " [ALIAS]",
Short: "Remove an alias",
Long: help.GetHelpFor([]string{consts.RmStr}),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
AliasesRemoveCmd(cmd, con, args)
},
}
flags.NewCompletions(aliasRemove).PositionalCompletion(AliasCompleter())
aliasCmd.AddCommand(aliasRemove)

return []*cobra.Command{aliasCmd}
}
17 changes: 8 additions & 9 deletions client/command/alias/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ import (
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"

"github.com/bishopfox/sliver/client/assets"
"github.com/bishopfox/sliver/client/console"
"github.com/bishopfox/sliver/util"
"github.com/spf13/cobra"
)

// AliasesInstallCmd - Install an alias
func AliasesInstallCmd(cmd *cobra.Command, con *console.SliverConsoleClient, args []string) {
// AliasesInstallCmd - Install an alias.
func AliasesInstallCmd(cmd *cobra.Command, con *console.SliverClient, args []string) {
aliasLocalPath := args[0]
fi, err := os.Stat(aliasLocalPath)
if os.IsNotExist(err) {
Expand All @@ -47,8 +46,8 @@ func AliasesInstallCmd(cmd *cobra.Command, con *console.SliverConsoleClient, arg
}
}

// Install an extension from a directory
func installFromDir(aliasLocalPath string, con *console.SliverConsoleClient) {
// Install an extension from a directory.
func installFromDir(aliasLocalPath string, con *console.SliverClient) {
manifestData, err := os.ReadFile(filepath.Join(aliasLocalPath, ManifestFileName))
if err != nil {
con.PrintErrorf("Error reading %s: %s", ManifestFileName, err)
Expand Down Expand Up @@ -100,8 +99,8 @@ func installFromDir(aliasLocalPath string, con *console.SliverConsoleClient) {
con.Printf("done!\n")
}

// Install an extension from a .tar.gz file
func InstallFromFile(aliasGzFilePath string, autoOverwrite bool, con *console.SliverConsoleClient) *string {
// Install an extension from a .tar.gz file.
func InstallFromFile(aliasGzFilePath string, autoOverwrite bool, con *console.SliverClient) *string {
manifestData, err := util.ReadFileFromTarGz(aliasGzFilePath, fmt.Sprintf("./%s", ManifestFileName))
if err != nil {
con.PrintErrorf("Failed to read %s from '%s': %s\n", ManifestFileName, aliasGzFilePath, err)
Expand Down Expand Up @@ -152,7 +151,7 @@ func InstallFromFile(aliasGzFilePath string, autoOverwrite bool, con *console.Sl
return &installPath
}

func installArtifact(aliasGzFilePath string, installPath, artifactPath string, con *console.SliverConsoleClient) error {
func installArtifact(aliasGzFilePath string, installPath, artifactPath string, con *console.SliverClient) error {
data, err := util.ReadFileFromTarGz(aliasGzFilePath, fmt.Sprintf("./%s", strings.TrimPrefix(artifactPath, string(os.PathSeparator))))
if err != nil {
return err
Expand Down
Loading

0 comments on commit bdd8390

Please sign in to comment.