Example #1
0
int read_pcap(const char *p_file){

    mWorld.qlist_head = mWorld.qlist_rear = 0;
    mWorld.count = 0;
    pcap_t *handler;
    char errbuf[PCAP_ERRBUF_SIZE];
    struct bpf_program filter;

    if(access(p_file,F_OK)){
        fprintf(stderr,"Error opening %s (is the path correct ? )\n",p_file);
        return -1;
    } else {   
        if((handler = pcap_open_offline(p_file,errbuf)) == NULL){
            fprintf(stderr,"[pcap_open_offline] Error\n");
            return -1;
        }
        
        mWorld.dl_len = pcap_dloff(handler);

        if(pcap_compile(handler,&filter,DNS_QUERY_FILTER,0,0) == -1)
            return PCAP_COMPILE_ERROR;

        if(pcap_setfilter(handler,&filter) == -1)
            return PCAP_SETFILTER_ERROR;
    
        if(pcap_dispatch(handler,0,(void *) pcap_callback, (void *) &mWorld) < 0){
            fprintf(stderr,"[pcap_dispatch] Error\n"); return -1;
        }

        pcap_close(handler);
    }
    return 1;

}
Example #2
0
int
main(int argc, char *argv[])
{
	int c, sock;
	char *intf, *filter, ebuf[PCAP_ERRBUF_SIZE];
	pcap_t *pd;
	
	intf = NULL;
	
	while ((c = getopt(argc, argv, "i:n:Ih?V")) != -1) {
		switch (c) {
		case 'i':
			intf = optarg;
			break;
		case 'n':
			Opt_nice = atoi(optarg);
			if (Opt_nice < MIN_NICE || Opt_nice > MAX_NICE)
				usage();
			break;
		case 'I':
			Opt_icmp = 1;
			break;
		default:
			usage();
			break;
		}
	}
	if (intf == NULL && (intf = pcap_lookupdev(ebuf)) == NULL)
		errx(1, "%s", ebuf);
	
	argc -= optind;
	argv += optind;
	
	if (argc == 0)
		usage();

	filter = copy_argv(argv);
	
	if ((pd = pcap_init(intf, filter, 128)) == NULL)
		errx(1, "couldn't initialize sniffing");

	if ((pcap_off = pcap_dloff(pd)) < 0)
		errx(1, "couldn't determine link layer offset");
	
	if ((sock = libnet_open_raw_sock(IPPROTO_RAW)) == -1)
		errx(1, "couldn't initialize sending");
	
	warnx("listening on %s [%s]", intf, filter);
	
	tcp_nice_loop(pd, sock);
	
	/* NOTREACHED */
	
	exit(0);
}
Example #3
0
int main(int argc, char *argv[])
{
    unsigned int port;
    int opt;
    unsigned long delay;
    pid_t pid;
    char *interface, *endptr;
    char libnet_ebuf[LIBNET_ERRBUF_SIZE];
    pcap_t *pcap;
    char pcap_expr[100];
    libnet_t *libnet;
    u_int32_t target_addr, source_addr;
    struct sigaction sa;

    interface = NULL;

    while ((opt = getopt(argc, argv, "i:")) != -1) {
        switch (opt) {
        case 'i':
            interface = optarg;
            break;
        default:
            usage();
            break;
        }
    }

    argc -= optind;
    argv += optind;

    if (argc != 3) {
        usage();
    }

    port = strtoul(argv[1], &endptr, 10);
    if ((*endptr != '\0') || port < 1 || port > 65535) {
        errx(1, "Invalid port: %s", argv[1]);
    }

    errno = 0;
    delay = strtoul(argv[2], &endptr, 10);
    if (*endptr != '\0') {
        errx(1, "invalid delay: %s", argv[2]);
    } else if ((errno == ERANGE &&
                (delay == LONG_MAX)) ||
               (errno != 0 && delay == 0)) {
        errx(1, "invalid delay (%s): %s", strerror(errno), argv[2]);
    }

    /* Initialize libnet */

    if ((libnet = libnet_init(LIBNET_RAW4, interface, libnet_ebuf)) == NULL) {
        errx(1, "couldn't initialize libnet: %s", libnet_ebuf);
    }

    if ((target_addr = libnet_name2addr4(libnet, argv[0], LIBNET_RESOLVE)) == -1) {
        errx(1, "could not resolve target %s", argv[0]);
    }

    if ((source_addr = libnet_get_ipaddr4(libnet)) == -1) {
        errx(1, "could not get local IP: %s", libnet_ebuf);
    }

    /* Initialize pcap */
    snprintf(pcap_expr, 99, "tcp and tcp[tcpflags] == 18 and src host %s and port %s",
             argv[0], argv[1]);
    if ((pcap = pcap_init(interface, pcap_expr, 64)) == NULL)
        errx(1, "couldn't initialize sniffing");

    if ((pcap_off = pcap_dloff(pcap)) < 0)
        errx(1, "couldn't determine link layer offset");

    /* Fork */

    pid = fork();
    if (pid == -1)
        errx(1, "fork failed");
    else if (pid == 0)
        send_syns(libnet, source_addr, target_addr, port, delay);
    else {
        child_pid = pid;
        sa.sa_handler = &handle_signal;
        sigaction(SIGINT, &sa, NULL);
        sigaction(SIGQUIT, &sa, NULL);
        sigaction(SIGTERM, &sa, NULL);

        send_acks(pcap, libnet);
    }

    return EXIT_SUCCESS;

}
Example #4
0
int
main(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind;
	int c;
	char *intf, *filter, ebuf[PCAP_ERRBUF_SIZE];
	char libnet_ebuf[LIBNET_ERRBUF_SIZE];
	libnet_t *l;
	pcap_t *pd;
	
	intf = NULL;
	
	while ((c = getopt(argc, argv, "i:AIMh?V")) != -1) {
		switch (c) {
		case 'i':
			intf = optarg;
			break;
		case 'A':
			Opt_win = 1;
			break;
		case 'I':
			Opt_icmp = 1;
			break;
		case 'M':
			Opt_pmtu = 1;
			break;
		default:
			usage();
			break;
		}
	}
	if (intf == NULL && (intf = pcap_lookupdev(ebuf)) == NULL)
		errx(1, "%s", ebuf);
	
	argc -= optind;
	argv += optind;
	
	if (argc == 0)
		usage();

	if ((Opt_win | Opt_icmp | Opt_pmtu) == 0)
		Opt_win = Opt_icmp = Opt_pmtu = 1;
	
	filter = copy_argv(argv);
	
	if ((pd = pcap_init(intf, filter, 128)) == NULL)
		errx(1, "couldn't initialize sniffing");

	if ((pcap_off = pcap_dloff(pd)) < 0)
		errx(1, "couldn't determine link layer offset");
	
	if ((l = libnet_init(LIBNET_RAW4, intf, libnet_ebuf)) == NULL)
		errx(1, "couldn't initialize sending");
	
	libnet_seed_prand(l);
	
	warnx("listening on %s [%s]", intf, filter);
	
	pcap_loop(pd, -1, tcp_nice_cb, (u_char *)l);
	
	/* NOTREACHED */
	
	exit(0);
}
Example #5
0
int
main(int argc, char *argv[])
{
	struct intf_entry ifent;
	intf_t *intf;
	int i, tests;
	char *cmd;
	
	if (argc < 3)
		usage();

	for (tests = 0, i = 1; i < argc - 1; i++) {
		cmd = argv[i];
		
		if (strcmp(cmd, "all") == 0)
			tests = ~0;
		else if (strcmp(cmd, "ping") == 0)
			tests |= TEST_PING;
		else if (strcmp(cmd, "ip-opt") == 0)
			tests |= TEST_IP_OPT;
		else if (strcmp(cmd, "ip-tracert") == 0)
			tests |= TEST_IP_TRACERT;
		else if (strcmp(cmd, "frag") == 0)
			tests |= TEST_FRAG;
		else if (strcmp(cmd, "frag-new") == 0)
			tests |= TEST_FRAG_NEW;
		else if (strcmp(cmd, "frag-old") == 0)
			tests |= TEST_FRAG_OLD;
		else if (strcmp(cmd, "frag-timeout") == 0)
			tests |= TEST_FRAG_TIMEOUT;
		else
			usage();
	}
	if (addr_aton(argv[i], &ctx.dst) < 0)
		err(1, "invalid host %s", argv[i]);

	if ((intf = intf_open()) == NULL)
		err(1, "couldn't open interface handle");

	ifent.intf_len = sizeof(ifent);
	
	if (intf_get_dst(intf, &ifent, &ctx.dst) < 0)
		err(1, "couldn't find interface for %s", addr_ntoa(&ctx.dst));
	
	memcpy(&ctx.src, &ifent.intf_addr, sizeof(ctx.src));
	ctx.src.addr_bits = IP_ADDR_BITS;
	
	intf_close(intf);
	
	if ((ctx.ip = ip_open()) == NULL)
		err(1, "couldn't open raw IP interface");

	if ((ctx.pcap = pcap_open(ifent.intf_name)) == NULL)
		err(1, "couldn't open %s for sniffing", ifent.intf_name);
	
	if ((ctx.dloff = pcap_dloff(ctx.pcap)) < 0)
		err(1, "couldn't determine link layer offset");
	
	ctx.rnd = rand_open();
	pkt_init(16);
	TAILQ_INIT(&ctx.pktq);

	ping = pkt_new();
	ip_pack_hdr(ping->pkt_ip, 0, IP_HDR_LEN + 8 + 24, 666, 0,
	    IP_TTL_DEFAULT, IP_PROTO_ICMP, ctx.src.addr_ip, ctx.dst.addr_ip);
	icmp_pack_hdr_echo(ping->pkt_icmp, ICMP_ECHO, ICMP_CODE_NONE,
	    666, 1, "AAAAAAAABBBBBBBBCCCCCCCC", 24);
	ping->pkt_end = ping->pkt_eth_data + IP_HDR_LEN + 8 + 24;
	pkt_decorate(ping);
	
	if ((tests & TEST_PING) != 0)
		test_ping();
	if ((tests & TEST_IP_OPT) != 0)
		test_ip_opt();
	if ((tests & TEST_IP_TRACERT) != 0)
		test_ip_tracert();
	if ((tests & TEST_FRAG) != 0)
		test_frag(NULL, 0);
	if ((tests & TEST_FRAG_NEW) != 0)
		test_frag("new", 0);
	if ((tests & TEST_FRAG_OLD) != 0)
		test_frag("old", 0);
	if ((tests & TEST_FRAG_TIMEOUT) != 0)
		test_frag(NULL, 1);

	rand_close(ctx.rnd);
	pcap_close(ctx.pcap);
	ip_close(ctx.ip);
	
	exit(0);
}
Example #6
0
int
main(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind;
	int c;
	char *p, *intf, *filter, ebuf[PCAP_ERRBUF_SIZE];
	char libnet_ebuf[LIBNET_ERRBUF_SIZE];
	libnet_t *l;
	
	intf = NULL;
	
	while ((c = getopt(argc, argv, "i:m:123456789h?V")) != -1) {
		switch (c) {
		case 'i':
			intf = optarg;
			break;
		case 'm':
			Opt_max_kill = atoi(optarg);
			break;
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			p = argv[optind - 1];
			if (p[0] == '-' && p[1] == c && p[2] == '\0')
				Opt_severity = atoi(++p);
			else
				Opt_severity = atoi(argv[optind] + 1);
			break;
		default:
			usage();
			break;
		}
	}
	if (intf == NULL && (intf = pcap_lookupdev(ebuf)) == NULL)
		errx(1, "%s", ebuf);

	argc -= optind;
	argv += optind;
	
	if (argc == 0)
		usage();
	
	filter = copy_argv(argv);
	
	if ((pd = pcap_init(intf, filter, 64)) == NULL)
		errx(1, "couldn't initialize sniffing");

	if ((pcap_off = pcap_dloff(pd)) < 0)
		errx(1, "couldn't determine link layer offset");
	
	if ((l = libnet_init(LIBNET_RAW4, intf, libnet_ebuf)) == NULL)
		errx(1, "couldn't initialize sending");
	
	libnet_seed_prand(l);
	
	warnx("listening on %s [%s]", intf, filter);
	
	pcap_loop(pd, -1, tcp_kill_cb, (u_char *)l);
  
	/* NOTREACHED */
	
	exit(0);
}