commit edc716c879d506471e883628ec743fb41c1635e1 Author: root Date: Fri Dec 16 16:27:38 2022 +0700 Init commit, python+ctype connected diff --git a/.iptables_parser_lib.c.swp b/.iptables_parser_lib.c.swp new file mode 100644 index 0000000..0cdfee0 Binary files /dev/null and b/.iptables_parser_lib.c.swp differ diff --git a/.wrapper.py.swp b/.wrapper.py.swp new file mode 100644 index 0000000..32bebb1 Binary files /dev/null and b/.wrapper.py.swp differ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4f3f025 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +CC=gcc +CFLAGS= +SFLAGS=-shared -fPIC +TARGET_DIR=lib + +parser_lib.so: $(TARGET_DIR) + $(CC) $(SFLAGS) iptables_parser_lib.c -o $(TARGET_DIR)/$@ diff --git a/iptables-log.tar.gz b/iptables-log.tar.gz new file mode 100644 index 0000000..97bc3be Binary files /dev/null and b/iptables-log.tar.gz differ diff --git a/iptables_parser b/iptables_parser new file mode 100755 index 0000000..c29e7ab Binary files /dev/null and b/iptables_parser differ diff --git a/iptables_parser.c b/iptables_parser.c new file mode 100644 index 0000000..2a72224 --- /dev/null +++ b/iptables_parser.c @@ -0,0 +1,125 @@ +#include +#include + +int main() +{ + 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 *filename = "/var/log/iptables.log"; + FILE *fp = fopen(filename, "r"); + + if (fp == NULL) + { + printf("Error: could not open file %s", filename); + return 1; + } + + // 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 > h-10 ) { + + 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); + + return 0; +} diff --git a/iptables_parser_lib.c b/iptables_parser_lib.c new file mode 100644 index 0000000..cfc5144 --- /dev/null +++ b/iptables_parser_lib.c @@ -0,0 +1,218 @@ +#include +#include + +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; +} diff --git a/iptables_parser_lib.so b/iptables_parser_lib.so new file mode 100755 index 0000000..2ab32e7 Binary files /dev/null and b/iptables_parser_lib.so differ diff --git a/lib/parser_lib.so b/lib/parser_lib.so new file mode 100755 index 0000000..db37500 Binary files /dev/null and b/lib/parser_lib.so differ diff --git a/wrapper.py b/wrapper.py new file mode 100755 index 0000000..35a4a01 --- /dev/null +++ b/wrapper.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +from ctypes import * +import _ctypes + +def c_parser(log_line): + so_file = "lib/parser_lib.so" + iptablesParser = CDLL(so_file) + iptablesParser.iptablesParser.argtype = c_char_p + iptablesParser.iptablesParser.restype = c_char_p + + iptablesParser.lineParser.argtype = c_char_p + iptablesParser.lineParser.restype = c_char_p + + parser_arg = log_line.encode('utf-8') + + # c_return = iptablesParser.iptablesParser(parser_arg) + c_return = iptablesParser.lineParser(parser_arg) + + _ctypes.dlclose(iptablesParser._handle) + + # iptablesParser.freeme(c_return) + + print() + print("[ Return on Python ]"+"-"*50+"[+]") + print(c_return.decode("utf-8")) + print(c_return) + +def file_pointer(): + f = open("/var/log/iptables.log", "r") + i = 0 + for x in f: + print() + print("*"*100) + print("SEQUENCE : ",i) + print("*"*100) + print(str(i)+" -> "+x) + c_parser(str(x)) + if i >= 3: + break + i = i + 1 + +file_pointer() +