您是否可以使用 PtrToStringAuto 在 macOS 上的 Powershell 7 中解密安全字符串?

2024-01-04

我没有成功地让以下代码片段输出“Hello World!”在PS7中

$string = $("Hello World!" | ConvertTo-SecureString -AsPlainText -Force)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto(
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($string))

上面的代码是解密不指定长度的安全字符串的示例。

相同的代码可以在 PS6 和 PS5 中工作以完全解密安全字符串,但在 PS7 中不起作用。我发现解决这个问题的唯一方法是使用 PtrToStringBSTR。然后,对于该用例,它可以在所有 PS 版本中按预期工作。

我在 Github 上的 Powershell 存储库中提出了问题,但尚未得到任何回复。老实说,我只是在寻找一些证据来证明这种行为对于其他人来说是一样的。

https://github.com/PowerShell/PowerShell/issues/11953 https://github.com/PowerShell/PowerShell/issues/11953

我认为这样的事情对于移植到 PS7 的大量代码来说将是一个重大改变。

这是我到目前为止发现的:

文档

https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.ptrtostringauto?view=netframework-4.8 https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.ptrtostringauto?view=netframework-4.8

根据文档,当指定整数时,PtrToStringAuto:

Allocates a managed String and copies the specified number of characters from a string stored in unmanaged memory into it.

指定 int 11 返回“Hello”,这是因为返回的所有其他字符均为 Null。在这种情况下,您必须指定 int 23 才能返回完整的字符串“Hello World!”使用这种方法。我已将输出存储在变量中来演示这一点。

$String = $("Hello World!" | ConvertTo-SecureString -AsPlainText -Force)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto(
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($string), 23)

$String[0] Returns H
$String[1] Returns NULL
$String[2] Returns E
$String[3] Returns NULL
etc.... 

如果未指定整数,则 PtrToStringAuto:

Allocates a managed String and copies all characters up to the first null character from a string stored in unmanaged memory into it.

我相信这表明要么安全字符串是用 NULL 值存储的,而在 PS6 中不是,要么 PtrToStringAuto 函数的行为已经改变,现在遵循上面文档描述的行为。

这只是 macOS 上的问题;但是,使用 PtrToStringBSTR 代替 PtrToStringAuto 来解密安全字符串可以在 Windows 和 macOS 上按预期工作。

这似乎相关:https://stackoverflow.com/a/11022662/4257163 https://stackoverflow.com/a/11022662/4257163

我也没有看到任何地方发生了变化。


注意[securestring] https://learn.microsoft.com/en-US/dotnet/api/System.Security.SecureString is 不推荐用于新代码 https://github.com/dotnet/platform-compat/blob/master/docs/DE0001.md anymore.

While on Windows secure strings offer limited protection - by storing the string encrypted in memory - via the DPAPI https://msdn.microsoft.com/en-us/library/ms995355.aspx - and by shortening the window during which the plain-text representation is held in memory, no encryption at all is used on Unix-like platforms.[1]


我发现解决这个问题的唯一方法是使用PtrToStringBSTR.

That is not only a way around the problem, PtrToStringBSTR https://learn.microsoft.com/en-US/dotnet/api/System.Runtime.InteropServices.Marshal.PtrToStringBSTR is the method that should have been used to begin with, given that the input string is a BSTR.[2]

请注意将安全字符串与常规字符串相互转换[string]实例违背了使用的真正目的[securestring]首先:您最终将在进程内存中得到敏感数据的纯文本表示,其生命周期您无法控制。

如果你真的想这样做,一个更简单、跨平台兼容的方法是:

[System.Net.NetworkCredential]::new('dummy', $string).Password

[1] This is especially problematic when you save a secure string in a file, via ConvertFrom-SecureString or Export-CliXml - see this answer https://stackoverflow.com/a/55797281/45375.

[2] The Auto in PtrToStringAuto() means that the unmanaged input string is assumed to use a platform-appropriate character encoding, whereas BSTR is a "Unicode" (UTF-16) string on all platforms. On Windows, an unmanaged string is assumed to have UTF-16 encoding (which is why the code works), whereas on Unix-like platforms it is UTF-8 since .NET Core 3.0 (PowerShell [Core] 7.0 is based on .NET Core 3.1), which explains your symptoms: the NUL chars. in the BSTR instance's UTF-16 code units are interpreted as characters in their own right when (mis)interpreted as UTF-8. Note that .NET Core 2.x (which is what PowerShell [Core] 6.x is based on) (inappropriately) defaulted to UTF-16, which this PR fixed https://github.com/dotnet/coreclr/pull/23664, amounting to a breaking change.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

您是否可以使用 PtrToStringAuto 在 macOS 上的 Powershell 7 中解密安全字符串? 的相关文章

随机推荐

  • 头部与身体之间的间距

    我有一个简单的 html 表 如下所示 table thead tr th Column 1 th th Column 2 th tr thead tbody tr class odd first row td Value 1 td td
  • 如何在不使用 super() 的情况下访问父类的重写方法?

    如下所示 我尝试将子类的对象强制转换为其父类的对象 进展顺利 但是 当我尝试访问父类的重写方法时 它不会发生 相反 调用子类中的重写方法 我知道我可以使用 super 关键字来做到这一点 但我只是想知道为什么这不能通过强制转换来完成 这是父
  • IE10 中的 Angular UI Bootstrap 进度条

    我正在使用 Angular UI Bootstrap 来显示进度条 在 IE 中我的网站出现问题后 我用 IE 查看了Angular UI Bootstrap 网站 http angular ui github io bootstrap 并
  • 如何处理对话框中的后退按钮?

    我正在开发一个应用程序 当按下按钮时 它会打开一个带有 确定 和 取消 按钮的对话框 效果很好 当用户按下后退按钮时 我按如下方式处理 public boolean onKeyDown int keyCode KeyEvent event
  • 使用 AVPlayer 返回“非多路径连接”错误

    我正在使用 AVKit 播放 YouTube URL 我在按钮操作中有这段代码 IBAction func trailerButtonAction sender Any guard let youtubeUrl youtubeURL els
  • 具有访问权限的 Excel VBA 不会在此代码上关闭

    你好 我几分钟前刚刚发帖 有人回答了我关于 Excel 未关闭的问题 我正在使用访问权限打开工作表并添加表格 Excel 不会关闭 这会导致问题 因为当我在另一个函数中再次获取 Excel 对象时 我正在使用的工作表将无法打开 也不会对其进
  • Javascript 和 CSS 之间保持 DRY

    假设您有一个可以通过按钮切换打开和关闭的菜单 我的标准方法是为关闭的菜单编写 CSS 并编写指定 或动画 打开菜单状态的 Javascript 最近我开始接触 Active js 一个客户端 MVC 框架 它为视图类提供了用于制作 DOM
  • 将 JSON 解组为映射/字符串列表

    我想将 Json 解组到映射 字符串列表 例如 Map gt 这是我的输入 pointsOfSale pointOfSale href pointsOfSale UUID 0abc2aca 7930 4c9e 9f38 8af3d0692e
  • Spark Window函数最后一个非空值

    我们有一个用户事件的时间序列数据库 如下所示 timestamp user id event ticke type error type 2019 06 06 14 33 31 user a choose ticket ticke b NU
  • 在 MySQL 中存储 0.00001

    我有一个赚取网站 我希望用户每次点击赚取 0 00001 我知道它低于 1p 我可以使用什么类型的色谱柱 我努力了int and float但两者都不起作用 Use DECIMAL http dev mysql com doc refman
  • 将勾号 (✔) 添加到 string.xml

    我在字符串消息上添加勾号 strings xml 但是当我在移动设备上显示它时 我得到一个 框 而不是刻度线 我已直接将符号粘贴到我的字符串消息上 我们有什么办法可以处理吗 我们需要使用 unicode 值吗 添加unicode符号 u27
  • 如何使用 Perl 进行批量搜索和替换?

    我有以下脚本 它接受输入文件 输出文件和 将输入文件中的字符串替换为其他字符串并写出 输出文件 我想更改脚本以遍历文件目录 即 脚本不应提示输入和输出文件 而应采用 作为参数的目录路径 例如 C temp allFilesTobeRepla
  • Angular 8 通用服务器端渲染

    我正在关注这个教程https blog angular university io angular universal https blog angular university io angular universal 但我无法执行第一个
  • 微服务:工作者角色、API 或两者兼而有之?

    我见过微服务的混合示例 它们实现为工作角色处理队列中的请求和 或 API REST 支持异步场景 可以利用队列 通过简单的哑队列侦听器将请求转发到微服务 REST API 而同步场景将直接调用 REST API 我认为微服务这个术语的定义很
  • Vuejs 子组件中的 Prop 值无法绑定到元素属性

    我正在使用 Vuetify 在 Vuejs 中开发一个管理应用程序 并且表单中有三个字段供用户选择十六进制颜色值 为了让用户更容易 我实现了一个基于的颜色选择器这个代码笔 https codepen io Brownsugar pen Na
  • JQuery 通过 IFrame 进行可拖动和可调整大小(解决方案)

    我最近在使用 JQuery Draggable 和 Resizing 插件时遇到了一些麻烦 在寻找解决方案时 我在许多不同的地方发现了一些非常零碎的代码 最后归档到一个干净的解决方案 该解决方案似乎对我来说几乎完美 我想我会与其他人分享我的
  • 将特定位置的位收集为新值

    我有一个大小为 N 个字符的位掩码 它是静态已知的 即可以在编译时计算 但它不是单个常量 所以我不能只是写下来 位设置为 1 表示 想要 的位 我有一个相同大小的值 该值只有在运行时才知道 我想按顺序从该值收集 想要的 位到新值的开头 为了
  • 在 Sublime Text 2 中重新格式化段落的常用方法是什么?

    当您有一个包含长行的文本文件时 如何将它们重新格式化为一定的宽度 同时 不把它们混在一起吗 一般来说 我在 Vim 中寻找的是这个 每行都要完成 V gq j with textwidth提前设定 ALT CMD q在 OS X 上 将硬换
  • 是否可以使用Javamail发送邮件而无需身份验证?

    我一直在复制这段代码http www tutorialspoint com java java sending email htm http www tutorialspoint com java java sending email ht
  • 您是否可以使用 PtrToStringAuto 在 macOS 上的 Powershell 7 中解密安全字符串?

    我没有成功地让以下代码片段输出 Hello World 在PS7中 string Hello World ConvertTo SecureString AsPlainText Force System Runtime InteropServ