forked from anshumanbh/git-all-secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
550 lines (450 loc) · 14.9 KB
/
main.go
File metadata and controls
550 lines (450 loc) · 14.9 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"sync"
"golang.org/x/oauth2"
"github.com/google/go-github/github"
uuid "github.com/satori/go.uuid"
)
var (
org = flag.String("org", "", "Name of the Organization to scan. Example: secretorg123")
token = flag.String("token", "", "Github Personal Access Token. This is required.")
outputFile = flag.String("output", "results.txt", "Output file to save the results.")
user = flag.String("user", "", "Name of the Github user to scan. Example: secretuser1")
repoURL = flag.String("repoURL", "", "HTTPS URL of the Github repo to scan. Example: https://github.com/anshumantestorg/repo1.git")
gistURL = flag.String("gistURL", "", "HTTPS URL of the Github gist to scan. Example: https://gist.github.com/secretuser1/81963f276280d484767f9be895316afc")
cloneForks = flag.Bool("cloneForks", false, "Option to clone org and user repos that are forks. Default is false")
toolName = flag.String("toolName", "all", "Specify whether to run gitsecrets, thog or repo-supervisor")
)
// Info Function to show colored text
func Info(format string, args ...interface{}) {
fmt.Printf("\x1b[34;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
}
func check(e error) {
if e != nil {
panic(e)
} else if _, ok := e.(*github.RateLimitError); ok {
log.Println("hit rate limit")
} else if _, ok := e.(*github.AcceptedError); ok {
log.Println("scheduled on GitHub side")
}
}
func gitclone(cloneURL string, repoName string, wg *sync.WaitGroup) {
cmd := exec.Command("/usr/bin/git", "clone", cloneURL, repoName)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
check(err)
wg.Done()
}
func cloneorgrepos(ctx context.Context, client *github.Client, org string) error {
Info("Cloning the repositories of the organization: " + org)
var orgRepos []*github.Repository
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 10},
}
for {
repos, resp, err := client.Repositories.ListByOrg(ctx, org, opt)
check(err)
orgRepos = append(orgRepos, repos...) //adding to the repo array
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
var orgclone sync.WaitGroup
//iterating through the repo array
for _, repo := range orgRepos {
switch *cloneForks {
case true:
//cloning everything
orgclone.Add(1)
fmt.Println(*repo.CloneURL)
//cloning the individual org repos
go gitclone(*repo.CloneURL, "/tmp/repos/org/"+*repo.Name, &orgclone)
case false:
//not cloning forks
if !*repo.Fork {
orgclone.Add(1)
fmt.Println(*repo.CloneURL)
//cloning the individual org repos
go gitclone(*repo.CloneURL, "/tmp/repos/org/"+*repo.Name, &orgclone)
}
}
}
orgclone.Wait()
fmt.Println("")
return nil
}
func cloneuserrepos(ctx context.Context, client *github.Client, user string) error {
Info("Cloning " + user + "'s repositories")
var userRepos []*github.Repository
opt3 := &github.RepositoryListOptions{
ListOptions: github.ListOptions{PerPage: 10},
}
for {
uRepos, resp, err := client.Repositories.List(ctx, user, opt3)
check(err)
userRepos = append(userRepos, uRepos...) //adding to the userRepos array
if resp.NextPage == 0 {
break
}
opt3.Page = resp.NextPage
}
var userrepoclone sync.WaitGroup
//iterating through the userRepos array
for _, userRepo := range userRepos {
switch *cloneForks {
case true:
//cloning everything
userrepoclone.Add(1)
fmt.Println(*userRepo.CloneURL)
go gitclone(*userRepo.CloneURL, "/tmp/repos/users/"+user+"/"+*userRepo.Name, &userrepoclone)
case false:
//not cloning forks
if !*userRepo.Fork {
userrepoclone.Add(1)
fmt.Println(*userRepo.CloneURL)
go gitclone(*userRepo.CloneURL, "/tmp/repos/users/"+user+"/"+*userRepo.Name, &userrepoclone)
}
}
}
userrepoclone.Wait()
fmt.Println("")
return nil
}
func cloneusergists(ctx context.Context, client *github.Client, user string) error {
Info("Cloning " + user + "'s gists")
var userGists []*github.Gist
opt4 := &github.GistListOptions{
ListOptions: github.ListOptions{PerPage: 10},
}
for {
uGists, resp, err := client.Gists.List(ctx, user, opt4)
check(err)
userGists = append(userGists, uGists...)
if resp.NextPage == 0 {
break
}
opt4.Page = resp.NextPage
}
var usergistclone sync.WaitGroup
//iterating through the userGists array
for _, userGist := range userGists {
usergistclone.Add(1)
fmt.Println(*userGist.GitPullURL)
//cloning the individual user gists
go gitclone(*userGist.GitPullURL, "/tmp/repos/users/"+user+"/"+*userGist.ID, &usergistclone)
}
usergistclone.Wait()
fmt.Println("")
return nil
}
func listallusers(ctx context.Context, client *github.Client, org string) ([]*github.User, error) {
Info("Users of the organization and their repositories and gists")
var allUsers []*github.User
opt2 := &github.ListMembersOptions{
ListOptions: github.ListOptions{PerPage: 10},
}
for {
users, resp, err := client.Organizations.ListMembers(ctx, org, opt2)
check(err)
allUsers = append(allUsers, users...) //adding to the allUsers array
if resp.NextPage == 0 {
break
}
opt2.Page = resp.NextPage
}
return allUsers, nil
}
func runGitsecrets(filepath string, reponame string, orgoruser string) error {
outputFile2 := "/tmp/results/gitsecrets/" + orgoruser + "_" + reponame + "_" + uuid.NewV4().String() + ".txt"
cmd2 := exec.Command("./rungitsecrets.sh", filepath, outputFile2)
var out2 bytes.Buffer
cmd2.Stdout = &out2
err2 := cmd2.Run()
check(err2)
return nil
}
func runTrufflehog(filepath string, reponame string, orgoruser string) error {
outputFile1 := "/tmp/results/thog/" + orgoruser + "_" + reponame + "_" + uuid.NewV4().String() + ".txt"
cmd1 := exec.Command("./thog/truffleHog/truffleHog.py", "-o", outputFile1, filepath)
var out1 bytes.Buffer
cmd1.Stdout = &out1
err1 := cmd1.Run()
check(err1)
return nil
}
func runReposupervisor(filepath string, reponame string, orgoruser string) error {
outputFile3 := "/tmp/results/repo-supervisor/" + orgoruser + "_" + reponame + "_" + uuid.NewV4().String() + ".txt"
cmd3 := exec.Command("./runreposupervisor.sh", filepath, outputFile3)
var out3 bytes.Buffer
cmd3.Stdout = &out3
err3 := cmd3.Run()
check(err3)
return nil
}
func runGitTools(tool string, filepath string, wg *sync.WaitGroup, reponame string, orgoruser string) {
switch tool {
case "all":
err := runGitsecrets(filepath, reponame, orgoruser)
check(err)
err = runTrufflehog(filepath, reponame, orgoruser)
check(err)
err = runReposupervisor(filepath, reponame, orgoruser)
check(err)
case "gitsecrets":
err := runGitsecrets(filepath, reponame, orgoruser)
check(err)
case "thog":
err := runTrufflehog(filepath, reponame, orgoruser)
check(err)
case "repo-supervisor":
err := runReposupervisor(filepath, reponame, orgoruser)
check(err)
}
wg.Done()
}
func scanforeachuser(user string, wg *sync.WaitGroup) {
var wguserrepogist sync.WaitGroup
gituserrepos, _ := ioutil.ReadDir("/tmp/repos/users/" + user)
for _, f := range gituserrepos {
wguserrepogist.Add(1)
go runGitTools(*toolName, "/tmp/repos/users/"+user+"/"+f.Name()+"/", &wguserrepogist, f.Name(), user)
}
wguserrepogist.Wait()
wg.Done()
}
func toolsOutput(toolname string, of *os.File) error {
linedelimiter := "----------------------------------------------------------------------------" +
"----------------------------------------------------------------------------" +
"----------------------------------------------------------------------------" +
"----------------------------------------------------------------------------"
_, err := of.WriteString("Tool: " + toolname + "\n")
check(err)
results, _ := ioutil.ReadDir("/tmp/results/" + toolname + "/")
for _, f := range results {
file, err := os.Open("/tmp/results/" + toolname + "/" + f.Name())
check(err)
fi, err := file.Stat()
check(err)
if fi.Size() == 0 {
continue
} else if fi.Size() > 0 {
fname := strings.Split(f.Name(), "_")
orgoruserstr := fname[0]
rnamestr := fname[1]
_, err1 := of.WriteString("OrgorUser: " + orgoruserstr + " RepoName: " + rnamestr + "\n")
check(err1)
if _, err2 := io.Copy(of, file); err2 != nil {
return err2
}
_, err3 := of.WriteString(linedelimiter + "\n")
check(err3)
of.Sync()
}
defer file.Close()
}
return nil
}
func singletoolOutput(toolname string, of *os.File) error {
results, _ := ioutil.ReadDir("/tmp/results/" + toolname + "/")
for _, f := range results {
file, err := os.Open("/tmp/results/" + toolname + "/" + f.Name())
check(err)
fi, err := file.Stat()
check(err)
if fi.Size() == 0 {
continue
} else if fi.Size() > 0 {
if _, err2 := io.Copy(of, file); err2 != nil {
return err2
}
of.Sync()
}
defer file.Close()
}
return nil
}
func combineOutput(toolname string, outputfile string) error {
// Read all files in /tmp/results/<tool-name>/ directories for all the tools
// open a new file and save it in the output directory - outputFile
// for each results file, write user/org and reponame, copy results from the file in the outputFile, end with some delimiter
of, err := os.Create(outputfile)
check(err)
switch toolname {
case "all":
tools := []string{"thog", "gitsecrets", "repo-supervisor"}
for _, tool := range tools {
err = toolsOutput(tool, of)
check(err)
}
case "gitsecrets":
err = singletoolOutput("gitsecrets", of)
check(err)
case "thog":
err = singletoolOutput("thog", of)
check(err)
case "repo-supervisor":
err = singletoolOutput("repo-supervisor", of)
check(err)
}
defer func() {
cerr := of.Close()
if err == nil {
err = cerr
}
}()
return nil
}
func scanorgrepos(org string) error {
var wgorg sync.WaitGroup
gitorgrepos, _ := ioutil.ReadDir("/tmp/repos/org/")
for _, f := range gitorgrepos {
wgorg.Add(1)
go runGitTools(*toolName, "/tmp/repos/org/"+f.Name()+"/", &wgorg, f.Name(), org)
}
wgorg.Wait()
return nil
}
func checkflags(token, org, user, repoURL, gistURL string) error {
if token == "" {
fmt.Println("Need a Github personal access token. Please provide that using the -token flag")
os.Exit(2)
} else if org == "" && user == "" && repoURL == "" && gistURL == "" {
fmt.Println("org, user, repoURL and gistURL can't all be empty. Please provide just one of these values")
os.Exit(2)
} else if org != "" && (user != "" || repoURL != "" || gistURL != "") {
fmt.Println("Can't have org along with any of user, repoURL or gistURL. Please provide just one of these values")
os.Exit(2)
} else if user != "" && (org != "" || repoURL != "" || gistURL != "") {
fmt.Println("Can't have user along with any of org, repoURL or gistURL. Please provide just one of these values")
os.Exit(2)
} else if repoURL != "" && (org != "" || user != "" || gistURL != "") {
fmt.Println("Can't have repoURL along with any of org, user or gistURL. Please provide just one of these values")
os.Exit(2)
} else if gistURL != "" && (org != "" || repoURL != "" || user != "") {
fmt.Println("Can't have gistURL along with any of org, user or repoURL. Please provide just one of these values")
os.Exit(2)
}
return nil
}
func makeDirectories() error {
os.MkdirAll("/tmp/repos/org", 0700)
os.MkdirAll("/tmp/repos/users", 0700)
os.MkdirAll("/tmp/repos/singlerepo", 0700)
os.MkdirAll("/tmp/repos/singlegist", 0700)
os.MkdirAll("/tmp/results/thog", 0700)
os.MkdirAll("/tmp/results/gitsecrets", 0700)
os.MkdirAll("/tmp/results/repo-supervisor", 0700)
return nil
}
func main() {
//Parsing the flags
flag.Parse()
//Logic to check the program is ingesting proper flags
err := checkflags(*token, *org, *user, *repoURL, *gistURL)
check(err)
//Authenticating to Github using the token
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: *token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
//Creating some temp directories to store repos & results. These will be deleted in the end
err = makeDirectories()
check(err)
//By now, we either have the org, user, repoURL or the gistURL. The program flow changes accordingly..
if *org != "" { //If org was supplied
Info("Since org was provided, the tool will proceed to scan all the org repos, then all the user repos and user gists in a recursive manner")
//cloning all the repos of the org
err := cloneorgrepos(ctx, client, *org)
check(err)
//getting all the users of the org into the allUsers array
allUsers, err := listallusers(ctx, client, *org)
check(err)
//iterating through the allUsers array
for _, user := range allUsers {
//cloning all the repos of a user
err1 := cloneuserrepos(ctx, client, *user.Login)
check(err1)
//cloning all the gists of a user
err2 := cloneusergists(ctx, client, *user.Login)
check(err2)
}
Info("Scanning all org repositories now..This may take a while so please be patient\n")
err = scanorgrepos(*org)
check(err)
Info("Finished scanning all org repositories\n")
Info("Scanning all user repositories and gists now..This may take a while so please be patient\n")
var wguser sync.WaitGroup
for _, user := range allUsers {
wguser.Add(1)
go scanforeachuser(*user.Login, &wguser)
}
wguser.Wait()
Info("Finished scanning all user repositories and gists\n")
} else if *user != "" { //If user was supplied
Info("Since user was provided, the tool will proceed to scan all the user repos and user gists\n")
err1 := cloneuserrepos(ctx, client, *user)
check(err1)
err2 := cloneusergists(ctx, client, *user)
check(err2)
Info("Scanning all user repositories and gists now..This may take a while so please be patient\n")
var wguseronly sync.WaitGroup
wguseronly.Add(1)
go scanforeachuser(*user, &wguseronly)
wguseronly.Wait()
Info("Finished scanning all user repositories and gists\n")
} else if *repoURL != "" || *gistURL != "" { //If either repoURL or gistURL was supplied
var url, repoorgist, fpath, rn, lastString string
var bpath = "/tmp/repos/"
if *repoURL != "" { //repoURL
url = *repoURL
repoorgist = "repo"
} else { //gistURL
url = *gistURL
repoorgist = "gist"
}
Info("The tool will proceed to clone and scan: " + url + " only\n")
splitArray := strings.Split(url, "/")
lastString = splitArray[len(splitArray)-1]
orgoruserName := splitArray[3]
switch repoorgist {
case "repo":
rn = strings.Split(lastString, ".")[0]
fpath = bpath + "singlerepo/" + rn
case "gist":
rn = lastString
fpath = bpath + "singlegist/" + lastString
}
//cloning
Info("Starting to clone: " + url + "\n")
var wgo sync.WaitGroup
wgo.Add(1)
go gitclone(url, fpath, &wgo)
wgo.Wait()
Info("Cloning of: " + url + " finished\n")
//scanning
Info("Starting to scan: " + url + "\n")
var wgs sync.WaitGroup
wgs.Add(1)
go runGitTools(*toolName, fpath+"/", &wgs, rn, orgoruserName)
wgs.Wait()
Info("Scanning of: " + url + " finished\n")
}
//Now, that all the scanning has finished, time to combine the output
Info("Combining the output into one file\n")
err = combineOutput(*toolName, *outputFile)
check(err)
}