-
Notifications
You must be signed in to change notification settings - Fork 164
/
LoadMethodScanner.ps1
53 lines (43 loc) · 1.84 KB
/
LoadMethodScanner.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Author: Matthew Graeber (@mattifestation)
# Load dnlib with Add-Type first
# dnlib can be obtained here: https://github.com/0xd4d/dnlib
# Example: ls C:\ -Recurse | Get-AssemblyLoadReference
filter Get-AssemblyLoadReference {
param (
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
[Alias('FullName')]
[String]
[ValidateNotNullOrEmpty()]
$Path
)
$FullPath = Resolve-Path $Path
$Module = $null
try {
$Module = [dnlib.DotNet.ModuleDefMD]::Load($FullPath)
} catch {
return
}
$listMemberRefMD = $Module.GetType().GetFields('NonPublic, Instance') | ? { $_.Name -eq 'listMemberRefMD' }
$MemberRefList = $listMemberRefMD.GetValue($Module)
$GenericParamContext = New-Object -TypeName dnlib.DotNet.GenericParamContext
$AssemblyLoadList = New-Object -TypeName 'System.Collections.Generic.List[System.Object]'
for ($i = 0; $i -lt $MemberRefList.Length; $i++) {
$MemberRefDefinition = $MemberRefList.Item($i, $GenericParamContext)
if (($MemberRefDefinition.Name.String -eq 'Load') -and
($MemberRefDefinition.ReturnType.FullName -eq 'System.Reflection.Assembly') -and
($MemberRefDefinition.MethodSig.Params.FullName -contains 'System.Byte[]')) {
<# The assembly "imports" a Load method that:
1) Is called "Load"
2) Returns a System.Reflection.Assembly instance
3) Has at least one parameter that accepts an argument of type System.Byte[]
#>
$AssemblyLoadList.Add($MemberRefDefinition)
}
}
if ($AssemblyLoadList.Count) {
[PSCustomObject] @{
AssemblyPath = $FullPath
LoadMethodImports = $AssemblyLoadList
}
}
}