php searcher
-
Hallo,
Ich bin daran ein Script zu schreiben welches gewisse Ordner nach "schad-software" (v.a. PHP Shells) durchsucht und diese ebenfalls entfernt.
Folgender Code habe ich dadurch produziert:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <stdlib.h> #include <pthread.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <pthread.h> #include <time.h> #include <string.h> #include <sys/stat.h> #include <fcntl.h> #include <signal.h> #include <unistd.h> /* DO NOT CHANGE ANYTHING HERE THESE VARS NEED TO STAY THE WAY THEY ARE!!!! */ char *string[]; int i = 0; int k = 0; int j = 0; int l = 0; char c; char *path [10000]; char line [10000]; char line2 [10000]; char save [10000]; char dirs [10000]; char *dir [10000]; char *dir_ [10000]; char *command [1000]; char *emailurl [10000]; /* START CHANGING YOUR STUFF RIGHT HERE! */ /* char *marker[] enables you to add as many scan-pattern as you like! please only use shell-scan-patters! if you misjudge you may easily detect "normal" files, which do not harm your server! */ char *marker[] = { "c99", "c98", "c100", "r57", "shell", "backconnect", "hacker" }; /* These are the markers to scan the content of a file! Use them with care! You may add as many as you like! */ char *infile_marker[] = { "exec", "phpinfo", "system", "popen", "c99", "r57" }; /* This var should always be set to the amount of elements defined in infile_marker! This is utterly important, do not use any random number! You are warned! */ int count_infile_marker = 6; /* Here you may define your e-mail address. All notification will be sent to this address */ char *email = "fendalex@vol.at"; /* This is where you define the text which will be sent via email! */ char *email_text = "Es.wurde.ein.verdächtiges.File.gefunden"; /* This is where you may define the subject of the email message */ char *email_subject = "WatchScript:.Meldung"; /* This is the path to the file where you define the directories to be scanned. (i.d perm 777) Change it as you like! */ char *extern_path = "/tmp/file"; /* THIS IS WHERE THE FUNCTIONS DO START!!! DO NOT CHANGE ANYTHING RIGHT HERE, YOU MAY DAMANGE THE PROGRAM! DO NOT DEBUG ANYTHING IN THE CODE TO DEBUG USE GDB! */ char* read_extern (char *path) { FILE* f = fopen(path, "r"); if (!f) return NULL; while (fgets(line2, 1000, f)) { dir[j] = malloc(strlen(line2)+1); if (!dir[j]) return NULL; memset(dir[j], '\0', strlen(line2)+1); strcpy(dir[j], line2); ++j; } fclose(f); } char* search_file (char *filename[]) { struct stat tester; FILE* f = fopen(filename, "r"); if (stat(filename, &tester) == -1) { printf("Exited because stat equalls -1\n\n"); } if (!S_ISDIR(tester.st_mode)) { if(f == 0) { //printf("%s\n", filename); return 0; } while(fgets(line, 1000, f)) { strcpy (save, line); } for (k = 0; k < count_infile_marker; k++) { if (strstr(save, infile_marker[k]) != NULL) { printf ("\n\nWe found something looking quite interesting...\n"); printf ("Detected marker was: "); printf ("%s", infile_marker[k]); printf ("\nAnd the file was: "); printf ("%s", filename); printf ("\n\n"); printf ("Moving file to: /tmp/found-files\n\n"); strcpy (command, "mv "); strcat (command, filename); strcat (command, " /tmp/found-files"); system (command); mail (1); } } fclose(f); memset (save, '\0', strlen(save)); } } void *read_dir (void *path_) { //printf ("Thread launched into *read_dir with value: "); //printf ("%s", (char *)path_); // Verzeichnis struct (struct dirent {long d_ino; u short d_reclen; u short d_namlen; char *dname;) DIR *dir; struct dirent *dirzeiger; if((dir = opendir((char *)path_)) != NULL) { while((dirzeiger=readdir(dir)) != NULL) { // Nicht sehr schön, aber lauffähig strcat (string, (*dirzeiger).d_name); // Call the function to scan the file strcpy (path, (char*) path_); strcat (path, "/"); strcat (path, (*dirzeiger).d_name); if (strstr((*dirzeiger).d_name, ".") != NULL) { strcpy ((*dirzeiger).d_name, "/etc/passwd"); } search_file (path); memset ((*dirzeiger).d_name, '\0', strlen((*dirzeiger).d_name)); } for (i = 0; i < count_infile_marker; ++i) { if (strstr(string, marker[i]) != NULL) { printf ("\n\nWe found some File-Name match.\nDeep Scanning is in progress...\n\n"); } } closedir(dir); } else { perror("Error handling directory function"); } } int mail (int status) { strcpy (emailurl, "GET /mail/index.php?absender="); strcat (emailurl, email); strcat (emailurl, "&text="); strcat (emailurl, email_text); strcat (emailurl, "&subject="); strcat (emailurl, email_subject); strcat (emailurl, " HTTP/1.1\r\nHost: solarys.so.funpic.de\r\n\r\n"); int s; struct sockaddr_in srv; s = socket(AF_INET, SOCK_STREAM, 0); if (s == -1) { perror("socket failed()"); return 2; } srv.sin_addr.s_addr = inet_addr("213.202.225.58"); srv.sin_port = htons( (unsigned short int) 80); srv.sin_family = AF_INET; if (connect(s, (struct sockaddr*) &srv, sizeof(srv)) == -1) { perror("connect failed()"); return 3; } send (s, emailurl, sizeof(emailurl), 0); } int main (int argc, char *argv[]) { system ("mkdir /tmp/found-files"); for (;;) { sleep(5); read_extern (extern_path); for (l = 0; l < j; ++l) { strncpy (dir_, dir[l], strlen(dir[l])-1); pthread_t t1[j]; pthread_create (&t1[j], NULL, read_dir, (void *)dir_); memset (dir_, '\0', strlen(dir_)); } } return 0; }
Das Problem:
Wenn ich das Programm laufen lasse, funktioniert es nach einer Weile einfach nicht mehr. Es stürzt nicht ab und ich bekomme keine Fehler, nein es findet einfach keine Dateien mehr, welche definitiv shells sind!Ich werd nicht schlau daraus und dachte vll kann mir hier jemand helfen..
Danke schonmals
-
Was sagen gdb oder valgrind? Hört sich so an als wenn du irgendwo Speicher nicht wieder frei machst - Ich habe jetzt gerade keine Lust danach zu suchen
-
Also was mir so beim drüberschauen auffällt:
char *string[]; // zeile 29 ... strcat (string, (*dirzeiger).d_name); // 200
// ändern von dir_ // zeiger auf dir wird übergeben während er wohlmöglich schon wieder geändert wird pthread_create (&t1[j], NULL, read_dir, (void *)dir_);
-
shellzer schrieb:
Das Problem:
Wenn ich das Programm laufen lasse, funktioniert es nach einer Weile einfach nicht mehr. Es stürzt nicht ab und ich bekomme keine Fehler, nein es findet einfach keine Dateien mehr, welche definitiv shells sind!Ich werd nicht schlau daraus und dachte vll kann mir hier jemand helfen..
Danke schonmals
Wieviele Threads erstellst du in deinen zwei Schleifen denn so im Schnitt?
-
Eigneltich nur 3 Threads.
Aber ich habe die Threads jetzt rausgeholt und mit normalen Funktionsaufrufen versehen. Das Ergebnis bleibt leider das Selbe:read_dir(dir_);
-
http://traps.darkmindz.com/2008/08/perl-php-shell-scanner-img-shell-scanner/
Schau dir das script doch mal an, damit hab ich gute Erfahrungen gemacht die letzten Tage.
Ist zwar kein C, entspricht aber vielleicht doch deinen Bedürfnissen.