1. บทนำ
นโยบายความปลอดภัยของ Cloud Armor ใช้เพื่อกำหนดค่ากฎที่ผู้ใช้กำหนดเพื่อกรองการรับส่งข้อมูลที่ Edge ของเครือข่าย Google ซึ่งอยู่ต้นทางจากโครงสร้างพื้นฐานของคุณ คุณใช้นโยบายความปลอดภัยของ Network Edge เพื่อช่วยปกป้องและอนุญาตหรือบล็อกการรับส่งข้อมูลที่กำหนดเป้าหมายไปยังประเภทปลายทางต่อไปนี้ได้ ได้แก่ Network Load Balancer, การส่งต่อโปรโตคอล และ VM ที่มี IP สาธารณะ

ในโค้ดแล็บนี้ เราจะสาธิตวิธีตั้งค่านโยบายความปลอดภัยของ Cloud Armor ด้วยกฎที่ผู้ใช้กำหนดเพื่อป้องกันการโจมตีแบบ DDoS

รูปที่ 1 Cloud Armor สำหรับ VM ที่มีการป้องกัน IP สาธารณะ
สิ่งที่คุณจะได้เรียนรู้
- นโยบายความปลอดภัยของ Cloud Armor ที่มีการกำหนดค่ากฎที่ผู้ใช้กำหนด
- การกำหนดค่าและการทดสอบออฟเซ็ต UDP
สิ่งที่คุณต้องมี
- ความรู้เกี่ยวกับ TCP/IP
- ความรู้เกี่ยวกับบรรทัดคำสั่ง Unix/Linux
2. ก่อนเริ่มต้น
ตรวจสอบว่าได้ตั้งค่ารหัสโปรเจ็กต์ใน Cloud Shell แล้ว
gcloud config list project gcloud config set project [YOUR-PROJECT-NAME] prodproject=YOUR-PROJECT-NAME echo $prodproject
3. สร้างเครือข่าย VPC เป้าหมาย
ในส่วนต่อไปนี้ เราจะตั้งค่าเครือข่าย VPC และการกำหนดค่าเครือข่ายที่เกี่ยวข้อง นโยบายความปลอดภัยที่ Edge ของเครือข่าย Cloud Armor เป็นแบบภูมิภาค เราจึงตั้งค่าทรัพยากรที่เกี่ยวข้องทั้งหมดในภูมิภาค asia-southeast1
เครือข่าย VPC
จาก Cloud Shell
gcloud compute networks create ca4nlb --project=$prodproject --subnet-mode=custom
สร้างซับเน็ต
จาก Cloud Shell
gcloud compute networks subnets create ca4nlb-asia-southeast1 --project=$prodproject --range=10.0.0.0/24 --network=ca4nlb --region=asia-southeast1
สร้างกฎไฟร์วอลล์
ในส่วนนี้ เราจะเพิ่มกฎไฟร์วอลล์เพื่ออนุญาตการรับส่งข้อมูล UDP ที่คาดไว้ไปยังพอร์ต 10000
จาก Cloud Shell ให้สร้างกฎไฟร์วอลล์เพื่อเปิดพอร์ต UDP 10000 สำหรับการทดสอบต่อไปนี้
gcloud compute firewall-rules create ca4nlb-udp10000 --allow udp:10000 --network ca4nlb --source-ranges 0.0.0.0/0 --enable-logging
สร้างกฎไฟร์วอลล์จาก Cloud Shell เพื่ออนุญาตให้ IAP เชื่อมต่อกับอินสแตนซ์ VM
gcloud compute firewall-rules create ca4nlb-iap-prod --network ca4nlb --allow tcp:22 --source-ranges=35.235.240.0/20 --enable-logging
4. สร้างอินสแตนซ์ VM เป้าหมาย
สร้าง VM เป้าหมายเพื่อทดสอบนโยบายความปลอดภัย VM นี้ควรมีที่อยู่ IP สาธารณะและเปิดพอร์ต UDP 10000
สร้างอินสแตนซ์ targetvm จาก 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. กำหนดค่าการป้องกัน DDoS ของเครือข่ายขั้นสูง
จาก 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. สร้างนโยบายความปลอดภัยของ Network Edge ด้วยกฎเริ่มต้น
สร้างนโยบายความปลอดภัยของ Network Edge
จาก Cloud Shell
gcloud alpha compute security-policies create customnetworkedge --type=CLOUD_ARMOR_NETWORK --region=asia-southeast1
แก้ไขกฎเริ่มต้น
จาก Cloud Shell
gcloud alpha compute security-policies rules update 2147483647 --security-policy=customnetworkedge --action=deny --region=asia-southeast1
7. สร้างนโยบายความปลอดภัยของ Network Edge ด้วยกฎที่กำหนดค่าไว้
ออฟเซ็ต UDP ที่กำหนดไว้ล่วงหน้าของผู้ใช้และกำหนดค่าไว้ในนโยบาย Cloud Armor แพ็กเก็ตที่มี "ค่าออฟเซ็ต" นี้จะผ่านการตรวจสอบนโยบายและส่งไปยัง VM ของแบ็กเอนด์ ในตัวอย่างต่อไปนี้ เราจะกำหนด "ออฟเซ็ต" 2 รายการที่มีค่าแตกต่างกัน
ค่าแรกอยู่หลังส่วนหัว UDP ตรงกับ 2 ไบต์ 0x1700
ค่าที่ 2 คือออฟเซ็ต 8 ไบต์ของส่วนหัว UDP ซึ่งตรงกับ 4 ไบต์ 0x12345678
ค่าที่กำหนดไว้ล่วงหน้าข้างต้นจะแปลงเป็นมุมมองบิตของแพ็กเก็ต UDP

จาก 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. แนบนโยบายความปลอดภัยกับ VM เป้าหมาย
จาก Cloud Shell ให้แนบนโยบายความปลอดภัยกับ VM ที่ได้รับการป้องกัน
gcloud alpha compute instances network-interfaces update targetvm \ --security-policy=customnetworkedge \ --security-policy-region=asia-southeast1 \ --network-interface=nic0 \ --zone=asia-southeast1-b
จาก Cloud Shell ให้ใช้คำสั่ง describe VM เป้าหมาย คุณจะเห็นว่ามีการแนบนโยบายความปลอดภัย บันทึก IP สาธารณะสำหรับการทดสอบต่อไปนี้
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
ยกเลิกการแนบนโยบายความปลอดภัยจาก VM ที่ได้รับการป้องกันจาก Cloud Shell
gcloud alpha compute instances network-interfaces update targetvm \ --network-interface=nic0 \ --zone=asia-southeast1-b \ --security-policy=
9. เตรียมทรัพยากรการทดสอบ
สร้างเครือข่าย VPC สำหรับทดสอบ
จาก Cloud Shell
gcloud compute networks create test --project=$prodproject --subnet-mode=custom
สร้างซับเน็ตทดสอบ
จาก Cloud Shell
gcloud compute networks subnets create test-asia-southeast1 --project=$prodproject --range=10.0.1.0/24 --network=test --region=asia-southeast1
สร้างไฟร์วอลล์
สร้างกฎไฟร์วอลล์จาก Cloud Shell เพื่ออนุญาตให้ IAP เชื่อมต่อกับอินสแตนซ์ VM
gcloud compute firewall-rules create test-iap-prod --network test --allow tcp:22 --source-ranges=35.235.240.0/20 --enable-logging
สร้าง VM สำหรับทดสอบ
จาก 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. การยืนยัน
เข้าสู่ระบบคอนโซล VM สำหรับการทดสอบและติดตั้งเครื่องมือสร้างแพ็กเก็ต packit
sudo apt install packit
ใช้ Packit เพื่อสร้างแพ็กเก็ต UDP ตามการออกแบบออฟเซ็ต UDP เราจำลองแพ็กเกต (-t udp) จากที่อยู่ IP ต้นทาง (-s 10.0.1.2) ของอินเทอร์เฟซ (-s ens4) ที่มีพอร์ตต้นทาง (-S 10000) ไปยังที่อยู่ IP ปลายทาง (-d 35.240.148.100) ของ targetVM ที่มีพอร์ตปลายทาง (-D 10000) เนื้อหาแพ็กเก็ตตรงกับค่า (-p ‘0x 17 00 00 00 00 00 00 00 12 34 56 78') เราจะส่งแพ็กเก็ต (-c 4)
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
ใน VM เป้าหมาย ให้เรียกใช้ tcpdump เพื่อบันทึกแพ็กเก็ต 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
หากเราเปลี่ยนรูปแบบการรับส่งข้อมูลใน VM ที่ใช้ทดสอบ เราจะดักจับแพ็กเก็ตใน VM เป้าหมายไม่ได้
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. การส่งข้อมูลทางไกล
เปิดเมตริกของ Cloud แล้วใช้ MQL ด้านล่างเพื่อค้นหาข้อมูลการวัดระยะของ 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
สร้างการเข้าชมจำนวนมากด้วยคำสั่งออฟเซ็ตการจับคู่
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
สร้างการเข้าชมจำนวนมากด้วยคำสั่งออฟเซ็ตที่ไม่ตรงกัน
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
ระบบจะกรองการวัดและส่งข้อมูลตาม policy_name และจัดกลุ่มตาม blocked เส้นสีน้ำเงินแสดงการเข้าชมที่กฎนโยบายอนุญาต เส้นสีเขียวแสดงการเข้าชมที่ถูกบล็อกโดยกฎนโยบาย

12. ขั้นตอนการล้างข้อมูล
ลบคอมโพเนนต์ของ Lab จาก Cloud Shell เดียวในเทอร์มินัล
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. ยินดีด้วย
ขอแสดงความยินดีที่ทำ Codelab เสร็จสมบูรณ์
สิ่งที่เราได้พูดถึง
- นโยบายความปลอดภัยของ Cloud Armor ที่มีกฎที่ลูกค้ากำหนด