⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 406.htm

📁 unix高级编程原吗
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>apue</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center">               ● UNIX网络编程                       (BM: clown)                </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p   align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="316.htm">上一层</a>][<a href="407.htm">下一篇</a>]
<hr><p align="left"><small>发信人: biff (大可), 信区: Security <br>

标  题: [转载] newscan <br>

发信站: 武汉白云黄鹤站 (Wed Jun  2 17:24:01 1999), 站内信件 <br>

  <br>

【 以下文字转载自 Winsock 讨论区 】 <br>

【 原文由 biff 所发表 】 <br>

/* <br>

 * Half open port scanner. Send SYNs, and look for a SYN-ACK. If you see one, <br>

 * the port is listening. <br>

 * <br>

 * The whole point is to evade TCP-wrapper software and other alarm systems <br>

 * which look for a fully established connection. Since this never establishes <br>

 * a connection, its existance isn't logged. <br>

 * <br>

 * Note, this can be detected by things like TCPdump, or other raw network <br>

 * monitors. <br>

 * <br>

 * Runs under SunOS 4.x with NIT. This is a proof-of-concept toy, not a <br>

 * production model. :-) <br>

 * <br>

 * Mike Neuman <br>

 * 12/7/93 <br>

 * <br>

 * <br>

 * You can do whatever you want with this code as long as you leave this <br>

 * header intact. <br>

 * <br>

 * Contact information: <br>

 * <br>

 * Mike Neuman (mcn@EnGarde.com) <br>

 */ <br>

  <br>

#include <stdio.h> <br>

#include <netdb.h> <br>

#include <ctype.h> <br>

#include <signal.h> <br>

#include <errno.h> <br>

#include <malloc.h> <br>

#include <setjmp.h> <br>

#include <sys/types.h> <br>

#include <sys/time.h> <br>

#include <sys/timeb.h> <br>

#include <sys/socket.h> <br>

#include <sys/file.h> <br>

#include <sys/ioctl.h> <br>

#include <net/nit.h> <br>



#include <sys/fcntlcom.h> <br>

  <br>

#include <sys/param.h> <br>

#include <sys/dir.h> <br>

#include <net/nit_if.h> <br>

#include <net/nit_pf.h> <br>

#include <net/nit_buf.h> <br>

#include <net/packetfilt.h> <br>

#include <sys/stropts.h> <br>

  <br>

#include <net/if.h> <br>

#include <netinet/in.h> <br>

#include <netinet/in_systm.h> <br>

#include <netinet/ip.h> <br>

#include <netinet/if_ether.h> <br>

#include <netinet/ip_var.h> <br>

#include <netinet/udp.h> <br>

#include <netinet/udp_var.h> <br>

#include <netinet/tcp.h> <br>

#include <netinet/tcpip.h> <br>

#include <sys/stream.h> <br>

#include <sys/tihdr.h> <br>



  <br>

#include <rpc/rpc.h> <br>

#include <rpc/pmap_prot.h> <br>

#include <rpc/pmap_clnt.h> <br>

  <br>

/* RPC makes an rpcdump call, which may be logged by a secure portmapper. <br>

 * If you're paranoid and want to guess at rpc stuff, specify -r <br>

 */ <br>

  <br>

main(argc,argv) <br>

int argc; <br>

char *argv[]; <br>

  <br>

{ <br>

  struct sockaddr_in server; <br>

  struct servent *sp; <br>

  struct hostent *hp; <br>

  int c, s, count, userpc=1, sock=RPC_ANYSOCK, minport = 1, maxport = 6001; <br>

  struct pmaplist *head = NULL, *headp=NULL; <br>

  struct timeval timeout; <br>

  register CLIENT *client; <br>

  struct rpcent *rpc; <br>



  unsigned long addr; <br>

  char *hostp; <br>

  extern char *optarg; <br>

  extern int optind; <br>

  <br>

  <br>

  /* 1: Parse options, get host addr */ <br>

  while ((c = getopt(argc, argv, "rhm:M:")) != -1) <br>

    switch(c) { <br>

      case 'r': <br>

        userpc = 0; <br>

        break; <br>

      case 'm': <br>

        minport = atoi(optarg); <br>

        break; <br>

      case 'M': <br>

        maxport = atoi(optarg); <br>

        break; <br>

      case 'h': <br>

        usage(argv[0]); <br>

        exit(1); <br>

    } <br>

    } <br>

  <br>

  if (optind != argc-1) { <br>

    usage(argv[0]); <br>

    exit(1); <br>

  } <br>

  <br>

  if (minport > maxport || minport <= 0) { <br>

    fprintf(stderr, "ERROR: minport must be > 0 and <= maxport\n"); <br>

    usage(argv[0]); <br>

    exit(1); <br>

  } <br>

  hostp = argv[optind]; <br>

  <br>

  if ((hp=gethostbyname(hostp))==NULL) { <br>

    addr = inet_addr(hostp); <br>

    if (addr == (u_long)-1) { <br>

      fprintf(stderr, "Unknown host '%s'\n", hostp); <br>

      usage(argv[0]); <br>

      exit(1); <br>

    } <br>

    bcopy(&server.sin_addr, &addr, 4); <br>

  } else { <br>

  } else { <br>

    bzero((char *)&server, sizeof server); <br>

    bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length); <br>

    server.sin_family=hp->h_addrtype; <br>

  } <br>

  <br>

  printf("Connections established for host %s:\n",hostp); <br>

  <br>

  /* 2: Call rpc and get its ports */ <br>

  head=NULL; <br>

  if (userpc) { <br>

    server.sin_port=htons(PMAPPORT); <br>

    timeout.tv_sec=10; <br>

    timeout.tv_usec=0; <br>

    if ((client = clnttcp_create(&server, PMAPPROG, <br>

                                 PMAPVERS, &sock, 50, 500))!=NULL) { <br>

      if (clnt_call(client, PMAPPROC_DUMP, xdr_void, NULL, <br>

                    xdr_pmaplist, &head, timeout) != RPC_SUCCESS) <br>

        head=NULL; <br>

    } <br>

  } <br>

  <br>

  /* 3: Try each port from minport - maxport */ <br>



  for (count=minport;count<=maxport;count++) { <br>

    printf("\r%6d",count); <br>

    fflush(stdout); <br>

    server.sin_port=count; <br>

    if (!fakeconnect(&server, hostp)) { <br>

      printf("\r   %4d tcp   ",count); <br>

      sp=getservbyport(count,"tcp"); <br>

      if (sp==NULL) { <br>

        switch(count) { <br>

          /* Put in known services not in /etc/services here */ <br>

          case 2000: printf("(Xnews)"); <br>

            break; <br>

          case 6000: printf("(X)"); <br>

            break; <br>

          default: <br>

            if (userpc) { <br>

              headp=head; <br>

              while (headp!=NULL) { <br>

                if (headp->pml_map.pm_prot!=IPPROTO_TCP) { <br>

                  headp=headp->pml_next; <br>

                  continue; <br>

                } <br>



                if (count!=headp->pml_map.pm_port) { <br>

                  headp=headp->pml_next; <br>

                  continue; <br>

                } <br>

                rpc = getrpcbynumber(headp->pml_map.pm_prog); <br>

                if (rpc) { <br>

                  printf("%-15s [rpc]", rpc->r_name); <br>

                  break; <br>

                } else { <br>

                  headp=headp->pml_next; <br>

                  continue; <br>

                } <br>

              } <br>

              if (rpc==NULL) <br>

                printf("                [rpc]"); <br>

            } else <br>

              printf("???\n"); <br>

            break; <br>

        } <br>

      } else <br>

        printf("%-15s",sp->s_name); <br>

      printf("\n"); <br>



    } <br>

  } <br>

  printf("\n"); <br>

  exit(0); <br>

} <br>

  <br>

usage(prog) <br>

char *prog; <br>

{ <br>

  fprintf(stderr,"Usage: %s [-r] [-m minport] [-M maxport] host\n", prog); <br>

  fprintf(stderr,"Options:\n"); <br>

  fprintf(stderr,"-r       Don't use RPC for port info\n"); <br>

  fprintf(stderr,"-m port  Specify port number to start at (default 1)\n"); <br>

  fprintf(stderr,"-M port  Specify port number to end at   (default 6001)\n"); <br>

  return; <br>

} <br>

  <br>

  <br>

int scan_return(fd, iph, tcph) <br>

int fd; <br>

struct ip *iph; <br>

struct tcphdr *tcph; <br>



  <br>

{ <br>

  char buf[8*1024], *bp, *bufstop, *cp, abuf[2048]; <br>

  int cc, done=0, retval=0; <br>

  struct nit_bufhdr *hdrp; <br>

  struct nit_iftime *ntp; <br>

  struct nit_ifdrops *ndp; <br>

  struct nit_iflen *nlp; <br>

  struct ether_header *ep; <br>

  u_short et; <br>

  register struct ip *ip; <br>

  struct tcphdr *tp; <br>

  <br>

  while (!done) { <br>

    if ((cc=read(fd, buf, 8*1024)) >= 0) { <br>

      bp=buf; <br>

      bufstop = buf+cc; <br>

      while (bp < bufstop) { <br>

        cp = bp; <br>

  <br>

        /* Get past NIT buffer */ <br>

        hdrp = (struct nit_bufhdr *)cp; <br>



        cp += sizeof(*hdrp); <br>

  <br>

        /* get past NIT timer   */ <br>

        ntp = (struct nit_iftime *)cp; <br>

        cp += sizeof(*ntp); <br>

  <br>

        /* get past the drops */ <br>

        ndp = (struct nit_ifdrops *)cp; <br>

        cp += sizeof(*ndp); <br>

  <br>

        /* get past packet len  */ <br>

        nlp = (struct nit_iflen *)cp; <br>

        cp += sizeof(*nlp); <br>

  <br>

        /* next snapshot        */ <br>

        bp += hdrp->nhb_totlen; <br>

  <br>

        /* Okay, got the packet, make sure it's a SYN-ACK reply */ <br>

        ep = (struct ether_header *)cp; <br>

        et = ntohs(ep->ether_type); <br>

        if ( et >= ETHERTYPE_TRAIL && et < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) <br>

          continue; <br>



        cp += sizeof(struct ether_header); <br>

        nlp->nh_pktlen -= sizeof(struct ether_header); <br>

        if (et != ETHERTYPE_IP) continue; <br>

  <br>

        /* It's: ethernet + IP */ <br>

  <br>

        ip=(struct ip *)cp; <br>

        bcopy((char *)ip, (char *)abuf, nlp->nh_pktlen); /* Align */ <br>

        ip = (struct ip *)abuf; <br>

  <br>

        if (ip->ip_p != IPPROTO_TCP) <br>

          continue; <br>

  <br>

        /* It's ethernet + IP + TCP */ <br>

        if (ip->ip_src.s_addr != iph->ip_dst.s_addr) <br>

          continue; <br>

        if (ip->ip_dst.s_addr != iph->ip_src.s_addr) <br>

          continue; <br>

  <br>

        /* It's ethernet + IP + TCP + from target + to us */ <br>

        cp+=sizeof(struct ip); <br>

        tp = (struct tcphdr *)cp; <br>



        if (tp->th_sport != tcph->th_dport) <br>

          continue; <br>

        if (tp->th_dport != tcph->th_sport) <br>

          continue; <br>

  <br>

        /* It's ethernet + IP + TCP + from target + to us + ports right */ <br>

        if ((tp->th_flags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) { <br>

          retval=0; <br>

          done=1; <br>

        } else { <br>

          retval=1; <br>

          done=1; <br>

        } <br>

        break; <br>

      } /* While (bp<bufstop) */ <br>

    } /* if read */ <br>

  } /* while !done */ <br>

  return(retval); <br>

} <br>

  <br>

jmp_buf env_buf; <br>

  <br>

  <br>

/* Signal handler for receive timeout */ <br>

handle_alarm() <br>

{ <br>

  alarm(0); /* Clear the alarm */ <br>

  longjmp(env_buf, 1); <br>

} <br>

  <br>

/* <br>

 * Sends a packet, and waits for the reply <br>

 */ <br>

int send_packet(fd, server, servername, localnet, netmask) <br>

int fd; <br>

struct sockaddr_in *server; <br>

char *servername; <br>

u_long localnet; <br>

u_long netmask; <br>

  <br>

{ <br>

  static struct sockaddr sa; <br>

  static struct strbuf ctl, datah; <br>

  static struct ip iph; <br>

  static char buf[2048], pbuf[2048], *oldbp; <br>



  static int computed=0; <br>

  <br>

  struct ether_header eh; <br>

  struct tcphdr tcph; <br>

  char *bp, *pbp; <br>

  struct ether_addr myaddr, faddr; <br>

  FILE *fp; <br>

  struct hostent *he; <br>

  u_long x; <br>

  int retval; <br>

  <br>

  if (!computed) { <br>

    /* ARP for the router/host, if possible */ <br>

    int s; <br>

    struct arpreq arpreq; <br>

    struct sockaddr_in *s_arp; <br>

  <br>

    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { <br>

      perror("Socket"); <br>

      exit(1); <br>

    } <br>

  <br>

  <br>

    arpreq.arp_pa.sa_family = AF_INET; <br>

    s_arp = (struct sockaddr_in *)&arpreq.arp_pa; <br>

    s_arp->sin_family = AF_INET; <br>

    s_arp->sin_addr.s_addr = server->sin_addr.s_addr; <br>

  <br>

    /* First, send junk to it to get an ARP entry */ <br>

    s_arp->sin_port = 9; <br>

    sendto(s, "blah", 4, 0, (struct sockaddr *)s_arp, <br>

           sizeof(struct sockaddr_in)); <br>

    s_arp = (struct sockaddr_in *)&arpreq.arp_ha; <br>

    s_arp->sin_family = AF_UNSPEC; <br>

  <br>

    if (ioctl(s, SIOCGARP, &arpreq) < 0) { <br>

      perror("ioctl"); <br>

      close(s); <br>

      exit(1); <br>

    } <br>

  <br>

    if (arpreq.arp_flags & ATF_COM) { <br>

      bcopy(arpreq.arp_ha.sa_data, &faddr, 6); <br>

    } else { <br>

      fprintf(stderr, "Can't find destination/router hardware address.\n"); <br>



      exit(1); <br>

    } <br>

    /* faddr and myaddr are now computed, create the ether header */ <br>

    bcopy(&faddr, &eh.ether_dhost, 6); <br>

    eh.ether_type = htons(ETHERTYPE_IP); <br>

    sa.sa_family = AF_UNSPEC; <br>

    bcopy((char *)&eh, (char *)sa.sa_data, sizeof(struct ether_header)); <br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -