4 * $author: Salvatore Sanfilippo <antirez@invece.org>$
5 * $copyright: Copyright (C) 1999 by Salvatore Sanfilippo$
6 * $license: This software is under GPL version 2 of license$
7 * $date: Fri Nov 5 11:55:48 MET 1999$
12 * hping official page at http://www.kyuzz.org/antirez
13 * Covered by GPL version 2, Read the COPYING file for more information
16 /* $Id: main.c,v 1.4 2004/06/18 09:53:11 antirez Exp $ */
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
28 #include <sys/types.h>
36 linkhdr_size, /* physical layer header size */
42 virtual_mtu = DEFAULT_VIRTUAL_MTU,
63 out_of_sequence_pkt = 0,
64 sending_wait = DEFAULT_SENDINGWAIT, /* see DEFAULT_SENDINGWAIT */
65 opt_rawipmode = FALSE,
69 opt_listenmode = FALSE,
70 opt_waitinusec = FALSE,
80 opt_winid_order = FALSE,
81 opt_keepstill = FALSE,
82 opt_datafromfile= FALSE,
88 opt_traceroute = FALSE,
91 opt_force_incdport = FALSE,
92 opt_icmptype = DEFAULT_ICMP_TYPE,
93 opt_icmpcode = DEFAULT_ICMP_CODE,
95 opt_tcpexitcode = FALSE,
97 opt_tr_keep_ttl = FALSE,
98 opt_tcp_timestamp = FALSE,
100 opt_tr_no_rtt = FALSE,
101 opt_rand_dest = FALSE,
102 opt_rand_source = FALSE,
105 opt_cplt_rte = FALSE,
109 src_ttl = DEFAULT_TTL,
110 src_id = -1, /* random */
111 base_dst_port = DEFAULT_DPORT,
112 dst_port = DEFAULT_DPORT,
115 initsport = DEFAULT_INITSPORT,
116 src_winsize = DEFAULT_SRCWINSIZE,
117 src_thoff = (TCPHDR_SIZE >> 2),
118 count = DEFAULT_COUNT,
119 ctrlzbind = DEFAULT_BIND,
122 icmp_ip_version = DEFAULT_ICMP_IP_VERSION,
123 icmp_ip_ihl = DEFAULT_ICMP_IP_IHL,
124 icmp_ip_tos = DEFAULT_ICMP_IP_TOS,
125 icmp_ip_tot_len = DEFAULT_ICMP_IP_TOT_LEN,
126 icmp_ip_id = DEFAULT_ICMP_IP_ID,
127 icmp_ip_protocol= DEFAULT_ICMP_IP_PROTOCOL,
128 icmp_ip_srcport = DEFAULT_DPORT,
129 icmp_ip_dstport = DEFAULT_DPORT,
130 opt_force_icmp = FALSE,
131 icmp_cksum = DEFAULT_ICMP_CKSUM,
132 raw_ip_protocol = DEFAULT_RAW_IP_PROTOCOL;
137 targetstraddr [1024],
138 ifname [1024] = {'\0'},
141 icmp_ip_srcip [1024],
142 icmp_ip_dstip [1024],
145 rsign [1024], /* reverse sign (hping -> gniph) */
163 struct itimerval usec_delay;
164 volatile struct delaytable_element delaytable[TABLESIZE];
166 struct hcmphdr *hcmphdr_p; /* global pointer used by send_hcmp to transfer
167 hcmp headers to data_handler */
170 char errbuf[PCAP_ERRBUF_SIZE];
171 struct pcap_pkthdr hdr;
174 int main(int argc, char **argv)
176 char setflags[1024] = {'\0'};
179 /* Check for the scripting mode */
180 if (argc == 1 || (argc > 1 && !strcmp(argv[1], "exec"))) {
186 hping_script(argc, argv);
187 exit(0); /* unreached */
189 fprintf(stderr, "Sorry, this hping binary was compiled "
190 "without TCL scripting support\n");
195 if (parse_options(argc, argv) == -1) {
196 printf("hping2: missing host argument\n"
197 "Try `hping2 --help' for more information.\n");
202 if (opt_sign || opt_listenmode) {
203 char *src = sign+strlen(sign)-1; /* last char before '\0' */
210 printf("DEBUG: reverse sign: %s\n", rsign);
213 /* get target address before interface processing */
214 if ((!opt_listenmode && !opt_safe) && !opt_rand_dest)
215 resolve((struct sockaddr*)&remote, targetname);
218 strlcpy(targetstraddr, targetname, sizeof(targetstraddr));
220 strlcpy(targetstraddr, inet_ntoa(remote.sin_addr),
221 sizeof(targetstraddr));
224 /* get interface's name and address */
225 if ( get_if_name() == -1 ) {
226 printf("[main] no such device\n");
230 if (opt_verbose || opt_debug) {
231 printf("using %s, addr: %s, MTU: %d\n",
232 ifname, ifstraddr, h_if_mtu);
235 /* open raw socket */
236 sockraw = open_sockraw();
238 printf("[main] can't open raw socket\n");
242 /* set SO_BROADCAST option */
243 socket_broadcast(sockraw);
244 /* set SO_IPHDRINCL option */
245 socket_iphdrincl(sockraw);
247 /* open sock packet or libpcap socket */
248 if (open_pcap() == -1) {
249 printf("[main] open_pcap failed\n");
253 /* get physical layer header size */
254 if ( get_linkhdr_size(ifname) == -1 ) {
255 printf("[main] physical layer header size unknown\n");
259 if (spoofaddr[0] == '\0')
260 resolve((struct sockaddr*)&local, ifstraddr);
262 resolve((struct sockaddr*)&local, spoofaddr);
264 if (icmp_ip_srcip[0] == '\0')
265 resolve((struct sockaddr*)&icmp_ip_src, "1.2.3.4");
267 resolve((struct sockaddr*)&icmp_ip_src, icmp_ip_srcip);
269 if (icmp_ip_dstip[0] == '\0')
270 resolve((struct sockaddr*)&icmp_ip_dst, "5.6.7.8");
272 resolve((struct sockaddr*)&icmp_ip_dst, icmp_ip_dstip);
274 if (icmp_gwip[0] == '\0')
275 resolve((struct sockaddr*)&icmp_gw, "0.0.0.0");
277 resolve((struct sockaddr*)&icmp_gw, icmp_gwip);
281 /* set initial source port */
283 initsport = src_port = 1024 + (rand() % 2000);
285 src_port = initsport;
287 for (c = 0; c < TABLESIZE; c++)
288 delaytable[c].seq = -1;
290 /* use SIGALRM to send packets like ping do */
291 Signal(SIGALRM, send_packet);
294 if (ctrlzbind != BIND_NONE) Signal(SIGTSTP, inc_destparm);
295 Signal(SIGINT, print_statistics);
296 Signal(SIGTERM, print_statistics);
298 /* if we are in listemode enter in listenmain() else */
299 /* print HPING... bla bla bla and enter in wait_packet() */
300 if (opt_listenmode) {
301 fprintf(stderr, "hping2 listen mode\n");
303 /* memory protection */
304 if (memlockall() == -1) {
305 perror("[main] memlockall()");
306 fprintf(stderr, "Warning: can't disable memory paging!\n");
307 } else if (opt_verbose || opt_debug) {
308 printf("Memory paging disabled\n");
316 fprintf(stderr, "Scanning %s (%s), port %s\n",
317 targetname, targetstraddr, opt_scanports);
323 strcat(setflags, "raw IP mode");
324 hdr_size = IPHDR_SIZE;
325 } else if (opt_icmpmode) {
326 strcat(setflags, "icmp mode");
327 hdr_size = IPHDR_SIZE + ICMPHDR_SIZE;
328 } else if (opt_udpmode) {
329 strcat(setflags, "udp mode");
330 hdr_size = IPHDR_SIZE + UDPHDR_SIZE;
332 if (tcp_th_flags & TH_RST) strcat(setflags, "R");
333 if (tcp_th_flags & TH_SYN) strcat(setflags, "S");
334 if (tcp_th_flags & TH_ACK) strcat(setflags, "A");
335 if (tcp_th_flags & TH_FIN) strcat(setflags, "F");
336 if (tcp_th_flags & TH_PUSH) strcat(setflags, "P");
337 if (tcp_th_flags & TH_URG) strcat(setflags, "U");
338 if (tcp_th_flags & TH_X) strcat(setflags, "X");
339 if (tcp_th_flags & TH_Y) strcat(setflags, "Y");
340 if (setflags[0] == '\0') strcat(setflags, "NO FLAGS are");
341 hdr_size = IPHDR_SIZE + TCPHDR_SIZE;
344 printf("HPING %s (%s %s): %s set, %d headers + %d data bytes\n",
352 /* memory protection */
353 if (opt_datafromfile || opt_sign) {
354 if (memlockall() == -1) {
355 perror("[main] memlockall()");
357 "Warning: can't disable memory paging!\n");
358 } else if (opt_verbose || opt_debug) {
359 printf("Memory paging disabled\n");
363 /* start packet sending */
364 kill(getpid(), SIGALRM);
369 "hping in flood mode, no replies will be shown\n");