Exemplo n.º 1
0
        /// <summary>
        /// Suck the brains out of the app
        /// </summary>
        static void Main(string[] args)
        {
            Console.WriteLine("OpenIZ BrainBug - Android Extraction Tool");
            Console.WriteLine("Version {0}", Assembly.GetEntryAssembly().GetName().Version);

            var parameters = new ParameterParser <ConsoleParameters>().Parse(args);

            if (parameters.Help)
            {
                new ParameterParser <ConsoleParameters>().WriteHelp(Console.Out);
                return;
            }
            if (parameters.TargetFile == null && parameters.ExtractDir == null)
            {
                Console.WriteLine("Either --tar or --extract must be specified");
                return;
            }

            if (parameters.PackageId != null)
            {
                var           exeFile = Path.Combine(parameters.SdkPath, "platform-tools", "adb.exe");
                StringBuilder argStr  = new StringBuilder();

                if (!String.IsNullOrEmpty(parameters.DeviceId))
                {
                    argStr.AppendFormat(" -s {0} ", parameters.DeviceId);
                }
                argStr.Append("backup ");

                argStr.AppendFormat("-f \"backup.ab\"", parameters.BackupFile);

                argStr.Append(" -noapk -noobb ");
                argStr.Append(parameters.PackageId);
                Console.WriteLine("Starting {0} {1}", exeFile, argStr.ToString());
                var pi = new Process();
                pi.StartInfo.FileName               = String.Format("\"{0}\"", exeFile);
                pi.StartInfo.Arguments              = argStr.ToString();
                pi.StartInfo.CreateNoWindow         = true;
                pi.StartInfo.RedirectStandardError  = true;
                pi.StartInfo.RedirectStandardOutput = true;
                pi.StartInfo.UseShellExecute        = false;
                pi.Start();
                Console.WriteLine(pi.StandardOutput.ReadToEnd());
                Console.WriteLine(pi.StandardError.ReadToEnd());
                pi.WaitForExit();

                if (File.Exists(parameters.BackupFile))
                {
                    File.Delete(parameters.BackupFile);
                }
                File.Move("backup.ab", parameters.BackupFile);
            }

            if (!File.Exists(parameters.BackupFile))
            {
                Console.WriteLine("Cannot find specified backup file!");
                return;
            }

            try
            {
                Console.WriteLine("Extracting {0}...", parameters.BackupFile);
                byte[] buffer = new byte[8096];
                using (FileStream ins = File.OpenRead(parameters.BackupFile))
                {
                    ins.Read(buffer, 0, 24);
                    String magic = System.Text.Encoding.UTF8.GetString(buffer, 0, 24);
                    //ins.Seek(24, SeekOrigin.Begin);
                    using (FileStream outs = File.Create(parameters.TargetFile))
                    {
                        using (ZLibNet.ZLibStream df = new ZLibNet.ZLibStream(ins, ZLibNet.CompressionMode.Decompress))
                        {
                            int br = 8096;
                            while (br == 8096)
                            {
                                br = df.Read(buffer, 0, 8096);
                                outs.Write(buffer, 0, br);
                            }
                        }
                    }
                }

                // Extract
                if (parameters.ExtractDir != null)
                {
                    if (!Directory.Exists(parameters.ExtractDir))
                    {
                        Directory.CreateDirectory(parameters.ExtractDir);
                    }
                    using (var fs = File.OpenRead(parameters.TargetFile))
                        using (var tar = TarReader.Open(fs))
                            while (tar.MoveToNextEntry())
                            {
                                string outName = Path.Combine(parameters.ExtractDir, tar.Entry.Key);
                                if (!Directory.Exists(Path.GetDirectoryName(outName)))
                                {
                                    Directory.CreateDirectory(Path.GetDirectoryName(outName));
                                }
                                Console.WriteLine("{0} > {1}", tar.Entry.Key, outName);

                                if (!tar.Entry.IsDirectory)
                                {
                                    using (var ofs = File.Create(outName))
                                        tar.WriteEntryTo(ofs);
                                }
                            }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemplo n.º 2
0
        public override void SaveChunkToStream(Stream s)
        {
            long sizeOffset, tempOffset;

            Write4(s, (uint)Id.File);
            sizeOffset = s.Position;
            s.Seek(4, SeekOrigin.Current);
            byte[] hash = MD5.Create().ComputeHash(Data);
            WriteArray(s, hash);
            WriteArray(s, new byte[] { 0x00, 0x00, 0x00, 0x00 });
            Write2(s, Permissions);
            Write2(s, Unknown); //WriteArray(s, new byte[] { 0xFF, 0xFF });
            Write4(s, (uint)Data.Length);
            Write1(s, (byte)(IsCompressed ? 1 : 0));
            WriteString(s, Name);
            WriteString(s, Date);
            WriteString(s, Time);

            if (IsCompressed == true)
            {
                int index   = 0;
                int reaming = (int)Data.Length;
                if (Data.Length > 0)
                {
                    do
                    {
                        int blockSize = 0;

                        if (reaming > 0x00100000)
                        {
                            blockSize = 0x00100000;
                        }
                        else
                        {
                            blockSize = reaming;
                        }

                        MemoryStream       compressed = new MemoryStream();
                        ZLibNet.ZLibStream compressor = new ZLibNet.ZLibStream(compressed, ZLibNet.CompressionMode.Compress, ZLibNet.CompressionLevel.BestCompression);
                        compressor.Write(Data, index, blockSize);
                        compressor.Flush();
                        byte[] compressedArray = new byte[compressed.Length];
                        compressed.Seek(0, SeekOrigin.Begin);
                        compressed.Read(compressedArray, 0, (int)compressed.Length);

                        Write4(s, 0x00000100);
                        Write4(s, (uint)(compressed.Length + 4));
                        Write4(s, Swap4((uint)blockSize));
                        WriteArray(s, compressedArray);

                        if ((compressed.Length % 4) != 0)
                        {
                            s.Seek(4 - (compressed.Length % 4), SeekOrigin.Current);
                        }

                        index   += 0x00100000;
                        reaming -= 0x00100000;
                    } while (index < Data.Length);
                }

                Write4(s, 0x00000101);
                Write4(s, 0x00000000);
            }
            else
            {
                WriteArray(s, Data);
            }

            tempOffset = s.Position;
            uint size = (uint)(tempOffset - sizeOffset - 4);

            s.Seek(sizeOffset, SeekOrigin.Begin);
            Write4(s, size);
            s.Seek(tempOffset, SeekOrigin.Begin);
        }