-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDPacketGenerator.c
More file actions
143 lines (112 loc) · 2.85 KB
/
Copy pathUDPacketGenerator.c
File metadata and controls
143 lines (112 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define __FAVOR_BSD
#define _USE_BSD
#include <netinet/udp.h>
#define PADDING_SIZE 1
#define N_LOOP 10
#define U_WAITING 100000
void udp(char *);
unsigned short int in_chksum (unsigned short int *, int);
unsigned long hasard(unsigned long, unsigned long);
int main(void) {
srand(time(NULL));
int i;
for(i=0;i<N_LOOP;i++)
{
udp("xxx.xxx.xxx.xxx"); //replace with IP
usleep(U_WAITING);
printf("-");
udp("xxx.xxx.xxx.xxx"); //replace with IP
usleep(U_WAITING);
printf("+");
}
return 0;
}
void udp(char *cible) {
int sd;
sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (sd == -1) {
fprintf(stderr,"socket() error, root ?\n");
}
//randomly generates IP
unsigned long ip_src = hasard(4294967295/2,4294967295);
unsigned long ip_dst = inet_addr(cible);
unsigned short p_src = (unsigned short) hasard(0,65535);
unsigned short p_dst = (unsigned short) hasard(0,65535);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = p_dst;
sin.sin_addr.s_addr = ip_dst; // destination
struct ip *ip;
struct udphdr *udp;
char *dgm, *data;
int pksize = sizeof(struct ip) + sizeof(struct udphdr) + PADDING_SIZE;
dgm = (char *) malloc(pksize);
ip = (struct ip *) dgm;
udp = (struct udphdr *) (dgm + sizeof(struct ip));
data = (char *) (dgm + sizeof(struct ip) + sizeof(struct udphdr));
memset(dgm, 0, pksize);
memcpy((char *) data, "G", PADDING_SIZE);
int un = 1;
if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, (char *)&un, sizeof(un)) == -1)
{
fprintf(stderr,"setsockopt()");
exit(-1);
}
//IP Header
ip->ip_v = 4;
ip->ip_hl = 5;
ip->ip_tos = 0;
ip->ip_len = sizeof(pksize);
ip->ip_ttl = 255;
ip->ip_off = 0;
ip->ip_id = sizeof( 45 );
ip->ip_p = IPPROTO_UDP;
ip->ip_sum = 0;
ip->ip_src.s_addr = ip_src;
ip->ip_dst.s_addr = ip_dst;
//UDP Header
udp->uh_sport = p_src;
udp->uh_dport = p_dst;
udp->uh_ulen = htons(sizeof(struct udphdr ) + PADDING_SIZE);
udp->uh_sum = 0;
// Send
if (sendto(sd, dgm, pksize, 0, (struct sockaddr *) &sin,
sizeof(struct sockaddr)) == -1) {
fprintf(stderr,"oops, sendto() error\n");
}
//Free Memory
free(dgm);
close(sd);
}
u_short in_chksum (u_short *addr, int len) // taken from papasmurf.c
{
register int nleft = len;
register u_short *w = addr;
register int sum = 0;
u_short answer = 0;
while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}
if (nleft == 1)
{
*(u_char *)(&answer) = *(u_char *)w;
sum += answer;
}
sum = (sum >> 16) + (sum + 0xffff);
sum += (sum >> 16);
answer = ~sum;
return(answer);
}
unsigned long hasard(unsigned long min, unsigned long max){
return (u_long) (min + ((float) rand() / RAND_MAX * (max - min + 1)));
}