iptables-parser/iptables_parser_lib.c

219 lines
5.6 KiB
C
Raw Normal View History

2023-10-24 07:04:36 -04:00
#include <stdio.h>
#include <string.h>
int h,i,j,k,l,v,w,x,y,ctr,dtr;
char str_in[200];
char newString[200][200];
char newParam[100][100];
char str_final[50];
char * lineParser(char * py_args)
{
// char newParam[100][100];
// char newString[100][100];
// char str_in[100];
//
char * ret_str = malloc(strlen(py_args));
j=0; ctr=0;
printf("\n%d -> %s \n", i, py_args);
printf("[%d]\t[ Param ]\t\t[ Value ] \n", i);
for (x=0; x <= (strlen(py_args)); x++)
{
if(py_args[x]==' '|| py_args[x]=='\0' || py_args[x]=='\n'){
newString[ctr][j]='\0';
ctr++;
j=0;
}
else {
newString[ctr][j]=py_args[x];
j++;
}
}
for (v=0; v <= ctr ; v++) {
strcpy(str_in, newString[v]);
k=0; dtr=0;
memset(newParam, 0, sizeof newParam);
for (w=0; w <= (strlen(str_in)) ; w++) {
if(str_in[w]=='='){
newParam[dtr][k]='\0';
dtr++;
k=0;
}
// else if(str_in[w]!='\n' && str_in[w]!=' ') {
else {
newParam[dtr][k]=str_in[w];
k++;
}
}
strcpy(str_final, newParam[0]);
strcat(ret_str, newParam[0]);
strcat(ret_str, "=");
// if ( newParam[0] && newParam[0][0] != "\n" ) {
if ( *str_final == EOF ) {
// if strcmp(&str_final, '\n') {
continue;
}
strcat(ret_str, newParam[1]);
strcat(ret_str, ";");
printf("[%d]\t[ %s ]\t\t[ %s ] \n", v, str_final, ret_str);
}
// printf("--------------------------------------------------------------------------------------------\n");
// printf("[%d]\t[ %s ]\t\t[ %s ] \n", v, str_final, newParam[1]);
// printf("--------------------------------------------------------------------------------------------\n");
char * fucker = malloc(sizeof(ret_str));
printf("\n==================================================================================\n");
strcat(fucker, ret_str);
// memcpy(fucker, "Fuckery", sizeof("Fuckery"));
printf(" Local value : %s\n", ret_str);
printf(" PY value : %s\n", py_args);
free(ret_str);
// free(fucker);
return fucker;
}
const char * iptablesParser(char * py_args)
{
char *filename = "/var/log/iptables.log";
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error: could not open file %s", filename);
return "Error";
}
// reading line by line, max 256 bytes
const unsigned MAX_LENGTH = 256;
char buffer[MAX_LENGTH];
h = 0;
while (fgets(buffer, MAX_LENGTH, fp)) {
h = h + 1;
}
printf(" %d\n ", h);
i = 0;
fclose(fp);
fp = fopen(filename, "r");
while (fgets(buffer, MAX_LENGTH, fp)) {
// printf("- %d -", i);
if ( i < 20 ) {
j=0; ctr=0;
printf("\n%d -> %s \n", i, buffer);
printf("[%d]\t[ Param ]\t\t[ Value ] \n", i);
for (x=0; x <= (strlen(buffer)); x++)
{
if(buffer[x]==' '|| buffer[x]=='\0' || buffer[x]=='\n'){
newString[ctr][j]='\0';
ctr++;
j=0;
}
else {
newString[ctr][j]=buffer[x];
j++;
}
}
for (v=0; v <= ctr ; v++) {
// printf("[ %d ]----> %s \n", v, newString[v]);
strcpy(str_in, newString[v]);
// printf("\n[%d]----> %s \n", i, str_in);
k=0; dtr=0;
memset(newParam, 0, sizeof newParam);
for (w=0; w <= (strlen(str_in)) ; w++) {
if(str_in[w]=='='){
newParam[dtr][k]='\0';
dtr++;
k=0;
}
// else if(str_in[w]!='\n' && str_in[w]!=' ') {
else {
newParam[dtr][k]=str_in[w];
k++;
}
}
strcpy(str_final, newParam[0]);
// if ( newParam[0] && newParam[0][0] != "\n" ) {
if ( *str_final == EOF ) {
// if strcmp(&str_final, '\n') {
continue;
}
printf("[%d]\t[ %s ]\t\t[ %s ] \n", v, str_final, newParam[1]);
// printf("[ Value %d]----> %s \n", i, newParam[1]);
// for (l=0; l <= dtr ; l++) {
// printf("[ + %d]----> %s \n", w, newParam[l]);
// }
}
// j=0; ctr=0;
// for (v=0; v <= (strlen(newString)); v++) {
// if(newString[v]=='='){
// newParam[ctr][j]='\0';
// ctr++;
// j=0;
// }
// else {
// newParam[ctr][j]=newString[v];
// j++;
// }
// }
}
i = i +1 ;
// printf("\n==================================================================================\n");
}
// close the file
fclose(fp);
char * fucker = malloc(sizeof("Fuckery"));
printf("\n==================================================================================\n");
strcpy(fucker, "Fuckery");
// memcpy(fucker, "Fuckery", sizeof("Fuckery"));
printf(" Local value : %s\n", fucker);
printf(" PY value : %s\n", py_args);
return str_final;
}