#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
MYSQL sql ;
int main()
{
int i ;
int pid ;
reset();
system("snort -D -c /etc/snort/snort.conf");
system("/usr/local/bin/autorule");
pid=fork();
if (pid == 0)
{
printf("child_pid==%d\n",getpid());
printf("pid==%d\n",pid);
// reset_x();
// reset_x()
db();
int j ;
for (j=0;;j++)
{
printf("imamamabayuganteng");
usleep(1000000000);
system("iptables -F");
system("iptables -X");
mysql_query(&sql, "delete from tersangka");
mysql_close(&sql);
system("/usr/local/bin/autorule");
}
exit(0);
}
for(i=0;;i++)
{
printf(" check: %i\n",i);
adap();
// sleep 0,01 detik
usleep(10000) ;
}
}
reset()
{
db();
system("iptables -F");
system("iptables -X");
mysql_query(&sql, "delete from tersangka");
mysql_close(&sql);
}
/*
reset_x()
{
db();
int j ;
// int pid ;
// pid=fork();
for (j=0;;j++)
{
// pid=fork();
usleep(10000000);
system("iptables -F");
system("iptables -X");
mysql_query(&sql, "delete from tersangka");
mysql_close(&sql);
//exit(0);
// }
}
*/
adap()
{
MYSQL_ROW row;
MYSQL_RES *res ;
char slack[100] ;
char slk[100],percaya[100],slkxxx[100],prokom[100],prokomxxx[100] ;
char scx[100],cmdscx1[100],cmdscx2[100],ksg1[100];
long int sc;
//long int *satu ;
//satu= (long int *) malloc(50);
db();
sprintf(slack,"SELECT * FROM iphdr order by sid desc limit 1");
mysql_query(&sql, slack);
if(!(res = mysql_store_result(&sql)))
{
printf ("query iphdr gagal\n");
exit(-1);
}
row = mysql_fetch_row(res);
if(row!=0)
{
sc= atoi(row[2]);
// satu = &sc ;
printf("%d.%d.%d.%d\n", (sc & 0xff000000) >> 24, (sc & 0xff0000) >> 16, (sc & 0xff00) >> 8,
(sc & 0xff));
sprintf(scx, "%d.%d.%d.%d", (sc & 0xff000000) >> 24, (sc & 0xff0000) >> 16,(sc & 0xff00) >> 8, (sc & 0xff));
// mysql_free_result(res);
mysql_free_result(res);
mysql_close(&sql);
//free(satu) ;
// }
/* pembanding */
db();
sprintf(percaya,"SELECT ip FROM trusted_ip where ip=\'%s\'",scx);
mysql_query(&sql,percaya);
if(!(res= mysql_store_result(&sql)))
{
printf("query trusted_ip gagal\n");
exit(-1);
}
row = mysql_fetch_row(res);
mysql_free_result(res);
mysql_close(&sql);
if(row==0)
{
db();
sprintf(slk,"SELECT *FROM tersangka where ip=\"%s\"",scx);
//printf ("test\n") ;
mysql_query(&sql,slk);
if(!(res= mysql_store_result(&sql)))
{
printf ("query tersangka gagal\n");
exit(-1);
}
row = mysql_fetch_row(res);
if(row==0)
{
printf ("dapet\n") ;
sprintf(slkxxx,"insert into tersangka values(\"%s\",\"%s\",\"\")",ksg1,scx);
mysql_query(&sql,slkxxx);
sprintf(prokom,"SELECT * FROM tersangka order by id desc limit 1");
mysql_query(&sql,prokom);
res = mysql_store_result(&sql);
row = mysql_fetch_row(res);
strcpy(prokomxxx,row[1]);
printf ("ip-src: %s\n",prokomxxx);
sprintf( cmdscx1, "/usr/sbin/iptables -A BANNED -s %s -j DROP", prokomxxx);
sprintf( cmdscx2, "/usr/sbin/iptables -A FORWARD -s %s -j DROP",prokomxxx);
system(cmdscx1);
system(cmdscx2);
//mysql_close(&sql);
mysql_free_result(res);
mysql_close(&sql);
//free(satu);
}
else
{
mysql_free_result(res);
mysql_close(&sql);
//free(satu);
}
}
else
{
printf(" ip dapat dipercaya\n");
}
}
//mysql_free_result(res);
mysql_close(&sql);
}
db()
{
mysql_init(&sql);
if(!(mysql_real_connect(&sql,"localhost","root",NULL,"",0,NULL,0)))
{
printf("gagal");
}
mysql_select_db(&sql,"snort");
printf("\n -");
}