source: npl/overig/netcat_openbsd/patches/0011-misc-failures-and-features.patch @ a9c55d3

perl-5.22
Last change on this file since a9c55d3 was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 12.6 KB
  • nc.1

    From: Aron Xu <aron@debian.org>
    Date: Mon, 13 Feb 2012 19:06:52 +0800
    Subject: misc connection failures
    
    ---
     nc.1     |   76 ++++++++++++++++++++++++++++++++++++---
     netcat.c |  119 ++++++++++++++++++++++++++++++++++++++++++--------------------
     2 files changed, 153 insertions(+), 42 deletions(-)
    
    diff --git a/nc.1 b/nc.1
    index 60e3668..477cb1b 100644
    a b  
    3434.Sh SYNOPSIS
    3535.Nm nc
    3636.Bk -words
    37 .Op Fl 46CDdhklnrStUuvZz
     37.Op Fl 46bCDdhklnrStUuvZz
    3838.Op Fl I Ar length
    3939.Op Fl i Ar interval
    4040.Op Fl O Ar length
    to use IPv4 addresses only. 
    9999Forces
    100100.Nm
    101101to use IPv6 addresses only.
     102.It Fl b
     103Allow broadcast.
    102104.It Fl C
    103105Send CRLF as line-ending.
    104106.It Fl D
    and which side is being used as a 
    323325The connection may be terminated using an
    324326.Dv EOF
    325327.Pq Sq ^D .
     328.Pp
     329There is no
     330.Fl c
     331or
     332.Fl e
     333option in this netcat, but you still can execute a command after connection
     334being established by redirecting file descriptors. Be cautious here because
     335opening a port and let anyone connected execute arbitrary command on your
     336site is DANGEROUS. If you really need to do this, here is an example:
     337.Pp
     338On
     339.Sq server
     340side:
     341.Pp
     342.Dl $ rm -f /tmp/f; mkfifo /tmp/f
     343.Dl $ cat /tmp/f | /bin/sh -i 2>&1 | nc -l 127.0.0.1 1234 > /tmp/f
     344.Pp
     345On
     346.Sq client
     347side:
     348.Pp
     349.Dl $ nc host.example.com 1234
     350.Dl $ (shell prompt from host.example.com)
     351.Pp
     352By doing this, you create a fifo at /tmp/f and make nc listen at port 1234
     353of address 127.0.0.1 on
     354.Sq server
     355side, when a
     356.Sq client
     357establishes a connection successfully to that port, /bin/sh gets executed
     358on
     359.Sq server
     360side and the shell prompt is given to
     361.Sq client
     362side.
     363.Pp
     364When connection is terminated,
     365.Nm
     366quits as well. Use
     367.Fl k
     368if you want it keep listening, but if the command quits this option won't
     369restart it or keep
     370.Nm
     371running. Also don't forget to remove the file descriptor once you don't need
     372it anymore:
     373.Pp
     374.Dl $ rm -f /tmp/f
     375.Pp
    326376.Sh DATA TRANSFER
    327377The example in the previous section can be expanded to build a
    328378basic data transfer model.
    The 
    382432flag can be used to tell
    383433.Nm
    384434to report open ports,
    385 rather than initiate a connection.
     435rather than initiate a connection. Usually it's useful to turn on verbose
     436output to stderr by use this option in conjunction with
     437.Fl v
     438option.
     439.Pp
    386440For example:
    387441.Bd -literal -offset indent
    388 $ nc -z host.example.com 20-30
     442$ nc \-zv host.example.com 20-30
    389443Connection to host.example.com 22 port [tcp/ssh] succeeded!
    390444Connection to host.example.com 25 port [tcp/smtp] succeeded!
    391445.Ed
    392446.Pp
    393 The port range was specified to limit the search to ports 20 \- 30.
     447The port range was specified to limit the search to ports 20 \- 30, and is
     448scanned by increasing order.
     449.Pp
     450You can also specify a list of ports to scan, for example:
     451.Bd -literal -offset indent
     452$ nc \-zv host.example.com 80 20 22
     453nc: connect to host.example.com 80 (tcp) failed: Connection refused
     454nc: connect to host.example.com 20 (tcp) failed: Connection refused
     455Connection to host.example.com port [tcp/ssh] succeeded!
     456.Ed
     457.Pp
     458The ports are scanned by the order you given.
    394459.Pp
    395460Alternatively, it might be useful to know which server software
    396461is running, and which versions.
    Original implementation by *Hobbit* 
    455520.br
    456521Rewritten with IPv6 support by
    457522.An Eric Jackson Aq ericj@monkey.org .
     523.br
     524Modified for Debian port by Aron Xu
     525.Aq aron@debian.org .
    458526.Sh CAVEATS
    459527UDP port scans using the
    460528.Fl uz
  • netcat.c

    diff --git a/netcat.c b/netcat.c
    index bf9940f..c938d11 100644
    a b  
    8888#include <netdb.h>
    8989#include <poll.h>
    9090#include <signal.h>
     91#include <stddef.h>
    9192#include <stdarg.h>
    9293#include <stdio.h>
    9394#include <stdlib.h>
     
    115116#define UDP_SCAN_TIMEOUT 3                      /* Seconds */
    116117
    117118/* Command Line Options */
     119int     bflag;                                  /* Allow Broadcast */
    118120int     Cflag = 0;                              /* CRLF line-ending */
    119121int     dflag;                                  /* detached, no stdin */
    120122unsigned int iflag;                             /* Interval Flag */
    char *portlist[PORT_MAX+1]; 
    146148char *unix_dg_tmp_socket;
    147149
    148150void    atelnet(int, unsigned char *, unsigned int);
    149 void    build_ports(char *);
     151void    build_ports(char **);
    150152void    help(void);
    151153int     local_listen(char *, char *, struct addrinfo);
    152154void    readwrite(int);
    int 
    171173main(int argc, char *argv[])
    172174{
    173175        int ch, s, ret, socksv;
    174         char *host, *uport;
     176        char *host, **uport;
    175177        struct addrinfo hints;
    176178        struct servent *sv;
    177179        socklen_t len;
    178         struct sockaddr_storage cliaddr;
     180        union {
     181                struct sockaddr_storage storage;
     182                struct sockaddr_un forunix;
     183        } cliaddr;
    179184        char *proxy = NULL;
    180185        const char *errstr, *proxyhost = "", *proxyport = NULL;
    181186        struct addrinfo proxyhints;
    main(int argc, char *argv[]) 
    189194        sv = NULL;
    190195
    191196        while ((ch = getopt(argc, argv,
    192             "46CDdhI:i:jklnO:P:p:q:rSs:tT:UuV:vw:X:x:Zz")) != -1) {
     197            "46bCDdhI:i:jklnO:P:p:q:rSs:tT:UuV:vw:X:x:Zz")) != -1) {
    193198                switch (ch) {
    194199                case '4':
    195200                        family = AF_INET;
    main(int argc, char *argv[]) 
    197202                case '6':
    198203                        family = AF_INET6;
    199204                        break;
     205                case 'b':
     206# if defined(SO_BROADCAST)
     207                        bflag = 1;
     208# else
     209                        errx(1, "no broadcast frame support available");
     210# endif
     211                        break;
    200212                case 'U':
    201213                        family = AF_UNIX;
    202214                        break;
    main(int argc, char *argv[]) 
    342354
    343355        /* Cruft to make sure options are clean, and used properly. */
    344356        if (argv[0] && !argv[1] && family == AF_UNIX) {
    345                 if (uflag)
    346                         errx(1, "cannot use -u and -U");
    347357# if defined(IPPROTO_DCCP) && defined(SOCK_DCCP)
    348358                if (dccpflag)
    349359                        errx(1, "cannot use -Z and -U");
    350360# endif
    351361                host = argv[0];
    352362                uport = NULL;
    353         } else if (!argv[0] && lflag) {
    354                 if (sflag)
    355                         errx(1, "cannot use -s and -l");
    356                 if (zflag)
    357                         errx(1, "cannot use -z and -l");
    358                 if (pflag)
    359                         uport=pflag;
    360         } else if (!lflag && kflag) {
    361                 errx(1, "cannot use -k without -l");
    362         } else if (argv[0] && !argv[1]) {
    363                 if  (!lflag)
    364                         usage(1);
    365                 uport = argv[0];
     363        } else if (argv[0] && !argv[1] && lflag) {
     364                if (pflag) {
     365                        uport = &pflag;
     366                        host = argv[0];
     367                } else {
     368                        uport = argv;
     369                        host = NULL;
     370                }
     371        } else if (!argv[0] && lflag && pflag) {
     372                uport = &pflag;
    366373                host = NULL;
    367374        } else if (argv[0] && argv[1]) {
    368375                host = argv[0];
    369                 uport = argv[1];
     376                uport = &argv[1];
    370377        } else
    371378                usage(1);
    372379
    373 
     380        if (lflag) {
     381                if (sflag)
     382                        errx(1, "cannot use -s and -l");
     383                if (zflag)
     384                        errx(1, "cannot use -z and -l");
     385                if (pflag)
     386                        /* This still does not work well because of getopt mess
     387                        errx(1, "cannot use -p and -l"); */
     388                        uport = &pflag;
     389        } else if (!lflag && kflag)
     390                errx(1, "cannot use -k without -l");
    374391
    375392        /* Get name of temporary socket for unix datagram client */
    376393        if ((family == AF_UNIX) && uflag && !lflag) {
    main(int argc, char *argv[]) 
    448465                        else
    449466                                s = unix_listen(host);
    450467                } else
    451                         s = local_listen(host, uport, hints);
     468                        s = local_listen(host, *uport, hints);
    452469                if (s < 0)
    453470                        err(1, NULL);
    454471
    main(int argc, char *argv[]) 
    457474                        local = ":::";
    458475                else
    459476                        local = "0.0.0.0";
    460                 fprintf(stderr, "Listening on [%s] (family %d, port %d)\n",
     477                if (vflag && (family != AF_UNIX))
     478                fprintf(stderr, "Listening on [%s] (family %d, port %s)\n",
    461479                        host ?: local,
    462480                        family,
    463481                        *uport);
    main(int argc, char *argv[]) 
    490508                                len = sizeof(cliaddr);
    491509                                connfd = accept(s, (struct sockaddr *)&cliaddr,
    492510                                    &len);
    493                                 if(vflag) {
     511                                if(vflag && family == AF_UNIX) {
     512                                        fprintf(stderr, "Connection from \"%.*s\" accepted\n",
     513                                                (len - (int)offsetof(struct sockaddr_un, sun_path)),
     514                                                ((struct sockaddr_un*)&cliaddr)->sun_path);
     515                                } else if(vflag) {
    494516                                        char *proto = proto_name(uflag, dccpflag);
    495517                                /* Don't look up port if -n. */
    496518                                        if (nflag)
    497519                                                sv = NULL;
    498520                                        else
    499                                                 sv = getservbyport(ntohs(atoi(uport)),
     521                                                sv = getservbyport(ntohs(atoi(*uport)),
    500522                                                        proto);
    501523
    502524                                        if (((struct sockaddr *)&cliaddr)->sa_family == AF_INET) {
    main(int argc, char *argv[]) 
    504526                                                inet_ntop(((struct sockaddr *)&cliaddr)->sa_family,&(((struct sockaddr_in *)&cliaddr)->sin_addr),dst,INET_ADDRSTRLEN);
    505527                                                fprintf(stderr, "Connection from [%s] port %s [%s/%s] accepted (family %d, sport %d)\n",
    506528                                                        dst,
    507                                                         uport,
     529                                                        *uport,
    508530                                                        proto,
    509531                                                        sv ? sv->s_name : "*",
    510532                                                        ((struct sockaddr *)(&cliaddr))->sa_family,
    main(int argc, char *argv[]) 
    515537                                                inet_ntop(((struct sockaddr *)&cliaddr)->sa_family,&(((struct sockaddr_in6 *)&cliaddr)->sin6_addr),dst,INET6_ADDRSTRLEN);
    516538                                                fprintf(stderr, "Connection from [%s] port %s [%s/%s] accepted (family %d, sport %d)\n",
    517539                                                        dst,
    518                                                         uport,
     540                                                        *uport,
    519541                                                        proto,
    520542                                                        sv ? sv->s_name : "*",
    521543                                                        ((struct sockaddr *)&cliaddr)->sa_family,
    main(int argc, char *argv[]) 
    523545                                        }
    524546                                        else {
    525547                                                fprintf(stderr, "Connection from unknown port %s [%s/%s] accepted (family %d, sport %d)\n",
    526                                                         uport,
     548                                                        *uport,
    527549                                                        proto,
    528550                                                        sv ? sv->s_name : "*",
    529551                                                        ((struct sockaddr *)(&cliaddr))->sa_family,
    530552                                                        ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
    531553                                        }
    532554                                }
     555                                if(!kflag)
     556                                        close(s);
    533557                                readwrite(connfd);
    534558                                close(connfd);
    535559                        }
    536560
     561                        if (vflag && kflag)
     562                                fprintf(stderr, "Connection closed, listening again.\n");
    537563                        if (kflag)
    538564                                continue;
    539565                        if (family != AF_UNIX) {
    unix_bind(char *path) 
    641667                return (-1);
    642668        }
    643669
     670        unlink(path);
     671
    644672        if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
    645673                close(s);
    646674                return (-1);
    unix_connect(char *path) 
    662690                if ((s = unix_bind(unix_dg_tmp_socket)) < 0)
    663691                        return (-1);
    664692        } else {
    665                 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
     693                if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
     694                        errx(1,"create unix socket failed");
    666695                        return (-1);
     696                }
    667697        }
    668698        (void)fcntl(s, F_SETFD, 1);
    669699
    unix_connect(char *path) 
    674704            sizeof(sun.sun_path)) {
    675705                close(s);
    676706                errno = ENAMETOOLONG;
     707                warn("unix connect abandoned");
    677708                return (-1);
    678709        }
    679710        if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
     711                warn("unix connect failed");
    680712                close(s);
    681713                return (-1);
    682714        }
    atelnet(int nfd, unsigned char *buf, unsigned int size) 
    11051137 * that we should try to connect to.
    11061138 */
    11071139void
    1108 build_ports(char *p)
     1140build_ports(char **p)
    11091141{
    11101142        struct servent *sv;
    11111143        const char *errstr;
    11121144        char *n;
    11131145        int hi, lo, cp;
    11141146        int x = 0;
     1147        int i;
    11151148
    11161149        char *proto = proto_name(uflag, dccpflag);
    1117         sv = getservbyname(p, proto);
     1150        sv = getservbyname(*p, proto);
    11181151        if (sv) {
    11191152                portlist[0] = calloc(1, PORT_MAX_LEN);
    11201153                if (portlist[0] == NULL)
    11211154                        err(1, NULL);
    11221155                snprintf(portlist[0], PORT_MAX_LEN, "%d", ntohs(sv->s_port));
    1123         } else if ((n = strchr(p, '-')) != NULL) {
     1156        } else if ((n = strchr(*p, '-')) != NULL) {
    11241157                *n = '\0';
    11251158                n++;
    11261159
    build_ports(char *p) 
    11281161                hi = strtonum(n, 1, PORT_MAX, &errstr);
    11291162                if (errstr)
    11301163                        errx(1, "port number %s: %s", errstr, n);
    1131                 lo = strtonum(p, 1, PORT_MAX, &errstr);
     1164                lo = strtonum(*p, 1, PORT_MAX, &errstr);
    11321165                if (errstr)
    1133                         errx(1, "port number %s: %s", errstr, p);
     1166                        errx(1, "port number %s: %s", errstr, *p);
    11341167
    11351168                if (lo > hi) {
    11361169                        cp = hi;
    build_ports(char *p) 
    11601193                        }
    11611194                }
    11621195        } else {
    1163                 hi = strtonum(p, 1, PORT_MAX, &errstr);
     1196                hi = strtonum(*p, 1, PORT_MAX, &errstr);
    11641197                if (errstr)
    1165                         errx(1, "port number %s: %s", errstr, p);
    1166                 portlist[0] = strdup(p);
     1198                        errx(1, "port number %s: %s", errstr, *p);
     1199                for (i=0;p[i];i++) {
     1200                        portlist[i] = strdup(p[i]);
     1201                }
    11671202                if (portlist[0] == NULL)
    11681203                        err(1, NULL);
    11691204        }
    set_common_sockopts(int s) 
    11981233{
    11991234        int x = 1;
    12001235
     1236# if defined(SO_BROADCAST)
     1237        if (bflag) {
     1238                if (setsockopt(s, IPPROTO_TCP, SO_BROADCAST,
     1239                        &x, sizeof(x)) == -1)
     1240                        err(1, NULL);
     1241        }
     1242# endif
    12011243# if defined(TCP_MD5SIG)
    12021244        if (Sflag) {
    12031245                if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
    help(void) 
    12931335        fprintf(stderr, "\tCommand Summary:\n\
    12941336        \t-4            Use IPv4\n\
    12951337        \t-6            Use IPv6\n\
     1338        \t-b            Allow broadcast\n\
    12961339        \t-C            Send CRLF as line-ending\n\
    12971340        \t-D            Enable the debug socket option\n\
    12981341        \t-d            Detach from stdin\n\
    void 
    13291372usage(int ret)
    13301373{
    13311374        fprintf(stderr,
    1332             "usage: nc [-46CDdhjklnrStUuvZz] [-I length] [-i interval] [-O length]\n"
     1375            "usage: nc [-46bCDdhjklnrStUuvZz] [-I length] [-i interval] [-O length]\n"
    13331376            "\t  [-P proxy_username] [-p source_port] [-q seconds] [-s source]\n"
    13341377            "\t  [-T toskeyword] [-V rtable] [-w timeout] [-X proxy_protocol]\n"
    13351378            "\t  [-x proxy_address[:port]] [destination] [port]\n");
Note: See TracBrowser for help on using the repository browser.