Repository navigation
Expand file tree
/
Copy pathconfig.cpp
More file actions
179 lines (147 loc) · 4.59 KB
/
Copy pathconfig.cpp
File metadata and controls
179 lines (147 loc) · 4.59 KB
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
#include "../include/config.hpp"
#include <fstream>
#include <nlohmann/json.hpp>
#include <windows.h>
cewrapper::Config _main_config;
cewrapper::Config &cewrapper::Config::get()
{
return _main_config;
}
inline int svtoi(const std::wstring_view sv)
{
// std::from_chars() doesn't seem to have a wchar version, so I guess this will have to do
const std::wstring temp(sv.data());
return std::stoi(temp);
}
void cewrapper::Config::initFromArguments(int argc, wchar_t *argv[])
{
int arg_idx = 1;
this->home_set = false;
while (arg_idx < argc)
{
std::wstring_view arg = argv[arg_idx];
if (arg.starts_with(L"--time_limit="))
{
this->time_limit_ms = svtoi(arg.substr(13)) * 1000;
arg_idx++;
}
else if (arg.compare(L"-vv") == 0)
{
this->debugging = true;
this->extra_debugging = true;
arg_idx++;
}
else if (arg.compare(L"-v") == 0)
{
this->debugging = true;
arg_idx++;
}
else if (arg.compare(L"--summary") == 0)
{
this->summary = true;
arg_idx++;
}
else if (arg.starts_with(L"--config="))
{
this->loadFromFile(arg.substr(9));
arg_idx++;
}
else if (arg.starts_with(L"--home="))
{
this->home = arg.substr(7);
this->home_set = true;
arg_idx++;
}
else if (arg.compare(L"--suspend") == 0)
{
this->suspend_after_start = true;
arg_idx++;
}
else if (arg.compare(L"--wait") == 0)
{
this->wait_before_spawn = true;
arg_idx++;
}
else if (arg.compare(L"--prepare-nul") == 0)
{
this->prepare_nul = true;
arg_idx++;
}
else
{
break;
}
}
this->args.clear();
// --prepare-nul is a standalone maintenance mode, everything else needs an executable to run
if (arg_idx >= argc)
{
if (this->prepare_nul)
return;
throw std::exception("No executable given");
}
this->progid = argv[arg_idx];
arg_idx += 1;
for (; arg_idx < argc; ++arg_idx)
this->args.push_back(argv[arg_idx]);
}
std::wstring utf8str_to_wstr(std::string_view utf8str)
{
size_t buffer_size = (utf8str.length() + 1) * sizeof(wchar_t);
wchar_t *buffer = static_cast<wchar_t *>(malloc(buffer_size));
if (buffer == nullptr)
throw std::exception("Not enough memory to allocate a buffer");
memset(buffer, 0, buffer_size);
int convertedChars = MultiByteToWideChar(CP_UTF8, 0, utf8str.data(), static_cast<int>(utf8str.length()),
reinterpret_cast<wchar_t *>(buffer), static_cast<int>(utf8str.length()));
if (convertedChars <= 0)
throw std::exception("Could not convert utf8 string");
std::wstring newstr(buffer, convertedChars);
free(buffer);
return newstr;
}
void cewrapper::Config::loadFromFile(const std::wstring_view file)
{
using json = nlohmann::json;
std::ifstream jsonfile(file.data());
json data = json::parse(jsonfile);
this->use_appcontainer = data.value("use_appcontainer", true);
this->mem_max = data.value("mem_max", 0);
this->pids_max = data.value("pids_max", 0);
for (auto &dir : data["allowed_paths"])
{
uint32_t rights{ GENERIC_READ };
if (dir.value("rw", false))
rights |= GENERIC_WRITE;
if (!dir.value("noexec", false))
rights |= GENERIC_EXECUTE;
std::string path = dir["path"];
if (path.empty())
continue;
if (path.length() >= MAXINT)
continue;
this->allowed_dirs.push_back({ .path = utf8str_to_wstr(path), .rights = rights });
}
for (auto ® : data["allowed_registry"])
{
uint32_t rights{ GENERIC_READ };
if (reg.value("rw", false))
rights = GENERIC_ALL;
registry_type_t regtype{};
std::string jsregtype = reg.value("type", "normal");
if (jsregtype.compare("wow6464"))
{
regtype = registry_type_t::wow6464;
}
else if (jsregtype.compare("wow6432"))
{
regtype = registry_type_t::wow6432;
}
std::string path = reg["path"];
if (path.empty())
continue;
if (path.length() >= MAXINT)
continue;
this->allowed_registry.push_back({ .path = utf8str_to_wstr(path), .rights = rights, .type = regtype });
}
}