tcp_saomiao.cpp
资源名称:Portscan.zip [点击查看]
上传用户:whjwfkj
上传日期:2021-07-07
资源大小:8k
文件大小:2k
源码类别:
扫描程序
开发平台:
Visual C++
- #include <stdio.h>
- #include <stdlib.h>
- #include <winsock2.h>
- #pragma comment(lib,"ws2_32.lib")
- int main(int argc,char **argv)
- {
- char *TargetIPAddr="172.19.26.15";
- unsigned int StartPort=80;
- unsigned int EndPort=85;
- SOCKET ScanSocket;
- struct sockaddr_in TargetAddr_in;
- int Ret;
- HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO bInfo;
- GetConsoleScreenBufferInfo(hCon,&bInfo);
- WSADATA wsaData;
- if((Ret=WSAStartup(MAKEWORD(2,1),&wsaData))!=0)
- {
- printf("WSAStartup failed with error %dn",Ret);
- return 0;
- }
- DWORD dwStart=GetTickCount();
- printf("Target IP:%sn",TargetIPAddr);
- for(unsigned int i=StartPort;i<=EndPort;i++)
- {
- ScanSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
- if(ScanSocket==INVALID_SOCKET)
- {
- printf("socket failed with error:%dn",WSAGetLastError());
- return 0;
- }
- TargetAddr_in.sin_family=AF_INET;
- TargetAddr_in.sin_addr.s_addr=inet_addr(TargetIPAddr);
- TargetAddr_in.sin_port=htons(i);
- if(connect(ScanSocket,(struct sockaddr*)&TargetAddr_in,sizeof(TargetAddr_in))==SOCKET_ERROR)
- {
- SetConsoleTextAttribute(hCon,10);
- printf("Port%5d Closen",i);
- }
- else
- {
- SetConsoleTextAttribute(hCon,14);
- printf("Port%5d Openn",i);
- }
- if(closesocket(ScanSocket)==SOCKET_ERROR)
- {
- printf("closesocket failed with error %dn",WSAGetLastError());
- return 0;
- }
- }
- SetConsoleTextAttribute(hCon,14);
- printf("ntime:%dmsn",GetTickCount()-dwStart);
- SetConsoleTextAttribute(hCon,bInfo.wAttributes);
- if(WSACleanup()==SOCKET_ERROR)
- {
- printf("WSACleanip failed with error %dn",WSAGetLastError());
- return 0;
- }
- return 1;
- }