当前位置:首页 >> 高中作文 >>

代码


#include <vector> #include <math.h> #include <bitset> #include <iostream> #include <winsock2.h> #include <mstcpip.h> #include <ntddndis.h> #include <windows.h> #pragma comment( lib, "ws2_32.lib" ) using namespace std; struct IPHeader { unsigned char mHeaderLengthAndVersion; unsigned char mTypeOfService; unsigned short mTotalLength; unsigned short mIdentifier; unsigned short mFragment; unsigned char mTTL; unsigned char mProtocolType; unsigned short mCheckSum; unsigned int mSourceIP; unsigned int mDestIP; int getHeaderLength() const { return mHeaderLengthAndVersion & 0xf; } }; struct TCPHeader { unsigned short mSourcePort; unsigned short mDestPort; unsigned int mSequence; unsigned int mAck; unsigned char mLengthAndReserve; unsigned char mFlags; unsigned short mWindow; unsigned short mCheckSum; unsigned short mUrgent; }; struct UDPHeader { unsigned short mSourcePort; unsigned short mDestPort; unsigned short mLength;

unsigned short mCheckSum; }; struct ICMPHeader { unsigned char mType; unsigned char mCode; unsigned short mCheckSum; unsigned short mID; unsigned short mSequence; unsigned long mTimeStamp; }; void ProcessIPPacket( char*, int ); void ProcessTCPPacket( char*, int ); void ProcessUDPPacket( char*, int ); void ProcessICMPPacket( char*, int ); int Welcome(); void SocketDis(); void PortScan(); void main(int argc, char *argv[]) { int choice=0; if(argc == 1){ choice = Welcome(); } else if(argc == 2){ choice = atoi (argv[1]); } try{ if(1!=choice&&2!=choice) throw "CHOICE Error!"; switch(choice){ case 1: SocketDis(); break; case 2: PortScan(); break; } } catch(char* inf){ cout << inf; system("pause"); } } int Welcome(){ cout << "1.Catch socket " << endl << "2.Scan port" << endl; cout << "Please choice :"; // 支持命令含参数

// 错误处理

int temp ; cin >> temp; return temp; } void SocketDis(){ WSADATA data; WSAStartup( MAKEWORD( 2, 2 ), &data ); SOCKET rawSock = socket( AF_INET, SOCK_RAW, IPPROTO_IP ); char name[ 1024 ]; gethostname( name, sizeof( name ) ); hostent* ent = gethostbyname( name ); SOCKADDR_IN addr; addr.sin_family = AF_INET; addr.sin_port = htons( 8888 ); memcpy( &addr.sin_addr.S_un.S_addr, ent->h_addr_list[ 0 ], ent->h_length ); bind( rawSock, (PSOCKADDR)&addr, sizeof( addr ) ); DWORD dummy; WSAIoctl( rawSock, SIO_RCVALL, &dummy, sizeof( dummy ), &dummy, sizeof( dummy ), &dummy, NULL , NULL ); //The WSAIoctl function controls the mode of a socket. for(;;) { ReadConsoleInput( hIn , &Buf , 1 , &Result ); // vector<char> vc( 128 * 1024 ); ProcessIPPacket( &vc[0], recv( rawSock, &vc[0], (int)vc.size(), 0) ); } } void PrintIP(int ip){ bitset<32> bit(ip); bitset<8> tempbit(0); long temp=0; for(int i=0; i<32; ++i){ if( i%8==0||i==31){ temp = tempbit.to_ulong(); if(temp != 0) cout << temp << "."; } tempbit[(i)%8] = bit[i]; } cout << "\b " << endl; } //convert ip(int) to ip(bite)

void ProcessIPPacket( char* packet, int size ) { IPHeader* header = (IPHeader*)packet; cout << "The source IP is :"; PrintIP(header->mSourceIP); cout << "The destination IP is :"; PrintIP(header->mDestIP); if( header->mProtocolType == IPPROTO_TCP ) ProcessTCPPacket( packet + header->getHeaderLength(), size - header->getHeaderLength() ); else if( header->mProtocolType == IPPROTO_UDP ) ProcessUDPPacket( packet + header->getHeaderLength(), size - header->getHeaderLength() ); else if( header->mProtocolType == IPPROTO_ICMP ) ProcessICMPPacket( packet + header->getHeaderLength(), size - header->getHeaderLength() ); } void ProcessTCPPacket( char* packet, int size ) { TCPHeader* header = (TCPHeader*)packet; cout<< "Got tcp packet" << "The source port is :" << header->mSourcePort << endl << "The destination port is :" << header->mDestPort << endl << "The length is : " << (int)header->mLengthAndReserve << "B" <<endl; } void ProcessUDPPacket( char* packet, int size ) { UDPHeader* header = (UDPHeader*)packet; cout<< "Got udp packet" << endl << "The source port is " << header->mSourcePort <<endl << "The destination port is " << header->mDestPort<<endl << "The length is " << header->mLength << "B" <<endl; } void ProcessICMPPacket( char* packet, int size ) { ICMPHeader* header = (ICMPHeader*)packet; cout << "Got icmp packet " << endl; } void PortScan(){ //端口扫描函数 int iportFrom,iportTo; int testsocket; int iopenedport = 0; struct sockaddr_in target_addr; WSADATA wsaData;

WORD wVersionRequested=MAKEWORD(1,1); char* hostip = new char[50]; system("cls"); cout << "*********PORT SCAN MODE********** " << endl; cout << "Please input IP :" ; cin >> hostip; cout << "Please input port from: "; cin >> iportFrom; cout << "Please input port end: "; cin >> iportTo; if (iportFrom > iportTo) { cout << "错误!开始端口号必须小于结束端口号" << endl; exit(1); } else { if (WSAStartup (wVersionRequested , &wsaData) ) { cout << "连接 socket 库失败,请检查版本号是否为 1.1\n" << endl; exit(1); } for (int i=iportFrom; i <= iportTo; i++) { cout << "正在建立 socket................................" << endl; if ((testsocket=socket (AF_INET,SOCK_STREAM,0) ) == INVALID_SOCKET) { cout << "Socket 建立失败!" << endl; exit(0); } target_addr.sin_family = AF_INET; target_addr.sin_port = htons(i); target_addr.sin_addr.s_addr = inet_addr (hostip); cout << "正在扫描端口:" << i << endl; if (connect (testsocket, (struct sockaddr *) &target_addr, sizeof(struct sockaddr)) == SOCKET_ERROR) //连接端口 cout << "端口" << i << "关闭!" << endl; else { iopenedport++; cout << "端口" << i << "开放\n" << endl; } } cout << " 目标主机 " << hostip << " 从 " << iportFrom << "--" << iportTo << " 共有 " <<

iopenedport << "个端口开放" << endl; closesocket (testsocket); WSACleanup(); } }


相关文章:
中国天气预报代码大全
中国天气预报代码大全阿巴嘎旗 CHXX0243 阿城 CHXX0001 爱辉 CHXX0174 阿合奇 CHXX0210 阿拉尔 CHXX0212 阿勒泰 CHXX0196 安达 CHXX0187 敖汉旗 CHXX0002 安康 CHXX...
G代码一览表
CNC 代码一览表 M 代码一览表 M 代码一览表 M 代码 M00 M01 M02 M03 M04 M05 M06 M08 M09 M12 M13 M15 M16 M19 M23 M24 M30 M35 程序停止 选择停止 ...
代码
世界港口 世界港口代码 .欧洲地区(EUROPE) 1.意大利(ITALY) 目的港 比萨 波伦那 杜林 佛洛伦萨 罗马 米兰 那不勒斯 热那瓦 威尼斯 三字代码 PSA BLQ TRN FLR ...
完整M代码
M 代码 内容 M00 程序停止 M01 任选停止 M02 程序结束 M03 工作主轴起动 (正转) M04 工作主轴起动 (反转) M05 主轴停止 M06 刀具交换 M07 M08 冷却液开 ...
质量审核员专业代码
质量审核员专业代码 15页 免费 ISO9001:2008质量管理体... 10页 1下载券 质量管理体系认证业务范... 35页 免费 质量管理体系审核员专业... 1页 免费 附表2...
5. 省份代码对照表
5. 省份代码对照表_计算机软件及应用_IT/计算机_专业资料。附件 5 省份代码对照表 代码 11 12 13 14 15 21 22 23 31 32 33 34 35 36 37 41 42 43 ...
参考文献代码
参考文献类型、电子文献载体类型及其标志代码项目 1.参考文献类型及其标志代码 计学数算汇报期位报标专据机编纸刊论告准利库程文序 D G N J D R S P CP ...
源代码是什么
源代码是什么_计算机软件及应用_IT/计算机_专业资料。介绍源代码是什么,可供初学者解读源代码:一系列人类可读的计算机语言指令 源程序(source code)是指未编译的按...
用地分类和代码_图文
1 城乡用地分类 用地分类 表 3.2.2 城乡用地分类和代码 类别代码 大类 H 中类 小类 类别名称 内容 包括城乡居民点建设用地、 区域交通设施用地、 区域公用...
海关代码
上海海关代码: 2200 上海海关 2203 沪机场海关 2206 沪邮局办 2209 龙吴海关 2212 奉贤海关 2215 虹桥开发区海关 2218 外高桥海关 2221 松江海关 2224 崇明海关 ...
更多相关标签: