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

代码


#include <vector> #include <math.h> #include <bitset> #include <iostream> #include <winsock2.h> #include <mstcpip.h> #include <ntddndis.h> #include &

lt;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(); } }


相关文章:
氨基酸缩写代码
氨基酸缩写代码_物理_自然科学_专业资料。附录 C 氨基酸缩写代码 以下是氨基酸的单字母记号和三字母记号 单字母记号 三字母记号 英文名称 A Ala Alanine C Cys ...
Discuz代码大全
Discuz代码大全_IT/计算机_专业资料。Discuz代码大全,经过整理编辑Discuz!代码大全 代码大全 代码大家在论坛发表主题或回帖时,经常要用到 Discuz!代码,本文就常用的 ...
中国天气预报代码大全
中国天气预报代码大全阿巴嘎旗 CHXX0243 阿城 CHXX0001 爱辉 CHXX0174 阿合奇 CHXX0210 阿拉尔 CHXX0212 阿勒泰 CHXX0196 安达 CHXX0187 敖汉旗 CHXX0002 安康 CHXX...
科目代码
科目代码_专业资料。企业常用科目 一、资产类 1001 库存现金 1002 银行存款 1121 应收票据 1122 应收账款 1123 预付账款 1131 应收股利 1132 应收利息 1231 其他...
源码买卖协议
代码买卖协议 甲方: 乙方: 身份证号码: 经甲乙双方友好协商,乙方准备向甲方买下《xxxx》的完整代码,内容包括如下: 1、 完整前端代码 2、 完整后端代码 3、 ...
参考文献类型及代码
参考文献类型及代码: 参考文献类型及代码:专著[M] ,论文集[C] ,报纸文章[N] ,期刊文章[J] ,学位论文[D] ,报告[R] ,标准[S] ,专利[P] ,论文集中的...
代码
世界港口 世界港口代码 .欧洲地区(EUROPE) 1.意大利(ITALY) 目的港 比萨 波伦那 杜林 佛洛伦萨 罗马 米兰 那不勒斯 热那瓦 威尼斯 三字代码 PSA BLQ TRN FLR ...
北京市行政区划代码
北京市行政区划代码_电力/水利_工程科技_专业资料。本标准是为适应信息技术需要而制订。本标准所给出的行政区划以北京市民政局所提供的北京市人民政府关于行政区划...
废气排放规律代码表
废气排放规律代码表_畜牧兽医_农林牧渔_专业资料。排污申报代码表 废气排放规律代码代码 废气排放规律 稳定连续排放 周期性连续排放 1 2 3 不规律连续排放 有...
国内区号、国际地区代码
国内长途直拨电话 先拨 0,再拨地区代码及所需电话号码 Domestic Call Dail 0, then dail the area code and the telephone number. 国际直拨电话 先拨 0,再...
更多相关标签:
代码大全 | 代码编辑器 | 源代码 | 网页代码 | 淘宝代码 | 代码生成器 | html代码 | 神秘代码 |