Cloud Armor cho NLB/VM với các quy tắc do người dùng xác định

1. Giới thiệu

Các chính sách bảo mật của Cloud Armor được dùng để định cấu hình các quy tắc do người dùng xác định nhằm lọc lưu lượng truy cập ở rìa mạng của Google, ở phía trên cơ sở hạ tầng của bạn. Bạn có thể dùng các chính sách bảo mật của Network Edge để giúp bảo vệ và cho phép hoặc chặn lưu lượng truy cập nhắm đến các loại điểm cuối sau: Bộ cân bằng tải mạng, Chuyển tiếp giao thức và VM có địa chỉ IP công khai.

7bc9d3ed0c03b54f.png

Trong lớp học lập trình này, chúng ta sẽ minh hoạ cách định cấu hình các chính sách bảo mật Cloud Armor bằng các quy tắc do người dùng xác định để ngăn chặn các cuộc tấn công DDoS.

f0a40260147e71b1.png

Hình 1. Cloud Armor cho VM có tính năng bảo vệ IP công khai.

Kiến thức bạn sẽ học được

  • Chính sách bảo mật Cloud Armor có cấu hình quy tắc do người dùng xác định
  • Cấu hình và kiểm thử độ lệch UDP.

Bạn cần có

  • Kiến thức về TCP/IP
  • Kiến thức về dòng lệnh Unix/Linux

2. Trước khi bắt đầu

Trong Cloud Shell, hãy đảm bảo rằng bạn đã thiết lập mã dự án

gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
prodproject=YOUR-PROJECT-NAME
echo $prodproject

3. Tạo mạng VPC mục tiêu

Trong phần sau, chúng ta sẽ thiết lập mạng VPC và các cấu hình mạng liên quan. Chính sách bảo mật ở biên mạng của Cloud Armor dựa trên khu vực, chúng tôi thiết lập tất cả các tài nguyên liên quan ở khu vực asia-southeast1.

Mạng VPC

Từ Cloud Shell

gcloud compute networks create ca4nlb --project=$prodproject --subnet-mode=custom

Tạo mạng con

Từ Cloud Shell

gcloud compute networks subnets create ca4nlb-asia-southeast1 --project=$prodproject --range=10.0.0.0/24 --network=ca4nlb --region=asia-southeast1

Tạo quy tắc tường lửa.

Trong phần này, chúng ta sẽ thêm một quy tắc tường lửa để cho phép lưu lượng truy cập UDP dự kiến đến cổng 10000.

Từ Cloud Shell, hãy tạo một quy tắc tường lửa để mở cổng UDP 10000 cho quá trình kiểm thử sau đây.

gcloud compute firewall-rules create ca4nlb-udp10000 --allow udp:10000 --network ca4nlb --source-ranges 0.0.0.0/0 --enable-logging

Tạo một quy tắc tường lửa từ Cloud Shell để cho phép IAP kết nối với các thực thể máy ảo của bạn.

gcloud compute firewall-rules create ca4nlb-iap-prod --network ca4nlb --allow tcp:22 --source-ranges=35.235.240.0/20 --enable-logging

4. Tạo các phiên bản máy ảo mục tiêu

Tạo một máy ảo mục tiêu để kiểm thử các chính sách bảo mật. VM này phải có địa chỉ IP công khai và mở cổng UDP 10000.

Tạo targetvm phiên bản từ Cloud Shell

gcloud compute instances create targetvm \
--zone=asia-southeast1-b \
--image-family=debian-11 \
--image-project=debian-cloud \
--network-interface=network-tier=PREMIUM,nic-type=GVNIC,stack-type=IPV4_ONLY,subnet=ca4nlb-asia-southeast1 \
--shielded-secure-boot \
--shielded-vtpm \
--shielded-integrity-monitoring

5. Định cấu hình chế độ bảo vệ nâng cao chống DDoS cho mạng

Từ Cloud Shell

 gcloud compute security-policies create ca_advanced_ddos \
     --type CLOUD_ARMOR_NETWORK \
     --region asia-southeast1

 gcloud compute security-policies update ca_advanced_ddos \
     --network-ddos-protection ADVANCED \
     --region asia-southeast1

 gcloud compute network-edge-security-services create caedgepolicy \
     --security-policy ca_advanced_ddos \
     --region asia-southeast1

6. Tạo Chính sách bảo mật của Network Edge bằng các quy tắc mặc định

Tạo chính sách bảo mật cho mạng biên

Từ Cloud Shell

gcloud alpha compute security-policies create customnetworkedge --type=CLOUD_ARMOR_NETWORK --region=asia-southeast1

Sửa đổi quy tắc mặc định

Từ Cloud Shell

gcloud alpha compute security-policies rules update 2147483647 --security-policy=customnetworkedge --action=deny --region=asia-southeast1

7. Tạo Chính sách bảo mật cho mạng biên bằng các quy tắc do người dùng định cấu hình

Người dùng xác định trước độ lệch UDP và được định cấu hình trong chính sách Cloud Armor. Gói có "giá trị bù" này sẽ vượt qua quy trình kiểm tra chính sách và được gửi đến máy ảo phụ trợ. Trong ví dụ sau, chúng ta sẽ xác định 2 "offset" có các giá trị khác nhau.

Giá trị đầu tiên nằm ngay sau tiêu đề UDP, khớp chính xác 2 byte 0x1700

Giá trị thứ hai là độ lệch 8 byte của tiêu đề UDP, khớp chính xác với 4 byte 0x12345678

Giá trị được xác định trước ở trên sẽ chuyển thành chế độ xem bit gói UDP.

cbfdaeb93292e07b.png

Từ Cloud Shell

gcloud alpha compute security-policies add-user-defined-field customnetworkedge \
--user-defined-field-name=SIG1_AT_0 \
--base=udp --offset=8 --size=2 --mask=0xFF00 \
--region=asia-southeast1

gcloud alpha compute security-policies add-user-defined-field customnetworkedge \
--user-defined-field-name=SIG2_AT_8 \
--base=udp --offset=16 --size=4 --mask=0xFFFFFFFF \
--region=asia-southeast1

gcloud alpha compute security-policies rules create 1000 \
--security-policy=customnetworkedge \
--network-user-defined-fields="SIG1_AT_0;0x1700,SIG2_AT_8;0x12345678" \
--action=allow --region=asia-southeast1

8. Đính kèm Chính sách bảo mật vào VM mục tiêu

Từ Cloud Shell, hãy đính kèm chính sách bảo mật vào VM được bảo vệ.

gcloud alpha compute instances network-interfaces update targetvm \
--security-policy=customnetworkedge \
--security-policy-region=asia-southeast1 \
--network-interface=nic0 \
--zone=asia-southeast1-b

Từ Cloud Shell, hãy mô tả máy ảo đích, bạn sẽ thấy securityPolicy được đính kèm. Ghi lại IP công khai để kiểm thử sau này.

gcloud alpha compute instances describe targetvm --zone=asia-southeast1-b

networkInterfaces:
- accessConfigs:
  - kind: compute#accessConfig
    name: External NAT
    natIP: 35.240.148.100
    networkTier: PREMIUM
    securityPolicy: https://www.googleapis.com/compute/alpha/projects/<project>/regions/asia-southeast1/securityPolicies/customnetworkedge

Trong Cloud Shell, hãy tách chính sách bảo mật khỏi VM được bảo vệ.

gcloud alpha compute instances network-interfaces update targetvm \
--network-interface=nic0 \
--zone=asia-southeast1-b \
--security-policy= 

9. Chuẩn bị tài nguyên kiểm thử.

Tạo mạng VPC thử nghiệm

Từ Cloud Shell

gcloud compute networks create test --project=$prodproject --subnet-mode=custom

Tạo mạng con thử nghiệm

Từ Cloud Shell

gcloud compute networks subnets create test-asia-southeast1 --project=$prodproject --range=10.0.1.0/24 --network=test --region=asia-southeast1

Tạo tường lửa

Tạo một quy tắc tường lửa từ Cloud Shell để cho phép IAP kết nối với các thực thể máy ảo của bạn.

gcloud compute firewall-rules create test-iap-prod --network test --allow tcp:22 --source-ranges=35.235.240.0/20 --enable-logging

Tạo máy ảo kiểm thử

Từ Cloud Shell

gcloud compute instances create test01 \
    --zone=asia-southeast1-b \
    --image-family=debian-11 \
    --image-project=debian-cloud \
    --network-interface=network-tier=PREMIUM,nic-type=GVNIC,stack-type=IPV4_ONLY,subnet=test-asia-southeast1 \
    --shielded-secure-boot \
    --shielded-vtpm \
    --shielded-integrity-monitoring

10. Xác minh

Đăng nhập vào Bảng điều khiển VM thử nghiệm và cài đặt trình tạo gói packit.

sudo apt install packit

Theo thiết kế độ lệch UDP, hãy dùng packit để tạo các gói UDP. Chúng tôi mô phỏng một gói (-t udp) từ địa chỉ IP nguồn giao diện (-s ens4) (-s 10.0.1.2) với các cổng nguồn (-S 10000) đến địa chỉ IP đích targetVM (-d 35.240.148.100) với các cổng đích (-D 10000). Nội dung gói trùng khớp với các giá trị (-p "0x 17 00 00 00 00 00 00 00 12 34 56 78"). Chúng ta sẽ gửi (-c 4) gói.

sudo packit -m inject -t UDP -i ens4 -s 10.0.1.2 -d 35.240.148.100 -S 10000 -D 10000 -p '0x 17 00 00 00 00 00 00 00 12 34 56 78' -c 4

Trong Máy ảo đích, hãy chạy tcpdump để ghi lại gói UDP.

sudo tcpdump port 10000 -v -n 

tcpdump: listening on ens4, link-type EN10MB (Ethernet), snapshot length 262144 bytes
06:36:18.434106 IP (tos 0x0, ttl 128, id 17173, offset 0, flags [none], proto UDP (17), length 40)
    35.197.157.140.10000 > 10.0.0.2.10000: UDP, length 12
06:36:19.433656 IP (tos 0x0, ttl 128, id 55641, offset 0, flags [none], proto UDP (17), length 40)
    35.197.157.140.10000 > 10.0.0.2.10000: UDP, length 12
06:36:20.433935 IP (tos 0x0, ttl 128, id 27161, offset 0, flags [none], proto UDP (17), length 40)
    35.197.157.140.10000 > 10.0.0.2.10000: UDP, length 12
06:36:21.434150 IP (tos 0x0, ttl 128, id 46782, offset 0, flags [none], proto UDP (17), length 40)
    35.197.157.140.10000 > 10.0.0.2.10000: UDP, length 12

Nếu thay đổi mẫu lưu lượng truy cập trong VM thử nghiệm, chúng ta sẽ không thể ghi lại bất kỳ gói nào trong VM mục tiêu.

sudo packit -m inject -t UDP -i ens4 -s 10.148.0.6 -d 34.87.79.31 -S 10000 -D 10000 -p '0x 33 33 00 00 00 00 00 00 12 34 56 78' -c 4

11. Telemetry

Mở Cloud Metric, sử dụng MQL bên dưới để truy vấn dữ liệu đo từ xa NetworkSercurityPolicy.

fetch networksecurity.googleapis.com/RegionalNetworkSecurityPolicy
| metric 'networksecurity.googleapis.com/l3/external/packet_count'
| filter (resource.policy_name == 'customnetworkedge')
| align rate(1m)
| every 1m
| group_by [metric.blocked], [value_packet_count_mean: mean(value.packet_count)]
| group_by 1m, [value_packet_count_mean_mean: mean(value_packet_count_mean)]
| every 1m

Tạo lưu lượng truy cập lớn bằng lệnh bù khớp.

sudo packit -m inject -t UDP -i ens4 -s 10.148.0.6 -d 34.87.79.31 -S 10000 -D 10000 -p '0x 17 00 00 00 00 00 00 00 12 34 56 78' -c 1000000 -w 0.001

[result]
Injected: 1000000  Packets/Sec: 10309.27  Bytes/Sec: 412371.13  Errors: 0

Tạo lưu lượng truy cập lớn bằng lệnh bù đắp không khớp.

sudo packit -m inject -t UDP -i ens4 -s 10.148.0.6 -d 34.87.79.31 -S 10000 -D 10000 -p '0x 11 00 00 00 00 00 00 00 12 34 56 78' -c 1000000 -w 0.001

[result]
Injected: 1000000  Packets/Sec: 10309.27  Bytes/Sec: 412371.13  Errors: 0

Dữ liệu đo từ xa được lọc theo policy_name và được nhóm theo trạng thái bị chặn. Đường màu xanh dương cho biết lưu lượng truy cập được phép theo các quy tắc của chính sách. Đường màu xanh lục cho biết lưu lượng truy cập bị chặn theo các quy tắc chính sách.

b11ba15d87f99775.png

12. Các bước dọn dẹp

Xoá các thành phần trong phòng thí nghiệm khỏi một Cloud Shell duy nhất trong thiết bị đầu cuối

gcloud compute instances delete targetvm --zone=asia-southeast1-b

gcloud compute firewall-rules delete ca4nlb-udp10000

gcloud compute firewall-rules delete ca4nlb-iap-prod

gcloud compute networks subnets delete ca4nlb-asia-southeast1 --region=asia-southeast1

gcloud compute networks delete ca4nlb

gcloud alpha compute security-policies delete customnetworkedge --region=asia-southeast1

gcloud alpha compute network-edge-security-services delete caedgepolicy --region=asia-southeast1

gcloud alpha compute security-policies delete ca_advanced_ddos --region=asia-southeast1

gcloud compute instances delete test01 --zone=asia-southeast1-b

gcloud compute firewall-rules delete test-iap-prod

gcloud compute networks subnets delete test-asia-southeast1 --region=asia-southeast1

gcloud compute networks delete test

13. Xin chúc mừng!

Chúc mừng bạn đã hoàn thành lớp học lập trình này.

Nội dung đã đề cập

  • Chính sách bảo mật Cloud Armor có các quy tắc do khách hàng xác định