Esempio n. 1
0
        private static int ProcessAction()
        {
            int num = -1;

            if (!string.IsNullOrEmpty(CmdLine.file))
            {
                if (!string.IsNullOrEmpty(CmdLine.region) && !string.IsNullOrEmpty(CmdLine.model) && !string.IsNullOrEmpty(CmdLine.version))
                {
                    num = CmdLine.DoDecrypt();
                }
                else if (!string.IsNullOrEmpty(CmdLine.logicValue) && !string.IsNullOrEmpty(CmdLine.version))
                {
                    num = CmdLine.DoDecrypt();
                }
            }
            else if (!string.IsNullOrEmpty(CmdLine.model) && !string.IsNullOrEmpty(CmdLine.region))
            {
                num = !CmdLine.checkonly ? CmdLine.DoDownload() : CmdLine.DoCheck();
            }
            if (num == -1)
            {
                CmdLine.DisplayUsage();
                num = 1;
            }
            return(num);
        }
Esempio n. 2
0
 public static int Decrypt(string encryptedFile, string outputFile, bool GUI = true)
 {
     using (FileStream fileStream1 = new FileStream(encryptedFile, FileMode.Open))
     {
         using (FileStream fileStream2 = new FileStream(outputFile, FileMode.Create))
         {
             RijndaelManaged rijndaelManaged = new RijndaelManaged();
             rijndaelManaged.Mode      = CipherMode.ECB;
             rijndaelManaged.BlockSize = 128;
             rijndaelManaged.Padding   = PaddingMode.PKCS7;
             using (ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(Crypto.KEY, Crypto.IV))
             {
                 try
                 {
                     Utility.PreventDeepSleep(Utility.PDSMode.Start);
                     using (CryptoStream cryptoStream = new CryptoStream((Stream)fileStream1, decryptor, CryptoStreamMode.Read))
                     {
                         byte[] buffer = new byte[4096];
                         long   num    = 0;
                         int    count;
                         do
                         {
                             Utility.PreventDeepSleep(Utility.PDSMode.Continue);
                             num += (long)(count = cryptoStream.Read(buffer, 0, buffer.Length));
                             fileStream2.Write(buffer, 0, count);
                             if (GUI)
                             {
                                 Crypto.form.SetProgressBar(Utility.GetProgress(num, fileStream1.Length));
                             }
                             else
                             {
                                 CmdLine.SetProgress(Utility.GetProgress(num, fileStream1.Length));
                             }
                         }while (count > 0);
                     }
                 }
                 catch (CryptographicException ex)
                 {
                     Logger.WriteLog("Error decrypting file: Wrong key.", false);
                     return(3);
                 }
                 catch (TargetInvocationException ex)
                 {
                     Logger.WriteLog("Error decrypting file: Please turn off FIPS compliance checking.", false);
                     return(800);
                 }
                 catch (IOException ex)
                 {
                     Logger.WriteLog("Error decrypting file: IOException: " + ex.Message, false);
                     return(3);
                 }
                 finally
                 {
                     Utility.PreventDeepSleep(Utility.PDSMode.Stop);
                 }
             }
         }
     }
     return(0);
 }
Esempio n. 3
0
 public static int Main(string[] args)
 {
     Thread.Sleep(200);
     if (CmdLine.InputValidation(args))
     {
         return(CmdLine.ProcessAction());
     }
     CmdLine.DisplayUsage();
     return(1);
 }
Esempio n. 4
0
 private static int Main(string[] args)
 {
     if (args.Length == 0)
     {
         Imports.FreeConsole();
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run((Form) new Form1());
     }
     else
     {
         Utility.run_by_cmd = true;
         Environment.Exit(CmdLine.Main(args));
     }
     return(0);
 }
Esempio n. 5
0
 private static int DoDecrypt()
 {
     Logger.WriteLog("========== SamFirm Firmware Decrypter ==========\n", false);
     Logger.WriteLog("Decrypting file " + CmdLine.file + "...", false);
     CmdLine.CreateProgressbar();
     if (CmdLine.file.EndsWith(".enc2"))
     {
         Crypto.SetDecryptKey(CmdLine.region, CmdLine.model, CmdLine.version);
     }
     else if (CmdLine.file.EndsWith(".enc4"))
     {
         Crypto.SetDecryptKey(CmdLine.version, CmdLine.logicValue);
     }
     if (Crypto.Decrypt(CmdLine.file, Path.GetFileNameWithoutExtension(CmdLine.file), false) != 0)
     {
         Logger.WriteLog("\nError decrypting file", false);
         Logger.WriteLog("Please make sure the filename is not modified and verify the version / logicValue argument", false);
         File.Delete(Path.GetFileNameWithoutExtension(CmdLine.file));
         return(3);
     }
     Logger.WriteLog("\nDecrypting successful", false);
     return(0);
 }
Esempio n. 6
0
 private static bool InputValidation(string[] args)
 {
     if (!CmdLine.ParseArgs(args))
     {
         Logger.WriteLog("Error parsing arguments\n", false);
         return(false);
     }
     if (!string.IsNullOrEmpty(CmdLine.file) && !File.Exists(CmdLine.file))
     {
         Logger.WriteLog("File " + CmdLine.file + " does not exist\n", false);
         return(false);
     }
     if (!string.IsNullOrEmpty(CmdLine.file) && !CmdLine.ParseFileName())
     {
         Logger.WriteLog("Could not parse filename. Make sure the filename was not edited\n", false);
         return(false);
     }
     if (string.IsNullOrEmpty(CmdLine.folder) || Directory.Exists(CmdLine.folder))
     {
         return(true);
     }
     Logger.WriteLog("Folder " + CmdLine.folder + " does not exist\n", false);
     return(false);
 }
Esempio n. 7
0
        private static int DoDownload()
        {
            Logger.WriteLog("========== SamFirm Firmware Downloader ==========\n", false);
            Command.Firmware fw;
            if (string.IsNullOrEmpty(CmdLine.version))
            {
                fw = Command.UpdateCheckAuto(CmdLine.model, CmdLine.region, CmdLine.binary);
                if (fw.FetchAttempts == 0)
                {
                    return(5);
                }
            }
            else
            {
                fw = Command.UpdateCheck(CmdLine.model, CmdLine.region, CmdLine.version, CmdLine.binary, false);
            }
            if (fw.Version == null)
            {
                return(2);
            }
            string str = Path.Combine(CmdLine.folder, fw.Filename);

            Logger.WriteLog("Downloading...\n", false);
            CmdLine.CreateProgressbar();
            int num1;

            do
            {
                Utility.ReconnectCmdLine();
                Utility.ReconnectDownload = false;
                num1 = Command.Download(fw.Path, fw.Filename, fw.Version, fw.Region, fw.Model_Type, str, fw.Size, false);
            }while (Utility.ReconnectDownload);
            if (num1 != 200 && num1 != 206)
            {
                Logger.WriteLog("Error: " + (object)num1, false);
                return(4);
            }
            if (CmdLine.autodecrypt)
            {
                if (str.EndsWith(".enc2"))
                {
                    Crypto.SetDecryptKey(fw.Region, fw.Model, fw.Version);
                }
                else if (str.EndsWith(".enc4"))
                {
                    if (fw.BinaryNature == 1)
                    {
                        Crypto.SetDecryptKey(fw.Version, fw.LogicValueFactory);
                    }
                    else
                    {
                        Crypto.SetDecryptKey(fw.Version, fw.LogicValueHome);
                    }
                }
                Logger.WriteLog("\nDecrypting...\n", false);
                CmdLine.CreateProgressbar();
                CmdLine.fwdest = Path.Combine(Path.GetDirectoryName(str), Path.GetFileNameWithoutExtension(fw.Filename));
                int num2 = Crypto.Decrypt(str, CmdLine.fwdest, false);
                File.Delete(str);
                if (num2 != 0)
                {
                    File.Delete(CmdLine.fwdest);
                    return(3);
                }
            }
            if (!string.IsNullOrEmpty(CmdLine.metafile))
            {
                CmdLine.SaveMeta(fw);
            }
            Logger.WriteLog("\nFinished", false);
            return(0);
        }
Esempio n. 8
0
        public static int DownloadBinary(string path, string file, string saveTo, string size, bool GUI = true)
        {
            int            num5;
            long           num = 0L;
            HttpWebRequest wr  = KiesRequest.Create("http://cloud-neofussvr.sslcs.cdngc.net/NF_DownloadBinaryForMass.do?file=" + path + file);

            wr.Method = "GET";
            wr.Headers["Authorization"] = Imports.GetAuthorization(Nonce).Replace("Authorization: ", "").Replace("nonce=\"", "nonce=\"" + Nonce);
            wr.Timeout          = 0x61a8;
            wr.ReadWriteTimeout = 0x61a8;
            if (System.IO.File.Exists(saveTo))
            {
                long length = new FileInfo(saveTo).Length;
                if (long.Parse(size) == length)
                {
                    Logger.WriteLog("File already downloaded.", false);
                    return(200);
                }
                Logger.WriteLog("File exists. Resuming download...", false);
                wr.AddRange((int)length);
                num = length;
            }
            using (HttpWebResponse response = (HttpWebResponse)wr.GetResponseFUS())
            {
                if (response == null)
                {
                    Logger.WriteLog("Error downloading: response is null", false);
                    return(0x385);
                }
                if ((response.StatusCode != HttpStatusCode.OK) && (response.StatusCode != HttpStatusCode.PartialContent))
                {
                    Logger.WriteLog("Error downloading: " + ((int)response.StatusCode), false);
                }
                else
                {
                    long total = long.Parse(response.GetResponseHeader("content-length")) + num;
                    if (!System.IO.File.Exists(saveTo) || (new FileInfo(saveTo).Length != total))
                    {
                        byte[]    buffer = new byte[0x2000];
                        Stopwatch sw     = new Stopwatch();
                        Utility.ResetSpeed(num);
                        try
                        {
                            Utility.PreventDeepSleep(Utility.PDSMode.Start);
                            using (BinaryWriter writer = new BinaryWriter(new FileStream(saveTo, FileMode.Append)))
                            {
                                int count = 0;
                                do
                                {
                                    Utility.PreventDeepSleep(Utility.PDSMode.Continue);
                                    if (GUI && form.PauseDownload)
                                    {
                                        goto Label_02BB;
                                    }
                                    num += count = response.GetResponseStream().Read(buffer, 0, buffer.Length);
                                    if (count > 0)
                                    {
                                        writer.Write(buffer, 0, count);
                                        if (GUI)
                                        {
                                            int dlspeed = Utility.DownloadSpeed(num, sw);
                                            if (dlspeed != -1)
                                            {
                                                form.lbl_speed.Invoke(new Action(delegate {
                                                    form.lbl_speed.Text = dlspeed + "kB/s";
                                                }));
                                            }
                                        }
                                    }
                                    if (GUI)
                                    {
                                        form.SetProgressBar(Utility.GetProgress(num, total));
                                    }
                                    else
                                    {
                                        CmdLine.SetProgress(Utility.GetProgress(num, total));
                                    }
                                }while (count > 0);
                            }
                        }
                        catch (IOException exception)
                        {
                            Logger.WriteLog("Error: Can't access output file " + saveTo, false);
                            if (GUI)
                            {
                                form.PauseDownload = true;
                            }
                            Logger.WriteLog(exception.ToString(), false);
                            return(-1);
                        }
                        catch (WebException)
                        {
                            Logger.WriteLog("Error: Connection interrupted", false);
                            SetReconnect();
                        }
                        finally
                        {
                            Utility.PreventDeepSleep(Utility.PDSMode.Stop);
                            if (GUI)
                            {
                                form.lbl_speed.Invoke(new Action(delegate {
                                    form.lbl_speed.Text = "0kB/s";
                                }));
                            }
                        }
                    }
                }
Label_02BB:
                num5 = (int)response.StatusCode;
            }
            return(num5);
        }
Esempio n. 9
0
 public static int Decrypt(string encryptedFile, string outputFile, bool GUI = true)
 {
     using (FileStream stream = new FileStream(encryptedFile, FileMode.Open))
     {
         using (FileStream stream2 = new FileStream(outputFile, FileMode.Create))
         {
             RijndaelManaged managed = new RijndaelManaged {
                 Mode      = CipherMode.ECB,
                 BlockSize = 0x80,
                 Padding   = PaddingMode.PKCS7
             };
             using (ICryptoTransform transform = managed.CreateDecryptor(KEY, IV))
             {
                 try
                 {
                     Utility.PreventDeepSleep(Utility.PDSMode.Start);
                     using (CryptoStream stream3 = new CryptoStream(stream, transform, CryptoStreamMode.Read))
                     {
                         byte[] buffer = new byte[0x1000];
                         long   num    = 0L;
                         int    count  = 0;
                         do
                         {
                             Utility.PreventDeepSleep(Utility.PDSMode.Continue);
                             num += count = stream3.Read(buffer, 0, buffer.Length);
                             stream2.Write(buffer, 0, count);
                             if (GUI)
                             {
                                 form.SetProgressBar(Utility.GetProgress(num, stream.Length));
                             }
                             else
                             {
                                 CmdLine.SetProgress(Utility.GetProgress(num, stream.Length));
                             }
                         }while (count > 0);
                     }
                 }
                 catch (CryptographicException)
                 {
                     Logger.WriteLog("Error decrypting file: Wrong key.", false);
                     return(3);
                 }
                 catch (TargetInvocationException)
                 {
                     Logger.WriteLog("Error decrypting file: Please turn off FIPS compliance checking.", false);
                     return(800);
                 }
                 catch (IOException exception)
                 {
                     Logger.WriteLog("Error decrypting file: IOException: " + exception.Message, false);
                     return(3);
                 }
                 finally
                 {
                     Utility.PreventDeepSleep(Utility.PDSMode.Stop);
                 }
             }
         }
     }
     return(0);
 }