Author: T4mQu0c
Source scan port đơn giản với C
Code:
// PortScanner.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//#include
#pragma comment(lib, "ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
// Khai bao bien
char IP[16] = {0};
int start, end, ret;
SOCKET sock;
SOCKADDR_IN sin;
WSADATA wsadata;
// Khoi tao socket
if(WSAStartup(MAKEWORD(2, 2), &wsadata) != NO_ERROR)
{
printf("Khoi tao Socket that bai");
goto Finish;
}
// Nhap cac gia tri IP, Port
printf("Nhap dia chi IP: ");
scanf("%s",&IP);
printf("Port bat dau [1-65535]: ");
scanf("%d", &start);
printf("Port ket thuc [1-65535]: ");
scanf("%d", &end);
printf("[*] Bat dau scan port tren IP %s\n", IP);
sock = socket(AF_INET,SOCK_STREAM,0);
while(start <= end){
ZeroMemory(&sin, sizeof(SOCKADDR_IN));
sin.sin_family = AF_INET;
sin.sin_port = htons(start);
sin.sin_addr.s_addr = inet_addr(IP);
ret = connect(sock,(SOCKADDR*)&sin, sizeof(sockaddr)) ;
if(ret != SOCKET_ERROR){
printf("\n[+] Port %d Open\n", start);
// Dong socket va tao socket moi
closesocket (sock);
sock = socket(AF_INET,SOCK_STREAM,0);
}
else {
printf(".");
}
start++;
}
WSACleanup();
printf("\n[*] Scan port ket thuc");
Finish:
getchar();
return 0;
}