Browse Source

Added another check to the anti-piracy

4.0.0-beta
Anairkoen Schno 5 years ago
parent
commit
13c1b4ccd2
2 changed files with 134 additions and 0 deletions
  1. +71
    -0
      IPA.Injector/AntiPiracy.cs
  2. +63
    -0
      IPA.Loader/Utilities/Extensions.cs

+ 71
- 0
IPA.Injector/AntiPiracy.cs View File

@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;
using IPA.Utilities;
#if NET3
using Net3_Proxy;
@ -16,6 +18,20 @@ namespace IPA.Injector
{
var dataPlugins = Path.Combine(GameVersionEarly.ResolveDataPath(path), "Plugins");
try
{
var userDir = GetPath(new Guid("374DE290-123F-4565-9164-39C4925E467B"),
KnownFolderFlags.AliasOnly | KnownFolderFlags.DontVerify);
var userDir2 = GetPath(new Guid("7d83ee9b-2244-4e70-b1f5-5393042af1e4"),
KnownFolderFlags.AliasOnly | KnownFolderFlags.DontVerify);
var curdir = Environment.CurrentDirectory;
if (curdir.IsSubPathOf(userDir) ||
curdir.IsSubPathOf(userDir2)) return false;
}
catch { }
return
File.Exists(Path.Combine(path, "IGG-GAMES.COM.url")) ||
File.Exists(Path.Combine(path, "SmartSteamEmu.ini")) ||
@ -24,5 +40,60 @@ namespace IPA.Injector
File.Exists(Path.Combine(dataPlugins, "HUHUVR_steam_api64.dll")) ||
Directory.GetFiles(dataPlugins, "*.ini", SearchOption.TopDirectoryOnly).Length > 0;
}
private static string GetPath(Guid guid, KnownFolderFlags flags)
{
int result = SHGetKnownFolderPath(guid, (uint)flags, WindowsIdentity.GetCurrent().Token, out IntPtr outPath);
if (result >= 0)
{
string path = Marshal.PtrToStringUni(outPath);
Marshal.FreeCoTaskMem(outPath);
return path;
}
else
{
throw new ExternalException("Cannot get the known folder path. It may not be available on this system.",
result);
}
}
/// <summary>
/// Retrieves the full path of a known folder identified by the folder's known folder ID.
/// </summary>
/// <param name="rfid">A known folder ID that identifies the folder.</param>
/// <param name="dwFlags">Flags that specify special retrieval options. This value can be 0; otherwise, one or
/// more of the <see cref="KnownFolderFlags"/> values.</param>
/// <param name="hToken">An access token that represents a particular user. If this parameter is NULL, which is
/// the most common usage, the function requests the known folder for the current user. Assigning a value of -1
/// indicates the Default User. The default user profile is duplicated when any new user account is created.
/// Note that access to the Default User folders requires administrator privileges.</param>
/// <param name="ppszPath">When this method returns, contains the address of a string that specifies the path of
/// the known folder. The returned path does not include a trailing backslash.</param>
/// <returns>Returns S_OK if successful, or an error value otherwise.</returns>
/// <msdn-id>bb762188</msdn-id>
[DllImport("Shell32.dll")]
private static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)]Guid rfid, uint dwFlags,
IntPtr hToken, out IntPtr ppszPath);
/// <summary>
/// Represents the retrieval options for known folders.
/// </summary>
/// <msdn-id>dd378447</msdn-id>
[Flags]
private enum KnownFolderFlags : uint
{
None = 0x00000000,
SimpleIDList = 0x00000100,
NotParentRelative = 0x00000200,
DefaultPath = 0x00000400,
Init = 0x00000800,
NoAlias = 0x00001000,
DontUnexpand = 0x00002000,
DontVerify = 0x00004000,
Create = 0x00008000,
NoAppcontainerRedirection = 0x00010000,
AliasOnly = 0x80000000
}
}
}

+ 63
- 0
IPA.Loader/Utilities/Extensions.cs View File

@ -1,4 +1,8 @@
using System;
using System.IO;
#if NET3
using Path = Net3_Proxy.Path;
#endif
namespace IPA.Utilities
{
@ -23,5 +27,64 @@ namespace IPA.Utilities
/// <param name="self">the bool? to unwrap</param>
/// <returns>the unwrapped value, or <see langword="false"/> if it was <see langword="null"/></returns>
public static bool Unwrap(this bool? self) => self != null && self.Value;
/// <summary>
/// Returns true if <paramref name="path"/> starts with the path <paramref name="baseDirPath"/>.
/// The comparison is case-insensitive, handles / and \ slashes as folder separators and
/// only matches if the base dir folder name is matched exactly ("c:\foobar\file.txt" is not a sub path of "c:\foo").
/// </summary>
public static bool IsSubPathOf(this string path, string baseDirPath)
{
string normalizedPath = Path.GetFullPath(path.Replace('/', '\\')
.WithEnding("\\"));
string normalizedBaseDirPath = Path.GetFullPath(baseDirPath.Replace('/', '\\')
.WithEnding("\\"));
return normalizedPath.StartsWith(normalizedBaseDirPath, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Returns <paramref name="str"/> with the minimal concatenation of <paramref name="ending"/> (starting from end) that
/// results in satisfying .EndsWith(ending).
/// </summary>
/// <example>"hel".WithEnding("llo") returns "hello", which is the result of "hel" + "lo".</example>
public static string WithEnding(this string str, string ending)
{
if (str == null)
return ending;
string result = str;
// Right() is 1-indexed, so include these cases
// * Append no characters
// * Append up to N characters, where N is ending length
for (int i = 0; i <= ending.Length; i++)
{
string tmp = result + ending.Right(i);
if (tmp.EndsWith(ending))
return tmp;
}
return result;
}
/// <summary>Gets the rightmost <paramref name="length" /> characters from a string.</summary>
/// <param name="value">The string to retrieve the substring from.</param>
/// <param name="length">The number of characters to retrieve.</param>
/// <returns>The substring.</returns>
public static string Right(this string value, int length)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if (length < 0)
{
throw new ArgumentOutOfRangeException("length", length, "Length is less than zero");
}
return (length < value.Length) ? value.Substring(value.Length - length) : value;
}
}
}

Loading…
Cancel
Save