static int plist_init(plist *self, PyObject *args, PyObject *kwargs) { int errnum = 0; u_int16_t bport, eport; char *token_list = NULL; libnet_t l; libnet_plist_t *pl = NULL; static char *kwlist[] = {"token_list", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &token_list)) return -1; if (token_list == NULL) { PyErr_SetString(PyExc_TypeError, "argument 'token_list' must be a string"); return -1; } if (libnet_plist_chain_new(&l, &pl, token_list) == -1) { PyErr_SetString(PyErr_LibnetError, libnet_geterror(&l)); return -1; } while((errnum = libnet_plist_chain_next_pair(pl, &bport, &eport)) == 1) { if (PyList_Append((PyObject *)self, Py_BuildValue("(I,I)", bport, eport)) == -1) { errnum = -1; goto fail; } } if (errnum == -1) PyErr_SetString(PyErr_LibnetError, libnet_geterror(&l)); fail: libnet_plist_chain_free(pl); return (errnum >= 0)?0:-1; }
int main(int argc, char *argv[]) { int packet_size, /* size of our packet */ payload_size, /* size of our packet */ c; /* misc */ u_long src_ip, dst_ip; /* source ip, dest ip */ u_short bport, eport; /* beginning and end ports */ u_short cport; /* current port */ u_char payload[MAX_PAYLOAD_SIZE]; /* packet payload */ u_char *packet; /* pointer to our packet buffer */ char err_buf[LIBNET_ERRBUF_SIZE]; /* error buffer */ u_char *device; /* pointer to the device to use */ struct libnet_link_int *network; /* pointer to link interface struct */ struct libnet_plist_chain plist; /* plist chain */ struct libnet_plist_chain *plist_p; /* plist chain pointer */ printf("libnet example code:\tmodule 4\n\n"); printf("packet injection interface:\tlink layer\n"); printf("packet type:\t\t\tUDP [with payload] using port list chaining\n"); plist_p = NULL; device = NULL; src_ip = 0; dst_ip = 0; while ((c = getopt(argc, argv, "i:d:s:p:")) != EOF) { switch (c) { case 'd': if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE))) { libnet_error(LIBNET_ERR_FATAL, "Bad destination IP address: %s\n", optarg); } break; case 'i': device = optarg; break; case 's': if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE))) { libnet_error(LIBNET_ERR_FATAL, "Bad source IP address: %s\n", optarg); } break; case 'p': plist_p = &plist; if (libnet_plist_chain_new(&plist_p, optarg) == -1) { libnet_error(LIBNET_ERR_FATAL, "Could not build port list\n"); } break; default: usage(argv[0]); exit(EXIT_FAILURE); } } if (!src_ip || !dst_ip || !plist_p) { usage(argv[0]); exit(EXIT_FAILURE); } c = argc - optind; if (c != 1) { usage(argv[0]); exit(EXIT_FAILURE); } memset(payload, 0, sizeof(payload)); strncpy(payload, argv[optind], strlen(argv[optind])); /* * Step 1: Network Initialization (interchangable with step 2). */ if (device == NULL) { struct sockaddr_in sin; /* * Try to locate a device. */ if (libnet_select_device(&sin, &device, err_buf) == -1) { libnet_error(LIBNET_ERR_FATAL, "libnet_select_device failed: %s\n", err_buf); } printf("device:\t\t\t\t%s\n", device); } if ((network = libnet_open_link_interface(device, err_buf)) == NULL) { libnet_error(LIBNET_ERR_FATAL, "libnet_open_link_interface: %s\n", err_buf); } /* * Get the payload from the user. Hrm. This might fail on a Sparc * if byte alignment is off... */ payload_size = strlen(payload); /* * We're going to build a UDP packet with a payload using the * link-layer API, so this time we need memory for a ethernet header * as well as memory for the ICMP and IP headers and our payload. */ packet_size = LIBNET_IP_H + LIBNET_ETH_H + LIBNET_UDP_H + payload_size; /* * Step 2: Memory Initialization (interchangable with step 1). */ if (libnet_init_packet(packet_size, &packet) == -1) { libnet_error(LIBNET_ERR_FATAL, "libnet_init_packet failed\n"); } /* * Step 3: Packet construction (ethernet header). */ libnet_build_ethernet(enet_dst, enet_src, ETHERTYPE_IP, NULL, 0, packet); /* * Step 3: Packet construction (IP header). */ libnet_build_ip(LIBNET_UDP_H + payload_size, 0, /* IP tos */ 242, /* IP ID */ 0, /* Frag */ 64, /* TTL */ IPPROTO_UDP, /* Transport protocol */ src_ip, /* Source IP */ dst_ip, /* Destination IP */ NULL, /* Pointer to payload (none) */ 0, packet + LIBNET_ETH_H); /* Packet header memory */ while (libnet_plist_chain_next_pair(plist_p, &bport, &eport)) { while (!(bport > eport) && bport != 0) { cport = bport++; /* * Step 3: Packet construction (UDP header). */ libnet_build_udp(242, /* source port */ cport, /* dest. port */ payload, /* payload */ payload_size, /* payload length */ packet + LIBNET_ETH_H + LIBNET_IP_H); /* * Step 4: Packet checksums (ICMP header *AND* IP header). */ if (libnet_do_checksum(packet + ETH_H, IPPROTO_UDP, LIBNET_UDP_H + payload_size) == -1) { libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n"); } if (libnet_do_checksum(packet + ETH_H, IPPROTO_IP, LIBNET_IP_H) == -1) { libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n"); } /* * Step 5: Packet injection. */ c = libnet_write_link_layer(network, device, packet, packet_size); if (c < packet_size) { libnet_error(LN_ERR_WARNING, "libnet_write_link_layer only wrote %d bytes\n", c); } else { printf("construction and injection completed, wrote all %d bytes, port %d\n", c, cport); } } } /* * Shut down the interface. */ if (libnet_close_link_interface(network) == -1) { libnet_error(LN_ERR_WARNING, "libnet_close_link_interface couldn't close the interface"); } /* * Free packet memory. */ libnet_destroy_packet(&packet); return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS); }
int main(int argc, char **argv) { int c, build_ip; struct timeval r; struct timeval s; struct timeval e; libnet_t *l; libnet_ptag_t udp; char *payload; libnet_ptag_t t; struct libnet_stats ls; u_short payload_s; u_long src_ip, dst_ip; u_short bport, eport, cport; libnet_plist_t plist, *plist_p; char errbuf[LIBNET_ERRBUF_SIZE]; printf("libnet 1.1.0 packet shaping: UDP2[link]\n"); l = libnet_init( LIBNET_LINK, /* injection type */ NULL, /* network interface */ errbuf); /* errbuf */ if (l == NULL) { fprintf(stderr, "libnet_init() failed: %s", errbuf); exit(EXIT_FAILURE); } src_ip = 0; dst_ip = 0; payload = NULL; payload_s = 0; plist_p = NULL; while ((c = getopt(argc, argv, "d:s:p:P:")) != EOF) { switch (c) { case 'd': if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1) { fprintf(stderr, "Bad destination IP address: %s\n", optarg); exit(1); } break; case 's': if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1) { fprintf(stderr, "Bad source IP address: %s\n", optarg); exit(1); } break; case 'P': plist_p = &plist; if (libnet_plist_chain_new(l, &plist_p, optarg) == -1) { fprintf(stderr, "Bad token in port list: %s\n", libnet_geterror(l)); exit(1); } break; case 'p': payload = optarg; payload_s = strlen(payload); break; default: usage(argv[0]); exit(EXIT_FAILURE); } } if (!src_ip || !dst_ip || !plist_p) { usage(argv[0]); exit(EXIT_FAILURE); } udp = 0; #if !(__WIN32__) gettimeofday(&s, NULL); #else /* This obviously is not as good - but it compiles now. */ s.tv_sec = time(NULL); s.tv_usec = 0; #endif build_ip = 1; while (libnet_plist_chain_next_pair(plist_p, &bport, &eport)) { while (!(bport > eport) && bport != 0) { cport = bport++; udp = libnet_build_udp( 1025, /* source port */ cport, /* destination port */ LIBNET_UDP_H + payload_s, /* packet size */ 0, /* checksum */ payload, /* payload */ payload_s, /* payload size */ l, /* libnet handle */ udp); /* libnet id */ if (udp == -1) { fprintf(stderr, "Can't build UDP header (at port %d): %s\n", cport, libnet_geterror(l)); goto bad; } if (build_ip) { build_ip = 0; t = libnet_build_ipv4( LIBNET_IPV4_H + LIBNET_UDP_H + payload_s, /* length */ 0, /* TOS */ 242, /* IP ID */ 0, /* IP Frag */ 64, /* TTL */ IPPROTO_UDP, /* protocol */ 0, /* checksum */ src_ip, /* source IP */ dst_ip, /* destination IP */ NULL, /* payload */ 0, /* payload size */ l, /* libnet handle */ 0); if (t == -1) { fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l)); goto bad; } t = libnet_build_ethernet( enet_dst, /* ethernet destination */ enet_src, /* ethernet source */ ETHERTYPE_IP, /* protocol type */ NULL, /* payload */ 0, /* payload size */ l, /* libnet handle */ 0); if (t == -1) { fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l)); goto bad; } } //usleep(100); c = libnet_write(l); if (c == -1) { fprintf(stderr, "write error: %s\n", libnet_geterror(l)); } else { fprintf(stderr, "wrote %d UDP packet to port %d\n", c, cport); // fprintf(stderr, "."); } } } #if !(__WIN32__) gettimeofday(&s, NULL); #else /* This obviously is not as good - but it compiles now. */ s.tv_sec = time(NULL); s.tv_usec = 0; #endif libnet_timersub(&e, &s, &r); fprintf(stderr, "Total time spent in loop: %ld.%ld\n", r.tv_sec, r.tv_usec); libnet_stats(l, &ls); fprintf(stderr, "Packets sent: %ld\n" "Packet errors: %ld\n" "Bytes written: %ld\n", ls.packets_sent, ls.packet_errors, ls.bytes_written); libnet_destroy(l); return (EXIT_SUCCESS); bad: libnet_destroy(l); return (EXIT_FAILURE); }
int main (int argc, char **argv) { libnet_t *l; char err_buf[LIBNET_ERRBUF_SIZE]; char *iface = NULL; char *src_s = NULL, *victim_s = NULL, *plist_s = NULL; libnet_plist_t *plist=NULL; struct libnet_in6_addr src, victim; int s = 0, d = 0, i = 0, p = 0, opt; libnet_ptag_t tcp_tag; u_long seq; extern char *optarg; extern int opterr; printf ("\n6pack - IPv6 Port Scanner - v0.0002 ( http://genhex.org/projects/6pack/ )\n(c) 2003 Bruno Morisson <*****@*****.**>\n\n"); while ((opt = getopt (argc, argv, "vTUb:t:i:p:s:d:")) != -1) { switch (opt) { case 's': if (s) usage (); src_s = optarg; s++; break; case 'd': if (d) usage (); victim_s = optarg; d++; break; case 'i': if (i) usage (); iface = optarg; i++; break; case 'p': if (p) usage (); plist_s = optarg; p++; break; case 'v': verbose++; break; case 't': timeout = atoi (optarg); break; default: usage (); } } if (d != 1 || p != 1) usage (); if (!(l = libnet_init (LIBNET_RAW6, iface, err_buf))) { fprintf (stderr, "error opening raw sock: %s\n", err_buf); exit (-1); } if (!iface) iface = libnet_getdevice (l); if (libnet_plist_chain_new (l, &plist, plist_s) == -1) { fprintf (stderr, "invalid portlist %s\n", libnet_geterror (l)); exit (-1); } if(s) src = libnet_name2addr6 (l, src_s, LIBNET_RESOLVE); else src = libnet_get_ipaddr6(l); if (!memcmp ((char *) &src, (char *) &in6addr_error, sizeof (in6addr_error))) { fprintf (stderr, "error in address: %s\n", libnet_geterror (l)); exit (-1); } victim = libnet_name2addr6 (l, victim_s, LIBNET_RESOLVE); if (!memcmp ((char *) &victim, (char *) &in6addr_error, sizeof (in6addr_error))) { fprintf (stderr, "error in address: %s\n", libnet_geterror (l)); exit (-1); } printf ("Using device: %s\n", iface); tcp_tag = build_pkt (l, &seq, src, victim); signal (SIGCHLD, SIG_IGN); /* We really shouldn't do this... */ if (!collect (iface, seq)) return -1; printf ("Scanning ports %s on %s\n", libnet_plist_chain_dump_string (plist), victim_s); send_tcp_pkts (l, plist, tcp_tag, seq); return 0; }