Skip to content

Commit

Permalink
Test2
Browse files Browse the repository at this point in the history
  • Loading branch information
CatmanFan committed Apr 14, 2024
1 parent bf345ca commit f3571d7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 83 deletions.
4 changes: 2 additions & 2 deletions FriishProduce/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<section name="FriishProduce.Settings.PCE" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="FriishProduce.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<!-- for information on using this configuration section, please refer to http://therealmagicmike.github.io/PBKDF2.NET/ --><section name="pbkdf2" type="System.Configuration.PBKDF2Section, PBKDF2.NET, Version=2.0.0.0" requirePermission="false" /></configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
Expand Down Expand Up @@ -238,4 +238,4 @@
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
<!-- for information on using this configuration section, please refer to http://therealmagicmike.github.io/PBKDF2.NET/ --><pbkdf2 hashName="HMACSHA256" iterationCount="1000" saltSize="8"></pbkdf2></configuration>
3 changes: 3 additions & 0 deletions FriishProduce/FriishProduce.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
<Reference Include="Ookii.Dialogs.WinForms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=66aa232afad40158, processorArchitecture=MSIL">
<HintPath>..\packages\Ookii.Dialogs.WinForms.4.0.0\lib\net462\Ookii.Dialogs.WinForms.dll</HintPath>
</Reference>
<Reference Include="PBKDF2.NET, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\PBKDF2.NET.2.0.0\lib\net45\PBKDF2.NET.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
Expand Down
132 changes: 51 additions & 81 deletions FriishProduce/_classes/Program/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace FriishProduce
{
public class Database
{
protected class TitleKey
protected class NUSD
{
private static string Secret(int start, int len)
{
Expand All @@ -33,22 +33,58 @@ private static string Secret(int start, int len)
return ret;
}

private static byte[] MungetId(string tid)
public static WAD GetWAD(string tid, string password, CommonKeyType cKeyType)
{
while (tid.StartsWith("00"))
tid = tid.Substring(2);
if (tid == "")
tid = "00";
return Byte.FromHex(tid);
}
WAD w = null;

private static byte[] Derive(string tid, string pass)
{
string SECRET = Secret(-3, 10);
return new Rfc2898DeriveBytes(Encoding.UTF8.GetBytes(pass), MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(SECRET + MungetId(tid))), 20, HashAlgorithmName.SHA1).GetBytes(32);
}
using (NusClient nus = new NusClient())
{
using (TMD tmd = nus.DownloadTMD(tid, ""))
{
Ticket tik = Ticket.Load(Byte.FromHex("0001000163C7CA3E6D51EF91B637251DF8A1887B12F222400D5DE7F4740343A0069EE091B0442B7E69C52259624D6471ABB421AC1884DF4D9BC516CC6940AA5BED4A653D196880D6E93F16BF3AA818DAF0EAD11D845B072C8AF5130858D08CDD26CDE82813170C18B09EF309CAD298C94B5E17F50E7E9AC9A858D7578E931806CB187EEA9C847D4B417B431C2A4A8D03EB218410D0E9622A3D46CFCD05C56F7113AA392A009034801B4F240F487F4FBF22F9A0CBDAFEF04E3293E4FE616C6BCA7659AE681F57BF7F32ACC5EDC123BA58BD67425EDC1B60B5766A4175A63CCED67C1DB6B1449532F075440C6E6B0DA338C797917AAAD8801BFC3339F09E4DCC2C0F5D8E3D000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000526F6F742D434130303030303030312D58533030303030303033000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041414141414141414141414141414141000000000000000000000000005959595959595959FFFF393900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100017D9D5EBA5281DCA7065D2F0868DB8AC73ACE7EA991F1969FE1D0F2C11FAEC0C3F01ADCB446ADE5CA03B625219462C6E1410DB9E63FDE98D1AF263B4CB28784278272EF27134B87C258D67B62F2B5BF9CB6BA8C89192EC50689AC7424A022094003EE98A4BD2F013B593FE5666CD5EB5AD7A49310F34EFBB43D46CBF1B523CF82F68EB56DB904A7C2A82BE11D78D39BA20D90D30742DB5E7AC1EFF221510962CFA914A880DCF417BA99930AEE08B0B0E51A3E9FAFCDC2D7E3CBA12F3AC00790DE447AC3C538A8679238078BD4C4B245AC2916886D2A0E594EED5CC835698B4D6238DF05724DCCF681808A7074065930BFF8514137E815FABAA172B8E0696C61E4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000526F6F742D43413030303030303031000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000158533030303030303033000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000F1B89FD1AD07A9378A7B100C7DC739BE9EDDB7320089AB25B1F871AF5AA9F4589ED18302328E811A1FEFD009C8063643F854B9E13BBB613A7ACF8714856BA45BAAE7BBC64EB2F75D87EBF267ED0FA441A933665E577D5ADEABFB462E7600CA9CE94DC4CB983992AB7A2FB3A39EA2BF9C53ECD0DCFA6B8B5EB2CBA40FFA4075F8F2B2DE973811872DF5E2A6C38B2FDC8E57DDBD5F46EB27D61952F6AEF862B7EE9AC682A2B19AA9B558FBEBB3892FBD50C9F5DC4A6E9C9BFE458034A942182DDEB75FE0D1B3DF0E97E39980877018C2B283F135757C5A30FC3F3084A49AAAC01EE706694F8E1448DA123ACC4FFA26AA38F7EFBF278F369779775DB7C5ADC78991DCF8438D000100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000B3ADB3226B3C3DFF1B4B407716FF4F7AD76486C895AC562D21F10601D4F66428191C07768FDF1AE2CE7B27C90FBC0AD0312578EC0779B657D4372413A7F86F0C14C0EF6E0941ED2B05EC3957360789004A878D2E9DF8C7A5A9F8CAB311B1187957BBF898E2A25402CF5439CF2BBFA0E1F85C066E839AE094CA47E01558F56E6F34E92AA2DC38937E37CD8C5C4DFD2F114FE868C9A8D9FED86E0C2175A2BD7E89B9C7B513F41A7961443910EFF9D7FE572218D56DFB7F497AA4CB90D4F1AEB176E4685DA7944060982F0448401FCFC6BAEBDA1630B473B415233508070A9F4F8978E62CEC5E9246A5A8BDA0857868750C3A112FAF95E838C8990E87B162CD10DAB3319665EF889B541BB336BB67539FAFC2AE2D0A2E75C02374EA4EAC8D99507F59B95377305F2635C608A99093AC8FC6DE23B97AEA70B4C4CF66B30E58320EC5B6720448CE3BB11C531FCB70287CB5C27C674FBBFD8C7FC94220A473231D587E5A1A1A82E37579A1BB826ECE0171C97563474B1D46E679B282376211CDC7002F4687C23C6DC0D5B5786EE1F273FF0192500FF4C7506AEE72B6F43DF608FEA583A1F9860F87AF524454BB47C3060C94E99BF7D632A7C8AB4B4FF535211FC18047BB7AFA5A2BD7B884AD8E564F5B89FF379737F1F5013B1F9EC4186F922AD5C4B3C0D5870B9C04AF1AB5F3BC6D0AF17D4708E443E973F7B7707754BAF3ECD2AC49000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000526F6F7400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001434130303030303030310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005BFA7D5CB279C9E2EEE121C6EAF44FF639F88F078B4B77ED9F9560B0358281B50E55AB721115A177703C7A30FE3AE9EF1C60BC1D974676B23A68CC04B198525BC968F11DE2DB50E4D9E7F071E562DAE2092233E9D363F61DD7C19FF3A4A91E8F6553D471DD7B84B9F1B8CE7335F0F5540563A1EAB83963E09BE901011F99546361287020E9CC0DAB487F140D6626A1836D27111F2068DE4772149151CF69C61BA60EF9D949A0F71F5499F2D39AD28C7005348293C431FFBD33F6BCA60DC7195EA2BCC56D200BAF6D06D09C41DB8DE9C720154CA4832B69C08C69CD3B073A0063602F462D338061A5EA6C915CD5623579C3EB64CE44EF586D14BAAA8834019B3EEBEED3790001000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
tik.FakeSign = tmd.FakeSign;
tik.TitleID = tmd.TitleID;
tik.NumOfDLC = 0;
tik.CommonKeyIndex = cKeyType;
var cKey = tik.CommonKeyIndex == CommonKeyType.vWii ? CommonKey.GetvWiiKey() : tik.CommonKeyIndex == CommonKeyType.Korean ? CommonKey.GetKoreanKey() : CommonKey.GetStandardKey();
var temp = tik.ToByteArray();

// Title key
// ****************
var hashedSecret = Byte.FromHex(Secret(0, 9) + tid.Substring(2));
var tKey = new PBKDF2(password, hashedSecret, 20).GetBytes(16);
tik.TitleKey = tKey;

tik.ToByteArray().Skip(0x1BF).Take(16).ToArray().CopyTo(temp, 0x1BF);

// Unknown buffer (last byte is 1 for VC titles)
// ****************
var buffer = new byte[48];
buffer[47] = 1;
buffer.CopyTo(temp, 0x1F2);

// Save ticket file
// ****************
tik = Ticket.Load(temp);

public static byte[][] DeriveAll(string tid) => new byte[][] { Derive(tid, "nintendo"), Derive(tid, "mypass") };
string path = Paths.WAD + "cetk";
if (!Directory.Exists(Path.GetDirectoryName(path))) Directory.CreateDirectory(Path.GetDirectoryName(path));
tik.Save(path);
tik.Dispose();
}

nus.UseLocalFiles = true;
nus.DownloadTitle(tid, "", Paths.WAD, new StoreType[] { StoreType.WAD });

foreach (var item in Directory.EnumerateFiles(Paths.WAD))
{
if (Path.GetExtension(item).ToLower().EndsWith("wad")) w = WAD.Load(item);
}
}

Directory.Delete(Paths.WAD, true);
return w;
}
}

public class DatabaseEntry
Expand Down Expand Up @@ -122,77 +158,11 @@ public string GetUpperID(int index)
/// </summary>
public WAD GetWAD(int index)
{
WAD w = getWAD(index, 0) ?? getWAD(index, 1);
WAD w = NUSD.GetWAD(GetID(index, true), "mypass", Regions[index] >= 6 ? CommonKeyType.Korean : CommonKeyType.Standard);
if (w == null) w = new WAD();

return w;
}

private WAD getWAD(int index, int password)
{
WAD w = null;

using (NusClient nus = new NusClient())
{
var template = Encoding.ASCII.GetBytes("0001000163C7CA3E6D51EF91B637251DF8A1887B12F222400D5DE7F4740343A0069EE091B0442B7E69C52259624D6471ABB421AC1884DF4D9BC516CC6940AA5BED4A653D196880D6E93F16BF3AA818DAF0EAD11D845B072C8AF5130858D08CDD26CDE82813170C18B09EF309CAD298C94B5E17F50E7E9AC9A858D7578E931806CB187EEA9C847D4B417B431C2A4A8D03EB218410D0E9622A3D46CFCD05C56F7113AA392A009034801B4F240F487F4FBF22F9A0CBDAFEF04E3293E4FE616C6BCA7659AE681F57BF7F32ACC5EDC123BA58BD67425EDC1B60B5766A4175A63CCED67C1DB6B1449532F075440C6E6B0DA338C797917AAAD8801BFC3339F09E4DCC2C0F5D8E3D000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000526F6F742D434130303030303030312D58533030303030303033000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041414141414141414141414141414141000000000000000000000000005959595959595959FFFF393900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100017D9D5EBA5281DCA7065D2F0868DB8AC73ACE7EA991F1969FE1D0F2C11FAEC0C3F01ADCB446ADE5CA03B625219462C6E1410DB9E63FDE98D1AF263B4CB28784278272EF27134B87C258D67B62F2B5BF9CB6BA8C89192EC50689AC7424A022094003EE98A4BD2F013B593FE5666CD5EB5AD7A49310F34EFBB43D46CBF1B523CF82F68EB56DB904A7C2A82BE11D78D39BA20D90D30742DB5E7AC1EFF221510962CFA914A880DCF417BA99930AEE08B0B0E51A3E9FAFCDC2D7E3CBA12F3AC00790DE447AC3C538A8679238078BD4C4B245AC2916886D2A0E594EED5CC835698B4D6238DF05724DCCF681808A7074065930BFF8514137E815FABAA172B8E0696C61E4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000526F6F742D43413030303030303031000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000158533030303030303033000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000F1B89FD1AD07A9378A7B100C7DC739BE9EDDB7320089AB25B1F871AF5AA9F4589ED18302328E811A1FEFD009C8063643F854B9E13BBB613A7ACF8714856BA45BAAE7BBC64EB2F75D87EBF267ED0FA441A933665E577D5ADEABFB462E7600CA9CE94DC4CB983992AB7A2FB3A39EA2BF9C53ECD0DCFA6B8B5EB2CBA40FFA4075F8F2B2DE973811872DF5E2A6C38B2FDC8E57DDBD5F46EB27D61952F6AEF862B7EE9AC682A2B19AA9B558FBEBB3892FBD50C9F5DC4A6E9C9BFE458034A942182DDEB75FE0D1B3DF0E97E39980877018C2B283F135757C5A30FC3F3084A49AAAC01EE706694F8E1448DA123ACC4FFA26AA38F7EFBF278F369779775DB7C5ADC78991DCF8438D000100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000B3ADB3226B3C3DFF1B4B407716FF4F7AD76486C895AC562D21F10601D4F66428191C07768FDF1AE2CE7B27C90FBC0AD0312578EC0779B657D4372413A7F86F0C14C0EF6E0941ED2B05EC3957360789004A878D2E9DF8C7A5A9F8CAB311B1187957BBF898E2A25402CF5439CF2BBFA0E1F85C066E839AE094CA47E01558F56E6F34E92AA2DC38937E37CD8C5C4DFD2F114FE868C9A8D9FED86E0C2175A2BD7E89B9C7B513F41A7961443910EFF9D7FE572218D56DFB7F497AA4CB90D4F1AEB176E4685DA7944060982F0448401FCFC6BAEBDA1630B473B415233508070A9F4F8978E62CEC5E9246A5A8BDA0857868750C3A112FAF95E838C8990E87B162CD10DAB3319665EF889B541BB336BB67539FAFC2AE2D0A2E75C02374EA4EAC8D99507F59B95377305F2635C608A99093AC8FC6DE23B97AEA70B4C4CF66B30E58320EC5B6720448CE3BB11C531FCB70287CB5C27C674FBBFD8C7FC94220A473231D587E5A1A1A82E37579A1BB826ECE0171C97563474B1D46E679B282376211CDC7002F4687C23C6DC0D5B5786EE1F273FF0192500FF4C7506AEE72B6F43DF608FEA583A1F9860F87AF524454BB47C3060C94E99BF7D632A7C8AB4B4FF535211FC18047BB7AFA5A2BD7B884AD8E564F5B89FF379737F1F5013B1F9EC4186F922AD5C4B3C0D5870B9C04AF1AB5F3BC6D0AF17D4708E443E973F7B7707754BAF3ECD2AC49000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000526F6F7400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001434130303030303030310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005BFA7D5CB279C9E2EEE121C6EAF44FF639F88F078B4B77ED9F9560B0358281B50E55AB721115A177703C7A30FE3AE9EF1C60BC1D974676B23A68CC04B198525BC968F11DE2DB50E4D9E7F071E562DAE2092233E9D363F61DD7C19FF3A4A91E8F6553D471DD7B84B9F1B8CE7335F0F5540563A1EAB83963E09BE901011F99546361287020E9CC0DAB487F140D6626A1836D27111F2068DE4772149151CF69C61BA60EF9D949A0F71F5499F2D39AD28C7005348293C431FFBD33F6BCA60DC7195EA2BCC56D200BAF6D06D09C41DB8DE9C720154CA4832B69C08C69CD3B073A0063602F462D338061A5EA6C915CD5623579C3EB64CE44EF586D14BAAA8834019B3EEBEED3790001000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
using (Ticket tik = Ticket.Load(template))
using (TMD tmd = nus.DownloadTMD(GetID(index, true), ""))
{
tik.FakeSign = tmd.FakeSign;
tik.TitleID = tmd.TitleID;
tik.TicketID = Shared.Swap(tik.TitleID);
tik.CommonKeyIndex = Regions[index] >= 6 ? CommonKeyType.Korean : CommonKeyType.Standard;

#region EncryptTitleKey
var cKey = tik.CommonKeyIndex == CommonKeyType.vWii ? CommonKey.GetvWiiKey() : tik.CommonKeyIndex == CommonKeyType.Korean ? CommonKey.GetKoreanKey() : CommonKey.GetStandardKey();
var temp = tik.ToByteArray();

byte[] iv = BitConverter.GetBytes(tik.TitleID);
Array.Resize(ref iv, 16);

using (RijndaelManaged rm = new RijndaelManaged())
{
rm.Key = cKey;
rm.IV = iv;
rm.KeySize = rm.BlockSize = 128;
rm.Mode = CipherMode.CBC;
rm.Padding = PaddingMode.None;

using (ICryptoTransform encryptor = rm.CreateEncryptor())
using (MemoryStream ms = new MemoryStream(TitleKey.DeriveAll(GetID(index, true))[password]))
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Read))
{
byte[] eKey = new byte[16];
cs.Read(eKey, 0, eKey.Length);
eKey.CopyTo(temp, 0x1BF);
rm.Clear();
}
}

var buffer = new byte[48];
buffer[47] = 1;
buffer.CopyTo(temp, 0x1F2);
tik.LoadFile(temp);
#endregion

string path = Paths.WAD + "cetk";
if (!Directory.Exists(Path.GetDirectoryName(path))) Directory.CreateDirectory(Path.GetDirectoryName(path));
tik.Save(path);
}

nus.UseLocalFiles = true;
nus.ContinueWithoutTicket = true;
nus.DownloadTitle(GetID(index, true), "", Paths.WAD, new StoreType[] { StoreType.WAD });

foreach (var item in Directory.EnumerateFiles(Paths.WAD))
{
if (Path.GetExtension(item).ToLower().EndsWith("wad")) w = WAD.Load(item);
}
}

Directory.Delete(Paths.WAD, true);
return w;
}
}

public List<DatabaseEntry> Entries { get; private set; }
Expand Down
1 change: 1 addition & 0 deletions FriishProduce/_classes/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static int PatternAt(byte[] source, string text)

public static byte[] FromHex(string hex)
{
hex = hex.ToUpper().Replace("-", "").Replace(" ", "");
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
Expand Down
1 change: 1 addition & 0 deletions FriishProduce/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<package id="Microsoft-WindowsAPICodePack-Core" version="1.1.5" targetFramework="net472" />
<package id="Octokit" version="9.1.2" targetFramework="net472" />
<package id="Ookii.Dialogs.WinForms" version="4.0.0" targetFramework="net472" />
<package id="PBKDF2.NET" version="2.0.0" targetFramework="net472" />
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net472" />
<package id="System.IO.Hashing" version="8.0.0" targetFramework="net472" />
Expand Down

0 comments on commit f3571d7

Please sign in to comment.