Security_Task.c

A security task program in C. Where it detects Failed logons with the auth.log file in Linux where you install fail2ban and "apt-get install rsyslog" before . Just like the Bash and Python version. However, it does nothing but print to screen. It's up to you to add functionality. I prefer the Python version because it is simpler to code and above all I don't have to allocate memory, and it's easier to manipulate strings. C might be fast, but it is not user-friendly.

Security_Task.c on Replit.com

Security_Task.c_MySQL

A security task program in C modified to include MySQL. Where it detects Failed logons with the auth.log file in Linux. Just like the other version before this version. However, it now includes MySQL, which I use exclusively in Linux.

You will need "apt-get install rsyslog" first for the auth.log and fail2ban also installed, and then you have to use this to install the MySQL libraries. Which is

sudo apt install default-libmysqlclient-dev


And it has to use the C99 options when compiling which would be

gcc -I/usr/include/mysql main.c -L/usr/lib/mysql -lmysqlclient -o main



Securitytask.c MySQL

New and improved c code for security task program includes MySQL.

									#include <mysql.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

void finish_with_error(MYSQL *con)
{
  fprintf(stderr, "%s
", mysql_error(con));
  mysql_close(con);
  exit(1);
}

void save(char *str)
{
    MYSQL *con = mysql_init(NULL);

    if (con == NULL)
    {
      fprintf(stderr, "%s
", mysql_error(con));
      exit(1);
    }
    
    if (mysql_real_connect(con, "localhost", "USER", "PASSWORD",
          "DATABASE", 0, NULL, 0) == NULL)
    {
      finish_with_error(con);
    }
    /* Variable to store user content */
    char data[1024];

    snprintf(data, sizeof(data), "%s", str);

    /* File pointer to hold reference to our file */
    FILE * fPtr;

    /*
     * Open file in w (write) mode.
     * "data/file1.txt" is complete path to create file
     */
    fPtr = fopen("failed.txt", "a");

    /* fopen() return NULL if last operation was unsuccessful */
    if(fPtr == NULL)
    {
        /* File not created hence exit */
        printf("Unable to create file.
");
        exit(EXIT_FAILURE);
    }

    /* Write data to file */
    fputs(data, fPtr);
    
    char buffer[1024];
    snprintf(buffer, sizeof(buffer), "INSERT INTO failed (message) VALUES ('%s')", str);
    
     if (mysql_query(con, buffer )) {
      finish_with_error(con);
     }
    mysql_close(con);
      /* Close file to save file data */
    fclose(fPtr);

  /* Success message */
    printf("File created and saved successfully. :) 
");
}

/* Signal Handler for SIGINT */
void sigint_handler(int sig_num)
{
    /* Reset handler to catch SIGINT next time.
       Refer http://en.cppreference.com/w/c/program/signal */
    printf("
 User provided signal handler for Ctrl+C 
");

    /* Do a graceful cleanup of the program like: free memory/resources/etc and exit */
    exit(0);
}

char *Security(char *d)
{
    char *r;
    r = malloc(200);
    //char r[1024]= "�";
    char cmd[200]= "�";
    snprintf(cmd, sizeof(cmd), "awk 'match($6,/Failed/) && /%s/ {print $1, $2, $3,$6,$7,$9,$11,$12,$13,$14,$15}' /var/log/auth.log", d);
    //printf("%s
",cmd);
    FILE *fp;
    free(r);
    if ((fp = popen(cmd,"r")) == NULL)
    {}
    else
        //return "none";
        // print_all(fp);
        snprintf(r,sizeof(r),"%s","");
    {
        int c;
        while ((c = getc(fp)) != EOF)
            strncat(r, &c, 1);
    }
    pclose(fp);
    return r;
}

char *Month(int choice)
{

    char *str;
    switch(choice)
    {
    case 1:
        str="Jan";
        break;
    case 2:
        str="Feb";
        break;
    case 3:
        str="Mar";
        break;
    case 4:
        str="Apr";
        break;
    case 5:
        str="May";
        break;
    case 6:
        str="Jun";
        break;
    case 7:
        str="Jul";
        break;
    case 8:
        str="Aug";
        break;
    case 9:
        str="Sep";
        break;
    case 10:
        str="Oct";
        break;
    case 11:
        str="Nov";
        break;
    case 12:
        str="Dec";
        break;
    default :
        printf("invalid number");
    }

    return str;
}

int main(int argc, char **argv)
{
    signal(SIGINT, sigint_handler);

    printf("MySQL client version: %s
", mysql_get_client_info());
    
    /* Infinite loop */
    while(1)
    {
        time_t t = time(NULL);
        struct tm tm = *localtime(&t);
        // printf("now: %d-%02d-%02d %02d:%02d:%02d
", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

// printf("Date and time: %s %d %02d:%02d:%02d
",Month(tm.tm_mon+1),tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec);
        // snprintf(d, sizeof(d), "%s %d %d:%d:%d", Month(tm.tm_mon+1), tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec);
        // d=Month(tm.tm_mon+1);
        char d[2000];
        char day[100];
        char hour[100];
        char minute[100];
        char second[100];
        sprintf(day,"%2d",tm.tm_mday);
        sprintf(hour, "%02d",tm.tm_hour);
        sprintf(minute, "%02d",tm.tm_min);
        sprintf(second, "%02d", tm.tm_sec);
        snprintf(d, sizeof(d), "%s %s %s:%s:%s", Month(tm.tm_mon+1),day,hour, minute, second);
        //nprintf(d, sizeof(d), "%s", "Oct 22 14:22:54");
        //printf("%s
",d);
        // char *sec=Security(d);
        //strncpy( sec, Security(d), sizeof( sec ) );
        char sec[125]= "�";
        snprintf(sec,sizeof(sec),"%s",Security(d));

        if (strlen(sec)!=0)
        {
            printf("%s
",d);
            printf("%s
",sec);
            sleep(1);
            save(sec);
        }
      }
    return 0;
}