Compare commits

..
19 Commits
Author SHA1 Message Date
Zachary Rice ef38b69c39 Merge pull request #302 from petegallagher/whitelist-gitleaks.toml
Update default whitelist to include gitleaks.toml
2019-11-30 09:44:23 -06:00
Zachary Rice ac6bde9996 Merge pull request #296 from petegallagher/private-key
Update asymmetric private key regex
2019-11-30 09:41:35 -06:00
Zachary Rice 23568cc0ba Merge pull request #295 from petegallagher/sendgrid
Adding regex for SendGrid API keys
2019-11-30 09:41:10 -06:00
Zachary Rice e61c2cfae7 Merge pull request #301 from petegallagher/AuditUncommitted
Fix repo-config and file whitelist for AuditUncommitted function
2019-11-30 09:40:24 -06:00
Peter Gallagher 38e0b11995 Fix test data for AuditUncommitted 2019-11-29 10:22:02 +00:00
Peter Gallagher 31903d1a80 Update default whitelist to include gitleaks.toml 2019-11-29 10:14:03 +00:00
Peter Gallagher 3655284b57 Fix test data for AuditUncommitted 2019-11-29 10:07:56 +00:00
Peter Gallagher db79a96118 Use Unix Epoch time for staged commit time 2019-11-29 10:07:27 +00:00
Peter Gallagher999 5152e1fc63 Reset commit values when auditing uncommitted changes 2019-11-28 23:13:59 +00:00
Peter Gallagher c04e137b28 Adding global file regex to AuditUncommitted 2019-11-28 22:53:08 +00:00
Peter Gallagher 9b3342fbff Allow use of --repo-config for AuditUncommitted function 2019-11-28 13:53:52 +00:00
Peter Gallagher d9a060a12d Honour file whitelist for AuditUncommitted function 2019-11-28 13:53:18 +00:00
Peter Gallagher 77f2fdd09a Update asymmetric private key regex 2019-11-25 15:59:40 +00:00
Peter Gallagher dba1ef6104 Adding regex for SendGrid API keys 2019-11-25 15:49:47 +00:00
Zachary Rice 725da55905 Merge pull request #289 from zricethezav/fix/3.0.2
recover on panic in patch generation, full filepath for report, versi…
2019-11-23 09:56:27 -05:00
zach rice cfac551ab8 recover on panic in patch generation, full filepath for report, version message if not built with LDFLAGS 2019-11-23 09:50:16 -05:00
Zachary Rice c559fdbf04 Merge pull request #287 from chenrui333/go-1.13.4
Bump golang and alpine to latest
2019-11-17 13:41:43 -06:00
Rui Chen e2ac47cd06 Bump alpine to v3.10 2019-11-17 14:37:04 -05:00
Rui Chen 8ef16d9efb Bump golang to v1.13.4 2019-11-17 14:36:37 -05:00
14 changed files with 142 additions and 80 deletions

No files matched your search

+2 -3
View File
@@ -1,8 +1,7 @@
sudo: required
language: go language: go
go: go:
- 1.13 - 1.13.x
services: services:
- docker - docker
script: script:
- env GO111MODULE=on make test - make test
+2 -3
View File
@@ -1,9 +1,9 @@
FROM golang:1.13.0 AS build FROM golang:1.13.4 AS build
WORKDIR /go/src/github.com/zricethezav/gitleaks WORKDIR /go/src/github.com/zricethezav/gitleaks
COPY . . COPY . .
RUN GO111MODULE=on CGO_ENABLED=0 go build -o bin/gitleaks *.go RUN GO111MODULE=on CGO_ENABLED=0 go build -o bin/gitleaks *.go
FROM alpine:3.7 FROM alpine:3.10
RUN apk add --no-cache bash git openssh RUN apk add --no-cache bash git openssh
COPY --from=build /go/src/github.com/zricethezav/gitleaks/bin/* /usr/bin/ COPY --from=build /go/src/github.com/zricethezav/gitleaks/bin/* /usr/bin/
ENTRYPOINT ["gitleaks"] ENTRYPOINT ["gitleaks"]
@@ -14,4 +14,3 @@ ENTRYPOINT ["gitleaks"]
# docker run --rm --name=gitleaks gitleaks --repo=https://github.com/zricethezav/gitleaks # docker run --rm --name=gitleaks gitleaks --repo=https://github.com/zricethezav/gitleaks
# This will check for secrets in https://github.com/zricethezav/gitleaks # This will check for secrets in https://github.com/zricethezav/gitleaks
+4 -3
View File
@@ -2,11 +2,12 @@ package audit
import ( import (
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"github.com/zricethezav/gitleaks/manager"
"io/ioutil" "io/ioutil"
"path" "path"
"github.com/zricethezav/gitleaks/manager"
log "github.com/sirupsen/logrus"
) )
// Run accepts a manager and begins an audit based on the options/configs set in the manager. // Run accepts a manager and begins an audit based on the options/configs set in the manager.
+63 -22
View File
@@ -4,23 +4,25 @@ import (
"bytes" "bytes"
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"github.com/BurntSushi/toml"
"github.com/sergi/go-diff/diffmatchpatch"
log "github.com/sirupsen/logrus"
"github.com/zricethezav/gitleaks/config"
"github.com/zricethezav/gitleaks/manager"
"gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
"gopkg.in/src-d/go-git.v4/storage/memory"
"io" "io"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"sync" "sync"
"time" "time"
"github.com/zricethezav/gitleaks/config"
"github.com/zricethezav/gitleaks/manager"
"github.com/BurntSushi/toml"
"github.com/sergi/go-diff/diffmatchpatch"
log "github.com/sirupsen/logrus"
"gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
"gopkg.in/src-d/go-git.v4/storage/memory"
) )
// Repo wraps a *git.Repository object in addition to a manager object and the name of the repo. // Repo wraps a *git.Repository object in addition to a manager object and the name of the repo.
@@ -82,6 +84,15 @@ func (repo *Repo) Clone(cloneOption *git.CloneOptions) error {
// AuditUncommitted will do a `git diff` and scan changed files that are being tracked. This is useful functionality // AuditUncommitted will do a `git diff` and scan changed files that are being tracked. This is useful functionality
// for a pre-commit hook so you can make sure your code does not have any leaks before committing. // for a pre-commit hook so you can make sure your code does not have any leaks before committing.
func (repo *Repo) AuditUncommitted() error { func (repo *Repo) AuditUncommitted() error {
// load up alternative config if possible, if not use manager's config
if repo.Manager.Opts.RepoConfig {
cfg, err := repo.loadRepoConfig()
if err != nil {
return err
}
repo.config = cfg
}
auditTimeStart := time.Now() auditTimeStart := time.Now()
r, err := repo.Head() r, err := repo.Head()
@@ -93,6 +104,12 @@ func (repo *Repo) AuditUncommitted() error {
if err != nil { if err != nil {
return err return err
} }
// Staged change so the commit details do not yet exist. Insert empty defaults.
c.Hash = plumbing.Hash{}
c.Message = "***STAGED CHANGES***"
c.Author.Name = ""
c.Author.Email = ""
c.Author.When = time.Unix(0, 0).UTC()
prevTree, err := c.Tree() prevTree, err := c.Tree()
if err != nil { if err != nil {
@@ -144,18 +161,35 @@ func (repo *Repo) AuditUncommitted() error {
} }
} }
dmp := diffmatchpatch.New() if fileMatched(filename, repo.config.Whitelist.File) {
diffs := dmp.DiffMain(prevFileContents, currFileContents, false) log.Debugf("whitelisted file found, skipping audit of file: %s", filename)
var diffContents string } else if fileMatched(filename, repo.config.FileRegex) {
for _, d := range diffs { repo.Manager.SendLeaks(manager.Leak{
switch d.Type { Line: "N/A",
case diffmatchpatch.DiffInsert: Offender: filename,
diffContents += fmt.Sprintf("%s\n", d.Text) Commit: c.Hash.String(),
case diffmatchpatch.DiffDelete: Repo: repo.Name,
diffContents += fmt.Sprintf("%s\n", d.Text) Rule: "file regex matched" + repo.config.FileRegex.String(),
Message: c.Message,
Author: c.Author.Name,
Email: c.Author.Email,
Date: c.Author.When,
File: filename,
})
} else {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(prevFileContents, currFileContents, false)
var diffContents string
for _, d := range diffs {
switch d.Type {
case diffmatchpatch.DiffInsert:
diffContents += fmt.Sprintf("%s\n", d.Text)
case diffmatchpatch.DiffDelete:
diffContents += fmt.Sprintf("%s\n", d.Text)
}
} }
InspectString(diffContents, c, repo, filename)
} }
InspectString(diffContents, c, repo, filename)
} }
} }
@@ -209,7 +243,6 @@ func (repo *Repo) Audit() error {
return err return err
} }
//checker := make(map[string]bool)
cc := 0 cc := 0
semaphore := make(chan bool, howManyThreads(repo.Manager.Opts.Threads)) semaphore := make(chan bool, howManyThreads(repo.Manager.Opts.Threads))
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
@@ -233,6 +266,14 @@ func (repo *Repo) Audit() error {
cc++ cc++
err = c.Parents().ForEach(func(parent *object.Commit) error { err = c.Parents().ForEach(func(parent *object.Commit) error {
defer func() {
if err := recover(); err != nil {
// sometimes the patch generation will fail due to a known bug in
// sergi's go-diff: https://github.com/sergi/go-diff/issues/89.
// Once a fix has been merged I will remove this recover.
return
}
}()
start := time.Now() start := time.Now()
patch, err := c.Patch(parent) patch, err := c.Patch(parent)
if err != nil { if err != nil {
+11 -10
View File
@@ -2,19 +2,20 @@ package audit
import ( import (
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"github.com/zricethezav/gitleaks/config"
"github.com/zricethezav/gitleaks/manager"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
fdiff "gopkg.in/src-d/go-git.v4/plumbing/format/diff"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"math" "math"
"path"
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
"time" "time"
"github.com/zricethezav/gitleaks/config"
"github.com/zricethezav/gitleaks/manager"
log "github.com/sirupsen/logrus"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
fdiff "gopkg.in/src-d/go-git.v4/plumbing/format/diff"
"gopkg.in/src-d/go-git.v4/plumbing/object"
) )
const maxLineLen = 200 const maxLineLen = 200
@@ -59,9 +60,9 @@ func getFileName(f fdiff.FilePatch) string {
fn := "???" fn := "???"
from, to := f.Files() from, to := f.Files()
if from != nil { if from != nil {
return path.Base(from.Path()) return from.Path()
} else if to != nil { } else if to != nil {
return path.Base(to.Path()) return to.Path()
} }
return fn return fn
+4 -2
View File
@@ -2,11 +2,13 @@ package config
import ( import (
"fmt" "fmt"
"github.com/BurntSushi/toml"
"github.com/zricethezav/gitleaks/options"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/zricethezav/gitleaks/options"
"github.com/BurntSushi/toml"
) )
// Whitelist is struct containing items that if encountered will whitelist // Whitelist is struct containing items that if encountered will whitelist
+10 -5
View File
@@ -62,9 +62,9 @@ title = "gitleaks config"
tags = ["key", "Slack"] tags = ["key", "Slack"]
[[rules]] [[rules]]
description = "EC" description = "Asymmetric Private Key"
regex = '''-----BEGIN EC PRIVATE KEY-----''' regex = '''-----BEGIN (EC|PGP|DSA|RSA|OPENSSH) PRIVATE KEY( BLOCK)?-----'''
tags = ["key", "EC"] tags = ["key", "AsymmetricPrivateKey"]
[[rules]] [[rules]]
description = "Generic Credential" description = "Generic Credential"
@@ -102,6 +102,11 @@ title = "gitleaks config"
regex = '''sk_live_[0-9a-z]{32}''' regex = '''sk_live_[0-9a-z]{32}'''
tags = ["key", "Picatic"] tags = ["key", "Picatic"]
[[rules]]
description = "SendGrid API Key"
regex = '''SG\.[\w_]{16,32}\.[\w_]{16,64}'''
tags = ["key", "SendGrid"]
[[rules]] [[rules]]
description = "Slack Webhook" description = "Slack Webhook"
regex = '''https://hooks.slack.com/services/T[a-zA-Z0-9_]{8}/B[a-zA-Z0-9_]{8}/[a-zA-Z0-9_]{24}''' regex = '''https://hooks.slack.com/services/T[a-zA-Z0-9_]{8}/B[a-zA-Z0-9_]{8}/[a-zA-Z0-9_]{24}'''
@@ -128,6 +133,6 @@ title = "gitleaks config"
tags = ["key", "twilio"] tags = ["key", "twilio"]
[whitelist] [whitelist]
description = "image whitelists" description = "Whitelisted files"
file = '''(.*?)(jpg|gif|doc|pdf|bin)$''' file = '''(^\.?gitleaks.toml$|(.*?)(jpg|gif|doc|pdf|bin)$)'''
` `
+1 -1
View File
@@ -1,6 +1,6 @@
module github.com/zricethezav/gitleaks module github.com/zricethezav/gitleaks
go 1.12 go 1.13
require ( require (
github.com/BurntSushi/toml v0.3.1 github.com/BurntSushi/toml v0.3.1
+7 -5
View File
@@ -2,18 +2,20 @@ package hosts
import ( import (
"context" "context"
"github.com/google/go-github/github" "strconv"
log "github.com/sirupsen/logrus" "strings"
"sync"
"github.com/zricethezav/gitleaks/audit" "github.com/zricethezav/gitleaks/audit"
"github.com/zricethezav/gitleaks/manager" "github.com/zricethezav/gitleaks/manager"
"github.com/zricethezav/gitleaks/options" "github.com/zricethezav/gitleaks/options"
"github.com/google/go-github/github"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object" "gopkg.in/src-d/go-git.v4/plumbing/object"
"strconv"
"strings"
"sync"
) )
// Github wraps a github client and manager. This struct implements what the Host interface defines. // Github wraps a github client and manager. This struct implements what the Host interface defines.
+5 -3
View File
@@ -2,12 +2,14 @@ package hosts
import ( import (
"context" "context"
log "github.com/sirupsen/logrus" "sync"
"github.com/xanzy/go-gitlab"
"github.com/zricethezav/gitleaks/audit" "github.com/zricethezav/gitleaks/audit"
"github.com/zricethezav/gitleaks/manager" "github.com/zricethezav/gitleaks/manager"
"github.com/zricethezav/gitleaks/options" "github.com/zricethezav/gitleaks/options"
"sync"
log "github.com/sirupsen/logrus"
"github.com/xanzy/go-gitlab"
) )
// Gitlab wraps a gitlab client and manager. This struct implements what the Host interface defines. // Gitlab wraps a gitlab client and manager. This struct implements what the Host interface defines.
+7 -5
View File
@@ -1,16 +1,18 @@
package main package main
import ( import (
"github.com/hako/durafmt" "io/ioutil"
log "github.com/sirupsen/logrus" "os"
"time"
"github.com/zricethezav/gitleaks/audit" "github.com/zricethezav/gitleaks/audit"
"github.com/zricethezav/gitleaks/config" "github.com/zricethezav/gitleaks/config"
"github.com/zricethezav/gitleaks/hosts" "github.com/zricethezav/gitleaks/hosts"
"github.com/zricethezav/gitleaks/manager" "github.com/zricethezav/gitleaks/manager"
"github.com/zricethezav/gitleaks/options" "github.com/zricethezav/gitleaks/options"
"io/ioutil"
"os" "github.com/hako/durafmt"
"time" log "github.com/sirupsen/logrus"
) )
func main() { func main() {
+8 -6
View File
@@ -6,18 +6,20 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/hako/durafmt"
"github.com/mattn/go-colorable"
log "github.com/sirupsen/logrus"
"github.com/zricethezav/gitleaks/config"
"github.com/zricethezav/gitleaks/options"
"gopkg.in/src-d/go-git.v4"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
"sync" "sync"
"text/tabwriter" "text/tabwriter"
"time" "time"
"github.com/zricethezav/gitleaks/config"
"github.com/zricethezav/gitleaks/options"
"github.com/hako/durafmt"
"github.com/mattn/go-colorable"
log "github.com/sirupsen/logrus"
"gopkg.in/src-d/go-git.v4"
) )
// Manager is a struct containing options and configs as well CloneOptions and CloneDir. // Manager is a struct containing options and configs as well CloneOptions and CloneDir.
+13 -7
View File
@@ -2,16 +2,18 @@ package options
import ( import (
"fmt" "fmt"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
"github.com/zricethezav/gitleaks/version"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
"io/ioutil" "io/ioutil"
"os" "os"
"os/user" "os/user"
"strings" "strings"
"github.com/zricethezav/gitleaks/version"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
) )
// No leaks or early exit due to invalid options // No leaks or early exit due to invalid options
@@ -72,7 +74,11 @@ func ParseOptions() (Options, error) {
} }
if opts.Version { if opts.Version {
fmt.Printf("%s\n", version.Version) if version.Version == "" {
fmt.Println("Gitleaks uses LDFLAGS to pull most recent version. Build with 'make build' for version")
} else {
fmt.Printf("%s\n", version.Version)
}
os.Exit(Success) os.Exit(Success)
} }
@@ -2,14 +2,14 @@
{ {
"line": " aws_access_key_id='AKIAIO5FODNN7DXAMPLE'", "line": " aws_access_key_id='AKIAIO5FODNN7DXAMPLE'",
"offender": "AKIAIO5FODNN7DXAMPLE", "offender": "AKIAIO5FODNN7DXAMPLE",
"commit": "d274003914c707212cbe84e3e466a00013ccb639", "commit": "0000000000000000000000000000000000000000",
"repo": "test_repo_1", "repo": "test_repo_1",
"rule": "AWS Manager ID", "rule": "AWS Manager ID",
"commitMessage": "comment\n", "commitMessage": "***STAGED CHANGES***",
"author": "zach rice", "author": "",
"email": "zricer@protonmail.com", "email": "",
"file": "server.test.py", "file": "server.test.py",
"date": "2019-10-24T10:03:38-04:00", "date": "1970-01-01T00:00:00Z",
"tags": "key, AWS" "tags": "key, AWS"
} }
] ]