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

added redis search function, allowing you to search for a key without… #7

Merged
merged 2 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.o
main
hunttools
.DS_Store
71 changes: 71 additions & 0 deletions cmd/redis_search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
b64 "encoding/base64"
//"encoding/json"
"fmt"
"strings"
//"io/ioutil"
//"os"

"github.com/spf13/cobra"
)

func init() {
redisSearchCmd.Flags().StringVarP(&UploadInfile, "infile", "I", "", "Infile generated by ht redis dump -j")
redisSearchCmd.Flags().StringVarP(&SearchString, "string", "s", "", "Key value to search for in the redis dump")
redisSearchCmd.Flags().BoolVarP(&Decode, "decode", "D", false, "Decode the base64 before writing to terminal (default no)")
redisSearchCmd.Flags().BoolVarP(&Fuzzy, "fuzzy", "F", false, "Fuzzy (contains) search for strings")
redisSearchCmd.MarkFlagRequired("infile")
redisSearchCmd.MarkFlagRequired("string")

// NOTE: if you see a command duplicated
// in the help system, check the `AddCommand`
// calls in all of your cmds...
redisRootCmd.AddCommand(redisSearchCmd)
}

var UploadInfile string
var SearchString string
var Decode bool = false
var Fuzzy = false

var redisSearchCmd = &cobra.Command{
Use: "search",
Short: "Searches the results of ht redis dump",
Long: `Searches the results of ht redis dump`,
RunE: func(cmd *cobra.Command, args []string) error {
keycount := 0

// Load JSON Infile
dataToUpload, err := loadRedisData(UploadInfile)
if err != nil {
fmt.Printf("Unable to load file: %s\n", UploadJsonInfile)
return err
}
// For each key, add to database
for _, redisData := range dataToUpload {
var key = fmt.Sprintf("%s:%s", redisData.Database, redisData.Key)

keycount++
// Need to base64 decode the value
value := fmt.Sprintf("%v", redisData.Value)
if key == SearchString || (Fuzzy && strings.Contains(key, SearchString)) {
if VerboseOutput {
fmt.Printf("key: %v\n", key)
}

if Decode {
dumpValue, _ := b64.StdEncoding.DecodeString(value)
fmt.Printf("%v\n", dumpValue)
} else {
fmt.Printf("%v\n", value)
}
}

}

return nil

},
}