|
1 | 1 | ---
|
2 |
| -title: Configure custom responses for Web Application Firewall with Azure Front Door |
| 2 | +title: Configure custom responses for Azure Front Door WAF policy |
3 | 3 | description: Learn how to configure a custom response code and message when Azure Web Application Firewall blocks a request.
|
4 | 4 | author: halkazwini
|
5 | 5 | ms.author: halkazwini
|
6 | 6 | ms.service: azure-web-application-firewall
|
7 | 7 | ms.topic: how-to
|
8 |
| -ms.date: 04/04/2025 |
| 8 | +ms.date: 05/08/2025 |
9 | 9 | ms.custom: devx-track-azurepowershell
|
10 |
| -zone_pivot_groups: front-door-tiers |
11 | 10 | ---
|
12 | 11 |
|
13 | 12 | # Configure a custom response for Azure Web Application Firewall
|
14 | 13 |
|
15 |
| -In this article, you learn how to configure a custom response page when Azure Web Application Firewall blocks a request. |
| 14 | +By default, when Azure Web Application Firewall (WAF) blocks a request because of a matched rule, it returns a 403 status code with the message "The request is blocked." The default message also includes the tracking reference string that's used to link to [log entries](./waf-front-door-monitor.md) for the request. |
16 | 15 |
|
17 |
| -By default, when Azure Web Application Firewall blocks a request because of a matched rule, it returns a 403 status code with the message "The request is blocked." The default message also includes the tracking reference string that's used to link to [log entries](./waf-front-door-monitor.md) for the request. You can configure a custom response status code and a custom message with a reference string for your use case. |
| 16 | +In this article, you learn how to configure a custom response status code and a custom message with a reference string for your use case using the Azure portal, PowerShell, or the Azure CLI. |
18 | 17 |
|
19 |
| -## Configure a custom response status code and message |
| 18 | +## Prerequisites |
20 | 19 |
|
21 | 20 | # [**Portal**](#tab/portal)
|
22 | 21 |
|
23 |
| -You can configure a custom response status code and body under **Policy settings** on the Azure Web Application Firewall portal. |
| 22 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
24 | 23 |
|
25 |
| -:::image type="content" source="../media/waf-front-door-configure-custom-response-code/custom-response-settings.png" alt-text="Screenshot that shows Azure Web Application Firewall Policy settings."::: |
| 24 | +- Sign in to the [Azure portal](https://portal.azure.com) with your Azure account. |
26 | 25 |
|
27 |
| -In the preceding example, we kept the response code as 403 and configured a short "Please contact us" message, as shown in the following image: |
| 26 | +# [**PowerShell**](#tab/powershell) |
28 | 27 |
|
29 |
| -:::image type="content" source="../media/waf-front-door-configure-custom-response-code/custom-response.png" alt-text="Screenshot that shows a custom response example."::: |
| 28 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
30 | 29 |
|
31 |
| -# [**PowerShell**](#tab/powershell) |
| 30 | +- Azure Cloud Shell or Azure PowerShell. |
32 | 31 |
|
33 |
| -Follow these steps to configure a custom response status code and message by using PowerShell. |
| 32 | + The steps in this article run the Azure PowerShell cmdlets interactively in [Azure Cloud Shell](/azure/cloud-shell/overview). To run the cmdlets in the Cloud Shell, select **Open Cloud Shell** at the upper-right corner of a code block. Select **Copy** to copy the code and then paste it into Cloud Shell to run it. You can also run the Cloud Shell from within the Azure portal. |
34 | 33 |
|
35 |
| -### Set up your PowerShell environment |
| 34 | + You can also [install Azure PowerShell locally](/powershell/azure/install-azure-powershell) to run the cmdlets. This article requires the Azure PowerShell module. If you run PowerShell locally, sign in to Azure using the [Connect-AzAccount](/powershell/module/az.accounts/connect-azaccount) cmdlet. |
36 | 35 |
|
37 |
| -Azure PowerShell provides a set of cmdlets that use the [Azure Resource Manager](../../azure-resource-manager/management/overview.md) model for managing your Azure resources. |
| 36 | +# [**Azure CLI**](#tab/cli) |
38 | 37 |
|
39 |
| -You can install [Azure PowerShell](/powershell/azure/) on your local machine and use it in any PowerShell session. Follow the instructions on the page to sign in with your Azure credentials. Then install the Az PowerShell module. |
| 38 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
40 | 39 |
|
41 |
| -### Connect to Azure with an interactive dialog for sign-in |
| 40 | +- Azure Cloud Shell or Azure CLI. |
42 | 41 |
|
43 |
| -``` |
44 |
| -Connect-AzAccount |
45 |
| -Install-Module -Name Az |
| 42 | + The steps in this article run the Azure CLI commands interactively in [Azure Cloud Shell](/azure/cloud-shell/overview). To run the commands in the Cloud Shell, select **Open Cloud Shell** at the upper-right corner of a code block. Select **Copy** to copy the code, and paste it into Cloud Shell to run it. You can also run the Cloud Shell from within the Azure portal. |
46 | 43 |
|
47 |
| -``` |
48 |
| -Make sure you have the current version of PowerShellGet installed. Run the following command and reopen PowerShell. |
| 44 | + You can also [install Azure CLI locally](/cli/azure/install-azure-cli) to run the commands. This article requires the Azure CLI version 2.67.0 or higher and **front-door** extension. Run [az --version](/cli/azure/reference-index#az-version) command to find the installed version. If you run Azure CLI locally, sign in to Azure using the [az login](/cli/azure/reference-index#az-login) command. |
49 | 45 |
|
50 |
| -``` |
51 |
| -Install-Module PowerShellGet -Force -AllowClobber |
52 |
| -``` |
| 46 | +--- |
53 | 47 |
|
54 |
| -### Install the Az.FrontDoor module |
| 48 | +## Configure a custom response status code and message |
55 | 49 |
|
56 |
| -``` |
57 |
| -Install-Module -Name Az.FrontDoor |
58 |
| -``` |
| 50 | +# [**Portal**](#tab/portal) |
59 | 51 |
|
60 |
| -### Create a resource group |
| 52 | +To customize the response status code and body, follow these steps: |
61 | 53 |
|
62 |
| -In Azure, you allocate related resources to a resource group. Here, we create a resource group by using [New-AzResourceGroup](/powershell/module/Az.resources/new-Azresourcegroup). |
| 54 | +1. Go to your Front Door WAF policy in the Azure portal. |
63 | 55 |
|
64 |
| -```azurepowershell-interactive |
65 |
| -New-AzResourceGroup -Name myResourceGroupWAF |
66 |
| -``` |
| 56 | +1. Under **Settings**, select **Policy settings**. |
67 | 57 |
|
68 |
| -### Create a new WAF policy with a custom response |
| 58 | +1. Enter the custom response status code and response body in the **Block response status code** and **Block response body** boxes, respectively. |
69 | 59 |
|
70 |
| -The following example shows how to create a new web application firewall (WAF) policy with a custom response status code set to 405 and a message of "You are blocked" by using |
71 |
| -[New-AzFrontDoorWafPolicy](/powershell/module/az.frontdoor/new-azfrontdoorwafpolicy). |
| 60 | + :::image type="content" source="../media/waf-front-door-configure-custom-response-code/custom-response-settings.png" alt-text="Screenshot that shows Azure Web Application Firewall Policy settings." lightbox="../media/waf-front-door-configure-custom-response-code/custom-response-settings.png"::: |
72 | 61 |
|
73 |
| -```azurepowershell |
74 |
| -# WAF policy setting |
75 |
| -New-AzFrontDoorWafPolicy ` |
76 |
| --Name myWAFPolicy ` |
77 |
| --ResourceGroupName myResourceGroupWAF ` |
78 |
| --EnabledState enabled ` |
79 |
| --Mode Detection ` |
80 |
| --CustomBlockResponseStatusCode 405 ` |
81 |
| --CustomBlockResponseBody "<html><head><title>You are blocked.</title></head><body></body></html>" |
82 |
| -``` |
| 62 | +1. Select **Save**. |
| 63 | + |
| 64 | +# [**PowerShell**](#tab/powershell) |
83 | 65 |
|
84 |
| -Modify the custom response code or response body settings of an existing WAF policy by using [Update-AzFrontDoorFireWallPolicy](/powershell/module/az.frontdoor/Update-AzFrontDoorWafPolicy). |
| 66 | +To customize the response status code and body, use [Update-AzFrontDoorWafPolicy](/powershell/module/az.frontdoor/Update-AzFrontDoorWafPolicy) cmdlet. |
85 | 67 |
|
86 |
| -```azurepowershell |
87 |
| -# modify WAF response code |
88 |
| -Update-AzFrontDoorFireWallPolicy ` |
89 |
| --Name myWAFPolicy ` |
90 |
| --ResourceGroupName myResourceGroupWAF ` |
91 |
| --EnabledState enabled ` |
92 |
| --Mode Detection ` |
93 |
| --CustomBlockResponseStatusCode 403 |
94 |
| -``` |
95 | 68 |
|
96 |
| -```azurepowershell |
97 |
| -# modify WAF response body |
98 |
| -Update-AzFrontDoorFireWallPolicy ` |
99 |
| --Name myWAFPolicy ` |
100 |
| --ResourceGroupName myResourceGroupWAF ` |
101 |
| --CustomBlockResponseBody "<html><head><title>Forbidden</title></head><body>{{azure-ref}}</body></html>" |
| 69 | +```azurepowershell-interactive |
| 70 | +# Update WAF policy settings to customize response body and status code |
| 71 | +Update-AzFrontDoorWafPolicy ` |
| 72 | +-Name 'myWAFPolicy' ` |
| 73 | +-ResourceGroupName 'myResourceGroup' ` |
| 74 | +-RequestBodyCheck 'Enabled' ` |
| 75 | +-RedirectUrl 'https://learn.microsoft.com/en-us/azure/web-application-firewall/' ` |
| 76 | +-CustomBlockResponseStatusCode '403' ` |
| 77 | +-CustomBlockResponseBody '<html><head><title>WAF Demo</title></head><body><p><h1><strong>WAF Custom Response Page</strong></h1></p><p>Please contact us with this information:<br>{{azure-ref}}</p></body></html>' |
102 | 78 | ```
|
103 | 79 |
|
104 |
| ---- |
| 80 | +# [**Azure CLI**](#tab/cli) |
105 | 81 |
|
106 |
| -> [!NOTE] |
107 |
| -> If you leave the block response body blank, the WAF returns a *403 Forbidden* response for normal WAF blocks and a *429 Too many requests* for rate limit blocks. |
| 82 | +To customize the response status code and body, use [az network front-door waf-policy update](/cli/azure/network/front-door/waf-policy#az-network-front-door-waf-policy-update) command. |
108 | 83 |
|
109 |
| -::: zone pivot="front-door-standard-premium" |
110 | 84 |
|
111 |
| -"{{azure-ref}}" inserts the unique reference string in the response body. The value matches the TrackingReference field in the `FrontDoorAccessLog` and `FrontDoorWebApplicationFirewallLog` logs. |
| 85 | +```azurecli-interactive |
| 86 | +# Update WAF policy settings to customize response body and status code |
| 87 | +az network front-door waf-policy update \ |
| 88 | + --name 'myWAFPolicy' \ |
| 89 | + --resource-group 'myResourceGroup' \ |
| 90 | + --request-body-check 'Enabled' \ |
| 91 | + --redirect-url 'https://learn.microsoft.com/en-us/azure/web-application-firewall/' \ |
| 92 | + --custom-block-response-status-code '403' \ |
| 93 | + --custom-block-response-body 'PGh0bWw+PGhlYWQ+PHRpdGxlPldBRiBEZW1vPC90aXRsZT48L2hlYWQ+PGJvZHk+PHA+PGgxPjxzdHJvbmc+V0FGIEN1c3RvbSBSZXNwb25zZSBQYWdlPC9zdHJvbmc+PC9oMT48L3A+PHA+UGxlYXNlIGNvbnRhY3QgdXMgd2l0aCB0aGlzIGluZm9ybWF0aW9uOjxicj57e2F6dXJlLXJlZn19PC9wPjwvYm9keT48L2h0bWw+' |
| 94 | +``` |
| 95 | + |
| 96 | +> [!NOTE] |
| 97 | +> The value of the `--custom-block-response-body` parameter must be a **base64** encoded string. |
112 | 98 |
|
113 |
| -::: zone-end |
| 99 | +--- |
114 | 100 |
|
115 |
| -::: zone pivot="front-door-classic" |
| 101 | +In the previous example, the response code was kept as 403 with a custom message: "*Please contact us with this information:*". |
116 | 102 |
|
117 |
| -"{{azure-ref}}" inserts the unique reference string in the response body. The value matches the TrackingReference field in the `FrontdoorAccessLog` and `FrontdoorWebApplicationFirewallLog` logs. |
| 103 | +:::image type="content" source="../media/waf-front-door-configure-custom-response-code/custom-response.png" alt-text="Screenshot that shows a custom response example."::: |
| 104 | + |
| 105 | +> [!NOTE] |
| 106 | +> `{{azure-ref}}` inserts the unique reference string in the response body. The value matches the TrackingReference field in the `FrontDoorAccessLog` and `FrontDoorWebApplicationFirewallLog` logs. |
118 | 107 |
|
119 |
| -::: zone-end |
| 108 | +> [!IMPORTANT] |
| 109 | +> If you leave the block response body blank, the WAF returns a ***403 Forbidden*** response for normal WAF blocks and a ***429 Too many requests*** for rate limit blocks. |
120 | 110 |
|
121 | 111 | ## Next step
|
122 | 112 |
|
|
0 commit comments