Skip to content

Commit 2e6bc2a

Browse files
committed
update monkey utils module
1 parent 74cdd1e commit 2e6bc2a

4 files changed

Lines changed: 83 additions & 5 deletions

File tree

core/modules/monkeyutils/monkeyutils.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ FunctionsToExport = @(
103103
'Test-IsValidUrl',
104104
'Update-MonkeyAsset',
105105
'Get-HashFromString',
106-
'Test-IsPsObject'
106+
'Test-IsPsObject',
107+
'Get-MonkeyLatestReleaseFromGitHub'
107108
)
108109

109110
# 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.

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ Function ConvertTo-SecureScriptBlock{
5555
$allowEnvVars= $false
5656
# Any variable will be allowed
5757
$allowedVariables = [string[]] @('*')
58-
#Remove Property references
59-
$sbTest = $InputObject.Replace('.','')
58+
#Remove Property references inside parentheses
59+
#$sbTest = $InputObject.Replace('.','')
60+
$sbTest = $InputObject -replace '(?<=\([^()]*)\.(?=[^()]*\))', ''
6061
ForEach($allow in $allowed){
6162
If([regex]::isMatch($sbTest.ToLower(),("-{0}" -f $allow.ToLower()))){
6263
$sbTest = $sbTest -ireplace [regex]::Escape(("-{0}" -f $allow.ToLower())), "-eq"
@@ -65,7 +66,9 @@ Function ConvertTo-SecureScriptBlock{
6566
$double_quotes ='".*?"'
6667
$single_quotes ='''.*?'''
6768
#Replace Where if any
68-
$sbTest = $sbTest.Replace('Where', ' -and ').Replace('{','').Replace('}','')
69+
$sbTest = $sbTest -replace '(?i)\.?Where', " -and "
70+
#Replace bracket
71+
$sbTest = $sbTest.Replace('{','').Replace('}','')
6972
#Replace @ and |
7073
$sbTest = $sbTest.Replace('@', '').Replace('|','')
7174
#Remove string with double quotes
@@ -82,6 +85,10 @@ Function ConvertTo-SecureScriptBlock{
8285
$sbTest = $sbTest -replace [regex]::Escape($match.Value), 0
8386
}
8487
}
88+
#Replace Count
89+
$sbTest = $sbTest -replace '(?i)\.?Count', ''
90+
#Replace final dots
91+
$sbTest = $sbTest -replace '\.', ''
8592
#Create an scriptblock that will not be executed
8693
$ScriptBlock = [scriptblock]::Create($sbTest)
8794
try{
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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-MonkeyLatestReleaseFromGitHub{
16+
<#
17+
.SYNOPSIS
18+
Get the latest published full release for the repository
19+
20+
.DESCRIPTION
21+
Get the latest published full release for the repository
22+
23+
.INPUTS
24+
25+
.OUTPUTS
26+
27+
.EXAMPLE
28+
29+
.NOTES
30+
Author : Juan Garrido
31+
Twitter : @tr1ana
32+
File Name : Get-MonkeyLatestReleaseFromGitHub
33+
Version : 1.0
34+
35+
.LINK
36+
https://github.com/silverhack/monkey365
37+
#>
38+
[cmdletbinding()]
39+
[OutputType([System.String])]
40+
Param (
41+
[parameter(Mandatory=$true, ValueFromPipeline = $True, HelpMessage="GitHub repository")]
42+
[String]$Url
43+
)
44+
Process{
45+
#Set null
46+
$repoUrl = $ghUser = $repository = $null
47+
$URI = [System.Uri]::new($Url);
48+
If($URI.Segments.Count -gt 2 -and $URI.Host.ToLower() -eq 'github.com'){
49+
$ghUser = $URI.Segments[1].ToLower().TrimEnd('/')
50+
$repository = $URI.Segments[2].ToLower().TrimEnd('/')
51+
If($null -ne $ghUser -and $null -ne $repository){
52+
$repoUrl = ('https://api.github.com/repos/{0}/{1}/releases/latest' -f $ghUser,$repository);
53+
}
54+
}
55+
If($null -ne $repoUrl){
56+
Try{
57+
$rawContent = Invoke-WebRequest -Uri $repoUrl -UseBasicParsing
58+
If($rawContent.StatusCode -eq [System.Net.HttpStatusCode]::OK){
59+
$content = $rawContent.Content | ConvertFrom-Json
60+
Write-Verbose ("Found {0}" -f $content.html_url)
61+
$content.tag_name.TrimStart('v')
62+
}
63+
}
64+
Catch{
65+
Write-Warning $_.Exception.Message
66+
}
67+
}
68+
}
69+
}

core/modules/monkeyutils/public/Update-PsObject.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,13 @@ Function Update-PsObject{
9393
}
9494
}
9595
# Output: If the value is an array, output it as a single object
96-
if ($null -ne $obj -and @($obj).Count -gt 1) {
96+
If ($obj -is [System.Collections.IEnumerable] -and $obj -isnot [string]){
9797
, $obj
9898
}
9999
else {
100100
$obj
101101
}
102+
102103
} -Force -ErrorAction SilentlyContinue
103104
}
104105
}

0 commit comments

Comments
 (0)