This commit is contained in:
es3n1n
2025-05-07 17:01:09 +09:00
commit a49f027fde
33 changed files with 2609 additions and 0 deletions

52
cxx-shared/shared/ctx.hpp Normal file
View 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

View 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
View 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

View 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

View 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