From e1a58935ea88621adee8caac3567f1a3e5ee455d Mon Sep 17 00:00:00 2001 From: zricethezav Date: Sat, 27 Jan 2018 13:20:41 -0500 Subject: [PATCH] [init] diffing commits for all branches, work on concurrency --- main.go | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++ options.go | 64 ++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 main.go create mode 100644 options.go diff --git a/main.go b/main.go new file mode 100644 index 00000000..bd9b6ce7 --- /dev/null +++ b/main.go @@ -0,0 +1,111 @@ +package main + +import ( + "bytes" + _ "fmt" + "log" + "os" + "os/exec" + "strings" +) + +// go get hunt is a github secret key hunter written in go. target organizations, users, and remote/local repos +// gotta be fast + +type Repo struct { + url string + name string + path string + branches *Branch +} + +type Branch struct { + name string +} + +var appRoot string + +func init() { + appRoot, _ = os.Getwd() +} + +func main() { + args := os.Args[1:] + opts := parseOptions(args) + start(opts) +} + +func start(opts *Options) { + if opts.Repo != "" { + repoStart(opts.Repo) + } +} + +func repoStart(repo_url string) { + err := exec.Command("git", "clone", repo_url).Run() + if err != nil { + log.Fatalf("failed to clone repo %v", err) + } + repo_name := strings.Split(repo_url, "/")[4] + if err := os.Chdir(repo_name); err != nil { + log.Fatal(err) + } + + repo := Repo{repo_url, repo_name, "", nil} + repo.audit() + repo.cleanup() +} + +// cleanup changes to app root and recursive rms target repo +func (repo Repo) cleanup() { + if err := os.Chdir(appRoot); err != nil { + log.Fatalf("failed cleaning up repo %v", err) + } + err := exec.Command("rm", "-rf", repo.name).Run() + if err != nil { + log.Fatal(err) + } +} + +// (Repo) audit parses git branch --all to audit remote branches +func (repo Repo) audit() { + var out []byte + var err error + var branch string + var commits [][]byte + + out, err = exec.Command("git", "branch", "--all").Output() + if err != nil { + log.Fatalf("error retrieving branches %v\n", err) + } + + // iterate through branches, git rev-list + branches := bytes.Split(out, []byte("\n")) + for i, branchB := range branches { + if i < 2 || i == len(branches)-1 { + continue + } + branch = string(bytes.Trim(branchB, " ")) + out, err = exec.Command("git", "rev-list", branch).Output() + if err != nil { + log.Fatalf("error retrieving commits %v\n", err) + } + // iterate through commits + commits = bytes.Split(out, []byte("\n")) + for j, commitB := range commits { + if j == len(commits)-2 { + break + } + diff(string(commitB), string(commits[j+1])) + } + } +} + +func diff(commit1 string, commit2 string) { + // fmt.Println(commit1, commit2) + _, err := exec.Command("git", "diff", commit1, commit2).Output() + if err != nil { + log.Fatalf("error retrieving commits %v\n", err) + } + //fmt.Printf("%s\n", out) +} diff --git a/options.go b/options.go new file mode 100644 index 00000000..0567e650 --- /dev/null +++ b/options.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" + "os" +) + +// TODO regex on type.. user/organization can be treated as the same: +// hittps://github.com/ +// hittps://github.com//repo +const usage = `usage: gogethunt [options] + +Options: + -u --user Target user + -r --repo Target repo + -o --org Target organization + -h --help Display this message + -e --entropy Enable entropy detection + -r --regex Enable regex detection +` + +type Options struct { + User string + Repo string + Org string +} + +func help() { + os.Stderr.WriteString(usage) + os.Exit(1) +} + +func optionsNextString(args []string, i *int) string { + if len(args) > *i+1 { + *i++ + } else { + help() + } + return args[*i] +} + +func parseOptions(args []string) *Options { + opts := &Options{} + for i := 0; i < len(args); i++ { + arg := args[i] + switch arg { + case "-o", "--org": + opts.Org = optionsNextString(args, &i) + case "-r", "--repo": + opts.Repo = optionsNextString(args, &i) + case "-u", "--user": + opts.User = optionsNextString(args, &i) + case "-h", "--help": + help() + return nil + default: + fmt.Printf("Uknown option %s\n\n", arg) + help() + return nil + } + } + + return opts +}