mirror of
https://github.com/es3n1n/defendnot.git
synced 2026-08-02 10:32:01 +00:00
initial
This commit is contained in:
23
cxx-shared/cxx-shared.vcxitems
Normal file
23
cxx-shared/cxx-shared.vcxitems
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Label="Globals">
|
||||
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' < '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
|
||||
<HasSharedItems>true</HasSharedItems>
|
||||
<ItemsProjectGuid>{8704ed28-3d20-4f31-9ba2-9a0040649372}</ItemsProjectGuid>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ProjectCapability Include="SourceItemsFromImports" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="$(MSBuildThisFileDirectory)shared\ctx.hpp" />
|
||||
<ClInclude Include="$(MSBuildThisFileDirectory)shared\defer.hpp" />
|
||||
<ClInclude Include="$(MSBuildThisFileDirectory)shared\ipc.hpp" />
|
||||
<ClInclude Include="$(MSBuildThisFileDirectory)shared\names.hpp" />
|
||||
<ClInclude Include="$(MSBuildThisFileDirectory)shared\util.hpp" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
6
cxx-shared/cxx-shared.vcxitems.user
Normal file
6
cxx-shared/cxx-shared.vcxitems.user
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ShowAllFiles>true</ShowAllFiles>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
52
cxx-shared/shared/ctx.hpp
Normal file
52
cxx-shared/shared/ctx.hpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
|
||||
#include "shared/util.hpp"
|
||||
|
||||
namespace shared {
|
||||
constexpr std::size_t kMaxNameLength = 128;
|
||||
constexpr std::string_view kCtxPath = "ctx.bin";
|
||||
|
||||
namespace detail {
|
||||
inline std::string ctx_path() {
|
||||
auto path = get_this_module_path().parent_path();
|
||||
path /= kCtxPath;
|
||||
return path.string();
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
enum class State : std::uint8_t {
|
||||
OFF = 0,
|
||||
ON,
|
||||
};
|
||||
|
||||
inline constinit struct Context {
|
||||
public:
|
||||
State state = State::ON;
|
||||
bool verbose = false;
|
||||
std::array<char, kMaxNameLength + 1> name = {0}; // +1 for the nullterm
|
||||
|
||||
void serialize() const {
|
||||
std::ofstream stream(detail::ctx_path(), std::ios::binary);
|
||||
if (!stream.good()) {
|
||||
throw std::runtime_error("can not write ctx.bin");
|
||||
}
|
||||
|
||||
stream.write(reinterpret_cast<const char*>(this), sizeof(*this));
|
||||
}
|
||||
|
||||
void deserialize() {
|
||||
std::ifstream stream(detail::ctx_path(), std::ios::binary);
|
||||
if (!stream.good()) {
|
||||
throw std::runtime_error("can not read ctx.bin");
|
||||
}
|
||||
|
||||
stream.read(reinterpret_cast<char*>(this), sizeof(*this));
|
||||
}
|
||||
} ctx = {};
|
||||
|
||||
static_assert(std::is_trivially_copyable_v<Context>);
|
||||
} // namespace shared
|
||||
30
cxx-shared/shared/defer.hpp
Normal file
30
cxx-shared/shared/defer.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <utility>
|
||||
|
||||
template <typename Callable>
|
||||
class DeferHolder {
|
||||
public:
|
||||
explicit DeferHolder(Callable&& callable) noexcept: callable_(std::forward<Callable>(callable)) { }
|
||||
|
||||
~DeferHolder() {
|
||||
callable_();
|
||||
}
|
||||
|
||||
private:
|
||||
Callable callable_;
|
||||
};
|
||||
|
||||
class Defer {
|
||||
public:
|
||||
constexpr Defer() noexcept = default;
|
||||
constexpr ~Defer() noexcept = default;
|
||||
|
||||
template <typename Callable>
|
||||
DeferHolder<Callable> operator%(Callable&& cb) {
|
||||
return DeferHolder<Callable>{std::forward<Callable>(cb)};
|
||||
}
|
||||
};
|
||||
|
||||
#define COMMON_CAT_(x, y) x##y
|
||||
#define COMMON_CAT(x, y) COMMON_CAT_(x, y)
|
||||
#define defer auto COMMON_CAT(_defer_instance_, __LINE__) = Defer{} % [&]()
|
||||
83
cxx-shared/shared/ipc.hpp
Normal file
83
cxx-shared/shared/ipc.hpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#pragma once
|
||||
#include <Windows.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string_view>
|
||||
|
||||
namespace shared {
|
||||
constexpr std::string_view kSegName = "defender-disabler-ipc";
|
||||
|
||||
enum class InterProcessCommunicationMode : std::uint8_t {
|
||||
READ = 0,
|
||||
WRITE,
|
||||
READ_WRITE,
|
||||
};
|
||||
|
||||
class InterProcessCommunication {
|
||||
class Data {
|
||||
public:
|
||||
bool finished = false;
|
||||
bool success = false;
|
||||
};
|
||||
static_assert(std::is_trivially_copyable_v<Data>);
|
||||
|
||||
public:
|
||||
explicit InterProcessCommunication(InterProcessCommunicationMode mode, bool should_create = false): mode_(mode), was_created_(should_create) {
|
||||
int flag = mode == InterProcessCommunicationMode::READ ? FILE_MAP_READ :
|
||||
mode == InterProcessCommunicationMode::READ_WRITE ? FILE_MAP_ALL_ACCESS :
|
||||
FILE_MAP_WRITE;
|
||||
|
||||
if (should_create) {
|
||||
setup_handle(CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(Data), kSegName.data()));
|
||||
} else {
|
||||
setup_handle(OpenFileMappingA(flag, FALSE, kSegName.data()));
|
||||
}
|
||||
|
||||
data_ = reinterpret_cast<Data*>(MapViewOfFile(handle_, flag, 0, 0, sizeof(Data)));
|
||||
if (data_ == nullptr) {
|
||||
throw std::runtime_error("unable to map ipc");
|
||||
}
|
||||
}
|
||||
|
||||
~InterProcessCommunication() {
|
||||
if (data_ != nullptr && was_created_ &&
|
||||
(mode_ == InterProcessCommunicationMode::WRITE || mode_ == InterProcessCommunicationMode::READ_WRITE)) {
|
||||
/// Erase all data
|
||||
std::memset(data_, 0, sizeof(*data_));
|
||||
}
|
||||
|
||||
if (data_ != nullptr) {
|
||||
UnmapViewOfFile(data_);
|
||||
data_ = nullptr;
|
||||
}
|
||||
|
||||
if (handle_ != INVALID_HANDLE_VALUE) {
|
||||
CloseHandle(handle_);
|
||||
handle_ = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] Data* operator->() noexcept {
|
||||
return data_;
|
||||
}
|
||||
|
||||
private:
|
||||
void setup_handle(HANDLE handle) {
|
||||
handle_ = handle;
|
||||
valid_ = handle_ != INVALID_HANDLE_VALUE;
|
||||
throw_if_invalid();
|
||||
}
|
||||
|
||||
void throw_if_invalid() const {
|
||||
if (!valid_) {
|
||||
throw std::runtime_error("unable to access ipc seg");
|
||||
}
|
||||
}
|
||||
|
||||
HANDLE handle_ = INVALID_HANDLE_VALUE;
|
||||
bool valid_ = false;
|
||||
Data* data_ = nullptr;
|
||||
InterProcessCommunicationMode mode_;
|
||||
bool was_created_;
|
||||
};
|
||||
} // namespace shared
|
||||
10
cxx-shared/shared/names.hpp
Normal file
10
cxx-shared/shared/names.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <string_view>
|
||||
|
||||
namespace names {
|
||||
constexpr std::string_view kProjectName = "defendnot";
|
||||
constexpr std::string_view kRepoUrl = "https://github.com/es3n1n/defendnot";
|
||||
|
||||
constexpr std::string_view kVictimProcess = "Taskmgr.exe";
|
||||
constexpr std::string_view kDllName = "defendnot.dll";
|
||||
} // namespace names
|
||||
30
cxx-shared/shared/util.hpp
Normal file
30
cxx-shared/shared/util.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
namespace shared {
|
||||
inline std::filesystem::path get_this_module_path() {
|
||||
char result[_MAX_PATH] = {0};
|
||||
|
||||
/// \fixme @es3n1n: This is sketchy
|
||||
HMODULE h_module = nullptr;
|
||||
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
||||
reinterpret_cast<LPCSTR>(&get_this_module_path), &h_module);
|
||||
|
||||
GetModuleFileNameA(h_module, result, sizeof(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
inline void alloc_console() {
|
||||
static std::once_flag fl;
|
||||
|
||||
/// Most likely the process we're injecting to does not have allocated console
|
||||
std::call_once(fl, []() -> void {
|
||||
AllocConsole();
|
||||
freopen_s(reinterpret_cast<FILE**>(stdout), "CONOUT$", "w", stdout);
|
||||
freopen_s(reinterpret_cast<FILE**>(stderr), "CONOUT$", "w", stderr);
|
||||
});
|
||||
}
|
||||
} // namespace shared
|
||||
Reference in New Issue
Block a user