4 ms·
Netcat – All you need to know
- sprash 5y agoBash has a /dev/tcp/$host/$port pseudo device which covers almost all use cases of Netcat just in case your machine has no Netcat installed. I still don't understand why such a device/file is not shipped with Unix/Linux by default so it would work with any shell or program.
- zaik 5y agoAre you sure this is a Bash feature and not some kernel module?
- FreeFull 5y agoIt definitely is a bash feature. From the bash manpage: Bash handles several filenames specially when they are used in redirections, as described in the following table. If the operating system on which bash is running provides these spe‐ cial files, bash will use them; otherwise it will emulate them internally with the behavior described below. /dev/fd/fd If fd is a valid integer, file descriptor fd is duplicated. /dev/stdin File descriptor 0 is duplicated. /dev/stdout File descriptor 1 is duplicated. /dev/stderr File descriptor 2 is duplicated. /dev/tcp/host/port If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open the corresponding TCP socket. /dev/udp/host/port If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open the corresponding UDP socket.
- gnu8 5y agoIt just seems confusing that bash is using the /dev namespace for this feature. Are there circumstances where /dev/tcp/host/port might be provided by the operating system and provide the same functionality? Or is that just saying bash will stay out of the way if you are trying to access special files in /dev if they really exist?
- dpedu 5y agoUnless the operating system decides to use bash to access some device, it's fine. Nothing besides bash can see these. They're not really there. It's a mirage created by bash.
- FreeFull 5y ago/dev/std{in,out,err} are provided by Linux, and bash does stay out of the way there. I'm not aware of anything that provides /dev/tcp/host/port, although I think Plan 9 might have something similar. If the file happens to exist, bash will use the file rather than its own special functionality.
- zamadatix 5y agoThe old TLI/XTI alternatives to BSD sockets (pre when BSD sockets were standardized into POSIX sockets) back in the System V days provided this path/socket connection format. For a modern holdover these are still available in Solaris.
- tyingq 5y agoGood for clients, but bash can't do a listen socket. Gawk can, though it's pretty limited.
- AnIdiotOnTheNet 5y agoBecause Linux didn't copy everything from Plan9. Unfortunately.
- yjftsjthsd-h 5y agoI wonder how hard it would be to write kernel modules to fix that.
- anthk 5y agoaux/listen on Linux world? What's next? purging NFS for 9p and wireguard?
- tyingq 5y agoWSL uses 9p, though it seems to be a bit slow in that area.
- dec0dedab0de 5y agonmap not only comes with ncat, but with nping, which I used to find incredibly useful for troubleshooting when I was a network engineer. Especially with load balancing, wccp, and other things that change routes based on port. I think it was based on a program called hping, but I haven't really followed that world in a while.
- sigmonsays 5y agono mention of socat?
- cowmix 5y agoYeah, socat is my go-to for tcp socket scripting.
- jaytaylor 5y agoDon't forget the venerable websocat for testing and scripting websockets! https://github.com/vi/websocat https://github.com/vi/websocat Such a useful tool when the need arises, lifesaver.
- linsomniac 5y agoIndeed. Nor any mention of the busybox netcat. busybox, a great program, but something about it's netcat will hang at the end of sending data, rather than closing the socket and exiting. Because of that I have to ^C it, and then wonder if it fully sent the last block of data. For decades now I've used minimal rescue environments and "netcat" to copy data around. A lot of these rescue environments, especially 20 years ago, were pretty minimal. I liked being able to use the RedHat or Fedora install media as rescue, because I always had it handy. So I fell back to this habit of, I know Python is on this system (RedHat installer), so I made a few versions of a python "netcat" program. A bigger one with more features, in case I was able to "wget" a file. Or smaller "in" and "out" programs that I could comfortably type in by hand. But I really need to get into the habit of using socat now. I had it in mind that it's hard to remember how to use, so I'd just wget my python version and be done with it. But I need to get in the habit. socat is SO powerful. Beware, Ubuntu 20.04 has version with a bug in TLS-based "file copy" socat usage. https://bugs.launchpad.net/ubuntu/+source/socat/+bug/1936407 https://bugs.launchpad.net/ubuntu/+source/socat/+bug/1936407 Simple fix is to just "dpkg -i" the 18.04 or 21.04 .deb file, there don't seem to be any strange dependencies.
- btschaegg 5y agoThis. So much this. I mean, netcat as a tool still isn't as known as it deserves to be (at least in the circles I frequent), but socat deserves it even more. Along with a few other unix-y tools (sponge, for example), I really cannot understand why it isn't pre-installed everywhere.
- tambourine_man 5y agoMore importantly, is that a figlet font simulating dripping ink? Cause it's freaking cool.
- sgt 5y agoNeed to include that into my next command line tool.
- tambourine_man 5y agoTo future googlers, the font is Bloody.flf Get it here: https://github.com/xero/figlet-fonts https://github.com/xero/figlet-fonts
- thayne 5y ago> Netcat having being initially written to be used on linux the variants are linux based uh, what? the original netcat was a Unix program, not linux specefic. I don't think GNU netcat is linux specific either. And the OpenBSD variant, as its name suggests was originally written for OpenBSD, although it was ported to Linux later.
- tyingq 5y agoYeah, that was funny to me too. Older makefiles had targets for Ultrix, Nextstep, and even Unixware :) https://sourceforge.net/p/nc110/git/ci/v1.10/tree/Makefile https://sourceforge.net/p/nc110/git/ci/v1.10/tree/Makefile
- dsr_ 5y agoHobbit wrote netcat in 1995: https://seclists.org/bugtraq/1995/Oct/28 https://seclists.org/bugtraq/1995/Oct/28
- chasil 5y agoThere is also a netcat inside busybox, and the Windows port from frippery.org implements it. The busybox netcat doesn't set off all the malware alarms on my corporate desktop. C:\Temp>\bin\busybox64 nc 1.2.3.4 21 220 (vsFTPd 2.0.5) quit 221 Goodbye. The Windows port is not very smart compared to the rest of the netcat family (the Linux x86-64 version has more of the common options). C:\Temp>\bin\busybox64 nc BusyBox v1.32.0-FRP-3445-g10e14d5eb (2020-04-11 10:50:47 BST) multi-call binary Usage: nc [-l] [-p PORT] [IPADDR PORT] Open a pipe to IP:PORT -l Listen mode, for inbound connects -p PORT Local port
- totetsu 5y agoI think nc works out of the box in MacOS also.
- coldtea 5y agoI think they just wrote Linux to substitute for UNIX in general, as the only UNIX (derivative) that matters. For younger devs, it's all Linux (vs Windows/MacOS/etc). UNIX and its variants is some distant history, like Multics or VMS was to 90s devs. Even FreeBSD is some niche thing they'll seldom, if ever, encounter (much less install). Heck, 20-something year old devs still hadn't reach puberty when SUN still mattered and was a thing.
- GekkePrutser 5y ago[OpenBSD Netcat] "In addition to those enhancements it is compiled to remove a feature that is considered a gaping security hole of the application." I wonder what feature this was? The site doesn't elaborate. Edit: Ah I saw the thing about the -e later on but I didn't think that was it as indeed it's not really a security hole imo. Rather just a tool.
- jfrunyon 5y ago-c and -e. They're not security holes, they're useful features which like most powerful tools can be misused or abused.
- GekkePrutser 5y agoAh yeah it was the security hole description that threw me off. I was expecting something more along the lines of a buffer overflow.
- mct 5y agoThe post does a poor job explaining it, but the term "gaping security hole" comes from traditional netcat's source code. In order to enable the "-e" (exec) flag, it must be compiled with "-DGAPING_SECURITY_HOLE".
- hddqsb 5y agoI believe the reason they are called "gaping security holes" is that if nc is installed as setuid root, they allow local privilege escalation (see https://serverfault.com/questions/237584/netcat-e-the-gaping-security-hole https://serverfault.com/questions/237584/netcat-e-the-gaping..., https://nc110.sourceforge.io/ https://nc110.sourceforge.io/). Another explanation is that they make it trivial to create reverse shells etc. (though it is still possible to create reverse shells without -e/-c, for example using named pipes).
- naikrovek 5y agoThe article definitely did elaborate, but not as much as I would have liked. The feature is the -e option, which is used (among other things) for reverse shells.
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- userbinator 5y agoBesides quickly transferring files, which is mentioned in the article, one of the most useful things I've done with netcat was when the company's cloud-based IM+email service went down and everyone in the office couldn't send messages to each other despite being in the same network. After some brief in-person discussion we used netcats for IM, and it worked quite well.
- ma2rten 5y agoWouldn't it have been easier to use another cloud-based service such as Slack?
- 1vuio0pswjnm7 5y agoNo, because netcat requires no signup, no "support", no "updates", no license, no fees. It is not closed source and there is no third party company such as "Slack" that needs to be involved in private communication. Netcat is a small, fast, open source program that is easy to compile on any UNIX-like OS. Whereas IRC, and presumably Slack, requires at least two programs, a server and a client, netcat requires only one.
- bavell 5y agoHow timely. Just today I had to install netcat on my new workstation (I chose the gnu version) in order to test connectivity to an SMTP server. It was super helpful just to see the raw response from the server. Apparently GoDaddy blocks all outbound 3rd party SMTP connections... ugh.
- stevekemp 5y agonetcat is a very useful tool, but for SMTP testing you might enjoy "swaks": http://web.archive.org/web/20180304190350/https://debian-administration.org/article/633/Testing_SMTP_servers_with_SWAKS http://web.archive.org/web/20180304190350/https://debian-adm...
- xiconfjs 5y agooh swaks seems nice. Thanks
- mastrsushi 5y agoNever trust something foolishly titled “All you need to know”