勉強日記

チラ裏

LPIC あずき本v5.0 ch12 セキュリティ 12.1

www.shoeisha.co.jp


ホストレベルのセキュリティ

  • 侵入には内外からのものがある
  • 外から
    • 必要なソフトウェアのみインストール
    • 不要なサービスを起動しない
    • ホストレベルの適切なアクセス制御
    • パケットフィルタリング
    • セキュリティ情報の確認を頻繁に
      • 必要があれば素早く対策
  • 内から
    • 適切なユーザーパスワード管理
    • root権限で動作するプログラムを最小限に
      • 定期的にチェック

スーパーサーバの設定と管理

  • 各種デーモンのプロキシ的なやつ
    • GoFのvirtual proxy的な
    • 他のサーバプログラムに代わってサービス要求を監視
    • 接続が確立した時点で本来のサーバプログラムに要求を引き渡す
    • 必要なときだけ起動するので、待機中のデーモンが消費するリソースを節約できる
  • スタンドアロン
    • スーパーサーバを経由しない
スタンドアロン スーパーサーバ
消費リソース 待機中デーモンぶん大 節約
即応性 o x
適するサーバープログラムの例 FTP, Telnet Webサーバ、メールサーバ
  • しゅるい
    • xinetd
      • たいていのディストリでこっち採用
    • inetd

xinetdの設定

/etc/xinetd.conf

- 全体的な設定
    - `defaults`セクション
#
# This is the master xinetd configuration file. Settings in the
# default section will be inherited by all service configurations
# unless explicitly overridden in the service configuration. See
# xinetd.conf in the man pages for a more detailed explanation of
# these attributes.

defaults
{
# The next two items are intended to be a quick access place to
# temporarily enable or disable services.
#
#   enabled     =
#   disabled    =

# Define general logging characteristics.
    log_type    = SYSLOG daemon info 
    log_on_failure  = HOST
    log_on_success  = PID HOST DURATION EXIT

# Define access restriction defaults
#
#   no_access   =
#   only_from   =
#   max_load    = 0
    cps     = 50 10
    instances   = 50
    per_source  = 10

# Address and networking defaults
#
#   bind        =
#   mdns        = yes
    v6only      = no

# setup environmental attributes
#
#   passenv     =
    groups      = yes
    umask       = 002

# Generally, banners are not used. This sets up their global defaults
#
#   banner      =
#   banner_fail =
#   banner_success  =
}

includedir /etc/xinetd.d
  • 主要な設定
    • instances = 50
      • 各サービスの最大デーモン数
    • cps = 50 10
      • connection per sec
      • <秒間最大コネクション数> <限度に達した場合のサービス休止秒数>
    • log_type = SYSLOG daemon info
      • ログの出力方法
    • log_on_failure = HOST
      • 接続拒否時のログ記録内容
    • log_on_success = PID HOST DURATION EXIT
      • 接続許可時のログ記録内容
    • includedir /etc/xinetd.d

/etc/xinetd.d/

  • サービスごとの設定ファイルが配置されている
    • /etc/xinetd.confincludedir /etc/xinetd.dより
ls /etc/xinetd.d
chargen-dgram   daytime-stream  echo-dgram     time-dgram
chargen-stream  discard-dgram   echo-stream    time-stream
daytime-dgram   discard-stream  tcpmux-server
  • /etc/xinetd.d/tcpmux-server
    • service tcpmuxセクション
# This is the configuration of the tcpmux server. It must be enabled in
# order to add any other tcpmux services.

service tcpmux
{
# This is for quick on or off of the service
    disable     = yes

# The next attributes are mandatory for all services
    id      = tcpmux-server
    type        = INTERNAL
    wait        = no
    socket_type = stream
#   protocol    =  socket type is usually enough

# External services must fill out the following
#   user        = root
#   group       =
#   server      =
#   server_args =

# External services not listed in /etc/services must fill out the next one
#   port        =

# RPC based services must fill out these
#   rpc_version =
#   rpc_number  =

# Logging options
#   log_type    =
#   log_on_success  =
#   log_on_failure  =

# Networking options
#   flags       =
#   bind        =
#   redirect    =
#   v6only      = 

# Access restrictions
#   only_from   =
#   no_access   =
#   access_times    =
#   cps     = 50 10
#   instances   = UNLIMITED
#   per_source  = UNLIMITED
#   max_load    = 0
#   deny_time   = 120
#   mdns        = yes 

# Environmental options
#   env     =
#   passenv     =
#   nice        = 0
#   umask       = 022
#   groups      = yes
#   rlimit_as   =
#   rlimit_cpu  =
#   rlimit_data =
#   rlimit_rss  =
#   rlimit_stack    =

# Banner options. (Banners aren't normally used)
#   banner      =
#   banner_success  =
#   banner_fail =
}
  • 主要な設定
    • disable = yes
      • サービスの有効(no)/無効(yes)
    • socket_type = stream
      • 通信のタイプ
        • TCP: stream
          • 順番保証されるから
        • UDP: dgram
          • User Datagram Protocol
    • wait = no
      • ウェイトタイム
        • シングルスレッド: yes
        • マルチスレッド: no
    • user = root
      • サービスを実行するユーザ名
    • server =
      • サーバプログラム(デーモン)へのフルパス
    • server_args =
      • サーバプログラム(デーモン)に渡す引数
    • nice = 0
      • 実行優先度
      • [-20; +20)
      • -20が最優先
    • only_from =
      • 接続許可接続元
    • no_access =
      • 接続拒否接続元
    • access_times =
      • アクセスを許可する時間帯
      • access_times = 9:00-17:00とか
  • 設定変更後はxinetdの再起動が必要
  • Systemd
systemctl restart xinetd.service
  • SysVinit
/etc/init.d/xinetd restart
  • Systemdはそもそもxinetdの代わりになる
  • `/etc/xinetd.d/hoge

systemdに変換する

参考

  • 変換元
  • /etc/xinetd.d/hoge
service application
{
        socket_type  = stream
        wait         = no
        user         = root
        server       = /opt/hoge
        type         = UNLISTED
        port         = N
        protocol     = tcp
        disable      = no
}
  • /etc/systemd/sysmtem/hoge.socket
[Unit]
Description=hoge Socket

[Socket]
ListenStream=N
Accept=yes

[Install]
WantedBy=sockets.target
  • /etc/systemd/system/hoge.service
[Unit]
Description=hoge Server

[Service]
ExecStart=/opt/hoge
User=root
Group=root
StandardInput=socket
  • 実行
systemctl enable hoge.socket
systemctl start hoge.socket

systemctl status hoge.socket

TCP Wrapperによるアクセス制御

  • TCP Wrapper
    • ネットワークサービスのアクセス制御を集中的に行う
  • tcpdデーモン
    • telnetdやftpdなどのプロキシ
      • スーパーサーバが適さない連中
      • GoFのprotection proxy的な
  • アクセス制御フロー
    1. サーバプログラムに代わってサービス要求を受け取る
    2. 設定に基づいてチェック
      1. /etc/hosts.allowの許可リストに入っていればOK
      2. else if /etc/hosts.denyの拒否リストに入っていなければOK
    3. 接続が許可された場合はサーバプログラムに処理を引き渡す
  • libwrap
    • TCP Wrapperライブラリ
    • tcpdなしでもTCP Wrapperの機能を利用できる
    • OpenSSHサーバ、各種POP/IMAPサーバなどが採用

/etc/hosts.allow/etc/hosts.deny

  • /etc/hosts.allow
# /etc/hosts.allow: list of hosts that are allowed to access the system.
#                   See the manual pages hosts_access(5) and hosts_options(5).
#
# Example:    ALL: LOCAL @some_netgroup
#             ALL: .foobar.edu EXCEPT terminalserver.foobar.edu
#
# If you're going to protect the portmapper use the name "rpcbind" for the
# daemon name. See rpcbind(8) and rpc.mountd(8) for further information.
#
  • /etc/hosts.deny
# /etc/hosts.deny: list of hosts that are _not_ allowed to access the system.
#                  See the manual pages hosts_access(5) and hosts_options(5).
#
# Example:    ALL: some.host.name, .some.domain
#             ALL EXCEPT in.fingerd: other.host.name, .other.domain
#
# If you're going to protect the portmapper use the name "rpcbind" for the
# daemon name. See rpcbind(8) and rpc.mountd(8) for further information.
#
# The PARANOID wildcard matches any host whose name does not match its
# address.
#
# You may wish to enable this to ensure any programs that don't
# validate looked up hostnames still leave understandable logs. In past
# versions of Debian this has been the default.
# ALL: PARANOID
  • 書式
    • <サービス名>: <対象ホストのリスト>
ワイルドカード 説明
ALL すべてのサービスもしくはホスト
A EXCEPT B B以外のA
LOCAL .を含まないすべてのホスト(localhostとか)
ローカルネットワークセグメント内のホスト
PARANOID クライアントのIPアドレスから逆引きしたホスト名と、
そのホスト名を正引きしたIPアドレスが一致することを確認する
  • paranoid
    • 辞書的な意味
      • Unreasonably or obsessively anxious, suspicious, or mistrustful.
      • 偏執症(患者)の; 誇大妄想的な; 病的なほど疑り深い.
  • /etc/hosts.denyの設定例
    • ALL: ALL
  • SIGHUPとかsystemd restartとか不要

開いているポートの確認

  • サーバプロセスを起動すると特定のポートを開いて接続を待ち受ける
  • セキュリティリスク
    • 攻撃者の情報収集や攻撃の対象
    • なので最小限にとどめるべき
  • 確認コマンド
    • ローカル一覧
      • netstat
      • ss
      • lsof
    • ローカル確認
      • fuser
    • ネットワーク越し一覧
      • nmap
        • 攻撃の前段のポートスキャンにも使われるやつ

netstat

netstat --help
usage: netstat [-vWeenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
       netstat [-vWnNcaeol] [<Socket> ...]
       netstat { [-vWeenNac] -i | [-cnNe] -M | -s [-6tuw] }

...
        -a, --all                display all sockets (default: connected)
...
  <Socket>={-t|--tcp} {-u|--udp} {-U|--udplite} {-S|--sctp} {-w|--raw}
           {-x|--unix} --ax25 --ipx --netrom
  • 開いている全TCP/UDPポートを取得
netstat -atu
# netstat --all --tcp --udp # おなじ
Proto 受信-Q 送信-Q 内部アドレス            外部アドレス            状態      
tcp        0      0 0.0.0.0:5900            0.0.0.0:*               LISTEN     
tcp        0      0 localhost:7379          0.0.0.0:*               LISTEN     
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN     
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN     
tcp        0      0 localhost:19000         0.0.0.0:*               LISTEN     
tcp        0      0 localhost:57618         localhost:7379          ESTABLISHED
tcp        0      0 localhost:7379          localhost:57164         ESTABLISHED
tcp        0      0 localhost:57164         localhost:7379          ESTABLISHED
tcp        0      0 wand-ThinkPad-X240:5900 192.168.11.2:60083      ESTABLISHED
tcp        0      0 wand-ThinkPad-X24:50934 tm-in-f188.1e100.n:5228 ESTABLISHED
tcp        0      0 localhost:7379          localhost:57618         ESTABLISHED
tcp6       0      0 [::]:5900               [::]:*                  LISTEN     
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN     
tcp6       0      0 [::]:44641              [::]:*                  LISTEN     
udp        0      0 0.0.0.0:56889           0.0.0.0:*                          
udp        0      0 localhost:domain        0.0.0.0:*                          
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                          
udp        0      0 0.0.0.0:ipp             0.0.0.0:*                          
udp        0      0 224.0.0.251:mdns        0.0.0.0:*                          
udp        0      0 224.0.0.251:mdns        0.0.0.0:*                          
udp        0      0 224.0.0.251:mdns        0.0.0.0:*                          
udp        0      0 224.0.0.251:mdns        0.0.0.0:*                          
udp        0      0 224.0.0.251:mdns        0.0.0.0:*                          
udp        0      0 0.0.0.0:mdns            0.0.0.0:*                          
udp6       0      0 [::]:53212              [::]:*                             
udp6       0      0 [::]:mdns               [::]:*                             

ss

  • socket statistics
ss --help
Usage: ss [ OPTIONS ]
       ss [ OPTIONS ] [ FILTER ]
...
   -a, --all           display all sockets
...
   -t, --tcp           display only TCP sockets
...
   -u, --udp           display only UDP sockets
...
  • 開いている全TCP/UDPポートを取得
ss -atu
# ss --all --tcp --udp # おなじ
Netid State   Recv-Q  Send-Q     Local Address:Port         Peer Address:Port   
udp   UNCONN  0       0                0.0.0.0:56889             0.0.0.0:*      
udp   UNCONN  0       0          127.0.0.53%lo:domain            0.0.0.0:*      
udp   UNCONN  0       0                0.0.0.0:bootpc            0.0.0.0:*      
udp   UNCONN  0       0                0.0.0.0:ipp               0.0.0.0:*      
udp   UNCONN  0       0            224.0.0.251:mdns              0.0.0.0:*      
udp   UNCONN  0       0            224.0.0.251:mdns              0.0.0.0:*      
udp   UNCONN  0       0            224.0.0.251:mdns              0.0.0.0:*      
udp   UNCONN  0       0            224.0.0.251:mdns              0.0.0.0:*      
udp   UNCONN  0       0            224.0.0.251:mdns              0.0.0.0:*      
udp   UNCONN  0       0                0.0.0.0:mdns              0.0.0.0:*      
udp   UNCONN  0       0                   [::]:53212                [::]:*      
udp   UNCONN  0       0                   [::]:mdns                 [::]:*      
tcp   LISTEN  0       5                0.0.0.0:5900              0.0.0.0:*      
tcp   LISTEN  0       5              127.0.0.1:7379              0.0.0.0:*      
tcp   LISTEN  0       128        127.0.0.53%lo:domain            0.0.0.0:*      
tcp   LISTEN  0       5              127.0.0.1:ipp               0.0.0.0:*      
tcp   LISTEN  0       5              127.0.0.1:19000             0.0.0.0:*      
tcp   ESTAB   0       0              127.0.0.1:57618           127.0.0.1:7379   
tcp   ESTAB   0       0              127.0.0.1:7379            127.0.0.1:57164  
tcp   ESTAB   0       0              127.0.0.1:57164           127.0.0.1:7379   
tcp   ESTAB   0       0           192.168.11.3:5900         192.168.11.2:60083  
tcp   ESTAB   0       0           192.168.11.3:50934      108.177.97.188:5228   
tcp   ESTAB   0       0              127.0.0.1:7379            127.0.0.1:57618  
tcp   LISTEN  0       5                   [::]:5900                 [::]:*      
tcp   LISTEN  0       5                  [::1]:ipp                  [::]:*      
tcp   LISTEN  0       128                    *:44641                   *:*      

lsof

  • list open files
man lsof
  • 抜粋
...
       -i [i]   selects  the  listing  of  files any of whose Internet address
                matches the address specified in i.  If no address  is  speci‐
                fied, this option selects the listing of all Internet and x.25
                (HP-UX) network files.

                If -i4 or -i6 is specified with  no  following  address,  
  • IPアドレスのi?たぶん
  • 開いているポートの情報を表示する
lsof -i
COMMAND     PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
emacs25    8082 wand   13u  IPv4 1601493      0t0  TCP localhost:7379->localhost:58702 (ESTABLISHED)
emacs25    8082 wand   15u  IPv4 1221623      0t0  TCP localhost:7379 (LISTEN)
emacs25    8082 wand   18u  IPv4 1221624      0t0  TCP localhost:57164->localhost:7379 (ESTABLISHED)
emacs25    8082 wand   19u  IPv4 1221625      0t0  TCP localhost:19000 (LISTEN)
emacs25    8082 wand   20u  IPv4 1221626      0t0  TCP localhost:7379->localhost:57164 (ESTABLISHED)
chrome     8446 wand  153u  IPv4 1266998      0t0  UDP 224.0.0.251:mdns 
chrome     8446 wand  154u  IPv4 1267000      0t0  UDP 224.0.0.251:mdns 
chrome     8446 wand  156u  IPv4 1267002      0t0  UDP 224.0.0.251:mdns 
chrome     8446 wand  212u  IPv4 1267004      0t0  UDP 224.0.0.251:mdns 
chrome     8492 wand   41u  IPv4 1604343      0t0  TCP localhost:58702->localhost:7379 (ESTABLISHED)
chrome     8492 wand   42u  IPv4 1266429      0t0  TCP wand-ThinkPad-X240s:50934->tm-in-f188.1e100.net:5228 (ESTABLISHED)
chrome     8492 wand   53u  IPv4 1226129      0t0  UDP 224.0.0.251:mdns 
vino-serv 21114 wand   11u  IPv6 1128733      0t0  TCP *:5900 (LISTEN)
vino-serv 21114 wand   12u  IPv4 1128734      0t0  TCP *:5900 (LISTEN)
vino-serv 21114 wand   14u  IPv4 1217389      0t0  TCP wand-ThinkPad-X240s:5900->192.168.11.2:60083 (ESTABLISHED)
apache2   21157 wand    4u  IPv6 1130784      0t0  TCP *:44641 (LISTEN)
apache2   21161 wand    4u  IPv6 1130784      0t0  TCP *:44641 (LISTEN)
apache2   21162 wand    4u  IPv6 1130784      0t0  TCP *:44641 (LISTEN)
  • 指定したポート番号が使われている通信を確認する
lsof -i:19000
COMMAND  PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
emacs25 8082 wand   19u  IPv4 1221625      0t0  TCP localhost:19000 (LISTEN)

nmap

  • 手元のUbuntuにはプリインストールされていなかった
man nmap
NMAP(1)                         [FIXME: manual]                        NMAP(1)

名前
       nmap - ネットワーク調査ツールおよびセキュリティ/ポート スキャナ

概要
       nmap [スキャンタイプ...] [オプション] {ターゲットの指定}
  • manのセクション的にはUser Commands(1)
    • cf. netstat, ss, lsofはSystem Manager's Manual(8)
  • file userの意味か
nmap localhost
Starting Nmap 7.60 ( https://nmap.org ) at 2019-04-14 23:16 JST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000096s latency).
Not shown: 998 closed ports
PORT     STATE SERVICE
631/tcp  open  ipp
5900/tcp open  vnc

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

fuser

man fuser
  • 抜粋
FUSER(1)                         User Commands                        FUSER(1)

NAME
       fuser - identify processes using files or sockets

SYNOPSIS
       fuser [-fuv] [-a|-s] [-4|-6] [-c|-m|-n space] [ -k [-i] [-M] [-w]
       [-SIGNAL] ] name ...
       fuser -l
       fuser -V
  • これもUser Commands(1)
       -n SPACE, --namespace SPACE
              Select  a  different  name  space.   The  name spaces file (file
              names, the default), udp (local UDP ports), and tcp  (local  TCP
              ports)  are supported.  For ports, either the port number or the
              symbolic name can be specified.  If there is no  ambiguity,  the
              shortcut notation name/space (e.g. 80/tcp) can be used.
fuser -n tcp 19000
# fuser -n 19000/tcp # なんか駄目だった
19000/tcp:            8082

SUIDが設定されているファイル

  • SUID: Set User ID
    • コマンドやプログラムを所有者の権限で実行
    • 【補】スクリプトに付与しても効かない(セキュリティ上の理由)
  • root所有なら、一般ユーザが実行した場合でもroot権限
    • 最小限に留めなければならない
    • 覚えのないファイルに設定されていたら、システムが侵害を受けた可能性がある
  • /以下、SUIDが設定されているファイルを全検索
sudo find / -perm -u+s -ls
  • -u+sの代わりに...
    • -g+s: SGID
    • -o+t: スティッキービット
  • findコマンドのオプション
       -perm mode
              File's  permission  bits  are  exactly mode (octal or symbolic).
              Since an exact match is required, if you want to use  this  form
              for  symbolic  modes,  you  may have to specify a rather complex
              mode string.  For example `-perm  g=w'  will  only  match  files
              which  have  mode 0020 (that is, ones for which group write per‐
              mission is the only permission set).  It is more likely that you
              will want to use the `/' or `-' forms, for example `-perm -g=w',
              which matches any file with group  write  permission.   See  the
              EXAMPLES section for some illustrative examples.

       -perm -mode
              All  of the permission bits mode are set for the file.  Symbolic
              modes are accepted in this form, and this is usually the way  in
              which  you would want to use them.  You must specify `u', `g' or
              `o' if you use a symbolic mode.   See the EXAMPLES  section  for
              some illustrative examples.

       -perm /mode
              Any  of the permission bits mode are set for the file.  Symbolic
              modes are accepted in this form.  You must specify `u',  `g'  or
              `o'  if  you  use a symbolic mode.  See the EXAMPLES section for
              some illustrative examples.  If no permission bits in  mode  are
              set,  this test matches any file (the idea here is to be consis‐
              tent with the behaviour of -perm -000).

       -perm +mode
              This is no longer  supported  (and  has  been  deprecated  since
              2005).  Use -perm /mode instead.
  • -perm...部分
    • +perm...deprected. -permに同じ
  • perm...完全一致
  • /perm...どれか一致