forked from mondoohq/cnspec-policies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mondoo-gcp-security.mql.yaml
715 lines (547 loc) · 25 KB
/
mondoo-gcp-security.mql.yaml
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# Copyright (c) Mondoo, Inc.
# SPDX-License-Identifier: BUSL-1.1
policies:
- uid: mondoo-gcp-security
name: Google Cloud (GCP) Security
version: 1.1.0
license: BUSL-1.1
tags:
mondoo.com/category: security
mondoo.com/platform: gcp,cloud
authors:
- name: Mondoo, Inc
email: [email protected]
docs:
desc: |-
## Overview
Google Cloud Security by Mondoo provides guidance for establishing minimum recommended security and operational best practices for Google Cloud.
## Remote scan
Remote scans use cnspec providers to retrieve on-demand scan results without having to install any agents.
For a complete list of providers run:
```bash
cnspec scan --help
```
### Prerequisites
Remote scans of Google Cloud Projects requires API credentials with access to the project.
### Scan a GCP project
Open a terminal and authenticate with Google Cloud:
```bash
gcloud auth login
```
Run a scan of a GCP project:
```bash
cnspec scan gcp
```
To target a specific project:
```bash
gcloud config set project <project_id>
```
```bash
cnspec scan gcp
```
## Join the community!
Our goal is to build policies that are simple to deploy, accurate, and actionable.
If you have any suggestions for how to improve this policy, or if you need support, [join the community](https://github.com/orgs/mondoohq/discussions) in GitHub Discussions.
groups:
- title: GCP Project
filters: |
asset.family.contains("google")
asset.runtime == "gcp"
checks:
- uid: mondoo-gcp-security-block-project-wide-ssh-keys-enabled-vm-instances
- uid: mondoo-gcp-security-cloud-storage-bucket-not-anonymously-publicly-accessible
- uid: mondoo-gcp-security-cloud-storage-buckets-have-uniform-bucket-level-access-enabled
- uid: mondoo-gcp-security-instances-are-not-configured-use-default-service-account
- uid: mondoo-gcp-security-instances-not-configured-with-default-service-account-full-access-cloud-api
- uid: mondoo-gcp-security-oslogin-enabled-project
scoring_system: highest impact
queries:
- uid: mondoo-gcp-security-instances-are-not-configured-use-default-service-account
title: Ensure that instances are not configured to use the default service account
impact: 95
variants:
- uid: gcp-compute-instances-configured-use-default-service-account-all
- uid: gcp-compute-instances-configured-use-default-service-account-single
docs:
desc: |
New projects that have enabled the Compute Engine API have a Compute Engine default service account, which contains the following email pattern:
```bash
```
The Compute Engine default service account is created with the IAM basic Editor role, but you can modify your service account's roles to control the service account's access to Google APIs.
You can disable or delete this service account from your project, but doing so might cause any applications that depend on the service account's credentials to fail. If you accidentally delete the Compute Engine default service account, you can try to recover the account within 30 days. For more information, see Creating and managing service accounts.
It is recommended that you do not configure instances with the default service account. Instead, create a user account using the principle of least privilege.
audit: |
__cnquery run__
To audit your Google Cloud Project with `cnquery run`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Run this query:
```mql
cnquery run gcp project <project-id> -c "gcp.compute.instances.where( name != /^gke/ ) {id name serviceAccounts.where( email == /^.*compute@developer\.gserviceaccount\.com$/ )}"
```
__cnquery shell__
To audit your Google Cloud Project with `cnquery shell`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Launch `cnquery shell`:
```bash
cnquery run gcp project <project-id>
```
3. Run this query:
```mql
gcp.compute.instances.where( name != /^gke/ ) {id name serviceAccounts.where(email == /^.*compute@developer\.gserviceaccount\.com$/ )}
```
remediation: |
### Terraform
To provision or update a compute instance with a custom service account:
```hcl
resource "google_compute_instance" "default" {
name = "secure-instance"
machine_type = var.machine_type
zone = var.zone
tags = ["terraform"]
service_account {
email = "[email protected]"
scopes = ["user-email", "compute-ro", "storage-ro"]
}
}
```
### Google Cloud Console
To change the policy using the GCP Console, follow these steps:
1. Log in to the GCP Console at https://console.cloud.google.com.
2. Select the Organization and Project where the instance you want to update is running.
3. Navigate to **Compute Engine**.
4. Select the compute instance that you want to update.
5. If the instance is not stopped, select **Stop**. Wait for the instance to stop.
6. Select **Edit**.
7. Scroll down to the Service Account section.
8. Select a different service account.
9. Select **Save**.
10. Select **START**.
### gcloud cli
To update the service account using the `gcloud` cli:
1. Stop the instance:
```bash
gcloud compute instances stop INSTANCE_NAME
```
2. Update the instance:
```bash
gcloud compute instances set-service-account INSTANCE_NAME --serviceaccount=SERVICE_ACCOUNT --scopes [SCOPE1, SCOPE2...]
```
3. Restart the instance:
```bash
gcloud compute instances start INSTANCE_NAME
```
- uid: gcp-compute-instances-configured-use-default-service-account-all
filters: asset.platform == "gcp" || asset.platform == "gcp-project"
mql: |
gcp.compute.instances.where(name != /^gke-/).all(
serviceAccounts.none(
email == /^.*compute@developer\.gserviceaccount\.com$/
)
)
- uid: gcp-compute-instances-configured-use-default-service-account-single
filters: |
asset.platform == "gcp-compute-instance"
gcp.compute.instance.name != /^gke-/
mql: |
gcp.compute.instance.serviceAccounts.none(
email == /^.*compute@developer\.gserviceaccount\.com$/
)
- uid: mondoo-gcp-security-instances-not-configured-with-default-service-account-full-access-cloud-api
title: Ensure instances are not configured to use the default service account with full access to all Cloud APIs
impact: 90
variants:
- uid: gcp-compute-instances-configured-use-default-service-account-full-access-all-cloud-all
- uid: gcp-compute-instances-configured-use-default-service-account-full-access-all-cloud-single
docs:
desc: |
Google compute instances provisioned with full access to all cloud APIs pose a security risk to a GCP environment. Instances should instead be provisioned using a non-default service account, and limited permissions to cloud APIs using the principle of least privilege.
audit: |
__cnquery run__
To audit your Google Cloud Project with `cnquery run`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Run this query:
```mql
cnquery run gcp project <project-id> -c "gcp.compute.instances.where( name != /^gke/ ) {id name serviceAccounts.where( email == /^.*compute@developer\.gserviceaccount\.com$/ ) {email scopes}}"
```
__cnquery shell__
To audit your Google Cloud Project with `cnquery shell`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Launch `cnquery shell`:
```bash
cnquery shell gcp
```
3. Run this query:
```mql
gcp.compute.instances.where(name != /^gke/) {id name serviceAccounts.where(email == /^.*compute@developer\.gserviceaccount\.com$/) {email scopes}}
```
remediation: |
### Terraform
To provision or update a compute instance with Terraform:
```hcl
resource "google_compute_instance" "default" {
name = "secure-instance"
machine_type = var.machine_type
zone = var.zone
tags = ["terraform"]
service_account {
# Google recommends custom service accounts with cloud-platform scope and permissions granted via IAM Roles.
email = google_service_account.default.email
scopes = ["cloud-platform"]
}
}
```
### Google Cloud Console
To change the policy using the Google Cloud Console:
1. Log in to the GCP Console at https://console.cloud.google.com.
2. Select the Organization and Project where the instance you want to update is running.
3. Navigate to **Compute Engine**.
4. Select the compute instance that you want to update.
5. If the instance is not stopped, select **Stop**. Wait for the instance to stop.
6. Select **Edit**.
7. Scroll down to the Service Account section.
8. Select a different service account or ensure Allow full access to all Cloud APIs is not selected.
9. Select **Save**.
10. Select **START**.
### gcloud cli
To update the service account using the `gcloud` cli:
1. Stop the instance:
```bash
gcloud compute instances stop INSTANCE_NAME
```
2. Update the instance:
```bash
gcloud compute instances set-service-account INSTANCE_NAME --serviceaccount=SERVICE_ACCOUNT --scopes [SCOPE1, SCOPE2...]
```
3. Restart the instance:
```bash
gcloud compute instances start INSTANCE_NAME
```
- uid: gcp-compute-instances-configured-use-default-service-account-full-access-all-cloud-all
filters: asset.platform == "gcp" || asset.platform == "gcp-project"
mql: |
gcp.compute.instances.where(name != /^gke-/).all(
serviceAccounts.none(
scopes == "https://www.googleapis.com/auth/cloud-platform"
)
)
- uid: gcp-compute-instances-configured-use-default-service-account-full-access-all-cloud-single
filters: |
asset.platform == "gcp-compute-instance"
gcp.compute.instance.name != /^gke-/
mql: |
gcp.compute.instance.serviceAccounts.none(
scopes == "https://www.googleapis.com/auth/cloud-platform"
)
- uid: mondoo-gcp-security-block-project-wide-ssh-keys-enabled-vm-instances
title: Ensure "Block Project-wide SSH keys" is enabled for VM instances
impact: 70
variants:
- uid: gcp-compute-block-project-wide-ssh-keys-enabled-vm-instances-all
- uid: gcp-compute-block-project-wide-ssh-keys-enabled-vm-instances-single
docs:
desc: |
Project-wide SSH keys can be used to login into all instances within a project. While using project-wide SSH keys eases SSH key management, if SSH keys are compromised, the potential security risk can impact all instances within a project.
The recommended approach is to use instance-specific SSH keys instead of common/shared project-wide SSH keys.
audit: |
__cnquery run__
To audit your Google Cloud Project with `cnquery run`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Run this query:
```mql
cnquery run gcp -c "gcp.compute.instances {id name metadata['block-project-ssh-keys'] }"
```
__cnquery shell__
To audit your Google Cloud Project with `cnquery shell`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Launch `cnquery shell`:
```bash
cnquery shell gcp
```
3. Run this query:
```mql
gcp.compute.instances {id name metadata['block-project-ssh-keys'] }
```
remediation: |
### Terraform
To provision or update a compute instance with Terraform:
```hcl
resource "google_compute_instance" "default" {
name = "secure-instance"
machine_type = var.machine_type
zone = var.zone
tags = ["terraform"]
metadata = {
block-project-ssh-keys = true
}
}
```
### Google Cloud Console
To change the policy using the GCP Console:
1. Log in to the GCP Console at https://console.cloud.google.com.
2. Select the organization/project where the instance(s) you want to update are running.
3. Navigate to **Compute Engine**.
4. Select the instance you want to update.
5. Select **EDIT** in the toolbar.
6. Under the **Security and access** section, select the **Block project-wide SSH keys** option.
7. Select **SAVE**.
Repeat these steps for each impacted Instance.
### gcloud cli
To update an instance using the `gcloud` cli:
1. Update the instance:
```bash
gcloud compute instances add-metadata INSTANCE_NAME --metadata block-projectssh-keys=TRUE
```
- uid: gcp-compute-block-project-wide-ssh-keys-enabled-vm-instances-all
filters: asset.platform == "gcp" || asset.platform == "gcp-project"
mql: |
gcp.compute.instances.all(
metadata['block-project-ssh-keys'] == true
)
- uid: gcp-compute-block-project-wide-ssh-keys-enabled-vm-instances-single
filters: asset.platform == "gcp-compute-instance"
mql: |
gcp.compute.instance.metadata['block-project-ssh-keys'] == true
- uid: mondoo-gcp-security-oslogin-enabled-project
title: Ensure oslogin is enabled for compute instances
impact: 70
variants:
- uid: gcp-compute-oslogin-enabled-project-all
- uid: gcp-compute-oslogin-enabled-project-single
docs:
desc: |
OS Login lets you use Compute Engine Identity and Access Management (IAM) roles to grant or revoke SSH access to your Linux instances. OS Login is an alternative to managing instance access by adding and removing SSH keys in metadata.
audit: |
__cnquery run__
To audit your Google Cloud Project with `cnquery run`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Run this query:
```mql
cnquery run gcp project <project-id> -c "gcp.compute.instances {id name metadata['enable-oslogin'] == true}"
```
__cnquery shell__
To audit your Google Cloud Project with `cnquery shell`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Launch `cnquery shell`:
```bash
cnquery shell gcp project <project-id>
```
3. Run this query:
```mql
gcp.compute.instances {id name metadata['enable-oslogin'] }
```
remediation: |
### Terraform
To configure OS Login for a project:
```hcl
resource "google_compute_project_metadata" "default" {
metadata = {
enable-oslogin = "TRUE"
}
}
```
To provision or update a compute instance with Terraform:
```hcl
resource "google_compute_instance" "default" {
name = "secure-instance"
machine_type = var.machine_type
zone = var.zone
tags = ["terraform"]
metadata = {
enable-oslogin = true
}
}
```
### Google Cloud Console
To configure OS Login for a project via Google Cloud Console:
1. In the Google Cloud console, go to the **Metadata** page.
2. Select **EDIT**.
3. Add a metadata entry, setting the key to `enable-oslogin` and the value to `TRUE`.
4. Select **SAVE** to apply the changes.
To configure OS Login for an existing instance:
1. In the Google Cloud console, go to the **Compute Engine**.
2. Select the name of the instance that you want to enable OS Login on.
3. On the instance details page, select **EDIT**.
4. Under **Custom metadata**, add a metadata entry, setting the key to `enable-oslogin` and the value to `TRUE`.
5. Select **SAVE**.
### gcloud cli
To update OS Login for a project using the `gcloud` cli:
```bash
gcloud compute project-info add-metadata --metadata enable-oslogin=TRUE
```
To update OS Login for an existing instance using the `gcloud` cli:
```bash
gcloud compute instances add-metadata INSTANCE_NAME --metadata enable-oslogin=TRUE
```
- uid: gcp-compute-oslogin-enabled-project-all
filters: asset.platform == "gcp" || asset.platform == "gcp-project"
mql: |
gcp.compute.instances.all(
metadata['enable-oslogin'] == true
)
- uid: gcp-compute-oslogin-enabled-project-single
filters: asset.platform == "gcp-compute-instance"
mql:
gcp.compute.instance.metadata['enable-oslogin'] == true
- uid: mondoo-gcp-security-cloud-storage-bucket-not-anonymously-publicly-accessible
title: Ensure that Cloud Storage buckets are not anonymously or publicly accessible
impact: 90
variants:
- uid: gcp-storage-cloud-storage-bucket-anonymously-publicly-accessible-all
- uid: gcp-storage-cloud-storage-bucket-anonymously-publicly-accessible-single
docs:
desc: |
Public access prevention protects Cloud Storage buckets and objects from being accidentally exposed to the public. When you enforce public access prevention, no one can make data in applicable buckets public through IAM policies or ACLs.
audit: |
__cnquery run__
To audit your Google Cloud Project with `cnquery run`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Run this query:
```mql
cnquery run gcp -c "gcloud.storage.buckets { iamPolicy { members {*} } } "
```
__cnquery shell__
To audit your Google Cloud Project with `cnquery shell`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Launch `cnquery shell`:
```bash
cnquery shell gcp
```
3. Run this query:
```mql
gcloud.storage.buckets { iamPolicy { members {*} } }
```
remediation: |
### Terraform
To update public access configuration using Terraform, ensure `allUsers` and `allAuthenticatedUsers` are not set:
```hcl
resource "google_storage_bucket_iam_binding" "binding" {
bucket = google_storage_bucket.default.name
role = "roles/storage.admin"
members = [
"user:[email protected]",
]
}
```
```hcl
resource "google_storage_bucket_iam_member" "member" {
bucket = google_storage_bucket.default.name
role = "roles/storage.admin"
member = "user:[email protected]"
}
```
### Google Cloud Console
1. In the Google Cloud console, go to the **Cloud Storage Bucket** page.
2. For the bucket you want to enforce public access prevention on, select the more actions menu.
3. Select **Edit access** from the drop-down menu.
4. In the Public access card, select **Prevent public access** to enforce public access prevention.
5. Select **Confirm**.
### gcloud cli
Update an existing storage bucket with the `gcloud` cli:
```bash
gcloud storage buckets update gs://BUCKET_NAME --no-pap
```
- uid: gcp-storage-cloud-storage-bucket-anonymously-publicly-accessible-all
filters: asset.platform == "gcp" || asset.platform == "gcp-project"
mql: |
gcp.storage.buckets.all(iamPolicy.all(members.none(_ == "allUsers")))
gcp.storage.buckets.all(iamPolicy.all(members.none(_ == "allAuthenticatedUsers")))
- uid: gcp-storage-cloud-storage-bucket-anonymously-publicly-accessible-single
filters: asset.platform == "gcp-storage-bucket"
mql: |
gcp.storage.bucket.iamPolicy.all(members.none(_ == "allUsers"))
gcp.storage.bucket.iamPolicy.all(members.none(_ == "allAuthenticatedUsers"))
- uid: mondoo-gcp-security-cloud-storage-buckets-have-uniform-bucket-level-access-enabled
title: Ensure that Cloud Storage buckets have uniform bucket-level access enabled
impact: 60
variants:
- uid: gcp-storage-cloud-storage-buckets-uniform-bucket-level-access-enabled-all
- uid: gcp-storage-cloud-storage-buckets-uniform-bucket-level-access-enabled-single
docs:
desc: |
Cloud Storage offers two systems for granting users permission to access your buckets and objects: IAM and Access Control Lists (ACLs). These systems act in parallel - in order for a user to access a Cloud Storage resource, only one of the systems needs to grant the user permission. IAM is used throughout Google Cloud and allows you to grant a variety of permissions at the bucket and project levels. ACLs are used only by Cloud Storage and have limited permission options, but they allow you to grant permissions on a per-object basis.
It is recommended to enable uniform bucket-level access on Cloud Storage buckets. Uniform bucket-level access is used to unify and simplify how you grant access to your Cloud Storage resources. Cloud Storage offers two systems that act in parallel to grant users permission to access buckets and objects:
audit: |
__cnquery run__
To audit your Google Cloud Project with `cnquery run`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Run this query:
```bash
cnquery run gcp -c "gcloud.storage.buckets { iamConfiguration['UniformBucketLevelAccess']['Enabled'] }"
```
__cnquery shell__
To audit your Google Cloud Project with `cnquery shell`:
1. Ensure the `gcloud` cli is configured to the GCP project:
```bash
gcloud config set project <project_id>
```
2. Launch `cnquery shell`:
```bash
cnquery shell gcp
```
3. Run this query:
```mql
gcloud.storage.buckets { iamConfiguration['UniformBucketLevelAccess']['Enabled'] }
```
remediation: |
### Terraform
```hcl
resource "google_storage_bucket" "example" {
name = "test-bucket"
bucket_policy_only = true
uniform_bucket_level_access = true
}
```
### Google Cloud Console
1. In the Google Cloud console, go to the **Cloud Storage Buckets** page.
2. In the list of buckets, select the name of the desired bucket.
3. Select the **Permissions** tab near the top of the page.
4. In the text box named **Access Control**, select the **Switch to** link. Note that the text box disappears 90 days after you enable uniform bucket-level access.
5. In the pop-up menu that appears, select **Fine-grained**.
6. Select **Save**.
### gcloud cli
```bash
gsutil uniformbucketlevelaccess set STATE gs://BUCKET_NAME
```
- uid: gcp-storage-cloud-storage-buckets-uniform-bucket-level-access-enabled-all
filters: asset.platform == "gcp" || asset.platform == "gcp-project"
mql: |
gcp.storage.buckets.all(
iamConfiguration.UniformBucketLevelAccess.enabled == true
)
- uid: gcp-storage-cloud-storage-buckets-uniform-bucket-level-access-enabled-single
filters: asset.platform == "gcp-storage-bucket"
mql: gcp.storage.bucket.iamConfiguration.UniformBucketLevelAccess.enabled == true