int main (int argc, char **argv) { unsigned long fakesrc, target; unsigned char *buf; unsigned char *data; int sock, i, flags, offset, len; if (argc != 2 || !(target = libnet_name_resolve(argv[1], 1))) { fprintf(stderr, "Usage: %s <target>\n", argv[0]); exit(1); } if ((sock = libnet_open_raw_sock(IPPROTO_RAW)) == -1) { perror("raw sock"); exit(1); } /* get random src addr. */ libnet_seed_prand(); fakesrc = libnet_get_prand(LIBNET_PRu32); buf = malloc(LIBNET_IP_H + LIBNET_ICMP_ECHO_H); data = (unsigned char *)malloc(FRAG_LEN); for (i = 0 ; i < 65536 ; i += (LIBNET_ICMP_ECHO_H + FRAG_LEN)) { offset = i; flags = 0; if (offset < 65120) { flags = IP_MF; len = FRAG_LEN; } else len = 410; /* for total reconstructed len of 65538 */ /* make IP header. */ libnet_build_ip(LIBNET_ICMP_ECHO_H + len, 0, 666, flags | (offset >> 3), 64, IPPROTO_ICMP, fakesrc, target, NULL, 0, buf); /* make ICMP packet. */ libnet_build_icmp_echo(8, 0, 666, 666, data, len, buf + LIBNET_IP_H); /* calculate ICMP checksum. */ libnet_do_checksum(buf, IPPROTO_ICMP, LIBNET_ICMP_ECHO_H + len); /* send it. */ libnet_write_ip(sock, buf, LIBNET_IP_H + LIBNET_ICMP_ECHO_H + len); /* tcpdump-style jonks. */ printf("%s > %s: (frag 666:%d@%d%s)\n", libnet_host_lookup(fakesrc,0), argv[1], LIBNET_ICMP_ECHO_H + len, offset, flags ? "+" : ""); } free(buf); return (EXIT_SUCCESS); }
void write_icmpether(struct libnet_link_int *iface, char *dev, struct eth_pair *dst, struct eth_pair *src) { unsigned char pkt[ETH_H + IP_H + ICMP_ECHO_H]; libnet_build_ethernet(dst->eth, src->eth, ETHERTYPE_IP, NULL, 0, pkt); libnet_build_ip(ICMP_ECHO_H, 0, 0, 0, 2, IPPROTO_ICMP, src->ip, dst->ip, NULL, 0, pkt + ETH_H); libnet_build_icmp_echo(ICMP_ECHO, 0, 0, 0, NULL, 0, pkt + ETH_H + IP_H); if(libnet_do_checksum(pkt+ETH_H, IPPROTO_IP, IP_H) == -1) perror("ipv4 cksum"); if(libnet_do_checksum(pkt+ETH_H, IPPROTO_ICMP, ICMP_ECHO_H) == -1) perror("ipv4 cksum"); if(libnet_write_link_layer(iface, dev, pkt, sizeof(pkt)) != sizeof(pkt)) perror("Write link layer"); }
int main(int argc, char **argv) { int sock, n, c, r, p_num; struct libnet_arena arena, *arena_p; u_char *packets[10]; u_char *payload; u_char *buf; u_long src_ip, dst_ip; src_ip = 0; dst_ip = 0; payload = NULL; while((c = getopt(argc, argv, "d:s:p:")) != EOF) { switch (c) { case 'd': if (!(dst_ip = libnet_name_resolve(optarg, 1))) { fprintf(stderr, "Bad destination IP address: %s\n", optarg); exit(1); } break; case 's': if (!(src_ip = libnet_name_resolve(optarg, 1))) { fprintf(stderr, "Bad source IP address: %s\n", optarg); exit(-1); } break; case 'p': payload = optarg; break; default: usage(); exit(-1); } } if (!src_ip || !dst_ip || !payload) { usage(); exit(-1); } /* allocate packet memory */ buf = (u_char *)malloc(LIBNET_IP_H + LIBNET_ICMP_H + strlen(payload)); if(!buf) { perror("malloc:"); exit(-1); } sock = libnet_open_raw_sock(IPPROTO_RAW); if (sock == -1) { perror("No socket"); exit(-1); } /* build headers */ libnet_build_ip(LIBNET_ICMP_ECHO_H, IPTOS_LOWDELAY | IPTOS_THROUGHPUT, TPING_ID, 0, TPING_TTL, IPPROTO_ICMP, src_ip, dst_ip, NULL, 0, buf); libnet_build_icmp_echo(ICMP_ECHO, 0, TPING_ID, 1, payload,strlen(payload), buf + LIBNET_IP_H); if (libnet_do_checksum(buf, IPPROTO_ICMP, LIBNET_ICMP_ECHO_H) < 0) { fprintf(stderr, "Can't do checksum!\n"); } /* * Write the packet to the network. */ r = libnet_write_ip(sock, buf, LIBNET_ICMP_ECHO_H + LIBNET_IP_H + strlen(payload)); if (r < LIBNET_ICMP_ECHO_H + LIBNET_IP_H + strlen(payload)) { fprintf(stderr, "Unable to send packet.\n"); } printf("ICMP Echo sent. Payload: %s\n",payload); }
int main(int argc, char **argv) { int sock, n, c, p_num; struct libnet_arena arena, *arena_p; u_char *packets[10]; u_long src_ip, dst_ip; printf("ICMP_ECHO / Arena allocator test\n"); src_ip = 0; dst_ip = 0; while((c = getopt(argc, argv, "d:s:")) != EOF) { switch (c) { case 'd': if (!(dst_ip = libnet_name_resolve(optarg, 1))) { fprintf(stderr, "Bad destination IP address: %s\n", optarg); exit(1); } break; case 's': if (!(src_ip = libnet_name_resolve(optarg, 1))) { fprintf(stderr, "Bad source IP address: %s\n", optarg); exit(1); } break; } } if (!src_ip || !dst_ip) { usage(argv[0]); exit(EXIT_FAILURE); } arena_p = &arena; p_num = 10; if (libnet_init_packet_arena(&arena_p, p_num, LIBNET_ICMP_ECHO_H + LIBNET_IP_H) == -1) { fprintf(stderr, "libnet_init_packet_arena failed\n"); exit(EXIT_FAILURE); } else { printf("Allocated an arena of %ld bytes..\n", LIBNET_GET_ARENA_SIZE(arena)); } /* * Open our raw IP socket and set IP_HDRINCL. */ sock = libnet_open_raw_sock(IPPROTO_RAW); if (sock == -1) { perror("No socket"); exit(EXIT_FAILURE); } for (n = 0; n < p_num; n++) { printf("%ld bytes remaining in arena\n", LIBNET_GET_ARENA_REMAINING_BYTES(arena)); packets[n] = libnet_next_packet_from_arena(&arena_p, LIBNET_ICMP_ECHO_H + LIBNET_IP_H); if (!packets[n]) { fprintf(stderr, "Arena is empty\n"); continue; } /* * Build the IP header (shown exploded for commenting). */ libnet_build_ip(LIBNET_ICMP_ECHO_H, /* Size of the payload */ IPTOS_LOWDELAY | IPTOS_THROUGHPUT, /* IP tos */ 242, /* IP ID */ 0, /* Frag stuff */ 48, /* TTL */ IPPROTO_ICMP, /* Transport protocol */ src_ip, /* Source IP */ dst_ip, /* Destination IP */ NULL, /* Pointer to payload (none) */ 0, packets[n]); /* Packet header memory */ /* * Build the ICMP header. */ libnet_build_icmp_echo(ICMP_ECHO, /* type */ 0, /* code */ 242, /* id */ 1, /* seq */ NULL, /* pointer to payload */ 0, /* size of payload */ packets[n] + LIBNET_IP_H); /* packet header memory */ if (libnet_do_checksum(packets[n], IPPROTO_ICMP, LIBNET_ICMP_ECHO_H) == -1) { fprintf(stderr, "Can't do checksum!\n"); } /* * Write the packet to the network. */ c = libnet_write_ip(sock, packets[n], LIBNET_ICMP_ECHO_H + LIBNET_IP_H); if (c < LIBNET_ICMP_ECHO_H + LIBNET_IP_H) { fprintf(stderr, "write_ip\n"); } printf("Completed %d of %d, wrote %d bytes\n", n + 1, p_num, c); } libnet_destroy_packet_arena(&arena_p); /* Blah. */ return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS); }