Trucha bug restoration is slowly coming to life...

This commit is contained in:
givememystuffplease 2009-08-04 00:01:01 +00:00
parent 1fff53a1e4
commit fda8531e22

View File

@ -583,8 +583,6 @@ namespace NUS_Downloader
else else
contenttypes[i] = (int)ContentTypes.Normal; contenttypes[i] = (int)ContentTypes.Normal;
startoffset += 36; startoffset += 36;
// DEBUG:
//WriteStatus(contenttypes[i].ToString());
} }
return contenttypes; return contenttypes;
@ -985,9 +983,6 @@ namespace NUS_Downloader
WriteStatus("Trucha Signing TMD..."); WriteStatus("Trucha Signing TMD...");
Array.Resize(ref tmd, 484 + (Convert.ToInt32(contentstrnum) * 36)); Array.Resize(ref tmd, 484 + (Convert.ToInt32(contentstrnum) * 36));
// DEBUG: Mii Channel Test...
tmd[0x18F] = 0x01;
tmd = ZeroSignature(tmd); tmd = ZeroSignature(tmd);
tmd = TruchaSign(tmd); tmd = TruchaSign(tmd);
@ -1004,9 +999,6 @@ namespace NUS_Downloader
Array.Resize(ref cetkbuff, 0x2A4); Array.Resize(ref cetkbuff, 0x2A4);
// DEBUG: Mii Channel Test...
cetkbuff[0x1DF] = 0x01;
cetkbuff = ZeroSignature(cetkbuff); cetkbuff = ZeroSignature(cetkbuff);
cetkbuff = TruchaSign(cetkbuff); cetkbuff = TruchaSign(cetkbuff);
@ -1589,7 +1581,6 @@ namespace NUS_Downloader
{ {
regitem.DropDownItems.Add("v" + versions[y]); regitem.DropDownItems.Add("v" + versions[y]);
} }
// TODO : wat...
regitem.DropDownItemClicked += new ToolStripItemClickedEventHandler(deepitem_clicked); regitem.DropDownItemClicked += new ToolStripItemClickedEventHandler(deepitem_clicked);
} }
} }
@ -1703,11 +1694,6 @@ namespace NUS_Downloader
void deepitem_clicked(object sender, ToolStripItemClickedEventArgs e) void deepitem_clicked(object sender, ToolStripItemClickedEventArgs e)
{ {
// DEBUG:
//WriteStatus("item text: " + e.ClickedItem.Text); VERSION
//WriteStatus("Owner of item: " + e.ClickedItem.OwnerItem.Text); REGION
//WriteStatus("Owner of Owner of item: " + e.ClickedItem.OwnerItem.OwnerItem.Text); TITLE ID
titleidbox.Text = e.ClickedItem.OwnerItem.OwnerItem.Text.Substring(0, 16); titleidbox.Text = e.ClickedItem.OwnerItem.OwnerItem.Text.Substring(0, 16);
titleidbox.Text = titleidbox.Text.Replace("XX", e.ClickedItem.OwnerItem.Text.Substring(0, 2)); titleidbox.Text = titleidbox.Text.Replace("XX", e.ClickedItem.OwnerItem.Text.Substring(0, 2));
@ -1858,11 +1844,6 @@ namespace NUS_Downloader
{ {
WriteStatus("\n" + e.ClickedItem.OwnerItem.ToolTipText); WriteStatus("\n" + e.ClickedItem.OwnerItem.ToolTipText);
} }
// DEBUG:
//WriteStatus("item text: " + e.ClickedItem.Text);
//WriteStatus("Owner of item: " + e.ClickedItem.OwnerItem.Text);
//WriteStatus("Owner of Owner of item: " + e.ClickedItem.OwnerItem.OwnerItem.Text);
} }
void HandleMismatch(int contentsize, int actualsize) void HandleMismatch(int contentsize, int actualsize)
@ -1984,8 +1965,6 @@ namespace NUS_Downloader
if (ComputeSHA(hashobject)[0] == 0x00) if (ComputeSHA(hashobject)[0] == 0x00)
{ {
// DEBUG:
//WriteStatus(DisplayBytes(ComputeSHA(hashobject)));
WriteStatus(" - Successfully Trucha Signed."); WriteStatus(" - Successfully Trucha Signed.");
return tmdortik; return tmdortik;
} }
@ -2174,8 +2153,7 @@ namespace NUS_Downloader
// The amount of time for the limit. // The amount of time for the limit.
byte[] limitseconds = new byte[4]; byte[] limitseconds = new byte[4];
limitseconds = InttoByteArray(Convert.ToInt32(timelimitsecs.Text), 4); limitseconds = InttoByteArray(Convert.ToInt32(timelimitsecs.Text), 4);
//DEBUG
//WriteStatus(DisplayBytes(limitseconds, " "));
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
cetkbuff[0x248 + i] = limitseconds[i]; cetkbuff[0x248 + i] = limitseconds[i];
@ -2861,9 +2839,6 @@ namespace NUS_Downloader
thelength += 1; thelength += 1;
remainder = thelength % alignto; remainder = thelength % alignto;
} }
// DEBUG:
WriteStatus("Aligning by: " + alignto + " ... New Length: " + thelength);
WriteStatus(" Dif: " + (thelength - content.Length));
Array.Resize(ref content, (int)thelength); Array.Resize(ref content, (int)thelength);
return content; return content;
} */ } */
@ -2872,10 +2847,6 @@ namespace NUS_Downloader
{ {
int len = (src.Length + pad - 1) / pad * pad; int len = (src.Length + pad - 1) / pad * pad;
// DEBUG:
//WriteStatus("New Length: " + len);
//WriteStatus("Dif: " + (len - src.Length));
Array.Resize(ref src, len); Array.Resize(ref src, len);
return src; return src;
} }
@ -2949,9 +2920,6 @@ namespace NUS_Downloader
resultArray[i] = 0x00; resultArray[i] = 0x00;
} }
} }
// DEBUG
//WriteStatus(" - Int->Byte[]: " + DisplayBytes(resultArray, " "));
return resultArray; return resultArray;
} }
@ -2989,6 +2957,8 @@ namespace NUS_Downloader
// Content is decrypted/new to the title... // Content is decrypted/new to the title...
string filename = contentsEdit.Items[contentsEdit.SelectedIndex].ToString().Substring(contentsEdit.Items[contentsEdit.SelectedIndex].ToString().IndexOf("] [") + 3, 12); string filename = contentsEdit.Items[contentsEdit.SelectedIndex].ToString().Substring(contentsEdit.Items[contentsEdit.SelectedIndex].ToString().IndexOf("] [") + 3, 12);
byte[] contentbt = FileLocationToByteArray(fileinfo[0] + filename); byte[] contentbt = FileLocationToByteArray(fileinfo[0] + filename);
byte[] newvalues = new byte[4];
newvalues[1] = 0x00;
int[] oldresults = ByteArrayContainsByteArray(contentbt, old_hash_check); int[] oldresults = ByteArrayContainsByteArray(contentbt, old_hash_check);
int[] newresults = ByteArrayContainsByteArray(contentbt, new_hash_check); int[] newresults = ByteArrayContainsByteArray(contentbt, new_hash_check);
@ -2998,23 +2968,24 @@ namespace NUS_Downloader
WriteStatus(String.Format(" - {0} Old-school ES Signing Fix(es) Found...", oldresults[0])); WriteStatus(String.Format(" - {0} Old-school ES Signing Fix(es) Found...", oldresults[0]));
for (int s = 1; s < oldresults.Length - 1; s++) for (int s = 1; s < oldresults.Length - 1; s++)
{ {
PatchTrucha(contentbt, oldresults[s]); contentbt = PatchBinary(contentbt, oldresults[s], newvalues);
WriteStatus(String.Format(" - Bug restored at 0x{0}", int.Parse(oldresults[s].ToString(), System.Globalization.NumberStyles.HexNumber))); WriteStatus(String.Format(" - Bug restored at 0x{0}", int.Parse(oldresults[s].ToString(), System.Globalization.NumberStyles.HexNumber)));
} }
} }
// TODO THIS DOESN't WORK I THINK REWRITE THE FILE>>>
if (newresults[0] != 0) if (newresults[0] != 0)
{ {
WriteStatus(String.Format(" - {0} New-school ES Signing Fix(es) Found...", newresults[0])); WriteStatus(String.Format(" - {0} New-school ES Signing Fix(es) Found...", newresults[0]));
for (int s = 1; s < newresults.Length - 1; s++) for (int s = 1; s < newresults.Length - 1; s++)
{ {
PatchTrucha(contentbt, newresults[s]); contentbt = PatchBinary(contentbt, newresults[s], newvalues);
WriteStatus(String.Format(" + Bug restored at 0x{0}.", int.Parse(newresults[s].ToString(), System.Globalization.NumberStyles.HexNumber))); WriteStatus(String.Format(" + Bug restored at 0x{0}.", int.Parse(newresults[s].ToString(), System.Globalization.NumberStyles.HexNumber)));
} }
} }
} }
else else
{ {
WriteStatus(" - The file you selected was encrypted, attempting to decrypt and patch...");
string filename = contentsEdit.Items[contentsEdit.SelectedIndex].ToString().Substring(contentsEdit.Items[contentsEdit.SelectedIndex].ToString().IndexOf("] [") + 3, 8); string filename = contentsEdit.Items[contentsEdit.SelectedIndex].ToString().Substring(contentsEdit.Items[contentsEdit.SelectedIndex].ToString().IndexOf("] [") + 3, 8);
byte[] ticket = FileLocationToByteArray(fileinfo[0] + "cetk"); byte[] ticket = FileLocationToByteArray(fileinfo[0] + "cetk");
@ -3049,9 +3020,9 @@ namespace NUS_Downloader
string[] tmdsizes = GetContentSizes(tmd, ContentCount(tmd)); string[] tmdsizes = GetContentSizes(tmd, ContentCount(tmd));
iv = new byte[16]; iv = new byte[16];
for (int e = 0; e < 16; e++) for (int f = 0; f < 16; f++)
{ {
iv[e] = 0x00; iv[f] = 0x00;
} }
byte[] hash = new byte[20]; byte[] hash = new byte[20];
@ -3069,62 +3040,66 @@ namespace NUS_Downloader
} }
} }
initCrypt(iv, commonkey); initCrypt(iv, commonkey);
//DEBUG
WriteStatus(DisplayBytes(iv, " "));
byte[] decContent = Decrypt(FileLocationToByteArray(fileinfo[0] + filename)); byte[] decContent = Decrypt(FileLocationToByteArray(fileinfo[0] + filename));
Array.Resize(ref decContent, int.Parse(tmdsizes[thiscontentidx], System.Globalization.NumberStyles.HexNumber)); Array.Resize(ref decContent, int.Parse(tmdsizes[14], System.Globalization.NumberStyles.HexNumber));
if ((Convert.ToBase64String(ComputeSHA(decContent))) == Convert.ToBase64String(hash)) if ((Convert.ToBase64String(ComputeSHA(decContent))) == Convert.ToBase64String(hash))
{ {
WriteStatus(" - Hash Check: Content is Unchanged..."); WriteStatus(" - Hash Check: Content is Unchanged...");
contents[c].SHAHash = hash;
//WriteStatus("HASH: " + DisplayBytes(hash, ""));
} }
else else
{ {
WriteStatus(" - Hash Check: Content changed (did you add an encrypted file from another title?)..."); WriteStatus(" - Hash Check: Content changed (did you add an encrypted file from another title?)...");
contents[c].SHAHash = ComputeSHA(decContent); WriteStatus(" - Content Hash: " + DisplayBytes(ComputeSHA(decContent), ""));
WriteStatus(" - TMD Hash: " + DisplayBytes(hash, ""));
} }
// TODO PATCH HERE if (File.Exists(fileinfo[0] + filename + ".app"))
// Re-encrypt
byte[] newiv = new byte[16];
for (int g = 0; g < newiv.Length; g++)
{ {
newiv[g] = 0x00; if (MessageBox.Show(fileinfo[0] + filename + ".app Exists! Delete the current file so we can move on?", "File Conflict", MessageBoxButtons.YesNo) == DialogResult.Yes)
} File.Delete(fileinfo[0] + filename + ".app");
byte[] smallix = NewIntegertoByteArray(c, 2); else
ivindex[0] = smallix[0]; return;
ivindex[1] = smallix[1];
//WriteStatus(" - Old Index: " + thiscontentidx + "; New Index: " + c);
// Pad back to 0x16 alignment
//AlignByteArray(decContent, 0x16
decContent = PadToMultipleOf(decContent, 16);
initCrypt(newiv, dtitlekey);
byte[] encContent = Encrypt(decContent);
File.Delete(fileinfo[0] + filename.Substring(0, 8));
FileStream encryptwrite = new FileStream(fileinfo[0] + filename.Substring(0, 8), FileMode.OpenOrCreate);
encryptwrite.Write(encContent, 0, encContent.Length);
encryptwrite.Close();
WriteStatus(" - Encrypted Content Again!");
// Patch content
// Probably leave decrypted
// Run an update through the TMD to update the contents
//MessageBox.Show("Currently you can only add the bug to decrypted contents. Sorry!");
} }
byte[] newvalues = new byte[4];
newvalues[1] = 0x00;
int[] oldresults = ByteArrayContainsByteArray(decContent, old_hash_check);
int[] newresults = ByteArrayContainsByteArray(decContent, new_hash_check);
if (oldresults[0] != 0)
{
WriteStatus(String.Format(" - {0} Old-school ES Signing Fix(es) Found...", oldresults[0]));
for (int s = 1; s < oldresults.Length - 1; s++)
{
decContent = PatchBinary(decContent, oldresults[s], newvalues);
WriteStatus(String.Format(" - Bug restored at 0x{0}", int.Parse(oldresults[s].ToString(), System.Globalization.NumberStyles.HexNumber)));
}
}
if (newresults[0] != 0)
{
WriteStatus(String.Format(" - {0} New-school ES Signing Fix(es) Found...", newresults[0]));
for (int s = 1; s < newresults.Length - 1; s++)
{
decContent = PatchBinary(decContent, newresults[s], newvalues);
WriteStatus(String.Format(" + Bug restored at 0x{0}.", int.Parse(newresults[s].ToString(), System.Globalization.NumberStyles.HexNumber)));
}
}
File.WriteAllBytes(fileinfo[0] + filename + ".app", decContent);
contentsEdit.Items[contentsEdit.SelectedIndex] = contentsEdit.Items[contentsEdit.SelectedIndex].ToString().Replace(filename, filename + ".app");
UpdateTMDContents();
WriteStatus("Trucha signing complete!");
}
} }
private int[] ByteArrayContainsByteArray(byte[] bigboy, byte[] littleman) private int[] ByteArrayContainsByteArray(byte[] bigboy, byte[] littleman)
@ -3150,11 +3125,12 @@ namespace NUS_Downloader
return offset; return offset;
} }
private byte[] PatchTrucha(byte[] content, int offset) private byte[] PatchBinary(byte[] content, int offset, byte[] newvalues)
{ {
for (int i = 0; i < 4; i++) for (int a = 0; a < newvalues.Length; a++)
{ {
content[offset + i] = 0; if (newvalues[a] >= 0)
content[offset + a] = newvalues[a];
} }
return content; return content;
} }