Skip to content

Commit 98a776c

Browse files
committed
feat(code-execution-restriction): ✨ Added code execution restriction functions
1 parent 505006f commit 98a776c

5 files changed

+153
-1
lines changed

src/PSMDE.psd1

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'PSMDE.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.20.0'
15+
ModuleVersion = '0.19.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -72,6 +72,10 @@
7272
FunctionsToExport = @(
7373
'Add-MdeMachineTag'
7474
'Clear-MdeAuthorizationInfo'
75+
'Disable-MdeMachineCodeExecutionRestriction'
76+
'Disable-MdeMachineIsolation'
77+
'Enable-MdeMachineCodeExecutionRestriction'
78+
'Enable-MdeMachineIsolation'
7579
'Get-MdeAuthorizationInfo'
7680
'Get-MdeBaselineComplianceAssessmentByMachine'
7781
'Get-MdeBaselineComplianceAssessmentExport'
@@ -80,13 +84,15 @@
8084
'Get-MdeConfigurationScore'
8185
'Get-MdeExposureScore'
8286
'Get-MdeExposureScoreByMachineGroups'
87+
'Get-MdeLiveResponseResult'
8388
'Get-MdeMachine'
8489
'Get-MdeMachineAction'
8590
'Get-MdeMachineAlerts'
8691
'Get-MdeMachineByFilter'
8792
'Get-MdeMachineByIp'
8893
'Get-MdeMachineByTag'
8994
'Get-MdeMachineInvestigationPackage'
95+
'Get-MdeMachineInvestigationPackageUri'
9096
'Get-MdeMachineLogonUsers'
9197
'Get-MdeMachineMissingKbs'
9298
'Get-MdeMachineRecommendations'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<#
2+
.SYNOPSIS
3+
Restrict execution of all applications on the device except a predefined set.
4+
5+
.DESCRIPTION
6+
Restrict execution of all applications on the device except a predefined set.
7+
8+
.NOTES
9+
Author: Jan-Henrik Damaschke
10+
11+
.LINK
12+
https://learn.microsoft.com/en-us/microsoft-365/security/defender-endpoint/restrict-code-execution?view=o365-worldwide
13+
14+
.PARAMETER id
15+
Optional. Specifies the id of the target MDE recommendation.
16+
17+
.EXAMPLE
18+
Disable-MdeMachineCodeExecutionRestriction -id "<GUID>" -comment "Your comment"
19+
20+
.ROLE
21+
@(@{permission = 'Machine.RestrictExecution'; permissionType = 'Application'}, @{permission = 'Machine.RestrictExecution'; permissionType = 'Delegated'})
22+
#>
23+
24+
function Disable-MdeMachineCodeExecutionRestriction {
25+
[CmdletBinding()]
26+
param (
27+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
28+
[string]
29+
$id,
30+
[Parameter(Mandatory)]
31+
[string]
32+
$comment
33+
)
34+
Begin {
35+
if (-not (Test-MdePermissions -functionName $PSCmdlet.CommandRuntime)) {
36+
$requiredRoles = (Get-Help $PSCmdlet.CommandRuntime -Full).role | Invoke-Expression
37+
Throw "Missing required permission(s). Please check if one of these is in current token roles: $($requiredRoles.permission)"
38+
}
39+
}
40+
Process {
41+
return Invoke-RetryRequest -Method Post -Uri "https://api.securitycenter.microsoft.com/api/machines/$id/unrestrictCodeExecution" -body (ConvertTo-Json -InputObject @{ Comment = $comment })
42+
}
43+
End {}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<#
2+
.SYNOPSIS
3+
Restrict execution of all applications on the device except a predefined set.
4+
5+
.DESCRIPTION
6+
Restrict execution of all applications on the device except a predefined set.
7+
8+
.NOTES
9+
Author: Jan-Henrik Damaschke
10+
11+
.LINK
12+
https://learn.microsoft.com/en-us/microsoft-365/security/defender-endpoint/restrict-code-execution?view=o365-worldwide
13+
14+
.PARAMETER id
15+
Optional. Specifies the id of the target MDE recommendation.
16+
17+
.EXAMPLE
18+
Enable-MdeMachineCodeExecutionRestriction -id "<GUID>" -comment "Your comment"
19+
20+
.ROLE
21+
@(@{permission = 'Machine.RestrictExecution'; permissionType = 'Application'}, @{permission = 'Machine.RestrictExecution'; permissionType = 'Delegated'})
22+
#>
23+
24+
function Enable-MdeMachineCodeExecutionRestriction {
25+
[CmdletBinding()]
26+
param (
27+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
28+
[string]
29+
$id,
30+
[Parameter(Mandatory)]
31+
[string]
32+
$comment
33+
)
34+
Begin {
35+
if (-not (Test-MdePermissions -functionName $PSCmdlet.CommandRuntime)) {
36+
$requiredRoles = (Get-Help $PSCmdlet.CommandRuntime -Full).role | Invoke-Expression
37+
Throw "Missing required permission(s). Please check if one of these is in current token roles: $($requiredRoles.permission)"
38+
}
39+
}
40+
Process {
41+
return Invoke-RetryRequest -Method Post -Uri "https://api.securitycenter.microsoft.com/api/machines/$id/restrictCodeExecution" -body (ConvertTo-Json -InputObject @{ Comment = $comment })
42+
}
43+
End {}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BeforeAll {
2+
Remove-Module PSMDE -Force -ErrorAction SilentlyContinue
3+
Import-Module (Split-Path $PSCommandPath).replace('tests', 'src').Replace('public', 'PSMDE.psd1')
4+
}
5+
6+
Describe "Disable-MdeMachineCodeExecutionRestriction" {
7+
8+
It 'Should have the PSMDE module loaded' {
9+
$module = Get-Module PSMDE
10+
$module | Should -Not -BeNullOrEmpty
11+
}
12+
13+
It 'Should have access to internal functions' {
14+
InModuleScope PSMDE {
15+
$iar = Get-Command Invoke-AzureRequest
16+
$iar | Should -Not -BeNullOrEmpty
17+
}
18+
}
19+
20+
It 'Should correctly create the request uri' {
21+
InModuleScope PSMDE {
22+
Mock Invoke-RetryRequest { return $uri }
23+
Mock Test-MdePermissions { return $true }
24+
$id = '12345'
25+
$comment = 'Comment'
26+
Disable-MdeMachineCodeExecutionRestriction -id $id -comment $comment | Should -Be "https://api.securitycenter.microsoft.com/api/machines/$id/unrestrictCodeExecution"
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BeforeAll {
2+
Remove-Module PSMDE -Force -ErrorAction SilentlyContinue
3+
Import-Module (Split-Path $PSCommandPath).replace('tests', 'src').Replace('public', 'PSMDE.psd1')
4+
}
5+
6+
Describe "Enable-MdeMachineCodeExecutionRestriction" {
7+
8+
It 'Should have the PSMDE module loaded' {
9+
$module = Get-Module PSMDE
10+
$module | Should -Not -BeNullOrEmpty
11+
}
12+
13+
It 'Should have access to internal functions' {
14+
InModuleScope PSMDE {
15+
$iar = Get-Command Invoke-AzureRequest
16+
$iar | Should -Not -BeNullOrEmpty
17+
}
18+
}
19+
20+
It 'Should correctly create the request uri' {
21+
InModuleScope PSMDE {
22+
Mock Invoke-RetryRequest { return $uri }
23+
Mock Test-MdePermissions { return $true }
24+
$id = '12345'
25+
$comment = 'Comment'
26+
Enable-MdeMachineCodeExecutionRestriction -id $id -comment $comment | Should -Be "https://api.securitycenter.microsoft.com/api/machines/$id/restrictCodeExecution"
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)