powershell-command.test.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { execFileSync } from "node:child_process";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. createEncodedPowerShellArgs,
  5. createWindowsPowerShellSecurityArgs,
  6. } from "../scripts/powershell-command.mjs";
  7. function decodeCommand(args: string[]): string {
  8. expect(args.slice(0, 4)).toEqual(["-NoLogo", "-NoProfile", "-NonInteractive", "-EncodedCommand"]);
  9. expect(args).toHaveLength(5);
  10. return Buffer.from(args[4]!, "base64").toString("utf16le");
  11. }
  12. describe("powershell-command", () => {
  13. it("把动态值编码进脚本且不再依赖普通 -Command 的尾随参数", () => {
  14. const values = ["C:\\Program Files\\Z'Code;测试\\helper.exe", "value with spaces & symbols"];
  15. const args = createEncodedPowerShellArgs(
  16. "[Console]::Out.Write($zcodeArg0 + '|' + $zcodeArg1)",
  17. values,
  18. );
  19. const command = decodeCommand(args);
  20. expect(command).not.toContain("$args");
  21. for (const [index, value] of values.entries()) {
  22. expect(command).not.toContain(value);
  23. expect(command).toContain(`$zcodeArg${index}=`);
  24. expect(command).toContain(Buffer.from(value, "utf8").toString("base64"));
  25. }
  26. });
  27. it("从 PSHOME 显式加载 Windows PowerShell Security 模块", () => {
  28. const command = decodeCommand(
  29. createWindowsPowerShellSecurityArgs("[Console]::Out.Write('loaded')"),
  30. );
  31. expect(command).toContain(
  32. "[IO.Path]::Combine($PSHOME,'Modules','Microsoft.PowerShell.Security'",
  33. );
  34. expect(command).toContain("Import-Module -Name $zcodeSecurityModule -Force -ErrorAction Stop");
  35. expect(command).toContain("Windows PowerShell Security module load failed");
  36. expect(command).toContain("exit 26");
  37. expect(command).not.toContain("$env:PSModulePath");
  38. });
  39. it.runIf(process.platform === "win32")(
  40. "Windows PowerShell 能还原带空格和脚本元字符的动态值",
  41. () => {
  42. const values = ["C:\\Program Files\\Z'Code;测试\\helper.exe", "value with spaces & symbols"];
  43. const output = execFileSync(
  44. "powershell.exe",
  45. createEncodedPowerShellArgs(
  46. "[Console]::Out.Write($zcodeArg0 + [Environment]::NewLine + $zcodeArg1)",
  47. values,
  48. ),
  49. { encoding: "utf8" },
  50. );
  51. expect(output.split(/\r?\n/)).toEqual(values);
  52. },
  53. );
  54. it.runIf(process.platform === "win32")(
  55. "Windows PowerShell 在 PSModulePath 被污染时仍加载系统 Security 模块",
  56. () => {
  57. const output = execFileSync(
  58. "powershell.exe",
  59. createWindowsPowerShellSecurityArgs(
  60. [
  61. "$signature=Microsoft.PowerShell.Security\\Get-AuthenticodeSignature -LiteralPath $zcodeArg0;",
  62. "[Console]::Out.Write($null -ne $signature)",
  63. ].join(""),
  64. [process.execPath],
  65. ),
  66. {
  67. encoding: "utf8",
  68. env: { ...process.env, PSModulePath: "Z:\\zcode-invalid-psmodulepath" },
  69. },
  70. );
  71. expect(output).toBe("True");
  72. },
  73. );
  74. });