试验一下CSDN的插入代码功能

以下代码实现了简易的根据URL下载网页的功能,原始链接已经找不到了,不过代码头部有作者信息。
  1. /*
  2.    Wget V. 0.7 Download File Form Url
  3.    
  4.    by wzt    <[email]wzt@xsec.org[/email]>
  5.    
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <sys/socket.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <netinet/in.h>
  14. #include <netdb.h>
  15. #include <time.h>
  16. #define    MAXLINE    65535
  17. #define SENDDATA    1000
  18. #define PORT        80
  19. struct hostent *host;
  20. char *data_m="Accept: */*/r/n"
  21.         "Accept-Language: zh-cn/r/n"
  22.         "UA-CPU: x86/r/n"
  23.         "Accept-Encoding: gzip, deflate/r/n"
  24.         "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; TencentTraveler ; .NET CLR 1.1.4322)/r/n";
  25.        
  26.        
  27. char *data_e="Connection: Keep-Alive/r/n/r/n";
  28. typedef struct DWONLOAD_FILE{
  29.    char hosts[100];
  30.    char ports[200];
  31.    char file[200];
  32.    int port;
  33. }DOWN;
  34. void usage(char *pro);
  35. void ver();
  36. DOWN abstract_host(char *src);
  37. void download_file(char *url_t,char *local_file);
  38. int connect_server(int port);
  39. void usage(char *pro)
  40. {
  41.    ver();
  42.    printf("usage : %s <remote file> <local file>/n",pro);
  43.    printf("exp: %s [url]http://tthacker.sitesled.com/backdoor/bindtty.c[/url] bindtty.c/n",pro);
  44.    exit(0);
  45. }
  46. void ver()
  47. {
  48.    printf("/nWget Ver 0.6 by W.Z.T, [email]wzt@xsec.org[/email] 2006/10/27/n");
  49. }
  50. int connect_server(int port)
  51. {
  52.    struct sockaddr_in remote;
  53.    int sock_id;
  54.    
  55.    if((sock_id=socket(AF_INET,SOCK_STREAM,0))<0){
  56.        perror("sock_id");
  57.        exit(0);
  58.    }
  59.    
  60.    remote.sin_family=AF_INET;
  61.    remote.sin_port=htons(port);
  62.    remote.sin_addr=*((struct in_addr *)host->h_addr);
  63.    
  64.    if(connect(sock_id,(struct sockaddr *)&remote,sizeof(struct sockaddr))<0){
  65.        perror("connect");
  66.        return -1;
  67.    }
  68.    
  69.    printf("/n[+] Connect :%s:%d OK!/r/n",inet_ntoa(remote.sin_addr),ntohs(remote.sin_port));
  70.    
  71.    return sock_id;
  72. }
  73. void download_file(char *url_t,char *local_file)
  74. {
  75.    DOWN file_get;
  76.    FILE *fp;
  77.    time_t t_s,t_e;
  78.    char download_url[SENDDATA];
  79.    char recv_data[MAXLINE];
  80.    int sock_fd;
  81.    long get_file_len=0;
  82.    int r_len;
  83.    int http_check;
  84.    char *real_data_pos;
  85.        
  86.    ver();
  87.    file_get=abstract_host(url_t);
  88.    printf("/n[+] Host :%s/n",file_get.hosts);
  89.    printf("[+] Port :%d/n",file_get.port);
  90.    printf("[+] File :%s/n",file_get.file);
  91.    
  92.    
  93.    if((fp=fopen(local_file,"w+"))==NULL){
  94.        printf("/n[-] Create Local File %s Failed./n",local_file);
  95.        exit(1);
  96.    }
  97.    
  98.    if((host=gethostbyname(file_get.hosts))==NULL){
  99.        herror("gethostbyname");
  100.        exit(0);
  101.    }
  102. // 提交url
  103.    sprintf(download_url,"GET %s HTTP/1.1/r/n%sHost: %s/r/n%s",file_get.file,data_m,file_get.hosts,data_e);
  104.    
  105. //    printf("%s",download_url);
  106.    
  107.    sock_fd=connect_server(file_get.port);
  108.    
  109.    if(sock_fd==-1){
  110.        printf("connect error./n");
  111.        exit(1);
  112.    }
  113.    time(&t_s);
  114.    printf("/n[+] Starting Download at %s",ctime(&t_s)+4);
  115.        
  116. // 接受并处理第-批数据
  117.    
  118.    write(sock_fd,download_url,strlen(download_url));
  119.    r_len=read(sock_fd,recv_data,MAXLINE);
  120.    
  121. //    printf("%s",recv_data);
  122.    http_check=atoi(recv_data+9);
  123.    
  124. //    printf("%d/n",http_check);
  125.    if(http_check == 200 || http_check == 206){
  126. //        printf("/n[+] Download Begin .../n");
  127.    }
  128.    else{
  129.        printf("/n[-] Bad request./n");
  130.        exit(1);
  131.    }
  132. // 获得文件大小
  133.    
  134.    get_file_len=atoi(strstr(recv_data,"Content-Length: ")+16);
  135.    printf("/n[+] File Size: %ld/n",get_file_len);
  136. // 寻找真正的文件开始位置
  137.    
  138.    real_data_pos=strstr(recv_data,"/r/n/r/n")+4;
  139.    fputs(real_data_pos,fp);
  140. // 循环读取数据
  141.    
  142.    memset(recv_data,0,MAXLINE);
  143.    while((r_len=read(sock_fd,recv_data,MAXLINE))>0){    
  144.        fputs(recv_data,fp);
  145. //        printf("%d/n",r_len);
  146.        memset(recv_data,0,MAXLINE);
  147.    }
  148.    
  149.    fclose(fp);
  150.        
  151.    time(&t_e);
  152.    printf("/n[+] Download File OK at %s",ctime(&t_e)+4);    
  153.    
  154. }
  155. DOWN abstract_host(char src[])
  156. {
  157.    DOWN file_download;
  158.    char *http="http://";
  159.    int port;
  160.    int port_flag=0;
  161.    int i,j=0,k,l=0,h;
  162.    i=strlen(http);
  163.    for(;i<strlen(src);i++){
  164.        if(src[i]==':'){
  165.            port_flag=1;
  166.            h=i-1;
  167.            i++;
  168.            for(;src[i]!='/';i++){     /* 提取ports */
  169.                file_download.ports[j++]=src[i];
  170.            }
  171.            file_download.ports[j]='/0';
  172.        }
  173.        if(src[i]=='/'){
  174.            k=i-1;
  175.            break;
  176.        }
  177.    }
  178.    
  179. /    
  180.    for(;i<strlen(src);i++){     /* 提取file */
  181.        file_download.file[l++]=src[i];
  182.    }
  183.    file_download.file[l]='/0';
  184.    i=strlen(http);   /* 提取主机名 */
  185.    j=0;
  186.    k=((port_flag==1)?h:k);
  187.    
  188.    for(;i<=k;i++)
  189.        file_download.hosts[j++]=src[i];
  190.    file_download.hosts[j]='/0';
  191.    
  192.    if(port_flag==0)   /* 提取port */
  193.        file_download.port=PORT;    
  194.    else
  195.        file_download.port=atoi(file_download.ports);
  196.    
  197.     
  198.    
  199.    return file_download;
  200. }
  201. int main(int argc,char **argv)
  202. {
  203.    if(argc<3){
  204.        usage(argv[0]);
  205.    }
  206.    
  207.    download_file(argv[1],argv[2]);
  208.        
  209.    return 0;
  210. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值