Advertisement
FrayxRulez

Osmeta mainBundle.dll unpacking

Mar 20th, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.55 KB | None | 0 0
  1. using CommandLine;
  2. using CommandLine.Text;
  3. using System;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7.  
  8. // Facebook:  -b "C:\Users\Fela\Downloads\Smandruppo\Facebook\mainBundle.dll"  -o "Facebook"
  9. // Messenger: -b "C:\Users\Fela\Downloads\Smandruppo\Messenger\mainBundle.dll" -o "Messenger"
  10. // Instagram: -b "C:\Users\Fela\Downloads\Smandruppo\Instagram\mainBundle.dll" -o "Instagram"
  11.  
  12. namespace TestMainBundle
  13. {
  14.     class Program
  15.     {
  16.         const int PKZip = 67324752; // 50 4B 03 04
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             var options = new Options();
  21.             if (Parser.Default.ParseArguments(args, options))
  22.             {
  23.                 if (File.Exists(options.MainBundle))
  24.                 {
  25.                     if (options.Output == null)
  26.                     {
  27.                         options.Output = Path.GetDirectoryName(options.MainBundle);
  28.                         options.Output = Path.GetFileName(options.Output);
  29.                     }
  30.  
  31.                     Directory.CreateDirectory(options.Output);
  32.  
  33.                     using (var stream = new FileStream(options.MainBundle, FileMode.Open, FileAccess.Read))
  34.                     {
  35.                         var reader = new BinaryReader(stream);
  36.  
  37.                         var dosHeader = reader.ReadStruct<IMAGE_DOS_HEADER>();
  38.                         stream.Seek(dosHeader.e_lfanew, SeekOrigin.Begin);
  39.  
  40.                         var ntHeadersSignature = reader.ReadUInt32();
  41.  
  42.                         var fileHeader = reader.ReadStruct<IMAGE_FILE_HEADER>();
  43.  
  44.                         if ((0x0100 & fileHeader.Characteristics) == 0x0100) // IMAGE_FILE_32BIT_MACHINE
  45.                         {
  46.                             stream.Seek(224, SeekOrigin.Current); // IMAGE_OPTIONAL_HEADER32 size
  47.                         }
  48.                         else
  49.                         {
  50.                             stream.Seek(240, SeekOrigin.Current); // IMAGE_OPTIONAL_HEADER64 size
  51.                         }
  52.  
  53.                         for (int i = 0; i < fileHeader.NumberOfSections; ++i)
  54.                         {
  55.                             var dataSegment = reader.ReadStruct<IMAGE_SECTION_HEADER>();
  56.                             if (dataSegment.Section.StartsWith(".data"))
  57.                             {
  58.                                 stream.Seek(dataSegment.PointerToRawData, SeekOrigin.Begin);
  59.  
  60.                                 var length = reader.ReadUInt32();
  61.                                 var reserved = reader.ReadUInt32();
  62.  
  63.                                 while (stream.Position - dataSegment.PointerToRawData < length)
  64.                                 {
  65.                                     var pkzip = reader.ReadStruct<PKZIP_FILE_HEADER>();
  66.                                     if (pkzip.Signature == PKZip)
  67.                                     {
  68.                                         var fileNameBuffer = reader.ReadBytes(pkzip.FileNameLength);
  69.                                         var fileName = Encoding.Default.GetString(fileNameBuffer);
  70.  
  71.                                         if (pkzip.ExtraFieldLength > 0)
  72.                                         {
  73.                                             reader.ReadBytes(pkzip.ExtraFieldLength); // SKIP
  74.                                         }
  75.  
  76.                                         var fileBuffer = reader.ReadBytes((int)pkzip.CompressedSize);
  77.  
  78.                                         if (fileName.EndsWith("/"))
  79.                                         {
  80.                                             Directory.CreateDirectory(Path.Combine(options.Output, fileName.TrimEnd('/')));
  81.                                         }
  82.                                         else
  83.                                         {
  84.                                             File.WriteAllBytes(Path.Combine(options.Output, fileName.Replace("/", "\\")), fileBuffer);
  85.                                         }
  86.                                     }
  87.                                     else
  88.                                     {
  89.                                         stream.Seek(stream.Length, SeekOrigin.Begin);
  90.                                         Console.WriteLine("No a PKZip, ending at: {0:X}", stream.Position);
  91.                                     }
  92.                                 }
  93.  
  94.                                 Console.WriteLine("Done");
  95.                             }
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.     static class Extensions
  104.     {
  105.         public static T ReadStruct<T>(this BinaryReader reader)
  106.         {
  107.             byte[] bytes = reader.ReadBytes(Marshal.SizeOf(typeof(T)));
  108.  
  109.             GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  110.             T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
  111.             handle.Free();
  112.  
  113.             return theStructure;
  114.         }
  115.     }
  116.  
  117.     class Options
  118.     {
  119.         [Option('b', "bundle", Required = true, HelpText = "Main bundle to be processed.")]
  120.         public string MainBundle { get; set; }
  121.  
  122.         [Option('o', "output", HelpText = "Prints all messages to standard output.")]
  123.         public string Output { get; set; }
  124.  
  125.         [ParserState]
  126.         public IParserState LastParserState { get; set; }
  127.  
  128.         [HelpOption]
  129.         public string GetUsage()
  130.         {
  131.             return HelpText.AutoBuild(this, (current) => HelpText.DefaultParsingErrorsHandler(this, current));
  132.         }
  133.     }
  134.  
  135.     #region PKZip Header Structures
  136.  
  137.     [StructLayout(LayoutKind.Sequential, Pack = 1)]
  138.     public struct PKZIP_FILE_HEADER
  139.     {
  140.         public UInt32 Signature;
  141.         public UInt16 Version;
  142.         public UInt16 Flags;
  143.         public UInt16 Compression;
  144.         public UInt16 ModificationTime;
  145.         public UInt16 ModificationDate;
  146.         public UInt32 Crc32;
  147.         public UInt32 CompressedSize;
  148.         public UInt32 UncompressedSize;
  149.         public UInt16 FileNameLength;
  150.         public UInt16 ExtraFieldLength;
  151.     }
  152.  
  153.     #endregion
  154.  
  155.     #region Portable Executable Header Structures
  156.  
  157.     public struct IMAGE_DOS_HEADER
  158.     {
  159.         public UInt16 e_magic;              // Magic number
  160.         public UInt16 e_cblp;               // Bytes on last page of file
  161.         public UInt16 e_cp;                 // Pages in file
  162.         public UInt16 e_crlc;               // Relocations
  163.         public UInt16 e_cparhdr;            // Size of header in paragraphs
  164.         public UInt16 e_minalloc;           // Minimum extra paragraphs needed
  165.         public UInt16 e_maxalloc;           // Maximum extra paragraphs needed
  166.         public UInt16 e_ss;                 // Initial (relative) SS value
  167.         public UInt16 e_sp;                 // Initial SP value
  168.         public UInt16 e_csum;               // Checksum
  169.         public UInt16 e_ip;                 // Initial IP value
  170.         public UInt16 e_cs;                 // Initial (relative) CS value
  171.         public UInt16 e_lfarlc;             // File address of relocation table
  172.         public UInt16 e_ovno;               // Overlay number
  173.         public UInt16 e_res_0;              // Reserved words
  174.         public UInt16 e_res_1;              // Reserved words
  175.         public UInt16 e_res_2;              // Reserved words
  176.         public UInt16 e_res_3;              // Reserved words
  177.         public UInt16 e_oemid;              // OEM identifier (for e_oeminfo)
  178.         public UInt16 e_oeminfo;            // OEM information; e_oemid specific
  179.         public UInt16 e_res2_0;             // Reserved words
  180.         public UInt16 e_res2_1;             // Reserved words
  181.         public UInt16 e_res2_2;             // Reserved words
  182.         public UInt16 e_res2_3;             // Reserved words
  183.         public UInt16 e_res2_4;             // Reserved words
  184.         public UInt16 e_res2_5;             // Reserved words
  185.         public UInt16 e_res2_6;             // Reserved words
  186.         public UInt16 e_res2_7;             // Reserved words
  187.         public UInt16 e_res2_8;             // Reserved words
  188.         public UInt16 e_res2_9;             // Reserved words
  189.         public UInt32 e_lfanew;             // File address of new exe header
  190.     }
  191.  
  192.     [StructLayout(LayoutKind.Sequential, Pack = 1)]
  193.     public struct IMAGE_FILE_HEADER
  194.     {
  195.         public UInt16 Machine;
  196.         public UInt16 NumberOfSections;
  197.         public UInt32 TimeDateStamp;
  198.         public UInt32 PointerToSymbolTable;
  199.         public UInt32 NumberOfSymbols;
  200.         public UInt16 SizeOfOptionalHeader;
  201.         public UInt16 Characteristics;
  202.     }
  203.  
  204.     [StructLayout(LayoutKind.Explicit)]
  205.     public struct IMAGE_SECTION_HEADER
  206.     {
  207.         [FieldOffset(0)]
  208.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  209.         public char[] Name;
  210.         [FieldOffset(8)]
  211.         public UInt32 VirtualSize;
  212.         [FieldOffset(12)]
  213.         public UInt32 VirtualAddress;
  214.         [FieldOffset(16)]
  215.         public UInt32 SizeOfRawData;
  216.         [FieldOffset(20)]
  217.         public UInt32 PointerToRawData;
  218.         [FieldOffset(24)]
  219.         public UInt32 PointerToRelocations;
  220.         [FieldOffset(28)]
  221.         public UInt32 PointerToLinenumbers;
  222.         [FieldOffset(32)]
  223.         public UInt16 NumberOfRelocations;
  224.         [FieldOffset(34)]
  225.         public UInt16 NumberOfLinenumbers;
  226.         [FieldOffset(36)]
  227.         public UInt32 Characteristics;
  228.  
  229.         public string Section
  230.         {
  231.             get { return new string(Name); }
  232.         }
  233.     }
  234.  
  235.     #endregion File Header Structures
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement