2020-05-02 13:15:48 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-05-04 17:22:24 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
2020-05-02 13:15:48 +02:00
|
|
|
|
2020-05-04 17:22:24 +02:00
|
|
|
#define HOSTNAME "lalis"
|
|
|
|
|
|
|
|
int isinarray( int pid, int array[], int n )
|
|
|
|
{
|
|
|
|
//if (n == 0) return 0;
|
|
|
|
int x;
|
|
|
|
for(x=1;x<=n;x++)
|
|
|
|
{
|
|
|
|
if( pid == array[x])
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-06 17:14:36 +02:00
|
|
|
int null2space( char str[] )
|
|
|
|
{
|
|
|
|
int flag =0;
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
while ( flag == 0 )
|
|
|
|
{
|
|
|
|
if ( (int) str[x] == 0 )
|
|
|
|
{
|
|
|
|
if ( (int) str[x+1] != 0 )
|
|
|
|
{
|
|
|
|
str[x] = ' ';
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
flag = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
return x-1 ;
|
|
|
|
}
|
|
|
|
|
2020-05-06 15:23:12 +02:00
|
|
|
int getpids(int pid, int exploded[])
|
|
|
|
{
|
|
|
|
FILE *fh;
|
|
|
|
char * pch;
|
|
|
|
char path[1024];
|
|
|
|
char str[4096];
|
|
|
|
char separator[] = " ";
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
sprintf( path, "/proc/%d/task/%d/children", pid,pid);
|
|
|
|
//printf(" %s", proc_path);
|
|
|
|
if ((fh = fopen( path, "r")) == NULL)
|
|
|
|
{
|
|
|
|
perror(path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ( fgets( str, 40, fh ) != NULL )
|
|
|
|
{
|
|
|
|
pch = strtok( str, separator );
|
|
|
|
while( pch != NULL )
|
|
|
|
{
|
|
|
|
exploded[x++] = atoi( pch );
|
|
|
|
pch = strtok( NULL , separator );
|
|
|
|
}
|
|
|
|
fclose(fh);
|
|
|
|
return x;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
fclose(fh);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int getprocinfo( int ppid, char cmdline[], char cmd[], char user[] )
|
2020-05-02 13:15:48 +02:00
|
|
|
{
|
2020-05-04 17:22:24 +02:00
|
|
|
FILE *fh1;
|
|
|
|
char child_path[128];
|
|
|
|
char str[1024];
|
|
|
|
char * pch;
|
2020-05-06 15:23:12 +02:00
|
|
|
int child_pid[10];
|
2020-05-04 17:22:24 +02:00
|
|
|
int flag = 0;
|
|
|
|
int r;
|
|
|
|
int x = 0;
|
|
|
|
int pid = ppid;
|
2020-05-06 15:23:12 +02:00
|
|
|
int level = 0;
|
|
|
|
int retval = 0;
|
2020-05-06 16:56:53 +02:00
|
|
|
char * pos;
|
2020-05-04 17:22:24 +02:00
|
|
|
|
|
|
|
while ( flag == 0)
|
|
|
|
{
|
2020-05-06 15:23:12 +02:00
|
|
|
r = getpids( pid, child_pid );
|
|
|
|
if( level == 0 && r == 2 )
|
2020-05-04 17:22:24 +02:00
|
|
|
{
|
2020-05-06 15:23:12 +02:00
|
|
|
flag = 1;
|
|
|
|
retval = 2;
|
|
|
|
}else if ( r != -1 )
|
2020-05-04 17:22:24 +02:00
|
|
|
{
|
2020-05-06 15:23:12 +02:00
|
|
|
level++;
|
|
|
|
pid = child_pid[0];
|
2020-05-04 17:22:24 +02:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
flag = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sprintf( child_path, "/proc/%d/cmdline", pid );
|
|
|
|
if ( (fh1= fopen( child_path, "r" )) == NULL)
|
|
|
|
{
|
|
|
|
perror(child_path);
|
2020-05-06 15:23:12 +02:00
|
|
|
return 2;
|
2020-05-04 17:22:24 +02:00
|
|
|
}
|
|
|
|
fgets( str, 1024, fh1);
|
|
|
|
flag = 0;
|
2020-05-06 17:14:36 +02:00
|
|
|
null2space( str );
|
2020-05-04 17:22:24 +02:00
|
|
|
sprintf(cmdline, "%s", str);
|
|
|
|
fclose(fh1);
|
|
|
|
sprintf( child_path, "/proc/%d/comm", pid );
|
|
|
|
if ( (fh1= fopen(child_path, "r" )) == NULL)
|
|
|
|
{
|
|
|
|
perror(child_path);
|
2020-05-06 15:23:12 +02:00
|
|
|
return 3;
|
2020-05-04 17:22:24 +02:00
|
|
|
}
|
|
|
|
fscanf( fh1, "%s", cmd );
|
|
|
|
fclose(fh1);
|
|
|
|
sprintf( child_path, "/proc/%d/environ", pid );
|
|
|
|
if ( (fh1= fopen(child_path, "r" )) == NULL)
|
|
|
|
{
|
|
|
|
perror(child_path);
|
|
|
|
return 5;
|
|
|
|
}
|
2020-05-06 16:56:53 +02:00
|
|
|
fgets( str, 1024, fh1);
|
2020-05-06 17:14:36 +02:00
|
|
|
null2space( str );
|
2020-05-06 17:00:29 +02:00
|
|
|
pos = strstr( str, "USER=");
|
|
|
|
sprintf( user, "%s", pos+5);
|
2020-05-04 17:22:24 +02:00
|
|
|
fclose(fh1);
|
2020-05-06 15:23:12 +02:00
|
|
|
return retval;
|
2020-05-04 17:22:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
FILE *fh;
|
|
|
|
FILE *fh1;
|
2020-05-06 15:23:12 +02:00
|
|
|
int n_ssh=10;
|
2020-05-04 17:22:24 +02:00
|
|
|
int id;
|
|
|
|
int pid;
|
|
|
|
int x=0;
|
2020-05-06 15:23:12 +02:00
|
|
|
int y=0;
|
2020-05-04 17:22:24 +02:00
|
|
|
int r;
|
|
|
|
int i;
|
|
|
|
int j;
|
2020-05-06 15:23:12 +02:00
|
|
|
int n;
|
|
|
|
int start=1;
|
|
|
|
int childrens[n_ssh];
|
|
|
|
int pids[n_ssh];
|
|
|
|
int flag[n_ssh];
|
|
|
|
int rinfo;
|
|
|
|
// char path[1024];
|
2020-05-04 17:22:24 +02:00
|
|
|
char commande[] = "/usr/local/bin/send_sms";
|
|
|
|
char logfile[] = "/var/log/sshdetect.log";
|
|
|
|
char cmd[24];
|
|
|
|
char cmdline[1000];
|
|
|
|
char user[24];
|
|
|
|
char str[1024];
|
|
|
|
char date[60];
|
|
|
|
time_t now ;
|
|
|
|
|
|
|
|
time( &now );
|
|
|
|
sprintf( date, "%s", ctime(&now));
|
|
|
|
date[strlen(date)-1] = 0;
|
|
|
|
if ( (fh = fopen(logfile, "a")) == NULL)
|
|
|
|
{
|
|
|
|
perror(logfile);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-05-06 15:23:12 +02:00
|
|
|
fprintf(fh, "%s: Démarrage de sshdetect\n", date);
|
2020-05-04 17:22:24 +02:00
|
|
|
fclose(fh);
|
|
|
|
sprintf( str, "%s \"%s - %s: Démarrage de sshdetect\"", commande, HOSTNAME, date );
|
|
|
|
id=fork();
|
|
|
|
if(id == 0)
|
|
|
|
{
|
|
|
|
r = system( str );
|
|
|
|
exit(r);
|
|
|
|
}else if( id<0 )
|
|
|
|
{
|
|
|
|
printf("erreur de création du fork: %s", str);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
// get the sshd process ID (PID)
|
|
|
|
if ( (fh = fopen("/run/sshd.pid", "r" )) == NULL)
|
|
|
|
{
|
|
|
|
perror("/run/sshd.pid");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ( fscanf(fh, "%i", &pid) == 0)
|
|
|
|
{
|
|
|
|
printf("erreur fscanf: /run/sshd.pid" );
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
fclose(fh);
|
|
|
|
//printf("%i", pid);
|
|
|
|
|
|
|
|
//get the list of children
|
2020-05-06 16:36:48 +02:00
|
|
|
if ( (n=getpids( pid, pids )) != -1)
|
2020-05-04 17:22:24 +02:00
|
|
|
{
|
2020-05-06 15:23:12 +02:00
|
|
|
for ( y=0; y<=n; y++)
|
2020-05-04 17:22:24 +02:00
|
|
|
{
|
2020-05-06 15:23:12 +02:00
|
|
|
pid = pids[y];
|
|
|
|
r = isinarray(pid, childrens, x);
|
|
|
|
if( r == 0 )
|
2020-05-04 17:22:24 +02:00
|
|
|
{
|
2020-05-06 15:23:12 +02:00
|
|
|
rinfo = getprocinfo( pid, cmdline, cmd, user );
|
|
|
|
if( rinfo == 0 )
|
2020-05-04 17:22:24 +02:00
|
|
|
{
|
2020-05-06 15:23:12 +02:00
|
|
|
x++;
|
|
|
|
childrens[x] = pid;
|
|
|
|
flag[x] = 1;
|
|
|
|
|
|
|
|
time( &now );
|
|
|
|
sprintf( date, "%s", ctime(&now));
|
|
|
|
date[strlen(date)-1] = 0;
|
|
|
|
sprintf( str, "%s \"%s: %s s'est connecté le %s avec la commande: %s %s\"", commande, HOSTNAME, user, date, cmd, cmdline );
|
|
|
|
if ( start != 1 )
|
|
|
|
{
|
|
|
|
id=fork();
|
|
|
|
if(id>0)
|
|
|
|
{
|
|
|
|
if ( (fh1 = fopen(logfile, "a")) == NULL)
|
|
|
|
{
|
|
|
|
perror(logfile);
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
fprintf(fh1, "%s: Connexion de %s\n#%s# %s\n", date, user, cmd, cmdline);
|
|
|
|
fclose(fh1);
|
|
|
|
}else if (id<0)
|
|
|
|
{
|
|
|
|
printf("erreur de création du fork: %s", str);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
//printf("%s\n", str);
|
|
|
|
r = system( str );
|
|
|
|
exit (r);
|
|
|
|
}
|
|
|
|
}else
|
2020-05-04 17:22:24 +02:00
|
|
|
{
|
2020-05-05 10:29:16 +02:00
|
|
|
if ( (fh1 = fopen(logfile, "a")) == NULL)
|
|
|
|
{
|
|
|
|
perror(logfile);
|
|
|
|
return 7;
|
|
|
|
}
|
2020-05-06 15:23:12 +02:00
|
|
|
fprintf(fh1, "%s: %s Connecté - %s %s\n", date, user, cmd, cmdline);
|
2020-05-05 10:29:16 +02:00
|
|
|
fclose(fh1);
|
2020-05-04 17:22:24 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 15:23:12 +02:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
flag[r] = 1;
|
2020-05-04 17:22:24 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 15:23:12 +02:00
|
|
|
for(i=1;i<=x;i++)
|
2020-05-04 17:22:24 +02:00
|
|
|
{
|
2020-05-06 15:23:12 +02:00
|
|
|
if (flag[i] == 0 )
|
2020-05-04 17:22:24 +02:00
|
|
|
{
|
2020-05-06 15:23:12 +02:00
|
|
|
printf("Session %d terminée\n", childrens[i]);
|
|
|
|
for( j=i; j<x; j++ )
|
|
|
|
{
|
|
|
|
childrens[j] = childrens[j+1];
|
|
|
|
flag[j] = flag[j+1];
|
|
|
|
}
|
|
|
|
i--;
|
|
|
|
x--;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
flag[i] = 0;
|
2020-05-04 17:22:24 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 15:23:12 +02:00
|
|
|
sleep(2);
|
2020-05-04 17:22:24 +02:00
|
|
|
}
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
2020-05-02 13:15:48 +02:00
|
|
|
}
|