Skip to content

Commit 28769cf

Browse files
committed
update internal modules
1 parent 7de0110 commit 28769cf

8 files changed

Lines changed: 107 additions & 44 deletions

File tree

core/modules/monkeycloudutils/monkeycloudutils.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ FunctionsToExport = @(
8888
'Get-MSGraphOrganization',
8989
'Test-IsValidTenantId',
9090
'New-MsalApplicationForSPO',
91-
'New-MsalApplicationForPnP'
91+
'New-MsalApplicationForPnP',
92+
'Get-MSServiceFromAudience'
9293
)
9394

9495
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Monkey365 - the PowerShell Cloud Security Tool for Azure and Microsoft 365 (copyright 2022) by Juan Garrido
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
Function Get-MSServiceFromAudience{
16+
<#
17+
.SYNOPSIS
18+
19+
.DESCRIPTION
20+
21+
.INPUTS
22+
23+
.OUTPUTS
24+
25+
.EXAMPLE
26+
27+
.NOTES
28+
Author : Juan Garrido
29+
Twitter : @tr1ana
30+
File Name : Get-MSServiceFromAudience
31+
Version : 1.0
32+
33+
.LINK
34+
https://github.com/silverhack/monkey365
35+
#>
36+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Scope="Function")]
37+
[CmdletBinding()]
38+
Param(
39+
# Well Known Azure service
40+
[Parameter(Mandatory = $True, ValueFromPipeline = $True, HelpMessage = 'Audience')]
41+
[String]$InputObject
42+
)
43+
Begin{
44+
$WellKnownAudience = [Ordered]@{
45+
Teams = '48ac35b8-9aa8-4d74-927d-1f4a14a0b239';
46+
SharePoint = '00000003-0000-0ff1-ce00-000000000000';
47+
ResourceManager = "https://management.azure.com?.$|core.windows.net?.$|core.usgovcloudapi.net?.$|usgovcloudapi.net?.$";
48+
MSGraph = "https://graph.microsoft.com?.$|.us?.$|microsoftgraph.chinacloudapi.cn?.$";
49+
ExchangeOnline = "https://outlook.office365.com|.us";
50+
AzurePortal = '74658136-14ec-4630-ad9b-26e160ff0fc6';
51+
PowerBI = 'https://analysis.windows.net|analysis.chinacloudapi.cn|analysis.usgovcloudapi.net/powerbi/api';
52+
AADRM = "https://aadrm.com?.$|.us?.$";
53+
AzureStorage = 'https://storage.azure.com?.$|us?.$';
54+
AzureVault = "cfa8b339-82a2-471a-a3c9-0fc0be7a4093";
55+
Fabric = "https://api.fabric.microsoft.com?.$|us?.$"
56+
}
57+
}
58+
Process{
59+
$WellKnownAudience.GetEnumerator().Where({$InputObject.Trim() -match $_.Value}) | Select-Object -ExpandProperty Name -ErrorAction Ignore
60+
}
61+
}
62+

core/modules/monkeycloudutils/public/Get-MonkeyEnvironment.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Function Get-MonkeyEnvironment{
131131
AADPortal = "https://main.iam.ad.ext.azure.com/api/";
132132
AADRM = "https://aadrm.us";
133133
Forms = "https://forms.office.com";
134-
Storage = "https://storage.azure.com/";
134+
Storage = "https://storage.azure.us/";
135135
Vaults = "https://vault.azure.net";
136136
Servicemanagement = 'https://management.core.usgovcloudapi.net/';
137137
Security = 'https://s2.security.ext.azure.com/api/';

core/modules/monkeycloudutils/public/Get-MonkeySubscriptionInfo.ps1

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,40 +41,39 @@ Function Get-MonkeySubscriptionInfo{
4141
[Parameter(Mandatory = $false, HelpMessage = 'Endpoint')]
4242
[String]$Endpoint
4343
)
44-
try{
45-
[System.Uri]$audience = $Endpoint
46-
}
47-
catch{
48-
Write-Warning $_
49-
$audience = $null
50-
}
51-
if($null -ne $audience){
52-
if(Test-IsValidAudience -token $AuthObject.AccessToken -audience $audience.Authority){
53-
$Authorization = $AuthObject.CreateAuthorizationHeader()
54-
# Set HTTP request headers to include Authorization header
55-
$requestHeader = @{
56-
"x-ms-version" = "2014-10-01";
57-
"Authorization" = $Authorization
58-
}
59-
$Server = [System.Uri]::new($Endpoint)
60-
$uri = [System.Uri]::new($Server,"/subscriptions?api-version=2022-09-01")
61-
$final_uri = $uri.ToString()
62-
try{
63-
$p = @{
64-
Uri = $final_uri;
65-
Method = "Get";
66-
Headers = $requestHeader;
67-
ContentType = 'application/json'
68-
}
69-
$subs = Invoke-RestMethod @p
70-
if($subs.Value){
71-
return $subs.Value
72-
}
44+
Try{
45+
#Get Authorization Header
46+
$methods = $AuthObject | Get-Member | Where-Object {$_.MemberType -eq 'Method'} | Select-Object -ExpandProperty Name
47+
#Get Authorization Header
48+
If($null -ne $methods -and $methods.Contains('CreateAuthorizationHeader')){
49+
$AuthHeader = $AuthObject.CreateAuthorizationHeader()
50+
}
51+
Else{
52+
$AuthHeader = ("Bearer {0}" -f $AuthObject.AccessToken)
53+
}
54+
$requestHeader = @{
55+
"Authorization" = $AuthHeader
56+
}
57+
$Server = [System.Uri]::new($Endpoint)
58+
$uri = [System.Uri]::new($Server,"/subscriptions?api-version=2022-09-01")
59+
$final_uri = $uri.ToString()
60+
try{
61+
$p = @{
62+
Uri = $final_uri;
63+
Method = "Get";
64+
Headers = $requestHeader;
65+
ContentType = 'application/json'
7366
}
74-
catch{
75-
Write-Verbose $_
67+
$subs = Invoke-RestMethod @p
68+
If($subs.Value){
69+
return $subs.Value
7670
}
7771
}
72+
Catch{
73+
Write-Verbose $_
74+
}
7875
}
79-
}
80-
76+
Catch{
77+
Write-Error $_.Exception
78+
}
79+
}

core/modules/monkeyruleset/private/Build-Query.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Function Build-Query{
6565
[void]$finalquery.Append((" {0}" -f $q));
6666
}
6767
If($finalquery.Length -gt 0){
68-
$safeQuery = $finalquery | ConvertTo-SecureScriptBlock
68+
$safeQuery = $finalquery.ToString().Trim() | ConvertTo-SecureScriptBlock
6969
if($safeQuery){
7070
$InputObject | Add-Member -type NoteProperty -name query -value $safeQuery
7171
return $InputObject
@@ -101,7 +101,7 @@ Function Build-Query{
101101
[void]$finalquery.Append((" {0}" -f $q));
102102
}
103103
If($finalquery.Length -gt 0){
104-
$safeQuery = $finalquery.ToString() | ConvertTo-SecureScriptBlock
104+
$safeQuery = $finalquery.ToString().Trim() | ConvertTo-SecureScriptBlock
105105
If($safeQuery){
106106
$unitRule | Add-Member -type NoteProperty -name query -value $safeQuery
107107
}

core/modules/monkeyruleset/private/Get-NewFilter.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ Function Get-NewFilter{
102102
ElseIf($RightCastValue -is [string]){
103103
$rightCondition = Get-CastValue -InputObject $RightCastValue
104104
If($nullLeft){
105-
$tmp_filter = ('$null -{0} {1}' -f $Operator, $rightCondition)
105+
$tmp_filter = ('$null -{0} $_.{1}' -f $Operator, $RightCastValue)
106106
}
107107
ElseIf($pipeline){
108-
$tmp_filter = ('$_ -{0} {1}' -f $Operator, $rightCondition)
108+
$tmp_filter = ('$_ -{0} $_.{1}' -f $Operator, $RightCastValue)
109109
}
110110
ElseIf($LeftItem.Contains('@odata.type')){
111111
#First remove odata.type
@@ -157,10 +157,10 @@ Function Get-NewFilter{
157157
}
158158
Else{
159159
If($nullLeft){
160-
$tmp_filter = ('$null -{0} {1}' -f $Operator, $RightCastValue)
160+
$tmp_filter = ('$null -{0} $_.{1}' -f $Operator, $RightCastValue)
161161
}
162162
ElseIf($pipeline){
163-
$tmp_filter = ('$_ -{0} {1}' -f $Operator, $RightCastValue)
163+
$tmp_filter = ('$_ -{0} $_.{1}' -f $Operator, $RightCastValue)
164164
}
165165
ElseIf($LeftItem.Contains('@')){
166166
$tmp_filter = ('$_."{0}" -{1} {2}' -f $LeftItem, $Operator, $RightCastValue)

core/modules/monkeyruleset/private/Get-ObjectFromDataset.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Function Get-ObjectFromDataset{
108108
$dataObjects = $dataObjects | Format-DataFromExpression @p -RuleName $InputObject.displayName
109109
}
110110
}
111+
#Check if filter should be applied before execute query
111112
#return dataObjects
112113
return $dataObjects
113114
}

core/modules/monkeyutils/public/ConvertTo-SecureScriptBlock.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ Function ConvertTo-SecureScriptBlock{
5757
$allowedVariables = [string[]] @('*')
5858
#Remove Property references
5959
$sbTest = $InputObject.Replace('.','')
60-
foreach($allow in $allowed){
61-
if([regex]::isMatch($sbTest.ToLower(),("-{0}" -f $allow.ToLower()))){
62-
$sbTest = $sbTest -ireplace [regex]::Escape($allow), "eq"
60+
ForEach($allow in $allowed){
61+
If([regex]::isMatch($sbTest.ToLower(),("-{0}" -f $allow.ToLower()))){
62+
$sbTest = $sbTest -ireplace [regex]::Escape(("-{0}" -f $allow.ToLower())), "-eq"
6363
}
6464
}
6565
$double_quotes ='".*?"'

0 commit comments

Comments
 (0)