zoukankan      html  css  js  c++  java
  • 用c写了个后台扫描

    /**
     * Notice: The program is not debug on internet and not use thread supervene.
     * date  : 6-26
     * author: jker 
     **/
    #include <sys/socket.h>
    #include <stdio.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdlib.h>
    #define LINE 1024
    #define dict "dict.txt"
    #define result "manager.txt"
     
    void itos(int num,char *str){
      int a=num;
      int x,y,z;
      x=a/100;
      y=a%100/10;
      z=a%10;
      if(x==0){
        if(y==0){
          str[0]+=z;
          str[1]='';
        }else{
          str[0]+=y;
          str[1]+=z;
          str[2]='';
        }
      }else{
        str[0]+=x;
        str[1]+=y;
        str[2]+=z;
        str[3]='';
      }
      return;
    }
    char *nip(char *oip){
      char *str[4]={""},*rtn;
      char *delim = ".";
      char * tmp;
      int i=0;
      char arr[4]={48,48,48,''};
      rtn = malloc(512);
      memset(rtn,'',512);
      tmp = strtok(oip,delim);
      str[i++]=tmp;
      while(tmp=strtok(NULL,delim)){
        str[i++]=tmp;
      }
     
      for(i=3;i>=0;i--){
        if(atoi(str[i])<255){
          itos(atoi(str[i])+1,arr);
          str[i]=arr;
          break;
        }
      }
     
      for(i=0;i<4;i++){
        strcat(rtn,str[i]);
        if(i<3)strcat(rtn,delim);
      }
     
      return rtn;
    }
    int main(int argc,char *argv[]){
      int sc,fd;
      unsigned short port = 80;
      struct sockaddr_in add;
      char *ip,*sip,*eip;
      struct hostent *host;
      char rheader[LINE],dictbuf[LINE],resbuf[LINE];
      char *dir,path[LINE]="",*arr[LINE],*tmp,state[3],manager[LINE];
      int i=0,n=0,bytes;
      if(argc != 3){
        printf("please input startIp and endIp.
    ");
        return -1;
      }
      dir = get_current_dir_name();
      strcat(path,dir);
      strcat(path,"/");
      strcat(path,dict);
      fd = open(path,O_RDONLY);
      read(fd,dictbuf,sizeof(dictbuf));
      close(fd);
      arr[n++]=strtok(dictbuf,"
    ");
      while(tmp=strtok(NULL,"
    "))arr[n++]=tmp;
     
      ip = malloc(512);
      memset(ip,'',512);
      sip = argv[1];
      eip = argv[2];
      strcpy(ip,sip);
     
      memset(manager,'',LINE);
      memset(rheader,'',LINE);
     
      sc = socket(AF_INET,SOCK_STREAM,0);
      if(sc == -1){
        printf("create socket fail!
    ");
        return -1;
      }
      printf("create socket ok.
    ");
      bzero(&add,sizeof(add));
      add.sin_family=AF_INET;
      add.sin_port=htons(port);
      bzero(&path,sizeof(path));
      strcat(path,dir);
      strcat(path,"/");
      strcat(path,result);
      fd = open(path,O_WRONLY|O_CREAT);
     
      do{
        host = gethostbyaddr(ip,strlen(ip),AF_INET);
        if(host == (struct hostent *)NULL) {
          printf("can`t get host from %s
    ",ip);
          if(strcmp(ip,eip)<0)ip=nip(ip);
          continue;
        }else{                                                                              
          printf("host name:%s
    ",host->h_name);
        }
     
        add.sin_addr.s_addr=inet_addr(ip);
        if(connect(sc,(struct sockaddr *)(&add),sizeof(struct sockaddr))==-1){
          printf("conn fail!
    ");
          return -1;
        }
        for(;i<n;i++){
          strcat(rheader,"HEAD ");
          strcat(rheader,arr[i]);
          strcat(rheader," HTTP/1.1");
          strcat(rheader,"
    ");
          strcat(rheader,"Host:");
          strcat(rheader,host->h_name);
          strcat(rheader,"
    ");
     
          //strcat(rheader,"Accept:*/*");
          //strcat(rheader,"
    ");
          strcat(rheader,"Connection:Keep-Alive");
          strcat(rheader,"
    ");
          write(sc, rheader, strlen(rheader));usleep(100);        
          bytes = read(sc, resbuf, LINE);        
          printf("Response from %s:
    ",ip);        
          write(STDOUT_FILENO, resbuf, bytes);
          for(i=0;i<3;i++){
            state[i]=resbuf[i+9];
          }
          if(!((state>="400" && state<="450")||state=="500")){
            strcat(manager,"http://");
            strcat(manager,host->h_name);
            strcat(manager,arr[i]);
            strcat(manager,"
    ");
       write(fd,manager,sizeof(manager));
          }
        } 
        close(sc);
        ip = nip(ip);
      }while(strcmp(ip,eip)<0);
     
      close(fd);
      return 0;
    }
  • 相关阅读:
    4-vim-工作模式-01-职责以及切换模式
    3-vim-打开和新建文件-02-删除交换文件
    poj1011Stick(dfs+剪枝)
    POJ 1251 Jungle Roads (prim)
    poj 2502 Subway
    poj 3624 Charm Bracelet (01背包)
    拦截导弹问题(动态规划)
    Policy Gradient
    深入了解马尔科夫决策过程(Markov Decision Process)
    深度学习中调参对模型容量的影响
  • 原文地址:https://www.cnblogs.com/rinack/p/3184926.html
Copyright © 2011-2022 走看看