📄 netcat.c
字号:
to also be able to send data back, you need this connect() line, which also has the side effect that now anything from a different source or even a different port on the other end won't show up and will cause ICMP errors. I guess that's what they meant by "connect". Let's try to remember what the "U" is *really* for, eh? */ rr = connect (nnetfd, (SA *)remend, sizeof (SA)); goto whoisit; } /* o_udpmode *//* fall here for TCP */ x = sizeof (SA); /* retval for accept */ arm (2, o_wait); /* wrap this in a timer, too; 0 = forever */ if (setjmp (jbuf) == 0) { rr = accept (nnetfd, (SA *)remend, &x); } else goto dol_tmo; /* timeout */ arm (0, 0); close (nnetfd); /* dump the old socket */ nnetfd = rr; /* here's our new one */whoisit: if (rr < 0) goto dol_err; /* bail out if any errors so far *//* If we can, look for any IP options. Useful for testing the receiving end of such things, and is a good exercise in dealing with it. We do this before the connect message, to ensure that the connect msg is uniformly the LAST thing to emerge after all the intervening crud. Doesn't work for UDP on any machines I've tested, but feel free to surprise me. */#ifdef IP_OPTIONS if (! o_verbose) /* if we wont see it, we dont care */ goto dol_noop; optbuf = Hmalloc (40); x = 40; rr = getsockopt (nnetfd, IPPROTO_IP, IP_OPTIONS, optbuf, &x); if (rr < 0) holler ("getsockopt failed");Debug (("ipoptions ret len %d", x)) if (x) { /* we've got options, lessee em... */ unsigned char * q = (unsigned char *) optbuf; char * p = bigbuf_net; /* local variables, yuk! */ char * pp = &bigbuf_net[128]; /* get random space farther out... */ memset (bigbuf_net, 0, 256); /* clear it all first */ while (x > 0) { sprintf (pp, "%2.2x ", *q); /* clumsy, but works: turn into hex */ strcat (p, pp); /* and build the final string */ q++; p++; x--; } holler ("IP options: %s", bigbuf_net); } /* if x, i.e. any options */dol_noop:#endif /* IP_OPTIONS *//* find out what address the connection was *to* on our end, in case we're doing a listen-on-any on a multihomed machine. This allows one to offer different services via different alias addresses, such as the "virtual web site" hack. */ memset (bigbuf_net, 0, 64); cp = &bigbuf_net[32]; x = sizeof (SA); rr = getsockname (nnetfd, (SA *) lclend, &x); if (rr < 0) holler ("post-rcv getsockname failed"); strcpy (cp, inet_ntoa (lclend->sin_addr));/* now check out who it is. We don't care about mismatched DNS names here, but any ADDR and PORT we specified had better fucking well match the caller. Converting from addr to inet_ntoa and back again is a bit of a kludge, but gethostpoop wants a string and there's much gnarlier code out there already, so I don't feel bad. The *real* question is why BFD sockets wasn't designed to allow listens for connections *from* specific hosts/ports, instead of requiring the caller to accept the connection and then reject undesireable ones by closing. In other words, we need a TCP MSG_PEEK. */ z = ntohs (remend->sin_port); strcpy (bigbuf_net, inet_ntoa (remend->sin_addr)); whozis = gethostpoop (bigbuf_net, o_nflag); errno = 0; x = 0; /* use as a flag... */ if (rad) /* xxx: fix to go down the *list* if we have one? */ if (memcmp (rad, whozis->iaddrs, sizeof (SA))) x = 1; if (rp) if (z != rp) x = 1; if (x) /* guilty! */ bail ("invalid connection to [%s] from %s [%s] %d", cp, whozis->name, whozis->addrs[0], z); holler ("connect to [%s] from %s [%s] %d", /* oh, you're okay.. */ cp, whozis->name, whozis->addrs[0], z); return (nnetfd); /* open! */dol_tmo: errno = ETIMEDOUT; /* fake it */dol_err: close (nnetfd); return (-1);} /* dolisten *//* udptest : fire a couple of packets at a UDP target port, just to see if it's really there. On BSD kernels, ICMP host/port-unreachable errors get delivered to our socket as ECONNREFUSED write errors. On SV kernels, we lose; we'll have to collect and analyze raw ICMP ourselves a la satan's probe_udp_ports backend. Guess where one could swipe the appropriate code from... Use the time delay between writes if given, otherwise use the "tcp ping" trick for getting the RTT. [I got that idea from pluvius, and warped it.] Return either the original fd, or clean up and return -1. */udptest (fd, where) int fd; IA * where;{ register int rr; rr = write (fd, bigbuf_in, 1); if (rr != 1) holler ("udptest first write failed?! errno %d", errno); if (o_wait) sleep (o_wait); else {/* use the tcp-ping trick: try connecting to a normally refused port, which causes us to block for the time that SYN gets there and RST gets back. Not completely reliable, but it *does* mostly work. */ o_udpmode = 0; /* so doconnect does TCP this time *//* Set a temporary connect timeout, so packet filtration doesnt cause us to hang forever, and hit it */ o_wait = 5; /* enough that we'll notice?? */ rr = doconnect (where, SLEAZE_PORT, 0, 0); if (rr > 0) close (rr); /* in case it *did* open */ o_wait = 0; /* reset it */ o_udpmode++; /* we *are* still doing UDP, right? */ } /* if o_wait */ errno = 0; /* clear from sleep */ rr = write (fd, bigbuf_in, 1); if (rr == 1) /* if write error, no UDP listener */ return (fd); close (fd); /* use it or lose it! */ return (-1);} /* udptest *//* oprint : Hexdump bytes shoveled either way to a running logfile, in the format:D offset - - - - --- 16 bytes --- - - - - # .... ascii ..... where "which" sets the direction indicator, D: 0 -- sent to network, or ">" 1 -- rcvd and printed to stdout, or "<" and "buf" and "n" are data-block and length. If the current block generates a partial line, so be it; we *want* that lockstep indication of who sent what when. Adapted from dgaudet's original example -- but must be ripping *fast*, since we don't want to be too disk-bound... */void oprint (which, buf, n) int which; char * buf; int n;{ int bc; /* in buffer count */ int obc; /* current "global" offset */ int soc; /* stage write count */ register unsigned char * p; /* main buf ptr; m.b. unsigned here */ register unsigned char * op; /* out hexdump ptr */ register unsigned char * a; /* out asc-dump ptr */ register int x; register unsigned int y; if (! ofd) bail ("oprint called with no open fd?!"); if (n == 0) return; op = stage; if (which) { *op = '<'; obc = wrote_out; /* use the globals! */ } else { *op = '>'; obc = wrote_net; } op++; /* preload "direction" */ *op = ' '; p = (unsigned char *) buf; bc = n; stage[59] = '#'; /* preload separator */ stage[60] = ' '; while (bc) { /* for chunk-o-data ... */ x = 16; soc = 78; /* len of whole formatted line */ if (bc < x) { soc = soc - 16 + bc; /* fiddle for however much is left */ x = (bc * 3) + 11; /* 2 digits + space per, after D & offset */ op = &stage[x]; x = 16 - bc; while (x) { *op++ = ' '; /* preload filler spaces */ *op++ = ' '; *op++ = ' '; x--; } x = bc; /* re-fix current linecount */ } /* if bc < x */ bc -= x; /* fix wrt current line size */ sprintf (&stage[2], "%8.8x ", obc); /* xxx: still slow? */ obc += x; /* fix current offset */ op = &stage[11]; /* where hex starts */ a = &stage[61]; /* where ascii starts */ while (x) { /* for line of dump, however long ... */ y = (int)(*p >> 4); /* hi half */ *op = hexnibs[y]; op++; y = (int)(*p & 0x0f); /* lo half */ *op = hexnibs[y]; op++; *op = ' '; op++; if ((*p > 31) && (*p < 127)) *a = *p; /* printing */ else *a = '.'; /* nonprinting, loose def */ a++; p++; x--; } /* while x */ *a = '\n'; /* finish the line */ x = write (ofd, stage, soc); if (x < 0) bail ("ofd write err"); } /* while bc */} /* oprint */#ifdef TELNETUSHORT o_tn = 0; /* global -t option *//* atelnet : Answer anything that looks like telnet negotiation with don't/won't. This doesn't modify any data buffers, update the global output count, or show up in a hexdump -- it just shits into the outgoing stream. Idea and codebase from Mudge@l0pht.com. */void atelnet (buf, size) unsigned char * buf; /* has to be unsigned here! */ unsigned int size;{ static unsigned char obuf [4]; /* tiny thing to build responses into */ register int x; register unsigned char y; register unsigned char * p; y = 0; p = buf; x = size; while (x > 0) { if (*p != 255) /* IAC? */ goto notiac; obuf[0] = 255; p++; x--; if ((*p == 251) || (*p == 252)) /* WILL or WONT */ y = 254; /* -> DONT */ if ((*p == 253) || (*p == 254)) /* DO or DONT */ y = 252; /* -> WONT */ if (y) { obuf[1] = y; p++; x--; obuf[2] = *p; /* copy actual option byte */ (void) write (netfd, obuf, 3);/* if one wanted to bump wrote_net or do a hexdump line, here's the place */ y = 0; } /* if y */notiac: p++; x--; } /* while x */} /* atelnet */#endif /* TELNET *//* readwrite : handle stdin/stdout/network I/O. Bwahaha!! -- the select loop from hell. In this instance, return what might become our exit status. */int readwrite (fd) int fd;{ register int rr; register char * zp; /* stdin buf ptr */ register char * np; /* net-in buf ptr */ unsigned int rzleft; unsigned int rnleft; USHORT netretry; /* net-read retry counter */ USHORT wretry; /* net-write sanity counter */ USHORT wfirst; /* one-shot flag to skip first net read *//* if you don't have all this FD_* macro hair in sys/types.h, you'll have to either find it or do your own bit-bashing: *ding1 |= (1 << fd), etc... */ if (fd > FD_SETSIZE) { holler ("Preposterous fd value %d", fd); return (1); } FD_SET (fd, ding1); /* global: the net is open */ netretry = 2; wfirst = 0; rzleft = rnleft = 0; if (insaved) { rzleft = insaved; /* preload multi-mode fakeouts */ zp = bigbuf_in; wfirst = 1; if (Single) /* if not scanning, this is a one-off first */ insaved = 0; /* buffer left over from argv construction, */ else { FD_CLR (0, ding1); /* OR we've already got our repeat chunk, */ close (0); /* so we won't need any more stdin */ } /* Single */ } /* insaved */ if (o_interval) sleep (o_interval); /* pause *before* sending stuff, too */ errno = 0; /* clear from sleep, close, whatever *//* and now the big ol' select shoveling loop ... */ while (FD_ISSET (fd, ding1)) { /* i.e. till the *net* closes! */ wretry = 8200; /* more than we'll ever hafta write */ if (wfirst) { /* any saved stdin buffer? */ wfirst = 0; /* clear flag for the duration */ goto shovel; /* and go handle it first */ } *ding2 = *ding1; /* FD_COPY ain't portable... *//* some systems, notably linux, crap into their select timers on return, so we create a expendable copy and give *that* to select. *Fuck* me ... */ if (timer1) memcpy (timer2, timer1, sizeof (struct timeval)); rr = select (16, ding2, 0, 0, timer2); /* here it is, kiddies */ if (rr < 0) { if (errno != EINTR) { /* might have gotten ^Zed, etc ?*/ holler ("select fuxored"); close (fd); return (1); } } /* select fuckup *//* if we have a timeout AND stdin is closed AND we haven't heard anything from the net during that time, assume it's dead and close it too. */ if (rr == 0) { if (! FD_ISSET (0, ding1)) netretry--; /* we actually try a coupla times. */ if (! netretry) { if (o_verbose > 1) /* normally we don't care */ holler ("net timeout"); close (fd); return (0); /* not an error! */ } } /* select timeout *//* xxx: should we check the exception fds too? The read fds seem to give us the right info, and none of the examples I found bothered. *//* Ding!! Something arrived, go check all the incoming hoppers, net first */ if (FD_ISSET (fd, ding2)) { /* net: ding! */ rr = read (fd, bigbuf_net, BIGSIZ); if (rr <= 0) { FD_CLR (fd, ding1); /* net closed, we'll finish up... */ rzleft = 0; /* can't write anymore: broken pipe */ } else { rnleft = rr; np = bigbuf_net;#ifdef TELNET if (o_tn) atelnet (np, rr); /* fake out telnet stuff */#endif /* TELNET */ } /* if rr */Debug (("got %d from the net, errno %d", rr, errno)) } /* net:ding *//* if we're in "slowly" mode there's probably still stuff in the stdin buffer, so don't read unless we really need MORE INPUT! MORE INPUT! */ if (rzleft) goto shovel;/* okay, suck more stdin */ if (FD_ISSET (0, ding2)) { /* stdin: ding! */ rr = read (0, bigbuf_in, BIGSIZ);/* Considered making reads here smaller for UDP mode, but 8192-byte mobygrams are kinda fun and exercise the reassembler. */ if (rr <= 0) { /* at end, or fukt, or ... */ FD_CLR (0, ding1); /* disable and close stdin */ close (0); } else { rzleft = rr; zp = bigbuf_in;/* special case for multi-mode -- we'll want to send this one buffer to every open TCP port or every UDP attempt, so save its size and clean up stdin */ if (! Single) { /* we might be scanning... */ insaved = rr; /* save len */ FD_CLR (0, ding1); /* disable further junk from stdin */ close (0); /* really, I mean it */ } /* Single */ } /* if rr/read */ } /* stdin:ding */shovel:/* now that we've dingdonged all our thingdings, send off the results. Geez, why does this look an awful lot like the big loop in "rsh"? ... not sure if the order of this matters, but write net -> stdout first. *//* sanity check. Works because they're both unsigned... */ if ((rzleft > 8200) || (rnleft > 8200)) { holler ("Bogus buffers: %d, %d", rzleft, rnleft); rzleft = rnleft = 0; }/* net write retries sometimes happen on UDP connections */ if (! wretry) { /* is something hung? */ holler ("too many output retries"); return (1); } if (rnleft) { rr = write (1, np, rnleft); if (rr > 0) { if (o_wfile) oprint (1, np, rr); /* log the stdout */ np += rr; /* fix up ptrs and whatnot */ rnleft -= rr; /* will get sanity-checked above */ wrote_out += rr; /* global count */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -