-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (106 loc) · 2.51 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
package main
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"os"
"strings"
"time"
)
type S3Bucket struct {
Name string
Creation time.Time
}
type Trails struct {
Name string
BucketName string
Region string
MultiRegion bool
Enabled bool
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg + "\n", args...)
os.Exit(1)
}
func setRegions (r string) ([]string) {
var regions []string
if r == "all" {
resolver := endpoints.DefaultResolver()
partitions := resolver.(endpoints.EnumPartitions).Partitions()
// Ignore AWS in China and GovCloud regions
// Future feature request
for _, p := range partitions {
if p.ID() == "aws-cn"{ continue }
if p.ID() == "aws-us-gov"{ continue }
for id, _ := range p.Regions() {
regions = append(regions, id)
}
}
} else {
regions = append(regions, r)
}
return regions
}
var auditWorkers = 1
var enableEC2, enableS3, enableTrail = false, false, false
func main() {
// Parse command-line flags
awsRegion := flag.String("region", "us-west-2", "AWS region to scan")
awsService := flag.String("service", "all", "AWS service to scan")
flag.Parse()
// Scans implemented services or select
// Existing modules: EC2, S3, CloudTrail
// Defaults to all
// TODO // Implement additional services, e.g. Lambda
switch *awsService {
case "ec2":
enableEC2 = true
case "s3":
enableS3 = true
case "cloudtrail":
enableTrail = true
default:
auditWorkers = 3
fmt.Println("Auditing AWS Services: EC2, S3, CloudTrail")
enableEC2, enableS3, enableTrail = true, true, true
}
// Scans all regions or select
// Defaults to "us-west-2"
var scanRegions []string
if strings.ToLower(*awsRegion) != "all" {
scanRegions = setRegions(*awsRegion)
} else {
scanRegions = setRegions("all")
}
fmt.Println("Auditing AWS Regions:", scanRegions)
// Describe AWS credentials
// Credentials file ~/.aws/credentials
sess, _ := session.NewSession(&aws.Config{
Region: aws.String(endpoints.UsWest2RegionID),
})
fmt.Println("AWS API Identity")
// describeIdentity(sess)
worker := make(chan bool, auditWorkers)
// Scans EC2, CloudTail and S3 AWS services
// Audit logic returns JSON
if enableS3 {
go auditS3(sess, worker)
}
if enableTrail {
go auditTrails(sess, scanRegions, worker)
}
if enableEC2 {
go auditEC2(sess, worker)
}
// TODO // REFACTOR
switch auditWorkers {
case 3:
<- worker
<- worker
<- worker
default:
<- worker
}
}