main.go 3.2 KB

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