/*
UDP Scanner
Written by Scorpio_wym
Date: May 17th,2008
*/
#include<iostream.h>
#include<winsock2.h>
#include<ws2tcpip.h>
#include<stdio.h>
#include<conio.h>
#pragma comment(lib,"WS2_32.lib")
#define SEND_FAULT -1
#define SEND_OK 0
#define SEND_SET_ERROR 1
#define RECV_OPEN 1
#define DATA_LEN 20
#define GET_HIPADR_ER 0
#define WAIT_SEC 1 /* seconds to wait for an answer */
#define WAIT_USEC 500 /* useconds nto wait for an answer */
/*--------------------------HEADER-STRUCT-DEFINE----------------------------*/
typedef struct ip_hdr { //define IP header
unsigned char h_len:4;
unsigned char h_ver:4;
unsigned char tos;
unsigned short total_len;
int frag_flags;
unsigned char ttl;
unsigned char proto;
unsigned short checksum_IP;
unsigned int sourceIP;
unsigned int destIP;
}IPHEADER;
typedef struct udp_hdr { // define UDP
unsigned short source_port;
unsigned short dest_port;
unsigned short UDP_size;
unsigned short checksum_UDP;
}UDPHEADER;
typedef struct Fabricative_UTPHDR { // define Fabricative header of UDP
unsigned int source_ip;
unsigned int dest_ip;
char fill_field;
unsigned char protocol;
unsigned int UDP_size;
}F_UDPHEADER;
typedef struct ICMP_header { /* we need incept the ICMP info,
if ICMP.code == 3 and ICMP.type == 3,
it explains that the port is open */
unsigned char type_icmp;
unsigned char code_icmp;
unsigned short checksum_icmp;
}ICMPHEADER;
typedef struct Incept_IP_data { // Incept the data from read(...),whether the port is open)
IPHEADER ip_msg;
ICMPHEADER icmp_msg;
}I_IPDATA;
/*---------------------------------------------------------------------*/
/*----------------------------define Data------------------------------*/
static unsigned short Source_Port = 5400; //default sourceport
SOCKADDR_IN SOURCE_ADDR;
SOCKADDR_IN DEST_ADDR;
unsigned long Check_Daddr;
char *RECV_DATA = "pls how me port info!";
int i;
SOCKET sockfd; //ICMP
int udpstd[] = { 49, /* login host protocol */
53, /* dns */
69, /* tftp */
123, /* ntp */
135, /* ep resulution */
137, /* NETBIOS name service */
138, /* NETBIOS datagram service */
161, /* snmp */
162, /* snmp trap */
513, /* who */
514, /* syslog */
515, /* printer */
631, /* ipp */
1812, /* radius */
2049, /* nfs */
4500, /* ip sec stuff */
6772,
27015, /* HL */
31337, /* DDoS */
32780 };
/*---------------------------------------------------------------------*/
/*------------------------function----------------------------------*/
USHORT checksum(USHORT *buffer, int size); //count checksum
void UDP_scan( ULONG S_ADDRESS,char *D_ADDRESS,unsigned short default_spt);
// UDP port to scan
USHORT Send_UDPinfo(UINT S_ADR,UINT D_ADR,USHORT DF_SPT,USHORT SCAN_DPT);
//send udp message
USHORT Get_ICMPinfo(UINT ICMP_SOCK, LPSOCKADDR_IN SOURCE_ADDRESS);
// get the information,we can judge the port whether it is open or close
ULONG Get_SHostip(void);
//get the local host ipaddress, ret a value to check whether get the right local host ipaddress
/*------------------------------------------------------------------*/
/*------------------------body----------------------------------*/
USHORT checksum(USHORT *buffer, int size) // checksum(...)
{
unsigned long cksum = 0;
while(size > 1) {
cksum += *buffer++;
size -= sizeof(USHORT);
}
if(size ) {
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
} //end checksum(...)
void UDP_scan( ULONG S_ADDRESS,char *D_ADDRESS,unsigned short default_spt) // UDP port to scan
{
USHORT retval;
servent *Open_port;
int stdsize = (sizeof(udpstd))/(sizeof(int));
cout<<" starting udp scan! (loading...)"<<endl<<endl;
for(i = 0; i < stdsize; udpstd[i++]) {
if(retval = Send_UDPinfo(S_ADDRESS, inet_addr(D_ADDRESS), Source_Port, udpstd[i]) == SEND_FAULT) {
cout<<"ERROR : "<<retval<<" Can't get any information from this port,maybe it's closed! "<<"msg: port "<<udpstd[i]<<endl;
return;
} else
if(retval == SEND_SET_ERROR) {
cout<<"ERROR : "<<retval<<" there must be setting wrong!"<<endl;
cout<<"Check the Error!"<<endl;
return;
} else
if(retval == SEND_OK) {
if((retval = Get_ICMPinfo(sockfd,&DEST_ADDR)) == RECV_OPEN)
{
Check_Daddr = inet_addr(D_ADDRESS);
Open_port = getservbyport(htons(udpstd[i]),"UDP");
if( Open_port != NULL) {
cout<<endl;
cout<<"This port:["<<udpstd[i]<<"] is opening! info: "<<Open_port->s_name<<endl;
} else {
cout<<endl;
cout<<"This port:["<<udpstd[i]<<"] is opening! info: It's a unknow port!"<<endl;
}
}
}
}
closesocket(sockfd);
}// end UDP_scan
USHORT Send_UDPinfo(UINT S_ADR,UINT D_ADR,USHORT DF_SPT,USHORT SCAN_DPT) //send udp message
{
SOCKET sock;
IPHEADER *ipheader;
UDPHEADER *udpheader;
F_UDPHEADER *f_udpheader;
char Packet_Buf[sizeof(IPHEADER)+sizeof(UDPHEADER) + DATA_LEN];
int Check_Ret = 15;
BOOL FLAG = true;
int Send_info = 0;
if ((sock=WSASocket(AF_INET,SOCK_RAW,IPPROTO_IP,NULL,0,WSA_FLAG_OVERLAPPED))==INVALID_SOCKET) {
cout<<"Socket Setup Error! Error: "<<GetLastError()<<endl;
return SEND_SET_ERROR;
}
if ((Check_Ret = setsockopt(sock,IPPROTO_IP, IP_HDRINCL,(char *)&FLAG,sizeof(FLAG))) < 0) {
cout<<"setsockopt IP_HDRINCL error! Error: "<<GetLastError()<<endl;
return SEND_SET_ERROR;
}
//compute checksum
memset(Packet_Buf, 0x00, sizeof(Packet_Buf));
memcpy(Packet_Buf+sizeof(IPHEADER)+sizeof(UDPHEADER),RECV_DATA,DATA_LEN); //Fill the data option
DEST_ADDR.sin_addr.S_un.S_addr = D_ADR;
DEST_ADDR.sin_family = AF_INET;
DEST_ADDR.sin_port = htons(SCAN_DPT);
ipheader = (IPHEADER *)Packet_Buf;
udpheader = (UDPHEADER *)(Packet_Buf + sizeof(IPHEADER));
f_udpheader = (F_UDPHEADER *)(Packet_Buf + sizeof(IPHEADER) - sizeof(F_UDPHEADER));
udpheader->source_port = htons(DF_SPT);
udpheader->dest_port = htons(SCAN_DPT);
udpheader->checksum_UDP = 0;
udpheader->UDP_size = htons(sizeof(UDPHEADER) + DATA_LEN);
f_udpheader->dest_ip = D_ADR;
f_udpheader->source_ip = S_ADR;
f_udpheader->fill_field = 0;
f_udpheader->protocol = IPPROTO_UDP;
f_udpheader->UDP_size = udpheader->UDP_size;
udpheader->checksum_UDP = checksum((USHORT *)f_udpheader,sizeof(UDPHEADER)
+ sizeof(F_UDPHEADER) + DATA_LEN);
ipheader->h_len = 5;
ipheader->h_ver = 4;
ipheader->tos = 0x10;
ipheader->total_len = sizeof(Packet_Buf);
ipheader->frag_flags = 0;
ipheader->ttl = 128;
ipheader->proto = IPPROTO_UDP;
ipheader->sourceIP = S_ADR;
ipheader->destIP = D_ADR;
ipheader->checksum_IP = 0;
ipheader->checksum_IP = checksum((USHORT *)ipheader,sizeof(IPHEADER));
Send_info = sendto(sock,Packet_Buf,ipheader->total_len,0,(struct sockaddr *)&DEST_ADDR,sizeof(DEST_ADDR));
if(Send_info == SOCKET_ERROR) {
cout<<"Send massage fault! Error :"<<GetLastError()<<endl;
return SEND_FAULT;
}
closesocket(sock);
return SEND_OK;
}//end Send_UDPinfo(...)
// get the information,we can judge the port whether it is open or close
USHORT Get_ICMPinfo(UINT ICMP_SOCK, LPSOCKADDR_IN SOURCE_ADDRESS)
{
int RECV_MSG;
fd_set rset;
timeval tv;
I_IPDATA icmpheader;
in_addr dest_inaddr;
tv.tv_sec = WAIT_SEC;
tv.tv_usec = WAIT_USEC;
char *Dest_Address = NULL;
unsigned char RECV_TTL;
int Recv_Slen = sizeof(SOCKADDR_IN);
FD_ZERO(&rset