-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfirewall.cpp
More file actions
558 lines (453 loc) · 18.5 KB
/
firewall.cpp
File metadata and controls
558 lines (453 loc) · 18.5 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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#include "firewall.h"
int CFirewall::StartFirewall() {
while (true) {
int nOption =0;
std::cout << "\nSelect Firewall Option \n\n"
<< "1. Run Firewall \n"
<< "2. Configure Firewall \n"
<< "3. View Logs \n\n"
<< "Please enter the option: ";
std::string strInput;
std::getline(std::cin, strInput);
std::cout << std::endl;
if (isValidNumber(strInput)) {
nOption = std::stoi(strInput);
}
switch (nOption) {
case 1:
RunFirewall();
break;
case 2:
ConfigureFirewall();
break;
case 3:
ViewLogs();
break;
default:
std::cerr << "Error: " << GetErrorMessage(ERROR_INVALID_OPTION) << std::endl;
exit(ERROR_INVALID_OPTION);
break;
}
}
return SUCCESS_CODE;
}
int CFirewall::RunFirewall() {
if (!FirewallConfig::Instance().Load(FIREWALL_INI_FILE)) {
std::cerr << "Failed to load firewall rules in RunFirewall\n";
return ERROR_INVALID_FUNCTION;
}
signal(SIGINT, handleExit);
signal(SIGTERM, handleExit);
DefaultRuleSet();
auto& vecIniData = FirewallConfig::Instance().GetIniData();
for (auto& stId : vecIniData) {
std::vector<std::string> vecIniList;
for (auto& stIdSecond : stId.second) {
vecIniList.push_back(stIdSecond.second);
}
RunIptables(vecIniList[DIRECTION], vecIniList[IP], vecIniList[PORT], vecIniList[ACTION]);
}
std::string logfile="/var/log/syslog";
std::string outputfile="./logs/firewall/$(date +%y%m%d).log";
std::string command = "tail -n 0 -F " + logfile + " | grep --line-buffered -E \"BLOCK|ALLOW\" | tee -a " + outputfile;
CFirewall::ExecCommand(command);
return SUCCESS_CODE;
}
int CFirewall::ConfigureFirewall() {
if (!FirewallConfig::Instance().Load(FIREWALL_INI_FILE)) {
std::cerr << "Failed to load firewall rules in Configure\n";
return ERROR_INVALID_FUNCTION;
}
int nStatusCode = SUCCESS_CODE;
while (true) {
std::string strInput;
std::vector<std::string> vecWords;
PrintConfigManual();
std::getline(std::cin, strInput);
std::cout << std::endl;
if (strInput.empty()) {
std::cerr << "Input cannot be only spaces." << std::endl;
return ERROR_INVALID_INPUT;
}
vecWords = ConfigureUserInput(strInput);
if (isValidInput(vecWords) != SUCCESS_CODE) {
continue;
}
std::string strCmd = vecWords[COMMAND];
if (strCmd == "a") {
nStatusCode = AddRule(vecWords);
} else if (strCmd == "u") {
nStatusCode = UpdateRule(vecWords);
} else if (strCmd == "d") {
nStatusCode = DeleteRule(vecWords);
} else if (strCmd == "l") {
nStatusCode = RuleList();
} else if (strCmd == "help") {
nStatusCode = PrintFirewallHelp();
} else if (strCmd == "exit") {
nStatusCode = EXIT_CONFIG;
break;
}
if (nStatusCode != SUCCESS_CODE) {
break;
}
}
return nStatusCode;
}
int CFirewall::ViewLogs() {
std::vector<std::string> vecFilesPath;
int nNumber;
int nCnt = 1;
VariadicTable<int, std::string> vt({"No", "File Name"}, 10);
if (std::filesystem::exists(LOG_FILE_PATH)) {
for (const auto& entry : std::filesystem::directory_iterator(LOG_FILE_PATH)) {
if (std::filesystem::is_regular_file(entry.status())) {
vecFilesPath.push_back(entry.path().string());
}
}
}
else {
std::cerr << "ERROR: Cannot open file" << std::endl;
return ERROR_CANNOT_OPEN_FILE;
}
std::sort(vecFilesPath.begin(), vecFilesPath.end());
for(std::string strFilePath : vecFilesPath) {
size_t siLastSlash = strFilePath.find_last_of('/');
std::string strFileName = strFilePath.substr(siLastSlash + 1);
vt.addRow(nCnt, strFileName);
nCnt++;
}
vt.print(std::cout);
std::cout << "\nPlease enter the number of the log file to read" << std::endl;
std::cout << "NUMBER: ";
std::string strInput;
std::getline(std::cin, strInput);
if (isValidNumber(strInput)) {
nNumber = std::stoi(strInput);
}
else {
PrintInputError(strInput);
return ERROR_INVALID_INPUT;
}
if (nNumber < 1 || nNumber > vecFilesPath.size()) {
PrintError("Invalid number");
return ERROR_INVALID_INPUT;
}
std::string strCmd = "more " + vecFilesPath[nNumber - 1];
system(strCmd.c_str());
return SUCCESS_CODE;
}
int CFirewall::RunIptables(const std::string& strDirection, const std::string& strIp, const std::string& strPort, const std::string& strAction) {
std::string strIptablesCmd = "iptables -A";
std::string strIptablesLogCmd = "";
std::string strErrCommand = " 2> /dev/null";
if (strDirection == "INPUT") {
strIptablesCmd += " INPUT ";
strIptablesCmd += strIp == "ANY" ? "" : "-s " + strIp;
} else if (strDirection == "OUTPUT") {
strIptablesCmd += " OUTPUT ";
strIptablesCmd += strIp == "ANY" ? "" : "-d " + strIp;
} else {
std::cerr << "Invalid Direction" << std::endl;
return ERROR_INVALID_OPTION;
}
strIptablesCmd += strPort == "ANY" ? "" : " -p tcp --dport " + strPort;
if (strAction == "DROP") {
strIptablesLogCmd = strIptablesCmd + " -j LOG --log-prefix \"BLOCK \"";
strIptablesCmd += " -j DROP";
} else if (strAction == "ACCEPT") {
strIptablesLogCmd = strIptablesCmd + " -j LOG --log-prefix \"ALLOW \"";
strIptablesCmd += " -j ACCEPT";
} else {
std::cerr << "Invalid Action" << std::endl;
return ERROR_INVALID_OPTION;
}
std::cout << strIptablesCmd << std::endl;
CFirewall::ExecCommand(strIptablesLogCmd + strErrCommand);
CFirewall::ExecCommand(strIptablesCmd + strErrCommand);
return SUCCESS_CODE;
}
std::vector<std::string> CFirewall::ConfigureUserInput(std::string& strInput) {
std::istringstream iss(strInput);
std::vector<std::string> vecWords;
std::string strWord;
while (iss >> strWord) {
std::transform(strWord.begin(), strWord.end(), strWord.begin(), ::tolower);
strWord = (strWord == "add") ? "a" : (strWord == "update") ? "u" : (strWord == "delete") ? "d" : (strWord == "list") ? "l" : strWord;
strWord = (strWord == "x" || strWord == "drop") ? "DROP" : (strWord == "o" || strWord == "accept") ? "ACCEPT" : strWord;
strWord = (strWord == "to" || strWord == "output") ? "OUTPUT" : (strWord == "from" || strWord == "input") ? "INPUT" : strWord;
strWord = (strWord == "any") ? "ANY" : strWord;
vecWords.push_back(strWord);
}
if (vecWords[COMMAND] == "a") {
if (vecWords.size() == ADD_MIN_LENGTH) {
if (isValidIP(vecWords[ADD_IP])) {
vecWords.emplace(vecWords.begin() + ADD_PORT, "ANY");
} else if (isValidPort(vecWords[ADD_IP])) {
vecWords.emplace(vecWords.begin() + ADD_IP, "ANY");
}
}
}
return vecWords;
}
int CFirewall::isValidInput(std::vector<std::string>& vecWords) {
if (!FirewallConfig::Instance().Load(FIREWALL_INI_FILE)) {
std::cerr << "Failed to load firewall rules in Configure\n";
return ERROR_INVALID_FUNCTION;
}
auto& vecIniData = FirewallConfig::Instance().GetIniData();
std::vector<std::string> vecDirectionWords = {"INPUT", "OUTPUT"};
std::vector<std::string> vecActionWords = {"DROP", "ACCEPT"};
std::string strCommand = vecWords[COMMAND];
if (strCommand == "a") {
if (!(vecWords.size() == ADD_MIN_LENGTH || vecWords.size() == ADD_MAX_LENGTH)) {
std::cerr << "Invalid length input." << std::endl;
return ERROR_INVALID_INPUT;
}
if (std::find(vecDirectionWords.begin(), vecDirectionWords.end(), vecWords[ADD_DIRECTION]) == vecDirectionWords.end()) {
PrintInputError(vecWords[ADD_DIRECTION]);
return ERROR_INVALID_INPUT;
}
if (!isValidIP(vecWords[ADD_IP])) {
PrintInputError(vecWords[ADD_IP]);
return ERROR_INVALID_INPUT;
}
if (!isValidPort(vecWords[ADD_PORT])) {
PrintInputError(vecWords[ADD_PORT]);
return ERROR_INVALID_INPUT;
}
if (std::find(vecActionWords.begin(), vecActionWords.end(), vecWords[ADD_ACTION]) == vecActionWords.end()) {
PrintInputError(vecWords[ADD_ACTION]);
return ERROR_INVALID_INPUT;
}
return SUCCESS_CODE;
} else if (strCommand == "u") {
if (vecWords.size() != UPDATE_LENGTH) {
std::cerr << "Invalid length input." << std::endl;
return ERROR_INVALID_INPUT;
}
if (!isValidNumber(vecWords[UPDATE_NUMBER])) {
PrintInputError(vecWords[UPDATE_NUMBER]);
return ERROR_INVALID_INPUT;
}
int nUpdateNum = std::stoi(vecWords[UPDATE_NUMBER]);
if (vecIniData.size() < nUpdateNum || nUpdateNum < 1) {
PrintInputError(vecWords[UPDATE_NUMBER]);
return ERROR_INVALID_INPUT;
}
if (vecWords[UPDATE_REDIRECTION] != ">") {
PrintInputError(vecWords[UPDATE_REDIRECTION]);
return ERROR_INVALID_INPUT;
}
if (vecWords[UPDATE_OPTION] == "direction") {
if (std::find(vecDirectionWords.begin(), vecDirectionWords.end(), vecWords[UPDATE_NEW_VALUE]) == vecDirectionWords.end()) {
PrintInputError(vecWords[UPDATE_NEW_VALUE]);
return ERROR_INVALID_INPUT;
}
} else if (vecWords[UPDATE_OPTION] == "ip") {
if (!isValidIP(vecWords[UPDATE_NEW_VALUE])) {
PrintInputError(vecWords[UPDATE_NEW_VALUE]);
return ERROR_INVALID_INPUT;
}
} else if (vecWords[UPDATE_OPTION] == "port") {
if (!isValidPort(vecWords[UPDATE_NEW_VALUE])) {
PrintInputError(vecWords[UPDATE_NEW_VALUE]);
return ERROR_INVALID_INPUT;
}
} else if (vecWords[UPDATE_OPTION] == "action") {
if (std::find(vecActionWords.begin(), vecActionWords.end(), vecWords[UPDATE_NEW_VALUE]) == vecActionWords.end()) {
PrintInputError(vecWords[UPDATE_NEW_VALUE]);
return ERROR_INVALID_INPUT;
}
} else {
PrintInputError(vecWords[UPDATE_NEW_VALUE]);
return ERROR_INVALID_INPUT;
}
return SUCCESS_CODE;
} else if (strCommand == "d") {
if (vecWords.size() != DELETE_LENGTH) {
std::cerr << "Invalid length input." << std::endl;
return ERROR_INVALID_INPUT;
}
if (vecWords[DELETE_NUMBER] == "all") {
return SUCCESS_CODE;
}
if (!isValidNumber(vecWords[DELETE_NUMBER])) {
PrintInputError(vecWords[DELETE_NUMBER]);
return ERROR_INVALID_INPUT;
}
int nDelNum = std::stoi(vecWords[DELETE_NUMBER]);
if (vecIniData.size() < nDelNum || nDelNum < 1) {
PrintInputError(vecWords[DELETE_NUMBER]);
return ERROR_INVALID_INPUT;
}
return SUCCESS_CODE;
} else if (strCommand == "l") {
if (vecWords.size() != 1) {
std::cerr << "Invalid Input" << std::endl;
return ERROR_INVALID_INPUT;
}
return SUCCESS_CODE;
} else if (strCommand == "exit" || strCommand == "help") {
return SUCCESS_CODE;
} else {
PrintInputError(strCommand);
return ERROR_INVALID_INPUT;
}
return ERROR_UNKNOWN;
}
int CFirewall::AddRule(std::vector<std::string>& vecWords) {
if (!FirewallConfig::Instance().Load(FIREWALL_INI_FILE)) {
std::cerr << "Failed to load firewall rules in Configure\n";
return ERROR_INVALID_FUNCTION;
}
try {
auto iniData = FirewallConfig::Instance().GetIniData();
if (iniData.size() >= MAX_RULE_NUM){
std::cout << "You cannot add more than " << std::to_string(MAX_RULE_NUM) << " rules \n" << std::endl;
return SUCCESS_CODE;
}
FirewallConfig::Instance().AddRule(vecWords[ADD_DIRECTION], vecWords[ADD_IP], vecWords[ADD_PORT], vecWords[ADD_ACTION]);
std::cout << "Rule successfully added\n" << std::endl;
return SUCCESS_CODE;
}
catch (std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
return ERROR_UNKNOWN;
}
}
int CFirewall::UpdateRule(std::vector<std::string>& vecWords) {
if (!FirewallConfig::Instance().Load(FIREWALL_INI_FILE)) {
std::cerr << "Failed to load firewall rules in Configure\n";
return ERROR_INVALID_FUNCTION;
}
try {
auto& vecIniData = FirewallConfig::Instance().GetIniData();
std::string strSectionName = GetSectionName(vecIniData, std::stoi(vecWords[UPDATE_NUMBER]));
FirewallConfig::Instance().UpdateRule(strSectionName, vecWords[UPDATE_OPTION], vecWords[UPDATE_NEW_VALUE]);
std::cout << "Rule successfully updated\n" << std::endl;
return SUCCESS_CODE;
}
catch (std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
return ERROR_UNKNOWN;
}
}
int CFirewall::DeleteRule(std::vector<std::string>& vecWords) {
if (!FirewallConfig::Instance().Load(FIREWALL_INI_FILE)) {
std::cerr << "Failed to load firewall rules in Configure\n";
return ERROR_INVALID_FUNCTION;
}
auto vecIniData = FirewallConfig::Instance().GetIniData();
std::string strSectionName = (vecWords[DELETE_NUMBER] == "all") ? "all" : GetSectionName(vecIniData, std::stoi(vecWords[DELETE_NUMBER]));
FirewallConfig::Instance().DeleteRule(strSectionName);
std::cout << "Rule successfully deleted\n" << std::endl;
return SUCCESS_CODE;
}
int CFirewall::RuleList() {
if (!FirewallConfig::Instance().Load(FIREWALL_INI_FILE)) {
std::cerr << "Failed to load firewall rules in Configure\n";
return ERROR_INVALID_FUNCTION;
}
VariadicTable<int, std::string, std::string, std::string, std::string> vt({"No", "Direction", "IP Address", "PORT", "Action"}, 10);
auto vecIniData = FirewallConfig::Instance().GetIniData();
int nRuleNumber = 0;
for (const auto& id : vecIniData) {
nRuleNumber++;
std::vector<std::string> vecDataFormat;
for (const auto& sd : id.second) {
vecDataFormat.push_back(sd.second);
}
vt.addRow(nRuleNumber, vecDataFormat[DIRECTION], vecDataFormat[IP], vecDataFormat[PORT], vecDataFormat[ACTION]);
}
vt.print(std::cout);
return SUCCESS_CODE;
}
void CFirewall::DefaultRuleSet(){
std::vector<std::string> vecDefaultRules = {
"DROP INPUT ANY 0",
"DROP OUTPUT ANY 0",
};
for (std::string strRule : vecDefaultRules){
std::istringstream issRule(strRule);
std::vector<std::string> vecRule;
std::string strTmp;
while (issRule >> strTmp){
vecRule.push_back(strTmp);
}
RunIptables(vecRule[DIRECTION], vecRule[IP], vecRule[PORT], vecRule[ACTION]);
}
}
void CFirewall::PrintConfigManual() {
std::cout << COLOR_BLUE "[ADD] : " COLOR_RESET " [A/add] [TO/FROM] [IP] [PORT] [ACCEPT(o)/DROP(x)] \n"
<< COLOR_GREEN "[UPDATE] : " COLOR_RESET " [U/update] [Rule Number] [OPTION] [>] [Change Value]\n"
<< COLOR_RED "[DELETE] : " COLOR_RESET " [D/delete] [Rule Number] \n"
<< COLOR_YELLOW "[LIST] : " COLOR_RESET " [L/list] \n\n"
<< COLOR_CYAN "[EXIT]" COLOR_RESET COLOR_MAGENTA "[HELP]" COLOR_RESET "\n\n"
<< "COMMAND: ";
}
void CFirewall::handleExit(int nSignum) {
std::cout << "\nProgram is terminating\n" << std::endl;
std::vector<std::string> vecCmdList = {"iptables -F", "pkill -f firewall_logs.sh"};
for (const std::string& strCmd : vecCmdList) {
CFirewall::ExecCommand(strCmd);
}
exit(nSignum);
}
void CFirewall::ExecCommand(const std::string& strCmd) {
FILE* pPipe = popen(strCmd.c_str(), "r");
if (!pPipe) {
std::cerr << "ERROR: popen() failed" << std::endl;
return;
}
char chBuffer[128];
while (fgets(chBuffer, sizeof(chBuffer), pPipe) != nullptr) {
std::cout << chBuffer;
}
pclose(pPipe);
}
std::string CFirewall::GetSectionName(const auto& vecIniData, int nNumber) {
int nCnt = 1;
for (const auto& stId : vecIniData) {
if (nNumber == nCnt) {
return stId.first;
} else {
nCnt++;
}
}
return "";
}
bool CFirewall::isValidIP(const std::string& strIp) {
std::regex stIpPattern("^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\\."
"(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\\."
"(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\\."
"(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$");
return std::regex_match(strIp, stIpPattern) || strIp == "ANY";
}
bool CFirewall::isValidPort(const std::string& strPort) {
std::regex stPortPattern("^(6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3}|0)$");
return std::regex_match(strPort, stPortPattern) || strPort == "ANY";
}
bool CFirewall::isValidNumber(const std::string& strNumber) {
std::istringstream iss(strNumber);
int nNum;
return (iss >> nNum) && (iss.eof());
}
int CFirewall::PrintFirewallHelp() {
std::cout << "A, add - Rule Add Command\n"
<< " - [TO] : Settings for outgoing packets\n"
<< " - [FROM] : Settings for incoming packets\n"
<< " - [DROP] : Packet blocking settings\n"
<< " - [ACCEPT]: Packet allow settings\n\n"
<< "U, update - Rule Update Command\n"
<< " - [Rule Number] : Rule Index Number\n"
<< " - [OPTION] : The title of the value you want to change\n"
<< " - [>] : Must use '>' \n"
<< " - [Change Value] : Value to change\n\n"
<< "D, delete - Rule Delete Command\n"
<< " - [Rule Number] : Rule Index Number\n\n"
<< "L, list - Rule Inquiry Command\n\n"
<< "EXIT - End Rule Set Commands\n"
<< std::endl;
return SUCCESS_CODE;
}