-
Notifications
You must be signed in to change notification settings - Fork 0
/
security-scan.go
80 lines (72 loc) · 1.97 KB
/
security-scan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bytes"
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
var err error
var outfile *os.File
var lines []string
dir := flag.String("git", "", "Git working directory to scan (defaults to current working directory)")
output := flag.String("o", "security-scan.csv", "Output CSV filename")
gopath := os.Getenv("GOPATH")
defaultMatchersPath := filepath.Join(gopath, "src", "github.com", "onetwopunch", "security-scan", "matchers.json")
matchers := flag.String("m", defaultMatchersPath, "JSON file containing a list of matchers\n\t[\n\t {\n\t \"description\":string,\n\t \"regex\":string\n\t }, ...\n\t]\n\t")
scanners := NewScanList(*matchers)
help := flag.Bool("h", false, "Usage")
flag.Parse()
if *help {
os.Exit(Usage())
}
os.MkdirAll(filepath.Dir(*output), 0755)
outfile, err = os.OpenFile(*output, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Reading Git history from %s...\n", *dir)
lines, err = gitLog(*dir)
if err != nil {
log.Fatal(err)
Usage()
}
writer := csv.NewWriter(outfile)
header := []string{"Description", "Filename", "Commit", "Author", "Line"}
writer.Write(header)
writer.Flush()
for _, scanner := range scanners {
fmt.Printf("[%v] Matches Found: 0\r", scanner.description)
for _, line := range lines {
if scanner.ScanLine(line) {
fmt.Printf("[%v] Matches Found: %v\r", scanner.description, len(scanner.matches))
}
}
fmt.Printf("\n")
writer.WriteAll(scanner.Records())
}
}
func gitLog(dir string) ([]string, error) {
var out bytes.Buffer
var err bytes.Buffer
cmd := exec.Command("git", "log", "-p")
cmd.Stderr = &err
cmd.Stdout = &out
cmd.Dir = dir
cmd.Run()
if err.Len() > 0 {
return []string{}, fmt.Errorf("%v", err.String())
}
lines := strings.Split(out.String(), "\n")
return lines, nil
}
func Usage() int {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
return 1
}