Skip to content

Commit

Permalink
Get rid of suspicious symbols
Browse files Browse the repository at this point in the history
aglab2 committed Jan 21, 2023
1 parent dedc1a7 commit 4ffe856
Showing 8 changed files with 44 additions and 261 deletions.
11 changes: 11 additions & 0 deletions MIPSInterpreter/MIPSInterpreter.csproj
Original file line number Diff line number Diff line change
@@ -33,6 +33,17 @@
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'ReleaseExternal|AnyCPU'">
<OutputPath>bin\ReleaseExternal\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
11 changes: 9 additions & 2 deletions StarManager.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
# Visual Studio Version 17
VisualStudioVersion = 17.4.33205.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarManager", "StarManager\StarManager.csproj", "{35EC9AD6-73A3-4AEA-8877-0875BA5C4699}"
EndProject
@@ -13,20 +13,27 @@ Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
ReleaseExternal|Any CPU = ReleaseExternal|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{35EC9AD6-73A3-4AEA-8877-0875BA5C4699}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35EC9AD6-73A3-4AEA-8877-0875BA5C4699}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35EC9AD6-73A3-4AEA-8877-0875BA5C4699}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35EC9AD6-73A3-4AEA-8877-0875BA5C4699}.Release|Any CPU.Build.0 = Release|Any CPU
{35EC9AD6-73A3-4AEA-8877-0875BA5C4699}.ReleaseExternal|Any CPU.ActiveCfg = ReleaseExternal|Any CPU
{35EC9AD6-73A3-4AEA-8877-0875BA5C4699}.ReleaseExternal|Any CPU.Build.0 = ReleaseExternal|Any CPU
{6A6C0223-4658-4790-82B5-28D6C5B074F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A6C0223-4658-4790-82B5-28D6C5B074F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A6C0223-4658-4790-82B5-28D6C5B074F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A6C0223-4658-4790-82B5-28D6C5B074F8}.Release|Any CPU.Build.0 = Release|Any CPU
{6A6C0223-4658-4790-82B5-28D6C5B074F8}.ReleaseExternal|Any CPU.ActiveCfg = ReleaseExternal|Any CPU
{6A6C0223-4658-4790-82B5-28D6C5B074F8}.ReleaseExternal|Any CPU.Build.0 = ReleaseExternal|Any CPU
{144EE1B2-EFFB-4014-AD15-4DC8BAE367A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{144EE1B2-EFFB-4014-AD15-4DC8BAE367A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{144EE1B2-EFFB-4014-AD15-4DC8BAE367A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{144EE1B2-EFFB-4014-AD15-4DC8BAE367A0}.Release|Any CPU.Build.0 = Release|Any CPU
{144EE1B2-EFFB-4014-AD15-4DC8BAE367A0}.ReleaseExternal|Any CPU.ActiveCfg = ReleaseExternal|Any CPU
{144EE1B2-EFFB-4014-AD15-4DC8BAE367A0}.ReleaseExternal|Any CPU.Build.0 = ReleaseExternal|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
52 changes: 0 additions & 52 deletions StarManager/Process/DeepPointer.cs
Original file line number Diff line number Diff line change
@@ -158,56 +158,4 @@ public bool DerefOffsets(Process process, out IntPtr ptr)
return true;
}
}

[StructLayout(LayoutKind.Sequential)]
public struct Vector3f
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }

public int IX { get { return (int)X; } }
public int IY { get { return (int)Y; } }
public int IZ { get { return (int)Z; } }

public Vector3f(float x, float y, float z) : this()
{
X = x;
Y = y;
Z = z;
}

public float Distance(Vector3f other)
{
float result = (X - other.X) * (X - other.X) +
(Y - other.Y) * (Y - other.Y) +
(Z - other.Z) * (Z - other.Z);
return (float)Math.Sqrt(result);
}

public float DistanceXY(Vector3f other)
{
float result = (X - other.X) * (X - other.X) +
(Y - other.Y) * (Y - other.Y);
return (float)Math.Sqrt(result);
}

public bool BitEquals(Vector3f other)
{
return X.BitEquals(other.X)
&& Y.BitEquals(other.Y)
&& Z.BitEquals(other.Z);
}

public bool BitEqualsXY(Vector3f other)
{
return X.BitEquals(other.X)
&& Y.BitEquals(other.Y);
}

public override string ToString()
{
return X + " " + Y + " " + Z;
}
}
}
176 changes: 0 additions & 176 deletions StarManager/Process/ProcessExtensions.cs
Original file line number Diff line number Diff line change
@@ -21,10 +21,6 @@ public class ProcessModuleWow64Safe
public string FileName { get; set; }
public int ModuleMemorySize { get; set; }
public string ModuleName { get; set; }
public FileVersionInfo FileVersionInfo
{
get { return FileVersionInfo.GetVersionInfo(FileName); }
}
public override string ToString()
{
return ModuleName ?? base.ToString();
@@ -104,39 +100,6 @@ public static ProcessModuleWow64Safe[] ModulesWow64Safe(this Process p)
return ret.ToArray();
}

public static IEnumerable<MemoryBasicInformation> MemoryPages(this Process process, bool all = false)
{
// hardcoded values because GetSystemInfo / GetNativeSystemInfo can't return info for remote process
var min = 0x10000L;
var max = process.Is64Bit() ? 0x00007FFFFFFEFFFFL : 0x7FFEFFFFL;

var mbiSize = (SizeT)Marshal.SizeOf(typeof(MemoryBasicInformation));

var addr = min;
do
{
MemoryBasicInformation mbi;
if (WinAPI.VirtualQueryEx(process.Handle, (IntPtr)addr, out mbi, mbiSize) == (SizeT)0)
break;
addr += (long)mbi.RegionSize;

// don't care about reserved/free pages
if (mbi.State != MemPageState.MEM_COMMIT)
continue;

// probably don't care about guarded pages
if (!all && (mbi.Protect & MemPageProtect.PAGE_GUARD) != 0)
continue;

// probably don't care about image/file maps
if (!all && mbi.Type != MemPageType.MEM_PRIVATE)
continue;

yield return mbi;

} while (addr < max);
}

public static bool Is64Bit(this Process process)
{
bool procWow64;
@@ -318,99 +281,6 @@ public static bool WriteBytes(this Process process, IntPtr addr, byte[] bytes)
return true;
}

private static bool WriteJumpOrCall(Process process, IntPtr addr, IntPtr dest, bool call)
{
var x64 = process.Is64Bit();

int jmpLen = x64 ? 12 : 5;

var instruction = new List<byte>(jmpLen);
if (x64)
{
instruction.AddRange(new byte[] { 0x48, 0xB8 }); // mov rax immediate
instruction.AddRange(BitConverter.GetBytes((long)dest));
instruction.AddRange(new byte[] { 0xFF, call ? (byte)0xD0 : (byte)0xE0 }); // jmp/call rax
}
else
{
int offset = unchecked((int)dest - (int)(addr + jmpLen));
instruction.AddRange(new byte[] { call ? (byte)0xE8 : (byte)0xE9 }); // jmp/call immediate
instruction.AddRange(BitConverter.GetBytes(offset));
}

MemPageProtect oldProtect;
process.VirtualProtect(addr, jmpLen, MemPageProtect.PAGE_EXECUTE_READWRITE, out oldProtect);
bool success = process.WriteBytes(addr, instruction.ToArray());
process.VirtualProtect(addr, jmpLen, oldProtect);

return success;
}

public static bool WriteJumpInstruction(this Process process, IntPtr addr, IntPtr dest)
{
return WriteJumpOrCall(process, addr, dest, false);
}

public static bool WriteCallInstruction(this Process process, IntPtr addr, IntPtr dest)
{
return WriteJumpOrCall(process, addr, dest, true);
}

public static IntPtr WriteDetour(this Process process, IntPtr src, int overwrittenBytes, IntPtr dest)
{
int jmpLen = process.Is64Bit() ? 12 : 5;
if (overwrittenBytes < jmpLen)
throw new ArgumentOutOfRangeException(nameof(overwrittenBytes),
$"must be >= length of jmp instruction ({jmpLen})");

// allocate memory to store the original src prologue bytes we overwrite with jump to dest
// along with the jump back to src
IntPtr gate;
if ((gate = process.AllocateMemory(jmpLen + overwrittenBytes)) == IntPtr.Zero)
throw new Win32Exception();

try
{
// read the original bytes from the prologue of src
var origSrcBytes = process.ReadBytes(src, overwrittenBytes);
if (origSrcBytes == null)
throw new Win32Exception();

// write the original prologue of src into the start of gate
if (!process.WriteBytes(gate, origSrcBytes))
throw new Win32Exception();

// write the jump from the end of the gate back to src
if (!process.WriteJumpInstruction(gate + overwrittenBytes, src + overwrittenBytes))
throw new Win32Exception();

// finally write the jump from src to dest
if (!process.WriteJumpInstruction(src, dest))
throw new Win32Exception();

// nop the leftover bytes in the src prologue
int extraBytes = overwrittenBytes - jmpLen;
if (extraBytes > 0)
{
var nops = Enumerable.Repeat((byte) 0x90, extraBytes).ToArray();
MemPageProtect oldProtect;
if (!process.VirtualProtect(src + jmpLen, nops.Length, MemPageProtect.PAGE_EXECUTE_READWRITE,
out oldProtect))
throw new Win32Exception();
if (!process.WriteBytes(src + jmpLen, nops))
throw new Win32Exception();
process.VirtualProtect(src + jmpLen, nops.Length, oldProtect);
}
}
catch
{
process.FreeMemory(gate);
throw;
}

return gate;
}

static object ResolveToType(byte[] bytes, Type type)
{
object val;
@@ -462,52 +332,6 @@ static object ResolveToType(byte[] bytes, Type type)
return val;
}

public static IntPtr AllocateMemory(this Process process, int size)
{
return WinAPI.VirtualAllocEx(process.Handle, IntPtr.Zero, (SizeT)size, (uint)MemPageState.MEM_COMMIT,
MemPageProtect.PAGE_EXECUTE_READWRITE);
}

public static bool FreeMemory(this Process process, IntPtr addr)
{
const uint MEM_RELEASE = 0x8000;
return WinAPI.VirtualFreeEx(process.Handle, addr, SizeT.Zero, MEM_RELEASE);
}

public static bool VirtualProtect(this Process process, IntPtr addr, int size, MemPageProtect protect,
out MemPageProtect oldProtect)
{
return WinAPI.VirtualProtectEx(process.Handle, addr, (SizeT)size, protect, out oldProtect);
}

public static bool VirtualProtect(this Process process, IntPtr addr, int size, MemPageProtect protect)
{
MemPageProtect oldProtect;
return WinAPI.VirtualProtectEx(process.Handle, addr, (SizeT)size, protect, out oldProtect);
}

public static IntPtr CreateThread(this Process process, IntPtr startAddress, IntPtr parameter)
{
IntPtr threadId;
return WinAPI.CreateRemoteThread(process.Handle, IntPtr.Zero, (SizeT)0, startAddress, parameter, 0,
out threadId);
}

public static IntPtr CreateThread(this Process process, IntPtr startAddress)
{
return CreateThread(process, startAddress, IntPtr.Zero);
}

public static void Suspend(this Process process)
{
WinAPI.NtSuspendProcess(process.Handle);
}

public static void Resume(this Process process)
{
WinAPI.NtResumeProcess(process.Handle);
}

public static float ToFloatBits(this uint i)
{
return BitConverter.ToSingle(BitConverter.GetBytes(i), 0);
27 changes: 0 additions & 27 deletions StarManager/Process/WinAPI.cs
Original file line number Diff line number Diff line change
@@ -85,33 +85,6 @@ public static extern uint GetModuleBaseName(IntPtr hProcess, IntPtr hModule, [Ou
public static extern bool IsWow64Process(IntPtr hProcess,
[Out, MarshalAs(UnmanagedType.Bool)] out bool wow64Process);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern SizeT VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress,
[Out] out MemoryBasicInformation lpBuffer, SizeT dwLength);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, SizeT dwSize, uint flAllocationType,
MemPageProtect flProtect);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, SizeT dwSize, uint dwFreeType);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, SizeT dwSize,
MemPageProtect flNewProtect, [Out] out MemPageProtect lpflOldProtect);

[DllImport("ntdll.dll", SetLastError = true)]
public static extern IntPtr NtSuspendProcess(IntPtr hProcess);

[DllImport("ntdll.dll", SetLastError = true)]
public static extern IntPtr NtResumeProcess(IntPtr hProcess);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, SizeT dwStackSize,
IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out IntPtr lpThreadId);

[StructLayout(LayoutKind.Sequential)]
public struct MODULEINFO
{
8 changes: 4 additions & 4 deletions StarManager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -6,11 +6,11 @@
// набора атрибутов. Измените значения этих атрибутов, чтобы изменить сведения,
// связанные со сборкой.
[assembly: AssemblyTitle("StarDisplay")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("Tool to view and edit stars for SM64")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StarDisplay")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyCompany("https://github.com/aglab2/hacktice")]
[assembly: AssemblyProduct("https://github.com/aglab2/SM64StarDisplay")]
[assembly: AssemblyCopyright("Copyright © aglab2 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

10 changes: 10 additions & 0 deletions StarManager/StarManager.csproj
Original file line number Diff line number Diff line change
@@ -69,6 +69,16 @@
<PropertyGroup>
<SignManifests>false</SignManifests>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'ReleaseExternal|AnyCPU'">
<OutputPath>bin\ReleaseExternal\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>none</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
10 changes: 10 additions & 0 deletions StarManagerUpdater/StarManagerUpdater.csproj
Original file line number Diff line number Diff line change
@@ -32,6 +32,16 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'ReleaseExternal|AnyCPU'">
<OutputPath>bin\ReleaseExternal\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Octokit, Version=0.46.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Octokit.0.46.0\lib\net46\Octokit.dll</HintPath>

0 comments on commit 4ffe856

Please sign in to comment.