main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. import (
  3. "bytes"
  4. _ "fmt"
  5. "log"
  6. "os"
  7. "os/exec"
  8. "regexp"
  9. "strings"
  10. )
  11. // go get hunt is a github secret key hunter written in go. target organizations, users, and remote/local repos
  12. // gotta be fast
  13. type Repo struct {
  14. url string
  15. name string
  16. path string
  17. }
  18. // memoization for commit1+commit2 hash
  19. var cache map[string]bool
  20. var appRoot string
  21. var regexes map[string]*regexp.Regexp
  22. func init() {
  23. appRoot, _ = os.Getwd()
  24. cache = make(map[string]bool)
  25. regexes = map[string]*regexp.Regexp{
  26. "github": regexp.MustCompile(`[g|G][i|I][t|T][h|H][u|U][b|B].+[=|:=|:|<-].*\w+.*`),
  27. "aws": regexp.MustCompile(`[a|A][w|W][s|S].+[=|:=|:|<-].*\w+.*`),
  28. "heroku": regexp.MustCompile(`[h|H][e|E][r|R][o|O][k|K][u|U].+[=|:=|:|<-].*\w+.*`),
  29. "facebook": regexp.MustCompile(`[f|F][a|A][c|C][e|E][b|B][o|O][o|O][k|K].+[=|:=|:|<-].*\w+.*`),
  30. "twitter": regexp.MustCompile(`[t|T][w|W][i|I][t|T][t|T][e|E][r|R].+[=|:=|:|<-].*\w+.*`),
  31. "reddit": regexp.MustCompile(`[r|R][e|E][d|D][d|D][i|I][t|T].+[=|:=|:|<-].*\w+.*`),
  32. "twilio": regexp.MustCompile(`[t|T][w|W][i|I][l|L][i|I][o|O].+[=|:=|:|<-].*\w+.*`),
  33. }
  34. }
  35. func main() {
  36. args := os.Args[1:]
  37. opts := parseOptions(args)
  38. start(opts)
  39. }
  40. func start(opts *Options) {
  41. if opts.Repo != "" {
  42. repoStart(opts.Repo)
  43. }
  44. }
  45. func repoStart(repo_url string) {
  46. err := exec.Command("git", "clone", repo_url).Run()
  47. if err != nil {
  48. log.Fatalf("failed to clone repo %v", err)
  49. }
  50. repo_name := strings.Split(repo_url, "/")[4]
  51. if err := os.Chdir(repo_name); err != nil {
  52. log.Fatal(err)
  53. }
  54. repo := Repo{repo_url, repo_name, ""}
  55. repo.audit()
  56. repo.cleanup()
  57. }
  58. // cleanup changes to app root and recursive rms target repo
  59. func (repo Repo) cleanup() {
  60. if err := os.Chdir(appRoot); err != nil {
  61. log.Fatalf("failed cleaning up repo %v", err)
  62. }
  63. err := exec.Command("rm", "-rf", repo.name).Run()
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. }
  68. // (Repo) audit parses git branch --all to audit remote branches
  69. func (repo Repo) audit() {
  70. var out []byte
  71. var err error
  72. var branch string
  73. var commits [][]byte
  74. out, err = exec.Command("git", "branch", "--all").Output()
  75. if err != nil {
  76. log.Fatalf("error retrieving branches %v\n", err)
  77. }
  78. // iterate through branches, git rev-list <branch>
  79. branches := bytes.Split(out, []byte("\n"))
  80. for i, branchB := range branches {
  81. if i < 2 || i == len(branches)-1 {
  82. continue
  83. }
  84. branch = string(bytes.Trim(branchB, " "))
  85. out, err = exec.Command("git", "rev-list", branch).Output()
  86. if err != nil {
  87. log.Fatalf("error retrieving commits %v\n", err)
  88. }
  89. // iterate through commits
  90. commits = bytes.Split(out, []byte("\n"))
  91. for j, commitB := range commits {
  92. if j == len(commits)-2 {
  93. break
  94. }
  95. // TODO need a memoization structure for commitB vs commits[j+1]
  96. // memoize the actual diff function
  97. diff(string(commitB), string(commits[j+1]))
  98. }
  99. }
  100. }
  101. func diff(commit1 string, commit2 string) {
  102. _, seen := cache[commit1+commit2]
  103. if seen {
  104. return
  105. }
  106. out, err := exec.Command("git", "diff", commit1, commit2).Output()
  107. if err != nil {
  108. log.Fatalf("error retrieving commits %v\n", err)
  109. }
  110. cache[commit1+commit2] = true
  111. checkRegex(string(out))
  112. }