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

Commit 92fcfdc

Browse files
author
mattifestation
committed
Add Get-Entropy
1 parent c5168cd commit 92fcfdc

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ Displays the process modules that have been loaded since the call to Register-Pr
140140

141141
Stops the running process module trace
142142

143+
#### `Get-Entropy`
144+
145+
Calculates the entropy of a file or byte array.
146+
143147
## AntivirusBypass
144148

145149
**AV doesn't stand a chance against PowerShell!**

ReverseEngineering/Get-Entropy.ps1

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
function Get-Entropy
2+
{
3+
<#
4+
.SYNOPSIS
5+
6+
Calculates the entropy of a file or byte array.
7+
8+
PowerSploit Function: Get-Entropy
9+
Author: Matthew Graeber (@mattifestation)
10+
License: BSD 3-Clause
11+
Required Dependencies: None
12+
Optional Dependencies: None
13+
14+
.PARAMETER ByteArray
15+
16+
Specifies the byte array containing the data from which entropy will be calculated.
17+
18+
.PARAMETER FilePath
19+
20+
Specifies the path to the input file from which entropy will be calculated.
21+
22+
.EXAMPLE
23+
24+
C:\PS>Get-Entropy -FilePath C:\Windows\System32\kernel32.dll
25+
26+
.EXAMPLE
27+
28+
C:\PS>ls C:\Windows\System32\*.dll | % { Get-Entropy -FilePath $_ }
29+
30+
.EXAMPLE
31+
32+
C:\PS>$RandArray = New-Object Byte[](10000)
33+
C:\PS>foreach ($Offset in 0..9999) { $RandArray[$Offset] = [Byte] (Get-Random -Min 0 -Max 256) }
34+
C:\PS>$RandArray | Get-Entropy
35+
36+
Description
37+
-----------
38+
Calculates the entropy of a large array containing random bytes.
39+
40+
.EXAMPLE
41+
42+
C:\PS> 0..255 | Get-Entropy
43+
44+
Description
45+
-----------
46+
Calculates the entropy of 0-255. This should equal exactly 8.
47+
48+
.OUTPUTS
49+
50+
System.Double
51+
52+
Get-Entropy outputs a double representing the entropy of the byte array.
53+
54+
.LINK
55+
56+
http://www.exploit-monday.com
57+
#>
58+
59+
[CmdletBinding()] Param (
60+
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ParameterSetName = 'Bytes')]
61+
[ValidateNotNullOrEmpty()]
62+
[Byte[]]
63+
$ByteArray,
64+
65+
[Parameter(Mandatory = $True, Position = 0, ParameterSetName = 'File')]
66+
[ValidateNotNullOrEmpty()]
67+
[IO.FileInfo]
68+
$FilePath
69+
)
70+
71+
BEGIN
72+
{
73+
$FrequencyTable = @{}
74+
$ByteArrayLength = 0
75+
}
76+
77+
PROCESS
78+
{
79+
if ($PsCmdlet.ParameterSetName -eq 'File')
80+
{
81+
$ByteArray = [IO.File]::ReadAllBytes($FilePath.FullName)
82+
}
83+
84+
foreach ($Byte in $ByteArray)
85+
{
86+
$FrequencyTable[$Byte]++
87+
$ByteArrayLength++
88+
}
89+
}
90+
91+
END
92+
{
93+
$Entropy = 0.0
94+
95+
foreach ($Byte in 0..255)
96+
{
97+
$ByteProbability = ([Double] $FrequencyTable[[Byte]$Byte]) / $ByteArrayLength
98+
if ($ByteProbability -gt 0)
99+
{
100+
$Entropy += -$ByteProbability * [Math]::Log($ByteProbability, 2)
101+
}
102+
}
103+
104+
Write-Output $Entropy
105+
}
106+
}

ReverseEngineering/ReverseEngineering.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ ModuleList = @(@{ModuleName = 'ReverseEngineering'; ModuleVersion = '1.0.0.0'; G
7676
FileList = 'ReverseEngineering.psm1', 'ReverseEngineering.psd1', 'Get-ILDisassembly.ps1', 'Get-NtSystemInformation.format.ps1xml',
7777
'Get-NtSystemInformation.ps1', 'Get-Member.ps1', 'Get-MethodAddress.ps1', 'Get-PEB.format.ps1xml',
7878
'Get-PEB.ps1', 'Get-Strings.ps1', 'Get-StructFromMemory.ps1', 'ConvertTo-String.ps1',
79-
'New-Object.ps1', 'Get-ILDisassembly.format.ps1xml', 'ProcessModuleTrace.ps1', 'Usage.md'
79+
'Get-Entropy.ps1', 'New-Object.ps1', 'Get-ILDisassembly.format.ps1xml', 'ProcessModuleTrace.ps1', 'Usage.md'
8080

8181
# Private data to pass to the module specified in RootModule/ModuleToProcess
8282
# PrivateData = ''

0 commit comments

Comments
 (0)