home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersguides-&-software / ncnt090.zip / README.TXT < prev   
Text File  |  1996-09-11  |  63KB  |  974 lines

  1.  
  2. THE DEFCON RELEASE 7/26/96
  3.  
  4. This is a beta release of netcat for Windows 95 and Windows NT.  It was
  5. ported from hobbit's netcat 1.0.
  6.  
  7. This was built on NT 4.0 using Visual C++ 4.0.  It has had some superficial
  8. testing on Windows 95 as well as NT 3.51 and 4.0.
  9.  
  10. *Things that don't work yet*
  11.  
  12. - None of the timer settings for delays and timeouts are implemented.
  13. - source routing may never work on NT.
  14. - the gaping security hole feature or exec function is not done.
  15.  
  16. I am planning on cleaning up the gnarly IFDEFs for Win32 and brining this
  17. release back into the fold of the standard unix make if possible.  NT
  18. pretends to be unix but it's only pretty close.
  19.  
  20. Please report any problems to the address below.
  21.  
  22. Weld Pond (weld@l0pht.com)
  23. L0pht Heavy Industries
  24.  
  25.  
  26. NOW ON TO hobbit's FULL README FILE
  27.  
  28. Netcat 1.10
  29. ===========                                                             /\_/\
  30.                                                                       / 0 0 \
  31. Netcat is a simple Unix utility which reads and writes data     ====v====
  32. across network connections, using TCP or UDP protocol.          \  W  /
  33. It is designed to be a reliable "back-end" tool that can      |     |     _
  34. be used directly or easily driven by other programs and          / ___ \    /
  35. scripts.  At the same time, it is a feature-rich network     / /   \ \  |
  36. debugging and exploration tool, since it can create almost    (((-----)))-'
  37. any kind of connection you would need and has several         /
  38. interesting built-in capabilities.  Netcat, or "nc" as the    (      ___
  39. actual program is named, should have been supplied long ago     \__.=|___E
  40. as another one of those cryptic but standard Unix tools.            /
  41.  
  42. In the simplest usage, "nc host port" creates a TCP connection to the given
  43. port on the given target host.  Your standard input is then sent to the host,
  44. and anything that comes back across the connection is sent to your standard
  45. output.  This continues indefinitely, until the network side of the connection
  46. shuts down.  Note that this behavior is different from most other applications
  47. which shut everything down and exit after an end-of-file on the standard input.
  48.  
  49. Netcat can also function as a server, by listening for inbound connections
  50. on arbitrary ports and then doing the same reading and writing.  With minor
  51. limitations, netcat doesn't really care if it runs in "client" or "server"
  52. mode -- it still shovels data back and forth until there isn't any more left.
  53. In either mode, shutdown can be forced after a configurable time of inactivity
  54. on the network side.
  55.  
  56. And it can do this via UDP too, so netcat is possibly the "udp telnet-like"
  57. application you always wanted for testing your UDP-mode servers.  UDP, as the
  58. "U" implies, gives less reliable data transmission than TCP connections and
  59. some systems may have trouble sending large amounts of data that way, but it's
  60. still a useful capability to have.
  61.  
  62. You may be asking "why not just use telnet to connect to arbitrary ports?"
  63. Valid question, and here are some reasons.  Telnet has the "standard input
  64. EOF" problem, so one must introduce calculated delays in driving scripts to
  65. allow network output to finish.  This is the main reason netcat stays running
  66. until the *network* side closes.  Telnet also will not transfer arbitrary
  67. binary data, because certain characters are interpreted as telnet options and
  68. are thus removed from the data stream.  Telnet also emits some of its
  69. diagnostic messages to standard output, where netcat keeps such things
  70. religiously separated from its *output* and will never modify any of the real
  71. data in transit unless you *really* want it to.  And of course telnet is
  72. incapable of listening for inbound connections, or using UDP instead.  Netcat
  73. doesn't have any of these limitations, is much smaller and faster than telnet,
  74. and has many other advantages.
  75.  
  76. Some of netcat's major features are:
  77.  
  78.     Outbound or inbound connections, TCP or UDP, to or from any ports
  79.     Full DNS forward/reverse checking, with appropriate warnings
  80.     Ability to use any local source port
  81.     Ability to use any locally-configured network source address
  82.     Built-in port-scanning capabilities, with randomizer
  83.     Built-in loose source-routing capability
  84.     Can read command line arguments from standard input
  85.     Slow-send mode, one line every N seconds
  86.     Hex dump of transmitted and received data
  87.     Optional ability to let another program service established connections
  88.     Optional telnet-options responder
  89.  
  90. Efforts have been made to have netcat "do the right thing" in all its various
  91. modes.  If you believe that it is doing the wrong thing under whatever
  92. circumstances, please notify me and tell me how you think it should behave.
  93. If netcat is not able to do some task you think up, minor tweaks to the code
  94. will probably fix that.  It provides a basic and easily-modified template for
  95. writing other network applications, and I certainly encourage people to make
  96. custom mods and send in any improvements they make to it.  This is the second
  97. release; the overall differences from 1.00 are relatively minor and have mostly
  98. to do with portability and bugfixes.  Many people provided greatly appreciated
  99. fixes and comments on the 1.00 release.  Continued feedback from the Internet
  100. community is always welcome!
  101.  
  102. Netcat is entirely my own creation, although plenty of other code was used as
  103. examples.  It is freely given away to the Internet community in the hope that
  104. it will be useful, with no restrictions except giving credit where it is due.
  105. No GPLs, Berkeley copyrights or any of that nonsense.  The author assumes NO
  106. responsibility for how anyone uses it.  If netcat makes you rich somehow and
  107. you're feeling generous, mail me a check.  If you are affiliated in any way
  108. with Microsoft Network, get a life.  Always ski in control.  Comments,
  109. questions, and patches to hobbit@avian.org.
  110.  
  111. Building
  112. ========
  113.  
  114. Compiling is fairly straightforward.  Examine the Makefile for a SYSTYPE that
  115. matches yours, and do "make <systype>".  The executable "nc" should appear.
  116. If there is no relevant SYSTYPE section, try "generic".  If you create new
  117. sections for generic.h and Makefile to support another platform, please follow
  118. the given format and mail back the diffs.
  119.  
  120. There are a couple of other settable #defines in netcat.c, which you can
  121. include as DFLAGS="-DTHIS -DTHAT" to your "make" invocation without having to
  122. edit the Makefile.  See the following discussions for what they are and do.
  123.  
  124. If you want to link against the resolver library on SunOS [recommended] and
  125. you have BIND 4.9.x, you may need to change XLIBS=-lresolv in the Makefile to
  126. XLIBS="-lresolv -l44bsd".
  127.  
  128. Linux sys/time.h does not really support presetting of FD_SETSIZE; a harmless
  129. warning is issued.
  130.  
  131. Some systems may warn about pointer types for signal().  No problem, though.
  132.  
  133. Exploration of features
  134. =======================
  135.  
  136. Where to begin?  Netcat is at the same time so simple and versatile, it's like
  137. trying to describe everything you can do with your Swiss Army knife.  This will
  138. go over the basics; you should also read the usage examples and notes later on
  139. which may give you even more ideas about what this sort of tool is good for.
  140.  
  141. If no command arguments are given at all, netcat asks for them, reads a line
  142. from standard input, and breaks it up into arguments internally.  This can be
  143. useful when driving netcat from certain types of scripts, with the side effect
  144. of hiding your command line arguments from "ps" displays.
  145.  
  146. The host argument can be a name or IP address.  If -n is specified, netcat
  147. will only accept numeric IP addresses and do no DNS lookups for anything.  If
  148. -n is not given and -v is turned on, netcat will do a full forward and reverse
  149. name and address lookup for the host, and warn you about the all-too-common
  150. problem of mismatched names in the DNS.  This often takes a little longer for
  151. connection setup, but is useful to know about.  There are circumstances under
  152. which this can *save* time, such as when you want to know the name for some IP
  153. address and also connect there.  Netcat will just tell you all about it, saving
  154. the manual steps of looking up the hostname yourself.  Normally mismatch-
  155. checking is case-insensitive per the DNS spec, but you can define ANAL at
  156. compile time to make it case-sensitive -- sometimes useful for uncovering minor
  157. errors in your own DNS files while poking around your networks.
  158.  
  159. A port argument is required for outbound connections, and can be numeric or a
  160. name as listed in /etc/services.  If -n is specified, only numeric arguments
  161. are valid.  Special syntax and/or more than one port argument cause different
  162. behavior -- see details below about port-scanning.
  163.  
  164. The -v switch controls the verbosity level of messages sent to standard error.
  165. You will probably want to run netcat most of the time with -v turned on, so you
  166. can see info about the connections it is trying to make.  You will probably
  167. also want to give a smallish -w argument, which limits the time spent trying to
  168. make a connection.  I usually alias "nc" to "nc -v -w 3", which makes it
  169. function just about the same for things I would otherwise use telnet to do.
  170. The timeout is easily changed by a subsequent -w argument which overrides the
  171. earlier one.  Specifying -v more than once makes diagnostic output MORE
  172. verbose.  If -v is not specified at all, netcat silently does its work unless
  173. some error happens, whereupon it describes the error and exits with a nonzero
  174. status.  Refused network connections are generally NOT considered to be errors,
  175. unless you only asked for a single TCP port and it was refused.
  176.  
  177. Note that -w also sets the network inactivity timeout.  This does not have any
  178. effect until standard input closes, but then if nothing further arrives from
  179. the network in the next <timeout> seconds, netcat tries to read the net once
  180. more for good measure, and then closes and exits.  There are a lot of network
  181. services now that accept a small amount of input and return a large amount of
  182. output, such as Gopher and Web servers, which is the main reason netcat was
  183. written to "block" on the network staying open rather than standard input.
  184. Handling the timeout this way gives uniform behavior with network servers that
  185. *don't* close by themselves until told to.
  186.  
  187. UDP connections are opened instead of TCP when -u is specified.  These aren't
  188. really "connections" per se since UDP is a connectionless protocol, although
  189. netcat does internally use the "connected UDP socket" mechanism that most
  190. kernels support.  Although netcat claims that an outgoing UDP connection is
  191. "open" immediately, no data is sent until something is read from standard
  192. input.  Only thereafter is it possible to determine whether there really is a
  193. UDP server on the other end, and often you just can't tell.  Most UDP protocols
  194. use timeouts and retries to do their thing and in many cases won't bother
  195. answering at all, so you should specify a timeout and hope for the best.  You
  196. will get more out of UDP connections if standard input is fed from a source
  197. of data that looks like various kinds of server requests.
  198.  
  199. To obtain a hex dump file of the data sent either way, use "-o logfile".  The
  200. dump lines begin with "<" or ">" to respectively indicate "from the net" or
  201. "to the net", and contain the total count per direction, and hex and ascii
  202. representations of the traffic.  Capturing a hex dump naturally slows netcat
  203. down a bit, so don't use it where speed is critical.
  204.  
  205. Netcat can bind to any local port, subject to privilege restrictions and ports
  206. that are already in use.  It is also possible to use a specific local network
  207. source address if it is that of a network interface on your machine.  [Note:
  208. this does not work correctly on all platforms.]  Use "-p portarg" to grab a
  209. specific local port, and "-s ip-addr" or "-s name" to have that be your source
  210. IP address.  This is often referred to as "anchoring the socket".  Root users
  211. can grab any unused source port including the "reserved" ones less than 1024.
  212. Absence of -p will bind to whatever unused port the system gives you, just like
  213. any other normal client connection, unless you use -r [see below].
  214.  
  215. Listen mode will cause netcat to wait for an inbound connection, and then the
  216. same data transfer happens.  Thus, you can do "nc -l -p 1234 < filename" and
  217. when someone else connects to your port 1234, the file is sent to them whether
  218. they wanted it or not.  Listen mode is generally used along with a local port
  219. argument -- this is required for UDP mode, while TCP mode can have the system
  220. assign one and tell you what it is if -v is turned on.  If you specify a target
  221. host and optional port in listen mode, netcat will accept an inbound connection
  222. only from that host and if you specify one, only from that foreign source port.
  223. In verbose mode you'll be informed about the inbound connection, including what
  224. address and port it came from, and since listening on "any" applies to several
  225. possibilities, which address it came *to* on your end.  If the system supports
  226. IP socket options, netcat will attempt to retrieve any such options from an
  227. inbound connection and print them out in hex.
  228.  
  229. If netcat is compiled with -DGAPING_SECURITY_HOLE, the -e argument specifies
  230. a program to exec after making or receiving a successful connection.  In the
  231. listening mode, this works similarly to "inetd" but only for a single instance.
  232. Use with GREAT CARE.  This piece of the code is normally not enabled; if you
  233. know what you're doing, have fun.  This hack also works in UDP mode.  Note that
  234. you can only supply -e with the name of the program, but no arguments.  If you
  235. want to launch something with an argument list, write a two-line wrapper script
  236. or just use inetd like always.
  237.  
  238. If netcat is compiled with -DTELNET, the -t argument enables it to respond
  239. to telnet option negotiation [always in the negative, i.e. DONT or WONT].
  240. This allows it to connect to a telnetd and get past the initial negotiation
  241. far enough to get a login prompt from the server.  Since this feature has
  242. the potential to modify the data stream, it is not enabled by default.  You
  243. have to understand why you might need this and turn on the #define yourself.
  244.  
  245. Data from the network connection is always delivered to standard output as
  246. efficiently as possible, using large 8K reads and writes.  Standard input is
  247. normally sent to the net the same way, but the -i switch specifies an "interval
  248. time" which slows this down considerably.  Standard input is still read in
  249. large batches, but netcat then tries to find where line breaks exist and sends
  250. one line every interval time.  Note that if standard input is a terminal, data
  251. is already read line by line, so unless you make the -i interval rather long,
  252. what you type will go out at a fairly normal rate.  -i is really designed
  253. for use when you want to "measure out" what is read from files or pipes.
  254.  
  255. Port-scanning is a popular method for exploring what's out there.  Netcat
  256. accepts its commands with options first, then the target host, and everything
  257. thereafter is interpreted as port names or numbers, or ranges of ports in M-N
  258. syntax.  CAVEAT: some port names in /etc/services contain hyphens -- netcat
  259. currently will not correctly parse those, so specify ranges using numbers if
  260. you can.  If more than one port is thus specified, netcat connects to *all* of
  261. them, sending the same batch of data from standard input [up to 8K worth] to
  262. each one that is successfully connected to.  Specifying multiple ports also
  263. suppresses diagnostic messages about refused connections, unless -v is
  264. specified twice for "more verbosity".  This way you normally get notified only
  265. about genuinely open connections.  Example: "nc -v -w 2 -z target 20-30" will
  266. try connecting to every port between 20 and 30 [inclusive] at the target, and
  267. will likely inform you about an FTP server, telnet server, and mailer along the
  268. way.  The -z switch prevents sending any data to a TCP connection and very
  269. limited probe data to a UDP connection, and is thus useful as a fast scanning
  270. mode just to see what ports the target is listening on.  To limit scanning
  271. speed if desired, -i will insert a delay between each port probe.  There are
  272. some pitfalls with regard to UDP scanning, described later, but in general it
  273. works well.
  274.  
  275. For each range of ports specified, scanning is normally done downward within
  276. that range.  If the -r switch is used, scanning hops randomly around within
  277. that range and reports open ports as it finds them.  [If you want them listed
  278. in order regardless, pipe standard error through "sort"...]  In addition, if
  279. random mode is in effect, the local source ports are also randomized.  This
  280. prevents netcat from exhibiting any kind of regular pattern in its scanning.
  281. You can exert fairly fine control over your scan by judicious use of -r and
  282. selected port ranges to cover.  If you use -r for a single connection, the
  283. source port will have a random value above 8192, rather than the next one the
  284. kernel would have assigned you.  Note that selecting a specific local port
  285. with -p overrides any local-port randomization.
  286.  
  287. Many people are interested in testing network connectivity using IP source
  288. routing, even if it's only to make sure their own firewalls are blocking
  289. source-routed packets.  On systems that support it, the -g switch can be used
  290. multiple times [up to 8] to construct a loose-source-routed path for your
  291. connection, and the -G argument positions the "hop pointer" within the list.
  292. If your network allows source-routed traffic in and out, you can test
  293. connectivity to your own services via remote points in the internet.  Note that
  294. although newer BSD-flavor telnets also have source-routing capability, it isn't
  295. clearly documented and the command syntax is somewhat clumsy.  Netcat's
  296. handling of "-g" is modeled after "traceroute".
  297.  
  298. Netcat tries its best to behave just like "cat".  It currently does nothing to
  299. terminal input modes, and does no end-of-line conversion.  Standard input from
  300. a terminal is read line by line with normal editing characters in effect.  You
  301. can freely suspend out of an interactive connection and resume.  ^C or whatever
  302. your interrupt character is will make netcat close the network connection and
  303. exit.  A switch to place the terminal in raw mode has been considered, but so
  304. far has not been necessary.  You can send raw binary data by reading it out of
  305. a file or piping from another program, so more meaningful effort would be spent
  306. writing an appropriate front-end driver.
  307.  
  308. Netcat is not an "arbitrary packet generator", but the ability to talk to raw
  309. sockets and/or nit/bpf/dlpi may appear at some point.  Such things are clearly
  310. useful; I refer you to Darren Reed's excellent ip_filter package, which now
  311. includes a tool to construct and send raw packets with any contents you want.
  312.  
  313. Example uses -- the light side
  314. ==============================
  315.  
  316. Again, this is a very partial list of possibilities, but it may get you to
  317. think up more applications for netcat.  Driving netcat with simple shell or
  318. expect scripts is an easy and flexible way to do fairly complex tasks,
  319. especially if you're not into coding network tools in C.  My coding isn't
  320. particularly strong either [although undoubtedly better after writing this
  321. thing!], so I tend to construct bare-metal tools like this that I can trivially
  322. plug into other applications.  Netcat doubles as a teaching tool -- one can
  323. learn a great deal about more complex network protocols by trying to simulate
  324. them through raw connections!
  325.  
  326. An example of netcat as a backend for something else is the shell-script
  327. Web browser, which simply asks for the relevant parts of a URL and pipes
  328. "GET /what/ever" into a netcat connection to the server.  I used to do this
  329. with telnet, and had to use calculated sleep times and other stupidity to
  330. kludge around telnet's limitations.  Netcat guarantees that I get the whole
  331. page, and since it transfers all the data unmodified, I can even pull down
  332. binary image files and display them elsewhere later.  Some folks may find the
  333. idea of a shell-script web browser silly and strange, but it starts up and
  334. gets me my info a hell of a lot faster than a GUI browser and doesn't hide
  335. any contents of links and forms and such.  This is included, as scripts/web,
  336. along with several other web-related examples.
  337.  
  338. Netcat is an obvious replacement for telnet as a tool for talking to daemons.
  339. For example, it is easier to type "nc host 25", talk to someone's mailer, and
  340. just ^C out than having to type ^]c or QUIT as telnet would require you to do.
  341. You can quickly catalog the services on your network by telling netcat to
  342. connect to well-known services and collect greetings, or at least scan for open
  343. ports.  You'll probably want to collect netcat's diagnostic messages in your
  344. output files, so be sure to include standard error in the output using
  345. `>& file' in *csh or `> file 2>&1' in bourne shell.
  346.  
  347. A scanning example: "echo QUIT | nc -v -w 5 target 20-250 500-600 5990-7000"
  348. will inform you about a target's various well-known TCP servers, including
  349. r-services, X, IRC, and maybe a few you didn't expect.  Sending in QUIT and
  350. using the timeout will almost guarantee that you see some kind of greeting or
  351. error from each service, which usually indicates what it is and what version.
  352. [Beware of the "chargen" port, though...]  SATAN uses exactly this technique to
  353. collect host information, and indeed some of the ideas herein were taken from
  354. the SATAN backend tools.  If you script this up to try every host in your
  355. subnet space and just let it run, you will not only see all the services,
  356. you'll find out about hosts that aren't correctly listed in your DNS.  Then you
  357. can compare new snapshots against old snapshots to see changes.  For going
  358. after particular services, a more intrusive example is in scripts/probe.
  359.  
  360. Netcat can be used as a simple data transfer agent, and it doesn't really
  361. matter which end is the listener and which end is the client -- input at one
  362. side arrives at the other side as output.  It is helpful to start the listener
  363. at the receiving side with no timeout specified, and then give the sending side
  364. a small timeout.  That way the listener stays listening until you contact it,
  365. and after data stops flowing the client will time out, shut down, and take the
  366. listener with it.  Unless the intervening network is fraught with problems,
  367. this should be completely reliable, and you can always increase the timeout.  A
  368. typical example of something "rsh" is often used for: on one side,
  369.  
  370.     nc -l -p 1234 | uncompress -c | tar xvfp -
  371.  
  372. and then on the other side
  373.  
  374.     tar cfp - /some/dir | compress -c | nc -w 3 othermachine 1234
  375.  
  376. will transfer the contents of a directory from one machine to another, without
  377. having to worry about .rhosts files, user accounts, or inetd configurations
  378. at either end.  Again, it matters not which is the listener or receiver; the
  379. "tarring" machine could just as easily be running the listener instead.  One
  380. could conceivably use a scheme like this for backups, by having cron-jobs fire
  381. up listeners and backup handlers [which can be restricted to specific addresses
  382. and ports between each other] and pipe "dump" or "tar" on one machine to "dd
  383. of=/dev/tapedrive" on another as usual.  Since netcat returns a nonzero exit
  384. status for a denied listener connection, scripts to handle such tasks could
  385. easily log and reject connect attempts from third parties, and then retry.
  386.  
  387. Another simple data-transfer example: shipping things to a PC that doesn't have
  388. any network applications yet except a TCP stack and a web browser.  Point the
  389. browser at an arbitrary port on a Unix server by telling it to download
  390. something like http://unixbox:4444/foo, and have a listener on the Unix side
  391. ready to ship out a file when the connect comes in.  The browser may pervert
  392. binary data when told to save the URL, but you can dig the raw data out of
  393. the on-disk cache.
  394.  
  395. If you build netcat with GAPING_SECURITY_HOLE defined, you can use it as an
  396. "inetd" substitute to test experimental network servers that would otherwise
  397. run under "inetd".  A script or program will have its input and output hooked
  398. to the network the same way, perhaps sans some fancier signal handling.  Given
  399. that most network services do not bind to a particular local address, whether
  400. they are under "inetd" or not, it is possible for netcat avoid the "address
  401. already in use" error by binding to a specific address.  This lets you [as
  402. root, for low ports] place netcat "in the way" of a standard service, since
  403. inbound connections are generally sent to such specifically-bound listeners
  404. first and fall back to the ones bound to "any".  This allows for a one-off
  405. experimental simulation of some service, without having to screw around with
  406. inetd.conf.  Running with -v turned on and collecting a connection log from
  407. standard error is recommended.
  408.  
  409. Netcat as well can make an outbound connection and then run a program or script
  410. on the originating end, with input and output connected to the same network
  411. port.  This "inverse inetd" capability could enhance the backup-server concept
  412. described above or help facilitate things such as a "network dialback" concept.
  413. The possibilities are many and varied here; if such things are intended as
  414. security mechanisms, it may be best to modify netcat specifically for the
  415. purpose instead of wrapping such functions in scripts.
  416.  
  417. Speaking of inetd, netcat will function perfectly well *under* inetd as a TCP
  418. connection redirector for inbound services, like a "plug-gw" without the
  419. authentication step.  This is very useful for doing stuff like redirecting
  420. traffic through your firewall out to other places like web servers and mail
  421. hubs, while posing no risk to the firewall machine itself.  Put netcat behind
  422. inetd and tcp_wrappers, perhaps thusly:
  423.  
  424.     www stream tcp nowait nobody /etc/tcpd /bin/nc -w 3 realwww 80
  425.  
  426. and you have a simple and effective "application relay" with access control
  427. and logging.  Note use of the wait time as a "safety" in case realwww isn't
  428. reachable or the calling user aborts the connection -- otherwise the relay may
  429. hang there forever.
  430.  
  431. You can use netcat to generate huge amounts of useless network data for
  432. various performance testing.  For example, doing
  433.  
  434.     yes AAAAAAAAAAAAAAAAAAAAAA | nc -v -v -l -p 2222 > /dev/null
  435.  
  436. on one side and then hitting it with
  437.  
  438.     yes BBBBBBBBBBBBBBBBBBBBBB | nc othermachine 2222 > /dev/null
  439.  
  440. from another host will saturate your wires with A's and B's.  The "very
  441. verbose" switch usage will tell you how many of each were sent and received
  442. after you interrupt either side.  Using UDP mode produces tremendously MORE
  443. trash per unit time in the form of fragmented 8 Kbyte mobygrams -- enough to
  444. stress-test kernels and network interfaces.  Firing random binary data into
  445. various network servers may help expose bugs in their input handling, which
  446. nowadays is a popular thing to explore.  A simple example data-generator is
  447. given in data/data.c included in this package, along with a small collection
  448. of canned input files to generate various packet contents.  This program is
  449. documented in its beginning comments, but of interest here is using "%r" to
  450. generate random bytes at well-chosen points in a data stream.  If you can
  451. crash your daemon, you likely have a security problem.
  452.  
  453. The hex dump feature may be useful for debugging odd network protocols,
  454. especially if you don't have any network monitoring equipment handy or aren't
  455. root where you'd need to run "tcpdump" or something.  Bind a listening netcat
  456. to a local port, and have it run a script which in turn runs another netcat
  457. to the real service and captures the hex dump to a log file.  This sets up a
  458. transparent relay between your local port and wherever the real service is.
  459. Be sure that the script-run netcat does *not* use -v, or the extra info it
  460. sends to standard error may confuse the protocol.  Note also that you cannot
  461. have the "listen/exec" netcat do the data capture, since once the connection
  462. arrives it is no longer netcat that is running.
  463.  
  464. Binding to an arbitrary local port allows you to simulate things like r-service
  465. clients, if you are root locally.  For example, feeding "^@root^@joe^@pwd^@"
  466. [where ^@ is a null, and root/joe could be any other local/remote username
  467. pair] into a "rsh" or "rlogin" server, FROM your port 1023 for example,
  468. duplicates what the server expects to receive.  Thus, you can test for insecure
  469. .rhosts files around your network without having to create new user accounts on
  470. your client machine.  The program data/rservice.c can aid this process by
  471. constructing the "rcmd" protocol bytes.  Doing this also prevents "rshd" from
  472. trying to create that separate standard-error socket and still gives you an
  473. input path, as opposed to the usual action of "rsh -n".  Using netcat for
  474. things like this can be really useful sometimes, because rsh and rlogin
  475. generally want a host *name* as an argument and won't accept IP addresses.  If
  476. your client-end DNS is hosed, as may be true when you're trying to extract
  477. backup sets on to a dumb client, "netcat -n" wins where normal rsh/rlogin is
  478. useless.
  479.  
  480. If you are unsure that a remote syslogger is working, test it with netcat.
  481. Make a UDP connection to port 514 and type in "<0>message", which should
  482. correspond to "kern.emerg" and cause syslogd to scream into every file it has
  483. open [and possibly all over users' terminals].  You can tame this down by
  484. using a different number and use netcat inside routine scripts to send syslog
  485. messages to places that aren't configured in syslog.conf.  For example,
  486. "echo '<38>message' | nc -w 1 -u loggerhost 514" should send to auth.notice
  487. on loggerhost.  The exact number may vary; check against your syslog.h first.
  488.  
  489. Netcat provides several ways for you to test your own packet filters.  If you
  490. bind to a port normally protected against outside access and make a connection
  491. to somewhere outside your own network, the return traffic will be coming to
  492. your chosen port from the "outside" and should be blocked.  TCP may get through
  493. if your filter passes all "ack syn", but it shouldn't be even doing that to low
  494. ports on your network.  Remember to test with UDP traffic as well!  If your
  495. filter passes at least outbound source-routed IP packets, bouncing a connection
  496. back to yourself via some gateway outside your network will create "incoming"
  497. traffic with your source address, which should get dropped by a correctly
  498. configured anti-spoofing filter.  This is a "non-test" if you're also dropping
  499. source-routing, but it's good to be able to test for that too.  Any packet
  500. filter worth its salt will be blocking source-routed packets in both
  501. directions, but you never know what interesting quirks you might turn up by
  502. playing around with source ports and addresses and watching the wires with a
  503. network monitor.
  504.  
  505. You can use netcat to protect your own workstation's X server against outside
  506. access.  X is stupid enough to listen for connections on "any" and never tell
  507. you when new connections arrive, which is one reason it is so vulnerable.  Once
  508. you have all your various X windows up and running you can use netcat to bind
  509. just to your ethernet address and listen to port 6000.  Any new connections
  510. from outside the machine will hit netcat instead your X server, and you get a
  511. log of who's trying.  You can either tell netcat to drop the connection, or
  512. perhaps run another copy of itself to relay to your actual X server on
  513. "localhost".  This may not work for dedicated X terminals, but it may be
  514. possible to authorize your X terminal only for its boot server, and run a relay
  515. netcat over on the server that will in turn talk to your X terminal.  Since
  516. netcat only handles one listening connection per run, make sure that whatever
  517. way you rig it causes another one to run and listen on 6000 soon afterward, or
  518. your real X server will be reachable once again.  A very minimal script just
  519. to protect yourself could be
  520.  
  521.     while true ; do
  522.       nc -v -l -s <your-addr> -p 6000 localhost 2
  523.     done
  524.  
  525. which causes netcat to accept and then close any inbound connection to your
  526. workstation's normal ethernet address, and another copy is immediately run by
  527. the script.  Send standard error to a file for a log of connection attempts.
  528. If your system can't do the "specific bind" thing all is not lost; run your
  529. X server on display ":1" or port 6001, and netcat can still function as a probe
  530. alarm by listening on 6000.
  531.  
  532. Does your shell-account provider allow personal Web pages, but not CGI scripts?
  533. You can have netcat listen on a particular port to execute a program or script
  534. of your choosing, and then just point to the port with a URL in your homepage.
  535. The listener could even exist on a completely different machine, avoiding the
  536. potential ire of the homepage-host administrators.  Since the script will get
  537. the raw browser query as input it won't look like a typical CGI script, and
  538. since it's running under your UID you need to write it carefully.  You may want
  539. to write a netcat-based script as a wrapper that reads a query and sets up
  540. environment variables for a regular CGI script.  The possibilities for using
  541. netcat and scripts to handle Web stuff are almost endless.  Again, see the
  542. examples under scripts/.
  543.  
  544. Example uses -- the dark side
  545. =============================
  546.  
  547. Equal time is deserved here, since a versatile tool like this can be useful
  548. to any Shade of Hat.  I could use my Victorinox to either fix your car or
  549. disassemble it, right?  You can clearly use something like netcat to attack
  550. or defend -- I don't try to govern anyone's social outlook, I just build tools.
  551. Regardless of your intentions, you should still be aware of these threats to
  552. your own systems.
  553.  
  554. The first obvious thing is scanning someone *else's* network for vulnerable
  555. services.  Files containing preconstructed data, be it exploratory or
  556. exploitive, can be fed in as standard input, including command-line arguments
  557. to netcat itself to keep "ps" ignorant of your doings.  The more random the
  558. scanning, the less likelihood of detection by humans, scan-detectors, or
  559. dynamic filtering, and with -i you'll wait longer but avoid loading down the
  560. target's network.  Some examples for crafting various standard UDP probes are
  561. given in data/*.d.
  562.  
  563. Some configurations of packet filters attempt to solve the FTP-data problem by
  564. just allowing such connections from the outside.  These come FROM port 20, TO
  565. high TCP ports inside -- if you locally bind to port 20, you may find yourself
  566. able to bypass filtering in some cases.  Maybe not to low ports "inside", but
  567. perhaps to TCP NFS servers, X servers, Prospero, ciscos that listen on 200x
  568. and 400x...  Similar bypassing may be possible for UDP [and maybe TCP too] if a
  569. connection comes from port 53; a filter may assume it's a nameserver response.
  570.  
  571. Using -e in conjunction with binding to a specific address can enable "server
  572. takeover" by getting in ahead of the real ones, whereupon you can snarf data
  573. sent in and feed your own back out.  At the very least you can log a hex dump
  574. of someone else's session.  If you are root, you can certainly use -s and -e to
  575. run various hacked daemons without having to touch inetd.conf or the real
  576. daemons themselves.  You may not always have the root access to deal with low
  577. ports, but what if you are on a machine that also happens to be an NFS server?
  578. You might be able to collect some interesting things from port 2049, including
  579. local file handles.  There are several other servers that run on high ports
  580. that are likely candidates for takeover, including many of the RPC services on
  581. some platforms [yppasswdd, anyone?].  Kerberos tickets, X cookies, and IRC
  582. traffic also come to mind.  RADIUS-based terminal servers connect incoming
  583. users to shell-account machines on a high port, usually 1642 or thereabouts.
  584. SOCKS servers run on 1080.  Do "netstat -a" and get creative.
  585.  
  586. There are some daemons that are well-written enough to bind separately to all
  587. the local interfaces, possibly with an eye toward heading off this sort of
  588. problem.  Named from recent BIND releases, and NTP, are two that come to mind.
  589. Netstat will show these listening on address.53 instead of *.53.  You won't
  590. be able to get in front of these on any of the real interface addresses, which
  591. of course is especially interesting in the case of named, but these servers
  592. sometimes forget about things like "alias" interface addresses or interfaces
  593. that appear later on such as dynamic PPP links.  There are some hacked web
  594. servers and versions of "inetd" floating around that specifically bind as well,
  595. based on a configuration file -- these generally *are* bound to alias addresses
  596. to offer several different address-based services from one machine.
  597.  
  598. Using -e to start a remote backdoor shell is another obvious sort of thing,
  599. easier than constructing a file for inetd to listen on "ingreslock" or
  600. something, and you can access-control it against other people by specifying a
  601. client host and port.  Experience with this truly demonstrates how fragile the
  602. barrier between being "logged in" or not really is, and is further expressed by
  603. scripts/bsh.  If you're already behind a firewall, it may be easier to make an
  604. *outbound* connection and then run a shell; a small wrapper script can
  605. periodically try connecting to a known place and port, you can later listen
  606. there until the inbound connection arrives, and there's your shell.  Running
  607. a shell via UDP has several interesting features, although be aware that once
  608. "connected", the UDP stub sockets tend to show up in "netstat" just like TCP
  609. connections and may not be quite as subtle as you wanted.  Packets may also be
  610. lost, so use TCP if you need reliable connections.  But since UDP is
  611. connectionless, a hookup of this sort will stick around almost forever, even if
  612. you ^C out of netcat or do a reboot on your side, and you only need to remember
  613. the ports you used on both ends to reestablish.  And outbound UDP-plus-exec
  614. connection creates the connected socket and starts the program immediately.  On
  615. a listening UDP connection, the socket is created once a first packet is
  616. received.  In either case, though, such a "connection" has the interesting side
  617. effect that only your client-side IP address and [chosen?] source port will
  618. thereafter be able to talk to it.  Instant access control!  A non-local third
  619. party would have to do ALL of the following to take over such a session:
  620.  
  621.     forge UDP with your source address [trivial to do; see below]
  622.     guess the port numbers of BOTH ends, or sniff the wire for them
  623.     arrange to block ICMP or UDP return traffic between it and your real
  624.       source, so the session doesn't die with a network write error.
  625.  
  626. The companion program data/rservice.c is helpful in scripting up any sort of
  627. r-service username or password guessing attack.  The arguments to "rservice"
  628. are simply the strings that get null-terminated and passed over an "rcmd"-style
  629. connection, with the assumption that the client does not need a separate
  630. standard-error port.  Brute-force password banging is best done via "rexec" if
  631. it is available since it is less likely to log failed attempts.  Thus, doing
  632. "rservice joe joespass pwd | nc target exec" should return joe's home dir if
  633. the password is right, or "Permission denied."  Plug in a dictionary and go to
  634. town.  If you're attacking rsh/rlogin, remember to be root and bind to a port
  635. between 512 and 1023 on your end, and pipe in "rservice joe joe pwd" and such.
  636.  
  637. Netcat can prevent inadvertently sending extra information over a telnet
  638. connection.  Use "nc -t" in place of telnet, and daemons that try to ask for
  639. things like USER and TERM environment variables will get no useful answers, as
  640. they otherwise would from a more recent telnet program.  Some telnetds actually
  641. try to collect this stuff and then plug the USER variable into "login" so that
  642. the caller is then just asked for a password!  This mechanism could cause a
  643. login attempt as YOUR real username to be logged over there if you use a
  644. Borman-based telnet instead of "nc -t".
  645.  
  646. Got an unused network interface configured in your kernel [e.g. SLIP], or
  647. support for alias addresses?  Ifconfig one to be any address you like, and bind
  648. to it with -s to enable all sorts of shenanigans with bogus source addresses.
  649. The interface probably has to be UP before this works; some SLIP versions
  650. need a far-end address before this is true.  Hammering on UDP services is then
  651. a no-brainer.  What you can do to an unfiltered syslog daemon should be fairly
  652. obvious; trimming the conf file can help protect against it.  Many routers out
  653. there still blindly believe what they receive via RIP and other routing
  654. protocols.  Although most UDP echo and chargen servers check if an incoming
  655. packet was sent from *another* "internal" UDP server, there are many that still
  656. do not, any two of which [or many, for that matter] could keep each other
  657. entertained for hours at the expense of bandwidth.  And you can always make
  658. someone wonder why she's being probed by nsa.gov.
  659.  
  660. Your TCP spoofing possibilities are mostly limited to destinations you can
  661. source-route to while locally bound to your phony address.  Many sites block
  662. source-routed packets these days for precisely this reason.  If your kernel
  663. does oddball things when sending source-routed packets, try moving the pointer
  664. around with -G.  You may also have to fiddle with the routing on your own
  665. machine before you start receiving packets back.  Warning: some machines still
  666. send out traffic using the source address of the outbound interface, regardless
  667. of your binding, especially in the case of localhost.  Check first.  If you can
  668. open a connection but then get no data back from it, the target host is
  669. probably killing the IP options on its end [this is an option inside TCP
  670. wrappers and several other packages], which happens after the 3-way handshake
  671. is completed.  If you send some data and observe the "send-q" side of "netstat"
  672. for that connection increasing but never getting sent, that's another symptom.
  673. Beware: if Sendmail 8.7.x detects a source-routed SMTP connection, it extracts
  674. the hop list and sticks it in the Received: header!
  675.  
  676. SYN bombing [sometimes called "hosing"] can disable many TCP servers, and if
  677. you hit one often enough, you can keep it unreachable for days.  As is true of
  678. many other denial-of-service attacks, there is currently no defense against it
  679. except maybe at the human level.  Making kernel SOMAXCONN considerably larger
  680. than the default and the half-open timeout smaller can help, and indeed some
  681. people running large high-performance web servers have *had* to do that just to
  682. handle normal traffic.  Taking out mailers and web servers is sociopathic, but
  683. on the other hand it is sometimes useful to be able to, say, disable a site's
  684. identd daemon for a few minutes.  If someone realizes what is going on,
  685. backtracing will still be difficult since the packets have a phony source
  686. address, but calls to enough ISP NOCs might eventually pinpoint the source.
  687. It is also trivial for a clueful ISP to watch for or even block outgoing
  688. packets with obviously fake source addresses, but as we know many of them are
  689. not clueful or willing to get involved in such hassles.  Besides, outbound
  690. packets with an [otherwise unreachable] source address in one of their net
  691. blocks would look fairly legitimate.
  692.  
  693. Notes
  694. =====
  695.  
  696. A discussion of various caveats, subtleties, and the design of the innards.
  697.  
  698. As of version 1.07 you can construct a single file containing command arguments
  699. and then some data to transfer.  Netcat is now smart enough to pick out the
  700. first line and build the argument list, and send any remaining data across the
  701. net to one or multiple ports.  The first release of netcat had trouble with
  702. this -- it called fgets() for the command line argument, which behind the
  703. scenes does a large read() from standard input, perhaps 4096 bytes or so, and
  704. feeds that out to the fgets() library routine.  By the time netcat 1.00 started
  705. directly read()ing stdin for more data, 4096 bytes of it were gone.  It now
  706. uses raw read() everywhere and does the right thing whether reading from files,
  707. pipes, or ttys.  If you use this for multiple-port connections, the single
  708. block of data will now be a maximum of 8K minus the first line.  Improvements
  709. have been made to the logic in sending the saved chunk to each new port.  Note
  710. that any command-line arguments hidden using this mechanism could still be
  711. extracted from a core dump.
  712.  
  713. When netcat receives an inbound UDP connection, it creates a "connected socket"
  714. back to the source of the connection so that it can also send out data using
  715. normal write().  Using this mechanism instead of recvfrom/sendto has several
  716. advantages -- the read/write select loop is simplified, and ICMP errors can in
  717. effect be received by non-root users.  However, it has the subtle side effect
  718. that if further UDP packets arrive from the caller but from different source
  719. ports, the listener will not receive them.  UDP listen mode on a multihomed
  720. machine may have similar quirks unless you specifically bind to one of its
  721. addresses.  It is not clear that kernel support for UDP connected sockets
  722. and/or my understanding of it is entirely complete here, so experiment...
  723.  
  724. You should be aware of some subtleties concerning UDP scanning.  If -z is on,
  725. netcat attempts to send a single null byte to the target port, twice, with a
  726. small time in between.  You can either use the -w timeout, or netcat will try
  727. to make a "sideline" TCP connection to the target to introduce a small time
  728. delay equal to the round-trip time between you and the target.  Note that if
  729. you have a -w timeout and -i timeout set, BOTH take effect and you wait twice
  730. as long.  The TCP connection is to a normally refused port to minimize traffic,
  731. but if you notice a UDP fast-scan taking somewhat longer than it should, it
  732. could be that the target is actually listening on the TCP port.  Either way,
  733. any ICMP port-unreachable messages from the target should have arrived in the
  734. meantime.  The second single-byte UDP probe is then sent.  Under BSD kernels,
  735. the ICMP error is delivered to the "connected socket" and the second write
  736. returns an error, which tells netcat that there is NOT a UDP service there.
  737. While Linux seems to be a fortunate exception, under many SYSV derived kernels
  738. the ICMP is not delivered, and netcat starts reporting that *all* the ports are
  739. "open" -- clearly wrong.  [Some systems may not even *have* the "udp connected
  740. socket" concept, and netcat in its current form will not work for UDP at all.]
  741. If -z is specified and only one UDP port is probed, netcat's exit status
  742. reflects whether the connection was "open" or "refused" as with TCP.
  743.  
  744. It may also be that UDP packets are being blocked by filters with no ICMP error
  745. returns, in which case everything will time out and return "open".  This all
  746. sounds backwards, but that's how UDP works.  If you're not sure, try "echo
  747. w00gumz | nc -u -w 2 target 7" to see if you can reach its UDP echo port at
  748. all.  You should have no trouble using a BSD-flavor system to scan for UDP
  749. around your own network, although flooding a target with the high activity that
  750. -z generates will cause it to occasionally drop packets and indicate false
  751. "opens".  A more "correct" way to do this is collect and analyze the ICMP
  752. errors, as does SATAN's "udp_scan" backend, but then again there's no guarantee
  753. that the ICMP gets back to you either.  Udp_scan also does the zero-byte
  754. probes but is excruciatingly careful to calculate its own round-trip timing
  755. average and dynamically set its own response timeouts along with decoding any
  756. ICMP received.  Netcat uses a much sleazier method which is nonetheless quite
  757. effective.  Cisco routers are known to have a "dead time" in between ICMP
  758. responses about unreachable UDP ports, so a fast scan of a cisco will show
  759. almost everything "open".  If you are looking for a specific UDP service, you
  760. can construct a file containing the right bytes to trigger a response from the
  761. other end and send that as standard input.  Netcat will read up to 8K of the
  762. file and send the same data to every UDP port given.  Note that you must use a
  763. timeout in this case [as would any other UDP client application] since the
  764. two-write probe only happens if -z is specified.
  765.  
  766. Many telnet servers insist on a specific set of option negotiations before
  767. presenting a login banner.  On a raw connection you will see this as small
  768. amount of binary gook.  My attempts to create fixed input bytes to make a
  769. telnetd happy worked some places but failed against newer BSD-flavor ones,
  770. possibly due to timing problems, but there are a couple of much better
  771. workarounds.  First, compile with -DTELNET and use -t if you just want to get
  772. past the option negotiation and talk to something on a telnet port.  You will
  773. still see the binary gook -- in fact you'll see a lot more of it as the options
  774. are responded to behind the scenes.  The telnet responder does NOT update the
  775. total byte count, or show up in the hex dump -- it just responds negatively to
  776. any options read from the incoming data stream.  If you want to use a normal
  777. full-blown telnet to get to something but also want some of netcat's features
  778. involved like settable ports or timeouts, construct a tiny "foo" script:
  779.  
  780.     #! /bin/sh
  781.     exec nc -otheroptions targethost 23
  782.  
  783. and then do
  784.  
  785.     nc -l -p someport -e foo localhost &
  786.     telnet localhost someport
  787.  
  788. and your telnet should connect transparently through the exec'ed netcat to
  789. the target, using whatever options you supplied in the "foo" script.  Don't
  790. use -t inside the script, or you'll wind up sending *two* option responses.
  791.  
  792. I've observed inconsistent behavior under some Linuxes [perhaps just older
  793. ones?] when binding in listen mode.  Sometimes netcat binds only to "localhost"
  794. if invoked with no address or port arguments, and sometimes it is unable to
  795. bind to a specific address for listening if something else is already listening
  796. on "any".  The former problem can be worked around by specifying "-s 0.0.0.0",
  797. which will do the right thing despite netcat claiming that it's listening on
  798. [127.0.0.1].  This is a known problem -- for example, there's a mention of it
  799. in the makefile for SOCKS.  On the flip side, binding to localhost and sending
  800. packets to some other machine doesn't work as you'd expect -- they go out with
  801. the source address of the sending interface instead.  The Linux kernel contains
  802. a specific check to ensure that packets from 127.0.0.1 are never sent to the
  803. wire; other kernels may contain similar code.  Linux, of course, *still*
  804. doesn't support source-routing, but they claim that it and many other network
  805. improvements are at least breathing hard.
  806.  
  807. There are several possible errors associated with making TCP connections, but
  808. to specifically see anything other than "refused", one must wait the full
  809. kernel-defined timeout for a connection to fail.  Netcat's mechanism of
  810. wrapping an alarm timer around the connect prevents the *real* network error
  811. from being returned -- "errno" at that point indicates "interrupted system
  812. call" since the connect attempt was interrupted.  Some old 4.3 BSD kernels
  813. would actually return things like "host unreachable" immediately if that was
  814. the case, but most newer kernels seem to wait the full timeout and *then* pass
  815. back the real error.  Go figure.  In this case, I'd argue that the old way was
  816. better, despite those same kernels generally being the ones that tear down
  817. *established* TCP connections when ICMP-bombed.
  818.  
  819. Incoming socket options are passed to applications by the kernel in the
  820. kernel's own internal format.  The socket-options structure for source-routing
  821. contains the "first-hop" IP address first, followed by the rest of the real
  822. options list.  The kernel uses this as is when sending reply packets -- the
  823. structure is therefore designed to be more useful to the kernel than to humans,
  824. but the hex dump of it that netcat produces is still useful to have.
  825.  
  826. Kernels treat source-routing options somewhat oddly, but it sort of makes sense
  827. once one understands what's going on internally.  The options list of addresses
  828. must contain hop1, hop2, ..., destination.  When a source-routed packet is sent
  829. by the kernel [at least BSD], the actual destination address becomes irrelevant
  830. because it is replaced with "hop1", "hop1" is removed from the options list,
  831. and all the other addresses in the list are shifted up to fill the hole.  Thus
  832. the outbound packet is sent from your chosen source address to the first
  833. *gateway*, and the options list now contains hop2, ..., destination.  During
  834. all this address shuffling, the kernel does NOT change the pointer value, which
  835. is why it is useful to be able to set the pointer yourself -- you can construct
  836. some really bizarre return paths, and send your traffic fairly directly to the
  837. target but around some larger loop on the way back.  Some Sun kernels seem to
  838. never flip the source-route around if it contains less than three hops, never
  839. reset the pointer anyway, and tries to send the packet [with options containing
  840. a "completed" source route!!] directly back to the source.  This is way broken,
  841. of course.  [Maybe ipforwarding has to be on?  I haven't had an opportunity to
  842. beat on it thoroughly yet.]
  843.  
  844. "Credits" section: The original idea for netcat fell out of a long-standing
  845. desire and fruitless search for a tool resembling it and having the same
  846. features.  After reading some other network code and realizing just how many
  847. cool things about sockets could be controlled by the calling user, I started
  848. on the basics and the rest fell together pretty quickly.  Some port-scanning
  849. ideas were taken from Venema/Farmer's SATAN tool kit, and Pluvius' "pscan"
  850. utility.  Healthy amounts of BSD kernel source were perused in an attempt to
  851. dope out socket options and source-route handling; additional help was obtained
  852. from Dave Borman's telnet sources.  The select loop is loosely based on fairly
  853. well-known code from "rsh" and Richard Stevens' "sock" program [which itself is
  854. sort of a "netcat" with more obscure features], with some more paranoid
  855. sanity-checking thrown in to guard against the distinct likelihood that there
  856. are subtleties about such things I still don't understand.  I found the
  857. argument-hiding method cleanly implemented in Barrett's "deslogin"; reading the
  858. line as input allows greater versatility and is much less prone to cause
  859. bizarre problems than the more common trick of overwriting the argv array.
  860. After the first release, several people contributed portability fixes; they are
  861. credited in generic.h and the Makefile.  Lauren Burka inspired the ascii art
  862. for this revised document.  Dean Gaudet at Wired supplied a precursor to
  863. the hex-dump code, and mudge@l0pht.com originally experimented with and
  864. supplied code for the telnet-options responder.  Outbound "-e <prog>" resulted
  865. from a need to quietly bypass a firewall installation.  Other suggestions and
  866. patches have rolled in for which I am always grateful, but there are only 26
  867. hours per day and a discussion of feature creep near the end of this document.
  868.  
  869. Netcat was written with the Russian railroad in mind -- conservatively built
  870. and solid, but it *will* get you there.  While the coding style is fairly
  871. "tight", I have attempted to present it cleanly [keeping *my* lines under 80
  872. characters, dammit] and put in plenty of comments as to why certain things
  873. are done.  Items I know to be questionable are clearly marked with "XXX".
  874. Source code was made to be modified, but determining where to start is
  875. difficult with some of the tangles of spaghetti code that are out there.
  876. Here are some of the major points I feel are worth mentioning about netcat's
  877. internal design, whether or not you agree with my approach.
  878.  
  879. Except for generic.h, which changes to adapt more platforms, netcat is a single
  880. source file.  This has the distinct advantage of only having to include headers
  881. once and not having to re-declare all my functions in a billion different
  882. places.  I have attempted to contain all the gross who's-got-what-.h-file
  883. things in one small dumping ground.  Functions are placed "dependencies-first",
  884. such that when the compiler runs into the calls later, it already knows the
  885. type and arguments and won't complain.  No function prototyping -- not even the
  886. __P(()) crock -- is used, since it is more portable and a file of this size is
  887. easy enough to check manually.  Each function has a standard-format comment
  888. ahead of it, which is easily found using the regexp " :$".  I freely use gotos.
  889. Loops and if-clauses are made as small and non-nested as possible, and the ends
  890. of same *marked* for clarity [I wish everyone would do this!!].
  891.  
  892. Large structures and buffers are all malloc()ed up on the fly, slightly larger
  893. than the size asked for and zeroed out.  This reduces the chances of damage
  894. from those "end of the buffer" fencepost errors or runaway pointers escaping
  895. off the end.  These things are permanent per run, so nothing needs to be freed
  896. until the program exits.
  897.  
  898. File descriptor zero is always expected to be standard input, even if it is
  899. closed.  If a new network descriptor winds up being zero, a different one is
  900. asked for which will be nonzero, and fd zero is simply left kicking around
  901. for the rest of the run.  Why?  Because everything else assumes that stdin is
  902. always zero and "netfd" is always positive.  This may seem silly, but it was a
  903. lot easier to code.  The new fd is obtained directly as a new socket, because
  904. trying to simply dup() a new fd broke subsequent socket-style use of the new fd
  905. under Solaris' stupid streams handling in the socket library.
  906.  
  907. The catch-all message and error handlers are implemented with an ample list of
  908. phoney arguments to get around various problems with varargs.  Varargs seems
  909. like deliberate obfuscation in the first place, and using it would also
  910. require use of vfprintf() which not all platforms support.  The trailing
  911. sleep in bail() is to allow output to flush, which is sometimes needed if
  912. netcat is already on the other end of a network connection.
  913.  
  914. The reader may notice that the section that does DNS lookups seems much
  915. gnarlier and more confusing than other parts.  This is NOT MY FAULT.  The
  916. sockaddr and hostent abstractions are an abortion that forces the coder to
  917. deal with it.  Then again, a lot of BSD kernel code looks like similar
  918. struct-pointer hell.  I try to straighten it out somewhat by defining my own
  919. HINF structure, containing names, ascii-format IP addresses, and binary IP
  920. addresses.  I fill this structure exactly once per host argument, and squirrel
  921. everything safely away and handy for whatever wants to reference it later.
  922.  
  923. Where many other network apps use the FIONBIO ioctl to set non-blocking I/O
  924. on network sockets, netcat uses straightforward blocking I/O everywhere.
  925. This makes everything very lock-step, relying on the network and filesystem
  926. layers to feed in data when needed.  Data read in is completely written out
  927. before any more is fetched.  This may not be quite the right thing to do under
  928. some OSes that don't do timed select() right, but this remains to be seen.
  929.  
  930. The hexdump routine is written to be as fast as possible, which is why it does
  931. so much work itself instead of just sprintf()ing everything together.  Each
  932. dump line is built into a single buffer and atomically written out using the
  933. lowest level I/O calls.  Further improvements could undoubtedly be made by
  934. using writev() and eliminating all sprintf()s, but it seems to fly right along
  935. as is.  If both exec-a-prog mode and a hexdump file is asked for, the hexdump
  936. flag is deliberately turned off to avoid creating random zero-length files.
  937. Files are opened in "truncate" mode; if you want "append" mode instead, change
  938. the open flags in main().
  939.  
  940. main() may look a bit hairy, but that's only because it has to go down the
  941. argv list and handle multiple ports, random mode, and exit status.  Efforts
  942. have been made to place a minimum of code inside the getopt() loop.  Any real
  943. work is sent off to functions in what is hopefully a straightforward way.
  944.  
  945. Obligatory vendor-bash: If "nc" had become a standard utility years ago,
  946. the commercial vendors would have likely packaged it setuid root and with
  947. -DGAPING_SECURITY_HOLE turned on but not documented.  It is hoped that netcat
  948. will aid people in finding and fixing the no-brainer holes of this sort that
  949. keep appearing, by allowing easier experimentation with the "bare metal" of
  950. the network layer.
  951.  
  952. It could be argued that netcat already has too many features.  I have tried
  953. to avoid "feature creep" by limiting netcat's base functionality only to those
  954. things which are truly relevant to making network connections and the everyday
  955. associated DNS lossage we're used to.  Option switches already have slightly
  956. overloaded functionality.  Random port mode is sort of pushing it.  The
  957. hex-dump feature went in later because it *is* genuinely useful.  The
  958. telnet-responder code *almost* verges on the gratuitous, especially since it
  959. mucks with the data stream, and is left as an optional piece.  Many people have
  960. asked for example "how 'bout adding encryption?" and my response is that such
  961. things should be separate entities that could pipe their data *through* netcat
  962. instead of having their own networking code.  I am therefore not completely
  963. enthusiastic about adding any more features to this thing, although you are
  964. still free to send along any mods you think are useful.
  965.  
  966. Nonetheless, at this point I think of netcat as my tcp/ip swiss army knife,
  967. and the numerous companion programs and scripts to go with it as duct tape.
  968. Duct tape of course has a light side and a dark side and binds the universe
  969. together, and if I wrap enough of it around what I'm trying to accomplish,
  970. it *will* work.  Alternatively, if netcat is a large hammer, there are many
  971. network protocols that are increasingly looking like nails by now...
  972.  
  973. _H* 960320 v1.10 RELEASE -- happy spring!
  974.