跳至主要内容

在 Cygwin 下编译 netcat_1.10-38

我在《Linux下用nc实现 DuplexPipe》中提到,Win 版的 netcat 无法实现 DuplexPipe 的功能。
其实 Cygwin 版的 netcat 也是如此。Cygwin 用的是 netcat 原生的 1.10 版本(项目地址是:http://nc110.sourceforge.net/),该版本没提供 -c 选项。因为我最近在做的项目需要一个这样的工具来帮助测试,于是就决定自己编译 1.10-38 版的 nc。

编译

首先到 debian 下 netcat 的主页下载源代码(http://packages.qa.debian.org/n/netcat.html),测试版已 经到 1.10-39 版本了,我选择稳定版 v1.10-38(http://packages.debian.org/source/stable/netcat)。需要下载两个文件:其中 netcat_1.10.orig.tar.gz 是原生的 nc;netcat_1.10-38.diff.gz 是升级包。
下载完成后先解压:
$ ls
netcat_1.10-38.diff.gz*  netcat_1.10.orig.tar.gz*
$ tar xvf netcat_1.10.orig.tar.gz
...
$ ls
netcat-1.10.orig/  netcat_1.10-38.diff.gz*  netcat_1.10.orig.tar.gz*
接着是释放补丁
$ cd netcat-1.10.orig
$ ls
Changelog  README  generic.h     netcat.c  stupidh*
Makefile   data/   netcat.blurb  scripts/
$ zcat ../netcat_1.10-38.diff.gz | patch -p1
$ ls
Changelog  README  debian/    nc.1          netcat.c  stupidh*
Makefile   data/   generic.h  netcat.blurb  scripts/
补丁被释放到 debian 目录下。然后就是给源代码打补丁了。
$ sed 's#^#patch -Np1 -i debian/patches/#' debian/patches/series | sh
patching file Makefile
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file debian/nc.1
patching file netcat.c
patching file debian/nc.1
patching file netcat.c
patching file debian/nc.1
patching file data/rservice.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file netcat.c
patching file scripts/webproxy
$
最后,为了能使用 -c 和 -e 选项,还要再修改一下 Makefile 文件。将 Makefile 的第 11 行“# DFLAGS = - DTEST -DDEBUG”替换成“DFLAGS = -DDEBIAN_VERSION='"1.10-38"' -DGAPING_SECURITY_HOLE -DIP_TOS -DTELNET”,保存退出。这样,就可以运行“make nc”来编译了。

安装

编译完成后会在当前目录下生成一个“nc.exe”。将它拷贝到“/bin”目录下,并将“nc.1”复制到 “/usr/share/man/man1”目录里。这样就安装完成了。你可以试试运行“nc -h”看看版本号是否正确。
$ nc -h
[v1.10-38]
connect to somewhere:   nc [-options] hostname port[s] [ports] ...
listen for inbound:     nc -l -p port [-options] [hostname] [port]
options:
        -c shell commands       as `-e'; use /bin/sh to exec [dangerous!!]
        -e filename             program to exec after connect [dangerous!!]
        -b                      allow broadcasts
        -g gateway              source-routing hop point[s], up to 8
        -G num                  source-routing pointer: 4, 8, 12, ...
        -h                      this cruft
        -i secs                 delay interval for lines sent, ports scanned
        -k                      set keepalive option on socket
        -l                      listen mode, for inbound connects
        -n                      numeric-only IP addresses, no DNS
        -o file                 hex dump of traffic
        -p port                 local port number
        -r                      randomize local and remote ports
        -q secs                 quit after EOF on stdin and delay of secs
        -s addr                 local source address
        -T tos                  set Type Of Service
        -t                      answer TELNET negotiation
        -u                      UDP mode
        -v                      verbose [use twice to be more verbose]
        -w secs                 timeout for connects and final net reads
        -z                      zero-I/O mode [used for scanning]
port numbers can be individual or ranges: lo-hi [inclusive];
hyphens in port names must be backslash escaped (e.g. 'ftp\-data').

模拟 WireShark

在项目中,我们有时会需要截获客户端和服务端之间发送的消息。WireShark 可以监听本地的通信,可是我觉得它操作比较麻烦。现在,有了 1.10-38 版的 nc,就可以很简单地实现这个功能!
$ nc -l -p 1234 -c 'tee 1234.txt | nc remotehost 1235 | tee 1235.txt'
执行了上面的命令后,再用客户端连接本地的 1234 端口,nc 就会自动去连接远程的 1235 端口。并且客户端发送的消息会保留一份副本到 1234.txt 文件中;服务器反馈的信息则会保存到 1235.txt 中。

评论

此博客中的热门博文

AutoHotKey 新手入门教程

AutoHotKey 真是一个好玩的工具!短短几行代码就是先了“窗口置顶”、“窗口透明”等功能,之前我还特意为此装了好几个小工具,现在都可以卸掉了。闲来无事,就把 Quick Start 翻译了一下,我没有逐字逐句地翻译,有时候我嫌原文罗嗦就用自己的话概括地描述了一下。 原文地址:http://www.autohotkey.com/docs/Tutorial.htm 教程目录 创建脚本 启动程序 模拟鼠标键盘 操纵窗口 输入 变量与剪切板 循环 操纵文件 其他特性 创建脚本 每个脚本都是一个纯文本文件,由一些能被 AutoHotKey.exe 执行的命令组成。一个脚本可能还包含 热键 和 热字符串 。如果没有热键和热字符串,脚本在启动的时候就会从头依次执行到尾。 创建一个新的脚本: 下载 并安装 AutoHotkey。 右击鼠标,选择 新建 -> 文本文档 。 输入文件名并确保以 .ahk 结尾。例如:Test.ahk。 右击文件,选择 编辑脚本 。 输入以下内容:#space::Run www.google.com 上一行的第一个字符 "#" 代表键盘上的 Windows 键;所以 #space 表示在按住 Windows 键后再按空格键。"::" 后面的命令会在热键激活后执行,在本例中则会打开谷歌主页。继续按下面步骤操作,来执行这个脚本: 保存并关闭该文件。 双击该文件来启动它。在系统托盘里会出现一个新图标。 按下 Windows 和空格键,网页会在默认的浏览器里打开。 右击系统托盘里的绿色图标可以退出或编辑当前脚本。 注意: 可以同时启动多个脚本,并且在系统托盘里都会有一个相应的图标。 每个脚本都能定义多个 热键 和 热字符串 。 想让某个脚本开机即启动,可以将它的 快捷方式放到开始菜单的启动目录里 。 启动程序 命令 Run 可以运行程序、打开文档、网页链接或快捷键。请参看以下示例: Run Notepad Run C:\My Documents\Address List.doc Run C:\My Documents\My Shortcut.lnk Run www.yahoo.com Run mailto:someone@somedoma...

JavaScript中的字符串乘法

JavaScript中的字符串乘法 redraiment, Date 原文 原文地址: http://www.davidflanagan.com/2009/08/string-multipli.html 原作者:David Flanagan In Ruby, the "*" operator used with a string on the left and a number on the right does string repetition. "Ruby"*2 evaluates to "RubyRuby", for example. This is only occasionally useful (when creating lines of hyphens for ASCII tables, for example) but it seems kind of neat. And it sure beats having to write a loop and concatenate n copies of a string one at a time--that just seems really inefficient. I just realized that there is a clever way to implement string multiplication in JavaScript: String.prototype.times = function(n) {     return Array.prototype.join.call({length:n+1}, this); }; "js".times(5) // => "jsjsjsjsjs" This method takes advantage of the behavior of the  Array.join()  method for arrays that have undefined elements. But it doesn't even bother creating an array with n+1 undefined ele...

DAO层测试

<dependency> <groupId>com.wix</groupId> <artifactId>wix-embedded-mysql</artifactId> <version>2.1.4</version> <scope>test</scope> </dependency> 利用 wix-embedded-mysql 把MySQL嵌入到进程中,作为内存型的MySQL来做单元测试。 脚本: resources/migrations/mysql/<database>/<timestamp>_<action>.sql 但多个项目需要共享数据库脚本,可能可以用 git submodule 共享。