предлагаю свой вариант программы,
которая заносит ip адреса в таблицу IPFW,
00024 507 329262 reject ip from table(24) to me 5060
00025 2722 1263108 allow udp from any to any 5060 via bge0
с которых пытаются безуспешно зарегистрироваться.
Все кто прошёл правило 00025
 
   без билета
 без билетапопадёт в таблицу ipfw table 24 list
Пока анализирует файл /var/log/asterisk/security
на наличие этих слов
"ChallengeResponseFailed",
"InvalidPassword",
"Wrong password",
"FailedACL"
 можно и пополнить словарный запас
 можно и пополнить словарный запас  
 После запуска остаётся в процессах.
таймер sleep(180сек);
Анализирует сообщения с начала последней 10-ти минутки.
Создаётся директория /var/log/447b
куда пишутся отчёты.
Конечно совершенству нет предела...
Планирую добиться того, чтобы перед тем как добавлять ip адрес в таблицу,
проводить проверку , а нет ли уже в таблице такого адреса.
Что бы не дёргать ipfw безтолку.
Да, все это опробовал на Asterisk 15.5.0
- Код: Выделить всё
- #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <unistd.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <syslog.h>
 #include <time.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <unistd.h>
 #include <netdb.h>
 #define COUNT_BUFFER 1024
 #define SBUF 1024
 #define WORK_DIR "/var/log/447b"
 #define LOG_FILE "/var/log/447b/447b.log"
 #define PID_FILE "/var/log/447b/pid"
 #define ASTERISK_WORK_DIR "/var/log/asterisk"
 #define ASTERISK_LOGFILE "/var/log/asterisk/security"
 #define TABLE_IPFW_ADD "ipfw table 24 add ";
 #define TABLE_IPFW_LIST "ipfw table 24 list";
 ////////// IPFW
 //00024 507 329262 reject ip from table(24) to me 5060
 //00025 2722 1263108 allow udp from any to any 5060 via bge0
 //ipfw table 24 add 1.1.1.1
 //char *flogin ="ChallengeResponseFailed";
 //char *fpasswd ="InvalidPassword";
 //char
 int count_buffer;
 FILE *list;
 int ulist;
 int z = 0;
 int w = 1;
 char *impact_list[1024];
 static char *atbl = TABLE_IPFW_ADD;
 static char *ltbl = TABLE_IPFW_LIST;
 long int pos_file;
 char *failers[4] ={"ChallengeResponseFailed","InvalidPassword","Wrong password","FailedACL"};
 char **temp;
 char spl_str[1024][1024];
 void err(char *str);
 char* getTime();
 char* get1hTime();
 int writeLog(char msg[256]);
 void SetPidFile(char * path_file);
 char* g_cmd(char command[128]);
 int de();
 int cmp_s(char *s1, char *s2);
 void err(char *str)
 {
 perror(str);
 exit(1);
 }
 char* getTime()
 {
 time_t now;
 struct tm *ptr;
 static char tbuf[64];
 bzero(tbuf,64);
 time(&now);
 ptr = localtime(&now);
 strftime(tbuf,64, "%Y-%m-%e %H:%M:%S", ptr);
 // strftime(tbuf,64, "%b %d %H", ptr);
 return tbuf;
 }
 char* get1hTime()
 {
 int ln =0;
 static char temp[64];
 time_t now;
 struct tm *ptr;
 static char tbuf[64];
 bzero(tbuf,64);
 time(&now);
 ptr = localtime(&now);
 // strftime(tbuf,64, "%Y-%m-%e %H:%M:%S", ptr);
 strftime(tbuf,64, "%b %d %H:%M", ptr);
 ln = strlen(tbuf);
 bzero(temp,64);
 for(int i =0;i<ln-1;i++){temp[i] = tbuf[i]; }
 return temp;
 //return tbuf;
 }
 int writeLog(char msg[512])
 {
 FILE * pLog;
 pLog = fopen(LOG_FILE, "a+");
 if(pLog == NULL) {
 return 1;
 }
 char str[512];
 bzero(str, 512);
 // strcpy(str, getTime());
 // strcat(str, " ==========================\n");
 // strcat(str, msg);
 // strcat(str, "\n");
 //char cmd[128];
 //char *t = TABLE_IPFW;
 snprintf(str,sizeof str,"%s\t%s\n",getTime(),msg);
 fputs(str, pLog);
 //fwrite(msg, 1, sizeof(msg), pLog);
 fclose(pLog);
 return 0;
 }
 void SetPidFile(char *path_file){
 FILE* f;
 //char *path_file = PID_FILE;
 f = fopen(path_file, "w+");
 if (f)
 {
 fprintf(f, "%u", getpid());
 fclose(f);
 }
 }
 char* g_cmd(char command[128])
 {
 FILE *pcm;
 static char bf[SBUF];
 int lastchar;
 
 //** delete "cd " and run chdir()
 char *istr = strstr(command,"cd ");
 if(istr != NULL){
 int sln= strlen(command);
 int nsln=sln-3;
 char *cmd; cmd=(char *)malloc(nsln);
 for(int j=3;j<sln;j++){cmd[j-3]=command[j];}
 cmd[nsln]='\0';chdir(cmd);free(cmd);return "0";
 };
 //**
 pcm=popen(command,"r");
 if(pcm== NULL){writeLog("Error Command"); return "1";}
 bzero(bf,SBUF);
 lastchar = fread(bf,1,SBUF,pcm);
 bf[lastchar]='\0';
 pclose(pcm);
 return bf;
 }
 int cmp_s(char *s1, char *s2)
 {
 //char ts1[] = "Call1234567890 FailedACL";
 //char ts2[] = "FailedACL";
 int t;
 int c;
 c =0;
 int ln1;
 ln1=strlen(s1);
 int ln2;
 ln2=strlen(s2);
 //intf("%d\t%d\n",ln1,ln2);
 //puts(s1);
 //puts(s2);
 for(int i=0;i<ln1;i++){
 for(int j=0;j<ln2;j++){
 if(s2[j]==s1[j+i]){
 //printf("%c=%c\n",s2[j],s1[j+i]);
 c=c+1;
 //getchar();
 //printf("%d\n",j+i);
 }
 }
 if(c==ln2){return 0;}
 c=0;t=ln2+i;
 if(t>ln1){ return 1;}
 }
 //puts("We Here");
 return 1;
 }
 int furgon(char *path_file, char **what_find_sstr, int count_elements){
 char sbuf[1024], *t, *hs, one_h[32];
 char cmd[128];
 int m;
 FILE *f;
 //FILE *lst = fopen(LIST_LOG, "a+");
 f = fopen(path_file, "r");
 if (f == NULL){perror("sender: message "); }
 bzero(one_h,32);
 //snprintf(one_h, sizeof one_h,"%s",get1hTime());
 strcpy(one_h,get1hTime() );
 //count_buffer=0;
 //puts(one_h);
 while(fgets(sbuf,1024,f) != NULL)
 {//begin Read file
 hs=strstr(sbuf,one_h);
 if(!hs){continue;}
 for(int i=0;i<count_elements;i++){
 m=cmp_s(sbuf,what_find_sstr[i]);
 //printf("%d",m);
 if(m == 0){
 /////////////////////////////split ok
 int lb = strlen(sbuf);
 ///////Begin Split String
 int cspl = 0;
 int elm =0;
 char ch ='/';
 for(int i=0;i<lb;i++){
 if(sbuf[i] == ch ){spl_str[cspl][elm]='\0';i=i+1;
 //printf("%s\n\r",spl_str[cspl]);
 cspl = cspl + 1;elm = 0;}
 spl_str[cspl][elm] = sbuf[i];
 elm = elm +1;
 }
 bzero(cmd,128);
 //fprintf(lst,"%s\n",spl_str[5]);
 snprintf(cmd,sizeof cmd,"%s%s%s",atbl,spl_str[5],"\0");
 writeLog(g_cmd(cmd));
 }
 }
 }// End Read File
 fclose(f);
 //fclose(lst);
 return 0;
 }
 ////////////////Process_File
 int de(){
 //chdir("/var/log/asterisk/");
 while(1){
 int u = furgon(ASTERISK_LOGFILE,failers,4);
 //int f = furgon(LOG_FILE,failers,4);
 sleep(180);
 }
 return 0;
 }
 ////////////////
 int main(int argc, char* argv[]) {
 int status;
 int pid;
 struct stat st = {0};
 if(stat(WORK_DIR,&st ) == -1){
 mkdir(WORK_DIR,0700);}
 /*
 if(argc !=2){
 fprintf(stderr," usage: Please Enter IP_addres or hostname remote client across 'probel'\n ");
 exit(1);
 }
 */
 ////hand start
 //int u = furgon(ASTERISK_LOGFILE,failers,4);
 /////////////////////////////////////////////////TEMP
 /**/
 pid_t parpid, sid;
 parpid = fork();
 if(parpid < 0) {
 exit(1);
 } else if(parpid != 0) {
 exit(0);
 }
 SetPidFile(PID_FILE);
 umask(0);
 sid = setsid();
 if(sid < 0) {
 exit(1);
 }
 if((chdir("/var/log")) < 0) {
 exit(1);
 }
 close(STDIN_FILENO);
 close(STDOUT_FILENO);
 close(STDERR_FILENO);
 // return de(argv[1]);
 
 
 return de();
 /**/
 //////////////////////////////////////////////TEMP
 //fclose(list);
 }





 
 

