This program is developed in ubuntu gccto execute this program use the following command
“gcc $(mysql_config –cflags) fmysql.c $(mysql_config –libs)”
for output “./a.out”
C program that connects to a MySQL server and checks intrusion attempts every 5 minutes. If an intrusion attempt is detected beep the internal speaker to alert the administrator.
#include<stdio.h> #include<stdlib.h> #include<mysql.h> int cnt=0; //count the no of while(1) loop execution int ct=0; int gt=0; int x=0; //hold the total result of table int i=0; //hold th current result of table int fval=0; //hold the no. of new instruction in table char *lcl_server;//mysql server location char *mysql_user; //mysql user name char *mysql_password; //mysql password char *mysql_dd = "mysql"; //mysql database main() { MYSQL *mysql_con; //for make the connection with mysql MYSQL_RES *mysql_res;// get the result MYSQL_ROW rw; //for get the no. of row to fetched system("clear"); //clear the screen printf("\n\n********** MySQL Intrusion detection in C **********"); printf("\n Server Location and Login"); printf("\n******************************************************"); lcl_server = (char *) malloc (35); //create the memory mysql_user=(char *)malloc(50); //create the memory mysql_password=(char *)malloc(50); //create the memory printf("\n Enter Server Location::"); scanf("%s",lcl_server); printf("\n Enter user name::"); scanf("%s",mysql_user); printf("\n Enter %s password::",mysql_user); scanf("%s",mysql_password); system("clear"); flag(); ct=0; while(1) //loop for checking the new instruction will arrive in mysql server { mysql_con = mysql_init('\0'); //init the server to null printf("\n\n********** MySQL Intrusion detection in C **********"); //connect the server and check the connection and error printf("\n\n Try To Build connection with '%2s' Database...",mysql_dd); if(!mysql_real_connect(mysql_con, lcl_server,mysql_user, mysql_password, mysql_dd, 0, NULL, 0)) { printf("\n %s", mysql_error(mysql_con)); //show the error exit(0); //exit } else { printf("\n\n '%s' Database is Ready to use...",mysql_dd); //display the msg } printf("\n\n Changing the log variable value...."); if(mysql_query(mysql_con,"set global log_output='TABLE'"))//setting the varible in //__mysql { printf("\n%s", mysql_error(mysql_con)); exit(0); } else { printf("\n\n Log variable value is set to 'Table'..."); } printf("\n\n Detecting Intrusion..."); if(mysql_query(mysql_con, "select * from general_log")) //execute the select query //__in general_log table in mysql database { printf("\n%s", mysql_error(mysql_con)); exit(0); } mysql_res = mysql_use_result(mysql_con); //get the result of query i=0; while((rw = mysql_fetch_row(mysql_res)) != NULL) //reading the recored at last { i=i+1; //increment the i counter } cnt=cnt+1; //increment the cnt counter fval = i-(cnt*4); //fval hold the count of new instruction in mysql_ //_not this program intrusion executed in mysql if(x!=fval-1) //compare the count of intrusion if new instruction__ //__condition is true will found then { cnt=0; printf("\a\a\n\n Instruction has been Detected...\a\a"); //give the msg and beep //to administrator flag(); //get the current count of_ //_intrusion in x } mysql_free_result(mysql_res);// free the result varible mysql_close(mysql_con); //close the connection printf("\n\nPress ctrl+z to exit..."); //msg for exit printf("\n"); // sleep(18000); //5 Min. wait sleep(5); system("clear"); } } flag() { MYSQL *mysql_con; MYSQL_RES *mysql_res; MYSQL_ROW rw; i=0; x=0; mysql_con = mysql_init(NULL); if(!mysql_real_connect(mysql_con, lcl_server,mysql_user, mysql_password, mysql_dd, 0, NULL, 0)) { printf("%s\n", mysql_error(mysql_con)); exit(0); } mysql_query(mysql_con,"set global log_output='TABLE'"); if (mysql_query(mysql_con, "select * from general_log")) { printf("%s\n", mysql_error(mysql_con)); exit(0); } mysql_res = mysql_use_result(mysql_con); while ((rw = mysql_fetch_row(mysql_res)) != NULL) { i=i+1; } x=i; mysql_free_result(mysql_res); mysql_close(mysql_con); }
sir where i have to write these code.i was writing in turbo c.but this gives error that what is mysql.h lib.tell me first step to do and execute this program
Hi adarsh,
Basically I used ubuntu GCC for developing this program first you need to install my-sql in Ubuntu by running this command
“sudo apt-get install mysql-server”
. Then you need to include the
“mysql.h”
header file.
Thanks.
sir thank you..one company asking me to write c/c++ code to connect to sql server….only select command should work….i have window 7.can you give me full code and path to run that program…
if i will give him than only they will select me….my email id@ piyushanand26@gmail.com
I tried out this program but i got error: expected ‘;’ before ‘{’ token at line num 94 can u help me out to solve this…