📄 ip-spoof.txt
字号:
sp_data+sp_ipoptlen,sp_tcpoptlen);if (sp_datalen!=0) memcpy(sp_buffer+IP_HEAD_BASE+TCP_HEAD_BASE+sp_ipoptlen+sp_tcpoptlen, sp_data+sp_ipoptlen+sp_tcpoptlen,sp_datalen);sp_struct.fd = sp_fd; sp_struct.data = sp_data;sp_struct.datalen = sp_datalen;sp_struct.source = sp_source;sp_struct.source_port = sp_source_port;sp_struct.dest = sp_dest;sp_struct.dest_port = sp_dest_port;sp_struct.seq = sp_seq;sp_struct.ack = sp_ack;sp_struct.flags = sp_flags;sp_struct.buffer = sp_buffer;sp_struct.IP_optlen = sp_ipoptlen; sp_struct.TCP_optlen = sp_tcpoptlen; sp_fix_TCP_packet(&sp_struct);sp_fix_IP_packet(&sp_struct, 6);sp_send_packet(&sp_struct, 6);}void sp_fix_UDP_packet (struct sp_data_exchange *sp){ char sp_pseudo_ip_construct[MTU];struct UDP_header *sp_help_udp;struct pseudo_IP_header *sp_help_pseudo;int i;for(i=0;i<MTU;i++) {sp_pseudo_ip_construct[i]=0;}sp_help_udp = (struct UDP_header *) (sp->buffer+IP_HEAD_BASE+sp->IP_optlen);sp_help_pseudo = (struct pseudo_IP_header *) sp_pseudo_ip_construct;sp_help_udp->source = htons(sp->source_port);sp_help_udp->destination = htons(sp->dest_port);sp_help_udp->length = htons(sp->datalen+UDP_HEAD_BASE);sp_help_pseudo->source = sp_getaddrbyname(sp->source);sp_help_pseudo->destination = sp_getaddrbyname(sp->dest);sp_help_pseudo->zero_byte = 0;sp_help_pseudo->protocol = 17;sp_help_pseudo->TCP_UDP_len = htons(sp->datalen+UDP_HEAD_BASE);memcpy(sp_pseudo_ip_construct+12, sp_help_udp, sp->datalen+UDP_HEAD_BASE);sp_help_udp->checksum=in_cksum((unsigned short *) sp_pseudo_ip_construct, sp->datalen+12+UDP_HEAD_BASE);#ifdef DEBUG printf("UDP header fixed...\n");#endif}void transmit_UDP (int sp_fd, char *sp_data, int sp_ipoptlen, int sp_datalen, char *sp_source, unsigned short sp_source_port, char *sp_dest, unsigned short sp_dest_port){char sp_buffer[1500];struct sp_data_exchange sp_struct;bzero(sp_buffer,1500);if (sp_ipoptlen!=0) memcpy(sp_buffer+IP_HEAD_BASE,sp_data,sp_ipoptlen);if (sp_data!=NULL) memcpy(sp_buffer+IP_HEAD_BASE+UDP_HEAD_BASE+sp_ipoptlen, sp_data+sp_ipoptlen,sp_datalen);sp_struct.fd = sp_fd; sp_struct.data = sp_data;sp_struct.datalen = sp_datalen;sp_struct.source = sp_source;sp_struct.source_port = sp_source_port;sp_struct.dest = sp_dest;sp_struct.dest_port = sp_dest_port;sp_struct.buffer = sp_buffer;sp_struct.IP_optlen = sp_ipoptlen;sp_struct.TCP_optlen = 0;sp_fix_UDP_packet(&sp_struct);sp_fix_IP_packet(&sp_struct, 17);sp_send_packet(&sp_struct, 17);}/* This routine stolen from ping.c -- HAHAHA!*/unsigned short in_cksum(unsigned short *addr,int len){register int nleft = len;register unsigned short *w = addr;register int sum = 0;unsigned short answer = 0; while (nleft > 1) { sum += *w++; nleft -= 2; }if (nleft == 1) { *(u_char *)(&answer) = *(u_char *)w ; sum += answer; }sum = (sum >> 16) + (sum & 0xffff);sum += (sum >> 16);answer = ~sum;return(answer);}/************************* Receiving department ****************************/int open_receiving (char *rc_device, char mode){int or_fd;struct sigaction rc_sa;int fcntl_flag;struct ifreq ifinfo;char test;/* create snoop socket and set interface promisc */if ((or_fd = socket(AF_INET, SOCK_PACKET, htons(0x3)))==-1) perror("Couldn't open Socket."), exit(1);strcpy(ifinfo.ifr_ifrn.ifrn_name,rc_device);if(ioctl(or_fd,SIOCGIFFLAGS,&ifinfo)<0) perror("Couldn't get flags."), exit(1);ifinfo.ifr_ifru.ifru_flags |= IFF_PROMISC;if(ioctl(or_fd,SIOCSIFFLAGS,&ifinfo)<0) perror("Couldn't set flags. (PROMISC)"), exit(1);if(mode&IO_HANDLE) { /* install handler */ rc_sa.sa_handler=rc_sigio; /* we don't use signal() */ sigemptyset(&rc_sa.sa_mask); /* because the timing window is */ rc_sa.sa_flags=0; /* too big... */ sigaction(SIGIO,&rc_sa,NULL); }if(fcntl(or_fd,F_SETOWN,getpid())<0) perror("Couldn't set ownership"), exit(1);if(mode&IO_HANDLE) { if( (fcntl_flag=fcntl(or_fd,F_GETFL,0))<0) perror("Couldn't get FLAGS"), exit(1); if(fcntl(or_fd,F_SETFL,fcntl_flag|FASYNC|FNDELAY)<0) perror("Couldn't set FLAGS"), exit(1); rc_fd_abc123=or_fd; }else { if(mode&IO_NONBLOCK) { if( (fcntl_flag=fcntl(or_fd,F_GETFL,0))<0) perror("Couldn't get FLAGS"), exit(1); if(fcntl(or_fd,F_SETFL,fcntl_flag|FNDELAY)<0) perror("Couldn't set FLAGS"), exit(1); }; };#ifdef DEBUG printf("Reading socket ready\n");#endifreturn or_fd;}/* returns 0 when no packet read! */int get_packet (int rc_fd, char *buffer, int *TCP_UDP_start,unsigned char *proto) {char help_buffer[MTU];int pack_len;struct IP_header *gp_IPhead;pack_len = read(rc_fd,help_buffer,1500);if(pack_len<0) { if(errno==EWOULDBLOCK) {pack_len=0;} else {perror("Read error:"); exit(1);} };if(pack_len>0) { pack_len -= DEV_PREFIX; memcpy(buffer,help_buffer+DEV_PREFIX,pack_len); gp_IPhead = (struct IP_header *) buffer; if(proto != NULL) *proto = gp_IPhead->protocol; if(TCP_UDP_start != NULL) *TCP_UDP_start = (gp_IPhead->verlen & 0xF) << 2; }return pack_len;}void wait_packet_timeout (int sig){alarm(0);WAIT_PACKET_WAIT_TIME=1;}int wait_packet(int wp_fd,struct sp_wait_packet *ret_values, char *wp_source, unsigned short wp_source_port, char *wp_dest, unsigned short wp_dest_port, int wp_flags, int wait_time) {char wp_buffer[1500];struct IP_header *wp_iphead;struct TCP_header *wp_tcphead;unsigned long wp_sourcel, wp_destl;int wp_tcpstart;char wp_proto;wp_sourcel=sp_getaddrbyname(wp_source);wp_destl=sp_getaddrbyname(wp_dest);WAIT_PACKET_WAIT_TIME=0;if(wait_time!=0) { signal(SIGALRM,wait_packet_timeout); alarm(wait_time); } while(1) { while(get_packet(wp_fd, wp_buffer, &wp_tcpstart, &wp_proto)<=0) { if (WAIT_PACKET_WAIT_TIME!=0) {alarm(0); return -1;} }; if(wp_proto == 6) { wp_iphead= (struct IP_header *) wp_buffer; wp_tcphead= (struct TCP_header *) (wp_buffer+wp_tcpstart); if( (wp_sourcel==wp_iphead->source)&&(wp_destl==wp_iphead->destination) ) { if( (ntohs(wp_tcphead->source)==wp_source_port) && (ntohs(wp_tcphead->destination)==wp_dest_port) ) { if( (wp_flags==0) || (ntohs(wp_tcphead->offset_flag)&wp_flags) ) { ret_values->seq=ntohl(wp_tcphead->seq_nr); ret_values->ack=ntohl(wp_tcphead->ACK_nr); ret_values->flags=ntohs(wp_tcphead->offset_flag)& (URG|ACK|PSH|FIN|RST|SYN); ret_values->datalen = ntohs(wp_iphead->length) - ((wp_iphead->verlen & 0xF) << 2) - ((ntohs(wp_tcphead->offset_flag) & 0xF000) >> 10); alarm(0); return 0; } } } } }/*impossible to get here.. but anyways*/alarm(0); return -1;}void close_receiving (void){close(rc_fd_abc123);}void rc_sigio (int sig) /* Packet handling routine */{char rc_buffer[1500];char packet_id [50];unsigned char *rc_so, *rc_dest;struct IP_header *rc_IPhead;struct TCP_header *rc_TCPhead;int pack_len;if(RC_FILTSET==0) return;if(SP_DATA_BUSY!=0) /* skip this packet */ return; pack_len = read(rc_fd_abc123,rc_buffer,1500);rc_IPhead = (struct IP_header *) (rc_buffer + DEV_PREFIX);if(rc_IPhead->protocol!=6) return; /* if not TCP */rc_TCPhead = (struct TCP_header *) (rc_buffer + DEV_PREFIX + ((rc_IPhead->verlen & 0xF) << 2)); rc_so = (unsigned char *) &(rc_IPhead->source);rc_dest = (unsigned char *) &(rc_IPhead->destination); sprintf(packet_id,"%u.%u.%u.%u.%u-%u.%u.%u.%u.%u", rc_so[0],rc_so[1],rc_so[2],rc_so[3],ntohs(rc_TCPhead->source), rc_dest[0],rc_dest[1],rc_dest[2],rc_dest[3],ntohs(rc_TCPhead->destination)); if(strcmp(packet_id,rc_filter_string)==0) { SP_DATA_BUSY=1; CUR_SEQ = ntohl(rc_TCPhead->seq_nr); CUR_ACK = ntohl(rc_TCPhead->ACK_nr); CUR_FLAGS = ntohs(rc_TCPhead->offset_flag); CUR_DATALEN = ntohs(rc_IPhead->length) - ((rc_IPhead->verlen & 0xF) << 2) - ((ntohs(rc_TCPhead->offset_flag) & 0xF000) >> 10); CUR_COUNT++; SP_DATA_BUSY=0; }}void set_filter (char *f_source, unsigned short f_source_port, char *f_dest, unsigned short f_dest_port){unsigned char *f_so, *f_des;unsigned long f_sol, f_destl;RC_FILTSET=0;if(DEV_PREFIX==9999) fprintf(stderr,"DEV_PREFIX not set!\n"), exit(1);f_sol = sp_getaddrbyname(f_source);f_destl = sp_getaddrbyname(f_dest);f_so = (unsigned char *) &f_sol;f_des = (unsigned char *) &f_destl; sprintf(rc_filter_string,"%u.%u.%u.%u.%u-%u.%u.%u.%u.%u", f_so[0],f_so[1],f_so[2],f_so[3],f_source_port, f_des[0],f_des[1],f_des[2],f_des[3],f_dest_port); RC_FILTSET=1;}-----------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -