为了顺应当前形势和更好的发展,黑基网已于9月19日正式更名为【安基网】,域名更换为www.safebase.cn,请卸载旧的APP并安装新的APP,给您带来不便,敬请理解!谢谢

黑基Web安全攻防班
安基网 首页 IT技术 安全攻防 查看内容

端口扫描程序VC版

2009-8-28 10:57| 投稿: security

摘要: #include <stdio.h>#include <winsock.h> #define NETWORK_ERROR -1#define NETWORK_OK 0#def...
#include <stdio.h>#include <winsock.h> #define NETWORK_ERROR -1#define NETWORK_OK 0#define PORT_MIN 1#define PORT_MAX 65535 HANDLE hThread;DWORD hID; char hostname[30]; int starting_port = 0;int ending_port = 0;int nopen = 0; DWORD portscan(); int main(){int ret; WSADATA dat;DWORD version; version = MAKEWORD(2,2); ret = WSAStartup(version,&dat); if(ret != 0){ printf("Error initializing Winsock.\n");WSACleanup();return NETWORK_ERROR;}if(ret == 0){ printf("Enter hostname:");scanf("%s",hostname); printf("Enter starting port:");scanf("%d",&starting_port); if(starting_port < PORT_MIN){printf("Invalid port number.\n");WSACleanup();return NETWORK_ERROR;} printf("Enter ending port:");scanf("%d",&ending_port); if(ending_port > PORT_MAX){printf("Invalid port number.\n");WSACleanup();return NETWORK_ERROR;} printf("\nScanning [%s]...\n",hostname); hThread = CreateThread(0,0,(LPTHREAD_START_ROUTINE)portscan,0,0,&hID); if(hThread == 0){printf("Failed to create thread.\n");WSACleanup();return NETWORK_ERROR;} sleep(-1);}WSACleanup();return NETWORK_OK;} DWORD portscan(){int i,nret; SOCKET thesocket;LPHOSTENT hostent; thesocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);hostent = gethostbyname((hostname)); for(i = starting_port;i < ending_port+1;++i){ SOCKADDR_IN hostinfo; hostinfo.sin_family = AF_INET;hostinfo.sin_addr=*((LPIN_ADDR)*hostent->h_addr_list);hostinfo.sin_port = htons(i); nret = connect(thesocket,(LPSOCKADDR)&hostinfo,sizeof(hostinfo)); if(nret == 0){printf("\n\t%d\n",i); ++nopen;} } printf("\nScan complete.\n\n");printf("Number of ports opened = %d\n",nopen); closesocket(thesocket);}

小编推荐:欲学习电脑技术、系统维护、网络管理、编程开发和安全攻防等高端IT技术,请 点击这里 注册黑基账号,公开课频道价值万元IT培训教程免费学,让您少走弯路、事半功倍,好工作升职加薪!



免责声明:本文由投稿者转载自互联网,版权归原作者所有,文中所述不代表本站观点,若有侵权或转载等不当之处请联系我们处理,让我们一起为维护良好的互联网秩序而努力!联系方式见网站首页右下角。


鲜花

握手

雷人

路过

鸡蛋

相关阅读

最新评论

最新

返回顶部