-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.cpp
More file actions
349 lines (276 loc) · 10.2 KB
/
Copy pathMain.cpp
File metadata and controls
349 lines (276 loc) · 10.2 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <iostream>
#include <exception>
#include <vector>
#include <sstream>
#include <filesystem>
#include <csignal>
#include <OsintgramCXX/App/AppProps.hpp>
#include <OsintgramCXX/App/WineDetect.hpp>
#include <dev_tools/commons/HelpPage.hpp>
#include <dev_tools/commons/Terminal.hpp>
#include <dev_tools/commons/Utils.hpp>
#include <dev_tools/logging/Logger.hpp>
#include <AppShell/Shell.hpp>
#include <IGApi/Session.hpp>
#include "ModInit.hpp"
#include "android/AndroidCA.hpp"
#include "settings/AppSettings.hpp"
#include "shell/ShellCallback.hpp"
#ifdef _WIN32
#include <windows.h>
#elif defined(__linux__)
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <sys/mount.h>
#endif
#if defined(__linux__) && !defined(__ANDROID__)
#include <sys/capability.h>
#include <typeinfo>
#endif
#if defined(__ANDROID__)
#include "android/TermuxCheck.hpp"
#endif
namespace fs = std::filesystem;
using namespace OsintgramCXX;
using namespace DevTools;
using namespace Application;
std::string chrootPath;
std::map<std::string, std::string> defMap;
bool main_shouldReconfigure = false;
void WinSetColorMode() {
#ifdef _WIN32
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) return;
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode)) return;
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
#endif
}
void exceptionHandler() {
if (std::exception_ptr exPtr = std::current_exception()) {
try {
std::rethrow_exception(exPtr);
} catch (const std::exception& ex) {
std::ostringstream sw;
sw << ex.what();
Terminal::println(std::cerr, Terminal::TermColor::RED,
"The application has unexpectedly crashed. Cause of this error:", false);
Terminal::println(std::cerr, Terminal::TermColor::RED, sw.str(), true);
}
} else
Terminal::println(std::cerr, Terminal::TermColor::RED, "Unknown Termination captured", true);
std::raise(SIGSEGV);
}
void usage() {
std::string cmd = ".";
if (fs::path execFile = ExecutableFile(); !execFile.empty()) {
std::string fileName = execFile.filename().string();
cmd = fileName == "java" ? "osintgram4j.jar" : fileName;
}
std::cout << "usage:" << std::endl;
std::cout << (IsAdmin() ? '#' : '$') << " " << cmd << " [options] (target) (target2 ...)" << std::endl << std::endl;
HelpPage hPage;
hPage.setSpaceWidth(3);
hPage.setStartSpaceWidth(4);
hPage.setDescSeparator("=");
hPage.addArg("-h | --help", std::nullopt, "Display usage and its help page");
hPage.addArg("-D[key", "value]", "Puts setting overrides of OsintgramCXX at startup");
hPage.addArg("-E[key", "value]", "Applies an environment variable to app shell");
hPage.addArg("--test-exec-time", std::nullopt, "Tests the execution time (does not start up the Shell");
#ifdef __ANDROID__
hPage.addArg("--reconfigure-info", std::nullopt,
"Reconfigures the Android Device Information and Network Certificate Setup");
#endif
#ifdef __linux__
hPage.addArg("--sandbox", "PATH",
"Isolates the data saved by this tool from the entire disk, increasing security factors");
#endif
std::string str = hPage.display();
str = str.substr(0, str.length() - 1);
std::cout << str << std::endl << std::endl;
std::cout << "where:" << std::endl;
std::cout << " > [options] Application Options" << std::endl;
std::cout << " > (target1) Primary target; optional" << std::endl;
std::cout << " > (target2 ...) Multiple optional targets" << std::endl;
}
#ifdef __linux__
void sigHandle(int) {
if (AppShell& shell = GetShellInstance(); shell.IsRunning())
shell.Stop(true);
}
#endif
void init() {
// don't use loggers now, just initialize them (for now)
Instances::getApplicationLoggingInstance();
Instances::getNetworkLoggingInstance();
Instances::getSecureLoggerInstance();
#ifdef _WIN32
if (Wine::WineExecution()) {
// by default, colors are already messed up, we don't need to attempt anything
Runtime::colorSupportEnabled = false;
} else {
if (HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); hOut != INVALID_HANDLE_VALUE) {
DWORD dwMode = 0;
if (GetConsoleMode(hOut, &dwMode)) {
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hOut, dwMode))
Runtime::colorSupportEnabled = false;
} else
Runtime::colorSupportEnabled = false;
} else
Runtime::colorSupportEnabled = false;
}
#endif
#ifdef __linux__
signal(SIGINT, sigHandle);
signal(SIGTERM, sigHandle);
signal(SIGABRT, sigHandle);
signal(SIGSEGV, sigHandle);
// presumably we ain't in a Terminal (presumable file piping)
if (!isatty(STDOUT_FILENO))
Runtime::colorSupportEnabled = false;
#endif
#ifdef __ANDROID__
if (!IsTermuxRunning()) {
std::cerr << "The Android build of OsintgramCXX requires Termux." << std::endl;
std::cerr << "On your mobile device, install Termux directly from GitHub or from F-Droid." << std::endl;
std::cerr << "Alternatively, you can install Termux from the Google Play Store, ";
std::cerr << "which might have flagged this as false positive due to earlier versions." << std::endl;
exit(1);
}
// TODO: rework CA certificate factory for OsintgramCXX
AndroidVer::prepare_cacerts();
#endif
}
void initSettings() {
}
void parseArgs(const std::vector<std::string>& args) {
AppShell& shell = GetShellInstance();
for (const std::string& arg : args) {
std::string _arg = TrimString(arg);
if (_arg.empty())
continue;
if (_arg[0] == '-') {
if (_arg == "-h" || _arg == "--help") {
usage();
exit(0);
}
if (_arg.rfind("-E", 0) == 0) {
std::string keyValue = arg.substr(2);
size_t eqPos = keyValue.find('=');
if (eqPos == std::string::npos) {
std::cerr << "Invalid format for -E argument (skipping): " << arg << std::endl;
continue;
}
shell.SetEnv(keyValue.substr(0, eqPos), keyValue.substr(eqPos + 1));
}
if (_arg.rfind("-D", 0) == 0) {
std::string keyValue = arg.substr(2);
size_t eqPos = keyValue.find('=');
if (eqPos == std::string::npos) {
std::cerr << "Invalid format for -D argument (skipping): " << arg << std::endl;
continue;
}
Runtime::defMap[keyValue.substr(0, eqPos)] = keyValue.substr(eqPos + 1);
}
if (_arg == "--reconfigure-info")
main_shouldReconfigure = true;
#ifdef __linux__
if (_arg.rfind("--sandbox", 0) == 0) {
size_t eqPos = arg.find('=');
if (eqPos == std::string::npos) {
std::cerr << "Invalid format for --sandbox argument: " << arg << std::endl;
continue;
}
chrootPath = arg.substr(eqPos + 1);
}
#endif
}
}
}
void OsintgramCXX_init() {
std::set_terminate(exceptionHandler);
init();
// required: coloring system in "src/AppCommons/Terminal.cpp" under Windows systems
// damn, this comment above me is so old, it still resolves back, before I changed up the structure to be more modular...
WinSetColorMode();
initSettings();
}
int main(int argc, char** argv) {
OsintgramCXX_init();
if (argc > 1) {
std::vector<std::string> args;
args.reserve(argc - 1);
for (int i = 1; i < argc; i++)
args.emplace_back(argv[i]);
parseArgs(args);
}
std::cout << TEXT_BLOCK() << std::endl << std::endl;
// yes, I'm an asshole, but mainly because of certain warning messages going into the line of the Shell,
// making things ugly in the process
threadSleep(10);
ModLoader_load();
AppShell& shell = GetShellInstance();
OSINT_IncludeShellCallback(shell);
// optional, by the CLI args, enable FS sandboxing under self-app build
#if __linux__
if (!chrootPath.empty()) {
if (!fs::exists(chrootPath)) {
try {
fs::create_directories(chrootPath);
} catch (const fs::filesystem_error& err) {
std::cerr << "Failed to create a directory at " << chrootPath << std::endl;
std::cerr << "Error caused: " << err.what() << std::endl;
return 1;
}
}
if (fs::exists(chrootPath) && !fs::is_directory(chrootPath)) {
std::cerr << "Unable to setup a secure environment (" << chrootPath << " is not a directory)" << std::endl;
return 1;
}
bool shouldChroot = getuid() == 0;
#ifndef __ANDROID__
if (!shouldChroot) {
cap_t caps = cap_get_proc();
if (!caps) {
std::cerr << "Unable to fetch executable capabilities" << std::endl;
return 1;
}
cap_flag_value_t cap_value;
if (cap_get_flag(caps, CAP_SYS_CHROOT, CAP_EFFECTIVE, &cap_value) == -1) {
std::cerr << "Failed to fetch chroot capabilities for the executable" << std::endl;
cap_free(caps);
return 1;
}
shouldChroot = cap_value == CAP_SET;
cap_free(caps);
}
#endif
if (shouldChroot) {
if (chroot(chrootPath.c_str()) != 0) {
std::cerr << "Sandboxing failed, error: " << std::strerror(errno) << std::endl;
return 1;
}
if (chdir("/") != 0) {
std::cerr << "Root Change failed, error: " << std::strerror(errno) << std::endl;
return 1;
}
} else {
std::cerr << "Sandboxing failed: not root";
#ifdef __ANDROID__
std::cerr << ", which requires the process to enable Sandboxing capability" << std::endl;
#else
std::cerr << ", nor given the binary's capability to do so" << std::endl;
#endif
return 1;
}
}
#endif
ModLoader_start();
shell.Start();
threadSleep(10);
shell.Stop();
return 0;
}