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

代码


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



相关文章:
法兰克G代码和M代码
车削:采用 A 组 G 代码(X Z R 用于绝对、U W C 用于增量输入) G10 P_ X_ Y_ Z_ R_ Q_ 绝对模式编程 G10 P_ U_ V_ W_ C_ Q_ 增量模式...
代码大全之必备
20110103145229469.mp3 七、音乐播放器调整位置代码: ***<DIV style="MARGIN: -12px 0px 0px 2px"> 你把这组代码放在播放器代码的上面,调第一个和第四个...
源代码是什么
源代码是什么_计算机软件及应用_IT/计算机_专业资料。介绍源代码是什么,可供初学者解读源代码:一系列人类可读的计算机语言指令 源程序(source code)是指未编译的按...
网页html代码大全
常用HTML 代码解释 一、文字 1.标题文字 <h#>...</h#> #=1~6;h1为最大字,h6为最小字 2.字体变化 ... 【1】字体大小 ... #=1~7;数字愈大字也...
尺寸代码
尺寸代码_纺织/轻工业_工程科技_专业资料。集装箱的尺寸代码第二行 含二或三个内容 1)国别代码(country code) 它指的是箱主公司所在国家的代码,非强制性的,...
html代码大全_很全
html代码大全_很全_计算机软件及应用_IT/计算机_专业资料 暂无评价|0人阅读|0次下载|举报文档 html代码大全_很全_计算机软件及应用_IT/计算机_专业资料。Html ...
G代码及解释
G代码及解释_IT/计算机_专业资料。常用 G 代码 G 代码命令 代码组及其含义 “模态代码” 和 “一般” 代码 “形式代码” 的功能在它被执行后会继续 维持,而...
G代码大全
G 代码 G00 G01 功能说明快速定位 直线插补 G00 X__ Y__ Z__ 指令格式 G01 X __ Y __ Z__;一般直线插补模式 N100 G01 X__ Y__, C__ N105 ...
G代码一览表
G代码一览表_计算机软件及应用_IT/计算机_专业资料。CNC G代码览表 CNC 代码一览表 M 代码一览表 M 代码一览表 M 代码 M00 M01 M02 M03 M04 M05 M06 M08...
G代码 M代码 全
G代码 M代码 全_机械/仪表_工程科技_专业资料。G代码 M代码 G 代码 内容 G00 快速定位 G01 直线插补 G02 圆弧插补 G03 圆弧插补 G04 暂停 G05 G06 G07 G...
更多相关标签: