forked from eth0izzle/shhgit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (123 loc) · 4.33 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bufio"
"bytes"
"os"
"regexp"
"strings"
"github.com/eth0izzle/shhgit/core"
"github.com/fatih/color"
)
var session = core.GetSession()
func ProcessRepositories() {
threadNum := *session.Options.Threads
for i := 0; i < threadNum; i++ {
go func(tid int) {
for {
repositoryId := <-session.Repositories
repo, err := core.GetRepository(session, repositoryId)
if err != nil {
session.Log.Warn("Failed to retrieve repository %d: %s", repositoryId, err)
continue
}
if repo.GetPermissions()["pull"] &&
uint(repo.GetStargazersCount()) >= *session.Options.MinimumStars &&
uint(repo.GetSize()) < *session.Options.MaximumRepositorySize {
processRepositoryOrGist(repo.GetCloneURL())
}
}
}(i)
}
}
func ProcessGists() {
threadNum := *session.Options.Threads
for i := 0; i < threadNum; i++ {
go func(tid int) {
for {
gistUrl := <-session.Gists
processRepositoryOrGist(gistUrl)
}
}(i)
}
}
func processRepositoryOrGist(url string) {
var (
matches []string
matchedAny bool = false
)
dir := core.GetTempDir(core.GetHash(url))
_, err := core.CloneRepository(session, url, dir)
if err != nil {
session.Log.Debug("[%s] Cloning failed: %s", url, err.Error())
os.RemoveAll(dir)
return
}
session.Log.Debug("[%s] Cloning in to %s", url, strings.Replace(dir, *session.Options.TempDirectory, "", -1))
for _, file := range core.GetMatchingFiles(dir) {
relativeFileName := strings.Replace(file.Path, *session.Options.TempDirectory, "", -1)
if *session.Options.SearchQuery != "" {
queryRegex := regexp.MustCompile(*session.Options.SearchQuery)
for _, match := range queryRegex.FindAllSubmatch(file.Contents, -1) {
matches = append(matches, string(match[0]))
}
if matches != nil {
count := len(matches)
m := strings.Join(matches, ", ")
session.Log.Important("[%s] %d %s for %s in file %s: %s", url, count, core.Pluralize(count, "match", "matches"), color.GreenString("Search Query"), relativeFileName, color.YellowString(m))
session.WriteToCsv([]string{url, "Search Query", relativeFileName, m})
}
} else {
for _, signature := range session.Signatures {
if matched, part := signature.Match(file); matched {
matchedAny = true
if part == core.PartContents {
if matches = signature.GetContentsMatches(file); matches != nil {
count := len(matches)
m := strings.Join(matches, ", ")
session.Log.Important("[%s] %d %s for %s in file %s: %s", url, count, core.Pluralize(count, "match", "matches"), color.GreenString(signature.Name()), relativeFileName, color.YellowString(m))
session.WriteToCsv([]string{url, signature.Name(), relativeFileName, m})
}
} else {
if *session.Options.PathChecks {
session.Log.Important("[%s] Matching file %s for %s", url, color.YellowString(relativeFileName), color.GreenString(signature.Name()))
session.WriteToCsv([]string{url, signature.Name(), relativeFileName, ""})
}
if *session.Options.EntropyThreshold > 0 && file.CanCheckEntropy() {
scanner := bufio.NewScanner(bytes.NewReader(file.Contents))
for scanner.Scan() {
line := scanner.Text()
if len(line) > 6 && len(line) < 100 {
entropy := core.GetEntropy(scanner.Text())
if entropy >= *session.Options.EntropyThreshold {
session.Log.Important("[%s] Potential secret in %s = %s", url, color.YellowString(relativeFileName), color.GreenString(scanner.Text()))
session.WriteToCsv([]string{url, signature.Name(), relativeFileName, scanner.Text()})
}
}
}
}
}
}
}
}
if !matchedAny {
os.Remove(file.Path)
}
}
if !matchedAny {
os.RemoveAll(dir)
}
}
func main() {
session.Log.Info("%s v%s started. Loaded %d signatures. Using %d GitHub tokens and %d threads. Work dir: %s", core.Name, core.Version, len(session.Signatures), len(session.Clients), *session.Options.Threads, *session.Options.TempDirectory)
if *session.Options.SearchQuery != "" {
session.Log.Important("Search Query '%s' given. Only returning matching results.", *session.Options.SearchQuery)
}
go core.GetRepositories(session)
go ProcessRepositories()
if *session.Options.ProcessGists {
go core.GetGists(session)
go ProcessGists()
}
session.Log.Info("Press Ctrl+C to stop and exit.\n")
select {}
}