様々なnetcat
Ubuntu10.10のncコマンド
1)簡単なクライアント/サーバの例
指定されたポート番号でlistenするサーバは以下で起動できます。
$ nc -l <port>
クライアントは以下で起動できます。
$ nc <hostname | IP> <port>
クライアントを起動すると標準入力からの入力待ちとなり、適当な文字を入力してリターンキーを押すと、サーバ側の標準出力に出力されます。
Ctrl+Cで終了し、コネクションが切れると接続先のncコマンドも終了します。サーバは「-k」オプションをつけるとコネクション切断でも終了せず、別の接続を受けられるようになります。ただし、同時には1つのコネクションしか扱えないようです。ひとつのサーバに対して複数のクライアントを起動すると、コネクションは複数確立できるものの、データの送受信は最初のコネクションでしかできず、最初のコネクションが切れると他のコネクションも切れてしまいます。
例
■サーバ側
$ $ nc -k -l 1234
テストデータです <-- クライアントで入力すると出力される
テストデータです2 <-- -kを指定したので続けて接続を受けられる
■ クライアント側
$ nc localhost 1234
テストデータです
^C <-- Ctrl+Cで終了。
$ nc localhost 1234
テストデータです2
^C
2)ファイル転送
ファイル転送はパイプとリダイレクトで以下のようにしてできます。
■サーバ側
$ nc -l 1234 > file_dest.txt
$ cat file_dest.txt
ファイル転送のテストです。 <--たしかに転送できている
■クライアント側
$ cat file_src.txt
ファイル転送のテストです。 <--これを送ります
$ cat file_src.txt | nc localhost 1234
オプションの詳細やその他の例はmanを見てください。ncコマンドを引数なしで起動すると、使用可能なオプションが表示されます。
usage: nc [-46DdhklnrStUuvzC] [-i interval] [-P proxy_username] [-p source_port]
[-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_protocol]
[-x proxy_address[:port]] [hostname] [port[s]]
実はオリジナルのnetcatには「-e prog」という接続後にコマンドを起動するオプションがあります。このオプションがバックドアなどに悪用される場合があるため、最近のncコマンドは無効化されているようです。くれぐれも悪用は厳禁ですが、ためしにインストールしてみました。
オリジナルnetcatコマンド(Hobbit版netcat)
インストール
2)適当なところに展開し、以下のコマンドでメイクします。
make <systype> [options]
systype には、使用するプラットフォームを指定します。 指定可能なものは Makefile をみるとわかりますが、linux や solaris、hpux、freebsd、dosなどいろいろあります。もし該当するものがなければ、generic を試せとREADMEに書いてあります。
また、options には、-DGAPING_SECURITY_HOLE(-eオプションを有効化) -DTELNET(-tオプションを有効化)が指定できます。他にも、デバッグ用の-DTEST -DDEBUGも指定できるようです。
フル機能使えるようにしたいため、以下でメイクしました。
make linux DFLAGS="-DGAPING_SECURITY_HOLE -DTELNET"
以下のエラーがでましたが、netcat.c に以下の行を追加すると、メイクができるようになりました。
undefined reference to `res_init' <----エラーメッセージ
#include <resolv.h> <----netcat.cにこれを追加。
使いかた
以下で利用可能なオプションを表示できます。
$ ./nc -h
[v1.10]
connect to somewhere: nc [-options] hostname port[s] [ports] ...
listen for inbound: nc -l -p port [-options] [hostname] [port]
options:
-e prog program to exec after connect [dangerous!!]
-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
-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
-s addr local source address
-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]
例
以下のようにすると、パスワードなしのtelnetのようなことができる。
■サーバ
$ cd /tmp
$ nc -l -p 1234 -e /bin/bash <-- bashを起動する。
■クライアント
$ nc localhost 1234
pwd <-- 入力するコマンド
/tmp <-- サーバを起動したディレクトリが表示される。
おまけ:GNU netcatのインストール
$ configure -h
`configure' configures this package to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
-h, --help display this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --version display version information and exit
-q, --quiet, --silent do not print `checking...' messages
--cache-file=FILE cache test results in FILE [disabled]
-C, --config-cache alias for `--cache-file=config.cache'
-n, --no-create do not create output files
--srcdir=DIR find the sources in DIR [configure dir or `..']
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc. You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.
For better control, use the options below.
Fine tuning of the installation directories:
--bindir=DIR user executables [EPREFIX/bin]
--sbindir=DIR system admin executables [EPREFIX/sbin]
--libexecdir=DIR program executables [EPREFIX/libexec]
--datadir=DIR read-only architecture-independent data [PREFIX/share]
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
--libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include]
--infodir=DIR info documentation [PREFIX/info]
--mandir=DIR man documentation [PREFIX/man]
Program names:
--program-prefix=PREFIX prepend PREFIX to installed program names
--program-suffix=SUFFIX append SUFFIX to installed program names
--program-transform-name=PROGRAM run sed PROGRAM on installed program names
System types:
--build=BUILD configure for building on BUILD [guessed]
--host=HOST cross-compile to build programs to run on HOST [BUILD]
--target=TARGET configure for building compilers for TARGET [HOST]
Optional Features:
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--disable-dependency-tracking Speeds up one-time builds
--enable-dependency-tracking Do not reject slow dependency extractors
--disable-rpath do not hardcode runtime library paths
--disable-nls do not use Native Language Support
--enable-debug enable debugging (requires GNU compiler, default:
no)
--enable-compat enable all old compatibility switches (default: no)
--enable-oldhexdump use the old style hexdump format (default: no)
--enable-oldtelnet use old telnet codes parsing policy (default: no)
Optional Packages:
--with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
--without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
--with-gnu-ld assume the C compiler uses GNU ld default=no
--with-libiconv-prefix=DIR search for libiconv in DIR/include and DIR/lib
--without-libiconv-prefix don't search for libiconv in includedir and libdir
--with-libintl-prefix=DIR search for libintl in DIR/include and DIR/lib
--without-libintl-prefix don't search for libintl in includedir and libdir
--with-included-getopt use the internal getopt library (default: auto)
Some influential environment variables:
CC C compiler command
CFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
nonstandard directory <lib dir>
CPPFLAGS C/C++ preprocessor flags, e.g. -I<include dir> if you have
headers in a nonstandard directory <include dir>
CPP C preprocessor
Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.
$ configure --prefix=/home/tomonari/Soft/netcat/netcat_gnu --exec-prefix=/home/tomonari/Soft/netcat/netcat_gnu
$ make
$ make install
$ netcat -h
GNU netcat 0.7.1, a rewrite of the famous networking tool.
Basic usages:
connect to somewhere: /home/tomonari/Soft/netcat/netcat_gnu/bin/netcat [options] hostname port [port] ...
listen for inbound: /home/tomonari/Soft/netcat/netcat_gnu/bin/netcat -l -p port [options] [hostname] [port] ...
tunnel to somewhere: /home/tomonari/Soft/netcat/netcat_gnu/bin/netcat -L hostname:port -p port [options]
Mandatory arguments to long options are mandatory for short options too.
Options:
-c, --close close connection on EOF from stdin
-e, --exec=PROGRAM program to exec after connect
-g, --gateway=LIST source-routing hop point[s], up to 8
-G, --pointer=NUM source-routing pointer: 4, 8, 12, ...
-h, --help display this help and exit
-i, --interval=SECS delay interval for lines sent, ports scanned
-l, --listen listen mode, for inbound connects
-L, --tunnel=ADDRESS:PORT forward local port to remote address
-n, --dont-resolve numeric-only IP addresses, no DNS
-o, --output=FILE output hexdump traffic to FILE (implies -x)
-p, --local-port=NUM local port number
-r, --randomize randomize local and remote ports
-s, --source=ADDRESS local source address (ip or hostname)
-t, --tcp TCP mode (default)
-T, --telnet answer using TELNET negotiation
-u, --udp UDP mode
-v, --verbose verbose (use twice to be more verbose)
-V, --version output version information and exit
-x, --hexdump hexdump incoming and outgoing traffic
-w, --wait=SECS timeout for connects and final net reads
-z, --zero zero-I/O mode (used for scanning)
最終更新:2011年02月27日 19:11