-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
526 lines (453 loc) · 21.2 KB
/
main.cpp
File metadata and controls
526 lines (453 loc) · 21.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>
#include "modules/Kernel.hpp"
#include "modules/HostsBlocker.hpp"
#include "modules/FileIntegrity.hpp"
#include "modules/DNS.hpp"
#include "modules/Optimization.hpp"
#include "modules/Cleanup.hpp"
#include "modules/SSH.hpp"
#include "modules/SELinux.hpp"
#include "modules/AppArmor.hpp"
#include "modules/Namespaces.hpp"
#include <string>
#include <vector>
#include <iostream>
#include <syslog.h>
using namespace ftxui;
struct Flag { bool on = false; };
static int runIntegrityCheck() {
if (!FileIntegrity::hasBaseline())
return 0;
auto results = FileIntegrity::check();
bool alert = false;
openlog("spp-integrity", LOG_PID, LOG_DAEMON);
for (const auto& r : results) {
if (r.status == IntegrityResult::MODIFIED) {
syslog(LOG_WARNING, "MODIFIE : %s", r.path.c_str());
std::cerr << "[SPP] MODIFIE : " << r.path << '\n';
alert = true;
} else if (r.status == IntegrityResult::MISSING) {
syslog(LOG_CRIT, "ABSENT : %s", r.path.c_str());
std::cerr << "[SPP] ABSENT : " << r.path << '\n';
alert = true;
}
}
closelog();
return alert ? 1 : 0;
}
int main(int argc, char* argv[]) {
if (argc > 1 && std::string(argv[1]) == "--check")
return runIntegrityCheck();
auto screen = ScreenInteractive::Fullscreen();
auto kernelOpts = KernelSecurity::kernelOptions();
auto fsOpts = KernelSecurity::fsOptions();
auto netOpts = KernelSecurity::netOptions();
std::vector<Flag> kernelState(kernelOpts.size());
std::vector<Flag> fsState(fsOpts.size());
std::vector<Flag> netState(netOpts.size());
for (size_t i = 0; i < kernelOpts.size(); ++i)
kernelState[i].on = KernelSecurity::isHardened(kernelOpts[i]);
for (size_t i = 0; i < fsOpts.size(); ++i)
fsState[i].on = KernelSecurity::isHardened(fsOpts[i]);
for (size_t i = 0; i < netOpts.size(); ++i)
netState[i].on = KernelSecurity::isHardened(netOpts[i]);
auto makeOption = [](const std::string& name, const std::string& detail, bool* state) -> Component {
auto cb = Checkbox(name, state);
return Renderer(cb, [cb, detail] {
return vbox({
cb->Render(),
hbox({ text(" "), paragraph(detail) | color(Color::GrayDark) }),
text(""),
});
});
};
Components kernelComps, fsComps, netComps;
for (size_t i = 0; i < kernelOpts.size(); ++i)
kernelComps.push_back(makeOption(kernelOpts[i].name, kernelOpts[i].detail, &kernelState[i].on));
for (size_t i = 0; i < fsOpts.size(); ++i)
fsComps.push_back(makeOption(fsOpts[i].name, fsOpts[i].detail, &fsState[i].on));
for (size_t i = 0; i < netOpts.size(); ++i)
netComps.push_back(makeOption(netOpts[i].name, netOpts[i].detail, &netState[i].on));
auto optionsContainer = Container::Vertical({});
for (auto& c : kernelComps) optionsContainer->Add(c);
for (auto& c : fsComps) optionsContainer->Add(c);
for (auto& c : netComps) optionsContainer->Add(c);
std::vector<std::pair<std::string,std::string>> dnsOpts = {
{"DNSSEC",
"Verifie la signature cryptographique des reponses DNS."
" Bloque le DNS spoofing et les attaques de type Kaminsky."},
{"DNS over TLS",
"Chiffre les requetes DNS via TLS. Empeche le FAI et les"
" intermediaires de lire ou alterer vos resolutions DNS."},
};
std::vector<Flag> dnsState(dnsOpts.size());
dnsState[0].on = DNSSecurity::isDNSSECEnabled();
dnsState[1].on = DNSSecurity::isDNSOverTLSEnabled();
Components dnsComps;
for (size_t i = 0; i < dnsOpts.size(); ++i)
dnsComps.push_back(makeOption(dnsOpts[i].first, dnsOpts[i].second, &dnsState[i].on));
for (auto& c : dnsComps) optionsContainer->Add(c);
auto memOpts = Optimization::memoryOptions();
auto netPerfOpts = Optimization::networkOptions();
std::vector<Flag> memOptState(memOpts.size());
std::vector<Flag> netPerfState(netPerfOpts.size());
for (size_t i = 0; i < memOpts.size(); ++i)
memOptState[i].on = KernelSecurity::isHardened(memOpts[i]);
for (size_t i = 0; i < netPerfOpts.size(); ++i)
netPerfState[i].on = KernelSecurity::isHardened(netPerfOpts[i]);
Components memOptComps, netPerfComps;
for (size_t i = 0; i < memOpts.size(); ++i)
memOptComps.push_back(makeOption(memOpts[i].name, memOpts[i].detail, &memOptState[i].on));
for (size_t i = 0; i < netPerfOpts.size(); ++i)
netPerfComps.push_back(makeOption(netPerfOpts[i].name, netPerfOpts[i].detail, &netPerfState[i].on));
for (auto& c : memOptComps) optionsContainer->Add(c);
for (auto& c : netPerfComps) optionsContainer->Add(c);
bool sshRootDisabled = SSHSecurity::isRootLoginDisabled();
auto sshComp = makeOption(
"Bloquer les connexions root en SSH",
"Interdit la connexion directe en root via SSH.",
&sshRootDisabled
);
optionsContainer->Add(sshComp);
auto nsOpts = NamespacesSecurity::options();
std::vector<Flag> nsState(nsOpts.size());
for (size_t i = 0; i < nsOpts.size(); ++i)
nsState[i].on = NamespacesSecurity::isHardened(nsOpts[i]);
Components nsComps;
for (size_t i = 0; i < nsOpts.size(); ++i)
nsComps.push_back(makeOption(nsOpts[i].name, nsOpts[i].detail, &nsState[i].on));
for (auto& c : nsComps) optionsContainer->Add(c);
bool selinuxPresent = SELinuxSecurity::isInstalled();
bool aaPresent = AppArmorSecurity::isInstalled();
auto selinuxModeOpts = selinuxPresent ? SELinuxSecurity::modeOptions() : std::vector<SELinuxOption>{};
auto selinuxBoolOpts = selinuxPresent ? SELinuxSecurity::booleanOptions() : std::vector<SELinuxOption>{};
std::vector<Flag> selinuxModeState(selinuxModeOpts.size());
std::vector<Flag> selinuxBoolState(selinuxBoolOpts.size());
for (size_t i = 0; i < selinuxModeOpts.size(); ++i)
selinuxModeState[i].on = SELinuxSecurity::isHardened(selinuxModeOpts[i]);
for (size_t i = 0; i < selinuxBoolOpts.size(); ++i)
selinuxBoolState[i].on = SELinuxSecurity::isHardened(selinuxBoolOpts[i]);
Components selinuxModeComps, selinuxBoolComps;
for (size_t i = 0; i < selinuxModeOpts.size(); ++i)
selinuxModeComps.push_back(makeOption(selinuxModeOpts[i].name, selinuxModeOpts[i].detail, &selinuxModeState[i].on));
for (size_t i = 0; i < selinuxBoolOpts.size(); ++i)
selinuxBoolComps.push_back(makeOption(selinuxBoolOpts[i].name, selinuxBoolOpts[i].detail, &selinuxBoolState[i].on));
auto aaEnforceOpts = aaPresent ? AppArmorSecurity::enforcementOptions() : std::vector<AppArmorOption>{};
auto aaProfileOpts = aaPresent ? AppArmorSecurity::profileOptions() : std::vector<AppArmorOption>{};
std::vector<Flag> aaEnforceState(aaEnforceOpts.size());
std::vector<Flag> aaProfileState(aaProfileOpts.size());
for (size_t i = 0; i < aaEnforceOpts.size(); ++i)
aaEnforceState[i].on = AppArmorSecurity::isHardened(aaEnforceOpts[i]);
for (size_t i = 0; i < aaProfileOpts.size(); ++i)
aaProfileState[i].on = AppArmorSecurity::isHardened(aaProfileOpts[i]);
Components aaEnforceComps, aaProfileComps;
for (size_t i = 0; i < aaEnforceOpts.size(); ++i)
aaEnforceComps.push_back(makeOption(aaEnforceOpts[i].name, aaEnforceOpts[i].detail, &aaEnforceState[i].on));
for (size_t i = 0; i < aaProfileOpts.size(); ++i)
aaProfileComps.push_back(makeOption(aaProfileOpts[i].name, aaProfileOpts[i].detail, &aaProfileState[i].on));
int trackerLevel = (int)HostsBlocker::currentLevel();
std::vector<std::string> trackerEntries = {
"Aucune (par defaut)",
"Minimum — 19 domaines",
"Basique — 79 domaines",
"Hard — 156 domaines",
};
auto trackerRadio = Radiobox(&trackerEntries, &trackerLevel);
bool serviceEnabled = FileIntegrity::isServiceEnabled();
std::vector<IntegrityResult> integrityResults;
std::string integrityMsg;
bool integrityIsError = false;
bool baselineTampered = false;
auto serviceCheckbox = Checkbox("Activer au demarrage", &serviceEnabled);
auto baselineBtn = Button("[ Creer baseline ]", [&] {
bool ok = FileIntegrity::generateBaseline();
integrityIsError = !ok;
baselineTampered = false;
integrityMsg = ok ? "Baseline creee et signee dans /var/lib/spp/"
: "Echec — lancez en sudo";
if (ok) integrityResults = FileIntegrity::check();
});
auto verifyBtn = Button("[ Verifier ]", [&] {
if (!FileIntegrity::hasBaseline()) {
integrityIsError = true;
integrityMsg = "Aucune baseline — creez-en une d'abord";
return;
}
baselineTampered = FileIntegrity::isBaselineTampered();
if (baselineTampered) {
integrityIsError = true;
integrityMsg = "ALERTE : La baseline a ete falsifiee !";
integrityResults = FileIntegrity::check();
return;
}
integrityResults = FileIntegrity::check();
bool anyBad = false;
for (const auto& r : integrityResults)
if (r.status != IntegrityResult::OK) { anyBad = true; break; }
integrityIsError = anyBad;
integrityMsg = anyBad ? "Anomalies detectees !" : "Tous les fichiers sont integres";
});
std::string statusMsg;
bool statusIsError = false;
auto applyBtn = Button("[ Appliquer ]", [&] {
int ok = 0, fail = 0;
auto applyGroup = [&](const std::vector<SysctlOption>& opts,
const std::vector<Flag>& states) {
for (size_t i = 0; i < opts.size(); ++i) {
bool success = states[i].on
? KernelSecurity::apply(opts[i])
: KernelSecurity::revert(opts[i]);
success ? ++ok : ++fail;
}
};
applyGroup(kernelOpts, kernelState);
applyGroup(fsOpts, fsState);
applyGroup(netOpts, netState);
applyGroup(memOpts, memOptState);
applyGroup(netPerfOpts, netPerfState);
for (size_t i = 0; i < nsOpts.size(); ++i) {
bool success = nsState[i].on
? NamespacesSecurity::apply(nsOpts[i])
: NamespacesSecurity::revert(nsOpts[i]);
success ? ++ok : ++fail;
}
// Persistance sysctl kernel : /etc/sysctl.d/99-spp.conf
std::vector<std::pair<std::string,std::string>> persistEntries;
auto collect = [&](const std::vector<SysctlOption>& opts, const std::vector<Flag>& states) {
for (size_t i = 0; i < opts.size(); ++i)
if (states[i].on)
persistEntries.push_back({ opts[i].key, opts[i].hardened });
};
collect(kernelOpts, kernelState);
collect(fsOpts, fsState);
collect(netOpts, netState);
collect(memOpts, memOptState);
collect(netPerfOpts, netPerfState);
KernelSecurity::writePersistenceConf(persistEntries) ? ++ok : ++fail;
// Persistance namespaces : /etc/sysctl.d/99-spp-ns.conf
std::vector<std::pair<std::string,std::string>> nsPersist;
for (size_t i = 0; i < nsOpts.size(); ++i)
if (nsState[i].on)
nsPersist.push_back({ nsOpts[i].key, nsOpts[i].hardened });
NamespacesSecurity::writePersistenceConf(nsPersist) ? ++ok : ++fail;
DNSSecurity::applyDNSSEC(dnsState[0].on) ? ++ok : ++fail;
DNSSecurity::applyDNSOverTLS(dnsState[1].on) ? ++ok : ++fail;
SSHSecurity::applyDisableRootLogin(sshRootDisabled) ? ++ok : ++fail;
auto applySelinux = [&](const std::vector<SELinuxOption>& opts,
const std::vector<Flag>& states) {
for (size_t i = 0; i < opts.size(); ++i) {
bool success = states[i].on
? SELinuxSecurity::apply(opts[i])
: SELinuxSecurity::revert(opts[i]);
success ? ++ok : ++fail;
}
};
applySelinux(selinuxModeOpts, selinuxModeState);
applySelinux(selinuxBoolOpts, selinuxBoolState);
auto applyAppArmor = [&](const std::vector<AppArmorOption>& opts,
const std::vector<Flag>& states) {
for (size_t i = 0; i < opts.size(); ++i) {
bool success = states[i].on
? AppArmorSecurity::apply(opts[i])
: AppArmorSecurity::revert(opts[i]);
success ? ++ok : ++fail;
}
};
applyAppArmor(aaEnforceOpts, aaEnforceState);
applyAppArmor(aaProfileOpts, aaProfileState);
bool hostsOk = HostsBlocker::apply((HostsBlocker::Level)trackerLevel);
hostsOk ? ++ok : ++fail;
if (serviceEnabled) {
FileIntegrity::installServiceFile();
FileIntegrity::enableService() ? ++ok : ++fail;
} else {
FileIntegrity::disableService() ? ++ok : ++fail;
}
statusIsError = (fail > 0);
if (fail == 0)
statusMsg = " OK — " + std::to_string(ok) + " changements appliques";
else
statusMsg = " " + std::to_string(ok) + " OK | " +
std::to_string(fail) + " echec (lancez en sudo)";
});
auto cleanupBtn = Button("[ Supprimer SPP ]", [&] {
auto r = Cleanup::removeAllTraces();
statusIsError = (r.fail > 0);
if (r.fail == 0)
statusMsg = " Toutes les traces SPP ont ete supprimees";
else
statusMsg = " " + std::to_string(r.ok) + " OK | " +
std::to_string(r.fail) + " echec (lancez en sudo)";
// Reflete la suppression dans l'interface
trackerLevel = (int)HostsBlocker::NONE;
serviceEnabled = false;
baselineTampered = false;
integrityResults.clear();
integrityMsg.clear();
for (size_t i = 0; i < selinuxModeState.size(); ++i)
selinuxModeState[i].on = SELinuxSecurity::isHardened(selinuxModeOpts[i]);
for (size_t i = 0; i < selinuxBoolState.size(); ++i)
selinuxBoolState[i].on = SELinuxSecurity::isHardened(selinuxBoolOpts[i]);
for (size_t i = 0; i < aaEnforceState.size(); ++i)
aaEnforceState[i].on = AppArmorSecurity::isHardened(aaEnforceOpts[i]);
for (size_t i = 0; i < aaProfileState.size(); ++i)
aaProfileState[i].on = AppArmorSecurity::isHardened(aaProfileOpts[i]);
});
auto quitBtn = Button("[ Quitter ]", screen.ExitLoopClosure());
auto rightContainer = Container::Vertical({
trackerRadio,
serviceCheckbox,
baselineBtn,
verifyBtn,
});
for (auto& c : selinuxModeComps) optionsContainer->Add(c);
for (auto& c : selinuxBoolComps) optionsContainer->Add(c);
for (auto& c : aaEnforceComps) optionsContainer->Add(c);
for (auto& c : aaProfileComps) optionsContainer->Add(c);
auto layout = Container::Vertical({
Container::Horizontal({
optionsContainer,
rightContainer,
}),
Container::Horizontal({applyBtn, cleanupBtn, quitBtn}),
});
auto renderer = Renderer(layout, [&] {
auto sectionHeader = [](const std::string& title, Color c) -> Element {
return hbox({
text(" " + title + " ") | bold | color(c),
separator() | color(c),
});
};
std::vector<Element> optLines;
optLines.push_back(sectionHeader("KERNEL", Color::Yellow));
for (auto& c : kernelComps) optLines.push_back(c->Render());
optLines.push_back(separator());
optLines.push_back(sectionHeader("FILESYSTEM", Color::Yellow));
for (auto& c : fsComps) optLines.push_back(c->Render());
optLines.push_back(separator());
optLines.push_back(sectionHeader("RESEAU", Color::Yellow));
for (auto& c : netComps) optLines.push_back(c->Render());
optLines.push_back(separator());
optLines.push_back(sectionHeader("DNS", Color::Cyan));
for (auto& c : dnsComps) optLines.push_back(c->Render());
optLines.push_back(separator());
optLines.push_back(sectionHeader("MEMOIRE", Color::Green));
for (auto& c : memOptComps) optLines.push_back(c->Render());
optLines.push_back(separator());
optLines.push_back(sectionHeader("PERF RESEAU", Color::Green));
for (auto& c : netPerfComps) optLines.push_back(c->Render());
optLines.push_back(separator());
optLines.push_back(sectionHeader("SSH", Color::Red));
optLines.push_back(sshComp->Render());
optLines.push_back(separator());
optLines.push_back(sectionHeader("NAMESPACES", Color::Magenta));
for (auto& c : nsComps) optLines.push_back(c->Render());
if (selinuxPresent) {
optLines.push_back(separator());
optLines.push_back(sectionHeader("SELINUX", Color::Blue));
for (auto& c : selinuxModeComps) optLines.push_back(c->Render());
for (auto& c : selinuxBoolComps) optLines.push_back(c->Render());
}
if (aaPresent) {
optLines.push_back(separator());
optLines.push_back(sectionHeader("APPARMOR", Color::Blue));
for (auto& c : aaEnforceComps) optLines.push_back(c->Render());
for (auto& c : aaProfileComps) optLines.push_back(c->Render());
}
auto trackerPanel = vbox({
sectionHeader("BLOQUEUR DE TRACKERS", Color::Cyan),
text(""),
trackerRadio->Render(),
text(""),
separator(),
paragraph(HostsBlocker::levelDescription(
(HostsBlocker::Level)trackerLevel
)) | color(Color::GrayDark),
filler(),
separator(),
text(" Modifie /etc/hosts") | color(Color::Yellow) | dim,
text(" Contenu original preserve") | color(Color::GrayDark) | dim,
text(" Requiert sudo") | color(Color::GrayDark) | dim,
});
std::vector<Element> intLines;
intLines.push_back(sectionHeader("INTEGRITE FICHIERS", Color::Magenta));
intLines.push_back(text(""));
intLines.push_back(serviceCheckbox->Render());
intLines.push_back(text(""));
intLines.push_back(baselineBtn->Render());
intLines.push_back(verifyBtn->Render());
if (!integrityResults.empty()) {
intLines.push_back(separator());
if (baselineTampered) {
intLines.push_back(
text(" BASELINE FALSIFIEE — RESULTATS NON FIABLES") | color(Color::Red) | bold
);
intLines.push_back(separator());
}
for (const auto& r : integrityResults) {
std::string mark;
Color col;
if (baselineTampered) {
mark = " ??? ";
col = Color::Red;
} else {
switch (r.status) {
case IntegrityResult::OK: mark = " OK "; col = Color::Green; break;
case IntegrityResult::MODIFIED: mark = " MOD "; col = Color::Red; break;
case IntegrityResult::MISSING: mark = " ABS "; col = Color::Red; break;
case IntegrityResult::NO_BASELINE: mark = " ? "; col = Color::Yellow; break;
}
}
intLines.push_back(hbox({
text(mark) | color(col) | bold,
text(r.label) | color(col),
}));
}
}
if (!integrityMsg.empty()) {
intLines.push_back(separator());
intLines.push_back(
text(" " + integrityMsg)
| color(integrityIsError ? Color::Red : Color::Green)
);
}
intLines.push_back(filler());
intLines.push_back(separator());
intLines.push_back(text(" Baseline : /var/lib/spp/") | color(Color::GrayDark) | dim);
auto integrityPanel = vbox(intLines);
Element status;
if (statusMsg.empty()) {
status = text(" Cochees = activer | Decochees = desactiver — puis Appliquer")
| color(Color::GrayDark);
} else {
status = text(statusMsg)
| (statusIsError ? color(Color::Red) : color(Color::Green));
}
return vbox({
hbox({
text(" SPP") | bold | color(Color::Red),
text(" — Systeme de Protection Patriote ") | bold,
}) | border,
hbox({
vbox(optLines) | vscroll_indicator | frame | flex | border,
vbox({
trackerPanel | border | flex,
integrityPanel | border,
}) | size(WIDTH, EQUAL, 38),
}) | flex,
hbox({
status | flex,
applyBtn->Render(),
text(" "),
cleanupBtn->Render() | color(Color::Red),
text(" "),
quitBtn->Render(),
text(" "),
}) | border,
});
});
screen.Loop(renderer);
return 0;
}