-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappdetector.cpp
More file actions
390 lines (365 loc) · 9.74 KB
/
appdetector.cpp
File metadata and controls
390 lines (365 loc) · 9.74 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
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <cstring>
#include <iostream>
#include <sstream>
#include <ctype.h>
#include <vector>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
/*
struktura uchovavajici hodnoty argumentu
*/
typedef struct arguments {
string ipAddress;
int interval;
string filter;
bool ipv4;
} ARGS;
/*
Napoveda programu
*/
void help() {
cout << "Pouziti:\n";
cout << "./appdetector [ -s IPadresa ] [ -i interval ] [ -f filter ]\n";
cout << "-s IPadresa - (povinny parametr) IP adresa syslog serveru, na ktery se odesilaji zpravy. UDP port je vzdy 514.\n";
cout << "-i interval - (povinny parametr) hodnota v sekundach, jak casto se kontroluje seznam bezicich aplikaci/sluzeb (min. hodn. 1).\n";
cout << "-f filtr - (povinny parametr) seznam aplikaci, ktere chceme detekovat. Seznam ma tvar app,app,app,... kde app je nazev aplikace skladajici se z tisknutelnych ASCII znaku. Napr \"vlc\" nebo \"vlc,firefox\", ...\n";
}
/**
* funkce overujici, zda se retezec sklada pouze z cisel
* @str - string obsahujici retezec, ktery testujeme zda obsahuje pouze cislice
**/
bool isNum(string str) {
for (unsigned int i = 0; i < str.size(); i++) {
if (!isdigit(str[i])) {
return false;
}
}
return true;
}
/**
* funkce overujici, zda se retezec sklada pouze z hexadecimalnich znaku (0-9, a-f)
* @str - string obsahujici retezec, ktery testujeme zda obsahuje pouze hexadecimalni znaky
**/
bool isHexa(string str) {
for (unsigned int i = 0; i < str.size(); i++) {
if (!isxdigit(str[i])) {
return false;
}
}
return true;
}
/**
* funkce na kontrolu spravne zadane IPv4
* @ip - string obsahujici IP adresu, u ktere budeme kontrolovat validitu
**/
int checkIPv4(string ip) {
size_t index;
string tmp;
int counter = 1;
while ((index = ip.find(".")) != string::npos) {
tmp = ip.substr(0, index);
if (!isNum(tmp) || tmp.length() > 3 || (atoi(tmp.c_str()) > 255)) {
return EXIT_FAILURE;
}
counter++;
ip.erase(0, index + 1);
}
if (!isNum(ip) || ip.length() > 3 || counter != 4 || (atoi(ip.c_str()) > 255)) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/**
* funkce na kontrolu spravne zadane IPv6
* @ip - string obsahujici IP adresu, u ktere budeme kontrolovat validitu
**/
int checkIPv6(string ip) {
size_t index;
bool colon = false;
string tmp;
int counter = 1;
if (ip.find(":") == 0) {
counter++;
ip = ip.substr(0, 1);
}
while ((index = ip.find(":")) != string::npos) {
tmp = ip.substr(0, index);
if (tmp.length() == 0) {
if (!colon) {
counter++;
colon = true;
ip.erase(0, index+1);
continue;
}
else {
return EXIT_FAILURE; // vicekrat :: v IPv6
}
}
if (!isHexa(tmp) || tmp.length() > 5) {
return EXIT_FAILURE;
}
ip.erase(0, index + 1);
counter++;
}
if (ip.length() > 0) {
if (!isHexa(ip) || ip.length() > 5 || counter > 8) {
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
/**
* funkce rozhodne, zda se jedna o IPv4 nebo IPv6 a podle toho zavola funkci na kontrolu IP
* @ip - string, ktery obsahuje IP adresu, u ktere se bude kontrolovat validita
* @args - struktura, do ktere ulozime, zda se jedna o IPv4 nebo IPv6
**/
int checkIp(string ip, ARGS &args) {
if (ip.find(".") != string::npos) {
if (checkIPv4(ip)) {
return EXIT_FAILURE;
}
else {
args.ipv4 = true;
return EXIT_SUCCESS;
}
}
else if (ip.find(":") != string::npos) {
if (checkIPv6(ip)) {
return EXIT_FAILURE;
}
else {
args.ipv4 = false;
return EXIT_SUCCESS;
}
}
else {
return EXIT_FAILURE;
}
}
/**
* funkce na zpracovani argumentu
* @argc - pocet zadanych argumentu
* @argv - pole obsahujici jednotlive argumenty
* @args - struktura, do ktere budeme ukladat hodnoty argumentu
**/
int argParse(int argc, char **argv, ARGS &args) {
bool sIsSet = false, iIsSet = false, fIsSet = false;
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-s")) {
if (sIsSet) {
cerr << "Znovupouziti argumentu!\n";
return EXIT_FAILURE;
}
sIsSet = true;
if (checkIp(argv[++i], args)) {
cerr << "Spatne zadana IP adresa!\n";
return EXIT_FAILURE;
}
args.ipAddress = argv[i];
}
else if (!strcmp(argv[i], "-i")) {
if (iIsSet) {
cerr << "Znovupouziti argumentu!\n";
return EXIT_FAILURE;
}
iIsSet = true;
if (!isNum(argv[i+1])) {
cerr << "Interval musi byt cislo!\n";
return EXIT_FAILURE;
}
args.interval = atoi(argv[i+1]);
i++;
}
else if (!strcmp(argv[i], "-f")) {
if (fIsSet) {
cerr << "Znovupouziti argumentu!\n";
return EXIT_FAILURE;
}
fIsSet = true;
args.filter = argv[++i];
size_t index;
index = args.filter.find(",");
// v cyklu se znak carky nahradi za znaky \| pro praci s funkci grep
while(index != string::npos) {
args.filter.replace(index, 1, "\\|");
index = args.filter.find(",");
cout << args.filter;
}
}
else if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
help();
exit(0);
}
else {
cerr << "Chybny argument!\n";
return EXIT_FAILURE;
}
}
if (!sIsSet || !iIsSet || !fIsSet) {
cerr << "Nebyl zadan povinny argument\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/**
* funkce na vytvoreni socketu
* @ipstr - string obsahujici ip adresu
* @sock - ukazatel na promennou vytvorenou ve funkci main, do ktere se ulozi socket
* @ipv4 - booleovska promenna, ktera nabyva true, pokud se jedna o IPv4
**/
int connection(string ipstr, int *sock, bool ipv4) {
struct in_addr ip;
struct sockaddr_in sockaddr;
struct hostent *host;
if (ipv4) {
*sock = socket(AF_INET, SOCK_STREAM, 0);
}
else {
*sock = socket(AF_INET6, SOCK_STREAM, 0);
}
if (*sock <= 0) {
cerr << "Chyba inicializace socketu\n";
return EXIT_FAILURE;
}
if (ipv4) {
if (!inet_pton(AF_INET, ipstr.c_str(), &ip)) {
cerr << "Nelze parsovat IP\n";
}
}
else {
if (!inet_pton(AF_INET6, ipstr.c_str(), &ip)) {
cerr << "Nelze parsovat IP\n";
}
}
if (ipv4) {
sockaddr.sin_family = AF_INET;
}
else {
sockaddr.sin_family = AF_INET6;
}
sockaddr.sin_port = htons(514);
if (ipv4) {
host = gethostbyaddr((const void*)&ip, sizeof ip, AF_INET);
}
else {
host = gethostbyaddr((const void*)&ip, sizeof ip, AF_INET6);
}
if (host == NULL) {
cerr << "Neco se porouchalo\n";
}
memcpy (&sockaddr.sin_addr, host->h_addr, host->h_length);
if ((connect(*sock, (const struct sockaddr *) &sockaddr, sizeof(sockaddr)) < 0)) {
cerr << "chyba pripojeni\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/**
* ze stringu vybere 1 radek, ktery z puvodniho stringu vymaze a vrati jako navratovou hodnotu
* @str - string obsahujici vysledek operace lsof
**/
string getLine(string *str) {
size_t index;
string line;
index = (*str).find("\n");
if (index != string::npos) {
line = (*str).substr(0, index);
}
else {
line = (*str);
}
(*str).erase(0, index+1);
return line;
}
/**
* funkce na overeni, zda se jedna o pripojeni localhostu
* @str - string obsahujici radek s podrobnosti o pripojeni
**/
bool isLocalhost(string str) {
size_t found;
found = str.find("127.0.0.1");
if (found != string::npos) {
return true;
}
else {
return false;
}
}
int main(int argc, char *argv[]) {
cout << "here";
int sock, resIndex, index, toSend;
char buff[512];
bool found;
string str = "";
ARGS args;
vector<string> result; // pole na ukladani vysledku. Pomoci tohoto pole se bude kontrolovat, ktere pripojeni jiz bylo vypsano
vector<string> toProcess; // pole na ukladani vysledku prikazu v promenne command
FILE *in;
if (argParse(argc, argv, args)) {
return EXIT_FAILURE;
}
if (connection(args.ipAddress, &sock, args.ipv4)) {
return EXIT_FAILURE;
}
string command = "lsof -Pnl +M -i | grep ESTABLISHED | grep \"";
command += args.filter; // pridani filtru na pozadovane aplikace
command += "\" | tr -s \" \" | cut -d\" \" -f 8,9,1 | awk '{ print $2 \" \" $3 \" \" $1}'"; // vyber pozadovanych sloupcu (nazev appky, IP, protokol) + prehazeni v pozadovanem poradi
command += " | sed -r \"s/\\]|\\[|\\->/ /g\" | "; // odmazani prebytecnych znaku + rozdeleni portu od IP v IPv6
command += "sed -r \"s/(\\.[[:digit:]]+):/\\1 /g\" | "; // rozdeleni portu od IP v IPv4
command += "sed -r \"s/ :/ /g\" | tr -s \" \" | uniq -u";
while (1) {
in = popen(command.c_str(), "r");
while(fgets(buff, sizeof(buff), in) != NULL){
str += buff;
}
while (strcmp(str.c_str(), "")) {
toProcess.push_back(getLine(&str));
}
resIndex = result.size() - 1;
toSend = result.size();
if (resIndex < 0) { // prvni pruchod nebo v predchozim pruchodu nebylo zadne spojeni => muzeme naplnit pole bez jakekoliv kontroly
for (int i = 0; i < toProcess.size(); i++) {
if (isLocalhost(toProcess[i])) {
continue;
}
result.push_back(toProcess[i]);
}
}
else {
for (; resIndex >= 0; resIndex--) {
found = false;
for (index = 0; index < toProcess.size(); index++) {
if (result[resIndex].compare(toProcess[index]) == 0 || isLocalhost(toProcess[index])) {
toProcess.erase(toProcess.begin() + index);
index--;
found = true;
}
}
if (!found) { // proslo se cele pole a prave kontrolovana komunikace nebyla nalezena => byla ukoncena
result.erase(result.begin() + resIndex);
toSend--;
}
}
for (int i = 0; i < toProcess.size(); i++) { // nyni vlozime zaznamy o nove komunikaci
result.push_back(toProcess[i]);
}
}
// vypis
for (; toSend < result.size(); toSend++) {
if ((send(sock, result[toSend].c_str(), result[toSend].length(), 0)) < 0) {
cerr << "chyba odesilani zpravy\n";
return EXIT_FAILURE;
}
cout << result[toSend] << endl;
}
sleep(args.interval);
}
return EXIT_SUCCESS;
}