ncatの基本的な使い方

提供: セキュリティ
移動: 案内検索
スポンサーリンク

ncat の基本的な使い方を紹介します。ncatは、ネットワークアプリケーションのクライアントと使うことができます。また、ncatを使って、ネットワークアプリケーションではないアプリケーションを、inetdを利用するように簡易的にデーモンのように使うこともできます。ncatとbashのようなシェルを併用することで、リモートシェルを実現できます。sshdすらも不要です。

読み方

ncat
えぬきゃっと、ねっときゃっと

概要

ncat の基本的な使い方を紹介します。

ncat は、2つのうちの1つの基本モードで使用します。

  • コネクトモード
  • listen モード

コネクトモードでは、ncat は、 listen しているサービスへ接続(もしくは UDPデータの送信)を開始します。 socketプログラミングでいえば、コネクトモードは、connectシステムコールを使うのと同じです。

listen モードでは、ncatは、bindとlisten システムコールを使って、接続要求(もしくは、データの到達)を待ちます。 コネクトモードは、「クライアント」モード、 listen モードは、「サーバ」モードであると考えることができます。

コネクトモードでncat を実行する方法は、以下の通りです。

ncat <host> [<port>]

host は、ホスト名かIPアドレス、port には、ポート番号 を指定します。

listen モードは、 --listen オプション(エイリアス -l) を加えます。

ncat --listen [<host>] [<port>]
ncat -l [<host>] [<port>]

listen モードでは、 host は、ncat が listen するアドレスです。 もし、省略すると ncat は、すべてのローカル インターフェース (INADDR_ANY) に対して bind します。 ポート番号を省略した場合、デフォルトでは 31337 ポートを使用します。 一般的に、特権階級ユーザ( root ) は、 1024 以下の ポート番号 を bind します。 listen している TCP サーバーは、通常、1つのコネクションだけを受け付け(accept)、クライアントが切断したときに、終了します。 --keep-open オプションを使うときは、ncat は、コネクションの限界まで、複数の同時接続を受け付けます。--keep-open(もしくは、短縮形の -k)を使用するとき、サーバは、クライアントから送信されるすべてを受け取り、それらのすべてをサーバは、送り返します。 UDP サーバは、1つのクライアント(データを送ってきた最初の1つ)とコミュニケーションします。なぜなら、UDP は、「接続された」クライアントのリストがありません。

デフォルトでは、 ncat は、 TCPIPv4 を使用します。 --udp もしくは -u オプションは、UDP を有効にします。--sctp は、SCTPを有効にし、 -6 は、 IPv6 を有効にします。

コネクトモードの例

ncat (と一般的なネットワークプロトコル)を学ぶよい方法は、ネットワークサービスに接続し、サービスと話をすることです。この場合は、 ncat を使って、ブラウザでウェブページを訪れたときに、ウェブブラウザがバックグラウンドで実行しているように、マニュアルで HTTP サーバーから ウェブページ を取得します。

タイプするのは、ここまで、です。

% ncat -C nmap.org 80
GET / HTTP/1.0

HTTP プロトコルは、改行が2つ必要です。

例 1. ウェブブラウザとしてのncat

% ncat -C nmap.org 80
GET / HTTP/1.0

HTTP/1.1 200 OK
Date: Sat, 15 Jun 2013 06:52:20 GMT
Server: Apache/2.2.15 (CentOS)
Accept-Ranges: bytes
Connection: close
Content-Type: text/html; charset=UTF-8

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Nmap - Free Security Scanner For Network Exploration & Security Audits.</
TITLE>
</HEAD>

省略

</HTML>

-C オプションは、 CRLFLFに置換するオプションです。行末のCRLFを置換します。CRLFの行末は、多くのプロトコルで要求されます。HTTPを含む、多くのサーバーは、プレイン改行(LF)を受け付けます。

listen モードの例

hello.http を下記の内容で作成します。

HTTP/1.0 200 OK
 
Hello, world!

listen モードで ncat のサーバを起動します。

% ncat -l localhost 8080 < hello.http

curl で localhost の 8080 で動作している ncat に問い合わせをします。 ncat からドキュメントが返されます。

% curl http://localhost:8080/
Hello, world!

ncat は、リクエストを表示し、レスポンスを返して、ncat 自信も終了します。

% ncat -l localhost 8080 < hello.http
GET / HTTP/1.1
User-Agent: curl/7.24.0 (i386-portbld-freebsd9.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.7
Host: localhost:8080
Accept: */*

ncatでバックドアのようなリモートシェル

リモートシェルを起動するには、以下の様なコマンドラインを実行します。 -k オプションを指定しないので、一度、接続が切断されると、ncat は終了してしまします。

$ ncat -l -p 2222 -e /bin/sh

listenモードで複数の接続を受け付けるには、-kオプションを指定します。

$ ncat -l -p 2222 -e /bin/sh -k

ncatのリモートシェルに接続するクライアントは、リモートシェル用のポートに接続します。

$ ncat localhost 2222

プロンプトは返ってきませんが、実行したコマンドの出力が表示されます。

$ ncat localhost 2222
uname
FreeBSD
hostname
a1.local

認証をしないでコマンドの実行を許すような使い方は、するべきではありません。

コマンドラインオプション

Ncat 6.40

% ncat -h
Ncat 6.40 ( http://nmap.org/ncat )
Usage: ncat [options] [hostname] [port]
 
Options taking a time assume seconds. Append 'ms' for milliseconds,
's' for seconds, 'm' for minutes, or 'h' for hours (e.g. 500ms).
  -4                         Use IPv4 only
  -6                         Use IPv6 only
  -U, --unixsock             Use Unix domain sockets only
  -C, --crlf                 Use CRLF for EOL sequence
  -c, --sh-exec <command>    Executes the given command via /bin/sh
  -e, --exec <command>       Executes the given command
      --lua-exec <filename>  Executes the given Lua script
  -g hop1[,hop2,...]         Loose source routing hop points (8 max)
  -G <n>                     Loose source routing hop pointer (4, 8, 12, ...)
  -m, --max-conns <n>        Maximum <n> simultaneous connections
  -h, --help                 Display this help screen
  -d, --delay <time>         Wait between read/writes
  -o, --output <filename>    Dump session data to a file
  -x, --hex-dump <filename>  Dump session data as hex to a file
  -i, --idle-timeout <time>  Idle read/write timeout
  -p, --source-port port     Specify source port to use
  -s, --source addr          Specify source address to use (doesn't affect -l)
  -l, --listen               Bind and listen for incoming connections
  -k, --keep-open            Accept multiple connections in listen mode
  -n, --nodns                Do not resolve hostnames via DNS
  -t, --telnet               Answer Telnet negotiations
  -u, --udp                  Use UDP instead of default TCP
      --sctp                 Use SCTP instead of default TCP
  -v, --verbose              Set verbosity level (can be used several times)
  -w, --wait <time>          Connect timeout
      --append-output        Append rather than clobber specified output files
      --send-only            Only send data, ignoring received; quit on EOF
      --recv-only            Only receive data, never send anything
      --allow                Allow only given hosts to connect to Ncat
      --allowfile            A file of hosts allowed to connect to Ncat
      --deny                 Deny given hosts from connecting to Ncat
      --denyfile             A file of hosts denied from connecting to Ncat
      --broker               Enable Ncat's connection brokering mode
      --chat                 Start a simple Ncat chat server
      --proxy <addr[:port]>  Specify address of host to proxy through
      --proxy-type <type>    Specify proxy type ("http" or "socks4")
      --proxy-auth <auth>    Authenticate with HTTP or SOCKS proxy server
      --ssl                  Connect or listen with SSL
      --ssl-cert             Specify SSL certificate file (PEM) for listening
      --ssl-key              Specify SSL private key (PEM) for listening
      --ssl-verify           Verify trust and domain name of certificates
      --ssl-trustfile        PEM file containing trusted SSL certificates
      --version              Display Ncat's version information and exit
 
See the ncat(1) manpage for full options, descriptions and usage examples

Ncat 6.25

% ncat -h
Ncat 6.25 ( http://nmap.org/ncat )
Usage: ncat [options] [hostname] [port]
 
Options taking a time assume seconds. Append 'ms' for milliseconds,
's' for seconds, 'm' for minutes, or 'h' for hours (e.g. 500ms).
  -4                         Use IPv4 only
  -6                         Use IPv6 only
  -U, --unixsock             Use Unix domain sockets only
  -C, --crlf                 Use CRLF for EOL sequence
  -c, --sh-exec <command>    Executes the given command via /bin/sh
  -e, --exec <command>       Executes the given command
  -g hop1[,hop2,...]         Loose source routing hop points (8 max)
  -G <n>                     Loose source routing hop pointer (4, 8, 12, ...)
  -m, --max-conns <n>        Maximum <n> simultaneous connections
  -h, --help                 Display this help screen
  -d, --delay <time>         Wait between read/writes
  -o, --output <filename>    Dump session data to a file
  -x, --hex-dump <filename>  Dump session data as hex to a file
  -i, --idle-timeout <time>  Idle read/write timeout
  -p, --source-port port     Specify source port to use
  -s, --source addr          Specify source address to use (doesn't affect -l)
  -l, --listen               Bind and listen for incoming connections
  -k, --keep-open            Accept multiple connections in listen mode
  -n, --nodns                Do not resolve hostnames via DNS
  -t, --telnet               Answer Telnet negotiations
  -u, --udp                  Use UDP instead of default TCP
      --sctp                 Use SCTP instead of default TCP
  -v, --verbose              Set verbosity level (can be used up to 3 times)
  -w, --wait <time>          Connect timeout
      --append-output        Append rather than clobber specified output files
      --send-only            Only send data, ignoring received; quit on EOF
      --recv-only            Only receive data, never send anything
      --allow                Allow only given hosts to connect to Ncat
      --allowfile            A file of hosts allowed to connect to Ncat
      --deny                 Deny given hosts from connecting to Ncat
      --denyfile             A file of hosts denied from connecting to Ncat
      --broker               Enable Ncat's connection brokering mode
      --chat                 Start a simple Ncat chat server
      --proxy <addr[:port]>  Specify address of host to proxy through
      --proxy-type <type>    Specify proxy type ("http" or "socks4")
      --proxy-auth <auth>    Authenticate with HTTP or SOCKS proxy server
      --ssl                  Connect or listen with SSL
      --ssl-cert             Specify SSL certificate file (PEM) for listening
      --ssl-key              Specify SSL private key (PEM) for listening
      --ssl-verify           Verify trust and domain name of certificates
      --ssl-trustfile        PEM file containing trusted SSL certificates
      --version              Display Ncat's version information and exit
 
See the ncat(1) manpage for full options, descriptions and usage examples

関連項目




スポンサーリンク