Skip to content

Commit

Permalink
Cleanup, simplification, and preparation for new modules (#14)
Browse files Browse the repository at this point in the history
* Large code cleanup in preparation for expanding with new modules

* Fixed typo in github action

Co-authored-by: Britton Hayes <[email protected]>
  • Loading branch information
brittonhayes and brittonhayes authored Feb 7, 2021
1 parent 68cfa8d commit 753aa43
Show file tree
Hide file tree
Showing 36 changed files with 807 additions and 994 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ changelog:
- '^docs:'
- '^test:'
- '^images:'
- '^examples:'
- '^_examples:'
scoop:
# Template for the url which is determined by the given Token (github or gitlab)
url_template: "https://github.com/brittonhayes/pillager/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
Expand Down
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,19 @@ pillager [cmd] --help

Pillager provides full support for [Gitleaks](https://github.com/zricethezav/gitleaks) rules. This can either be passed in with a [rules.toml](./rules.toml) file, or you can use the default ruleset by leaving the rules flag blank.

> Currently entropy is not implemented, but it is absolutely planned to add support for Gitleaks' entropy configs in future updates.
```toml
# rules.toml
title = "pillager rules"

[[rules]]
description = "AWS Access Key"
regex = '''(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}'''
tags = ["key", "AWS"]
[[rules.entropies]]
Min = "3.5"
Max = "4.5"
Group = "1"

[[rules]]
description = "Email Address"
regex = '''(?i)([A-Za-z0-9!#$%&'*+\/=?^_{|.}~-]+@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)'''
Expand All @@ -80,23 +84,15 @@ tags = ["email", "User Info"]

## Documentation

:books: [View the docs](./pkg/hunter)
:books: [View the docs](hunter)

GoDoc documentation is available on [pkg.go.dev for pillager](https://pkg.go.dev/github.com/brittonhayes/pillager) but it also available for all packages in the [./pkg](./pkg) directory.
Just open the folder of any package and you'll see the GoDocs rendered in beautiful Github-flavored markdown thanks to the awesome [gomarkdoc](https://github.com/princjef/gomarkdoc) tool.
Just open the folder of any package, and you'll see the GoDocs rendered in beautiful Github-flavored markdown thanks to the awesome [gomarkdoc](https://github.com/princjef/gomarkdoc) tool.

---

### Shoulders of Giants :star:

#### [afero's regexpFs](https://github.com/spf13/afero#regexpfs).

**What is RegexpFs?**

> A filtered view on file names, any file NOT matching the provided patterns will be treated as non-existing.
This is important because it limits the number of files being scanned in the first place, thus cutting down on the time to finish the scan. In other words, we probably don't want to attempt to scan a `*.mp4` from top to bottom, but we definitely _do_ want to scan a `*.env` from top to bottom.

#### [afero's Cobra](https://github.com/spf13/cobra)

**What is Cobra?**
Expand Down
18 changes: 18 additions & 0 deletions _examples/hunter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/brittonhayes/pillager/hunter"
"github.com/brittonhayes/pillager/rules"
"github.com/spf13/afero"
)

func main() {
// Create a new hunter config
c := hunter.NewConfig(afero.NewOsFs(), ".", true, rules.Load(""), hunter.StringToFormat("JSON"))

// Create a new hunter from the config
h := hunter.NewHunter(c)

// Start hunting
_ = h.Hunt()
}
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions _examples/testdata/github.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ [email protected]
[email protected]
[email protected]
[email protected]
[email protected]
27 changes: 27 additions & 0 deletions _examples/testdata/ssh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAsZsEGWDwTgdodV5CXE6it5k7ttD2SN+VTOLY+KLD0pISpZwq
GQW5u8cfz4yLDm2CW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brv4wsZUbDF9HphQc
A2b6gi2FhKBvBPi5uuGwTWg9g64PlHZtLiItj6OzG+KPbV6hbxXlsZVDWa/3JPEf
W5eaKs3zGeZ+mg72ajv4tQKBgDMB/TAhyFe3OCZxc4YYsRWEeS7xgvCx9b49O0tC
3GYKVp2W7ZzWshTed8As/Jxv2doamZiIJ1u8axPz6p2VM+/uH4oYj9kFAH3ab6Rh
WWMqGINLGIQAZB4JjXQeQL1ws5cj8Pn9rlqpclATL/LRtd+7L4hOlebBWHJgGXhD
CM/hAoGBAJC3PdClk2nUT3gVLSaIaS+MuwV/pOIydB8mP7arTW/I1hR/m32pNdhL
o7FreZIOKDPWPcjd4hKlyMtteQoZtBT8mzvOKMnKGT4D3m+tHqQddxabBQ3yeDmh
wXfDn7PCx2tH98tQoo9wO+HuRdgXTLjlrJ7tSV8ovLQmXUynOw9D
-----END RSA PRIVATE KEY-----
55 changes: 13 additions & 42 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,42 @@
package main_test

import (
"github.com/brittonhayes/pillager/pkg/hunter"
"github.com/spf13/afero"
"log"
"os"
"testing"

"github.com/brittonhayes/pillager/hunter"
"github.com/brittonhayes/pillager/rules"
"github.com/spf13/afero"
"github.com/zricethezav/gitleaks/v7/scan"
)

// A benchmark of the Hunter Load Rules method
func BenchmarkHunterLoadRules(b *testing.B) {
for n := 0; n < b.N; n++ {
hunter.LoadRules("")
rules.Load("")
}
}

// A benchmark of the Hound Fetch method which
// A benchmark of the Hound Howl method which
// prints results out in desired format
func BenchmarkHunterHoundFetch(b *testing.B) {
func BenchmarkHunterHoundHowl(b *testing.B) {
b.StopTimer()
h := hunter.NewHound(&hunter.Config{
System: afero.NewMemMapFs(),
Rules: hunter.LoadRules(""),
Rules: rules.Load(""),
Format: hunter.JSONFormat,
})
h.Findings = []hunter.Finding{
{
Count: 1,
Message: "Found something juicy",
Path: "example.toml",
Loot: []string{"Token 1234560"},
findings := scan.Report{
Leaks: []scan.Leak{
{Line: "[email protected]", LineNumber: 16, Offender: "[email protected]", Rule: "Email Addresses"},
},
}

b.StartTimer()
for n := 0; n < b.N; n++ {
defer quiet()()
h.Fetch()
}
}

// A benchmark of the Hunter Inspect method
func BenchmarkHunterInspect(b *testing.B) {
b.StopTimer()
fs := afero.NewMemMapFs()
f, err := fs.Create("fake.toml")
if err != nil {
panic(err)
}
defer f.Close()
_, err = f.Write([]byte(`[email protected]`))
if err != nil {
panic(err)
}

h := hunter.NewHunter(&hunter.Config{
System: fs,
Rules: hunter.LoadRules(""),
BasePath: ".",
Verbose: true,
Format: hunter.JSONFormat,
})

b.StartTimer()
for n := 0; n < b.N; n++ {
defer quiet()()
h.Inspect(f.Name(), h.Config.System)
h.Howl(findings)
}
}

Expand Down
56 changes: 37 additions & 19 deletions cmd/hunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package cmd

import (
"github.com/brittonhayes/pillager/pkg/hunter"
"github.com/brittonhayes/pillager/hunter"
"github.com/brittonhayes/pillager/rules"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
Expand All @@ -13,39 +14,56 @@ var (
verbose bool
rulesConfig string
output string
templ string
)

// huntCmd represents the hunt command
var huntCmd = &cobra.Command{
Use: "hunt [directory]",
Short: "Hunt for loot",
Long: "Hunt inside the file system for valuable information",
Args: cobra.MinimumNArgs(1),
RunE: StartHunt(),
Example: `
# Run a basic hunt
pillager hunt ./
# Print out results in JSON format
pillager hunt ./example -r rules.toml -f json
# Print out results with a custom inline template
pillager hunt ./example -r rules.toml -f custom --template "{{ range .Leaks}}Leak: {{.Line}}{{end}}"
# Print out results with a custom template file
pillager hunt ./example -r rules.toml -f custom --template "$(cat templates/simple.tmpl)"
`,
Args: cobra.MinimumNArgs(1),
RunE: StartHunt(),
}

func init() {
rootCmd.AddCommand(huntCmd)
huntCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "toggle verbose output")
huntCmd.Flags().StringVarP(&rulesConfig, "rules-config", "r", "", "path to gitleaks rules config")
huntCmd.Flags().StringVarP(&output, "output", "o", "yaml", "set output format (json, yaml)")
huntCmd.Flags().StringVarP(&rulesConfig, "rules", "r", "", "path to gitleaks rules.toml config")
huntCmd.Flags().StringVarP(&output, "format", "f", "yaml", "set output format (json, yaml, custom)")
huntCmd.Flags().StringVarP(
&templ,
"template",
"t",
hunter.DefaultTemplate,
"set go text/template string for output format",
)
}

func StartHunt() func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
fs := afero.NewOsFs()
c := hunter.Config{
System: fs,
BasePath: hunter.CheckPath(fs, args[0]),
Verbose: verbose,
Format: hunter.StringToFormat(output),
Rules: hunter.LoadRules(rulesConfig),
}
h := hunter.NewHunter(&c)
err := h.Hunt()
if err != nil {
return err
}
return nil
c := hunter.NewConfig(
afero.NewOsFs(),
args[0],
verbose,
rules.Load(rulesConfig),
hunter.StringToFormat(output),
templ,
)
h := hunter.NewHunter(c)
return h.Hunt()
}
}
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"os"

"github.com/gookit/color"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)

var cfgFile string
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.7.1
github.com/zricethezav/gitleaks/v7 v7.2.2
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect
golang.org/x/sys v0.0.0-20201204225414-ed752295db88 // indirect
golang.org/x/text v0.3.4 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
Expand Down
Loading

0 comments on commit 753aa43

Please sign in to comment.