-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSecurity.cpp
197 lines (175 loc) · 9.74 KB
/
Security.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// Created by Dottik 16/8/2024.
//
#include "Security.hpp"
#include <cstdint>
#include <cstdio>
#include <lstate.h>
#include "RobloxManager.hpp"
#include "Utilities.hpp"
#include "lstring.h"
// Capability Name, Capability, IsUsingBITTESTQ?
std::unordered_map<std::string, std::pair<std::uint64_t, bool>> allCapabilities = {{"Plugin", {0x1, false}},
{"LocalUser", {0x2, false}},
{"WritePlayer", {0x4, false}},
{"RobloxScript", {0x8, false}},
{"RobloxEngine", {0x10, false}},
{"NotAccessible", {0x20, false}},
{"RunClientScript", {0x8, true}},
{"RunServerScript", {0x9, true}},
{"AccessOutsideWrite", {0xb, true}},
{"Unassigned", {0xf, true}},
{"AssetRequire", {0x10, true}},
{"LoadString", {0x11, true}},
{"ScriptGlobals", {0x12, true}},
{"CreateInstances", {0x13, true}},
{"Basic", {0x14, true}},
{"Audio", {0x15, true}},
{"DataStore", {0x16, true}},
{"Network", {0x17, true}},
{"Physics", {0x18, true}},
{"UI", {0x19, true}},
{"CSG", {0x1a, true}},
{"Chat", {0x1b, true}},
{"Animation", {0x1c, true}},
{"Avatar", {0x1d, true}},
{"Input", {0x1e, true}},
{"Environment", {0x1f, true}},
{"RemoteEvent", {0x20, true}},
{"LegacySound", {0x21, true}},
{"PluginOrOpenCloud", {0x3d, true}},
{"Assistant", {0x3e, true}}};
std::unordered_map<std::int32_t, std::list<std::string>> identityCapabilities = {
{3,
{"RunServerScript", "Plugin", "LocalUser", "RobloxScript", "RunClientScript", "AccessOutsideWrite", "Avatar",
"RemoteEvent", "Environment", "Input", "LegacySound"}},
{2, {"CSG", "Chat", "Animation", "RemoteEvent", "Avatar", "LegacySound"}}, // These are needed for 'require' to work!
{4, {"Plugin", "LocalUser", "RemoteEvent", "Avatar", "LegacySound"}},
{6,
{"RunServerScript", "Plugin", "LocalUser", "Avatar", "RobloxScript", "RunClientScript", "AccessOutsideWrite",
"Input", "Environment", "RemoteEvent", "PluginOrOpenCloud", "LegacySound"}},
{8,
{"Plugin",
"LocalUser",
"WritePlayer",
"RobloxScript",
"RobloxEngine",
"NotAccessible",
"RunClientScript",
"RunServerScript",
"AccessOutsideWrite",
"Unassigned",
"AssetRequire",
"LoadString",
"ScriptGlobals",
"CreateInstances",
"Basic",
"Audio",
"DataStore",
"Network",
"Physics",
"UI",
"CSG",
"Chat",
"Animation",
"Avatar",
"Input",
"Environment",
"RemoteEvent",
"PluginOrOpenCloud",
"Assistant", "LegacySound"}}};
std::shared_ptr<Security> Security::pInstance;
std::shared_ptr<Security> Security::GetSingleton() {
if (Security::pInstance == nullptr)
Security::pInstance = std::make_shared<Security>();
return Security::pInstance;
}
void Security::PrintCapabilities(std::uint32_t capabilities) {
const auto logger = Logger::GetSingleton();
logger->PrintInformation(RbxStu::Security, std::format("0x{:X} got these capabilities:", capabilities));
for (auto capability = allCapabilities.begin(); capability != allCapabilities.end(); ++capability) {
if (capability->second.second == true) {
if ((capabilities) & (1ull << capability->second.first)) {
logger->PrintInformation(RbxStu::Security, capability->first);
}
} else {
if ((capability->second.first & capabilities) == capability->second.first) {
logger->PrintInformation(RbxStu::Security, capability->first);
}
}
}
};
std::uint64_t Security::IdentityToCapabilities(const std::uint32_t identity) {
std::uint64_t capabilities =
0x3FFFFFF00ull | (1ull << 48ull); // Basic capability | Checkcaller check, the capabilities work as flags.
if (const auto capabilitiesForIdentity = identityCapabilities.find(identity);
capabilitiesForIdentity != identityCapabilities.end()) {
for (const auto &capability: capabilitiesForIdentity->second) {
if (auto it = allCapabilities.find(capability); it != allCapabilities.end()) {
if (it->second.second == true) {
capabilities |= (1ull << it->second.first);
continue;
}
capabilities |= it->second.first;
} else {
Logger::GetSingleton()->PrintWarning(
RbxStu::Security, std::format("Couldn't find capability {} in allCapabilities", capability));
}
}
}
return capabilities;
}
void Security::SetThreadSecurity(lua_State *L, std::int32_t identity) {
if (!Utilities::IsPointerValid(static_cast<RBX::Lua::ExtraSpace *>(L->userdata)))
L->global->cb.userthread(L->global->mainthread,
L); // If unallocated, then we must run the callback to create a valid RobloxExtraSpace
auto *plStateUd =
static_cast<RBX::Lua::ExtraSpace *const>(L->userdata); // Const is applied to whats on the left first, if
// there is nothing, it applies to that on the right
const auto capabilities = Security::GetSingleton()->IdentityToCapabilities(identity);
plStateUd->contextInformation = RBX::Security::ExtendedIdentity{identity, 0, nullptr};
plStateUd->capabilities = capabilities;
}
static void set_proto(Proto *proto, std::uint64_t *proto_identity) {
// NEVER FORGET TO SET THE PROTOS and SUB PROTOS USERDATA!!
proto->userdata = static_cast<void *>(proto_identity);
for (auto i = 0; i < proto->sizep; i++) {
set_proto(proto->p[i], proto_identity);
}
}
bool Security::IsOurThread(lua_State *L) {
/// The way we currently have of checking if a thread is our thread is through... capabilities!
/// Roblox handles capabilities using an std::int64_t, giving us 47 fun bits to play around with.
/// This way, we can set the bit 47th, used to describe NOTHING, to set it as
/// our thread. Then we & it to validate it is present on the integer with an AND, which it shouldn't be ever if
/// its anything normal, but we aren't normal!
/// When doing the bit shift, in C++ by default numbers are std::int32_t, we must use std::uint64_t, thus we must
/// post-fix the the number with 'ull'
const auto extraSpace = static_cast<RBX::Lua::ExtraSpace *const>(L->userdata);
const auto logger = Logger::GetSingleton();
const auto passed = (extraSpace->capabilities & (1ull << 48ull)) == (1ull << 48ull);
return passed;
}
bool Security::SetLuaClosureSecurity(Closure *lClosure, std::uint32_t identity) {
if (lClosure->isC)
return false;
const auto pProto = lClosure->l.p;
auto *pMem = pProto->userdata != nullptr ? static_cast<std::uint64_t *>(pProto->userdata)
: static_cast<std::uint64_t *>(malloc(sizeof(std::uint64_t)));
*pMem = Security::GetSingleton()->IdentityToCapabilities(identity);
set_proto(pProto, pMem);
return true;
}
void walk_proto_wipe(lua_State *const L, Proto *const proto) {
proto->debugname = nullptr;
proto->source = luaS_new(L, "");
proto->linedefined = -1;
for (int i = 0; i < proto->sizep; i++) {
walk_proto_wipe(L, proto->p[i]);
}
}
void Security::WipeClosurePrototype(lua_State *L, const Closure *lClosure) {
if (lClosure->isC)
return;
walk_proto_wipe(L, lClosure->l.p);
}