nmap と lua

アインシュタインの影

やっと、「アインシュタインの影」と言う、電波天文学の本を読み終えた。

銀河の遥か彼方にある★を観測したい、そうすれば宇宙の成り立ちやこの世の原理が分かるんじゃなかろうか? 知りたいという欲求に突き動かされた天文学者の物語(の中間報告)。

遠くの物を観測するには、倍率が高い望遠鏡を用意しなければならない。しかも観測したいのは、銀河の中心に位置するブラックホール。地球とそのブラックホールの間にはごみが漂っているので、可視光ではどんなに倍率が高くても見えない。そこで光族の一種である電波をつかって観測。電波なら、携帯とかでお馴染みで、物体をすり抜けて届く波長帯がある。

大きな反射鏡で電波を集める。それの究極な形は、地球のあちこちにある電波望遠鏡での同時観測。望遠鏡間の距離が大きければ大きい程、口径の大きい電波望遠鏡として扱える。

後は、集めた電波をコンピュータにかけて、合成するしくみ。世界各国の協力が必要。また、資金集めも必要。苦悩する天文学者。政治的な駆け引きもある。大変な世界。 超精密は原子時計が3000万円するとか、それをそれぞれの天文台に配る。金がかかるのも頷けるよ。

なんとかチャンスに恵まれて、本命のいて座A*と言うブラックホールは逃したが(予定日にメキシコの天気が悪くてディッシュを回せなかったとか)、他のブラックホールの撮影に成功。2019年4月に発表があった。

史上初、ブラックホールの撮影に成功

たくさんのアンテナをつないでひとつに「電波干渉計」

nmap

前回の続きでnmapします。何と言っても、ETHな望遠鏡には負けるけど、ネット上のサーバーの観測装置ですからね。前回もnew TVにブラックホールを発見したし。。。

nmap with gdb

OpenBSDでもgdbしたいと思って、sourceから入れてみる事にした。そしたら、configure中に、yaccは機能不足だからbisonを使ってって依頼された。GNUに流されているんだな。

そんな訳でbison入れたら、config成功。成功マークで


       :================:
      /||# nmap -A _   ||
     / ||              ||
    |  ||              ||
     \ ||              ||
       ==================
........... /      \.............
:\        ############            \
: ---------------------------------
: |  *   |__________|| ::::::::::  |
\ |      |          ||   .......   |
  --------------------------------- 8

こんなアートが出てきた。debianな時はゴジラだった。環境によって出し物を買えてるの? そでは無くて、神の思し召しみたい。

                  ___.-------.___
              _.-' ___.--;--.___ `-._
           .-' _.-'  /  .+.  \  `-._ `-.
         .' .-'      |-|-o-|-|      `-. `.
        (_ <O__      \  `+'  /      __O> _)
          `--._``-..__`._|_.'__..-''_.--'
                ``--._________.--''
 ____  _____  ____    ____       _       _______
|_   \|_   _||_   \  /   _|     / \     |_   __ \
  |   \ | |    |   \/   |      / _ \      | |__) |
  | |\ \| |    | |\  /| |     / ___ \     |  ___/
 _| |_\   |_  _| |_\/_| |_  _/ /   \ \_  _| |_
|_____|\____||_____||_____||____| |____||_____|

こんなアートも思し召しによっては出てくるみたいだ。

さて、コンパイル。

gcc -c -I/tmp/nmap-7.80/nbase -DHAVE_CONFIG_H -DNSOCK_VERSION=\"0.02\" -D_FORTIFY_SOURCE=2 -I../include -g -O2 -Wall   nsock_pcap.c -o nsock_pcap.o
nsock_pcap.c: In function 'do_actual_pcap_read':
nsock_pcap.c:424: error: incompatible types in assignment
gmake[1]: *** [Makefile:48: nsock_pcap.o] Error 1
gmake[1]: Leaving directory '/tmp/nmap-7.80/nsock/src'
gmake: *** [Makefile:157: build-nsock] Error 2

例によってエラーになった。portsのレシピを見れば解決するんだろうけど、自分で頑張るというか再発明にチャレンジ。

#ifdef PCAP_RECV_TIMEVAL_VALID
      npp.ts     = pkt_header->ts;    // <--- Line 424
#else
      /* On these platforms time received from pcap is invalid.
       * It's better to set current time */
      memcpy(&npp.ts, nsock_gettimeofday(), sizeof(struct timeval));
#endif

やっつけ仕事で、424行ってか、ifdef判定を潰した。そんな事で委員会? ちゃんとコンパイルが出来たんで佳しとする。

run

出来上がったので、試運転します。

gdb) b ultra_scan
Breakpoint 1 at 0x10f460: file scan_engine.cc, line 2708.
(gdb) r localhost > /dev/null
Starting program: /tmp/nmap-7.80/nmap localhost > /dev/null
reakpoint 1, ultra_scan (Targets=..., ports=0x59910d24dc0 <ports>, scantype=PING_SCAN, to=0x59910d24ed0 <massping(Target**, int, scan_lists*)::group_to>) at scan_engine.cc:2708
2708                    stype scantype, struct timeout_info *to) {
(gdb) bt
#0  ultra_scan (Targets=..., ports=0x59910d24dc0 <ports>, scantype=PING_SCAN, to=0x59910d24ed0 <massping(Target**, int, scan_lists*)::group_to>) at scan_engine.cc:2708
#1  0x0000059910aea475 in massping (hostbatch=0x1, num_hosts=1, ports=0x59910d24dc0 <ports>) at targets.cc:299
#2  0x0000059910aea8b5 in refresh_hostbatch (hs=0x7f7fffff41b0, exclude_group=<optimized out>, ports=0x59910d24dc0 <ports>, pingtype=80) at targets.cc:634
#3  0x0000059910aea961 in nexthost (hs=0x7f7fffff41b0, exclude_group=0x59910d24dc0 <ports>, ports=0xe, pingtype=282218192) at targets.cc:644
#4  0x0000059910a9940d in nmap_main (argc=2, argv=0x7f7fffff4e68) at nmap.cc:2063
#5  0x0000059910b100fd in main (argc=2, argv=0x7f7fffff4e68) at main.cc:237

ここから、細部に至るのは大変そう。

nmap –script

nmapにはluaが組み込まれていて、簡単に呼び出せるような。YAMAHAのルーターみたいに、ネットワーク系に強いって事なのかな。

lua語でスクリプト(関数)を定義しておいて、それを引数と共に簡単に呼び出せるようになっています。スクリプトの呼び出しには、–scriptって、まんまなスイッチで指示します。

http-title

一番簡単そうな、httpのタイトルを取って来るやつ。スクリプト集は、/usr/local/share/nmap/scriptsの中にあります。よってたかって便利なやつが集められているんで、独自に組む事はほとんど必要無いでしょう。

ob$ nmap --script http-title -p443 hamesspam.sakura.ne.jp
Starting Nmap 7.80 ( https://nmap.org ) at 2020-06-30 15:04 JST
Nmap scan report for hamesspam.sakura.ne.jp (219.94.129.63)
Host is up (0.024s latency).
rDNS record for 219.94.129.63: www1053.sakura.ne.jp

PORT    STATE SERVICE
443/tcp open  https
|_http-title: HAM es SPAM

Nmap done: 1 IP address (1 host up) scanned in 1.31 seconds

これオイラーのページにアクセスしてみた図。親切にもホストの正式名を報告してくれています。

ssl-cert

もう一発、今度はhttpsで繋ぐ時の前段階で発せらる、身元保証人の確認。

ob$ nmap --script ssl-cert -p443 hamesspam.sakura.ne.jp
 :
ORT    STATE SERVICE
443/tcp open  https
| ssl-cert: Subject: commonName=*.sakura.ne.jp
| Subject Alternative Name: DNS:*.sakura.ne.jp, ...
| Issuer: commonName=Gehirn Managed Certification Authority - RSA DV/organizationName=Gehirn Inc./stateOrProvinceName=Tokyo/countryName=JP
| Public Key type: rsa
| Public Key bits: 2048
| Signature Algorithm: sha256WithRSAEncryption
| Not valid before: 2020-05-28T00:00:00
| Not valid after:  2022-05-28T23:59:59
| MD5:   76a2 b238 4e37 7b7c bef9 aacc 8157 a75f
|_SHA-1: 46d9 e68a eb2a 58a0 340f 17fc 80d4 0069 73bd 95cd

以前確認した時は、有効期限がぎりぎりだったけど、ちゃんと更新されてるね。

ssh-auth-methods

更に、sshでloginする時の方法を教えてくれるやつ。

ob$ nmap -p 22 --script ssh-auth-methods --script-args="ssh.user=sakae" localhost
 :
PORT   STATE SERVICE
22/tcp open  ssh
| ssh-auth-methods:
|   Supported authentication methods:
|     publickey
|     password
|_    keyboard-interactive

こういうのは隙間スクリプトなんだな。

どんな扱いをしてるか見る。

ocal shortport = require "shortport"
local stdnse = require "stdnse"
local libssh2_util = require "libssh2-utility"
local rand = require "rand"

使うライブラリィー名の登録か。stdnseってのは、stdlibなんだな。

ocal username = stdnse.get_script_args("ssh.user") or rand.random_alpha(5)
portrule = shortport.port_or_service(22, 'ssh')

function action (host, port)
  local result = stdnse.output_table()
  local helper = libssh2_util.SSHConnection:new()
  if not helper:connect(host, port) then
    return "Failed to connect to ssh server"
  end

  local authmethods = helper:list(username)

  result["Supported authentication methods"] = authmethods

  return result
end

actionって関数が基底の引数(host,port)で呼び出される。それから外れる引数は、 get_script_args を呼んで整えておけ。portは名前でも番号でもおk。

関数内では、出力用の連想配列を用意。sshで接続。上手くいったらhelper関数を使って臨むデータを取り出す。それをキー、バリューの形にして返すとな。

/usr/local/share/nmap/nselib

に、標準のライブラリィーが置いてあるんだな。 150個ぐらいは有りそう。

httpとかftpとかsqlとかが有るのは、他のスクリプトと同様。勿論、上で見たstdnseもここに置いてある。

ob$ grep -e ^function stdnse.lua
function get_script_args (...)
function output_table ()
function pretty_printer (obj, printer)
function get_timeout(host, max_timeout, min_timeout)

何となく、上で出て来たな。

nse_main.lua

親玉が有った。説明も親切に書いてくれている。

local function main (hosts, scantype)
  -- Used to set up the runlevels.
  local threads, runlevels = {}, {};

  --  args    A table that contains the arguments passed to scripts,
  --            arguments can be host and port tables.
  --  env     A table that contains the global script environment:
  --            categories, description, author, license, nmap table,
  --            action function, rule functions, SCRIPT_PATH,
  --            SCRIPT_NAME, SCRIPT_TYPE (pre|host|port|post rule).
  --  host    A table that contains the target host information. This
  --          will be nil for Pre-scanning and Post-scanning scripts.
  --  port    A table that contains the target port information. This
  --          will be nil for Pre-scanning and Post-scanning scripts.

lua

方々で使われている。Nginx , Wireshark , Neovim とかゲーム業界とかね。 お気楽 Lua プログラミング超入門 によると、JIT系も提供されてて、結構速いみたいです。余り表に出てこないけど、知っている人は、なる程って感じです。

on FreeBSD

我お膝元にも鎮座してました。そう、FreeBSDをbootするのにも活躍してます。

[sakae@fb /boot]$ ls -l *lua*
-r-xr-xr-x  3 root  wheel  393216 Nov  1  2019 loader_lua
-r-xr-xr-x  2 root  wheel  460800 Nov 13  2019 loader_lua.efi
-r--r--r--  2 root  wheel  422408 Nov  1  2019 userboot_lua.so

lua:
total 112
-r--r--r--  1 root  wheel   4128 Nov  1  2019 cli.lua
-r--r--r--  1 root  wheel   3211 Nov  1  2019 color.lua
 :

以前は4thだったような気がしたけど、luaに移行しちゃったのかな。最近余りFreeBSDを追っていないので、よう分からん。

lua 5.4

5.3系が出てから2年経過。ディスコンになってしまっていたかと思ったら、新しいのがつい最近出てきた。組み込みが生きる道なんで、そんなにアップデートを繰り返すのは良くない。じっくりと機能を吟味してるんだな。

 Lua 5.4 was released on 29 Jun 2020. See its main new features.

The current release is Lua 5.4.0, released on 29 Jun 2020. 

Welcome to Lua 5.4 こちらに説明が有る。折角なんで、debian(32Bit)に入れてみるか。

淹れ方は簡単。単にmakeって叩くだけ。

debian:src$ ./lua
Lua 5.4.0  Copyright (C) 1994-2020 Lua.org, PUC-Rio
>
debian:src$ time ./lua tara.lua
11

real    0m52.361s
user    0m51.930s
sys     0m0.020s

有名なあれをやってみた。で、この値は速いの遅いの? 比較対象が欲しい。ひと昔前のものと 比較してみる。

debian:lua-5.3.5$ make
Please do 'make PLATFORM' where PLATFORM is one of these:
   aix bsd c89 freebsd generic linux macosx mingw posix solaris
See doc/readme.html for complete instructions.
debian:lua-5.3.5$ make linux
 :

ちょっと、一手間かけて作ってあげる。

debian:src$ ./lua
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
>
debian:src$ time ./lua tara.lua
11

real    2m42.834s
user    2m41.383s
sys     0m0.088s

これが2年間の進歩です。速くなるようにチューニングされてないかって疑惑があるな。こんなに差が付くって、昔は無駄の塊だったのかい?

誰か追試を求む。

debian:src$ cat tara.lua
function tak(x, y, z)
  if x <= y then
    return z
  else
    return tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y))
  end
end

print( tak(22, 11, 0) )
debian:src$ ./luac tara.lua
debian:src$ ls -l luac.out
-rw-r--r-- 1 sakae sakae 484 Jul  1 06:35 luac.out

ソースファイルをバイトコンパイルするluacってのが用意されてる。それでコンパイルすると、上記のように luac.out ってのが出来る。これなら少しは速く実行してくれるかと思うんだけど、

debian:src$ time ./lua luac.out
11

real    2m35.635s
user    2m34.322s
sys     0m0.076s

微妙な差しか無し。黙って、新しい版のやつを使っておけ。

debian:src$ time luajittex tara.lua
11

real    0m22.859s
user    0m22.702s
sys     0m0.008s

debain機には何でも突っ込んでおく主義。Texの片割れとして上記のようにjit版が入っていた。Texの一員として頑張ってた。

debian:src$ luajittex -v
This is LuajitTeX, Version 1.07.0 (TeX Live 2019/dev/Debian)

Execute  'luajittex --credits'  for credits and version details.

There is NO warranty. Redistribution of this software is covered by
the terms of the GNU General Public License, version 2 or (at your option)
any later version. For more information about these matters, see the file
named COPYING and the LuaTeX source.

LuaTeX is Copyright 2018 Taco Hoekwater and the LuaTeX Team.

隠れluaなんだな。色々な方面へ地味に進出中ってのがPythonと違って奥ゆかしいな。猫も杓子Pythonな世の中は嫌いさ。

debian:src$ time ruby tara.rb
11
real    0m49.300s
user    0m48.904s
sys     0m0.032s
debian:src$ ruby -v
ruby 2.6.1p33 (2019-01-30 revision 66950) [i686-linux]

最後にruby版と比べてみた。


This year's Index

Home