Skip to content

Commit

Permalink
Fixed crash if NuGet Security.Principal was missing
Browse files Browse the repository at this point in the history
Added warning if not using correct build version for game.
  • Loading branch information
erfg12 committed Oct 10, 2020
1 parent d5a5040 commit 10571a5
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 125 deletions.
2 changes: 1 addition & 1 deletion Memory/Memory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Description>Read and Write to process memory. Make PC cheat trainers easily!</Description>
<PackageIcon>icon.png</PackageIcon>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.2.20</Version>
<Version>1.2.21</Version>
<Platforms>x64;x86</Platforms>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
Expand Down
85 changes: 47 additions & 38 deletions Memory/memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Reflection;
using System.ComponentModel;

namespace Memory
{
Expand Down Expand Up @@ -136,6 +137,14 @@ static extern bool VirtualFreeEx(
uint dwFreeType
);

[DllImport("psapi.dll")]
static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In][MarshalAs(UnmanagedType.U4)] int nSize);
[DllImport("psapi.dll", SetLastError = true)]
public static extern bool EnumProcessModules(IntPtr hProcess,
[Out] IntPtr lphModule,
uint cb,
[MarshalAs(UnmanagedType.U4)] out uint lpcbNeeded);

[DllImport("kernel32.dll")]
private static extern bool ReadProcessMemory(IntPtr hProcess, UIntPtr lpBaseAddress, [Out] byte[] lpBuffer, UIntPtr nSize, IntPtr lpNumberOfBytesRead);

Expand Down Expand Up @@ -395,10 +404,10 @@ public void UnfreezeValue(string address)
/// <returns></returns>
public bool OpenProcess(int pid)
{
if (!IsAdmin())
/*if (!IsAdmin())
{
Debug.WriteLine("WARNING: You are NOT running this program as admin! Visit https://github.com/erfg12/memory.dll/wiki/Administrative-Privileges");
}
Debug.WriteLine("WARNING: This program may not be running with raised privileges! Visit https://github.com/erfg12/memory.dll/wiki/Administrative-Privileges");
}*/

if (pid <= 0)
{
Expand All @@ -421,7 +430,12 @@ public bool OpenProcess(int pid)
}

pHandle = OpenProcess(0x1F0FFF, true, pid);
Process.EnterDebugMode();

try {
Process.EnterDebugMode();
} catch (Win32Exception) {
Debug.WriteLine("WARNING: You are not running with raised privileges! Visit https://github.com/erfg12/memory.dll/wiki/Administrative-Privileges");
}

if (pHandle == IntPtr.Zero)
{
Expand All @@ -432,18 +446,19 @@ public bool OpenProcess(int pid)
return false;
}

// Lets set the process to 64bit or not here (cuts down on api calls)
Is64Bit = Environment.Is64BitOperatingSystem && (IsWow64Process(pHandle, out bool retVal) && !retVal);

mainModule = theProc.MainModule;

GetModules();

// Lets set the process to 64bit or not here (cuts down on api calls)
Is64Bit = Environment.Is64BitOperatingSystem && (IsWow64Process(pHandle, out bool retVal) && !retVal);
Debug.WriteLine("Program is operating at Administrative level. Process #" + theProc + " is open and modules are stored.");
Debug.WriteLine("Process #" + theProc + " is now open.");

return true;
}
catch {
Debug.WriteLine("ERROR: OpenProcess has crashed. Are you trying to hack a x64 game? https://github.com/erfg12/memory.dll/wiki/64bit-Games");
catch (Exception ex) {
Debug.WriteLine("ERROR: OpenProcess has crashed. " + ex);
return false;
}
}
Expand All @@ -465,10 +480,18 @@ public bool OpenProcess(string proc)
/// <returns></returns>
public bool IsAdmin()
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
try
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
catch
{
Debug.WriteLine("ERROR: Could not determin if program is running as admin. Is the NuGet package \"System.Security.Principal.Windows\" missing?");
return false;
}
}

Expand All @@ -491,38 +514,24 @@ public void GetModules()
if (theProc == null)
return;

modules.Clear();
foreach (ProcessModule Module in theProc.Modules)
if (_is64Bit && IntPtr.Size != 8)
{
//if (!string.IsNullOrEmpty(Module.ModuleName) && !modules.ContainsKey(Module.ModuleName))
modules.Add(Module.ModuleName, Module.BaseAddress);
Debug.WriteLine("WARNING: Game is x64, but your Trainer is x86! You will be missing some modules, change your Trainer's Solution Platform.");
}

/*IntPtr handleToSnapshot = IntPtr.Zero;
try
else if (!_is64Bit && IntPtr.Size == 8)
{
handleToSnapshot = CreateToolhelp32Snapshot((uint)SnapshotFlags.Module, (uint)theProc.Id);
MODULEENTRY32 moduleEntry = new MODULEENTRY32();
if (Module32First(handleToSnapshot, ref moduleEntry))
{
do
{
modules.Add(moduleEntry.szModule, moduleEntry.modBaseAddr);
} while (Module32Next(handleToSnapshot, ref moduleEntry));
}
else
{
Debug.WriteLine(string.Format("Failed with win32 error code {0}", Marshal.GetLastWin32Error()));
}
Debug.WriteLine("WARNING: Game is x86, but your Trainer is x64! You will be missing some modules, change your Trainer's Solution Platform.");
}
catch (Exception ex)

modules.Clear();

foreach (ProcessModule Module in theProc.Modules)
{
Debug.WriteLine(string.Format("Can't get the process. {0}", ex));
//if (!string.IsNullOrEmpty(Module.ModuleName) && !modules.ContainsKey(Module.ModuleName))
modules.Add(Module.ModuleName, Module.BaseAddress);
}
finally
{
CloseHandle(handleToSnapshot);
}*/

Debug.WriteLine("Found " + modules.Count() + " process modules.");
}

public void SetFocus()
Expand Down
Loading

0 comments on commit 10571a5

Please sign in to comment.