<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>[转]Netcat简介 - Rochen - 博客园</title>
<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=H3IOkgdPffpmgIkls4TofKh6PlSWVifjfgEhZr2VHqo1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/BOOK/bundle-BOOK.css?v=qzznqq7AzRkoka6GRibxinoTgveiXem5TPZDQ_eQWN41"/>
<link id="mobile-style" media="only screen and (max-width: 767px)" type="text/css" rel="stylesheet" href="/skins/BOOK/bundle-BOOK-mobile.css?v=2kvKmvRY2V7A8icv7UeoLCg33YxjDCYf0xhrzgD2C1k1"/>
<link title="RSS" type="application/rss+xml" rel="alternate" href="https://www.cnblogs.com/LaplaceDemon/rss"/>
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="https://www.cnblogs.com/LaplaceDemon/rsd.xml"/>
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="https://www.cnblogs.com/LaplaceDemon/wlwmanifest.xml"/>
<script src="//common.cnblogs.com/script/jquery.js" type="text/javascript"></script>  
<script type="text/javascript">var currentBlogApp = 'LaplaceDemon', cb_enable_mathjax=false;var isLogined=true;</script>
<script src="/bundles/blog-common.js?v=Gjc-c4WOecxW2V4mwH9-EN2lPzwMWd4wyAzo535GvA01" type="text/javascript"></script>
</head>
<body>
<a name="top"></a>

<script type="text/javascript">
	try {
		if (screen.availWidth > 1200) {
			document.getElementById("MainCss").href = '/Skins/BOOK/style2.css';
		}
	} catch (e) { }
</script>

<div id="header">
<div id="HeaderTitle">
<div id="Title">
<a id="Header1_HeaderTitle" class="headermaintitle" href="https://www.cnblogs.com/LaplaceDemon/">循道</a>
</div>
<div id="subTitle">万事万物皆为道，漫漫红尘皆失道；上下求索为吾道，环宇顾宙吾何道</div>
</div>
</div>

<div id="main">
	
<div id="post_detail">
	<div class="post">
		<h2>
			<a id="cb_post_title_url" href="https://www.cnblogs.com/LaplaceDemon/archive/2010/08/26/1809458.html">[转]Netcat简介</a>
		</h2>
		<div id="cnblogs_post_body" class="blogpost-body"><p>&nbsp;NetCat是一个非常简单的Unix工具，可以读、写TCP或UDP网络连接(network <br />connection)。它被设计成一个可靠的后端(back-end) 工具，能被其它的程序<br />程序或脚本直接地或容易地驱动。同时，它又是一个功能丰富的网络调试和开<br />发工具，因为它可以建立你可能用到的几乎任何类型的连接，以及一些非常有<br />意思的内建功能。NetCat，它的实际可运行的名字叫nc，应该早很就被提供，<br />就象另一个没有公开但是标准的Unix工具。<br />　　GNU也有一个netcat项目，但此处学习的不是GNU的那个。&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp; 最简单的使用方法，&rdquo;nc host port&rdquo;，能建立一个TCP连接，连向指定的<br />主机和端口。接下来，你的从标准输入中输入的任何内容都会被发送到指定的<br />主机，任何通过连接返回来的信息都被显示在你的标准输出上。这个连接会一<br />直持续下去，至到连接两端的程序关闭连接。注意，这种行为不同于大多数网<br />络程序，它们会在从标准输入读到一个文件结束符后退出。<br />　　NetCat还可以当服务器使用，监听任意指定端口的连接请求(inbound <br />connection )，并可做同样的读写操作。除了较小限制外，它实际并不关心自<br />己以&ldquo;客户端&rdquo;模式还是&ldquo;服务器&rdquo;模式运行，它都会来回运送全部数据。在任何<br />一种模式下，都可以设置一个非活动时间来强行关闭连接。<br />　　它还可以通过UDP来完成这些功能，因此它就象一个telnet那样的UDP程序，<br />用来测试你的UDP服务器。正如它的&ldquo;U&rdquo;所指的，UDP跟TCP相比是一种不可靠的<br />数据传输，一些系统在使用UDP 传送大量数据时会遇到麻烦，但它还有一些用<br />途。<br />　　你可能会问&ldquo;为什么不用telnet来连接任意的端口&rdquo;？问题提得好(valid)，<br />这儿有一些理由。Telnet有&ldquo;标准输入文件结束符(standard input EOF)&rdquo;问题，<br />所以需要在脚本中延迟计算以便等待网络输出结束。这就是netcat持续运行直<br />到连接被关闭的主要原因。Telnet也不能传输任意的二进制数据，因为一些特<br />定的字符会被解释为Telnet的参数而被从数据流中去除。Telnet还将它的一些<br />诊断信息显示到标准输出上，而NetCat会将这信息与它的输出分开以不改变真<br />实数据的传输，除非你要求它这么做。当然了，Telnet也不能监听端口，也不<br />能使用UDP。 NetCat没有这些限制，比Telnet更小巧和快捷，而且还有一些其<br />它的功能。<br /><br />　　NetCat的一些主要功能：<br /><br />*支持连出和连入(outbound and inbound connection)，TCP和UDP，任意源和目的端口<br />*全部DNS正向/反向检查，给出恰当的警告<br />*使用任何源端口<br />*使用任何本地设置的网络资源地址<br />*内建端口扫描功能，带有随机数发生器<br />*内建loose source-routing功能<br />*可能标准输入读取命令行参数<br />*慢发送模式，每N秒发送一行<br />*以16进制显示传送或接收的数据<br />*允许其它程序服务建立连接，可选<br />*对Telnet应答，可选<br /><br />编译NetCat<br />==========<br />　　编译NetCat是非常简单的。检查一下Makefile，找到符合你的系统类型的<br />SYSTYPE如何拼写,然后运行&ldquo;make&rdquo;，然后可执行的nc就会出现了。如果没有合<br />适的SYSTYPE，用&rdquo;generic&rdquo;试试。<br />　　其Makefile中有dos, ultrix, sunos, solaris-static, solaris, aix, <br />linux, irix, osf, freebsd, bsdi, netbsd, hpux, unixware, aux, next, <br />generic等SYSTYPE，其中generic不算系统类型，则dos其实并不支持。在本文<br />一开始的NetCat的链接页面中，也有一个Windows 版本的NetCat，是另一个人<br />做的移植。<br />　　Linux的sys/time.h并不真正支持FD_SETSIZE的 表示，编译时会有一个无<br />害的警告。在一些系统中编译时，可能会与signal()有关的指针类型警告，但<br />不影响编译结果。<br /><br />开发NetCat的功能<br />===============<br />　　NetCat小巧且功能强大，描述它的功能就是象描述瑞士军刀的功能一样。<br />　　如果没有提供命令行参数，NetCat会提示你从标准输入来输入命令参数，<br />然后NetCat会在内部解析输入。用这种办法输入命令式参数，可以用来防止借<br />助&ldquo;ps&rdquo;来查看你的命令行参数。<br />　　主机参数可以是一个名字或一个IP地址。如果-n出现，则它接受IP地址，<br />而不再对计算机的名字或域名进行解析。如果没有-n，但加上-v，则NetCat可<br />进行正/反向域名解析，并警告the all-too-common problem of mismatched <br />name in DNS 。这会耗费稍多一点时间，但在某些情况下会有用处。如，你想<br />知道某个IP的主机名，NetCat可省却你手工查找的时间。<br />　　要建立对外的连接，必须提供一个端口号，可以是个数字，也可以<br />/etc/services列表中的端口服务名。当-n 出现时，则只有数字形式的端口可<br />以接收。<br />　　-v参数，可以将一些关于连接建立信息输出到标准错误。-v参数多出现几<br />次，则显示的信息会更多一些。如果-v参数没有出现，则NetCat将默默地工作，<br />至到出现错误为止。<br />　　-w参数后跟一个时间值，用以指定建立链接时的等待时间，-w如果多次出<br />现，则后面的值将取代前面的设置。-w还用来设置连接非活动时间，当标准输<br />入结束以后，如果等待指定的一段时间后仍没有数据返回，则NetCat会再试一<br />次，然后关闭连接并退出。<br />　　当-u参数出现时，用UDP建立连接。<br />　　用-o logfile参数，可以将连接上往来传输的数据以16进制的形式记录到<br />logfile中（每行的左半部分是16进制显示，右半部分为ascii显示）。其中，<br />每行的第一个字符为&rdquo;&lt;&rdquo;或&rdquo;&gt;&rdquo;，分别表示接收的数据或发送的数据。<br />　　NetCat用-s ip-addr或-s name来绑定本地网络资源地址，-p portarg 来<br />绑定本地端口。除了因权限限制或端口已经使用外，-p可以绑定任何端口。 <br />Root用户可以绑定保留的1024以内的端口。如果不用-p指定端口，则使用系统<br />给定的未使用的端口。(-p功能在客户端状态也可以使用,-s功能并不是在所有<br />的平台上都可用)<br />　　-l参数可以使NetCat以服务器状态运行。<br />&rdquo;nc -l -p 1234 [remote hostname] [remote port]&rdquo;可以用来指定入连的主机<br />和端口，如果申请连接的主机或端口不符指定，则会断开连接。<br />　　当编译时置-DGAPING_SECURITY_HOLE，则-e参数被NetCat支持。-e后面跟<br />一可执行程序的名称，当一个连接（入或出）被建立时，这个程序被运行。尤<br />其当NetCat以服务器端运行时，-e参数使其有点象inetd了， 只是只能运行一<br />个进行而已。需要说明的是，-e后的程序不能从NetCat的命令行接收参数，如<br />果有参数要传递，可能需要一个脚本。<br />　　当编译时置-DTELNET，则-t参数被支持，此时NetCat可以登录到一个<br />telnetd服务器，并提供相关的握手应答，至到出现登录提示符。<br />　　NetCat用8k的读写，来尽可能高效将收到数据显示到标准输出上及将标准<br />输入写到连接上。-i参数，可以用来设置发送一行标准输入信息的间隔，以减<br />少发送速度。<br />　　端口扫描是一探测主机服务的流行方法。NetCat的命令行中，先是参数，<br />再是主机，最后是端口。端口可以是一些服务名、端口号，或者是一个端口范<br />围（形如N-M）。<br />&nbsp;&nbsp;&nbsp; &rdquo;nc -v -w 2 -z -i 1 20-30&rdquo;用来扫描target主机的20-30(两端包含)端口，<br />-z表示不发送任何数据到TCP连接或非常有限的数据到UDP连接。-i用以指明两<br />个端口建立连接的时间的间隔。-w用以指明连接不活动时间。通常情况下，扫<br />描按从高到低的顺序依次扫描指定的端口，-r参数可以让NetCat在指定的端口<br />范围内随机地扫描端口。（当-r被用于单个连接时，本地的端口在8192以上，<br />除非用-p指定)<br />　　-g可以用来指定网关（最多可达8个），-G可以用来指定source-routing <br />pointer。(这是原文，但我还是不明白。：（-g =&gt; Group hops <br />Many people are interested in testing network connectivity using IP <br />source routing, even if it's only to make sure their own firewalls <br />are blocking source-routed packets. On systems that support it, the<br />-g switch can be used multiple times [up to 8] to construct a <br />loose-source-routed path for your connection, and the -G argument <br />positions the ``hop pointer'' within the list. If your network <br />allows source-routed traffic in and out, you can test connectivity <br />to your own services via remote points in the internet. Note that <br />although newer BSD-flavor telnets also have source-routing <br />capability, it isn't clearly documented and the command syntax is <br />somewhat clumsy. Netcat's handling of ``-g'' is modeled after<br />``traceroute''.）<br />　　NetCat不是一个任意包发生器，但可以与raw socket通话，nit/bpf/dlpi<br />有时也能行( nit/bpf/dlpi may appear at some point).推荐Drren Reed的<br />ip_filter包，里面有一个工具能创建并发送raw packets.<br /><br />netcat可以作为类似于telent的客户端,也可以监听某个端口作为服务器,还可<br />以作为扫描工具扫描对方主机的端口,还可以用来传输文件,不相信吗? 听我慢<br />慢道来:)<br />首先我们要弄明白netcat的工作原理,其实netcat的原理很简单,它就是从网络<br />的一端读入数据,然后输出到网络的另一端,它可以使用tcp和udp协议. 之所以<br />叫做netcat,因为它是网络上的cat,想象一下cat的功能,读出一个文件的内容,<br />然后输出到屏幕上(默认的stdout是屏幕,当然可以重定向到其他地方).netcat<br />也是如此,它读取一端的输入,然后传送到网络的另一端,<br />就这么简单.但是千万不要小看了它,netcat可以完成很多任务,,尤其是和其他<br />程序组合时.好了,废话少说,进入正题吧.:p <br />网上有两种版本的netcat,一个是@stake公司的netcat,<br />https://www.atstake.com/research/tools/network_utilities/&nbsp; 也就是最初<br />的版本,还有一个是GNU的netcat.https://netcat.sourceforge.net/download.php <br />我个人更倾向于使用GNU的netcat,因为它的功能更多,不过GNU的没有windows <br />平台的版本:confused: <br />至于编译和安装我想就不用说了,如果这关都过不了,我想也有点太&hellip;&hellip;，看看<br />readme和install文件，一般情况下./configure&amp;&amp;make&amp;&amp;make install就ok了，<br />具体的./configure选项看看帮助。<br /><br />netcat的命令行程序名字为nc,是netcat的缩写,安装完了是找不到netcat这个<br />程序的.:)<br /><br />root@mail etc #nc -h<br />GNU netcat 0.7.0, a rewrite of the famous networking tool.<br />Basic usages:<br />connect to somewhere: nc [options] hostname port [port] ...<br />listen for inbound: nc -l -p port [options] [hostname] [port] ...<br />tunnel to somewhere: nc -L hostname:port -p port [options]<br /><br />Mandatory arguments to long options are mandatory for short options<br />too.<br />Options:<br />-c, --close close connection on EOF from stdin<br />-e, --exec=PROGRAM program to exec after connect<br />-g, --gateway=LIST source-routing hop point[s], up to 8<br />-G, --pointer=NUM source-routing pointer: 4, 8, 12, ...<br />-h, --help display this help and exit<br />-i, --interval=SECS delay interval for lines sent, ports scanned<br />-l, --listen listen mode, for inbound connects<br />-L, --tunnel=ADDRESS:PORT forward local port to remote address<br />-n, --dont-resolve numeric-only IP addresses, no DNS<br />-o, --output=FILE output hexdump traffic to FILE (implies -x)<br />-p, --local-port=NUM local port number<br />-r, --randomize randomize local and remote ports<br />-s, --source=ADDRESS local source address (ip or hostname)<br />-t, --tcp TCP mode (default)<br />-T, --telnet answer using TELNET negotiation<br />-u, --udp UDP mode<br />-v, --verbose verbose (use twice to be more verbose)<br />-V, --version output version information and exit<br />-x, --hexdump hexdump incoming and outgoing traffic<br />-w, --wait=SECS timeout for connects and final net reads<br />-z, --zero zero-I/O mode (used for scanning)<br /><br />Remote port number can also be specified as range. Example: '1-1024'<br /><br />我用的是GNU的netcat,比起@stake公司的netcat多了-c 选项,不过这是很有用<br />的一个选项,后面我们会讲到.还有GNU的-L,-t ,-T选项和@stake的-L -t 用途<br />是不一样的,自己琢磨吧.<br /><br />一.客户端<br />这是最简单的使用方式,nc <br />nc www.apache.org 80<br />get / http/1.1<br />HTTP/1.1 400 Bad Request<br />Date: Mon, 08 Dec 2003 06:23:31 GMT<br />Server: Apache/2.0.48-dev (Unix)<br />Content-Length: 310<br />Connection: close<br />Content-Type: text/html; charset=iso-8859-1<br /><br /><br />400 Bad Request<br /><br />Bad Request<br />Your browser sent a request that this server could not understand.<br /><br /><br />Apache/2.0.48-dev (Unix) Server at www.apache.org Port 80<br /><br />呵呵,看到了什么,我什么也没说哦:p<br /><br />二.简单服务器<br />nc -l -p //这里-l参数表明nc处于监听模式,-p指定端口号.<br />nc -l -p 1234[假设这台主机ip为192.168.0.1]<br />然后从客户端输入, nc 192.168.0.1 1234 然后你从任一端输入的数据就会显<br />示在另一端了.其实netcat的server和client的区别并不大,区别仅仅在于谁执<br />行了-l来监听端口,一旦连接建立以后,就没有什么区别了. 从这里我们也可以<br />了解netcat的工作原理了,通过网络链接读写数据.[It is a simple Unix <br />utility which reads and writes data across network connections, <br />using TCP or UDP protocol]--@stake主页是这么说的.<br /><br />三.telnet服务器<br />nc有一个-e的选项,用来指定在连接后执行的程序.<br />在windows平台上可以指定-e cmd.exe[winxp,win2000,] 如果是98就指定<br />command.exe.linux则指定-e bash,或者任何你喜欢的shell, 或者是你自己编<br />写的程序,通常是做为后门:p<br />指定-e的效果是由你指定的程序代替了nc自己来接受另一端的输入,并把输入<br />(命令)后反馈的结果显示到另一端.<br />server: nc -l -p 1234 -e bash<br />client: nc 192.168.0.1 1234 就可以远程登陆server了<br />其实我们不一定非要在server端指定-e,也可以在client端指定.<br />server: nc -l -p 1234<br />client: nc -e 192.168.0.1 1234 .这样,就相当于在server上远程登陆client<br />了.我前面说过,有关client和server的区分是没有什么意义的.谁做为telnet <br />server的标准只有一个,谁执行了-e [shell].<br /><br />四.ftp服务器<br />nc可以从任何地方接受输入,不仅仅是-e指定的程序,还可以是文件; nc可以将<br />输入重定向到任何地方,不仅仅是默认的屏幕.指定的方法很简单,使用 &gt; 和<br />somefile<br />例2; server: nc -l -c -p 1234 &gt;somefile<br />client: nc 192.168.0.1 1234 /check/host.disk1<br />然后,可以利用linux内核的loopback特性,把host.disk以只读的方式mount上,<br />然后就可以做取证分析了.<br />[如果真的做取证分析,一定不要在原始的受害主机硬盘上find和类似的操作,<br />因为这会修改时间标记而破坏原始的证据]<br /><br /><br />例4. 将文件压缩后再传送.<br />如果你的文件很大,何不先压缩它呢,利用管道, 我们甚至不用生成压缩后的中<br />间文件!<br />源主机: tar czf - work|nc -l -c -p 1234 <br />目的主机: nc 192.168.0.1 1234|tar xzvf -<br /><br />[tar打包时最好不要使用绝对路径,虽然GNU的tar能把它转换成相对路径,但不<br />是所有的平台都能做到,所以如果不想把你的文件系统搞乱的话,就使用相对路<br />径吧!]<br /><br /><br />&hellip;&hellip;&hellip;&hellip;<br /><br />这里的server和client的组合是可以互换的，以第六节中例1来说明：<br />你也可以这样：<br />干净主机： nc -l -p 1234 -c |md5sum -c -|grep -v OK<br />被入侵主机：find etc -type f|xargs md5sum|nc 192.168.0.2 1234</p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory"></div>
<div id="EntryTag"></div>
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div>


		<p class="postfoot">
			posted on <span id="post-date">2010-08-26 20:11</span> <a href='https://www.cnblogs.com/LaplaceDemon/'>Rochen</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>)  <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=1809458" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(1809458);return false;">收藏</a>
		</p>
	</div>
	<script type="text/javascript">var allowComments=true,cb_blogId=63067,cb_entryId=1809458,cb_blogApp=currentBlogApp,cb_blogUserGuid='b58ab2ba-bdd2-de11-ba8f-001cf0cd104b',cb_entryCreatedDate='2010/8/26 20:11:00';loadViewCount(cb_entryId);var cb_postType=1;</script>
	
	</div><a name="!comments"></a><div id="blog-comments-placeholder"></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id='comment_form' class='commentform'>
<a name='commentform'></a>
<div id='divCommentShow'></div>
<div id='comment_nav'><span id='span_refresh_tips'></span><a href='javascript:void(0);' onclick='return RefreshCommentList();' id='lnk_RefreshComments' runat='server' clientidmode='Static'>刷新评论</a><a href='#' onclick='return RefreshPage();'>刷新页面</a><a href='#top'>返回顶部</a></div>
<div id='comment_form_container'></div>
<div class='ad_text_commentbox' id='ad_text_under_commentbox'></div>
<div id='ad_t2'></div>
<div id='opt_under_post'></div>
<div id='cnblogs_c1' class='c_ad_block'></div>
<div id='under_post_news'></div>
<div id='cnblogs_c2' class='c_ad_block'></div>
<div id='under_post_kb'></div>
<div id='HistoryToday' class='c_ad_block'></div>
<script type='text/javascript'>
    fixPostBody();
    setTimeout(function () { incrementViewCount(cb_entryId); }, 50);
    deliverAdT2();
    deliverAdC1();
    deliverAdC2();    
    loadNewsAndKb();
    loadBlogSignature();
    LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
    GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate, cb_postType);
    loadOptUnderPost();
    GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);   
</script>
</div>


	
<p id="footer">
	Powered by: 
	<br />
	
	<a id="Footer1_Hyperlink3" NAME="Hyperlink1" href="https://www.cnblogs.com/" style="font-family:Verdana;font-size:12px;">博客园</a>
	<br />
	Copyright &copy; Rochen
</p>
</div>
<div id="rightmenu">
	
		<div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script>
		
<h3>导航</h3>
<ul>
			<li><a id="blog_nav_sitehome" href="https://www.cnblogs.com/">博客园</a></li>
			<li><a id="blog_nav_myhome" href="https://www.cnblogs.com/LaplaceDemon/">首页</a></li>
			<li><a id="blog_nav_newpost" rel="nofollow" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li>
			<li><a id="blog_nav_contact" accesskey="9" rel="nofollow" href="https://msg.cnblogs.com/send/Rochen">联系</a></li>
			<li><a id="blog_nav_rss" href="https://www.cnblogs.com/LaplaceDemon/rss">订阅</a><a id="blog_nav_rss_image" href="https://www.cnblogs.com/LaplaceDemon/rss"><img src="//www.cnblogs.com/images/xml.gif" alt="订阅" /></a>
			<li><a id="blog_nav_admin" rel="nofollow" href="https://i.cnblogs.com/">管理</a></li>
</ul>
		<div id="blog_stats">
<h3>统计</h3>
	<ul>
		<li>随笔 - 16
		<li>文章 - 0
		<li>评论 - 3
		<li>引用 - 0
	</li>
</ul></div>
		
<h3>公告</h3>
	<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>

		<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
	
</div>
			
			
			
			
			 

	

</body>
</html>