[00:00.000 --> 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. [00:10.580 --> 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. [00:16.760 --> 00:19.600] The title of the talk is Creating Azure Policy Compliant Backdoor. [00:19.600 --> 00:21.280] Please welcome Victor. [00:27.250 --> 00:28.370] Thank you everyone. [00:28.370 --> 00:29.930] Can you hear me well? [00:30.650 --> 00:31.390] Good. [00:31.390 --> 00:34.370] Because I'm a little bit quiet. [00:34.630 --> 00:36.090] So let's get started. [00:36.090 --> 00:37.730] So, my name is Victor. [00:37.730 --> 00:39.450] Thank you for the introduction. [00:39.450 --> 00:44.470] I'm a principal security consultant and I work at the NCC Group. [00:44.550 --> 00:48.870] I'm specialized in cloud and CICT security. [00:52.690 --> 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. [01:01.090 --> 01:02.590] So that's why I keep coming back. [01:04.650 --> 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. [01:17.670 --> 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. [01:35.010 --> 01:41.650] So, the policy itself is written in JSON and it's called a policy definition. [01:41.650 --> 01:47.870] If you group more than one policy definition together, then it's called a policy initiative. [01:48.530 --> 01:57.410] You assign this policy to a scope, which can be a resource, resource group, subscription, and management group. [01:57.410 --> 02:06.490] And you can also fix the non-compliant resources and that you can do with the remediation or remediation class. [02:08.190 --> 02:10.550] So this is how it looks like. [02:10.550 --> 02:12.470] This is a very basic one. [02:12.570 --> 02:19.110] You will see that the display, the description, and the mode, that's the metadata. [02:19.290 --> 02:26.330] You can use some parameters, so you can reuse your policy and assign it multiple times with different values. [02:26.330 --> 02:33.510] And then you have the policy rule with logical evaluation and effects. [02:33.750 --> 02:36.510] So, basically, you have an if-then case. [02:36.510 --> 02:41.450] So, if there is a resource that has a certain property, then do this. [02:41.450 --> 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. [02:52.550 --> 02:58.510] And if you do a little bit more, then you can go up to back this. [02:58.510 --> 03:04.650] So this is probably, you can't see it, but this is more than 200 lines of JSON code. [03:05.130 --> 03:08.670] But this is an actual existing policy as well. [03:10.570 --> 03:17.170] So, to create one policy, we will need something to work on. [03:17.170 --> 03:21.850] So we will need the property of the Azure resource itself. [03:21.990 --> 03:31.430] And for that, we will look for something called policy areas and resource properties. [03:31.430 --> 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. [03:42.530 --> 03:46.650] I will have some examples on how to look for this. [03:47.170 --> 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. [04:02.870 --> 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. [04:15.230 --> 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. [04:27.730 --> 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. [04:39.150 --> 04:48.450] So we will look for a storage account, and also the storage account that has the support HTTPS traffic only properties. [04:48.450 --> 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. [05:02.770 --> 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. [05:20.070 --> 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. [05:29.070 --> 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. [05:43.430 --> 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. [05:56.110 --> 06:02.730] So sometimes it's a storage account, but you can modify the compute or something else. [06:03.890 --> 06:09.230] These as well, or if the second one is for a modifiable attribute. [06:15.130 --> 06:23.050] There is one important thing that I wanted to mention, that this is Azure policy, and this is for Azure resources. [06:23.050 --> 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. [06:35.130 --> 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. [06:47.250 --> 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. [07:02.290 --> 07:08.370] And at the end of my slides I included a couple of links, they are collected together. [07:10.030 --> 07:15.310] You need these two permissions at least just to create the policy itself. [07:16.270 --> 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. [07:27.150 --> 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. [07:40.490 --> 07:47.850] But you can basically force it on-demand as well, or call it on-demand as well. [07:48.110 --> 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. [08:02.930 --> 08:08.190] I think this way you can speed up a little bit, but it will take still a little bit of time. [08:12.230 --> 08:19.070] So basically we can use this as a resource enumeration service as well. [08:19.070 --> 08:21.890] I included three examples. [08:22.590 --> 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. [08:32.210 --> 08:37.530] And the last one is we want to see all the virtual machines with public IP addresses. [08:40.490 --> 08:44.430] So the first one is listing all the storage accounts. [08:44.490 --> 08:50.450] Basically we will just use this path and the property that we are looking for. [08:50.890 --> 08:57.850] So microsoft.storage/.storageaccounts, that's the field that we will put in the policy rule itself. [08:59.250 --> 09:04.670] And this is a working example to list all the storage accounts. [09:04.670 --> 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. [09:18.350 --> 09:22.370] And I did the same for these examples as well. [09:23.670 --> 09:29.950] The next one is the storage account that has allow public access property. [09:29.950 --> 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. [09:41.170 --> 09:46.330] So this will say if the storage account has public access or not. [09:46.510 --> 09:53.710] And I will use the CLI command just to make sure there are no more than one property. [09:54.410 --> 10:01.450] And then I will put it in the policy rule, so I put two properties. [10:01.450 --> 10:09.090] The one is storage account, the other one is the allow public access that was in the previous slide. [10:09.330 --> 10:18.330] You can use something as not or without the not and also like all of or any of. [10:18.330 --> 10:23.690] So you can combine a lot of properties into one policy rule. [10:23.710 --> 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. [10:35.130 --> 10:37.750] And this is the whole policy itself. [10:40.190 --> 10:44.370] The last one is other virtual machines with external IP. [10:44.370 --> 10:49.490] And it turned out that this is an already existing policy and it's built-in. [10:49.490 --> 10:54.550] I just included the GitHub repo for it as well. [10:56.950 --> 11:00.870] Basically the policy, we are looking for the network interface. [11:01.090 --> 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. [11:13.530 --> 11:17.910] And this is the policy that I was talking about. [11:17.910 --> 11:23.790] Here what you have to change is the effect itself because here it's denied. [11:23.790 --> 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. [11:37.190 --> 11:46.490] So I created a proof-of-concept policy that will create a service principal. [11:46.490 --> 11:54.590] And with the service principal you can log in back and you will have owner roles on the subscription. [11:54.910 --> 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. [12:03.710 --> 12:06.290] I will go for no. [12:06.490 --> 12:13.510] And I will also have a remediation task and I have to use the deploy-if-not-exists effect. [12:13.510 --> 12:19.330] Because this is the only way I know that we can run the PowerShell script. [12:21.550 --> 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. [12:29.570 --> 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. [12:40.790 --> 12:45.750] But it's referenced and will be hosted at a GitHub repository. [12:47.150 --> 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. [12:59.050 --> 13:02.230] So as I mentioned, it will create a service principal. [13:02.230 --> 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. [13:12.970 --> 13:18.650] So the tenant ID, app ID and the secret for the logging. [13:19.330 --> 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. [13:30.190 --> 13:35.110] The remediation task is only run once, so this is very important. [13:35.730 --> 13:42.530] So it won't keep making the service principal, it will only create once. [13:42.610 --> 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. [13:52.990 --> 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. [14:04.930 --> 14:11.490] So this is a way to visualize what will happen and what's in the Azure policy and remediation task. [14:11.490 --> 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. [14:21.690 --> 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. [14:34.870 --> 14:41.610] So this Azure container instance will be created automatically because we are using that deployment script. [14:45.550 --> 14:49.350] And I will just show the demo video. [15:03.570 --> 15:10.910] So we can see that there is nothing in the compliance menu at the moment. [15:10.910 --> 15:13.970] There is no policy assignments as well. [15:14.750 --> 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. [15:24.070 --> 15:27.190] So I will create now the backdoor policy. [15:27.270 --> 15:30.330] I will assign it to the subscription level. [15:30.670 --> 15:35.130] We have to use some kind of category, it doesn't really matter. [15:42.090 --> 15:44.550] And I will name it Defcon32. [15:45.410 --> 15:53.370] Here we can see that the policy and the remediation task will require an owner role in order to successfully run. [15:54.210 --> 15:59.290] Because I will need some permission to do the stuff, the things. [15:59.290 --> 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. [16:09.810 --> 16:14.630] And as you might see, there are identities not found. [16:14.630 --> 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. [16:31.830 --> 16:35.030] It's just some pre-existing resources. [16:35.030 --> 16:37.490] That's the managed identity I will use. [16:37.630 --> 16:39.410] I have three storage accounts. [16:39.410 --> 16:44.270] One will be used, as mentioned before, for downloading the PowerShell script. [16:46.370 --> 16:48.870] This is the PowerShell script. [16:48.870 --> 16:51.870] I called it license.ps1. [16:51.870 --> 16:57.790] It will do like creating storage account, managed identity, install Azure CI and so on. [17:07.760 --> 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. [17:21.880 --> 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. [17:32.720 --> 17:36.580] And I will use the deploy-if-not-exist effect. [17:36.700 --> 17:42.560] Here's the same role definition that's required that we saw in the web UI. [17:42.680 --> 17:48.140] You can see this is a deployment template and this is the second deployment template. [17:49.080 --> 17:56.580] And then the deployment script which is referenced at the GitHub repository. [17:57.800 --> 17:59.620] And it's called license.ps1. [17:59.620 --> 18:00.120] It's the same. [18:06.900 --> 18:09.960] Now I will assign the policy. [18:11.200 --> 18:17.280] Here I just fill out the form that's needed for the remediation task to be run. [18:17.280 --> 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. [18:32.720 --> 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. [18:48.580 --> 18:56.600] Here I will just select the user-assigned manager identity that I have for the policy as well. [18:59.660 --> 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. [19:09.940 --> 19:13.300] You can see it's the same IP address and same port. [19:34.020 --> 19:41.540] So now the policy gets successfully assigned and we will see the definition is there, DEFCON32. [19:42.080 --> 19:48.430] And then the assignments exist as well, same DEFCON32. [19:49.080 --> 19:52.330] And then in the compliance menu we will see again. [19:53.410 --> 19:56.170] It will have some different status. [19:56.170 --> 19:59.950] Here we see that it's not started yet, I just created. [20:00.110 --> 20:08.050] And then it will change after 10 minutes to compliant or non-compliant. [20:09.350 --> 20:11.410] So here we can see it's non-compliant. [20:11.410 --> 20:14.490] Then I will check on the remediation task itself. [20:19.390 --> 20:24.130] So here we can see that there is a remediation task and it's in evaluating. [20:25.930 --> 20:29.310] It takes a lot of time for the remediation task. [20:29.310 --> 20:37.470] Now it's in progress, which means all the steps are running now. [20:44.810 --> 20:46.710] And the magic happens. [20:49.410 --> 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. [20:56.290 --> 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. [21:10.210 --> 21:19.650] So because the web server is running and the connection is still there, the remediation task is still in progress. [21:19.870 --> 21:26.190] But here we can see that the container instance is there and still running. [21:26.430 --> 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. [21:36.290 --> 21:45.770] And once I broke the connection or stopped the web server, then the remediation task will be completed. [21:48.170 --> 21:49.810] There's one more thing. [21:49.810 --> 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. [22:01.790 --> 22:09.230] So I stopped the web server, I refreshed the remediation task and it will be now complete. [22:23.030 --> 22:31.670] And then I will just copy and paste the app ID, the password and the tenant ID. [22:36.990 --> 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. [22:46.310 --> 22:57.520] So one manager identity and the storage account that was created for the deployment script. [23:09.760 --> 23:15.120] And that's the storage account that was used to download the PowerShell script. [23:31.240 --> 23:38.540] So there are some improvements that can be done but as you can see it requires a lot. [23:38.800 --> 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. [23:48.500 --> 23:51.160] I will just quickly go through the remaining slides. [23:51.160 --> 23:57.960] So there is an activity log button for the policy itself and then you will see all the steps. [23:58.560 --> 24:04.400] All the activities like the manager identity role assignment, the service principal creation. [24:04.940 --> 24:12.580] Here you can see that the license PowerShell script is there and the Azure CLI that I downloaded is all there. [24:12.820 --> 24:19.900] Also because we used and created the manager identity and service principal so it will be in the entry ID logs. [24:21.160 --> 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. [24:36.840 --> 24:37.420] So that's it. [24:37.420 --> 24:38.840] Thank you for listening.