📄 ip-spoof.txt
字号:
/* dest/port. This enables the updating of global data. Can */ /* be called multiple times. *//* *//* void close_receiving (void) *//* When opened a IO_HANDLE mode receiving socket close it with *//* this. *//* *//**** Global DATA (IO_HANDLE mode) ****************************************//* *//* When accessing global data, copy the values to local vars and then use *//* them. Reduce access time to a minimum. *//* Mind you use of this is very limited, if you are a novice on IO, just *//* ignore it, the other functions are good enough!). If not, rewrite the *//* handler for your own use... *//* *//* sig_atomic_t SP_DATA_BUSY *//* Put this on NON-ZERO when accesing global data. Incoming *//* packets will be ignored then, data can not be overwritten. *//* *//* unsigned long int CUR_SEQ, CUR_ACK; *//* Last recorded SEQ and ACK number of the filtered "stream". *//* Before accessing this data set SP_DATA_BUSY non-zero, *//* afterward set it back to zero. *//* *//* unsigned long int CUR_COUNT; *//* increased everytime other data is updated *//* *//* unsigned int CUR_DATALEN; *//* Length of date in last TCP packet *//* *//**************************************************************************/#include "sys/socket.h" /* includes, what would we do without them */#include "netdb.h"#include "stdlib.h"#include "unistd.h"#include "stdio.h"#include "errno.h"#include "netinet/in.h"#include "netinet/ip.h"#include "linux/if.h"#include "sys/ioctl.h"#include "sys/types.h"#include "signal.h"#include "fcntl.h"#undef DEBUG #define IP_VERSION 4 /* keep y'r hands off... */#define MTU 1500 #define IP_HEAD_BASE 20 /* using fixed lengths to send */ #define TCP_HEAD_BASE 20 /* no options etc... */ #define UDP_HEAD_BASE 8 /* Always fixed */ #define IO_HANDLE 1#define IO_NONBLOCK 2int DEV_PREFIX = 9999; sig_atomic_t WAIT_PACKET_WAIT_TIME=0;/**** IO_HANDLE ************************************************************/int rc_fd_abc123;sig_atomic_t RC_FILTSET=0;char rc_filter_string[50]; /* x.x.x.x.p-y.y.y.y.g */sig_atomic_t SP_DATA_BUSY=0;unsigned long int CUR_SEQ=0, CUR_ACK=0, CUR_COUNT=0;unsigned int CUR_DATALEN;unsigned short CUR_FLAGS;/***************************************************************************/struct sp_wait_packet{ unsigned long seq,ack; unsigned short flags; int datalen;}; /* Code from Sniffit - BTW my own program.... no copyright violation here */ #define URG 32 /* TCP flags */#define ACK 16 #define PSH 8 #define RST 4#define SYN 2 #define FIN 1 struct PACKET_info{ int len, datalen; unsigned long int seq_nr, ACK_nr; u_char FLAGS;};struct IP_header /* The IPheader (without options) */{ unsigned char verlen, type; unsigned short length, ID, flag_offset; unsigned char TTL, protocol; unsigned short checksum; unsigned long int source, destination;};struct TCP_header /* The TCP header (without options) */{ unsigned short source, destination; unsigned long int seq_nr, ACK_nr; unsigned short offset_flag, window, checksum, urgent;};struct UDP_header /* The UDP header */{ unsigned short source, destination; unsigned short length, checksum;}; struct pseudo_IP_header /* The pseudo IP header (checksum calc) */ { unsigned long int source, destination; char zero_byte, protocol; unsigned short TCP_UDP_len;};/* data structure for argument passing */struct sp_data_exchange { int fd; /* Sh!t from transmit_TCP */ char *data; int datalen; char *source; unsigned short source_port; char *dest; unsigned short dest_port; unsigned long seq, ack; unsigned short flags; char *buffer; /* work buffer */ int IP_optlen; /* IP options length in bytes */ int TCP_optlen; /* TCP options length in bytes */ };/**************** all functions *******************************************/void transmit_TCP (int fd, char *sp_data, int sp_ipoptlen, int sp_tcpoptlen, int sp_datalen, char *sp_source, unsigned short sp_source_port, char *sp_dest, unsigned short sp_dest_port, unsigned long sp_seq, unsigned long sp_ack, unsigned short sp_flags);void transmit_UDP (int sp_fd, char *sp_data, int ipoptlen, int sp_datalen, char *sp_source, unsigned short sp_source_port, char *sp_dest, unsigned short sp_dest_port);int get_packet (int rc_fd, char *buffer, int *, unsigned char*);int wait_packet(int,struct sp_wait_packet *,char *, unsigned short,char *, unsigned short, int, int);static unsigned long sp_getaddrbyname(char *);int open_sending (void);int open_receiving (char *, char);void close_receiving (void);void sp_send_packet (struct sp_data_exchange *, unsigned char);void sp_fix_TCP_packet (struct sp_data_exchange *);void sp_fix_UDP_packet (struct sp_data_exchange *);void sp_fix_IP_packet (struct sp_data_exchange *, unsigned char);unsigned short in_cksum(unsigned short *, int );void rc_sigio (int);void set_filter (char *, unsigned short, char *, unsigned short);/********************* let the games commence ****************************/static unsigned long sp_getaddrbyname(char *sp_name){struct hostent *sp_he;int i;if(isdigit(*sp_name)) return inet_addr(sp_name);for(i=0;i<100;i++) { if(!(sp_he = gethostbyname(sp_name))) {printf("WARNING: gethostbyname failure!\n"); sleep(1); if(i>=3) /* always a retry here in this kind of application */ printf("Coudn't resolv hostname."), exit(1); } else break; }return sp_he ? *(long*)*sp_he->h_addr_list : 0;}int open_sending (void){struct protoent *sp_proto; int sp_fd;int dummy=1;/* they don't come rawer */if ((sp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))==-1) perror("Couldn't open Socket."), exit(1);#ifdef DEBUG printf("Raw socket ready\n");#endifreturn sp_fd;}void sp_send_packet (struct sp_data_exchange *sp, unsigned char proto){int sp_status;struct sockaddr_in sp_server;struct hostent *sp_help;int HEAD_BASE;/* Construction of destination */bzero((char *)&sp_server, sizeof(struct sockaddr)); sp_server.sin_family = AF_INET;sp_server.sin_addr.s_addr = inet_addr(sp->dest); if (sp_server.sin_addr.s_addr == (unsigned int)-1) { /* if target not in DOT/number notation */ if (!(sp_help=gethostbyname(sp->dest))) fprintf(stderr,"unknown host %s\n", sp->dest), exit(1); bcopy(sp_help->h_addr, (caddr_t)&sp_server.sin_addr, sp_help->h_length); };switch(proto) { case 6: HEAD_BASE = TCP_HEAD_BASE; break; /* TCP */ case 17: HEAD_BASE = UDP_HEAD_BASE; break; /* UDP */ default: exit(1); break; };sp_status = sendto(sp->fd, (char *)(sp->buffer), sp->datalen+HEAD_BASE+IP_HEAD_BASE+sp->IP_optlen, 0, (struct sockaddr *)&sp_server,sizeof(struct sockaddr)); if (sp_status < 0 || sp_status != sp->datalen+HEAD_BASE+IP_HEAD_BASE+sp->IP_optlen) { if (sp_status < 0) perror("Sendto"), exit(1); printf("hmm... Only transmitted %d of %d bytes.\n", sp_status, sp->datalen+HEAD_BASE); };#ifdef DEBUG printf("Packet transmitted...\n");#endif}void sp_fix_IP_packet (struct sp_data_exchange *sp, unsigned char proto){ struct IP_header *sp_help_ip;int HEAD_BASE;switch(proto) { case 6: HEAD_BASE = TCP_HEAD_BASE; break; /* TCP */ case 17: HEAD_BASE = UDP_HEAD_BASE; break; /* UDP */ default: exit(1); break; };sp_help_ip = (struct IP_header *) (sp->buffer);sp_help_ip->verlen = (IP_VERSION << 4) | ((IP_HEAD_BASE+sp->IP_optlen)/4);sp_help_ip->type = 0;sp_help_ip->length = htons(IP_HEAD_BASE+HEAD_BASE+sp->datalen+sp->IP_optlen+sp->TCP_optlen);sp_help_ip->ID = htons(12545); /* TEST */ sp_help_ip->flag_offset = 0;sp_help_ip->TTL = 69;sp_help_ip->protocol = proto;sp_help_ip->source = sp_getaddrbyname(sp->source);sp_help_ip->destination = sp_getaddrbyname(sp->dest);sp_help_ip->checksum=in_cksum((unsigned short *) (sp->buffer), IP_HEAD_BASE+sp->IP_optlen);#ifdef DEBUG printf("IP header fixed...\n");#endif}void sp_fix_TCP_packet (struct sp_data_exchange *sp){ char sp_pseudo_ip_construct[MTU];struct TCP_header *sp_help_tcp;struct pseudo_IP_header *sp_help_pseudo;int i;for(i=0;i<MTU;i++) {sp_pseudo_ip_construct[i]=0;}sp_help_tcp = (struct TCP_header *) (sp->buffer+IP_HEAD_BASE+sp->IP_optlen);sp_help_pseudo = (struct pseudo_IP_header *) sp_pseudo_ip_construct;sp_help_tcp->offset_flag = htons( (((TCP_HEAD_BASE+sp->TCP_optlen)/4)<<12) | sp->flags); sp_help_tcp->seq_nr = htonl(sp->seq);sp_help_tcp->ACK_nr = htonl(sp->ack);sp_help_tcp->source = htons(sp->source_port);sp_help_tcp->destination = htons(sp->dest_port);sp_help_tcp->window = htons(0x7c00); /* dummy for now 'wujx' */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 = 6;sp_help_pseudo->TCP_UDP_len = htons(sp->datalen+TCP_HEAD_BASE+sp->TCP_optlen);memcpy(sp_pseudo_ip_construct+12, sp_help_tcp, sp->TCP_optlen+sp->datalen+TCP_HEAD_BASE);sp_help_tcp->checksum=in_cksum((unsigned short *) sp_pseudo_ip_construct, sp->datalen+12+TCP_HEAD_BASE+sp->TCP_optlen);#ifdef DEBUG printf("TCP header fixed...\n");#endif}void transmit_TCP (int sp_fd, char *sp_data, int sp_ipoptlen, int sp_tcpoptlen, int sp_datalen, char *sp_source, unsigned short sp_source_port, char *sp_dest, unsigned short sp_dest_port, unsigned long sp_seq, unsigned long sp_ack, unsigned short sp_flags){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_tcpoptlen!=0) memcpy(sp_buffer+IP_HEAD_BASE+TCP_HEAD_BASE+sp_ipoptlen,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -