Add more validation to patcher

This commit is contained in:
jvyden 2022-06-14 03:10:33 -04:00
parent bd2fe376d1
commit 281ddc2117
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 65 additions and 27 deletions

View file

@ -1,7 +1,7 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="HttpUrlsUsage" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<inspection_tool class="HttpUrlsUsage" enabled="false" level="WEAK WARNING" enabled_by_default="false">
<option name="ignoredUrls">
<list>
<option value="http://localhost" />

View file

@ -14,6 +14,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnionPatcher.Gui.MacOS", "U
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnionPatcher.Cli", "UnionPatcher.Cli\UnionPatcher.Cli.csproj", "{848F551E-C759-4F6F-B991-A27EFAC36754}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Platforms", "Platforms", "{1036EB31-D1CB-46AD-AE39-10DBABE643FA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -80,9 +82,10 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{235B4DEC-11A0-42EF-9B8D-16529869ED1A} = {59194212-45BB-4A27-A08F-54DB6ADA26B3}
{55DFA962-3EC5-44E4-9F93-FACF3E7EEBB2} = {59194212-45BB-4A27-A08F-54DB6ADA26B3}
{5F8CDEA2-B483-40D1-B1FB-EF8EC667185A} = {59194212-45BB-4A27-A08F-54DB6ADA26B3}
{DF1F0B76-FC45-41D5-B11C-9DDC620BE0F3} = {59194212-45BB-4A27-A08F-54DB6ADA26B3}
{848F551E-C759-4F6F-B991-A27EFAC36754} = {59194212-45BB-4A27-A08F-54DB6ADA26B3}
{1036EB31-D1CB-46AD-AE39-10DBABE643FA} = {59194212-45BB-4A27-A08F-54DB6ADA26B3}
{55DFA962-3EC5-44E4-9F93-FACF3E7EEBB2} = {1036EB31-D1CB-46AD-AE39-10DBABE643FA}
{DF1F0B76-FC45-41D5-B11C-9DDC620BE0F3} = {1036EB31-D1CB-46AD-AE39-10DBABE643FA}
{5F8CDEA2-B483-40D1-B1FB-EF8EC667185A} = {1036EB31-D1CB-46AD-AE39-10DBABE643FA}
EndGlobalSection
EndGlobal

View file

@ -10,19 +10,21 @@ Wikipedia entry on ELF: https://en.wikipedia.org/wiki/Executable_and_Linkable_Fo
namespace LBPUnion.UnionPatcher {
public class ElfFile {
internal const int MinimumSize = 52;
private enum WordSize : byte {
ThirtyTwoBits = 0x01,
SixtyFourBits = 0x02
SixtyFourBits = 0x02,
}
private enum Endianness : byte {
Little = 0x01,
Big = 0x02
Big = 0x02,
}
private enum InstructionSetArchitecture : UInt16 {
PowerPC = 0x15, //64-bit PowerPC (PS3)
ARM = 0x28 //32-bit ARM (Vita)
ARM = 0x28, //32-bit ARM (Vita)
}
public string Name { get; } = "Binary Blob";
@ -35,10 +37,16 @@ namespace LBPUnion.UnionPatcher {
public byte[] Contents { get; } = null;
public ElfFile(byte[] fileContents) {
if(fileContents.Length < 52)
if(fileContents.Length < MinimumSize)
return;
IsValid = fileContents[0x00..0x04].SequenceEqual(new byte[] {0x7F, (byte)'E', (byte)'L', (byte)'F'});
IsValid = fileContents[..0x04].SequenceEqual(new byte[] {
0x7F,
(byte)'E',
(byte)'L',
(byte)'F',
});
if(!IsValid) return;
byte identClassValue = fileContents[0x04];

View file

@ -4,61 +4,80 @@ using System.Text;
namespace LBPUnion.UnionPatcher {
public static class Patcher {
private static readonly string[] ToBePatched = {
private static readonly string[] toBePatched = {
// Normal LittleBigPlanet gameserver URLs
"https://littlebigplanetps3.online.scee.com:10061/LITTLEBIGPLANETPS3_XML",
"http://littlebigplanetps3.online.scee.com:10060/LITTLEBIGPLANETPS3_XML",
// LittleBigPlanet 3-Specific URLs
// LittleBigPlanet 3 Presence URLs
"http://live.littlebigplanetps3.online.scee.com:10060/LITTLEBIGPLANETPS3_XML",
"http://presence.littlebigplanetps3.online.scee.com:10060/LITTLEBIGPLANETPS3_XML",
#region Spinoff URLs
// LittleBigPlanet PSP URLs
"http://lbppsp.online.scee.com:10060/LITTLEBIGPLANETPSP_XML",
"https://lbppsp.online.scee.com:10061/LITTLEBIGPLANETPSP_XML",
// LittleBigPlanet Vita URLs
"http://lbpvita.online.scee.com:10060/LITTLEBIGPLANETPS3_XML",
"https://lbpvita.online.scee.com:10061/LITTLEBIGPLANETPS3_XML",
#endregion
#region Beta URLS
// LittleBigPlanet 2 Beta URLs
"http://lbp2ps3-beta.online.scee.com:10060/LITTLEBIGPLANETPS3_XML",
"https://lbp2ps3-beta.online.scee.com:10061/LITTLEBIGPLANETPS3_XML",
// LittleBigPlanet (3?) Beta URLs
"http://littlebigplanetps3-beta.online.scee.com:10060/LITTLEBIGPLANETPS3_XML",
"https://littlebigplanetps3-beta.online.scee.com:10061/LITTLEBIGPLANETPS3_XML",
// LittleBigPlanet Vita URLs
"http://lbpvita.online.scee.com:10060/LITTLEBIGPLANETPS3_XML",
"https://lbpvita.online.scee.com:10061/LITTLEBIGPLANETPS3_XML",
// LittleBigPlanet Vita Beta URLs
"http://lbpvita-beta.online.scee.com:10060/LITTLEBIGPLANETPS3_XML",
"https://lbpvita-beta.online.scee.com:10061/LITTLEBIGPLANETPS3_XML",
#endregion
};
public static void PatchFile(string fileName, Uri serverUrl, string outputFileName) {
PatchFile(fileName, serverUrl.ToString(), outputFileName);
}
public static void PatchFile(string fileName, string serverUrl, string outputFileName) {
File.WriteAllBytes(outputFileName, PatchData(File.ReadAllBytes(fileName), serverUrl));
}
public static byte[] PatchData(byte[] data, Uri serverUrl) {
return PatchData(data, serverUrl.ToString());
}
public static byte[] PatchData(byte[] data, string serverUrl) {
Console.WriteLine("NOTICE: As Union Patcher is still a work in progress, the patching method is not refined. " +
"Due to this, it may show that URLS are not found. These can be safely ignored." +
"Please check the EBOOT manually in a hex editor to make sure your specified url has been" +
"added properly. Thank you");
#region Validation
if(serverUrl.EndsWith('/')) {
throw new ArgumentException("URL must not contain a trailing slash!");
}
// Attempt to create URI to see if it's valid
if(!Uri.TryCreate(serverUrl, UriKind.RelativeOrAbsolute, out _)) {
throw new Exception("URL must be valid.");
}
if(serverUrl.Length > data.Length) {
throw new ArgumentException("URL cannot be bigger than the file to patch.");
}
#endregion
string dataAsString = Encoding.ASCII.GetString(data);
using MemoryStream ms = new(data);
using BinaryWriter writer = new(ms);
// using writer.Write(string) writes the length as a byte beforehand which is problematic because
// LBP uses null-terminated strings, not length-defined strings
// Using writer.Write(string) writes the length as a byte beforehand
// This is problematic because LBP (being written in C/C++) uses null-terminated strings,
// as opposed to length-text written strings as C# likes to serialize.
byte[] serverUrlAsBytes = Encoding.ASCII.GetBytes(serverUrl);
foreach(string url in ToBePatched) {
bool wroteUrl = false;
foreach(string url in toBePatched) {
if(serverUrl.Length > url.Length) {
throw new ArgumentOutOfRangeException(nameof(serverUrl), $"Server URL ({serverUrl.Length} characters long) is above maximum length {url.Length}");
}
int offset = dataAsString.IndexOf(url, StringComparison.Ordinal);
if(offset < 1) {
Console.WriteLine($"WARNING: URL {url} not found!");
continue;
}
if(offset < 1) continue;
writer.BaseStream.Position = offset;
for(int i = 0; i < url.Length; i++) {
@ -67,6 +86,14 @@ namespace LBPUnion.UnionPatcher {
writer.BaseStream.Position = offset; // Reset position to beginning
writer.Write(serverUrlAsBytes);
wroteUrl = true;
}
if(!wroteUrl) {
throw new Exception("No patchable URLs were detected in the " +
"provided file. Please make sure you are patching " +
"the correct file.");
}
writer.Flush();