Skip to content

Commit

Permalink
Add mono module
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Nov 11, 2023
1 parent 5c522f4 commit d82a679
Show file tree
Hide file tree
Showing 11 changed files with 993 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ target_sources(
)

add_subdirectory(GUI)
add_subdirectory(injector)
add_subdirectory(system)
140 changes: 140 additions & 0 deletions src/injector/Assembly.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include "Assembly.hpp"

namespace Xash
{
namespace Injector
{
void Assembly::ClearInstructions()
{
_asmInstructions.clear();
}

const std::vector<uint8_t> &Assembly::GetAssembly() const
{
return _asmInstructions;
}

void Assembly::Ret()
{
_asmInstructions.push_back(0xC3);
}

void Assembly::CallRax()
{
_asmInstructions.push_back(0xFF);
_asmInstructions.push_back(0xD0);
}

void Assembly::SubRsp(uint8_t value)
{
_asmInstructions.push_back(0x48);
_asmInstructions.push_back(0x83);
_asmInstructions.push_back(0xEC);
_asmInstructions.push_back(value);
}

void Assembly::AddRsp(uint8_t value)
{
_asmInstructions.push_back(0x48);
_asmInstructions.push_back(0x83);
_asmInstructions.push_back(0xC4);
_asmInstructions.push_back(value);
}

void Assembly::MoveIntoRax(void *value)
{
_asmInstructions.push_back(0x48);
_asmInstructions.push_back(0xB8);
_asmInstructions.insert(
_asmInstructions.end(), (uint8_t *)&value, (uint8_t *)&value + sizeof(void *)
);
}

void Assembly::MoveRaxIntoValue(void *value)
{
_asmInstructions.push_back(0x48);
_asmInstructions.push_back(0xA3);
_asmInstructions.insert(
_asmInstructions.end(), (uint8_t *)&value, (uint8_t *)&value + sizeof(void *)
);
}

void Assembly::XorRaxRax()
{
_asmInstructions.push_back(0x48);
_asmInstructions.push_back(0x31);
_asmInstructions.push_back(0xC0);
}

void Assembly::XorRcxRcx()
{
_asmInstructions.push_back(0x48);
_asmInstructions.push_back(0x31);
_asmInstructions.push_back(0xC9);
}

void Assembly::XorRdxRdx()
{
_asmInstructions.push_back(0x48);
_asmInstructions.push_back(0x31);
_asmInstructions.push_back(0xD2);
}

void Assembly::XorR8R8()
{
_asmInstructions.push_back(0x4D);
_asmInstructions.push_back(0x31);
_asmInstructions.push_back(0xC0);
}

void Assembly::XorR9R9()
{
_asmInstructions.push_back(0x4D);
_asmInstructions.push_back(0x31);
_asmInstructions.push_back(0xC9);
}

void Assembly::MoveIntoRcx(void *value)
{
_asmInstructions.push_back(0x48);
_asmInstructions.push_back(0xB9);
_asmInstructions.insert(
_asmInstructions.end(), (uint8_t *)&value, (uint8_t *)&value + sizeof(void *)
);
}

void Assembly::MoveIntoRdx(void *value)
{
_asmInstructions.push_back(0x48);
_asmInstructions.push_back(0xBA);
_asmInstructions.insert(
_asmInstructions.end(), (uint8_t *)&value, (uint8_t *)&value + sizeof(void *)
);
}

void Assembly::MoveIntoR8(void *value)
{
_asmInstructions.push_back(0x49);
_asmInstructions.push_back(0xB8);
_asmInstructions.insert(
_asmInstructions.end(), (uint8_t *)&value, (uint8_t *)&value + sizeof(void *)
);
}

void Assembly::MoveIntoR9(void *value)
{
_asmInstructions.push_back(0x49);
_asmInstructions.push_back(0xB9);
_asmInstructions.insert(
_asmInstructions.end(), (uint8_t *)&value, (uint8_t *)&value + sizeof(void *)
);
}

void Assembly::MonoThreadAttach(void *monoAttachFunc, void *MonoDomain)
{
MoveIntoRax(monoAttachFunc);
MoveIntoRcx(MonoDomain);
CallRax();
}
} // namespace Injector
} // namespace Xash
40 changes: 40 additions & 0 deletions src/injector/Assembly.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <vector>

namespace Xash
{
namespace Injector
{
class Assembly
{
public:
void ClearInstructions();
const std::vector<uint8_t> &GetAssembly() const;

void Ret();
void CallRax();

void SubRsp(uint8_t value);
void AddRsp(uint8_t value);

void MoveIntoRax(void *value);
void MoveIntoRcx(void *value);
void MoveIntoRdx(void *value);
void MoveIntoR8(void *value);
void MoveIntoR9(void *value);
void MoveRaxIntoValue(void *value);

void XorRaxRax();
void XorRcxRcx();
void XorRdxRdx();
void XorR8R8();
void XorR9R9();

void MonoThreadAttach(void *monoAttachFunc, void *MonoDomain);

private:
std::vector<uint8_t> _asmInstructions;
};
} // namespace Injector
} // namespace Xash
14 changes: 14 additions & 0 deletions src/injector/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.27)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/MonoModule.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Assembly.cpp
)
Loading

0 comments on commit d82a679

Please sign in to comment.