Skip to content

Commit

Permalink
feat: i dont know what i did anymore lmao
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondNewtonLaw committed Oct 2, 2024
1 parent 84f603b commit 3c49667
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 218 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ add_library(Module SHARED
Environment/Libraries/WebSocket.hpp
Environment/Libraries/Http.cpp
Environment/Libraries/Http.hpp
Environment/Libraries/Debugger.cpp
Environment/Libraries/Debugger.hpp

# Disassembler abstraction
Disassembler/Disassembler.cpp
Expand Down
4 changes: 1 addition & 3 deletions ClosureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int ClosureManager::newcclosure_handler(lua_State *L) {
const auto callResult = lua_pcall(L, argc, LUA_MULTRET, 0);
if (callResult != LUA_OK && callResult != LUA_YIELD &&
std::strcmp(luaL_optstring(L, -1, ""), "attempt to yield across metamethod/C-call boundary") == 0) {
return lua_yield(L, 0);
return lua_yield(L, LUA_MULTRET);
}

if (callResult == LUA_ERRRUN)
Expand Down Expand Up @@ -362,8 +362,6 @@ int ClosureManager::newcclosure(lua_State *L) {
clManager->FixClosure(L, closure);
lua_pushcclosurek(L, ClosureManager::newcclosure_handler, functionName, 0, nullptr);
const auto cclosure = lua_toclosure(L, closureIndex);
luaC_barrierfast(L, closure);
luaS_fix(cclosure);
clManager->m_newcclosureMap[currentDataModel][cclosure] = closure;
lua_remove(L, lua_gettop(L) - 1); // Balance lua stack.
return 1;
Expand Down
50 changes: 24 additions & 26 deletions Debugger/DebuggerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "Communication/PacketSerdes.hpp"
#include "Disassembler/Disassembler.hpp"
#include "Environment/Libraries/Debugger.hpp"
#include "Luau/Compiler.h"
#include "LuauManager.hpp"
#include "RobloxManager.hpp"
Expand All @@ -25,18 +24,18 @@ RbxStu::LuauFunctionDefinitions::luau_load original_rluau_load;
RBX::Studio::FunctionTypes::luau_execute original_rluau_execute;

void rluau_execute__detour(lua_State *L) {
const auto debugManager = DebuggerManager::GetSingleton();
const auto currentCl = clvalue(L->ci->func);
if (!currentCl->isC) {

if (debugManager->IsLocalPlayerScript(currentCl->l.p->source->data)) {
// printf("LocalScript Found: %s\n", currentCl->l.p->source->data);
}

if (debugManager->IsServerScript(currentCl->l.p->source->data)) {
// printf("Server Script Found: %s\n", currentCl->l.p->source->data);
}
}
// const auto debugManager = DebuggerManager::GetSingleton();
// const auto currentCl = clvalue(L->ci->func);
// if (!currentCl->isC) {
//
// if (debugManager->IsLocalPlayerScript(currentCl->l.p->source->data)) {
// // printf("LocalScript Found: %s\n", currentCl->l.p->source->data);
// }
//
// if (debugManager->IsServerScript(currentCl->l.p->source->data)) {
// // printf("Server Script Found: %s\n", currentCl->l.p->source->data);
// }
// }

return original_rluau_execute(L);
}
Expand Down Expand Up @@ -83,25 +82,24 @@ void DebuggerManager::Initialize() {
logger->PrintInformation(RbxStu::DebuggerManager, std::format("- '{}' at address {}.", funcName, funcAddress));
}

logger->PrintInformation(RbxStu::DebuggerManager, "Setting up hook on luau_load to track bytecode load-ins...");

const auto rLuauLoad =
reinterpret_cast<RbxStu::LuauFunctionDefinitions::luau_load>(luauManager->GetFunction("luau_load"));
// logger->PrintInformation(RbxStu::DebuggerManager, "Setting up hook on luau_load to track bytecode load-ins...");

MH_CreateHook(rLuauLoad, rluau_load__detour, reinterpret_cast<void **>(&original_rluau_load));
MH_EnableHook(rLuauLoad);
// const auto rLuauLoad =
// reinterpret_cast<RbxStu::LuauFunctionDefinitions::luau_load>(luauManager->GetFunction("luau_load"));

logger->PrintInformation(RbxStu::DebuggerManager, "Set hook. luau_load -> Instrument bytecode loading");
// MH_CreateHook(rLuauLoad, rluau_load__detour, reinterpret_cast<void **>(&original_rluau_load));
// MH_EnableHook(rLuauLoad);
// logger->PrintInformation(RbxStu::DebuggerManager, "Set hook. luau_load -> Instrument bytecode loading");

logger->PrintInformation(RbxStu::DebuggerManager, "Setting up hook on luau_execute to track execution.");
// logger->PrintInformation(RbxStu::DebuggerManager, "Setting up hook on luau_execute to track execution.");

const auto rLuauExecute =
reinterpret_cast<RBX::Studio::FunctionTypes::luau_execute>(luauManager->GetFunction("luau_execute"));
// const auto rLuauExecute =
// reinterpret_cast<RBX::Studio::FunctionTypes::luau_execute>(luauManager->GetFunction("luau_execute"));

MH_CreateHook(rLuauExecute, rluau_execute__detour, reinterpret_cast<void **>(&original_rluau_execute));
MH_EnableHook(rLuauExecute);
// MH_CreateHook(rLuauExecute, rluau_execute__detour, reinterpret_cast<void **>(&original_rluau_execute));
// MH_EnableHook(rLuauExecute);

logger->PrintInformation(RbxStu::DebuggerManager, "Set hook. luau_execute -> Instrument script execution.");
// logger->PrintInformation(RbxStu::DebuggerManager, "Set hook. luau_execute -> Instrument script execution.");
}
void DebuggerManager::PushScriptTracking(const char *chunkname, lua_State *scriptState) {

Expand Down
5 changes: 2 additions & 3 deletions Environment/EnvironmentManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "Libraries/Closures.hpp"
#include "Libraries/Console.hpp"
#include "Libraries/Debug.hpp"
#include "Libraries/Debugger.hpp"
#include "Libraries/Filesystem.hpp"
#include "Libraries/Globals.hpp"
#include "Libraries/Http.hpp"
Expand Down Expand Up @@ -59,7 +58,6 @@ std::shared_ptr<EnvironmentManager> EnvironmentManager::GetSingleton() {
EnvironmentManager::pInstance->m_vLibraryList.push_back(new Instance{});
EnvironmentManager::pInstance->m_vLibraryList.push_back(new Input{});
EnvironmentManager::pInstance->m_vLibraryList.push_back(new Http{});
EnvironmentManager::pInstance->m_vLibraryList.push_back(new Debugger{});
}

return EnvironmentManager::pInstance;
Expand Down Expand Up @@ -300,7 +298,8 @@ void EnvironmentManager::PushEnvironment(_In_ lua_State *L) {
for (const auto &[bannedName, sound]: specificBlockage) {
if (Utilities::ToLower(bannedName).find(instanceClassName) != std::string::npos) {
for (const auto &func: sound) {
if (indexAsString.find(func) != std::string::npos) {
if (indexAsString.find(func) != std::string::npos &&
strstr(indexAsString.c_str(), func.c_str()) == indexAsString.c_str()) {
goto banned__index;
}
if (func == "BLOCK_ALL" && strcmp(loweredIndex.c_str(), "classname") != 0 &&
Expand Down
8 changes: 5 additions & 3 deletions Environment/Libraries/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
#include "Logger.hpp"
#include "RobloxManager.hpp"

static std::string ConsoleWindowTitle{"-- RbxStu V2 --"};

namespace RbxStu {
namespace Console {
HWND CreateIfNotCreated() {
if (const auto hWnd = GetConsoleWindow(); !hWnd || hWnd == INVALID_HANDLE_VALUE) {
AllocConsole();
Logger::GetSingleton()->OpenStandard();
SetConsoleTitleA("-- RbxStu V2 --");
SetConsoleTitleA(ConsoleWindowTitle.c_str());
Logger::GetSingleton()->PrintInformation(RbxStu::Anonymous, "-- roblox console created --");
}

Expand All @@ -39,8 +41,8 @@ namespace RbxStu {

int rconsolesettitle(lua_State *L) {
CreateIfNotCreated();
const auto wndName = luaL_checkstring(L, 1);
SetConsoleTitleA(wndName);
ConsoleWindowTitle = luaL_checkstring(L, 1);
SetConsoleTitleA(ConsoleWindowTitle.c_str());
return 0;
}

Expand Down
51 changes: 0 additions & 51 deletions Environment/Libraries/Debugger.cpp

This file was deleted.

14 changes: 0 additions & 14 deletions Environment/Libraries/Debugger.hpp

This file was deleted.

1 change: 0 additions & 1 deletion Environment/Libraries/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ namespace RbxStu {
});
}));

L->ci->flags |= 1;
return lua_yield(L, 1);
}

Expand Down
60 changes: 31 additions & 29 deletions LuauManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,20 @@ namespace RbxStu {
} // namespace RbxStu

static void luau__freeblock(lua_State *L, uint32_t sizeClass, void *block) {
if (reinterpret_cast<std::uintptr_t>(block) > 0x00007FF000000000) {
Logger::GetSingleton()->PrintWarning(
RbxStu::HookedFunction,
std::format("Suspicious address caught (non-heap range): {}. Deallocation blocked!", block));
return;
}
if (!Utilities::IsPointerValid(static_cast<std::uintptr_t *>(block)) ||
!Utilities::IsPointerValid(reinterpret_cast<std::uintptr_t **>(reinterpret_cast<std::uintptr_t>(block) - 8)) ||
!Utilities::IsPointerValid(*reinterpret_cast<std::uintptr_t **>(reinterpret_cast<std::uintptr_t>(block) - 8))) {
Logger::GetSingleton()->PrintWarning(
RbxStu::HookedFunction, std::format("Suspicious address caught: {}. Deallocation blocked!", block));
return;
}
// This has been fixed, the pointer check no longer needs to exist.
// if (reinterpret_cast<std::uintptr_t>(block) > 0x00007FF000000000) {
// Logger::GetSingleton()->PrintWarning(
// RbxStu::HookedFunction,
// std::format("Suspicious address caught (non-heap range): {}. Deallocation blocked!", block));
// return;
// }
// if (!Utilities::IsPointerValid(static_cast<std::uintptr_t *>(block)) ||
// !Utilities::IsPointerValid(reinterpret_cast<std::uintptr_t **>(reinterpret_cast<std::uintptr_t>(block) - 8))
// || !Utilities::IsPointerValid(*reinterpret_cast<std::uintptr_t **>(reinterpret_cast<std::uintptr_t>(block) -
// 8))) { Logger::GetSingleton()->PrintWarning(
// RbxStu::HookedFunction, std::format("Suspicious address caught: {}. Deallocation blocked!", block));
// return;
// }

return (reinterpret_cast<RbxStu::LuauFunctionDefinitions::freeblock>(
LuauManager::GetSingleton()->GetHookOriginal("freeblock"))(L, sizeClass, block));
Expand Down Expand Up @@ -224,22 +225,23 @@ void LuauManager::Initialize() {
logger->PrintInformation(RbxStu::LuauManager, "All cleaned up!");

logger->PrintInformation(RbxStu::LuauManager, "Hooking functions... [3/4]");

logger->PrintInformation(RbxStu::LuauManager, "- Installing pointer check hook into freeblock...");
this->m_mapHookMap["freeblock"] = new void *();

// Error checking, because Dottik didn't add it.
// - MakeSureDudeDies
if (MH_CreateHook(this->m_mapLuauFunctions["freeblock"], luau__freeblock, &this->m_mapHookMap["freeblock"]) !=
MH_OK) {
logger->PrintError(RbxStu::LuauManager, "Failed to create freeblock hook!");
throw std::exception("Creating freeblock hook failed.");
}

if (MH_EnableHook(this->m_mapLuauFunctions["freeblock"]) != MH_OK) {
logger->PrintError(RbxStu::LuauManager, "Failed to enable freeblock hook!");
throw std::exception("Enabling freeblock hook failed.");
}
logger->PrintInformation(RbxStu::LuauManager, "Hooks completed! [3/4]");

// logger->PrintInformation(RbxStu::LuauManager, "- Installing pointer check hook into freeblock...");
// this->m_mapHookMap["freeblock"] = new void *();

//// Error checking, because Dottik didn't add it.
//// - MakeSureDudeDies
// if (MH_CreateHook(this->m_mapLuauFunctions["freeblock"], luau__freeblock, &this->m_mapHookMap["freeblock"]) !=
// MH_OK) {
// logger->PrintError(RbxStu::LuauManager, "Failed to create freeblock hook!");
// throw std::exception("Creating freeblock hook failed.");
// }

// if (MH_EnableHook(this->m_mapLuauFunctions["freeblock"]) != MH_OK) {
// logger->PrintError(RbxStu::LuauManager, "Failed to enable freeblock hook!");
// throw std::exception("Enabling freeblock hook failed.");
// }

// this->m_mapHookMap["luaE_newthread"] = new void *();
// MH_CreateHook(this->m_mapLuauFunctions["luaE_newthread"], luaE__newthread,
Expand Down
22 changes: 11 additions & 11 deletions RobloxManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,28 @@ void *rbx__scriptcontext__resumeWaitingThreads(
const auto getDataModel = reinterpret_cast<RbxStu::StudioFunctionDefinitions::r_RBX_ScriptContext_getDataModel>(
robloxManager->GetRobloxFunction("RBX::ScriptContext::getDataModel"));

{
const auto dataModel = getDataModel(scriptContext);
if (const auto debuggerManager = DebuggerManager::GetSingleton();
dataModel->m_bIsOpen && !debuggerManager->IsInitialized()) {
const auto globalState = robloxManager->GetGlobalState(scriptContext);
debuggerManager->RegisterCallbackCopy(lua_callbacks(globalState.value()));
}

if (dataModel->m_bIsOpen)
robloxManager->SetScriptContext(robloxManager->GetDataModelType(dataModel), &scriptContext);
}
if (!scheduler->IsInitialized() && luauManager->IsInitialized()) { // !scheduler->is_initialized()
if (getDataModel == nullptr) {
logger->PrintWarning(RbxStu::HookedFunction, "Initialization of Scheduler may be unstable! Cannot "
"determine DataModel for the obtained ScriptContext!");
} else {
const auto expectedDataModel = robloxManager->GetCurrentDataModel(scheduler->GetExecutionDataModel());

if (!expectedDataModel.has_value() || getDataModel(scriptContext) != expectedDataModel.value() ||
!robloxManager->IsDataModelValid(scheduler->GetExecutionDataModel())) {
goto __scriptContext_resumeWaitingThreads__cleanup;
}
}

if (const auto debuggerManager = DebuggerManager::GetSingleton(); !debuggerManager->IsInitialized()) {
const auto dataModel = getDataModel(scriptContext);
const auto globalState = robloxManager->GetGlobalState(scriptContext);
debuggerManager->RegisterCallbackCopy(lua_callbacks(globalState.value()));

if (dataModel->m_bIsOpen)
robloxManager->SetScriptContext(robloxManager->GetDataModelType(dataModel), &scriptContext);
}

// HACK!: We do not want to initialize the scheduler on the
// first resumptions of waiting threads. This will cause
// us to access invalid memory, as the global state is not truly set up yet apparently,
Expand Down Expand Up @@ -741,6 +740,7 @@ void *RobloxManager::GetRobloxFunction(const std::string &functionName) {
if (this->m_mapRobloxFunctions.contains(functionName)) {
return this->m_mapRobloxFunctions[functionName];
}

return nullptr;
}

Expand Down
2 changes: 2 additions & 0 deletions Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ bool Scheduler::ExecuteSchedulerJob(lua_State *runOn, SchedulerJob *job) {

if (robloxManager->GetRobloxTaskDefer().has_value()) {
const auto defer = robloxManager->GetRobloxTaskDefer().value();
logger->PrintInformation(RbxStu::Scheduler, "Scheduling via RBX::ScriptContext::task_defer...");
defer(L);
} else if (robloxManager->GetRobloxTaskSpawn().has_value()) {
const auto spawn = robloxManager->GetRobloxTaskSpawn().value();
logger->PrintInformation(RbxStu::Scheduler, "Scheduling via RBX::ScriptContext::task_spawn...");
spawn(L);
} else {
logger->PrintError(RbxStu::Scheduler,
Expand Down
Loading

0 comments on commit 3c49667

Please sign in to comment.