powershell - 是否可以混淆单个 Powershell 行?
问题描述
在 javascript 中,您可以使用类似 eval(function{p,a,c,k,e,d ... 的东西来混淆代码。是否可以使用一行Powershell 来做到这一点。
基本上,我加载了一个 Powershell 文件,它会检查字符串的顺序。如果是“A|B|C”,则将代码更改为“B|C|A”。当浏览器启动时,如果它在那个 JS 文件中看到“B|C|A”,它就会运行。当浏览器退出时,Powershell 再次将其更改回“A|B|C”(如果用户现在尝试直接打开 HTML 页面,Javascript 会在文件中看到“A|B|C” - 不正确的代码 - 并退出应用程序)
显然,如果用户看到正在发生的事情,他们现在将如何破解锁。所以我想“隐藏”文件的来源或处理操作。然后是代码:
$obsData = Get-Content -Encoding UTF8 "$ScriptDir\ob_data.js"
if ($obsData -like 'maxlength|run|outfile'){
$obsData[1] -replace 'norun','-r-' -replace 'run','norun' -replace '-r-','run'|Out-File -Encoding UTF8 "$myPath\ob_data.js"
Start-Process $browserChoice "file:$ScriptDir/standard.html" -WindowStyle Maximized
}
退出时会发生相反的情况,改变“run”和“norun”的位置(顺便说一下,显示的“maxlength|run|outfile”字符串位于 eval 混淆的 JS 代码的末尾)
我不是在寻找使用 SecureString 等的“重型”恶意软件风格混淆,只是让“普通用户”远离气味......比如
"ob_data.js"
-Encoding UTF8 "$ScriptDir\char[6f] char[62] char[5f] chat[64]...
$file= (base64 to ASCII)="b2JfZGF0YS5qcw=="
-Encoding UTF8 "$ScriptDir\$file
如果它可以用更神秘的代码“隐藏”替换行 - 再次让普通人无法猜测发生了什么,那就更好了。
在实践中,脚本改变了“run”和norun”的位置所以默认是(run="0" norun="1") 当被Powershell交换时(norun="0" run="1") 不影响混淆代码。
解决方案
与 JavaScript 不同,您不能从浏览器运行 PowerShell 命令/代码。
您不能直接从任何网页运行它。您需要某种 Web 应用程序包装器来启动它。...
C# 包装代码:
private List<PSObject> RunPowershellScript(string relativePath, List<CommandParameter> parameters)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
//Here's how you add a new script with arguments
Command cmd = new Command(Server.MapPath(relativePath));
foreach (var item in parameters)
{
cmd.Parameters.Add(item);
}
pipeline.Commands.Add(cmd);
// Execute PowerShell script
var res = pipeline.Invoke();
var results =res.ToList();
return results;
}
}
或利用这种方法:
您可以使用 PowerShell 对任何字符串或命令进行基础编码。
powershell.exe /?
# The help for PowerShell.exe also shows you how to encode a command with Base64:
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("'mikefrobbins.com'"))
# While it could be decoded within PowerShell:
[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('JwBtAGkAawBlAGYAcgBvAGIAYgBpAG4AcwAuAGMAbwBtACcA'))
<#
Adding quotes around the domain name also allows it to be decoded with
PowerShell.exe using the -EncodedCommand parameter without having to encode it
with a command such as Write-Output:
#>
powershell.exe -encodedCommand JwBtAGkAawBlAGYAcgBvAGIAYgBpAG4AcwAuAGMAbwBtACcA
但是,适当的风险管理网络会阻止这些编码的东西,因为这是黑客在预先受到攻击的主机上操作的方式,以使用 PowerShell 作为 Post Exploit 工作。
推荐阅读
- regex - 两个字符之间的正则表达式匹配字符串
- jquery - 在可拖动的 jqGrid 中选择列的内容
- python-3.x - Python使用键和值列表创建字典
- python - FFT预测、金融
- ruby - 使用 Chrome 设置代理的 Ruby Selenium-Webdriver 选项
- asp.net - 如何在asp.net core中连接amazon rds
- c# - Asp.Net Core 2.1 Windows 身份验证。HttpContext.User.Identity.Name 在 IIS 中不起作用
- gitlab - GitLab CE 升级失败
- entity-framework - 外键id设置为0
- c# - 虚拟路径到物理路径