您可以使用 Web 應用程式防火牆 (WAF),限制應用程式閘道上的流量。 WAF 會使用 OWASP 規則來保護您的應用程式。 這些規則包括防禦諸如 SQL 插入攻擊、跨網站指令碼攻擊,以及工作階段劫持等攻擊。
在本文中,您將學會如何:
- 設定網路
- 建立已啟用 WAF 的應用程式閘道
- 建立虛擬機器擴展集
- 建立儲存體帳戶並設定診斷
如果您想要的話,可以使用 Azure PowerShell 完成本程序。
如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶 。
必要條件
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱開始使用 Azure Cloud Shell。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱 使用 Azure CLI 向 Azure 進行驗證。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能的詳細資訊,請參閱 使用和管理 Azure CLI 的擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
- 本文需要 2.0.4 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。
建立資源群組
資源群組是在其中部署與管理 Azure 資源的邏輯容器。 使用 az group create 建立名為 myResourceGroupAG 的 Azure 資源群組。
az group create --name myResourceGroupAG --location eastus
建立網路資源
這些虛擬網路和子網路可用來為應用程式閘道及其相關聯的資源提供網路連線。 建立名為 myVNet 的虛擬網路,以及名為 myAGSubnet 的子網路。 然後建立名為 myAGPublicIPAddress 的公用 IP 位址。
az network vnet create \
--name myVNet \
--resource-group myResourceGroupAG \
--location eastus \
--address-prefix 10.0.0.0/16 \
--subnet-name myBackendSubnet \
--subnet-prefix 10.0.1.0/24
az network vnet subnet create \
--name myAGSubnet \
--resource-group myResourceGroupAG \
--vnet-name myVNet \
--address-prefix 10.0.2.0/24
az network public-ip create \
--resource-group myResourceGroupAG \
--name myAGPublicIPAddress \
--allocation-method Static \
--sku Standard
建立包含 WAF 的應用程式閘道
您可以使用 az network application-gateway create 來建立名為 myAppGateway 的應用程式閘道。 當您使用 Azure CLI 建立應用程式閘道時,需要指定設定資訊,例如容量、SKU 和 HTTP 設定。 應用程式閘道會指派給 myAGSubnet 和 myAGPublicIPAddress。
az network application-gateway waf-policy create \
--name waf-pol \
--resource-group myResourceGroupAG \
--type OWASP \
--version 3.2
az network application-gateway create \
--name myAppGateway \
--location eastus \
--resource-group myResourceGroupAG \
--vnet-name myVNet \
--subnet myAGSubnet \
--capacity 2 \
--sku WAF_v2 \
--http-settings-cookie-based-affinity Disabled \
--frontend-port 80 \
--http-settings-port 80 \
--http-settings-protocol Http \
--public-ip-address myAGPublicIPAddress \
--waf-policy waf-pol \
--priority 1
可能需要幾分鐘的時間來建立應用程式閘道。 建立應用程式閘道後,您可以看到它的這些新功能:
- appGatewayBackendPool - 應用程式閘道必須至少有一個後端位址集區。
- appGatewayBackendHttpSettings - 指定以連接埠 80 和 HTTP 通訊協定來進行通訊。
- appGatewayHttpListener - 與 appGatewayBackendPool 相關聯的預設接聽程式。
- appGatewayFrontendIP - 將 myAGPublicIPAddress 指派給 appGatewayHttpListener。
- rule1 - 與 appGatewayHttpListener 相關聯的預設路由規則。
建立虛擬機器擴展集
在此範例中,您會建立虛擬機器擴展集,為應用程式閘道中的後端集區提供兩部伺服器。 擴展集中的虛擬機器會與 myBackendSubnet 子網路相關聯。 若要建立擴展集,您可以使用 az vmss create。
在執行此指令碼之前,請將 <username> 和 <password> 取代為您的值。
az vmss create \
--name myvmss \
--resource-group myResourceGroupAG \
--image Ubuntu2204 \
--admin-username <username> \
--admin-password <password> \
--instance-count 2 \
--vnet-name myVNet \
--subnet myBackendSubnet \
--vm-sku Standard_DS2 \
--upgrade-policy-mode Automatic \
--app-gateway myAppGateway \
--backend-pool-name appGatewayBackendPool
安裝 NGINX
az vmss extension set \
--publisher Microsoft.Azure.Extensions \
--version 2.0 \
--name CustomScript \
--resource-group myResourceGroupAG \
--vmss-name myvmss \
--settings '{ "fileUris": ["https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/install_nginx.sh"],"commandToExecute": "./install_nginx.sh" }'
建立儲存體帳戶並設定診斷
在本文中,應用程式閘道會使用儲存體帳戶來儲存資料,以達到偵測與預防的目的。 您也可以使用 Azure 監視器記錄或事件中樞來記錄資料。
建立儲存體帳戶
使用 az storage account create 建立名為 myagstore1 的儲存體帳戶。
az storage account create \
--name myagstore1 \
--resource-group myResourceGroupAG \
--location eastus \
--sku Standard_LRS \
--encryption-services blob
設定診斷
設定診斷以將資料記錄到 ApplicationGatewayAccessLog、ApplicationGatewayPerformanceLog 和 ApplicationGatewayFirewallLog 記錄。 使用您的訂用帳戶識別碼來取代 <subscriptionId>,然後使用 az monitor diagnostic-settings create 來設定診斷。
appgwid=$(az network application-gateway show --name myAppGateway --resource-group myResourceGroupAG --query id -o tsv)
storeid=$(az storage account show --name myagstore1 --resource-group myResourceGroupAG --query id -o tsv)
az monitor diagnostic-settings create --name appgwdiag --resource $appgwid \
--logs '[ { "category": "ApplicationGatewayAccessLog", "enabled": true, "retentionPolicy": { "days": 30, "enabled": true } }, { "category": "ApplicationGatewayPerformanceLog", "enabled": true, "retentionPolicy": { "days": 30, "enabled": true } }, { "category": "ApplicationGatewayFirewallLog", "enabled": true, "retentionPolicy": { "days": 30, "enabled": true } } ]' \
--storage-account $storeid
測試應用程式閘道
若要取得應用程式閘道的公用 IP 位址,請使用 az network public-ip show。 將公用 IP 位址複製並貼到您瀏覽器的網址列。
az network public-ip show \
--resource-group myResourceGroupAG \
--name myAGPublicIPAddress \
--query [ipAddress] \
--output tsv
清除資源
若不再需要,可移除資源群組、應用程式閘道和所有相關資源。
az group delete --name myResourceGroupAG