start	end	text
0	10580	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.
10580	16760	In this talk, Victor is going to speak on Azure Policy, what it is, how to write one, what permissions are needed, and more.
16760	19600	The title of the talk is Creating Azure Policy Compliant Backdoor.
19600	21280	Please welcome Victor.
27250	28370	Thank you everyone.
28370	29930	Can you hear me well?
30650	31390	Good.
31390	34370	Because I'm a little bit quiet.
34630	36090	So let's get started.
36090	37730	So, my name is Victor.
37730	39450	Thank you for the introduction.
39450	44470	I'm a principal security consultant and I work at the NCC Group.
44550	48870	I'm specialized in cloud and CICT security.
52690	61090	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.
61090	62590	So that's why I keep coming back.
64650	77670	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.
77670	92850	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.
95010	101650	So, the policy itself is written in JSON and it's called a policy definition.
101650	107870	If you group more than one policy definition together, then it's called a policy initiative.
108530	117410	You assign this policy to a scope, which can be a resource, resource group, subscription, and management group.
117410	126490	And you can also fix the non-compliant resources and that you can do with the remediation or remediation class.
128190	130550	So this is how it looks like.
130550	132470	This is a very basic one.
132570	139110	You will see that the display, the description, and the mode, that's the metadata.
139290	146330	You can use some parameters, so you can reuse your policy and assign it multiple times with different values.
146330	153510	And then you have the policy rule with logical evaluation and effects.
153750	156510	So, basically, you have an if-then case.
156510	161450	So, if there is a resource that has a certain property, then do this.
161450	169270	For example, if there is a storage account with public access, then out it or just deny and don't create it.
172550	178510	And if you do a little bit more, then you can go up to back this.
178510	184650	So this is probably, you can't see it, but this is more than 200 lines of JSON code.
185130	188670	But this is an actual existing policy as well.
190570	197170	So, to create one policy, we will need something to work on.
197170	201850	So we will need the property of the Azure resource itself.
201990	211430	And for that, we will look for something called policy areas and resource properties.
211430	221390	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.
222530	226650	I will have some examples on how to look for this.
227170	240850	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.
242870	255230	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.
255230	267590	And once I check the properties itself on the deployment template, I can see it is translated to the supports HTTPS traffic only property.
267730	279150	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.
279150	288450	So we will look for a storage account, and also the storage account that has the support HTTPS traffic only properties.
288450	298850	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.
302770	319810	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.
320070	328390	So this is the Azure CLI command that will give you all the areas that you can use in the policy rule.
329070	341990	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.
343430	356110	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.
356110	362730	So sometimes it's a storage account, but you can modify the compute or something else.
363890	369230	These as well, or if the second one is for a modifiable attribute.
375130	383050	There is one important thing that I wanted to mention, that this is Azure policy, and this is for Azure resources.
383050	395010	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.
395130	407250	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.
407250	422150	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.
422290	428370	And at the end of my slides I included a couple of links, they are collected together.
430030	435310	You need these two permissions at least just to create the policy itself.
436270	445770	If you use remediation tasks, then you will use a managed identity, and those managed identities will need more permissions as well.
447150	460490	So the policy itself is evaluated when the resource within the scope is created or updated, and also every 24 hours it will run.
460490	467850	But you can basically force it on-demand as well, or call it on-demand as well.
468110	482930	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.
482930	488190	I think this way you can speed up a little bit, but it will take still a little bit of time.
492230	499070	So basically we can use this as a resource enumeration service as well.
499070	501890	I included three examples.
502590	511850	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.
512210	517530	And the last one is we want to see all the virtual machines with public IP addresses.
520490	524430	So the first one is listing all the storage accounts.
524490	530450	Basically we will just use this path and the property that we are looking for.
530890	537850	So microsoft.storage/.storageaccounts, that's the field that we will put in the policy rule itself.
539250	544670	And this is a working example to list all the storage accounts.
544670	557350	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.
558350	562370	And I did the same for these examples as well.
563670	569950	The next one is the storage account that has allow public access property.
569950	580970	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.
581170	586330	So this will say if the storage account has public access or not.
586510	593710	And I will use the CLI command just to make sure there are no more than one property.
594410	601450	And then I will put it in the policy rule, so I put two properties.
601450	609090	The one is storage account, the other one is the allow public access that was in the previous slide.
609330	618330	You can use something as not or without the not and also like all of or any of.
618330	623690	So you can combine a lot of properties into one policy rule.
623710	633050	And you can see like I am looking for three different properties, but it's enough if only one of them is true.
635130	637750	And this is the whole policy itself.
640190	644370	The last one is other virtual machines with external IP.
644370	649490	And it turned out that this is an already existing policy and it's built-in.
649490	654550	I just included the GitHub repo for it as well.
656950	660870	Basically the policy, we are looking for the network interface.
661090	670570	And this other property is an array, so it will check if all the elements in the array is there or not.
673530	677910	And this is the policy that I was talking about.
677910	683790	Here what you have to change is the effect itself because here it's denied.
683790	693350	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.
697190	706490	So I created a proof-of-concept policy that will create a service principal.
706490	714590	And with the service principal you can log in back and you will have owner roles on the subscription.
714910	723450	So the proof-of-concept policy will check if a storage account or the storage account has this HTTPS enabled or not.
723710	726290	I will go for no.
726490	733510	And I will also have a remediation task and I have to use the deploy-if-not-exists effect.
733510	739330	Because this is the only way I know that we can run the PowerShell script.
741550	748990	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.
749570	760790	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.
760790	765750	But it's referenced and will be hosted at a GitHub repository.
767150	777550	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.
779050	782230	So as I mentioned, it will create a service principal.
782230	792970	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.
792970	798650	So the tenant ID, app ID and the secret for the logging.
799330	810190	So as mentioned before, the policy is run every 24 hours, but I will talk about this at the problem section as well.
810190	815110	The remediation task is only run once, so this is very important.
815730	822530	So it won't keep making the service principal, it will only create once.
822610	832990	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.
832990	842150	So for example, there will be two additional managed identities it created or an additional storage account and some role assignments as well.
844930	851490	So this is a way to visualize what will happen and what's in the Azure policy and remediation task.
851490	861690	So there will be a nested deployment template with a deployment script and the deployment script itself is hosted at a GitHub repository.
861690	874830	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.
874870	881610	So this Azure container instance will be created automatically because we are using that deployment script.
885550	889350	And I will just show the demo video.
903570	910910	So we can see that there is nothing in the compliance menu at the moment.
910910	913970	There is no policy assignments as well.
914750	923930	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.
924070	927190	So I will create now the backdoor policy.
927270	930330	I will assign it to the subscription level.
930670	935130	We have to use some kind of category, it doesn't really matter.
942090	944550	And I will name it Defcon32.
945410	953370	Here we can see that the policy and the remediation task will require an owner role in order to successfully run.
954210	959290	Because I will need some permission to do the stuff, the things.
959290	968230	At this moment, my managed identity, I call it managed identity, but it's not there so it doesn't have any role.
969810	974630	And as you might see, there are identities not found.
974630	983490	That's because when you use a remediation task with system-managed identities and it's deleted, then it will leave some garbage behind.
991830	995030	It's just some pre-existing resources.
995030	997490	That's the managed identity I will use.
997630	999410	I have three storage accounts.
999410	1004270	One will be used, as mentioned before, for downloading the PowerShell script.
1006370	1008870	This is the PowerShell script.
1008870	1011870	I called it license.ps1.
1011870	1017790	It will do like creating storage account, managed identity, install Azure CI and so on.
1027760	1040460	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.
1041880	1052720	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.
1052720	1056580	And I will use the deploy-if-not-exist effect.
1056700	1062560	Here's the same role definition that's required that we saw in the web UI.
1062680	1068140	You can see this is a deployment template and this is the second deployment template.
1069080	1076580	And then the deployment script which is referenced at the GitHub repository.
1077800	1079620	And it's called license.ps1.
1079620	1080120	It's the same.
1086900	1089960	Now I will assign the policy.
1091200	1097280	Here I just fill out the form that's needed for the remediation task to be run.
1097280	1111660	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.
1112720	1127220	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.
1128580	1136600	Here I will just select the user-assigned manager identity that I have for the policy as well.
1139660	1149300	And then I have a very simple Python-based web server where I will listen and wait for the service principal credentials.
1149940	1153300	You can see it's the same IP address and same port.
1174020	1181540	So now the policy gets successfully assigned and we will see the definition is there, DEFCON32.
1182080	1188430	And then the assignments exist as well, same DEFCON32.
1189080	1192330	And then in the compliance menu we will see again.
1193410	1196170	It will have some different status.
1196170	1199950	Here we see that it's not started yet, I just created.
1200110	1208050	And then it will change after 10 minutes to compliant or non-compliant.
1209350	1211410	So here we can see it's non-compliant.
1211410	1214490	Then I will check on the remediation task itself.
1219390	1224130	So here we can see that there is a remediation task and it's in evaluating.
1225930	1229310	It takes a lot of time for the remediation task.
1229310	1237470	Now it's in progress, which means all the steps are running now.
1244810	1246710	And the magic happens.
1249410	1256290	So you can see here is the app ID, the disk name, the password and the tenant ID for the service principal.
1256290	1268870	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.
1270210	1279650	So because the web server is running and the connection is still there, the remediation task is still in progress.
1279870	1286190	But here we can see that the container instance is there and still running.
1286430	1296290	And then the manager identity has the owner role and the additional manager identities have been created with the role assignments as well.
1296290	1305770	And once I broke the connection or stopped the web server, then the remediation task will be completed.
1308170	1309810	There's one more thing.
1309810	1321790	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.
1321790	1329230	So I stopped the web server, I refreshed the remediation task and it will be now complete.
1343030	1351670	And then I will just copy and paste the app ID, the password and the tenant ID.
1356990	1366310	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.
1366310	1377520	So one manager identity and the storage account that was created for the deployment script.
1389760	1395120	And that's the storage account that was used to download the PowerShell script.
1411240	1418540	So there are some improvements that can be done but as you can see it requires a lot.
1418800	1428320	Basically everything is in the audit log and the deployment script is stored on the storage account and the remediation task run only once.
1428500	1431160	I will just quickly go through the remaining slides.
1431160	1437960	So there is an activity log button for the policy itself and then you will see all the steps.
1438560	1444400	All the activities like the manager identity role assignment, the service principal creation.
1444940	1452580	Here you can see that the license PowerShell script is there and the Azure CLI that I downloaded is all there.
1452820	1459900	Also because we used and created the manager identity and service principal so it will be in the entry ID logs.
1461160	1476840	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.
1476840	1477420	So that's it.
1477420	1478840	Thank you for listening.
