forked from oscar-marroquin/google-cloud-armor-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud-armor-policy.tf
220 lines (197 loc) · 5.18 KB
/
cloud-armor-policy.tf
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
##################################################################################
# Cloud Armor Policy to protect against OWASP 10 and another one custom rules
##################################################################################
##################################################################################
# VARIABLES
##################################################################################
variable "project" {}
variable "company_name" {}
variable "business_unit" {}
variable "environment_tag" {}
variable "admin_path" {}
variable "ip_blacklist" {}
##################################################################################
# CLOUD ARMOR POLICY
##################################################################################
resource "random_integer" "rand" {
min = 10000
max = 99999
}
resource "google_compute_security_policy" "owasp_10_policy" {
name = local.policy_name
description = "Policy for external users."
rule {
action = "allow"
priority = "3000"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["${local.ifconfig_co_json.ip}/32"]
}
}
description = "Whitelist Rule to allow known source IP addresses."
}
rule {
action = "deny(404)"
priority = "3500"
match {
expr {
expression = "request.path.matches('${var.admin_path}') && !inIpRange(origin.ip, '${local.ifconfig_co_json.ip}/32')"
}
}
description = "Allow only admins to manage the website."
}
rule {
action = "deny(404)"
priority = "4900"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = [var.ip_blacklist]
}
}
description = "Blacklist Rule to block known source IP addresses."
}
rule {
action = "deny(404)"
priority = "5900"
match {
expr {
expression = "evaluatePreconfiguredExpr('xss-stable')"
}
}
description = "Protect against Cross-site scripting."
}
rule {
action = "deny(404)"
priority = "5800"
match {
expr {
expression = "evaluatePreconfiguredExpr('sqli-stable')"
}
}
description = "Protect against SQL Injection."
}
rule {
action = "deny(404)"
priority = "5700"
match {
expr {
expression = "evaluatePreconfiguredExpr('lfi-stable')"
}
}
description = "Protect against Local file inclusion."
}
rule {
action = "deny(404)"
priority = "5600"
match {
expr {
expression = "evaluatePreconfiguredExpr('rfi-stable')"
}
}
description = "Protect against Remote file inclusion."
}
rule {
action = "deny(404)"
priority = "5500"
match {
expr {
expression = "evaluatePreconfiguredExpr('rce-stable')"
}
}
description = "Protect against Remote code execution."
}
rule {
action = "deny(404)"
priority = "5400"
match {
expr {
expression = "evaluatePreconfiguredExpr('scannerdetection-stable')"
}
}
description = "Protect against Scanner detection."
}
rule {
action = "deny(404)"
priority = "5300"
match {
expr {
expression = "evaluatePreconfiguredExpr('protocolattack-stable')"
}
}
description = "Protect against Protocol attack."
}
rule {
action = "deny(404)"
priority = "5200"
match {
expr {
expression = "evaluatePreconfiguredExpr('php-stable')"
}
}
description = "Protect against PHP Injection."
}
rule {
action = "deny(404)"
priority = "5100"
match {
expr {
expression = "evaluatePreconfiguredExpr('sessionfixation-stable')"
}
}
description = "Protect against Session Fixation."
}
rule {
action = "deny(404)"
priority = "6900"
match {
expr {
expression = "origin.region_code == 'CN' || origin.region_code == 'RU' || origin.region_code == 'AF' || origin.region_code == 'CD' || origin.region_code == 'CG'"
}
}
description = "Block traffic from known malicious countries."
}
rule {
action = "deny(404)"
priority = "6899"
match {
expr {
expression = "origin.region_code == 'CZ' || origin.region_code == 'HK' || origin.region_code == 'IR' || origin.region_code == 'IQ'"
}
}
description = "Block traffic from known malicious countries."
}
rule {
action = "deny(404)"
priority = "6898"
match {
expr {
expression = "origin.region_code == 'KZ' || origin.region_code == 'KP' || origin.region_code == 'SO'"
}
}
description = "Block traffic from known malicious countries."
}
rule {
action = "allow"
priority = "10000"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["0.0.0.0/0"]
}
}
description = "Allow traffic from internet."
}
rule {
action = "deny(404)"
priority = "2147483647"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["*"]
}
}
description = "Default rule, higher priority overrides it."
}
}