mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-16 12:57:44 +01:00
very useful message /s
This commit is contained in:
parent
1775122d03
commit
349ba01572
3 changed files with 61 additions and 68 deletions
|
|
@ -53,15 +53,11 @@ namespace Dalamud.Bootstrap
|
||||||
|
|
||||||
// Acquire the process handle and read the command line
|
// Acquire the process handle and read the command line
|
||||||
using var process = Process.Open(pid);
|
using var process = Process.Open(pid);
|
||||||
var commandLine = process.ReadCommandLine();
|
|
||||||
|
|
||||||
// Recover the key
|
|
||||||
|
|
||||||
// TODO: check if contains arg[1]
|
|
||||||
|
|
||||||
if EncryptedArgument.Extract(commandLine[1], )
|
|
||||||
|
|
||||||
|
var argument = ReadGameArgument(process);
|
||||||
|
|
||||||
|
argument.Remove("T");
|
||||||
|
//var newCommandLine =
|
||||||
// TODO:
|
// TODO:
|
||||||
// .... if arg1 exists
|
// .... if arg1 exists
|
||||||
// DecodeSqexArg(arguments[1]);
|
// DecodeSqexArg(arguments[1]);
|
||||||
|
|
@ -89,6 +85,22 @@ namespace Dalamud.Bootstrap
|
||||||
return createdTick & 0xFFFF_0000;
|
return createdTick & 0xFFFF_0000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ArgumentBuilder ReadGameArgument(Process process)
|
||||||
|
{
|
||||||
|
var arguments = process.ReadArguments();
|
||||||
|
|
||||||
|
if (arguments.Length < 1)
|
||||||
|
{
|
||||||
|
throw new BootstrapException($"Process id {process.Id} does not have any arguments to parse.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (EncryptedArgument.Extract(arguments[0], out var encryptedPayload, out var _))
|
||||||
|
{
|
||||||
|
var key = RecoverKey(process);
|
||||||
|
EncryptedArgument.Decry(encryptedPayload, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Injects Dalamud into the process. See remarks for process state prerequisites.
|
/// Injects Dalamud into the process. See remarks for process state prerequisites.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ using Dalamud.Bootstrap.Crypto;
|
||||||
|
|
||||||
namespace Dalamud.Bootstrap.SqexArg
|
namespace Dalamud.Bootstrap.SqexArg
|
||||||
{
|
{
|
||||||
internal sealed class EncryptedArgument : IDisposable
|
internal sealed class EncryptedArgument
|
||||||
{
|
{
|
||||||
private static char[] ChecksumTable = new char[]
|
private static char[] ChecksumTable = new char[]
|
||||||
{
|
{
|
||||||
|
|
@ -21,45 +21,26 @@ namespace Dalamud.Bootstrap.SqexArg
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private const char NoChecksumMarker = '!';
|
private const char NoChecksumMarker = '!';
|
||||||
|
|
||||||
/// <summary>
|
private IMemoryOwner<byte> m_encryptedData;
|
||||||
/// A data that is not encrypted.
|
|
||||||
/// </summary>
|
|
||||||
private IMemoryOwner<byte> m_data;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an object that can take (e.g. /T=1234)
|
/// Encrypts the argument with given key.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">A data that is not encrypted.</param>
|
/// <param name="argument"></param>
|
||||||
/// <remarks>
|
/// <param name="key"></param>
|
||||||
/// This takes the ownership of the data.
|
public EncryptedArgument(string argument, uint key)
|
||||||
/// </remarks>
|
|
||||||
public EncryptedArgument(IMemoryOwner<byte> data)
|
|
||||||
{
|
{
|
||||||
m_data = data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public EncryptedArgument(string argument)
|
|
||||||
{
|
|
||||||
var buffer = MemoryPool<byte>.Shared.Rent(Encoding.UTF8.GetByteCount(argument));
|
|
||||||
Encoding.UTF8.GetBytes(argument, buffer.Memory.Span);
|
|
||||||
|
|
||||||
m_data = buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
m_data?.Dispose();
|
|
||||||
m_data = null!;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extracts the payload and checksum from the encrypted argument.
|
/// Extracts the payload and checksum from the encrypted argument.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="argument"></param>
|
/// <param name="argument"></param>
|
||||||
/// <param name="payload">An encrypted payload encoded in url-safe base64 string extracted. The value is undefined if the function fails.</param>
|
/// <param name="payload">An encrypted payload extracted. The value is undefined if the function fails.</param>
|
||||||
/// <param name="checksum">A checksum of the key extracted. The value is undefined if the function fails.</param>
|
/// <param name="checksum">A checksum of the key extracted. The value is undefined if the function fails.</param>
|
||||||
/// <returns>Returns true on success, false otherwise.</returns>
|
/// <returns>Returns true on success, false otherwise.</returns>
|
||||||
public static bool Extract(string argument, out string payload, out char checksum)
|
public static bool Extract(string argument, out byte[] payload, out char checksum)
|
||||||
{
|
{
|
||||||
// must start with //**sqex0003, some characters, one checksum character and end with **//
|
// must start with //**sqex0003, some characters, one checksum character and end with **//
|
||||||
var regex = new Regex(@"^\/\/\*\*sqex0003(?<payload>.+)(?<checksum>.)\*\*\/\/$");
|
var regex = new Regex(@"^\/\/\*\*sqex0003(?<payload>.+)(?<checksum>.)\*\*\/\/$");
|
||||||
|
|
@ -67,45 +48,31 @@ namespace Dalamud.Bootstrap.SqexArg
|
||||||
var match = regex.Match(argument);
|
var match = regex.Match(argument);
|
||||||
if (!match.Success)
|
if (!match.Success)
|
||||||
{
|
{
|
||||||
payload = "";
|
payload = null!;
|
||||||
checksum = '\0';
|
checksum = '\0';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
payload = match.Groups["payload"].Value;
|
// Extract checksum
|
||||||
checksum = match.Groups["checksum"].Value[0];
|
checksum = match.Groups["checksum"].Value[0];
|
||||||
|
|
||||||
|
// Extract payload
|
||||||
|
var payloadStr = match.Groups["payload"].Value;
|
||||||
|
payload = DecodeUrlSafeBase64(payloadStr);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static EncryptedArgument FromEncryptedData(string argument, uint key)
|
public override string ToString()
|
||||||
{
|
{
|
||||||
// Create the key
|
var checksum = GetChecksumFromKey();
|
||||||
Span<byte> keyBytes = stackalloc byte[8];
|
}
|
||||||
CreateKey(key, keyBytes);
|
|
||||||
|
|
||||||
if (!Extract(argument, out var encryptedStr, out var _))
|
private static char GetChecksumFromKey(uint key)
|
||||||
{
|
{
|
||||||
throw new SqexArgException($"Could not extract the argument and checksum from {argument}");
|
var index = (key & 0x000F_0000) >> 16;
|
||||||
}
|
|
||||||
|
|
||||||
var encryptedData = DecodeUrlSafeBase64(encryptedStr);
|
return ChecksumTable[index];
|
||||||
|
|
||||||
// Allocate the buffer to store decrypted data
|
|
||||||
var decryptedData = CreateAlignedBuffer(encryptedData.Length);
|
|
||||||
|
|
||||||
// Decrypt the data with the key
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var blowfish = new Blowfish(keyBytes);
|
|
||||||
blowfish.Decrypt(encryptedData, decryptedData.Memory.Span);
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
decryptedData?.Dispose(); // TODO: clean up this thing?
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new EncryptedArgument(decryptedData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -147,11 +114,24 @@ namespace Dalamud.Bootstrap.SqexArg
|
||||||
.Replace('-', '+')
|
.Replace('-', '+')
|
||||||
.Replace('_', '/');
|
.Replace('_', '/');
|
||||||
|
|
||||||
return Convert.FromBase64String(base64Str);
|
try
|
||||||
|
{
|
||||||
|
return Convert.FromBase64String(base64Str);
|
||||||
|
}
|
||||||
|
catch (FormatException ex)
|
||||||
|
{
|
||||||
|
// This is expected to happen if the argument is ill-formed
|
||||||
|
throw new SqexArgException($"A payload {payload} does not look like a valid encrypted argument.", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Encrypt(uint key)
|
private static string EncodeUrlSafeBase64(byte[] payload)
|
||||||
{
|
{
|
||||||
|
var payloadStr = Convert.ToBase64String(payload);
|
||||||
|
|
||||||
|
return payloadStr
|
||||||
|
.Replace('+', '-')
|
||||||
|
.Replace('/', '_');
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -140,7 +140,7 @@ namespace Dalamud.Bootstrap
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string[] ReadCommandLine()
|
public string[] ReadArguments()
|
||||||
{
|
{
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
|
|
@ -151,7 +151,8 @@ namespace Dalamud.Bootstrap
|
||||||
|
|
||||||
// Read the command line (which is utf16-like string)
|
// Read the command line (which is utf16-like string)
|
||||||
var commandLine = ReadMemoryExact(procParam.CommandLine.Buffer, procParam.CommandLine.Length);
|
var commandLine = ReadMemoryExact(procParam.CommandLine.Buffer, procParam.CommandLine.Length);
|
||||||
return ParseCommandLine(commandLine);
|
|
||||||
|
return ParseCommandLineToArguments(commandLine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -173,7 +174,7 @@ namespace Dalamud.Bootstrap
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string[] ParseCommandLine(ReadOnlySpan<byte> commandLine)
|
private string[] ParseCommandLineToArguments(ReadOnlySpan<byte> commandLine)
|
||||||
{
|
{
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue