将 System.Int32 转换为 System.UIntPtr



我正在使用PowerShell中的P/Invoke功能来调用Win32VirtualProtectEx函数。

对于第三个参数,它需要一个无符号的int指针作为参数,根据函数签名

static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);.

我所拥有的是一个名为$shellcode的变量,并希望将其写入某个内存地址,所以我需要保留$shellcode.Length个字节。不过,我不知道该投向谁。我已经尝试过

[System.UIntPtr]$shellcode.Length

# Set memory for shellcode to Execute and Read
$oldProtect = 0
$procHandle.GetType().fullname
$basePtr.GetType().fullname
$shellcode.Length.GetType().fullname
$PAGE_EXECUTE_READWRITE.GetType().fullname
$oldProtect.GetType().fullname
$result = $Kernel32::VirtualProtectEx($procHandle, $basePtr, $shellcode.Length, $PAGE_EXECUTE_READ, [ref] $oldProtect);

但它返回

System.IntPtr
System.IntPtr
System.Int32
System.Int32
System.Int32
Cannot convert argument "dwSize", with value: "168", for "VirtualProtectEx" to type
"System.UIntPtr": "Cannot convert the "168" value of type "System.Int32" to type
"System.UIntPtr"."
At C:UsersAdminDesktopmalware.ps1:59 char:1
+ $result = $Kernel32::VirtualProtectEx($procHandle, $basePtr, $shellco ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

使用构造函数:[UIntPtr]::new($shellcode.Length).

相关内容

最新更新