应用Asio库编写TCP扫描器
刚开始学习使用C++ BOOST库,用BOOST ASIO网络库写的一个简单command-line扫描器,原理是调用boost::asio::ip::tcp::socket类的connect成员函数连接目标主机及要扫描的(TCP)端口。函数返回boost::system::error_code类型对象ec,ec对象(通过boost::system::error_code类的转换函数)可转换成bool类型值。用语句if(ec)进行判断,当端口打开connect成功,ec==false;端口关闭connect失败,ec==true。
connect函数原型:
void connect(
const endpoint_type & peer_endpoint);
boost::system::error_code connect(
const endpoint_type & peer_endpoint,
boost::system::error_code & ec);
1
#include
<
boost
/
asio.hpp
>
2 #include < iostream >
3 #include < cstdlib >
4 #include < vector >
5
6 using boost::asio::ip::tcp;
7 using std::vector;
8
9 static inline void print_port( int i);
10 static inline void print_port( int i, bool if_print_port_closed);
11 static vector < char const *>& std_split( char * const str);
12 unsigned short scaner(tcp::socket & s,tcp::resolver & r, char const * h, char const * p);
13
14 bool if_has_underline = false ;
15
16 int main( int argc, char * argv[])
17 {
18std::cout<<"****** Tcp Detect by www.cppblog.com/jerry @ 7/21/2009 ******"<<std::endl;
19if(argc!=3)
20{
21 std::cout<<"Usage: "<<argv[0]<<" <hostname> <port>"<<std::endl;
22 std::cout<<"eg: "<<std::endl;
23 std::cout<<" "<<argv[0]<<" 192.168.12.115 110-140"<<std::endl;
24 std::cout<<" "<<argv[0]<<" 192.168.12.115 21,22,23,25,80"<<std::endl;
25 return 1;
26}
27
28try
29{
30 int port=0;
31 vector<char const*> &i_ptr=std_split(argv[2]);
32 char const *max_port="65535";
33
34 boost::asio::io_service io_service;
35 tcp::resolver resolver(io_service);
36 tcp::socket socket(io_service);
37 if(i_ptr.size()>0)
38 {
39 if(!if_has_underline)
40 {
41 for(int i=0;i<i_ptr.size();++i)
42 {
43 if(atoi(i_ptr[i])>65535 || atoi(i_ptr[i])<=0) i_ptr[i]=max_port;
44 port=scaner(socket,resolver,argv[1],i_ptr[i]);
45 if(port)
46 print_port(port);
47 else
48 print_port(atoi(i_ptr[i]),1);
49 }
50 }
51 else
52 {
53 int p=atoi(i_ptr[0]),p1=atoi(i_ptr[1]),tmp=0;
54 char str[6];
55 if(p>65535 || p<=0) p=65535;
56 if(p1>65535 || p1<0) p1=65535;
57 if(p>p1)
58 {
59 tmp=p;
60 p=p1;
61 p1=tmp;
62 }
63 for(int i=p;i<=p1;++i)
64 {
65 port=scaner(socket,resolver,argv[1],itoa(i,str,10));
66 if(port)
67 print_port(port);
68 else
69 print_port(i,1);
70 }
71 }
72 }
73}
74catch(std::exception &e)
75{
76 std::cout<<e.what()<<std::endl;
77 return -1;
78}
79
80return 0;
81}
82
83 static inline void print_port( int i)
84 {
85std::cout<<i<<"\topened\t=====> good!"<<std::endl;
86}
87 static inline void print_port( int i, bool if_print_port_closed)
88 {
89if(if_print_port_closed)
90 std::cout<<i<<"\tclosed"<<std::endl;
91else
92 std::cout<<i<<"\topened\t=====> good!"<<std::endl;
93}
94
95 static vector < char const *>& std_split( char * const str)
96 {
97//static char flag[5]="flag";
98vector<char const*>* char_ptr_vec=new vector<char const*>();
99unsigned count=0,length=strlen(str);
100for(int i=0;i<length;++i)
101{
102 if(*(str+i)==',')
103 {
104 *(str+i)=0;
105 char_ptr_vec->push_back(str+count);
106 count=i+1;
107 }
108 else if(*(str+i)=='-')
109 {
110 *(str+i)=0;
111 char_ptr_vec->push_back(str);
112 count=i+1;
113 char_ptr_vec->push_back(str+count);
114 if_has_underline=true;
115 //push the third element as a flag
116 //char_ptr_vec->push_back(flag);
117 break;
118 }
119}
120
121//if(count==0) char_ptr_vec.push_back(str);
122char_ptr_vec->push_back(str+count);
123return *char_ptr_vec;
124}
125
126 unsigned short scaner(tcp::socket & s,tcp::resolver & r, char const * h, char const * p)
127 {
128try
129{
130 tcp::resolver::query query(h,p);
131 tcp::resolver::iterator endpoint_iterator=r.resolve(query);
132 tcp::resolver::iterator end;
133 boost::system::error_code error=boost::asio::error::host_not_found;
134
135 while(error && endpoint_iterator!=end)
136 {
137 s.close();
138 s.connect(*endpoint_iterator++,error);
139 }
140 if(error)
141 {
142 return 0;
143 //throw boost::system::error_code(error);
144 }
145}
146catch(std::exception &e)
147{
148 std::cout<<e.what()<<std::endl;
149 return 0;
150}
151
152return atoi(p);
153}
2 #include < iostream >
3 #include < cstdlib >
4 #include < vector >
5
6 using boost::asio::ip::tcp;
7 using std::vector;
8
9 static inline void print_port( int i);
10 static inline void print_port( int i, bool if_print_port_closed);
11 static vector < char const *>& std_split( char * const str);
12 unsigned short scaner(tcp::socket & s,tcp::resolver & r, char const * h, char const * p);
13
14 bool if_has_underline = false ;
15
16 int main( int argc, char * argv[])
17 {
18std::cout<<"****** Tcp Detect by www.cppblog.com/jerry @ 7/21/2009 ******"<<std::endl;
19if(argc!=3)
20{
21 std::cout<<"Usage: "<<argv[0]<<" <hostname> <port>"<<std::endl;
22 std::cout<<"eg: "<<std::endl;
23 std::cout<<" "<<argv[0]<<" 192.168.12.115 110-140"<<std::endl;
24 std::cout<<" "<<argv[0]<<" 192.168.12.115 21,22,23,25,80"<<std::endl;
25 return 1;
26}
27
28try
29{
30 int port=0;
31 vector<char const*> &i_ptr=std_split(argv[2]);
32 char const *max_port="65535";
33
34 boost::asio::io_service io_service;
35 tcp::resolver resolver(io_service);
36 tcp::socket socket(io_service);
37 if(i_ptr.size()>0)
38 {
39 if(!if_has_underline)
40 {
41 for(int i=0;i<i_ptr.size();++i)
42 {
43 if(atoi(i_ptr[i])>65535 || atoi(i_ptr[i])<=0) i_ptr[i]=max_port;
44 port=scaner(socket,resolver,argv[1],i_ptr[i]);
45 if(port)
46 print_port(port);
47 else
48 print_port(atoi(i_ptr[i]),1);
49 }
50 }
51 else
52 {
53 int p=atoi(i_ptr[0]),p1=atoi(i_ptr[1]),tmp=0;
54 char str[6];
55 if(p>65535 || p<=0) p=65535;
56 if(p1>65535 || p1<0) p1=65535;
57 if(p>p1)
58 {
59 tmp=p;
60 p=p1;
61 p1=tmp;
62 }
63 for(int i=p;i<=p1;++i)
64 {
65 port=scaner(socket,resolver,argv[1],itoa(i,str,10));
66 if(port)
67 print_port(port);
68 else
69 print_port(i,1);
70 }
71 }
72 }
73}
74catch(std::exception &e)
75{
76 std::cout<<e.what()<<std::endl;
77 return -1;
78}
79
80return 0;
81}
82
83 static inline void print_port( int i)
84 {
85std::cout<<i<<"\topened\t=====> good!"<<std::endl;
86}
87 static inline void print_port( int i, bool if_print_port_closed)
88 {
89if(if_print_port_closed)
90 std::cout<<i<<"\tclosed"<<std::endl;
91else
92 std::cout<<i<<"\topened\t=====> good!"<<std::endl;
93}
94
95 static vector < char const *>& std_split( char * const str)
96 {
97//static char flag[5]="flag";
98vector<char const*>* char_ptr_vec=new vector<char const*>();
99unsigned count=0,length=strlen(str);
100for(int i=0;i<length;++i)
101{
102 if(*(str+i)==',')
103 {
104 *(str+i)=0;
105 char_ptr_vec->push_back(str+count);
106 count=i+1;
107 }
108 else if(*(str+i)=='-')
109 {
110 *(str+i)=0;
111 char_ptr_vec->push_back(str);
112 count=i+1;
113 char_ptr_vec->push_back(str+count);
114 if_has_underline=true;
115 //push the third element as a flag
116 //char_ptr_vec->push_back(flag);
117 break;
118 }
119}
120
121//if(count==0) char_ptr_vec.push_back(str);
122char_ptr_vec->push_back(str+count);
123return *char_ptr_vec;
124}
125
126 unsigned short scaner(tcp::socket & s,tcp::resolver & r, char const * h, char const * p)
127 {
128try
129{
130 tcp::resolver::query query(h,p);
131 tcp::resolver::iterator endpoint_iterator=r.resolve(query);
132 tcp::resolver::iterator end;
133 boost::system::error_code error=boost::asio::error::host_not_found;
134
135 while(error && endpoint_iterator!=end)
136 {
137 s.close();
138 s.connect(*endpoint_iterator++,error);
139 }
140 if(error)
141 {
142 return 0;
143 //throw boost::system::error_code(error);
144 }
145}
146catch(std::exception &e)
147{
148 std::cout<<e.what()<<std::endl;
149 return 0;
150}
151
152return atoi(p);
153}