-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
306 lines (280 loc) · 10.3 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
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
package main
import (
"context"
"fmt"
"log"
"os"
accountAudit "aws-security-hub/audit/account"
apigatewayAudit "aws-security-hub/audit/apigateway"
cloudfrontChecker "aws-security-hub/audit/cloudfront"
documentdbChecker "aws-security-hub/audit/documentdb"
ec2Checker "aws-security-hub/audit/ec2"
s3Checker "aws-security-hub/audit/s3"
"aws-security-hub/types"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func initAWSClient() (*types.AWSClient, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(viper.GetString("aws_region")),
)
if err != nil {
return nil, fmt.Errorf("unable to load SDK config: %v", err)
}
return &types.AWSClient{Config: cfg}, nil
}
var rootCmd = &cobra.Command{
Use: "audit",
Short: "Audit your AWS resources",
}
// CloudFront.1
var checkCloudfrontDefaultRootObjectConfiguredCmd = &cobra.Command{
Use: "cloudfront-default-root-object-configured",
Short: "CloudFront distributions should have a default root object configured",
Aliases: []string{"cloudfront.1"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := cloudfrontChecker.CheckCloudfrontDefaultRootObjectConfigured(client.Config)
// Print Result
log.Printf("[CloudFront.1] %s", result)
},
}
// CloudFront.3
var checkCloudfrontViewerPolicyHttpsCmd = &cobra.Command{
Use: "cloudfront-viewer-policy-https",
Short: "CloudFront distributions should require encryption in transit",
Aliases: []string{"cloudfront.3"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := cloudfrontChecker.CheckCloudfrontViewerPolicyHttps(client.Config)
// Print Result
log.Printf("[CloudFront.3] %s", result)
},
}
// CloudFront.4
var checkCloudfrontOriginFailoverEnabledCmd = &cobra.Command{
Use: "cloudfront-origin-failover-enabled",
Short: "CloudFront distributions should have origin failover enabled",
Aliases: []string{"cloudfront.4"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := cloudfrontChecker.CheckCloudfrontOriginFailoverEnabled(client.Config)
// Print Result
log.Printf("[CloudFront.4] %s", result)
},
}
// CloudFront.5
var checkCloudfrontAccesslogsEnabledCmd = &cobra.Command{
Use: "cloudfront-accesslogs-enabled",
Short: "CloudFront distributions should have access logging enabled",
Aliases: []string{"cloudfront.5"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := cloudfrontChecker.CheckCloudfrontAccesslogsEnabled(client.Config)
// Print Result
log.Printf("[CloudFront.5] %s", result)
},
}
// CloudFront.12
var checkCloudfrontS3OriginNonExistentBucketCmd = &cobra.Command{
Use: "cloudfront-s3-origin-non-existent-bucket",
Short: "CloudFront distributions should not point to non-existent S3 origins",
Aliases: []string{"cloudfront.12"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := cloudfrontChecker.CheckCloudfrontS3OriginNonExistentBucket(client.Config)
// Print Result
log.Printf("[CloudFront.12] %s", result)
},
}
// CloudFront.13
var checkCloudfrontS3OriginAccessControlEnabledCmd = &cobra.Command{
Use: "cloudfront-s3-origin-access-control-enabled",
Short: "CloudFront distributions should use origin access control",
Aliases: []string{"cloudfront.13"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := cloudfrontChecker.CheckCloudfrontS3OriginAccessControlEnabled(client.Config)
// Print Result
log.Printf("[CloudFront.13] %s", result)
},
}
// CloudFront.14
var checkTaggedCloudfrontDistributionCmd = &cobra.Command{
Use: "tagged-cloudfront-distribution",
Short: "CloudFront distributions should be tagged",
Aliases: []string{"cloudfront.14"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := cloudfrontChecker.CheckTaggedCloudfrontDistribution(client.Config)
// Print Result
log.Printf("[CloudFront.14] %s", result)
},
}
// DocumentDB.1
var checkDocdbClusterEncryptedCmd = &cobra.Command{
Use: "docdb-cluster-encrypted",
Short: "Amazon DocumentDB clusters should be encrypted at rest",
Aliases: []string{"documentdb.1"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := documentdbChecker.CheckDocdbClusterEncrypted(client.Config)
// Print Result
log.Printf("[DocumentDB.1] %s", result)
},
}
// DocumentDB.2
var checkDocdbClusterBackupRetentionCheckCmd = &cobra.Command{
Use: "docdb-cluster-backup-retention-check",
Short: "Amazon DocumentDB clusters should have an adequate backup retention period",
Aliases: []string{"documentdb.2"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := documentdbChecker.CheckDocdbClusterBackupRetentionCheck(client.Config)
// Print Result
log.Printf("[DocumentDB.2] %s", result)
},
}
// DocumentDB.3
var checkDocdbClusterSnapshotPublicProhibitedCmd = &cobra.Command{
Use: "docdb-cluster-snapshot-public-prohibited",
Short: "Amazon DocumentDB manual cluster snapshots should not be public",
Aliases: []string{"documentdb.3"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := documentdbChecker.CheckDocdbClusterSnapshotPublicProhibited(client.Config)
// Print Result
log.Printf("[DocumentDB.3] %s", result)
},
}
// DocumentDB.4
var CheckDocdbClusterAuditLoggingEnabledCmd = &cobra.Command{
Use: "docdb-cluster-audit-logging-enabled",
Short: "Amazon DocumentDB clusters should publish audit logs to Amazon CloudWatch Logs",
Aliases: []string{"documentdb.4"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := documentdbChecker.CheckDocdbClusterAuditLoggingEnabled(client.Config)
// Print Result
log.Printf("[DocumentDB.4] %s", result)
},
}
// DocumentDB.5
var checkDocdbClusterDeletionProtectionEnabledCmd = &cobra.Command{
Use: "docdb-cluster-deletion-protection-enabled",
Short: "Amazon DocumentDB clusters should have deletion protection enabled",
Aliases: []string{"documentdb.5"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := documentdbChecker.CheckDocdbClusterDeletionProtectionEnabled(client.Config)
// Print Result
log.Printf("[DocumentDB.5] %s", result)
},
}
// EC2.1
var checkEbsSnapshotPublicRestorableCheckCmd = &cobra.Command{
Use: "ebs-snapshot-public-restorable-check",
Short: "Check EBS snapshots for public restorability",
Aliases: []string{"ec2.1"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
ec2Client := ec2.NewFromConfig(client.Config)
result := ec2Checker.CheckEbsSnapshotPublicRestorableCheck(ec2Client)
log.Printf("[EC2.1] %s", result)
},
}
// S3.1
var checkS3AccountLevelPublicAccessBlocksPeriodicCmd = &cobra.Command{
Use: "s3-account-level-public-access-blocks-periodic",
Short: "S3 general purpose buckets should have block public access settings enabled",
Aliases: []string{"s3.1"},
Run: func(cmd *cobra.Command, args []string) {
client, err := initAWSClient()
if err != nil {
log.Fatalf("Failed to initialize AWS client: %v", err)
}
result := s3Checker.CheckS3AccountLevelPublicAccessBlocksPeriodic(client.Config)
log.Printf("[S3.1] %s", result)
},
}
func init() {
// Set default AWS region to South Korea (ap-northeast-2)
viper.SetDefault("aws_region", "ap-northeast-2")
viper.BindEnv("aws_access_key_id")
viper.BindEnv("aws_secret_access_key")
viper.BindEnv("aws_region")
viper.SetConfigFile(".env")
err := viper.ReadInConfig()
if err != nil {
fmt.Printf("[*] No config file found, using environment variables.\n")
}
// Amazon account controls
for _, cmd := range accountAudit.GetCommands(initAWSClient) {
rootCmd.AddCommand(cmd)
}
// Amazon API Gateway controls
for _, cmd := range apigatewayAudit.GetCommands(initAWSClient) {
rootCmd.AddCommand(cmd)
}
rootCmd.AddCommand(checkCloudfrontDefaultRootObjectConfiguredCmd) // CloudFront.1
rootCmd.AddCommand(checkCloudfrontViewerPolicyHttpsCmd) // CloudFront.3
rootCmd.AddCommand(checkCloudfrontOriginFailoverEnabledCmd) // CloudFront.4
rootCmd.AddCommand(checkCloudfrontAccesslogsEnabledCmd) // CloudFront.5
rootCmd.AddCommand(checkCloudfrontS3OriginNonExistentBucketCmd) // CloudFront.12
rootCmd.AddCommand(checkCloudfrontS3OriginAccessControlEnabledCmd) // CloudFront.13
rootCmd.AddCommand(checkTaggedCloudfrontDistributionCmd) // CloudFront.14
rootCmd.AddCommand(checkDocdbClusterEncryptedCmd) // DocumentDB.1
rootCmd.AddCommand(checkDocdbClusterBackupRetentionCheckCmd) // DocumentDB.2
rootCmd.AddCommand(checkDocdbClusterSnapshotPublicProhibitedCmd) // DocumentDB.3
rootCmd.AddCommand(CheckDocdbClusterAuditLoggingEnabledCmd) // DocumentDB.4
rootCmd.AddCommand(checkDocdbClusterDeletionProtectionEnabledCmd) // DocumentDB.5
rootCmd.AddCommand(checkEbsSnapshotPublicRestorableCheckCmd) // EC2.1
rootCmd.AddCommand(checkS3AccountLevelPublicAccessBlocksPeriodicCmd) // S3.1
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}