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

代码


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


相关文章:
氨基酸缩写代码
氨基酸缩写代码_物理_自然科学_专业资料。附录 C 氨基酸缩写代码 以下是氨基酸的单字母记号和三字母记号 单字母记号 三字母记号 英文名称 A Ala Alanine C Cys ...
代码
世界港口 世界港口代码 .欧洲地区(EUROPE) 1.意大利(ITALY) 目的港 比萨 波伦那 杜林 佛洛伦萨 罗马 米兰 那不勒斯 热那瓦 威尼斯 三字代码 PSA BLQ TRN FLR ...
科目三考试项目及扣分项代码_图文
机动车驾驶人科目三 考试项目及扣分项代码 公安部交通管理科学研究所 2012 年 11 月目录 2 科目三考试 ... 1 2.1 科目三考试项目代码 ... 1 2.2 科目三...
参考文献代码
参考文献类型、电子文献载体类型及其标志代码项目 1.参考文献类型及其标志代码 计学数算汇报期位报标专据机编纸刊论告准利库程文序 D G N J D R S P CP ...
科目代码
科目代码_专业资料。企业常用科目 一、资产类 1001 库存现金 1002 银行存款 1121 应收票据 1122 应收账款 1123 预付账款 1131 应收股利 1132 应收利息 1231 其他...
国家代码表
国家代码表_其它考试_资格考试/认证_教育专区。国家代码表国籍代码 AFG ARE ARG AUS AUT AZE BEL BGD BGR BLR BRA BRN CAN CHE CHL CHN CUB CZE DEU DNK ...
FANUC G代码
G 代码 G00 G01 G02 G03 G04 组别 01 功能 附注 快速定位 直线插补 模态 模态 顺时针圆弧插补 模态 逆时针圆弧插补 模态 00 暂停 非模态 数据设置 模态 数据...
淘宝数据代码大全
店铺装修常用图片尺寸及常用代码大全一普通店铺: 01 店标: 大小:宽 100 像素,高 100 像素 图片直接上传在管理我的店铺—基本设置—店标—浏览 要求:图片的格式...
SAP MM模块常用事务代码(汇总)
SAP MM模块常用事务代码(汇总)_IT/计算机_专业资料 暂无评价|0人阅读|0次下载|举报文档 SAP MM模块常用事务代码(汇总)_IT/计算机_专业资料。关于SAP系统中的MM...
源代码是什么
源代码是什么_计算机软件及应用_IT/计算机_专业资料。介绍源代码是什么,可供初学者解读源代码:一系列人类可读的计算机语言指令 源程序(source code)是指未编译的按...
更多相关标签: