Bulk add Cloudflare's IPs to Azure App Service Access Restrictions using Az PowerShell
Tags: azure, cloudflare, powershell
Published: 2020-09-09 17:33:50

---
title: Bulk add Cloudflare's IPs to Azure App Service Access Restrictions using Az PowerShell
published: true
tags: azure, cloudflare, powershell
canonical_url: https://swimburger.net/blog/azure/bulk-add-cloudflares-ips-to-azure-app-service-access-restrictions-using-az-powershell
cover_image: https://swimburger.net/media/1145/bulk-add-cloudflares-social.png?anchor=center&mode=crop&width=1000&height=420&rnd=132042204870000000
---

When enabling Cloudflare on your website, Cloudflare acts as a caching proxy and web application firewall (WAF). By default Azure's App Services are exposed publicly using the following format [APPSERVICE\_NAME].azurewebsites.net. If you leave your app service publicly exposed via that domain name, Cloudflare's security can be easily bypassed by attacking your service via Azure's default DNS.

You can block access to your service by using Azure App Service's Access Restriction feature which allows you to lock it down to a list of IPv4 ranges, IPv6 ranges, or specific Virtual Network. This will prevent attackers from accessing your service directly, but it will also block Cloudflare. To resolve this you need to add Cloudflare's IP ranges.

Cloudflare hosts 2 text files containing all their IP ranges: [IPv4's](https://www.cloudflare.com/ips-v4) & [IPv6's](https://www.cloudflare.com/ips-v6). This list is rather long and if you want to insert those IP ranges manually using Azure's GUI, it'll will take a few minutes. When adding a single IP range manually, all traffic is blocked immediately except coming from that IP range. So the longer you take inserting the ranges manually, the more legitimate traffic is being blocked from Cloudflare. For that reason and general laziness, you should use a script to do this for you.

## Bulk insert Cloudflare's IP Ranges script

In a [previous post](/blog/azure/bulk-add-ip-access-restrictions-to-azure-app-service-using-az-powershell "Bulk add IP Access Restrictions to Azure App Service using Az PowerShell"), I shared a script you can use to bulk insert IP Access Restrictions to your Azure App Service using PowerShell. In this post I'll build upon that script and add the functionality necessary to insert all Cloudflare's IP ranges. The following script will:

1. Fetch Cloudflare's IP range text files
2. Parse the text file into valid IP Access Restriction Hashtables
3. Pass the IP range hashtables to the bulk insert script

```powershell
Param( 
    [Parameter(Mandatory = $true)] 
    [string] $ResourceGroupName, 
    [Parameter(Mandatory = $true)] 
    [string] $AppServiceName, 
    [Parameter(Mandatory = $true)] 
    [string] $SubscriptionId, 
    [Parameter(Mandatory = $true)] 
    [string] $RulePriority
)

$ErrorActionPreference = "Stop"

$IPv4s = (Invoke-WebRequest -Uri "https://www.cloudflare.com/ips-v4").Content.TrimEnd([Environment]::NewLine).Split([Environment]::NewLine);
$IPv6s = (Invoke-WebRequest -Uri "https://www.cloudflare.com/ips-v6").Content.TrimEnd([Environment]::NewLine).Split([Environment]::NewLine);

$NewIpRestrictions = @();
foreach($IPv4 in $IPv4s){
    $NewIpRestrictions += @{
        ipAddress = $IPv4; 
        action = "Allow";
        priority = $RulePriority;
        name = "Cloudflare IPv4";
        description = "Cloudflare IPv4";
        tag = "Default";
    }
}

foreach($IPv6 in $IPv6s){
    $NewIpRestrictions += @{
        ipAddress = $IPv6; 
        action = "Allow";
        priority = $RulePriority;
        name = "Cloudflare IPv6";
        description = "Cloudflare IPv6";
        tag = "Default";
    }
}

& "$PSScriptRoot\AddRestrictedIPAzureAppService.ps1" -ResourceGroupName $ResourceGroupName -AppServiceName $AppServiceName -SubscriptionId $SubscriptionId -NewIpRules $NewIpRestrictions
```

Usage:

```
.\AddCloudflareRestrictedIPApp.ps1 `
    -ResourceGroupName "YourResourceGroup" `
    -AppServiceName "YourAppServiceName" `
    -SubscriptionId "YourSubscriptionGuid" `
    -RulePriority "100"
```

- You need to save both scripts to your disk and make sure they're in the same folder. Download the scripts from [this GitHub gist](https://gist.github.com/Swimburger/b2d58bff38156b73a5417b7f818fc5be).
- You need to have the [Az PowerShell module](https://github.com/Azure/azure-powershell) installed.
- The RulePriority matters in case you already have access restrictions and will determine the order of evaluation when a request is made against your service.

I hope this script saved you some time!