-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
s
committed
Aug 19, 2021
1 parent
e5e3c82
commit 9b364f5
Showing
134 changed files
with
18,100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/> | ||
</startup> | ||
<runtime> | ||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<dependentAssembly> | ||
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/> | ||
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0"/> | ||
</dependentAssembly> | ||
</assemblyBinding> | ||
</runtime> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{A45BC8F5-CB13-4006-9EAE-A07CA7CC091C}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>DebugCompiler</RootNamespace> | ||
<AssemblyName>DebugCompiler</AssemblyName> | ||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
<Deterministic>true</Deterministic> | ||
<TargetFrameworkProfile /> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>TRACE;DEBUG;X86</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>x64</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<StartupObject>DebugCompiler.Root</StartupObject> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> | ||
<DebugSymbols>true</DebugSymbols> | ||
<OutputPath>bin\x64\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<DebugType>full</DebugType> | ||
<PlatformTarget>x64</PlatformTarget> | ||
<LangVersion>7.3</LangVersion> | ||
<ErrorReport>prompt</ErrorReport> | ||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> | ||
<Prefer32Bit>true</Prefer32Bit> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> | ||
<OutputPath>bin\x64\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<Optimize>true</Optimize> | ||
<DebugType>pdbonly</DebugType> | ||
<PlatformTarget>x64</PlatformTarget> | ||
<LangVersion>7.3</LangVersion> | ||
<ErrorReport>prompt</ErrorReport> | ||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> | ||
<Prefer32Bit>true</Prefer32Bit> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System.Numerics" /> | ||
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System.Windows.Forms" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
<Reference Include="xdevkit"> | ||
<HintPath>.\xdevkit.dll</HintPath> | ||
<EmbedInteropTypes>False</EmbedInteropTypes> | ||
</Reference> | ||
<Reference Include="xdrpc"> | ||
<HintPath>.\xdrpc.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Extensions.cs" /> | ||
<Compile Include="Injector.cs" /> | ||
<Compile Include="Root.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="T7Memory.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\External\External\External.csproj"> | ||
<Project>{810bdf68-87bb-45a2-ac4f-87643045bf4c}</Project> | ||
<Name>External</Name> | ||
</ProjectReference> | ||
<ProjectReference Include="..\T7CompilerLib\T7CompilerLib.csproj"> | ||
<Project>{7527fb74-e8ac-43f1-9e48-e9d4d2b5d5b3}</Project> | ||
<Name>T7CompilerLib</Name> | ||
</ProjectReference> | ||
<ProjectReference Include="..\TreyarchCompiler\TreyarchCompiler.csproj"> | ||
<Project>{2cdefd40-3952-44a7-b69e-711fbacac869}</Project> | ||
<Name>TreyarchCompiler</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using T7MemUtil; | ||
|
||
namespace DebugCompiler | ||
{ | ||
internal static class Extensions | ||
{ | ||
public static IntPtr Open(this Process process, int dwDesiredAccess = T7Memory.PROCESS_ACCESS) | ||
{ | ||
return T7Memory.OpenProcess(dwDesiredAccess, false, process.Id); | ||
} | ||
|
||
public static IntPtr Relocate(this Process process, IntPtr Handle) | ||
{ | ||
return new IntPtr(process.MainModule.BaseAddress.ToInt64() + Handle.ToInt64()); | ||
} | ||
|
||
public static IntPtr Add(this IntPtr pointer, long value) | ||
{ | ||
return new IntPtr(value + pointer.ToInt64()); | ||
} | ||
|
||
public static IntPtr Relocate(this Process process, uint Handle) | ||
{ | ||
return process.Relocate(new IntPtr(Handle)); | ||
} | ||
|
||
public static long ReadInt64(this IntPtr ProcessHandle, IntPtr Address) | ||
{ | ||
byte[] buffer = new byte[sizeof(long)]; | ||
IntPtr numBytes = IntPtr.Zero; | ||
T7Memory.ReadProcessMemory(ProcessHandle, Address, buffer, (IntPtr)sizeof(long), ref numBytes); | ||
return BitConverter.ToInt64(buffer, 0); | ||
} | ||
|
||
public static uint ReadUInt32(this IntPtr ProcessHandle, IntPtr Address) | ||
{ | ||
byte[] buffer = new byte[sizeof(uint)]; | ||
IntPtr numBytes = IntPtr.Zero; | ||
T7Memory.ReadProcessMemory(ProcessHandle, Address, buffer, (IntPtr)sizeof(uint), ref numBytes); | ||
return BitConverter.ToUInt32(buffer, 0); | ||
} | ||
|
||
public static byte[] ReadBytes(this IntPtr ProcessHandle, IntPtr Address, int Count) | ||
{ | ||
byte[] buffer = new byte[Count]; | ||
IntPtr numBytes = IntPtr.Zero; | ||
T7Memory.ReadProcessMemory(ProcessHandle, Address, buffer, (IntPtr)Count, ref numBytes); | ||
return buffer; | ||
} | ||
|
||
public static IntPtr ReadVoidPtr(this IntPtr ProcessHandle, IntPtr Address) | ||
{ | ||
return new IntPtr(ProcessHandle.ReadInt64(Address)); | ||
} | ||
|
||
public static T ToStruct<T>(this byte[] data) where T : struct | ||
{ | ||
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); | ||
T val = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T)); | ||
handle.Free(); | ||
return val; | ||
} | ||
|
||
public static T ReadStruct<T>(this IntPtr ProcessHandle, IntPtr Address) where T : struct | ||
{ | ||
return ProcessHandle.ReadBytes(Address, Marshal.SizeOf(typeof(T))).ToStruct<T>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
using System; | ||
using System.Text; | ||
using System.Runtime.InteropServices; | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
namespace DebugCompiler | ||
{ | ||
public class Injector | ||
{ | ||
[DllImport("kernel32.dll")] | ||
internal static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId); | ||
|
||
[DllImport("kernel32.dll", CharSet = CharSet.Auto)] | ||
internal static extern IntPtr GetModuleHandle(string lpModuleName); | ||
|
||
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] | ||
internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName); | ||
|
||
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] | ||
static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, | ||
uint dwSize, uint flAllocationType, uint flProtect); | ||
|
||
[DllImport("kernel32.dll", SetLastError = true)] | ||
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten); | ||
|
||
[DllImport("kernel32.dll")] | ||
static extern IntPtr CreateRemoteThread(IntPtr hProcess, | ||
IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId); | ||
|
||
[DllImport("psapi.dll", SetLastError = true)] | ||
public static extern bool EnumProcessModulesEx( | ||
IntPtr hProcess, | ||
[Out] IntPtr lphModule, | ||
UInt32 cb, | ||
[MarshalAs(UnmanagedType.U4)] out UInt32 lpcbNeeded, | ||
DwFilterFlag dwff); | ||
|
||
[DllImport("psapi.dll")] | ||
static extern uint GetModuleFileNameEx( | ||
IntPtr hProcess, | ||
IntPtr hModule, | ||
[Out] StringBuilder lpBaseName, | ||
[In][MarshalAs(UnmanagedType.U4)] int nSize); | ||
|
||
// privileges | ||
const int PROCESS_CREATE_THREAD = 0x0002; | ||
const int PROCESS_QUERY_INFORMATION = 0x0400; | ||
const int PROCESS_VM_OPERATION = 0x0008; | ||
const int PROCESS_VM_WRITE = 0x0020; | ||
const int PROCESS_VM_READ = 0x0010; | ||
|
||
// used for memory allocation | ||
const uint MEM_COMMIT = 0x00001000; | ||
const uint MEM_RESERVE = 0x00002000; | ||
const uint PAGE_READWRITE = 4; | ||
|
||
[Flags] | ||
public enum ProcessAccessFlags : uint | ||
{ | ||
All = 0x001F0FFF, | ||
Terminate = 0x00000001, | ||
CreateThread = 0x00000002, | ||
VirtualMemoryOperation = 0x00000008, | ||
VirtualMemoryRead = 0x00000010, | ||
VirtualMemoryWrite = 0x00000020, | ||
DuplicateHandle = 0x00000040, | ||
CreateProcess = 0x000000080, | ||
SetQuota = 0x00000100, | ||
SetInformation = 0x00000200, | ||
QueryInformation = 0x00000400, | ||
QueryLimitedInformation = 0x00001000, | ||
Synchronize = 0x00100000 | ||
} | ||
|
||
public enum DwFilterFlag : uint | ||
{ | ||
LIST_MODULES_DEFAULT = 0x0, // This is the default one app would get without any flag. | ||
LIST_MODULES_32BIT = 0x01, // list 32bit modules in the target process. | ||
LIST_MODULES_64BIT = 0x02, // list all 64bit modules. 32bit exe will be stripped off. | ||
LIST_MODULES_ALL = (LIST_MODULES_32BIT | LIST_MODULES_64BIT) // list all the modules | ||
} | ||
|
||
private static bool ModuleExists(Process process, string dllName) | ||
{ | ||
var dllFileName = Path.GetFileName(dllName); | ||
|
||
IntPtr procPtr = OpenProcess((int)ProcessAccessFlags.All, false, process.Id); | ||
|
||
IntPtr[] hMods = new IntPtr[1024]; | ||
|
||
GCHandle gch = GCHandle.Alloc(hMods, GCHandleType.Pinned); // Don't forget to free this later | ||
IntPtr pModules = gch.AddrOfPinnedObject(); | ||
|
||
// Setting up the rest of the parameters for EnumProcessModules | ||
var uiSize = (uint)(Marshal.SizeOf(typeof(IntPtr)) * (hMods.Length)); | ||
|
||
bool foundModule = false; | ||
|
||
if (EnumProcessModulesEx(procPtr, pModules, uiSize, out var cbNeeded, DwFilterFlag.LIST_MODULES_64BIT)) | ||
{ | ||
Int32 uiTotalNumberofModules = (Int32)(cbNeeded / Marshal.SizeOf(typeof(IntPtr))); | ||
|
||
for (int i = 0; i < uiTotalNumberofModules; i++) | ||
{ | ||
StringBuilder sb = new StringBuilder(1024); | ||
|
||
GetModuleFileNameEx(procPtr, hMods[i], sb, sb.Capacity); | ||
|
||
if (Path.GetFileName(sb.ToString()) == dllFileName) | ||
{ | ||
foundModule = true; | ||
break; | ||
} | ||
|
||
} | ||
} | ||
|
||
gch.Free(); | ||
|
||
return foundModule; | ||
} | ||
|
||
/// <summary> | ||
/// Injects the dll with the specified name into the remote process. | ||
/// </summary> | ||
/// <param name="process"></param> | ||
/// <param name="dllName"></param> | ||
/// <returns></returns> | ||
public static DllInjectionStatus Inject(Process process, string dllName) | ||
{ | ||
if (ModuleExists(process, dllName)) | ||
return DllInjectionStatus.AlreadyExists; | ||
|
||
IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, process.Id); | ||
|
||
IntPtr loadLibraryAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA"); | ||
|
||
IntPtr allocMemAddress = VirtualAllocEx(procHandle, IntPtr.Zero, (uint)((dllName.Length + 1) * Marshal.SizeOf(typeof(char))), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); | ||
|
||
if (allocMemAddress == IntPtr.Zero) return DllInjectionStatus.RemoteAllocationFailed; | ||
|
||
UIntPtr bytesWritten; | ||
|
||
if (!WriteProcessMemory(procHandle, allocMemAddress, Encoding.Default.GetBytes(dllName), (uint)((dllName.Length + 1) * Marshal.SizeOf(typeof(char))), out bytesWritten)) | ||
return DllInjectionStatus.ProcessWriteFailed; | ||
|
||
if (CreateRemoteThread(procHandle, IntPtr.Zero, 0, loadLibraryAddr, allocMemAddress, 0, IntPtr.Zero) == IntPtr.Zero) | ||
return DllInjectionStatus.InjectionFailed; | ||
|
||
return DllInjectionStatus.Success; | ||
} | ||
} | ||
|
||
public enum DllInjectionStatus | ||
{ | ||
Success = 0, | ||
AlreadyExists, | ||
InjectionFailed, | ||
RemoteAllocationFailed, | ||
ProcessWriteFailed | ||
} | ||
} |
Oops, something went wrong.