目的:掃描目的的主機端口,來探測看目的主機各個端口的情況。
c++源程序如下:
- #include <string.h>
- #include <WinSock.h>
- #include <iostream>
- #include<string>
- using namespace std;
- /*
- // SCAN.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #ifndef _INIT_SCOK
- #define _INIT_SCOK
- #include <stdlib.h>
- #include <winsock2.h>
- #pragma comment(lib,"WS2_32")
- #include <Mswsock.h>
- #pragma comment(lib,"mswsock")
- #include <Ws2tcpip.h>
- #include <MSTcpIP.h>
- #endif
- typedef struct tagParam
- {
- DWORD dwDesIp;
- USHORT nDesPort;
- static LONG nThread;
- }ScanParam,*pScanParam;
- LONG ScanParam::nThread = 0;
- DWORD WINAPI ScanPort(LPVOID lpParam);
- int main(int argc, char* argv[])
- {
- DWORD dwAddr = 0;
- LONG nStart = 0,nEnd = 0,nThread = 0;
- char buf[MAX_BUF_LEN];
- WSADATA wsaData;
- BYTE minorVer = 2;
- BYTE majorVer = 2;
- WORD sockVersion = MAKEWORD(majorVer,majorVer);
- if(::WSAStartup(sockVersion,&wsaData)!=0)
- {
- exit(0);
- }
- do
- {
- printf("請輸入需要掃描的IP地址:");
- gets(buf);
- }
- while((strcmp(buf,"") == 0));
- dwAddr = ::inet_addr(buf);
- do
- {
- printf("請輸入掃描的起始端口:");
- scanf("%d",&nStart);
- }
- while (nStart < 0);
- do
- {
- printf("請輸入掃描的結束端口:");
- scanf("%d",&nEnd);
- }
- while (nStart > nEnd);
- do
- {
- printf("請輸入掃描的最大線程數:");
- scanf("%d",&nThread);
- }
- while (nThread < 0);
- printf("正在掃描中......\r\n");
- for (USHORT nPort = (USHORT)nStart;nPort != (USHORT)nEnd;++nPort)
- {
- pScanParam pData = new ScanParam();
- pData->dwDesIp = dwAddr;
- pData->nDesPort = nPort;
- if (CreateThread(NULL, 0, ScanPort, (LPVOID)pData, 0, NULL) != NULL)
- {
- ::Sleep(10);
- }
- while(ScanParam::nThread >= nThread)
- {
- ::Sleep(10);
- }
- }
- while(ScanParam::nThread > 0)
- {
- ::Sleep(10);
- }
- printf("掃描完成!\r\n");
- system("pause");
- ::WSACleanup();
- return 0;
- }
- DWORD WINAPI ScanPort(LPVOID lpParam)
- {
- pScanParam pData = (pScanParam)lpParam;
- if(pData == NULL)
- return -1;
- ScanParam::nThread++;
- // 創建套節字
- SOCKET s = NULL;
- // 填寫遠程地址信息
- sockaddr_in remote;
- remote.sin_family = AF_INET;
- remote.sin_addr.S_un.S_addr = pData->dwDesIp;
- remote.sin_port = ::htons(pData->nDesPort);
- if((s = ::socket(AF_INET,SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET)
- {
- printf("Failed socket() %d \n", ::WSAGetLastError());
- return -1;
- }
- printf("%s端口[%d]是否打開 \n",inet_ntoa(remote.sin_addr),::ntohs(remote.sin_port));
- if(::connect(s,(SOCKADDR*)&remote,sizeof(SOCKADDR))!= SOCKET_ERROR)
- {
- printf("%s端口[%d]已打開\r\n",inet_ntoa(remote.sin_addr),::ntohs(remote.sin_port));
- }
- ::closesocket(s);
- ScanParam::nThread--;
- DEL_P(pData);
- return 1;
- }
- */
- #pragma comment(lib,"ws2_32.lib")
- int main()
- {
- //初始化Windows Sockets 動態庫
- WSADATA wsaData;
- if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
- {
- cout << "失敗:WSAStartup" << endl;
- return 1;
- }
-
- //連接服務器
- char IP[100];
- int star_host, end_host;
- cout << "請輸入目標主機+開始端口+終止端口 " << endl;
- cin >> IP >> star_host >> end_host;
-
- SOCKADDR_IN addrServ;
- addrServ.sin_family = AF_INET;
- addrServ.sin_addr.S_un.S_addr = inet_addr(IP);
- if (star_host > end_host)
- {
- cout << "終止端口應該大于等于起始端口" << endl;
- return 0;
- }
- for (int now_host = star_host; now_host <= end_host; now_host++)
- {
- addrServ.sin_port = htons(now_host);
- //創建套接字
- SOCKET sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sClient == INVALID_SOCKET)
- {
- cout << "失敗:創建socket!" << endl;
- return 1;
- }
- if (connect(sClient, (sockaddr *)&addrServ, sizeof(sockaddr)) == SOCKET_ERROR)
- {
- cout << "關閉:" << now_host << endl;
- }
- else
- cout << "開放:" << now_host << endl;
- closesocket(sClient);
- }
- //關閉套接字,釋放資源
- //closesocket(sClient);
- WSACleanup();
- return 0;
- }
復制代碼
所有資料51hei提供下載:
掃描目的主機端口.zip
(4.76 KB, 下載次數: 14)
2017-6-7 22:12 上傳
點擊文件名下載附件
|