一个分析Linux ARP日志的源代码程序
#include #include #include #include #include #include #include using namespace std;typedef list ListStr;typedef set SetStr;typedef ListStr::iterator Iter;typedef SetStr::iterator s_iter;typedef basic_
#include <iostream>
#include <string>
#include <fstream>
#include <list>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
typedef list<string> ListStr;
typedef set<string> SetStr;
typedef ListStr::iterator Iter;
typedef SetStr::iterator s_iter;
typedef basic_string<char>::size_type S_T;
static S_T npos = -1;
string CONFIG_FILE;
string LOG_FILE;
const string DEFAULT_CONFIG_FILE="/root/config.txt";
const string DEFAULT_LOG_FILE="/root/log.log";
//Func Define
void splitstr(const string& _source,const string& _split,ListStr& _ls);
vector<string> split(const string& src, string delimit, string null_subst);
bool IsInMACAndIP(const ListStr& _ls,const string& _s);
int ParseArg(int argc,char *argv[]);
string getip(const string& _s);
string getmac(const string& _s);
int main(int argc,char *argv[]){
/
//Define Data
/
ListStr IPAndMac;
ListStr LogStr;
ListStr ExecLogStrCount;
SetStr ExecLogStr;
Iter i_im;
Iter i_ls;
s_iter s_i;
//
//Initial Data
//
//
int i_r = ParseArg(argc,argv);
if(i_r <= 0){
cout << "Error Params" << endl;
exit(0);
}else{
fstream fs_config(CONFIG_FILE.c_str());
//cout << CONFIG_FILE << endl;
fstream fs_log(LOG_FILE.c_str());
//cout << LOG_FILE << endl;
IPAndMac.clear();
LogStr.clear();
ExecLogStr.clear();
ExecLogStr.clear();
///
//Exception
///
if(!fs_config ){
cout <<"Can not Open Config File " <<CONFIG_FILE << endl;
exit(0);
}
if(!fs_log){
cout <<" Can not Open Log File " << LOG_FILE << endl;
exit(0);
}
else{
//Read Config
///
string s_config_line;
while(getline(fs_config,s_config_line)){
IPAndMac.push_back(s_config_line);
}
//
//Read Log
string s_log_line;
while(getline(fs_log,s_log_line)){
LogStr.push_back(s_log_line);
}
//
//Do Test Data
/
// NOw Get All Default Mac And Data
//For LogStr
//Get ARP IP And Mac
//Is In IP And Mac
//if In
//else not >>>>>> Arp Attack Cout IP And Address
for(i_ls = LogStr.begin(); i_ls != LogStr.end() ; ++ i_ls){
vector<string> v = split(*i_ls," ","");
string s_IP = v[9];
string s_MAC = v[1];
string s_temp = s_MAC + "=" + s_IP;
ExecLogStr.insert(s_temp);
ExecLogStrCount.push_back(s_temp);
}
for(s_i = ExecLogStr.begin(); s_i != ExecLogStr.end(); ++ s_i){
if(!IsInMACAndIP(IPAndMac,*s_i)){
int i_num = count(ExecLogStrCount.begin(),ExecLogStrCount.end(),*s_i);
cout << "The Computer@@@" << *s_i <<"@@@Attack@@@" <<i_num <<"@@@Times!"<< endl;
}
}
}
return 0;
}
}
string getip(const string& _s){
return "";
}
string getmac(const string& _s){
return "";
}
void splitstr(const string& _source,const string& _split,ListStr& _ls){
//todo:
_ls.clear();
string strtemp;
strtemp = _source;
string::size_type len = _source.length();
string::size_type itmp = strtemp.find(_split);
do{
_ls.push_back(strtemp.substr(0,itmp));
//cout << strtemp.substr(0,itmp) << endl;
if(len-itmp-1 > 0){
//cout << strtemp << endl;
strtemp = strtemp.substr(itmp+1,len-itmp-1);
//cout << strtemp << endl;
itmp = strtemp.find(_split);
len = strtemp.length();
}
else{
cout << 2 << endl;
break;
}
}while( itmp>0 );
//ListStr.push_back(_source.substr(0,i1));
//cout << _source.substr(0,i1) << endl;
//cout << _source.substr(i1+1,len-i1-2)<< endl;
//cout << il << endl;
}
bool IsInMACAndIP(const ListStr& _ls,const string& _s){
ListStr::const_iterator i = find(_ls.begin(),_ls.end(),_s);
if(i == _ls.end()){
return false;
}
else{
return true;
}
}
vector<string> split(const string& src, string delimit, string null_subst="")
{
if( src.empty() || delimit.empty() ) throw "split: empty string/0";
vector<string> v;
S_T deli_len = delimit.size();
long index = npos, last_search_position = 0;
while( (index=src.find(delimit, last_search_position))!=npos )
{
if(index==last_search_position)
v.push_back(null_subst);
else
v.push_back( src.substr(last_search_position, index-last_search_position) );
last_search_position = index + deli_len;
}
string last_one = src.substr(last_search_position);
v.push_back( last_one.empty()? null_subst:last_one );
return v;
}
int ParseArg(int argc,char *argv[]){
/*
for(int i = 0 ; i < argc ; i++)
{
//cout << argc << endl;
cout << argv[i] << endl;
}
*/
if(argc == 1){
CONFIG_FILE = DEFAULT_CONFIG_FILE;
LOG_FILE = DEFAULT_LOG_FILE;
return argc;
}
else if(argc == 3){
//cout << 3333 << endl;
char tbuf[256];
strcpy(tbuf,argv[1]);
string s_temp = (string)(tbuf);
if(s_temp == "-c"){
//cout << 1111 << endl;
char buf[256];
strcpy(buf,argv[2]);
CONFIG_FILE = (string)(buf);
LOG_FILE = DEFAULT_LOG_FILE;
//cout << CONFIG_FILE << endl;
return argc;
}
if(argv[1] == "-l"){
char buf[256];
strcpy(buf,argv[2]);
LOG_FILE = (string)(buf);
CONFIG_FILE = DEFAULT_CONFIG_FILE;
//cout << LOG_FILE << endl;
return argc;
}
}
else if(argc == 5){
char tbuf1[256];
strcpy(tbuf1,argv[1]);
string s_temp1 = (string)(tbuf1);
char tbuf2[256];
strcpy(tbuf2,argv[3]);
string s_temp2 = (string)(tbuf2);
if(s_temp1 == "-c" || s_temp1 == "-C")
CONFIG_FILE = (string)argv[2];
if(s_temp2 == "-l" || s_temp2 == "-L")
LOG_FILE = (string)argv[4];
//cout << CONFIG_FILE << "@@@@@" << LOG_FILE << endl;
return argc;
}
else{
return -1;
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)