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-07 13:54:04 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2020-05-07 17:10:00 +02:00
|
|
|
#include <utmp.h>
|
2020-05-02 13:15:48 +02:00
|
|
|
|
2020-05-04 17:22:24 +02:00
|
|
|
#define HOSTNAME "lalis"
|
|
|
|
|
2020-05-08 02:16:35 +02:00
|
|
|
//test if pid is in lsit of known sshd processus
|
2020-05-04 17:22:24 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-05-07 17:10:00 +02:00
|
|
|
} return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-08 13:52:11 +02:00
|
|
|
char* frtime(const time_t timet)
|
|
|
|
{
|
|
|
|
int jour;
|
|
|
|
struct tm *date_tm;
|
|
|
|
|
|
|
|
static const char nomjour[][10] = {
|
|
|
|
"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"
|
|
|
|
};
|
|
|
|
static const char nommois[][10] = {
|
|
|
|
"Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin",
|
|
|
|
"Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"
|
|
|
|
};
|
|
|
|
static char result[40];
|
|
|
|
date_tm = localtime(&timet);
|
|
|
|
strftime(result, 4, "%w", date_tm);
|
|
|
|
jour = atoi(result);
|
2020-05-08 14:04:14 +02:00
|
|
|
strftime(result, 40, "%d/%m/%y à %T", date_tm);
|
|
|
|
sprintf(result, "%d %s", jour, result);
|
2020-05-08 13:52:11 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-08 02:16:35 +02:00
|
|
|
//get utmp datas
|
|
|
|
void getutmp( int pid, char user[], char host_ip[], char host_ipv6[], time_t * time )
|
2020-05-07 17:10:00 +02:00
|
|
|
{
|
|
|
|
struct utmp * utmp;
|
2020-05-08 13:07:36 +02:00
|
|
|
int ipv6;
|
2020-05-08 12:52:33 +02:00
|
|
|
int ipv4;
|
2020-05-08 02:16:35 +02:00
|
|
|
int x;
|
|
|
|
char str[6];
|
|
|
|
|
2020-05-07 17:14:51 +02:00
|
|
|
setutent();
|
2020-05-07 17:16:44 +02:00
|
|
|
while ( (utmp = getutent()) != NULL )
|
2020-05-07 17:10:00 +02:00
|
|
|
{
|
2020-05-07 17:44:02 +02:00
|
|
|
if ( utmp->ut_pid == pid )
|
|
|
|
{
|
2020-05-08 02:16:35 +02:00
|
|
|
sprintf(user, "%s", utmp->ut_user); //got user login
|
2020-05-08 13:12:40 +02:00
|
|
|
//sprintf(host_ip, "%s", utmp->ut_host); //got ip of origin
|
2020-05-08 02:16:35 +02:00
|
|
|
if((utmp->ut_addr_v6[1] && utmp->ut_addr_v6[2] && utmp->ut_addr_v6[3]) == 0)
|
|
|
|
{
|
2020-05-08 13:07:36 +02:00
|
|
|
ipv4 = utmp->ut_addr_v6[0] & 0x00000000000000ff;
|
2020-05-08 13:10:56 +02:00
|
|
|
sprintf( str, "%d.", ipv4);
|
2020-05-08 02:16:35 +02:00
|
|
|
strcat(host_ip, str);
|
2020-05-08 13:07:36 +02:00
|
|
|
ipv4 = (utmp->ut_addr_v6[0] & 0x000000000000ff00) >> 8;
|
2020-05-08 13:10:56 +02:00
|
|
|
sprintf( str, "%d.", ipv4);
|
2020-05-08 02:16:35 +02:00
|
|
|
strcat(host_ip, str);
|
2020-05-08 13:07:36 +02:00
|
|
|
ipv4 = (utmp->ut_addr_v6[0] & 0x0000000000ff0000) >> 16;
|
2020-05-08 13:10:56 +02:00
|
|
|
sprintf( str, "%d.", ipv4);
|
2020-05-08 02:16:35 +02:00
|
|
|
strcat(host_ip, str);
|
2020-05-08 13:07:36 +02:00
|
|
|
ipv4 = (utmp->ut_addr_v6[0] & 0x00000000ff000000) >> 24;
|
2020-05-08 13:10:56 +02:00
|
|
|
sprintf( str, "%d", ipv4);
|
2020-05-08 02:16:35 +02:00
|
|
|
strcat(host_ip, str);
|
|
|
|
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
for (x=0;x<4;x++)
|
|
|
|
{
|
2020-05-08 13:07:36 +02:00
|
|
|
ipv6 = utmp->ut_addr_v6[x] & 0x000000000000ffff;
|
|
|
|
sprintf( str, "%x:", ipv6);
|
|
|
|
strcat(host_ipv6, str);
|
|
|
|
ipv6 = (utmp->ut_addr_v6[x] & 0x00000000ffff0000) >> 16;
|
|
|
|
sprintf( str, "%x:", ipv6);
|
|
|
|
strcat(host_ipv6, str);
|
|
|
|
ipv6 = (utmp->ut_addr_v6[x] & 0x0000ffff00000000) >> 32;
|
|
|
|
sprintf( str, "%x:", ipv6);
|
2020-05-08 02:16:35 +02:00
|
|
|
strcat(host_ipv6, str);
|
2020-05-08 13:07:36 +02:00
|
|
|
ipv6 = (utmp->ut_addr_v6[x] & 0xffff000000000000) >> 48;
|
|
|
|
sprintf( str, "%x:", ipv6);
|
2020-05-08 02:16:35 +02:00
|
|
|
strcat(host_ipv6, str);
|
|
|
|
}
|
|
|
|
host_ipv6[strlen(host_ipv6)-1] = '\0';
|
|
|
|
}
|
|
|
|
*time = (time_t) utmp->ut_tv.tv_sec; //got connexion time
|
2020-05-07 17:48:24 +02:00
|
|
|
break;
|
2020-05-07 17:44:02 +02:00
|
|
|
}
|
2020-05-04 17:22:24 +02:00
|
|
|
}
|
2020-05-07 17:10:00 +02:00
|
|
|
endutent();
|
2020-05-04 17:22:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 02:16:35 +02:00
|
|
|
//replace null characters by space
|
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-08 02:16:35 +02:00
|
|
|
// get the childs pids
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 02:16:35 +02:00
|
|
|
int getprocinfo( int ppid, char cmdline[], char cmd[], char user[], char host_ip[], char host_ipv6[], char date[] )
|
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];
|
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 pid = ppid;
|
2020-05-06 15:23:12 +02:00
|
|
|
int level = 0;
|
|
|
|
int retval = 0;
|
2020-05-07 19:19:34 +02:00
|
|
|
time_t timet=0;
|
2020-05-04 17:22:24 +02:00
|
|
|
|
2020-05-08 02:16:35 +02:00
|
|
|
//get connexion time
|
|
|
|
getutmp( pid, user, host_ip, host_ipv6, &timet );
|
2020-05-07 19:25:37 +02:00
|
|
|
if ( timet == 0)
|
2020-05-07 19:05:41 +02:00
|
|
|
{
|
2020-05-07 19:19:34 +02:00
|
|
|
time( &timet );
|
2020-05-07 19:05:41 +02:00
|
|
|
}
|
2020-05-08 13:54:14 +02:00
|
|
|
sprintf( date, "%s", frtime(timet) );
|
2020-05-07 19:25:37 +02:00
|
|
|
date[strlen(date)-1] = 0;
|
2020-05-08 02:16:35 +02:00
|
|
|
|
|
|
|
//get the pid of the last processus
|
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;
|
2020-05-06 18:50:57 +02:00
|
|
|
retval = -1;
|
2020-05-06 15:23:12 +02:00
|
|
|
}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;
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 02:16:35 +02:00
|
|
|
|
|
|
|
// get the command parameters
|
2020-05-04 17:22:24 +02:00
|
|
|
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);
|
2020-05-08 02:16:35 +02:00
|
|
|
// get the command name
|
2020-05-04 17:22:24 +02:00
|
|
|
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);
|
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;
|
2020-05-07 13:54:04 +02:00
|
|
|
int status;
|
2020-05-06 15:23:12 +02:00
|
|
|
// 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];
|
2020-05-06 18:50:57 +02:00
|
|
|
char user[24]="";
|
2020-05-08 13:10:56 +02:00
|
|
|
char host_ip[42]="";
|
|
|
|
char host_ipv6[42]="";
|
2020-05-04 17:22:24 +02:00
|
|
|
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 19:05:56 +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 18:50:57 +02:00
|
|
|
user[0]='\0';
|
2020-05-08 02:16:35 +02:00
|
|
|
rinfo = getprocinfo( pid, cmdline, cmd, user, host_ip, host_ipv6, date );
|
2020-05-06 15:23:12 +02:00
|
|
|
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;
|
2020-05-07 19:05:41 +02:00
|
|
|
// date of connexion
|
2020-05-06 15:23:12 +02:00
|
|
|
|
2020-05-06 18:50:57 +02:00
|
|
|
if (user[0] == '\0')
|
|
|
|
{
|
2020-05-07 19:05:41 +02:00
|
|
|
sprintf( str, "%s \"%s: tunnel ouvert le %s depuis %s avec la commande: %s %s\"", commande, HOSTNAME, date, host_ip, cmd, cmdline );
|
2020-05-06 18:50:57 +02:00
|
|
|
}else
|
|
|
|
{
|
2020-05-07 19:05:41 +02:00
|
|
|
sprintf( str, "%s \"%s: %s s'est connecté le %s depuis %s avec la commande: %s %s\"", commande, HOSTNAME, user, date, host_ip, cmd, cmdline );
|
2020-05-06 18:50:57 +02:00
|
|
|
}
|
2020-05-06 15:23:12 +02:00
|
|
|
if ( start != 1 )
|
|
|
|
{
|
|
|
|
id=fork();
|
|
|
|
if(id>0)
|
|
|
|
{
|
|
|
|
if ( (fh1 = fopen(logfile, "a")) == NULL)
|
|
|
|
{
|
|
|
|
perror(logfile);
|
|
|
|
return 7;
|
|
|
|
}
|
2020-05-07 19:05:41 +02:00
|
|
|
fprintf(fh1, "%s: Connexion de %s depuis %s commande: %s %s", date, user, host_ip, cmd, cmdline);
|
2020-05-06 15:23:12 +02:00
|
|
|
fclose(fh1);
|
|
|
|
}else if (id<0)
|
|
|
|
{
|
|
|
|
printf("erreur de création du fork: %s", str);
|
|
|
|
}else
|
|
|
|
{
|
2020-05-07 14:20:41 +02:00
|
|
|
printf("%s\n", str);
|
2020-05-06 15:23:12 +02:00
|
|
|
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-07 19:25:37 +02:00
|
|
|
fprintf(fh1, "%s: %s Connecté depuis %s - %s %s\n", date, user, host_ip, 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-07 13:55:37 +02:00
|
|
|
waitpid(-1, &status ,WNOHANG);
|
2020-05-04 17:22:24 +02:00
|
|
|
}
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
2020-05-02 13:15:48 +02:00
|
|
|
}
|