Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit 59e6f94

Browse files
committed
For ./ScriptModification/ :
-PSScriptAnalyzering -Tweaking of synopsis blocks in order to support platyPS -Code standardization -Generated docs
1 parent 9ed26d6 commit 59e6f94

12 files changed

+574
-53
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Compresses, Base-64 encodes, and outputs generated code to load a managed dll in
3636

3737
Encrypts text files/scripts.
3838

39-
#### `Remove-Comments`
39+
#### `Remove-Comment`
4040

4141
Strips comments and extra whitespace from a script.
4242

ScriptModification/Out-CompressedDll.ps1

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ function Out-CompressedDll
55
66
Compresses, Base-64 encodes, and outputs generated code to load a managed dll in memory.
77
8-
PowerSploit Function: Out-CompressedDll
9-
Author: Matthew Graeber (@mattifestation)
10-
License: BSD 3-Clause
11-
Required Dependencies: None
12-
Optional Dependencies: None
13-
8+
PowerSploit Function: Out-CompressedDll
9+
Author: Matthew Graeber (@mattifestation)
10+
License: BSD 3-Clause
11+
Required Dependencies: None
12+
Optional Dependencies: None
13+
1414
.DESCRIPTION
1515
1616
Out-CompressedDll outputs code that loads a compressed representation of a managed dll in memory as a byte array.
@@ -21,7 +21,7 @@ Specifies the path to a managed executable.
2121
2222
.EXAMPLE
2323
24-
C:\PS> Out-CompressedDll -FilePath evil.dll
24+
Out-CompressedDll -FilePath evil.dll
2525
2626
Description
2727
-----------
@@ -36,7 +36,9 @@ Only pure MSIL-based dlls can be loaded using this technique. Native or IJW ('it
3636
http://www.exploit-monday.com/2012/12/in-memory-dll-loading.html
3737
#>
3838

39-
[CmdletBinding()] Param (
39+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
40+
[CmdletBinding()]
41+
Param (
4042
[Parameter(Mandatory = $True)]
4143
[String]
4244
$FilePath
@@ -51,7 +53,7 @@ http://www.exploit-monday.com/2012/12/in-memory-dll-loading.html
5153

5254
$FileBytes = [System.IO.File]::ReadAllBytes($Path)
5355

54-
if (($FileBytes[0..1] | % {[Char]$_}) -join '' -cne 'MZ')
56+
if (($FileBytes[0..1] | ForEach-Object {[Char]$_}) -join '' -cne 'MZ')
5557
{
5658
Throw "$Path is not a valid executable."
5759
}

ScriptModification/Out-EncodedCommand.ps1

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ function Out-EncodedCommand
55
66
Compresses, Base-64 encodes, and generates command-line output for a PowerShell payload script.
77
8-
PowerSploit Function: Out-EncodedCommand
9-
Author: Matthew Graeber (@mattifestation)
10-
License: BSD 3-Clause
11-
Required Dependencies: None
12-
Optional Dependencies: None
13-
8+
PowerSploit Function: Out-EncodedCommand
9+
Author: Matthew Graeber (@mattifestation)
10+
License: BSD 3-Clause
11+
Required Dependencies: None
12+
Optional Dependencies: None
13+
1414
.DESCRIPTION
1515
1616
Out-EncodedCommand prepares a PowerShell script such that it can be pasted into a command prompt. The scenario for using this tool is the following: You compromise a machine, have a shell and want to execute a PowerShell script as a payload. This technique eliminates the need for an interactive PowerShell 'shell' and it bypasses any PowerShell execution policies.
@@ -49,13 +49,13 @@ Base-64 encodes the entirety of the output. This is usually unnecessary and effe
4949
5050
.EXAMPLE
5151
52-
C:\PS> Out-EncodedCommand -ScriptBlock {Write-Host 'hello, world!'}
52+
Out-EncodedCommand -ScriptBlock {Write-Host 'hello, world!'}
5353
5454
powershell -C sal a New-Object;iex(a IO.StreamReader((a IO.Compression.DeflateStream([IO.MemoryStream][Convert]::FromBase64String('Cy/KLEnV9cgvLlFQz0jNycnXUSjPL8pJUVQHAA=='),[IO.Compression.CompressionMode]::Decompress)),[Text.Encoding]::ASCII)).ReadToEnd()
5555
5656
.EXAMPLE
5757
58-
C:\PS> Out-EncodedCommand -Path C:\EvilPayload.ps1 -NonInteractive -NoProfile -WindowStyle Hidden -EncodedOutput
58+
Out-EncodedCommand -Path C:\EvilPayload.ps1 -NonInteractive -NoProfile -WindowStyle Hidden -EncodedOutput
5959
6060
powershell -NoP -NonI -W Hidden -E cwBhAGwAIABhACAATgBlAHcALQBPAGIAagBlAGMAdAA7AGkAZQB4ACgAYQAgAEkATwAuAFMAdAByAGUAYQBtAFIAZQBhAGQAZQByACgAKABhACAASQBPAC4AQwBvAG0AcAByAGUAcwBzAGkAbwBuAC4ARABlAGYAbABhAHQAZQBTAHQAcgBlAGEAbQAoAFsASQBPAC4ATQBlAG0AbwByAHkAUwB0AHIAZQBhAG0AXQBbAEMAbwBuAHYAZQByAHQAXQA6ADoARgByAG8AbQBCAGEAcwBlADYANABTAHQAcgBpAG4AZwAoACcATABjAGkAeABDAHMASQB3AEUAQQBEAFEAWAAzAEUASQBWAEkAYwBtAEwAaQA1AEsAawBGAEsARQA2AGwAQgBCAFIAWABDADgAaABLAE8ATgBwAEwAawBRAEwANAAzACsAdgBRAGgAdQBqAHkAZABBADkAMQBqAHEAcwAzAG0AaQA1AFUAWABkADAAdgBUAG4ATQBUAEMAbQBnAEgAeAA0AFIAMAA4AEoAawAyAHgAaQA5AE0ANABDAE8AdwBvADcAQQBmAEwAdQBYAHMANQA0ADEATwBLAFcATQB2ADYAaQBoADkAawBOAHcATABpAHMAUgB1AGEANABWAGEAcQBVAEkAagArAFUATwBSAHUAVQBsAGkAWgBWAGcATwAyADQAbgB6AFYAMQB3ACsAWgA2AGUAbAB5ADYAWgBsADIAdAB2AGcAPQA9ACcAKQAsAFsASQBPAC4AQwBvAG0AcAByAGUAcwBzAGkAbwBuAC4AQwBvAG0AcAByAGUAcwBzAGkAbwBuAE0AbwBkAGUAXQA6ADoARABlAGMAbwBtAHAAcgBlAHMAcwApACkALABbAFQAZQB4AHQALgBFAG4AYwBvAGQAaQBuAGcAXQA6ADoAQQBTAEMASQBJACkAKQAuAFIAZQBhAGQAVABvAEUAbgBkACgAKQA=
6161
@@ -72,7 +72,8 @@ This cmdlet was inspired by the createcmd.ps1 script introduced during Dave Kenn
7272
http://www.exploit-monday.com
7373
#>
7474

75-
[CmdletBinding( DefaultParameterSetName = 'FilePath')] Param (
75+
[CmdletBinding( DefaultParameterSetName = 'FilePath')]
76+
Param (
7677
[Parameter(Position = 0, ValueFromPipeline = $True, ParameterSetName = 'ScriptBlock' )]
7778
[ValidateNotNullOrEmpty()]
7879
[ScriptBlock]

ScriptModification/Out-EncryptedScript.ps1

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ function Out-EncryptedScript
55
66
Encrypts text files/scripts.
77
8-
PowerSploit Function: Out-EncryptedScript
9-
Author: Matthew Graeber (@mattifestation)
10-
License: BSD 3-Clause
11-
Required Dependencies: None
12-
Optional Dependencies: None
8+
PowerSploit Function: Out-EncryptedScript
9+
Author: Matthew Graeber (@mattifestation)
10+
License: BSD 3-Clause
11+
Required Dependencies: None
12+
Optional Dependencies: None
1313
1414
.DESCRIPTION
1515
@@ -36,7 +36,8 @@ is randomly generated by default.
3636
3737
.EXAMPLE
3838
39-
C:\PS> Out-EncryptedScript .\Naughty-Script.ps1 password salty
39+
$Password = ConvertTo-SecureString 'Password123!' -AsPlainText -Force
40+
Out-EncryptedScript .\Naughty-Script.ps1 $Password salty
4041
4142
Description
4243
-----------
@@ -48,10 +49,10 @@ function 'de' and the base64-encoded ciphertext.
4849
4950
.EXAMPLE
5051
51-
C:\PS> [String] $cmd = Get-Content .\evil.ps1
52-
C:\PS> Invoke-Expression $cmd
53-
C:\PS> $decrypted = de password salt
54-
C:\PS> Invoke-Expression $decrypted
52+
[String] $cmd = Get-Content .\evil.ps1
53+
Invoke-Expression $cmd
54+
$decrypted = de password salt
55+
Invoke-Expression $decrypted
5556
5657
Description
5758
-----------
@@ -64,34 +65,39 @@ unencrypted script is called via Invoke-Expression
6465
This command can be used to encrypt any text-based file/script
6566
#>
6667

67-
[CmdletBinding()] Param (
68+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
69+
[CmdletBinding()]
70+
Param (
6871
[Parameter(Position = 0, Mandatory = $True)]
6972
[String]
7073
$ScriptPath,
71-
74+
7275
[Parameter(Position = 1, Mandatory = $True)]
73-
[String]
76+
[Security.SecureString]
7477
$Password,
75-
78+
7679
[Parameter(Position = 2, Mandatory = $True)]
7780
[String]
7881
$Salt,
79-
82+
8083
[Parameter(Position = 3)]
8184
[ValidateLength(16, 16)]
8285
[String]
83-
$InitializationVector = ((1..16 | % {[Char](Get-Random -Min 0x41 -Max 0x5B)}) -join ''),
84-
86+
$InitializationVector = ((1..16 | ForEach-Object {[Char](Get-Random -Min 0x41 -Max 0x5B)}) -join ''),
87+
8588
[Parameter(Position = 4)]
8689
[String]
8790
$FilePath = '.\evil.ps1'
8891
)
8992

93+
$TempCred = New-Object System.Management.Automation.PSCredential('a', $Password)
94+
$PlaintextPassword = $TempCred.GetNetworkCredential().Password
95+
9096
$AsciiEncoder = New-Object System.Text.ASCIIEncoding
9197
$ivBytes = $AsciiEncoder.GetBytes($InitializationVector)
9298
# While this can be used to encrypt any file, it's primarily designed to encrypt itself.
9399
[Byte[]] $scriptBytes = Get-Content -Encoding Byte -ReadCount 0 -Path $ScriptPath
94-
$DerivedPass = New-Object System.Security.Cryptography.PasswordDeriveBytes($Password, $AsciiEncoder.GetBytes($Salt), "SHA1", 2)
100+
$DerivedPass = New-Object System.Security.Cryptography.PasswordDeriveBytes($PlaintextPassword, $AsciiEncoder.GetBytes($Salt), "SHA1", 2)
95101
$Key = New-Object System.Security.Cryptography.TripleDESCryptoServiceProvider
96102
$Key.Mode = [System.Security.Cryptography.CipherMode]::CBC
97103
[Byte[]] $KeyBytes = $DerivedPass.GetBytes(16)

ScriptModification/Remove-Comments.ps1 renamed to ScriptModification/Remove-Comment.ps1

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
function Remove-Comments
1+
function Remove-Comment
22
{
33
<#
44
.SYNOPSIS
55
66
Strips comments and extra whitespace from a script.
77
8-
PowerSploit Function: Remove-Comments
9-
Author: Matthew Graeber (@mattifestation)
10-
License: BSD 3-Clause
11-
Required Dependencies: None
12-
Optional Dependencies: None
13-
8+
PowerSploit Function: Remove-Comment
9+
Author: Matthew Graeber (@mattifestation)
10+
License: BSD 3-Clause
11+
Required Dependencies: None
12+
Optional Dependencies: None
13+
1414
.DESCRIPTION
1515
16-
Remove-Comments strips out comments and unnecessary whitespace from a script. This is best used in conjunction with Out-EncodedCommand when the size of the script to be encoded might be too big.
16+
Remove-Comment strips out comments and unnecessary whitespace from a script. This is best used in conjunction with Out-EncodedCommand when the size of the script to be encoded might be too big.
1717
1818
A major portion of this code was taken from the Lee Holmes' Show-ColorizedContent script. You rock, Lee!
1919
@@ -27,11 +27,11 @@ Specifies the path to your script.
2727
2828
.EXAMPLE
2929
30-
C:\PS> $Stripped = Remove-Comments -Path .\ScriptWithComments.ps1
30+
$Stripped = Remove-Comment -Path .\ScriptWithComments.ps1
3131
3232
.EXAMPLE
3333
34-
C:\PS> Remove-Comments -ScriptBlock {
34+
Remove-Comment -ScriptBlock {
3535
### This is my awesome script. My documentation is beyond reproach!
3636
Write-Host 'Hello, World!' ### Write 'Hello, World' to the host
3737
### End script awesomeness
@@ -41,7 +41,7 @@ Write-Host 'Hello, World!'
4141
4242
.EXAMPLE
4343
44-
C:\PS> Remove-Comments -Path Inject-Shellcode.ps1 | Out-EncodedCommand
44+
Remove-Comment -Path Inject-Shellcode.ps1 | Out-EncodedCommand
4545
4646
Description
4747
-----------
@@ -57,15 +57,17 @@ Accepts either a string containing the path to a script or a scriptblock.
5757
5858
System.Management.Automation.ScriptBlock
5959
60-
Remove-Comments returns a scriptblock. Call the ToString method to convert a scriptblock to a string, if desired.
60+
Remove-Comment returns a scriptblock. Call the ToString method to convert a scriptblock to a string, if desired.
6161
6262
.LINK
6363
6464
http://www.exploit-monday.com
6565
http://www.leeholmes.com/blog/2007/11/07/syntax-highlighting-in-powershell/
6666
#>
67-
68-
[CmdletBinding( DefaultParameterSetName = 'FilePath' )] Param (
67+
68+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
69+
[CmdletBinding( DefaultParameterSetName = 'FilePath' )]
70+
Param (
6971
[Parameter(Position = 0, Mandatory = $True, ParameterSetName = 'FilePath' )]
7072
[ValidateNotNullOrEmpty()]
7173
[String]

ScriptModification/ScriptModification.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ FunctionsToExport = '*'
2626

2727
# List of all files packaged with this module
2828
FileList = 'ScriptModification.psm1', 'ScriptModification.psd1', 'Out-CompressedDll.ps1', 'Out-EncodedCommand.ps1',
29-
'Out-EncryptedScript.ps1', 'Remove-Comments.ps1', 'Usage.md'
29+
'Out-EncryptedScript.ps1', 'Remove-Comment.ps1', 'Usage.md'
3030

3131
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Out-CompressedDll
2+
3+
## SYNOPSIS
4+
Compresses, Base-64 encodes, and outputs generated code to load a managed dll in memory.
5+
6+
PowerSploit Function: Out-CompressedDll
7+
Author: Matthew Graeber (@mattifestation)
8+
License: BSD 3-Clause
9+
Required Dependencies: None
10+
Optional Dependencies: None
11+
12+
## SYNTAX
13+
14+
```
15+
Out-CompressedDll [-FilePath] <String>
16+
```
17+
18+
## DESCRIPTION
19+
Out-CompressedDll outputs code that loads a compressed representation of a managed dll in memory as a byte array.
20+
21+
## EXAMPLES
22+
23+
### -------------------------- EXAMPLE 1 --------------------------
24+
```
25+
Out-CompressedDll -FilePath evil.dll
26+
```
27+
28+
Description
29+
-----------
30+
Compresses, base64 encodes, and outputs the code required to load evil.dll in memory.
31+
32+
## PARAMETERS
33+
34+
### -FilePath
35+
Specifies the path to a managed executable.
36+
37+
```yaml
38+
Type: String
39+
Parameter Sets: (All)
40+
Aliases:
41+
42+
Required: True
43+
Position: 1
44+
Default value: None
45+
Accept pipeline input: False
46+
Accept wildcard characters: False
47+
```
48+
49+
## INPUTS
50+
51+
## OUTPUTS
52+
53+
## NOTES
54+
Only pure MSIL-based dlls can be loaded using this technique.
55+
Native or IJW ('it just works' - mixed-mode) dlls will not load.
56+
57+
## RELATED LINKS
58+
59+
[http://www.exploit-monday.com/2012/12/in-memory-dll-loading.html](http://www.exploit-monday.com/2012/12/in-memory-dll-loading.html)
60+

0 commit comments

Comments
 (0)