Dir Scanner
-
Hallo,
Ich habe über die Nacht hinweg meinen Dir-Scanner über einen Ordner laufen lassen, der 1.7GB hat. 10Stunden später, war der Scanner immer noch nicht fertig!
Kann es sein, dass ich einen logischen Fehler im Scanner habe? Also dass dieser nie richtig terminiert bzw. nicht alle Files durchsucht?
Hier der Code:
void print_rec_dir(const char *path, string scan_pattern[], int scan_pattern_elements) { /* Let's define the structs we need to work with files and directories */ struct dirent *entry; struct stat entrystat; /* This is the absolute path to the current file */ char fullpath[PATH_MAX]; /* This is the buffer which will be filled if a file gets read */ string buffer; string ibuffer; /* Just a counter variable */ int k = 0; int mail_check = 0; /* Let's open the directory */ DIR *dir = opendir(path); if (!dir) { cout << "Could not open directory: " << path << endl; perror("Error was"); return; } /* Let's read until there are no files or directories left */ while ((entry = readdir(dir))) { strncpy(fullpath, path, PATH_MAX); strncat(fullpath, "/", PATH_MAX); strncat(fullpath, entry->d_name, PATH_MAX); cout<<fullpath<<endl; if (stat(fullpath, &entrystat)) { /* Permission denied, invalid links... */ fprintf(stderr, "stat(\"%s\"): %s\n", fullpath, strerror(errno)); continue; } if (S_ISDIR(entrystat.st_mode)) { /* It's a directory -> lets call the own function again */ if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) { print_rec_dir(fullpath, scan_pattern, scan_pattern_elements); } } else { /* It's a filename */ /* Let's scan it */ ifstream file_i(fullpath); while (!file_i.eof()) { k++; getline (file_i, buffer); ibuffer += buffer + "\n"; if (k > 200000) { break; } } file_i.close(); for (int i = 0; i <= scan_pattern_elements-2; i++) { if (strstr (ibuffer.c_str(), scan_pattern[i].c_str()) != NULL) { cout << "Found: " << fullpath << " Pattern: " << scan_pattern[i] <<endl << endl; mail_check = 1; } } ibuffer = ""; } /* If mail check = 1 => we found a file => we must send an email */ if (mail_check == 1) { mail_raw ("alexander@localhost", "File was detected:", "We found a file: " + (string)fullpath); if (mail_web("fendalex@vol.at", "Found", "A.file.was.found:." + (string)fullpath) == false) { cout << "Could not send web-email. Check HTTP Connection" << endl; system ("logger 'Could not send web-email. Check HTTP Connection'"); } mail_check = 0; } } closedir(dir); }
Der Aufruf erfolgt in der main.cpp durch:
for (int i = 0; i < include_lines; i++) { print_rec_dir (include_array[i].c_str(), pattern_array, pattern_lines); /* Calls the scan: 1par: path, 2par: pattern to match, 3par: number of lines */ }
Werte der Variablen:
include_array[i] Hat die Pfad Werte welche aus einer Config Datei ausgelesen werden also z.b.
include_array[0] = "/tmp"
include_array[1] = "/opt/lampp/htdocs"in meinem Fall hat include_array nur an der Stelle 0 einen Wert und zwar einen Pfad auf: /opt/lampp/htdocs/files
pattern_array enthält scannmuster für die Datei in meinem Fall sind folgende Werte zugewiesen:
pattern_array[0] = "c99"
pattern_array[1] = "r57"
pattern_array[2] = "c100"
pattern_array[3] = "shell"Pattern lines hat die Anzahl der Werte in dem pattern_array.
In meinem Fall wären dies jetzt 4.Hoffe jemand kann mir weiterhelfen!
Vielen Dank!
-
Hallo,
einen wirklichen Fehler sehe ich jetzt nicht, aber was sagt denn strace wenn du dir den laufenden/haengenden Prozess mal anschaust. Waere mal interessant bei was fuer einer aktion er gerade steht.
Viele Gruesse
hago
-
es könnte sein, dass du zyklische Synmlinks hast, sowas in der art
/abc/symlink1 -> /abc/def/hij
/abc/def/hij/symlink2 -> ../.. (oder /abc/ oder /abc/def, usw)dann ist es kein Wunder, dass dass nach 10 Stunden nicht gestoppt hat.
Das ist nur eine Vermutung, sitze jetzt vor keiner man page, bin nicht sicher, ob ein symlink auf einen Ordner auch durch S_ISDIR durchgeht.
-
An den symlinks kanns net liegen - habs gecheckt, da sind gar keine symlinks in dem Verz.
Gut, dann dachte ich mir nehmen wir doch den Bash Befehl find. und passen den Code mit popen() an:
char* pipe_ (char *command) { char* buf = new char [10000]; char* lese = new char [10000]; /* lets open a pipe */ FILE *p = popen(command, "r"); /* Do some error checking */ if(p==NULL) { perror ("Could not create pipe"); exit(1); } /* Read until there is nothing more to read */ while(feof(p) == 0) { memset(buf, '\0', sizeof(buf)); fgets(buf, 10000, p); strcat(lese, buf); } /* close the pipe */ pclose(p); return lese; } void print_rec_dir(const char *path, string scan_pattern[], int scan_pattern_elements) { char* back = new char [10000]; char command [10000]; int mail = 0; for (int i = 0; i <= scan_pattern_elements-1; i++) { strcpy (command, "find "); strcat (command, path); strcat (command, " -type f -exec grep -l \""); strcat (command, scan_pattern[i].c_str()); strcat (command, "\" {} \\;"); back = pipe_ (command); /* Let's check the result */ for (int i = 0; i <= 10000; i++) { if (back[i] != NULL) { if (mail == 0) { cout << "Found a file: " <<endl; /* We did find something */ mail_raw ("alexander@localhost", "File was detected:", "We found a file, check: /tmp/found-log"); if (mail_web("fendalex@vol.at", "Found", "A.file.was.found,.check:./tmp/found-log") == false) { cout << "Could not send web-email. Check HTTP Connection" << endl; system ("logger 'Could not send web-email. Check HTTP Connection'"); } system ("logger Found a suspicious file"); mail = 1; } ofstream outf ("/tmp/found-log", ios::app); outf << back[i]; outf.close(); } } } }
Wie schon gehabt:
path: /home
scam_pattern[]: Hat 3 Werte: [0]: c99shell [1]: r57shell [2]:eval
scan_pattern_elements: Entspricht der Anzahl der Elemente von scan_pattern. Also 3 in meinem Fall
Führe ich das Programm z.B. gegen /tmp aus, so funktioniert dies einwandfrei.
Wenn ich das Programm jetzt aber gegen Verz. laufen lasse, welche größer sind, passiert folgendes:./main
Segmentation fault
alexander@sysadm:~/Desktop/sis$ find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
find: Der Prozeß grep wurde durch das Signal 13 abgebrochen.
[quote]Für Hilfen, wär ich Euch sehr dankbar