1 00:00:00,000 --> 00:00:10,580 Next up, we've got Victor Gazdag, who's worked as a pen tester and security consultant for nine years and leads the Cloud Research Working Group and the M365 Capability Service. 2 00:00:10,580 --> 00:00:16,760 In this talk, Victor is going to speak on Azure Policy, what it is, how to write one, what permissions are needed, and more. 3 00:00:16,760 --> 00:00:19,600 The title of the talk is Creating Azure Policy Compliant Backdoor. 4 00:00:19,600 --> 00:00:21,280 Please welcome Victor. 5 00:00:27,250 --> 00:00:28,370 Thank you everyone. 6 00:00:28,370 --> 00:00:29,930 Can you hear me well? 7 00:00:30,650 --> 00:00:31,390 Good. 8 00:00:31,390 --> 00:00:34,370 Because I'm a little bit quiet. 9 00:00:34,630 --> 00:00:36,090 So let's get started. 10 00:00:36,090 --> 00:00:37,730 So, my name is Victor. 11 00:00:37,730 --> 00:00:39,450 Thank you for the introduction. 12 00:00:39,450 --> 00:00:44,470 I'm a principal security consultant and I work at the NCC Group. 13 00:00:44,550 --> 00:00:48,870 I'm specialized in cloud and CICT security. 14 00:00:52,690 --> 00:01:01,090 I have a bunch of certification related to Azure and this is my second talk at DEF CON, so you guys are an awesome crowd. 15 00:01:01,090 --> 00:01:02,590 So that's why I keep coming back. 16 00:01:04,650 --> 00:01:17,670 So Azure Policy basically evaluates resources and actions that you as a user are doing in the environment and compares it to the rules that are created in the policy. 17 00:01:17,670 --> 00:01:32,850 So, for example, you can say that you don't want to allow creating a resource without a tag or you want to prevent creating resources in certain regions, for example, in Europe, and so on. 18 00:01:35,010 --> 00:01:41,650 So, the policy itself is written in JSON and it's called a policy definition. 19 00:01:41,650 --> 00:01:47,870 If you group more than one policy definition together, then it's called a policy initiative. 20 00:01:48,530 --> 00:01:57,410 You assign this policy to a scope, which can be a resource, resource group, subscription, and management group. 21 00:01:57,410 --> 00:02:06,490 And you can also fix the non-compliant resources and that you can do with the remediation or remediation class. 22 00:02:08,190 --> 00:02:10,550 So this is how it looks like. 23 00:02:10,550 --> 00:02:12,470 This is a very basic one. 24 00:02:12,570 --> 00:02:19,110 You will see that the display, the description, and the mode, that's the metadata. 25 00:02:19,290 --> 00:02:26,330 You can use some parameters, so you can reuse your policy and assign it multiple times with different values. 26 00:02:26,330 --> 00:02:33,510 And then you have the policy rule with logical evaluation and effects. 27 00:02:33,750 --> 00:02:36,510 So, basically, you have an if-then case. 28 00:02:36,510 --> 00:02:41,450 So, if there is a resource that has a certain property, then do this. 29 00:02:41,450 --> 00:02:49,270 For example, if there is a storage account with public access, then out it or just deny and don't create it. 30 00:02:52,550 --> 00:02:58,510 And if you do a little bit more, then you can go up to back this. 31 00:02:58,510 --> 00:03:04,650 So this is probably, you can't see it, but this is more than 200 lines of JSON code. 32 00:03:05,130 --> 00:03:08,670 But this is an actual existing policy as well. 33 00:03:10,570 --> 00:03:17,170 So, to create one policy, we will need something to work on. 34 00:03:17,170 --> 00:03:21,850 So we will need the property of the Azure resource itself. 35 00:03:21,990 --> 00:03:31,430 And for that, we will look for something called policy areas and resource properties. 36 00:03:31,430 --> 00:03:41,390 And if you modify something, so if there is an effect called modify, if you use that, then you also need your property itself to be modifiable. 37 00:03:42,530 --> 00:03:46,650 I will have some examples on how to look for this. 38 00:03:47,170 --> 00:04:00,850 So, I included different tools that can help you look for this, so like Azure CLI, the deployment template, or the Azure resource explorer, so you can look for this. 39 00:04:02,870 --> 00:04:15,230 So a quick example, this is a storage account, and I'm looking for, on the web UI, requires secure transfer for REST API operations. 40 00:04:15,230 --> 00:04:27,590 And once I check the properties itself on the deployment template, I can see it is translated to the supports HTTPS traffic only property. 41 00:04:27,730 --> 00:04:39,150 So we will need these, and in the next slide I will show you how to get the full pass for it, but basically this is what we will put in the policy rule itself. 42 00:04:39,150 --> 00:04:48,450 So we will look for a storage account, and also the storage account that has the support HTTPS traffic only properties. 43 00:04:48,450 --> 00:04:58,850 It's very important to double check the deployment template, because sometimes you expect it to be true, but it will be empty, or null, and so on. 44 00:05:02,770 --> 00:05:19,810 So this is the resource explorer, and you saw a lot of providers, like in this case we will continue with the storage account, so you will see that Microsoft.storage, and there are a lot of resources and properties here. 45 00:05:20,070 --> 00:05:28,390 So this is the Azure CLI command that will give you all the areas that you can use in the policy rule. 46 00:05:29,070 --> 00:05:41,990 And the second one, which is quite a long line, I just filtered to see what's the full pass for the supports HTTPS traffic only property. 47 00:05:43,430 --> 00:05:56,110 I included in the slides a couple of commands that is working and can help you look for modifiables and properties, and you can rewrite them. 48 00:05:56,110 --> 00:06:02,730 So sometimes it's a storage account, but you can modify the compute or something else. 49 00:06:03,890 --> 00:06:09,230 These as well, or if the second one is for a modifiable attribute. 50 00:06:15,130 --> 00:06:23,050 There is one important thing that I wanted to mention, that this is Azure policy, and this is for Azure resources. 51 00:06:23,050 --> 00:06:35,010 So it's not for Azure AD and Enter ID, so it's not like for, I'm looking for all the users who has three character long passwords, it's not for that. 52 00:06:35,130 --> 00:06:47,250 But there is a way in my demo video and proof of concept where I can work on the Azure AD or Enter ID resources. 53 00:06:47,250 --> 00:07:02,150 So you have two types of policies, one is the managed one, that's created and maintained by Azure itself, and the other one is custom policy, which is you are creating, or some people in the community already created a lot of them. 54 00:07:02,290 --> 00:07:08,370 And at the end of my slides I included a couple of links, they are collected together. 55 00:07:10,030 --> 00:07:15,310 You need these two permissions at least just to create the policy itself. 56 00:07:16,270 --> 00:07:25,770 If you use remediation tasks, then you will use a managed identity, and those managed identities will need more permissions as well. 57 00:07:27,150 --> 00:07:40,490 So the policy itself is evaluated when the resource within the scope is created or updated, and also every 24 hours it will run. 58 00:07:40,490 --> 00:07:47,850 But you can basically force it on-demand as well, or call it on-demand as well. 59 00:07:48,110 --> 00:08:02,930 I found an Azure command to do this, but the problem is the first time you create the policy itself, it will take 10-15 minutes to run first. 60 00:08:02,930 --> 00:08:08,190 I think this way you can speed up a little bit, but it will take still a little bit of time. 61 00:08:12,230 --> 00:08:19,070 So basically we can use this as a resource enumeration service as well. 62 00:08:19,070 --> 00:08:21,890 I included three examples. 63 00:08:22,590 --> 00:08:31,850 The first one is listing all the storage accounts, then moving on a little bit more useful, listing all the storage accounts with anonymous access. 64 00:08:32,210 --> 00:08:37,530 And the last one is we want to see all the virtual machines with public IP addresses. 65 00:08:40,490 --> 00:08:44,430 So the first one is listing all the storage accounts. 66 00:08:44,490 --> 00:08:50,450 Basically we will just use this path and the property that we are looking for. 67 00:08:50,890 --> 00:08:57,850 So microsoft.storage/.storageaccounts, that's the field that we will put in the policy rule itself. 68 00:08:59,250 --> 00:09:04,670 And this is a working example to list all the storage accounts. 69 00:09:04,670 --> 00:09:17,350 So it's a recommended way if you create an Azure policy that you don't do it from scratch, but try to look for an already existing one and just try to modify it what you need. 70 00:09:18,350 --> 00:09:22,370 And I did the same for these examples as well. 71 00:09:23,670 --> 00:09:29,950 The next one is the storage account that has allow public access property. 72 00:09:29,950 --> 00:09:40,970 So when I check the storage account and the deployment template, I can see that this allow public access property is what I am looking for. 73 00:09:41,170 --> 00:09:46,330 So this will say if the storage account has public access or not. 74 00:09:46,510 --> 00:09:53,710 And I will use the CLI command just to make sure there are no more than one property. 75 00:09:54,410 --> 00:10:01,450 And then I will put it in the policy rule, so I put two properties. 76 00:10:01,450 --> 00:10:09,090 The one is storage account, the other one is the allow public access that was in the previous slide. 77 00:10:09,330 --> 00:10:18,330 You can use something as not or without the not and also like all of or any of. 78 00:10:18,330 --> 00:10:23,690 So you can combine a lot of properties into one policy rule. 79 00:10:23,710 --> 00:10:33,050 And you can see like I am looking for three different properties, but it's enough if only one of them is true. 80 00:10:35,130 --> 00:10:37,750 And this is the whole policy itself. 81 00:10:40,190 --> 00:10:44,370 The last one is other virtual machines with external IP. 82 00:10:44,370 --> 00:10:49,490 And it turned out that this is an already existing policy and it's built-in. 83 00:10:49,490 --> 00:10:54,550 I just included the GitHub repo for it as well. 84 00:10:56,950 --> 00:11:00,870 Basically the policy, we are looking for the network interface. 85 00:11:01,090 --> 00:11:10,570 And this other property is an array, so it will check if all the elements in the array is there or not. 86 00:11:13,530 --> 00:11:17,910 And this is the policy that I was talking about. 87 00:11:17,910 --> 00:11:23,790 Here what you have to change is the effect itself because here it's denied. 88 00:11:23,790 --> 00:11:33,350 So if you want just to see all these virtual machines, then you have to change it to audit and you will have it in the compliance menu. 89 00:11:37,190 --> 00:11:46,490 So I created a proof-of-concept policy that will create a service principal. 90 00:11:46,490 --> 00:11:54,590 And with the service principal you can log in back and you will have owner roles on the subscription. 91 00:11:54,910 --> 00:12:03,450 So the proof-of-concept policy will check if a storage account or the storage account has this HTTPS enabled or not. 92 00:12:03,710 --> 00:12:06,290 I will go for no. 93 00:12:06,490 --> 00:12:13,510 And I will also have a remediation task and I have to use the deploy-if-not-exists effect. 94 00:12:13,510 --> 00:12:19,330 Because this is the only way I know that we can run the PowerShell script. 95 00:12:21,550 --> 00:12:28,990 There will be a role assignment to the manage identity because I'm using a remediation task, so I will need certain roles and permissions. 96 00:12:29,570 --> 00:12:40,790 And the nice thing about the proof-of-concept is that the deployment script is not using an inline script, so it won't be in the policy itself. 97 00:12:40,790 --> 00:12:45,750 But it's referenced and will be hosted at a GitHub repository. 98 00:12:47,150 --> 00:12:57,550 So I did the same as what was recommended, so I was looking for a policy on the internet and I just modified everything that I needed to make it work. 99 00:12:59,050 --> 00:13:02,230 So as I mentioned, it will create a service principal. 100 00:13:02,230 --> 00:13:12,970 And once the service principal is created, it will call out to a website and just push all the necessary information that it needs for logging. 101 00:13:12,970 --> 00:13:18,650 So the tenant ID, app ID and the secret for the logging. 102 00:13:19,330 --> 00:13:30,190 So as mentioned before, the policy is run every 24 hours, but I will talk about this at the problem section as well. 103 00:13:30,190 --> 00:13:35,110 The remediation task is only run once, so this is very important. 104 00:13:35,730 --> 00:13:42,530 So it won't keep making the service principal, it will only create once. 105 00:13:42,610 --> 00:13:52,990 So because I found this example, I just modified and in the video you will see some other tasks or steps that it will do. 106 00:13:52,990 --> 00:14:02,150 So for example, there will be two additional managed identities it created or an additional storage account and some role assignments as well. 107 00:14:04,930 --> 00:14:11,490 So this is a way to visualize what will happen and what's in the Azure policy and remediation task. 108 00:14:11,490 --> 00:14:21,690 So there will be a nested deployment template with a deployment script and the deployment script itself is hosted at a GitHub repository. 109 00:14:21,690 --> 00:14:34,830 We will need a storage account as well because that's where the PowerShell script will be downloaded and this storage account will be mounted automatically by the Azure container instance. 110 00:14:34,870 --> 00:14:41,610 So this Azure container instance will be created automatically because we are using that deployment script. 111 00:14:45,550 --> 00:14:49,350 And I will just show the demo video. 112 00:15:03,570 --> 00:15:10,910 So we can see that there is nothing in the compliance menu at the moment. 113 00:15:10,910 --> 00:15:13,970 There is no policy assignments as well. 114 00:15:14,750 --> 00:15:23,930 But there are some built-in definitions and I also just show that I have already made some custom policies that I tried and played with. 115 00:15:24,070 --> 00:15:27,190 So I will create now the backdoor policy. 116 00:15:27,270 --> 00:15:30,330 I will assign it to the subscription level. 117 00:15:30,670 --> 00:15:35,130 We have to use some kind of category, it doesn't really matter. 118 00:15:42,090 --> 00:15:44,550 And I will name it Defcon32. 119 00:15:45,410 --> 00:15:53,370 Here we can see that the policy and the remediation task will require an owner role in order to successfully run. 120 00:15:54,210 --> 00:15:59,290 Because I will need some permission to do the stuff, the things. 121 00:15:59,290 --> 00:16:08,230 At this moment, my managed identity, I call it managed identity, but it's not there so it doesn't have any role. 122 00:16:09,810 --> 00:16:14,630 And as you might see, there are identities not found. 123 00:16:14,630 --> 00:16:23,490 That's because when you use a remediation task with system-managed identities and it's deleted, then it will leave some garbage behind. 124 00:16:31,830 --> 00:16:35,030 It's just some pre-existing resources. 125 00:16:35,030 --> 00:16:37,490 That's the managed identity I will use. 126 00:16:37,630 --> 00:16:39,410 I have three storage accounts. 127 00:16:39,410 --> 00:16:44,270 One will be used, as mentioned before, for downloading the PowerShell script. 128 00:16:46,370 --> 00:16:48,870 This is the PowerShell script. 129 00:16:48,870 --> 00:16:51,870 I called it license.ps1. 130 00:16:51,870 --> 00:16:57,790 It will do like creating storage account, managed identity, install Azure CI and so on. 131 00:17:07,760 --> 00:17:20,460 You might see that there is an Azure login with the managed identity and creating the service principle and using invoke request method to reach the web server. 132 00:17:21,880 --> 00:17:32,720 Here I will just highlight what I'm looking in the policy resource or storage account and if it's HTTPS is enforced or not. 133 00:17:32,720 --> 00:17:36,580 And I will use the deploy-if-not-exist effect. 134 00:17:36,700 --> 00:17:42,560 Here's the same role definition that's required that we saw in the web UI. 135 00:17:42,680 --> 00:17:48,140 You can see this is a deployment template and this is the second deployment template. 136 00:17:49,080 --> 00:17:56,580 And then the deployment script which is referenced at the GitHub repository. 137 00:17:57,800 --> 00:17:59,620 And it's called license.ps1. 138 00:17:59,620 --> 00:18:00,120 It's the same. 139 00:18:06,900 --> 00:18:09,960 Now I will assign the policy. 140 00:18:11,200 --> 00:18:17,280 Here I just fill out the form that's needed for the remediation task to be run. 141 00:18:17,280 --> 00:18:31,660 So where the container instance will be created, where the storage account will be used by the container instance and where the PowerShell script will be downloaded and the remediation ID that will be used. 142 00:18:32,720 --> 00:18:47,220 So the video itself is a little bit more than 8 minutes but it's heavily edited because if I don't edit it will be like half an hour because I have to wait a lot of time for something to happen. 143 00:18:48,580 --> 00:18:56,600 Here I will just select the user-assigned manager identity that I have for the policy as well. 144 00:18:59,660 --> 00:19:09,300 And then I have a very simple Python-based web server where I will listen and wait for the service principal credentials. 145 00:19:09,940 --> 00:19:13,300 You can see it's the same IP address and same port. 146 00:19:34,020 --> 00:19:41,540 So now the policy gets successfully assigned and we will see the definition is there, DEFCON32. 147 00:19:42,080 --> 00:19:48,430 And then the assignments exist as well, same DEFCON32. 148 00:19:49,080 --> 00:19:52,330 And then in the compliance menu we will see again. 149 00:19:53,410 --> 00:19:56,170 It will have some different status. 150 00:19:56,170 --> 00:19:59,950 Here we see that it's not started yet, I just created. 151 00:20:00,110 --> 00:20:08,050 And then it will change after 10 minutes to compliant or non-compliant. 152 00:20:09,350 --> 00:20:11,410 So here we can see it's non-compliant. 153 00:20:11,410 --> 00:20:14,490 Then I will check on the remediation task itself. 154 00:20:19,390 --> 00:20:24,130 So here we can see that there is a remediation task and it's in evaluating. 155 00:20:25,930 --> 00:20:29,310 It takes a lot of time for the remediation task. 156 00:20:29,310 --> 00:20:37,470 Now it's in progress, which means all the steps are running now. 157 00:20:44,810 --> 00:20:46,710 And the magic happens. 158 00:20:49,410 --> 00:20:56,290 So you can see here is the app ID, the disk name, the password and the tenant ID for the service principal. 159 00:20:56,290 --> 00:21:08,870 And I can use this to log in and because this service principal has owner role at the subscription level, I can list all the resources and do a lot of things. 160 00:21:10,210 --> 00:21:19,650 So because the web server is running and the connection is still there, the remediation task is still in progress. 161 00:21:19,870 --> 00:21:26,190 But here we can see that the container instance is there and still running. 162 00:21:26,430 --> 00:21:36,290 And then the manager identity has the owner role and the additional manager identities have been created with the role assignments as well. 163 00:21:36,290 --> 00:21:45,770 And once I broke the connection or stopped the web server, then the remediation task will be completed. 164 00:21:48,170 --> 00:21:49,810 There's one more thing. 165 00:21:49,810 --> 00:22:01,790 So this is the deployment script and you will be able to see all the steps that happened because of the PowerShell script or what's defined in the PowerShell script itself. 166 00:22:01,790 --> 00:22:09,230 So I stopped the web server, I refreshed the remediation task and it will be now complete. 167 00:22:23,030 --> 00:22:31,670 And then I will just copy and paste the app ID, the password and the tenant ID. 168 00:22:36,990 --> 00:22:46,310 So you can see it's a successful login and I will just list all the resources just to show that I see the same stuff that's on the web UI. 169 00:22:46,310 --> 00:22:57,520 So one manager identity and the storage account that was created for the deployment script. 170 00:23:09,760 --> 00:23:15,120 And that's the storage account that was used to download the PowerShell script. 171 00:23:31,240 --> 00:23:38,540 So there are some improvements that can be done but as you can see it requires a lot. 172 00:23:38,800 --> 00:23:48,320 Basically everything is in the audit log and the deployment script is stored on the storage account and the remediation task run only once. 173 00:23:48,500 --> 00:23:51,160 I will just quickly go through the remaining slides. 174 00:23:51,160 --> 00:23:57,960 So there is an activity log button for the policy itself and then you will see all the steps. 175 00:23:58,560 --> 00:24:04,400 All the activities like the manager identity role assignment, the service principal creation. 176 00:24:04,940 --> 00:24:12,580 Here you can see that the license PowerShell script is there and the Azure CLI that I downloaded is all there. 177 00:24:12,820 --> 00:24:19,900 Also because we used and created the manager identity and service principal so it will be in the entry ID logs. 178 00:24:21,160 --> 00:24:36,840 And these are just the links for the Azure policy examples and the additional links are articles that are useful for creating these policies and learn more about that. 179 00:24:36,840 --> 00:24:37,420 So that's it. 180 00:24:37,420 --> 00:24:38,840 Thank you for listening.