Skip to content

Commit 2b7fe4c

Browse files
Merge pull request KelvinTegelaar#1077 from KelvinTegelaar/dev
Dev to release
2 parents 3ac3aae + bca97eb commit 2b7fe4c

26 files changed

Lines changed: 771 additions & 403 deletions

Config/SharePoint.BPATemplate.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@
3131
},
3232
{
3333
"name": "Resharing by external users",
34-
"value": "isResharingByExternalUsersEnabled",
35-
"formatter": "reverseBool"
34+
"value": "SharepointSettings.isResharingByExternalUsersEnabled",
35+
"formatter": "bool"
3636
},
3737
{
3838
"name": "Allow users to sync from unmanaged devices",
3939
"value": "SharepointSettings.isUnmanagedSyncAppForTenantRestricted",
40-
"formatter": "bool"
40+
"formatter": "reverseBool"
4141
},
4242
{
43-
"name": "Site creation by standards users enabled",
43+
"name": "Site creation by standard users enabled",
4444
"value": "SharepointSettings.isSiteCreationEnabled",
45-
"formatter": "reverseBool"
45+
"formatter": "bool"
4646
},
4747
{
4848
"name": "Deleted user data rention(days)",

Modules/CIPPCore/Public/Add-CIPPApplicationPermission.ps1

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,26 @@ function Add-CIPPApplicationPermission {
1111
}
1212
Set-Location (Get-Item $PSScriptRoot).FullName
1313
if ($RequiredResourceAccess -eq 'CIPPDefaults') {
14-
$RequiredResourceAccess = (Get-Content '.\SAMManifest.json' | ConvertFrom-Json).requiredResourceAccess
14+
#$RequiredResourceAccess = (Get-Content '.\SAMManifest.json' | ConvertFrom-Json).requiredResourceAccess
15+
16+
$Permissions = Get-CippSamPermissions -NoDiff
17+
$RequiredResourceAccess = [System.Collections.Generic.List[object]]::new()
18+
19+
foreach ($AppId in $Permissions.Permissions.PSObject.Properties.Name) {
20+
$AppPermissions = @($Permissions.Permissions.$AppId.applicationPermissions)
21+
$Resource = @{
22+
resourceAppId = $AppId
23+
resourceAccess = [System.Collections.Generic.List[object]]::new()
24+
}
25+
foreach ($Permission in $AppPermissions) {
26+
$Resource.ResourceAccess.Add(@{
27+
id = $Permission.id
28+
type = 'Role'
29+
})
30+
}
31+
32+
$RequiredResourceAccess.Add($Resource)
33+
}
1534
}
1635
$ServicePrincipalList = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/servicePrincipals?`$select=AppId,id,displayName&`$top=999" -skipTokenCache $true -tenantid $Tenantfilter -NoAuthCheck $true
1736
$ourSVCPrincipal = $ServicePrincipalList | Where-Object -Property AppId -EQ $ApplicationId

Modules/CIPPCore/Public/Add-CIPPDelegatedPermission.ps1

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,31 @@ function Add-CIPPDelegatedPermission {
1515
}
1616

1717
if ($RequiredResourceAccess -eq 'CIPPDefaults') {
18-
$RequiredResourceAccess = (Get-Content '.\SAMManifest.json' | ConvertFrom-Json).requiredResourceAccess
19-
$AdditionalPermissions = Get-Content '.\AdditionalPermissions.json' | ConvertFrom-Json
18+
$Permissions = Get-CippSamPermissions -NoDiff
19+
$NoTranslateRequired = $Permissions.Type -eq 'Table'
20+
$RequiredResourceAccess = [System.Collections.Generic.List[object]]::new()
21+
foreach ($AppId in $Permissions.Permissions.PSObject.Properties.Name) {
22+
$DelegatedPermissions = @($Permissions.Permissions.$AppId.delegatedPermissions)
23+
$ResourceAccess = [System.Collections.Generic.List[object]]::new()
24+
foreach ($Permission in $DelegatedPermissions) {
25+
$ResourceAccess.Add(@{
26+
id = $Permission.value
27+
type = 'Scope'
28+
})
29+
}
30+
$Resource = @{
31+
resourceAppId = $AppId
32+
resourceAccess = @($ResourceAccess)
33+
}
34+
$RequiredResourceAccess.Add($Resource)
35+
}
2036

2137
if ($Tenantfilter -eq $env:TenantID) {
2238
$RequiredResourceAccess = $RequiredResourceAccess + ($AdditionalPermissions | Where-Object { $RequiredResourceAccess.resourceAppId -notcontains $_.resourceAppId })
2339
} else {
2440
# remove the partner center permission if not pushing to partner tenant
2541
$RequiredResourceAccess = $RequiredResourceAccess | Where-Object { $_.resourceAppId -ne 'fa3d9a0c-3fb0-42cc-9193-47c7ecd2edbd' }
2642
}
27-
$RequiredResourceAccess = $RequiredResourceAccess + ($AdditionalPermissions | Where-Object { $RequiredResourceAccess.resourceAppId -notcontains $_.resourceAppId })
2843
}
2944
$Translator = Get-Content '.\PermissionsTranslator.json' | ConvertFrom-Json
3045
$ServicePrincipalList = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/servicePrincipals?`$select=AppId,id,displayName&`$top=999" -tenantid $Tenantfilter -skipTokenCache $true -NoAuthCheck $true
@@ -46,16 +61,22 @@ function Add-CIPPDelegatedPermission {
4661
continue
4762
}
4863
}
49-
$AdditionalScopes = ($AdditionalPermissions | Where-Object -Property resourceAppId -EQ $App.resourceAppId).resourceAccess | Where-Object -Property type -EQ 'Scope'
64+
5065
$DelegatedScopes = $App.resourceAccess | Where-Object -Property type -EQ 'Scope'
51-
if ($AdditionalScopes) {
52-
$NewScope = (@(($Translator | Where-Object { $_.id -in $DelegatedScopes.id }).value) + @($AdditionalScopes.id | Select-Object -Unique)) -join ' '
66+
if ($NoTranslateRequired) {
67+
$NewScope = @($DelegatedScopes | ForEach-Object { $_.id } | Sort-Object -Unique) -join ' '
5368
} else {
54-
if ($NoTranslateRequired) {
55-
$NewScope = @($DelegatedScopes | ForEach-Object { $_.id } | Sort-Object -Unique) -join ' '
56-
} else {
57-
$NewScope = @(($Translator | Where-Object { $_.id -in $DelegatedScopes.id }).value | Sort-Object -Unique) -join ' '
69+
$NewScope = foreach ($Scope in $DelegatedScopes.id) {
70+
if ($Scope -match '^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$') {
71+
$TranslatedScope = ($Translator | Where-Object -Property id -EQ $Scope).value
72+
if ($TranslatedScope) {
73+
$TranslatedScope
74+
}
75+
} else {
76+
$Scope
77+
}
5878
}
79+
$NewScope = (@($NewScope) | Sort-Object -Unique) -join ' '
5980
}
6081

6182
$OldScope = ($CurrentDelegatedScopes | Where-Object -Property Resourceid -EQ $svcPrincipalId.id)
@@ -83,7 +104,7 @@ function Add-CIPPDelegatedPermission {
83104
# Added permissions
84105
$Added = ($Compare | Where-Object { $_.SideIndicator -eq '=>' }).InputObject -join ' '
85106
$Removed = ($Compare | Where-Object { $_.SideIndicator -eq '<=' }).InputObject -join ' '
86-
$Results.add("Successfully updated permissions for $($svcPrincipalId.displayName). $(if ($Added) { "Added: $Added"}) $(if ($Removed) { "Removed: $Removed"})")
107+
$Results.add("Successfully updated permissions for $($svcPrincipalId.displayName). $(if ($Added) { "Added: $Added"}) $(if ($Removed) { "Removed: $Removed"})")
87108
}
88109
}
89110

Modules/CIPPCore/Public/Add-CIPPScheduledTask.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ function Add-CIPPScheduledTask {
5151
$task.Recurrence.value
5252
}
5353

54+
if ([int64]$task.ScheduledTime -eq 0 -or [string]::IsNullOrEmpty($task.ScheduledTime)) {
55+
$task.ScheduledTime = [int64](([datetime]::UtcNow) - (Get-Date '1/1/1970')).TotalSeconds
56+
}
57+
5458
$entity = @{
5559
PartitionKey = [string]'ScheduledTask'
5660
TaskState = [string]'Planned'
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function Get-CIPPAlertHuntressRogueApps {
2+
<#
3+
.SYNOPSIS
4+
Check for rogue apps in a Tenant
5+
.DESCRIPTION
6+
This function checks for rogue apps in the tenant by comparing the service principals in the tenant with a list of known rogue apps provided by Huntress.
7+
.FUNCTIONALITY
8+
Entrypoint
9+
.LINK
10+
https://huntresslabs.github.io/rogueapps/
11+
#>
12+
[CmdletBinding()]
13+
Param (
14+
[Parameter(Mandatory = $false)]
15+
[Alias('input')]
16+
$InputValue,
17+
$TenantFilter
18+
)
19+
20+
try {
21+
$RogueApps = Invoke-RestMethod -Uri 'https://raw.githubusercontent.com/huntresslabs/rogueapps/main/public/rogueapps.json'
22+
$RogueAppFilter = $RogueApps.appId -join "','"
23+
$ServicePrincipals = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/servicePrincipals?`$filter=appId in ('$RogueAppFilter')" -tenantid $TenantFilter
24+
25+
if (($ServicePrincipals | Measure-Object).Count -gt 0) {
26+
$AlertData = foreach ($ServicePrincipal in $ServicePrincipals) {
27+
$RogueApp = $RogueApps | Where-Object { $_.appId -eq $ServicePrincipal.appId }
28+
[pscustomobject]@{
29+
'App Name' = $RogueApp.appDisplayName
30+
'App Id' = $RogueApp.appId
31+
'Description' = $RogueApp.description
32+
'Enabled' = $ServicePrincipal.accountEnabled
33+
'Created' = $ServicePrincipal.createdDateTime
34+
'Tags' = $RogueApp.tags -join ', '
35+
'References' = $RogueApp.references -join ', '
36+
'Huntress Added' = $RogueApp.dateAdded
37+
}
38+
}
39+
Write-AlertTrace -cmdletName $MyInvocation.MyCommand -tenantFilter $TenantFilter -data $AlertData
40+
}
41+
} catch {
42+
#Write-AlertMessage -tenant $($TenantFilter) -message "Failed to check for rogue apps for $($TenantFilter): $(Get-NormalizedError -message $_.Exception.message)"
43+
}
44+
}

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Push-ExecScheduledCommand.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ function Push-ExecScheduledCommand {
44
Entrypoint
55
#>
66
param($Item)
7+
$item = $Item | ConvertTo-Json -Depth 100 | ConvertFrom-Json
78
Write-Host "We are going to be running a scheduled task: $($Item.TaskInfo | ConvertTo-Json -Depth 10)"
89

910
$Table = Get-CippTable -tablename 'ScheduledTasks'
@@ -99,8 +100,16 @@ function Push-ExecScheduledCommand {
99100
'(\d+)d$' { [int64]$matches[1] * 86400 }
100101
default { throw "Unsupported recurrence format: $($task.Recurrence)" }
101102
}
103+
104+
if ($secondsToAdd -gt 0) {
105+
$unixtimeNow = [int64](([datetime]::UtcNow) - (Get-Date '1/1/1970')).TotalSeconds
106+
if ([int64]$task.ScheduledTime -lt ($unixtimeNow - $secondsToAdd)) {
107+
$task.ScheduledTime = $unixtimeNow
108+
}
109+
}
110+
102111
$nextRunUnixTime = [int64]$task.ScheduledTime + [int64]$secondsToAdd
103-
Write-Host "The job is recurring and should occur again at: $nextRunUnixTime"
112+
Write-Host "The job is recurring. It was scheduled for $($task.ScheduledTime). The next runtime should be $nextRunUnixTime"
104113
Update-AzDataTableEntity @Table -Entity @{
105114
PartitionKey = $task.PartitionKey
106115
RowKey = $task.RowKey

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Webhooks/Push-AuditLogTenant.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ function Push-AuditLogTenant {
33

44
# Get Table contexts
55
$AuditBundleTable = Get-CippTable -tablename 'AuditLogBundles'
6-
$SchedulerConfig = Get-CIPPTable -TableName 'SchedulerConfig'
6+
$SchedulerConfig = Get-CippTable -TableName 'SchedulerConfig'
77
$WebhookTable = Get-CippTable -tablename 'webhookTable'
8-
$ConfigTable = Get-CIPPTable -TableName 'WebhookRules'
8+
$ConfigTable = Get-CippTable -TableName 'WebhookRules'
99

1010
# Query CIPPURL for linking
1111
$CIPPURL = Get-CIPPAzDataTableEntity @SchedulerConfig -Filter "PartitionKey eq 'webhookcreation'" | Select-Object -First 1 -ExpandProperty CIPPURL
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
function Invoke-ExecServicePrincipals {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
CIPP.Core.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$TenantFilter = $env:TenantId
12+
13+
$Success = $true
14+
15+
$Action = $Request.Query.Action ?? 'Default'
16+
try {
17+
switch ($Request.Query.Action) {
18+
'Create' {
19+
$Action = 'Create'
20+
if ($Request.Query.AppId -match '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$') {
21+
$Body = @{
22+
'appId' = $Request.Query.AppId
23+
} | ConvertTo-Json -Compress
24+
try {
25+
$Results = New-GraphPostRequest -Uri 'https://graph.microsoft.com/beta/servicePrincipals' -tenantid $TenantFilter -type POST -body $Body
26+
} catch {
27+
$Results = "Unable to create service principal: $($_.Exception.Message)"
28+
$Success = $false
29+
}
30+
} else {
31+
$Results = 'Invalid AppId'
32+
$Success = $false
33+
}
34+
}
35+
default {
36+
if ($Request.Query.AppId) {
37+
$Action = 'Get'
38+
$Results = New-GraphGetRequest -Uri "https://graph.microsoft.com/beta/servicePrincipals(appId='$($Request.Query.AppId)')" -tenantid $TenantFilter -NoAuthCheck $true
39+
} else {
40+
$Action = 'List'
41+
$Results = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/servicePrincipals?$top=999&$orderby=displayName&$count=true' -ComplexFilter -tenantid $TenantFilter -NoAuthCheck $true
42+
}
43+
}
44+
}
45+
} catch {
46+
$Results = $_.Exception.Message
47+
$Success = $false
48+
}
49+
50+
$Metadata = @{
51+
'Action' = $Action
52+
'Success' = $Success
53+
}
54+
55+
if ($Request.Query.AppId) {
56+
$Metadata.AppId = $Request.Query.AppId
57+
}
58+
59+
$Body = @{
60+
'Results' = $Results
61+
'Metadata' = $Metadata
62+
}
63+
64+
$Json = $Body | ConvertTo-Json -Depth 10 -Compress
65+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
66+
StatusCode = [HttpStatusCode]::OK
67+
Body = $Json
68+
})
69+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function Invoke-ExecSAMAppPermissions {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
CIPP.SuperAdmin.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
switch ($Request.Query.Action) {
12+
'Update' {
13+
try {
14+
$Permissions = $Request.Body.Permissions
15+
$Entity = @{
16+
'PartitionKey' = 'CIPP-SAM'
17+
'RowKey' = 'CIPP-SAM'
18+
'Permissions' = [string]($Permissions.Permissions | ConvertTo-Json -Depth 10 -Compress)
19+
}
20+
$Table = Get-CIPPTable -TableName 'AppPermissions'
21+
$null = Add-CIPPAzDataTableEntity @Table -Entity $Entity -Force
22+
$Body = @{
23+
'Results' = 'Permissions Updated'
24+
}
25+
} catch {
26+
$Body = @{
27+
'Results' = $_.Exception.Message
28+
}
29+
}
30+
}
31+
default {
32+
$Body = Get-CippSamPermissions
33+
}
34+
}
35+
36+
37+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
38+
StatusCode = [HttpStatusCode]::OK
39+
Body = ConvertTo-Json -Depth 10 -InputObject $Body
40+
})
41+
42+
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Endpoint/MEM/Invoke-AddDefenderDeployment.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ Function Invoke-AddDefenderDeployment {
3535
iosMobileApplicationManagementEnabled = [bool]$Compliance.appSync
3636
microsoftDefenderForEndpointAttachEnabled = [bool]$true
3737
} | ConvertTo-Json -Compress
38-
$SettingsRequest = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/mobileThreatDefenseConnectors/' -tenantid $tenant -type POST -body $SettingsObj -AsApp $true
39-
"$($Tenant): Successfully set Defender Compliance and Reporting settings"
38+
$ExistingSettings = New-GraphGETRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/mobileThreatDefenseConnectors/fc780465-2017-40d4-a0c5-307022471b92' -tenantid $tenant
39+
if ($ExistingSettings) {
40+
"Defender Intune Configuration already active for $($Tenant). Skipping"
41+
} else {
42+
$SettingsRequest = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/mobileThreatDefenseConnectors/' -tenantid $tenant -type POST -body $SettingsObj -AsApp $true
43+
"$($Tenant): Successfully set Defender Compliance and Reporting settings"
44+
}
45+
4046

4147
$Settings = switch ($PolicySettings) {
4248
{ $_.ScanArchives } {
@@ -210,11 +216,11 @@ Function Invoke-AddDefenderDeployment {
210216
settings = @($EDRSettings)
211217
}
212218
Write-Host ( $EDRbody)
213-
$CheckExististingEDR = New-GraphGETRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/configurationPolicies' -tenantid $tenant
219+
$CheckExististingEDR = New-GraphGETRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/configurationPolicies' -tenantid $tenant | Where-Object -Property Name -EQ 'EDR Configuration'
214220
if ('EDR Configuration' -in $CheckExististingEDR.Name) {
215221
"$($Tenant): EDR Policy already exists. Skipping"
216222
} else {
217-
#$EDRRequest = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/configurationPolicies' -tenantid $tenant -type POST -body $EDRbody
223+
$EDRRequest = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/configurationPolicies' -tenantid $tenant -type POST -body $EDRbody
218224
if ($ASR.AssignTo -ne 'none') {
219225
$AssignBody = if ($ASR.AssignTo -ne 'AllDevicesAndUsers') { '{"assignments":[{"id":"","target":{"@odata.type":"#microsoft.graph.' + $($asr.AssignTo) + 'AssignmentTarget"}}]}' } else { '{"assignments":[{"id":"","target":{"@odata.type":"#microsoft.graph.allDevicesAssignmentTarget"}},{"id":"","target":{"@odata.type":"#microsoft.graph.allLicensedUsersAssignmentTarget"}}]}' }
220226
$assign = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies('$($EDRRequest.id)')/assign" -tenantid $tenant -type POST -body $AssignBody

0 commit comments

Comments
 (0)