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

代码


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


相关文章:
完整M代码
M 代码 内容 M00 程序停止 M01 任选停止 M02 程序结束 M03 工作主轴起动 (正转) M04 工作主轴起动 (反转) M05 主轴停止 M06 刀具交换 M07 M08 冷却液开 ...
四川省行政区划代码_图文
四川省行政区划代码_计算机软件及应用_IT/计算机_专业资料。四川省行政区划代码表(一)行政区域代码:510000 成都 代码 510101 地区名称 29 市辖区 锦江区 青羊区 ...
中国天气预报代码大全
中国天气预报代码大全阿巴嘎旗 CHXX0243 阿城 CHXX0001 爱辉 CHXX0174 阿合奇 CHXX0210 阿拉尔 CHXX0212 阿勒泰 CHXX0196 安达 CHXX0187 敖汉旗 CHXX0002 安康 CHXX...
5. 省份代码对照表
5. 省份代码对照表_计算机软件及应用_IT/计算机_专业资料。附件 5 省份代码对照表 代码 11 12 13 14 15 21 22 23 31 32 33 34 35 36 37 41 42 43 ...
科目代码
科目代码_专业资料。企业常用科目 一、资产类 1001 库存现金 1002 银行存款 1121 应收票据 1122 应收账款 1123 预付账款 1131 应收股利 1132 应收利息 1231 其他...
专利国省代码
专利国省代码省 市代码 11 北京 12 天津 13 河北 14 山西 35 福建 52 贵州 71 台湾 15 内蒙 21 辽宁 22 吉林 23 黑龙江 31 上海 41 河南 42 湖北 61...
移动错误代码说明
移动错误代码 1 由 SMSC 返回的一般结果状态报告 DELIVRD EXPIRED 含义 消息发送成功 因为用户长时间关机或者不在服 务区等导致的短消息超时没有递 交到用户手机...
2009版学科分类与代码表(GBT13745-2009)
65 67 68 71 72 73 76 76 81 83 84 85 87 88 89 90 91 91 93 II GB/T 13745—××× 前 言 本标准代替GB/T 13745-1992《学科分类与代码》。 本...
科目三考试项目及扣分项代码_图文
机动车驾驶人科目三 考试项目及扣分项代码 公安部交通管理科学研究所 2012 年 11 月目录 2 科目三考试 ... 1 2.1 科目三考试项目代码 ... 1 2.2 科目三...
代码
世界港口 世界港口代码 .欧洲地区(EUROPE) 1.意大利(ITALY) 目的港 比萨 波伦那 杜林 佛洛伦萨 罗马 米兰 那不勒斯 热那瓦 威尼斯 三字代码 PSA BLQ TRN FLR ...
更多相关标签: