Advertisement
BaSs_HaXoR

VSHPatcher VSH.cs [Source] by iMCSx & PS3ITA team

Feb 26th, 2015
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.55 KB | None | 0 0
  1. // ##################################################################################################
  2. // #                        VSH PATCHER SOURCE CODE BY iMCSx & PS3ITA TEAM                          #
  3. // ##################################################################################################
  4. /*********************************************************************************************
  5.  *
  6.  * iMCSx's VSH Patcher
  7.  *
  8.  * The Github address for the full source code  http://www.github.com/iMCSx/VSHPatcher
  9.  *
  10.  * A simple application to decrypt / patch / encrypt any vsh.self.
  11.  * The patch remove 2 PowerPC instructions and allow you to connect on PSN server.
  12.  *
  13.  * After some look into IDA it's possible to make only 1 instruction edit to patch the vsh
  14.  * But i'll follow the PS3ITA instructions, i take any risk.
  15.  *
  16.  * I'm not responsible about anything on your PS3.
  17.  * If something happen, just re-install your firmware.
  18.  *
  19.  * Tested on many firmwares and it works fine...
  20.  *
  21.  * Credits :
  22.  * - The PS3ITA Team
  23.  * - Naehrwert (Scetool)
  24.  * - HeAd (Testing)
  25.  *
  26.  ********************************************************************************************/
  27.  
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Diagnostics;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Text;
  34. using System.Threading;
  35. using System.Threading.Tasks;
  36. using System.Windows.Forms;
  37.  
  38. namespace VSH_Patcher
  39. {
  40.     class VSH
  41.     {
  42.         public static string WorkingDir = Application.StartupPath + @"\Tools\";
  43.  
  44.         public static string Unpacked_Name = "vsh_unpacked.elf";
  45.         public static string Patched_Name = "vsh_patched.elf";
  46.         public static string Origin_Name = "vsh_origin.self";
  47.         public static string Name = "vsh.self";
  48.  
  49.         public static byte[] MagicHeader = new byte[4] { 0x53, 0x43, 0x45, 0x00 };
  50.  
  51.         private static string SplitFileName(string path)
  52.         {
  53.             int length = path.LastIndexOf('\\');
  54.             string fileName = path.Substring(length);
  55.             return fileName;
  56.         }
  57.  
  58.         // Well we don't repet the same things everywhere...
  59.         public static void DeleteFile(string file)
  60.         {
  61.             if (File.Exists(file)) File.Delete(file);
  62.         }
  63.  
  64.         // Check if the header is a signed elf by SCE.
  65.         public static bool IsSelfSigned(string fullpath)
  66.         {
  67.             if (File.Exists(fullpath))
  68.             {
  69.                 List<byte> magicFile = File.ReadAllBytes(fullpath).ToList().GetRange(0, 4);
  70.                 if (ArrayCompare(magicFile.ToArray(), MagicHeader))
  71.                     return true;
  72.             }
  73.             else MessageBox.Show("Is self signed : File not found");
  74.  
  75.             return false;
  76.         }
  77.        
  78.         // Decrypt the self and give the elf
  79.         public static bool Unpack(string fullpath)
  80.         {
  81.             if (File.Exists(fullpath))
  82.             {
  83.                 DeleteFile(WorkingDir + Origin_Name);
  84.  
  85.                 File.Copy(fullpath, WorkingDir + Origin_Name);
  86.  
  87.                 string fileToUnpack = SplitFileName(fullpath).Replace("\\", "");
  88.                 string arguments = string.Format("scetool.exe --decrypt {0} {1}", Origin_Name, Unpacked_Name);
  89.  
  90.                 ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", "/C " + arguments);
  91.  
  92.                 Process p = new Process();
  93.  
  94.                 startInfo.CreateNoWindow = true;
  95.                 startInfo.WorkingDirectory = WorkingDir;
  96.                 startInfo.EnvironmentVariables["CYGWIN"] = "nodosfilewarning";
  97.                 startInfo.RedirectStandardInput = true;
  98.                 startInfo.UseShellExecute = false;
  99.                 startInfo.RedirectStandardOutput = true;
  100.                 startInfo.RedirectStandardError = true;
  101.  
  102.                 p = Process.Start(startInfo);
  103.                 p.WaitForExit();
  104.                 p.Close();
  105.  
  106.                 if (File.Exists(WorkingDir + Unpacked_Name))
  107.                     return true;
  108.             }
  109.             else MessageBox.Show("Unpacking failed : File not found");
  110.  
  111.             return false;
  112.         }
  113.  
  114.         // Encrypt the elf into a VSH signed elf format.
  115.         public static bool Pack(string filepath_elf)
  116.         {
  117.             if (File.Exists(filepath_elf))
  118.             {
  119.                 string fileToPack = SplitFileName(filepath_elf).Replace("\\", "");
  120.  
  121.                 // Build our command line using the default vsh.self as template.
  122.                 string arguments = string.Format("scetool --template {0} --sce-type=SELF --compress-data=TRUE --encrypt {1} {2}", Origin_Name, fileToPack, Name);
  123.  
  124.                 ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", "/C " + arguments);
  125.  
  126.                 Process p = new Process();
  127.  
  128.                 startInfo.CreateNoWindow = true;
  129.                 startInfo.WorkingDirectory = WorkingDir;
  130.                 startInfo.EnvironmentVariables["CYGWIN"] = "nodosfilewarning";
  131.                 startInfo.RedirectStandardInput = true;
  132.                 startInfo.UseShellExecute = false;
  133.                 startInfo.RedirectStandardOutput = true;
  134.                 startInfo.RedirectStandardError = true;
  135.  
  136.                 p = Process.Start(startInfo);
  137.                 p.WaitForExit();
  138.                 p.Close();
  139.  
  140.                 if (File.Exists(WorkingDir + Name))
  141.                 {
  142.                     DeleteFile(WorkingDir + Origin_Name);
  143.                     return true;
  144.                 }
  145.             }
  146.             else MessageBox.Show("Packing failed : File not found");
  147.  
  148.             return false;
  149.         }
  150.  
  151.         // Check if the VSH is already patched
  152.         public static bool IsPatched(string filepath_elf)
  153.         {
  154.             if (File.Exists(filepath_elf))
  155.             {
  156.                 // Get the data of the file into a list (much faster!)
  157.                 List<byte> elf_data = File.ReadAllBytes(filepath_elf).ToList();
  158.                 byte[] patch_sequence = StringToByteArray("2F8000026000000060000000");
  159.  
  160.                 for (int i = 0; i < elf_data.Count; i = i + 4)
  161.                 {
  162.                     // Out of array check
  163.                     if ((elf_data.Count - i) < 12)
  164.                         break;
  165.  
  166.                     // Get our sequence to compare
  167.                     byte[] sequence = elf_data.GetRange(i, 12).ToArray();
  168.  
  169.                     // If equals then return true
  170.                     if (ArrayCompare(sequence, patch_sequence)) return true;
  171.                 }
  172.  
  173.             }
  174.             else MessageBox.Show("Packing failed : File not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  175.             return false;
  176.         }
  177.  
  178.         public static bool PatchPSN(string filepath_elf)
  179.         {
  180.             if (File.Exists(filepath_elf))
  181.             {
  182.                 // Get the data of the file into a list (much faster!)
  183.                 List<byte> elf_data = File.ReadAllBytes(filepath_elf).ToList();
  184.                 byte[] ps3ita_patch_sequence = StringToByteArray("2F800002409E003C48000010");
  185.                 byte[] ps3ita_patched = StringToByteArray("2F8000026000000060000000");
  186.                 byte[] ps3ita_patch = StringToByteArray("6000000060000000");
  187.  
  188.                 for (int i = 0; i < elf_data.Count; i = i + 4)
  189.                 {
  190.                     // Out of array check
  191.                     if ((elf_data.Count - i) < 12)
  192.                         break;
  193.  
  194.                     // Get our sequence to compare
  195.                     byte[] sequence = elf_data.GetRange(i, 12).ToArray();
  196.  
  197.                     // Much faster than enumerable function
  198.                     if (ArrayCompare(sequence, ps3ita_patch_sequence) || ArrayCompare(sequence, ps3ita_patched))
  199.                     {
  200.                         int offset = i+4; //  -4 * 4;
  201.                         elf_data.RemoveRange(offset, ps3ita_patch.Length);
  202.                         elf_data.InsertRange(offset, ps3ita_patch);
  203.  
  204.                         // I'm want to be sure it is working...
  205.                         if (ArrayCompare(elf_data.GetRange(offset, 8).ToArray(), ps3ita_patch))
  206.                         {
  207.                             // Write our file patched !
  208.                             File.WriteAllBytes(WorkingDir + Patched_Name, elf_data.ToArray());
  209.  
  210.                             if (File.Exists(WorkingDir + Patched_Name))
  211.                             {
  212.                                 // Delete the elf unpacked if exists.
  213.                                 DeleteFile(WorkingDir + Unpacked_Name);
  214.                                 return true;
  215.                             }
  216.                         }
  217.                         else MessageBox.Show("The patch failed, report to iMCSx this error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  218.                     }
  219.                 }
  220.  
  221.             }
  222.             else MessageBox.Show("Packing failed : File not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  223.  
  224.             return false;
  225.         }
  226.  
  227.         // Custom function to check arrays faster
  228.         public static bool ArrayCompare(byte[] b1, byte[] b2)
  229.         {
  230.             if (b1.Length == b2.Length)
  231.             {
  232.                 for (int i = 0; i < b1.Length; i++)
  233.                     if (b1[i] != b2[i])
  234.                         return false;
  235.                 return true;
  236.             }
  237.             return false;
  238.         }
  239.  
  240.         // Convert hex string to bytes
  241.         internal static byte[] StringToByteArray(string hex)
  242.         {
  243.             string replace = hex.Replace("0x", "").Replace(" ", "");
  244.             string Stringz = replace.Insert(replace.Length - 1, "0");
  245.  
  246.             int Odd = replace.Length;
  247.             bool Nombre;
  248.             if (Odd % 2 == 0)
  249.                 Nombre = true;
  250.             else
  251.                 Nombre = false;
  252.             try
  253.             {
  254.                 if (Nombre == true)
  255.                 {
  256.                     return Enumerable.Range(0, replace.Length)
  257.                  .Where(x => x % 2 == 0)
  258.                  .Select(x => Convert.ToByte(replace.Substring(x, 2), 16))
  259.                  .ToArray();
  260.                 }
  261.                 else
  262.                 {
  263.                     return Enumerable.Range(0, replace.Length)
  264.                  .Where(x => x % 2 == 0)
  265.                  .Select(x => Convert.ToByte(Stringz.Substring(x, 2), 16))
  266.                  .ToArray();
  267.                 }
  268.             }
  269.             catch { throw new System.ArgumentException("Value not possible.", "Byte Array"); }
  270.         }
  271.     }
  272. }
  273. // ##################################################################################################
  274. //BaSs_HaXoR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement