Esempio n. 1
0
int
main(int argc, char *argv[])
{
    int  c;
    char errbuf[256];
    char *device = NULL;
    struct libnet_link_int *l;
    struct ether_addr *e;
    u_long i;

    while ((c = getopt(argc, argv, "i:")) != EOF)
    {
        switch (c)
        {
            case 'i':
                device = optarg;
                break;
            default:
                exit(EXIT_FAILURE);
        }
    }

    if (!device)
    {
        fprintf(stderr, "Specify a device\n");
        exit(EXIT_FAILURE);
    }

    l = libnet_open_link_interface(device, errbuf);
    if (!l)
    {
        fprintf(stderr, "libnet_open_link_interface: %s\n", errbuf);
        exit(EXIT_FAILURE);
    }

    printf("Interface %s\n", device);
    e = libnet_get_hwaddr(l, device,  errbuf);
    if (!e)
    {
        fprintf(stderr, "Can't get hardware address: %s\n", errbuf);
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("MAC address: ");
        for (c = 0; c < 6; c++)
        {
            printf("%x", e->ether_addr_octet[c]);
            if (c != 5)
            {
                printf(":");
            }
        }
        printf("\n");
    }

    i = libnet_get_ipaddr(l, device, errbuf);
    if (!i)
    {
        fprintf(stderr, "Can't get ip address: %s\n", errbuf);
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("IP  address: ");
        printf("%s\n", libnet_host_lookup(ntohl(i), 0));
    }
    exit(EXIT_SUCCESS);
}
Esempio n. 2
0
int
libnet_do_arp(struct libnet_link_int *l, u_char *device, struct ether_addr *e,
        u_long ip)
{
    int n, i;
    u_char *buf, errbuf[256], *packet, *ip_p;
    struct libnet_ethernet_hdr *p;
    struct libnet_arp_hdr *a;
    u_long local_ip;
    pcap_t *pd;
    struct pcap_pkthdr pc_hdr;

    /*
     *  Initialize a packet.
     */
    if (libnet_init_packet(LIBNET_ARP_H + LIBNET_ETH_H, &buf) == -1)
    {
        perror("libnet_init_packet memory:");
        exit(EXIT_FAILURE);
    }

    /*
     *  Get the ethernet address of the device.
     */
    e = libnet_get_hwaddr(l, device, errbuf);
    if (e == NULL)
    {
        fprintf(stderr, "libnet_get_hwaddr: %s\n", errbuf);
        return (-1);
    }

    /*
     *  Get the IP address of the device.
     */
    local_ip = htonl(libnet_get_ipaddr(l, device, errbuf));
    if (!local_ip)
    {
        fprintf(stderr, "libnet_get_ipaddr: %s\n", errbuf);
        return (-1);
    }

    /*
     *  Open the pcap device.
     */
    pd = pcap_open_live(device, LIBNET_ARP_H + LIBNET_ETH_H, 1, 500, errbuf);
    if (pd == NULL)
    {
        fprintf(stderr, "pcap_open_live: %s\n", errbuf);
        return (-1);
    }

    /*
     *  Ethernet header
     */
    libnet_build_ethernet(
            enet_dst,               /* broadcast ethernet address */
            e->ether_addr_octet,    /* source ethernet address */
            ETHERTYPE_ARP,          /* this frame holds an ARP packet */
            NULL,                   /* payload */
            0,                      /* payload size */
            buf);                   /* packet header memory */

    /*
     *  ARP header
     */
    libnet_build_arp(
            ARPHRD_ETHER,           /* hardware address type */
            ETHERTYPE_IP,           /* protocol address type */
            ETHER_ADDR_LEN,         /* hardware address length */
            4,                      /* protocol address length */
            ARPOP_REQUEST,          /* packet type - ARP request */
            e->ether_addr_octet,    /* source (local) ethernet address */
            (u_char *)&local_ip,    /* source (local) IP address */
            enet_dst,               /* target's ethernet address (broadcast) */
            (u_char *)&ip,          /* target's IP address */
            NULL,                   /* payload */
            0,                      /* payload size */
            buf + LIBNET_ETH_H);    /* packet header memory */

    n = libnet_write_link_layer(l, device, buf, LIBNET_ARP_H + LIBNET_ETH_H);
    if (n == -1)
    {
        fprintf(stderr, "libnet_write_link_layer choked\n");
        return (-1);
    }
    printf("Sent a %d byte ARP request looking for the MAC of %s.\n",
        n, libnet_host_lookup(ip, 0));
    printf("Waiting for a response...\n");

    for (;(packet = ((u_char *)pcap_next(pd, &pc_hdr)));)
    {
        p = (struct libnet_ethernet_hdr *)(packet);
        if (ntohs(p->ether_type) == ETHERTYPE_ARP)
        {
            a = (struct libnet_arp_hdr *)(packet + LIBNET_ETH_H);
            if (ntohs(a->ar_op) == ARPOP_REPLY)
            {
                ip_p = (u_char *)&ip;
                if (IP_UCHAR_COMP(a->ar_spa, ip_p))
                {
                    printf("Target hardware address: ");
                    for (i = 0; i < 6; i++)
                    {
                        printf("%x", a->ar_sha[i]);
                        if (i != 5)
                        {
                            printf(":");
                        }
                    }
                    printf("\n");
                }
            }
        }
    }

    libnet_destroy_packet(&buf);
    return (n);
}
Esempio n. 3
0
int main(int argc, char **argv) {

	bpf_u_int32 mask, mask2;				/* subnet mask */
	bpf_u_int32 net, net2;					/* ip */
	char errbuf[PCAP_ERRBUF_SIZE];			/* error buffer */
	pcap_t *handle;							/* packet capture handle */
	char filter_exp[] = "ip";				/* filter expression */
	struct bpf_program fp;					/* compiled filter program (expression) */
	int c,num_packets = -1;					/* number of packets to capture */
    
	struct libnet_link_int *l;
	u_long i;
	 
	/* check command-line options */
    while ((c = getopt(argc, argv, "i:I:d:n:hpcf:")) != EOF) {
        switch (c) {
            case 'i':
				dev = optarg;
				dev2 = dev;				
                break;
            case 'I':
				dev2 = optarg;
				break;
			case 'd':
				daddr2 = optarg;
                break;
            case 'n':
				num_packets = atoi(optarg);
                break;
            case 'f':
				strcpy(filter_exp, optarg);
                break;
            case 'h':
				hide_header = 1;
                break;
            case 'p':
				hide_payload = 1;
                break;
            case 'c':
				capture_only = 1;
                break;
            default:
                print_app_usage(argv[0]);
                exit(EXIT_FAILURE);
        }
    }

	if (dev == NULL) {
		print_app_usage(argv[0]);
		exit(EXIT_FAILURE);
	}

	/* get source ip address associated with forward device */
	l = libnet_open_link_interface(dev2, errbuf);
	if (!l) {
        printf("libnet_open_link_interface: %s\n", errbuf);
        goto failure;
    }
	
	i = libnet_get_ipaddr(l, dev2, errbuf);
	if (!i) {
        printf("Can't get ip address: %s\n", errbuf);
        goto failure;
    }
	
	saddr2 = (char *)libnet_host_lookup(ntohl(i), 0);
					
	/* get network number and mask associated with capture device */
	if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
		printf("  Error: couldn't get netmask for interface %s\n\n", errbuf);
		goto failure;
	}

	/* print capture info */
	printf("\n Capture from: %s\n", dev);
	printf("   Forward to: %s\n", dev2);
	printf("  Src Address: %s\n", saddr2);
	if (daddr2) printf("  Dst Address: %s\n", daddr2);
	else printf("  Dst Address: Not changed\n");
	if(num_packets > 0) printf("Packets to capture: %d\n", num_packets);
	printf("Packet Filter: %s\n", filter_exp);
	printf("\n");

	/* open capture device */
	handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
	if (handle == NULL) {
		printf("\n  Error: couldn't open interface %s: %s\n\n", dev, errbuf);
		goto failure;
	}

	/* make sure we're capturing on an Ethernet device */
	if (pcap_datalink(handle) != DLT_EN10MB) {
		printf("\n  Error: %s is not on ethernet\n\n", dev);
		goto failure;
	}

	/* compile the filter expression */
	if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
		printf("\n  Error: couldn't parse filter %s: %s\n\n", filter_exp, pcap_geterr(handle));
		goto failure;
	}

	/* apply the compiled filter */
	if (pcap_setfilter(handle, &fp) == -1) {
		printf("\n  Error: couldn't install filter %s: %s\n\n", filter_exp, pcap_geterr(handle));
		goto failure;
	}

	/* now we can set our callback function */
	pcap_loop(handle, num_packets, got_packet, NULL);

	/* cleanup */
	pcap_freecode(&fp);
	pcap_close(handle);

	printf("\nCapture and forward complete.\n\n");
	exit(EXIT_SUCCESS);

failure:
	exit(EXIT_FAILURE);

}