Skip to content

Commit

Permalink
Merge pull request #1857 from BishopFox/v1.6.0/download-refactor
Browse files Browse the repository at this point in the history
Simplifying Implant Download Code
  • Loading branch information
moloch-- authored Jan 20, 2025
2 parents 53c61cb + 5935217 commit 67c6140
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 122 deletions.
10 changes: 7 additions & 3 deletions client/command/filesystem/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/hex"
"fmt"
"os"
"strings"

"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
Expand Down Expand Up @@ -72,16 +73,16 @@ func CatCmd(cmd *cobra.Command, con *console.SliverClient, args []string) {
con.PrintErrorf("Failed to decode response %s\n", err)
return
}
PrintCat(download, cmd, con)
PrintCat(filePath, download, cmd, con)
})
con.PrintAsyncResponse(download.Response)
} else {
PrintCat(download, cmd, con)
PrintCat(filePath, download, cmd, con)
}
}

// PrintCat - Print the download to stdout.
func PrintCat(download *sliverpb.Download, cmd *cobra.Command, con *console.SliverClient) {
func PrintCat(originalFileName string, download *sliverpb.Download, cmd *cobra.Command, con *console.SliverClient) {
var (
lootDownload bool = true
err error
Expand All @@ -108,6 +109,9 @@ func PrintCat(download *sliverpb.Download, cmd *cobra.Command, con *console.Sliv
con.Printf("\n")
}
}
if !strings.Contains(download.Path, originalFileName) {
con.PrintInfof("Supplied pattern %s matched file %s\n\n", originalFileName, download.Path)
}
if color, _ := cmd.Flags().GetBool("colorize-output"); color {
if err = colorize(download); err != nil {
con.Println(string(download.Data))
Expand Down
4 changes: 2 additions & 2 deletions client/command/filesystem/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func Commands(con *console.SliverClient) []*cobra.Command {
f.StringP("file-type", "F", "", "force a specific file type (binary/text) if looting (optional)")
f.Int64P("timeout", "t", flags.DefaultTimeout, "grpc timeout in seconds")
f.Int64P("bytes", "b", 0, "Grab the first number of bytes from the file")
f.Int64P("lines", "l", 0, "Grab the first number of lines from the file")
f.Int64P("lines", "l", 10, "Grab the first number of lines from the file")
})
carapace.Gen(headCmd).PositionalCompletion(carapace.ActionValues().Usage("path to the file to print"))

Expand All @@ -322,7 +322,7 @@ func Commands(con *console.SliverClient) []*cobra.Command {
f.StringP("file-type", "F", "", "force a specific file type (binary/text) if looting (optional)")
f.Int64P("timeout", "t", flags.DefaultTimeout, "grpc timeout in seconds")
f.Int64P("bytes", "b", 0, "Grab the last number of bytes from the file")
f.Int64P("lines", "l", 0, "Grab the last number of lines from the file")
f.Int64P("lines", "l", 10, "Grab the last number of lines from the file")
})
carapace.Gen(tailCmd).PositionalCompletion(carapace.ActionValues().Usage("path to the file to print"))

Expand Down
14 changes: 10 additions & 4 deletions client/command/filesystem/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ func prettifyDownloadName(path string) string {
filteredString = multipleUnderscoreRegex.ReplaceAllString(filteredString, "_")

// If there is an underscore at the front of the filename, strip that off
if strings.HasPrefix(filteredString, "_") {
filteredString = filteredString[1:]
}
filteredString, _ = strings.CutPrefix(filteredString, "_")

return filteredString
}
Expand All @@ -118,7 +116,15 @@ func HandleDownloadResponse(download *sliverpb.Download, cmd *cobra.Command, arg
}
}

remotePath := args[0]
// Use download.Path because a glob matching a single file on the remote will not have the
// correct file name - the filename will contain the globs if we use the path from the user
// On non-Windows systems, filepath.Base will not see backslashes, so we will replace them
// on systems that do not use backslashes as path separators
remotePath := download.Path
if strings.Contains(download.Path, "\\") && string(os.PathSeparator) != "\\" {
remotePath = strings.ReplaceAll(download.Path, "\\", "/")
}

var localPath string
if len(args) == 1 {
localPath = "."
Expand Down
9 changes: 3 additions & 6 deletions client/command/filesystem/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func HeadCmd(cmd *cobra.Command, con *console.SliverClient, args []string, head
} else {
operationName = "bytes"
}
} else if cmd.Flags().Changed("lines") {
} else {
fetchBytes = false
fetchSize, _ = cmd.Flags().GetInt64("lines")
if fetchSize < 0 {
Expand All @@ -76,9 +76,6 @@ func HeadCmd(cmd *cobra.Command, con *console.SliverClient, args []string, head
} else {
operationName = "lines"
}
} else {
con.PrintErrorf("A number of bytes or a number of lines must be specified.")
return
}

ctrl := make(chan bool)
Expand Down Expand Up @@ -118,10 +115,10 @@ func HeadCmd(cmd *cobra.Command, con *console.SliverClient, args []string, head
con.PrintErrorf("Failed to decode response %s\n", err)
return
}
PrintCat(download, cmd, con)
PrintCat(filePath, download, cmd, con)
})
con.PrintAsyncResponse(download.Response)
} else {
PrintCat(download, cmd, con)
PrintCat(filePath, download, cmd, con)
}
}
Loading

0 comments on commit 67c6140

Please sign in to comment.