mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 21:44:23 +01:00
better naming
This commit is contained in:
1 parent
a430e46a8e
commit
15d3d8e7de
4 files changed
+60
-112
No files matched your search
@@ -1,51 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/nbutton23/zxcvbn-go"
|
"github.com/nbutton23/zxcvbn-go"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// checkDiff operates on a single diff between to chronological commits
|
|
||||||
func checkDiff(commit1 string, commit2 string, repoName string) []string {
|
|
||||||
// var leakPrs bool
|
|
||||||
// var leaks []string
|
|
||||||
// _, seen := cache[commit1+commit2]
|
|
||||||
// if seen {
|
|
||||||
// fmt.Println("WE HAVE SEEN THIS")
|
|
||||||
// return []string{}
|
|
||||||
// }
|
|
||||||
|
|
||||||
if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := exec.Command("git", "diff", commit1, commit2)
|
|
||||||
_, err := cmd.Output()
|
|
||||||
// fmt.Println(string(out))
|
|
||||||
if err != nil {
|
|
||||||
return []string{}
|
|
||||||
}
|
|
||||||
return []string{}
|
|
||||||
|
|
||||||
// cache[commit1+commit2] = true
|
|
||||||
// lines := checkRegex(string(out))
|
|
||||||
// if len(lines) == 0 {
|
|
||||||
// return []string{}
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// for _, line := range lines {
|
|
||||||
// leakPrs = checkEntropy(line)
|
|
||||||
// if leakPrs {
|
|
||||||
// leaks = append(leaks, line)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return leaks
|
|
||||||
}
|
|
||||||
|
|
||||||
// check each line of a diff and see if there are any potential secrets
|
// check each line of a diff and see if there are any potential secrets
|
||||||
// [1] https://people.eecs.berkeley.edu/~rohanpadhye/files/key_leaks-msr15.pdf
|
// [1] https://people.eecs.berkeley.edu/~rohanpadhye/files/key_leaks-msr15.pdf
|
||||||
func checkRegex(diff string) []string {
|
func checkRegex(diff string) []string {
|
||||||
@@ -54,7 +13,7 @@ func checkRegex(diff string) []string {
|
|||||||
lines := strings.Split(diff, "\n")
|
lines := strings.Split(diff, "\n")
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
// doubtful a leak would be on a line > 120 characters
|
// doubtful a leak would be on a line > 120 characters
|
||||||
if len(line) == 0 || len(line) > 80 {
|
if len(line) == 0 || len(line) > 120 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, re := range regexes {
|
for _, re := range regexes {
|
||||||
@@ -85,7 +44,6 @@ func checkEntropy(target string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// entropy := shannonEntropy(target)
|
|
||||||
entropy := zxcvbn.PasswordStrength(target, nil).Entropy
|
entropy := zxcvbn.PasswordStrength(target, nil).Entropy
|
||||||
|
|
||||||
// tune this/make option
|
// tune this/make option
|
||||||
|
|||||||
+32
-39
@@ -8,8 +8,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ReportElem struct {
|
type ReportElem struct {
|
||||||
@@ -19,13 +21,7 @@ type ReportElem struct {
|
|||||||
CommitB string `json:"commitB"`
|
CommitB string `json:"commitB"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Repo struct {
|
type GitLeak struct {
|
||||||
url string
|
|
||||||
name string
|
|
||||||
path string
|
|
||||||
}
|
|
||||||
|
|
||||||
type MemoMessage struct {
|
|
||||||
hash string
|
hash string
|
||||||
leaks []string
|
leaks []string
|
||||||
commitA string
|
commitA string
|
||||||
@@ -33,10 +29,10 @@ type MemoMessage struct {
|
|||||||
branch string
|
branch string
|
||||||
}
|
}
|
||||||
|
|
||||||
var lock = sync.RWMutex{}
|
func start(opts *Options, repoUrl string) {
|
||||||
var cmdLock = sync.Mutex{}
|
c := make(chan os.Signal, 2)
|
||||||
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
func repoStart(repoUrl string) {
|
|
||||||
err := exec.Command("git", "clone", repoUrl).Run()
|
err := exec.Command("git", "clone", repoUrl).Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to clone repo %v", err)
|
log.Fatalf("failed to clone repo %v", err)
|
||||||
@@ -45,39 +41,45 @@ func repoStart(repoUrl string) {
|
|||||||
if err := os.Chdir(repoName); err != nil {
|
if err := os.Chdir(repoName); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
go func() {
|
||||||
|
<-c
|
||||||
|
cleanup(repoName)
|
||||||
|
os.Exit(1)
|
||||||
|
}()
|
||||||
|
|
||||||
repo := Repo{repoUrl, repoName, ""}
|
report := getLeaks(repoName)
|
||||||
report := repo.audit()
|
cleanup(repoName)
|
||||||
repo.cleanup()
|
|
||||||
|
|
||||||
reportJson, _ := json.MarshalIndent(report, "", "\t")
|
reportJson, _ := json.MarshalIndent(report, "", "\t")
|
||||||
err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repo.name), reportJson, 0644)
|
err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJson, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanup changes to app root and recursive rms target repo
|
// cleanup changes to app root and recursive rms target repo
|
||||||
func (repo Repo) cleanup() {
|
func cleanup(repoName string) {
|
||||||
if err := os.Chdir(appRoot); err != nil {
|
if err := os.Chdir(appRoot); err != nil {
|
||||||
log.Fatalf("failed cleaning up repo. Does the repo exist? %v", err)
|
log.Fatalf("failed cleaning up repo. Does the repo exist? %v", err)
|
||||||
}
|
}
|
||||||
err := exec.Command("rm", "-rf", repo.name).Run()
|
err := exec.Command("rm", "-rf", repoName).Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// audit parses git branch --all
|
// audit parses git branch --all
|
||||||
func (repo Repo) audit() []ReportElem {
|
func getLeaks(repoName string) []ReportElem {
|
||||||
var (
|
var (
|
||||||
out []byte
|
out []byte
|
||||||
err error
|
err error
|
||||||
branch string
|
branch string
|
||||||
commits [][]byte
|
commits [][]byte
|
||||||
// leaks []string
|
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
commitA string
|
commitA string
|
||||||
commitB string
|
commitB string
|
||||||
concurrent = 10
|
concurrent = 100
|
||||||
semaphoreChan = make(chan struct{}, concurrent)
|
semaphoreChan = make(chan struct{}, concurrent)
|
||||||
|
gitLeaks = make(chan GitLeak)
|
||||||
|
cmdLock = sync.Mutex{}
|
||||||
|
cache = make(map[string]bool)
|
||||||
)
|
)
|
||||||
|
|
||||||
out, err = exec.Command("git", "branch", "--all").Output()
|
out, err = exec.Command("git", "branch", "--all").Output()
|
||||||
@@ -88,16 +90,13 @@ func (repo Repo) audit() []ReportElem {
|
|||||||
// iterate through branches, git rev-list <branch>
|
// iterate through branches, git rev-list <branch>
|
||||||
branches := bytes.Split(out, []byte("\n"))
|
branches := bytes.Split(out, []byte("\n"))
|
||||||
|
|
||||||
messages := make(chan MemoMessage)
|
|
||||||
|
|
||||||
for i, branchB := range branches {
|
for i, branchB := range branches {
|
||||||
if i < 2 || i == len(branches)-1 {
|
if i < 2 || i == len(branches)-1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
branch = string(bytes.Trim(branchB, " "))
|
branch = string(bytes.Trim(branchB, " "))
|
||||||
cmdLock.Lock()
|
cmdLock.Lock()
|
||||||
cmd := exec.Command("git", "rev-list", branch)
|
out, err := exec.Command("git", "rev-list", "--topo-order", branch).Output()
|
||||||
out, err := cmd.Output()
|
|
||||||
cmdLock.Unlock()
|
cmdLock.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("skipping branch", branch, err)
|
fmt.Println("skipping branch", branch, err)
|
||||||
@@ -114,32 +113,26 @@ func (repo Repo) audit() []ReportElem {
|
|||||||
commitB = string(currCommit)
|
commitB = string(currCommit)
|
||||||
_, seen := cache[commitA+commitB]
|
_, seen := cache[commitA+commitB]
|
||||||
if seen {
|
if seen {
|
||||||
fmt.Println("WE HAVE SEEN THIS")
|
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
cache[commitA+commitB] = true
|
cache[commitA+commitB] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(commitA string, commitB string,
|
go func(commitA string, commitB string, branch string, repoName string) {
|
||||||
j int, branch string) {
|
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
var leakPrs bool
|
var leakPrs bool
|
||||||
var leaks []string
|
var leaks []string
|
||||||
fmt.Println(j, branch)
|
|
||||||
|
|
||||||
if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repo.name)); err != nil {
|
if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
semaphoreChan <- struct{}{}
|
semaphoreChan <- struct{}{}
|
||||||
fmt.Println("diffing", branch, j)
|
out, err := exec.Command("git", "diff", commitA, commitB).Output()
|
||||||
cmd := exec.Command("git", "diff", commitA, commitB)
|
|
||||||
out, err := cmd.Output()
|
|
||||||
<-semaphoreChan
|
<-semaphoreChan
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("no diff: ", err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
lines := checkRegex(string(out))
|
lines := checkRegex(string(out))
|
||||||
@@ -154,17 +147,17 @@ func (repo Repo) audit() []ReportElem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
messages <- MemoMessage{commitA + commitB, leaks, commitA, commitB, branch}
|
gitLeaks <- GitLeak{commitA + commitB, leaks, commitA, commitB, branch}
|
||||||
|
|
||||||
}(commitA, commitB, j, branch)
|
}(commitA, commitB, branch, repoName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
for memoMsg := range messages {
|
for gitLeak := range gitLeaks {
|
||||||
fmt.Println(memoMsg)
|
if len(gitLeak.leaks) != 0 {
|
||||||
if len(memoMsg.leaks) != 0 {
|
fmt.Println(gitLeak.branch)
|
||||||
report = append(report, ReportElem{memoMsg.leaks, memoMsg.branch,
|
report = append(report, ReportElem{gitLeak.leaks, gitLeak.branch,
|
||||||
memoMsg.commitA, memoMsg.commitB})
|
gitLeak.commitA, gitLeak.commitB})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -5,9 +5,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cache map[string]bool
|
|
||||||
appRoot string
|
appRoot string
|
||||||
regexes map[string]*regexp.Regexp
|
regexes map[string]*regexp.Regexp
|
||||||
assignRegex *regexp.Regexp
|
assignRegex *regexp.Regexp
|
||||||
@@ -16,9 +14,9 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
appRoot, _ = os.Getwd()
|
appRoot, _ = os.Getwd()
|
||||||
cache = make(map[string]bool)
|
|
||||||
// TODO update regex to look for things like:
|
// TODO update regex to look for things like:
|
||||||
// client("fewafewakwafejwkaf",
|
// TODO ability to add/filter regex
|
||||||
|
// client("AKAI32fJ334...",
|
||||||
regexes = map[string]*regexp.Regexp{
|
regexes = map[string]*regexp.Regexp{
|
||||||
"github": regexp.MustCompile(`[g|G][i|I][t|T][h|H][u|U][b|B].*(=|:|:=|<-).*\w+.*`),
|
"github": regexp.MustCompile(`[g|G][i|I][t|T][h|H][u|U][b|B].*(=|:|:=|<-).*\w+.*`),
|
||||||
"aws": regexp.MustCompile(`[a|A][w|W][s|S].*(=|:=|:|<-).*\w+.*`),
|
"aws": regexp.MustCompile(`[a|A][w|W][s|S].*(=|:=|:|<-).*\w+.*`),
|
||||||
@@ -32,14 +30,8 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
args := os.Args[1:]
|
args := os.Args[2:]
|
||||||
opts := parseOptions(args)
|
repoUrl := os.Args[1]
|
||||||
start(opts)
|
opts := parseOptions(args, repoUrl)
|
||||||
|
start(opts, repoUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
func start(opts *Options) {
|
|
||||||
if opts.Repo != "" {
|
|
||||||
repoStart(opts.Repo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
+21
-16
@@ -3,26 +3,21 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO regex on type.. user/organization can be treated as the same:
|
// TODO regex on type.. user/organization can be treated as the same:
|
||||||
// hittps://github.com/<user or org>
|
// hittps://github.com/<user or org>
|
||||||
// hittps://github.com/<user or org>/repo
|
// hittps://github.com/<user or org>/repo
|
||||||
const usage = `usage: gogethunt [options]
|
const usage = `usage: gitleaks [git link] [options]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-u --user Target user
|
-c Concurrency factor (potential number of git files open)
|
||||||
-r --repo Target repo
|
|
||||||
-o --org Target organization
|
|
||||||
-h --help Display this message
|
-h --help Display this message
|
||||||
-e --entropy Enable entropy detection
|
|
||||||
-r --regex Enable regex detection
|
|
||||||
`
|
`
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
User string
|
Concurrency int
|
||||||
Repo string
|
|
||||||
Org string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func help() {
|
func help() {
|
||||||
@@ -30,6 +25,20 @@ func help() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func optionsNextInt(args []string, i *int) int {
|
||||||
|
if len(args) > *i+1 {
|
||||||
|
*i++
|
||||||
|
} else {
|
||||||
|
help()
|
||||||
|
}
|
||||||
|
argInt, err := strconv.Atoi(args[*i])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
|
||||||
|
help()
|
||||||
|
}
|
||||||
|
return argInt
|
||||||
|
}
|
||||||
|
|
||||||
func optionsNextString(args []string, i *int) string {
|
func optionsNextString(args []string, i *int) string {
|
||||||
if len(args) > *i+1 {
|
if len(args) > *i+1 {
|
||||||
*i++
|
*i++
|
||||||
@@ -39,17 +48,13 @@ func optionsNextString(args []string, i *int) string {
|
|||||||
return args[*i]
|
return args[*i]
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseOptions(args []string) *Options {
|
func parseOptions(args []string, repoUrl string) *Options {
|
||||||
opts := &Options{}
|
opts := &Options{}
|
||||||
for i := 0; i < len(args); i++ {
|
for i := 0; i < len(args); i++ {
|
||||||
arg := args[i]
|
arg := args[i]
|
||||||
switch arg {
|
switch arg {
|
||||||
case "-o", "--org":
|
case "-c":
|
||||||
opts.Org = optionsNextString(args, &i)
|
opts.Concurrency = optionsNextInt(args, &i)
|
||||||
case "-r", "--repo":
|
|
||||||
opts.Repo = optionsNextString(args, &i)
|
|
||||||
case "-u", "--user":
|
|
||||||
opts.User = optionsNextString(args, &i)
|
|
||||||
case "-h", "--help":
|
case "-h", "--help":
|
||||||
help()
|
help()
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in new issue
Block a user