chkrootkit で INFECTED になる

OSはAlmaLinux 9.1 で、ある時chkrootkitが
Searching for Linux.Xor.DDoS … INFECTED: Possible Malicious Linux.Xor.DDoS installed
汚染されていると言っている。早速 /tmpに実行フィルがあるかチェックする
# find /tmp -executable -type f
すると /tmp/tAIOtkaU/2023-02-11_04-41-01/script.sh に実行権限が与えられている。
このファイルを見ると、timeshift でrestoreした時のrsyncのファイルであったので、こんなファイルはいらないので削除すると
#
chkrootkit | grep INFECTED
と打ち込んでもINFECTEDには成らない。

* 別の日にAlmaLinuxに作ったサブホストでchkrootkitを実行すると
Checking `chsh’… INFECTED
と表示が出るので、コマンドの様なんでどこにあるかチェックする
# find / -name chsh -ls
19482653 4 -rw-r–r– 1 root root 192 10月 16 05:14 /etc/pam.d/chsh
38372077 28 -rws–x–x 1 root root 24936 10月 16 05:15 /usr/bin/chsh
53105243 4 -rw-r–r– 1 root root 504 2月 3 2021 /usr/share/bash-completion/completions/chsh
でこのchshコマンドを調べるとログイン時のシェルを変更するコマンドだ、私は使わないので /usr/bin/chshを削除し、再度
#
chkrootkit | grep INFECTED
今度は何事もなくプロンプトに帰ってきた。

カテゴリー: Linux運用時のメモ | コメントする

8:ブリッジ(br0)を作成

参考:https://centossrv.com/almalinux/softethervpnserver.shtml の■VPNクライアントからVPNサーバーへのアクセス設定
# nmcli device ← 物理インタフェース名確認
DEVICE                  TYPE           STATE       CONNECTION
enp37s0                  ethernet     接続済み    enp37s0
wlp38s0                   wifi             切断済み —
p2p-dev-wlp38s0  wifi-p2p     切断済み —
lo                              loopback   管理無し —
# nmcli c add type bridge ifname br0 ← ブリッジインタフェースbr0作成
接続 ‘bridge-br0’ (8d7a1c76-46d6-4792-a303-51e0636e7a9d) が正常に追加されました。
# nmcli con modify bridge-br0 bridge.stp no ← ブリッジインタフェースbr0のSTPを無効化
# nmcli con modify bridge-br0 ipv4.method manual ipv4.addresses 192.168.4.45/24 ipv4.gateway 192.168.4.1 ipv4.dns “192.168.4.1 8.8.8.8” ← ブリッジインタフェースbr0ネットワーク設定 ※例:IPアドレスを192.168.4.45、ネットマスクを24、ゲートウェイを192.168.4.1、DNSサーバーのアドレスを192.168.4.1    8.8.8.8とする
# nmcli con add type bridge-slave ifname enp37s0 master bridge-br0 ← 物理インタフェースenp37s0をブリッジインタフェースbr0に接続
接続 ‘bridge-slave-enp37s0’ (60c828c4-88d8-4e22-a739-835a8d830d0d) が正常に追加されました。
# nmcli c del enp37s0 ← 物理インタフェースenp37s0削除
接続 ‘enp37s0’ (cba75ec1-c383-33f0-b3fd-0f48e0cce1e6) が正常に削除されました
# reboot ← システム再起動
# dnf install -y bridge-utils ← brctlコマンドインストール
# brctl show ← ブリッジ接続状態照会
bridge name   bridge id                         STP enabled           interfaces
br0                    8000.309c23a7851f         no                            enp37s0
# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
        inet 192.168.4.45 netmask 255.255.255.0 broadcast 192.168.4.255
        ether 30:9c:23:a7:85:1f txqueuelen 1000 (Ethernet)
        RX packets 4071 bytes 4118294 (3.9 MiB)
        RX errors 0 dropped 6 overruns 0 frame 0
        TX packets 3403 bytes 593116 (579.2 KiB)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

enp37s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
        ether 30:9c:23:a7:85:1f txqueuelen 1000 (Ethernet)
        RX packets 4450 bytes 4201103 (4.0 MiB)
        RX errors 0 dropped 6 overruns 0 frame 0
        TX packets 3588 bytes 602736 (588.6 KiB)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
        inet 127.0.0.1 netmask 255.0.0.0
        inet6 ::1 prefixlen 128 scopeid 0x10<host>
        loop txqueuelen 1000 (Local Loopback)
        RX packets 149 bytes 11677 (11.4 KiB)
        RX errors 0 dropped 0 overruns 0 frame 0
        TX packets 149 bytes 11677 (11.4 KiB)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

wlp38s0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
       ether 6a:11:31:fb:08:1e txqueuelen 1000 (Ethernet)
       RX packets 0 bytes 0 (0.0 B)
       RX errors 0 dropped 0 overruns 0 frame 0
       TX packets 0 bytes 0 (0.0 B)
       TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
br0にIPがあり、enp37s0にはIPが無い。

カテゴリー: AlmaLinuxに挑戦 | コメントする

7:Anthyの辞書登録

設定 → Keyboard → 右ペインの入力ソースにある日本語(Anthy)の右の縦の点3つをクリックし、Preference をクリック ← 開いたウィンドウで辞書タブをクリックし、”辞書の編集コマンド(E):”の右端の縦の点3つをクリックし、霞ウィンドウを開く
右下の追加をクリックし、選択された単語に漢字や記号を入力し、読みに読み方を入力、頻度は500のまま、品詞に該当するものを選び、下にある保存をクリック。
開いているウィンドウを消す。

カテゴリー: その他 | コメントする

6:アンチウイルスソフト導入(Clam AntiVirus)

参考:https://centossrv.com/almalinux/clamav.shtml
# dnf install -y epel-release ← EPELリポジトリインストール
# dnf -y install clamav clamav-server-systemd clamav-update clamav-scanner-systemd ← Clam AntiVirusインストール
# gedit /etc/freshclam.conf
#NotifyClamd /path/to/clamd.conf  ← #を取って下記に変更
NotifyClamd /etc/clamd.d/scan.conf ← 追加(ウイルス定義ファイル更新をclamdに通知する)
保存後
# freshclam  ← ウイルス定義ファイル最新化
前略
bytecode.cvd updated (version: 333, sigs: 92, f-level: 63, builder: awillia2)
ERROR: NotifyClamd: No communication socket specified in /etc/clamd.d/scan.conf
ERROR: Can’t send to clamd: Socket operation on non-socket
# gedit /etc/clamd.d/scan.conf  ← Clam AntiVirus設定ファイル編集
#User clamscan ← 行頭に#を追加してコメントアウト(root権限で動作するようにする)

# Path to a local socket file the daemon will listen on.
# Default: disabled (must be specified by a user)
LocalSocket /run/clamd.scan/clamd.sock ← 行頭の#を削除

# systemctl start clamd@scan ← clamd起動
# systemctl enable clamd@scan ← clamd自動起動設定 [root@almalinux ~]
# systemctl start clamav-freshclam ← clamav-freshclam起動 [root@almalinux ~]
# systemctl enable clamav-freshclam ← clamav-freshclam自動起動設定
# clamdscan -c /etc/clamd.d/scan.conf –remove ← ウイルススキャンテスト(ウイルスなしの場合)
/root: OK

———– SCAN SUMMARY ———–
Infected files: 0
Time: 53.727 sec (0 m 53 s)
Start Date: 2023:02:10 09:54:05
End Date: 2023:02:10 09:54:59

# wget http://www.eicar.org/download/eicar.com ← テスト用ウイルスをダウンロード
# wget http://www.eicar.org/download/eicar.com.txt ← 〃
# wget http://www.eicar.org/download/eicar_com.zip ← 〃
# wget http://www.eicar.org/download/eicarcom2.zip ← 〃

# clamdscan -c /etc/clamd.d/scan.conf –remove ← ウイルススキャンテスト(ウイルスありの場合)
/root/eicar.com: Eicar-Test-Signature FOUND ← ウイルス検知 /root/eicar.com: Removed. ← ウイルス削除
/root/eicar.com.txt: Eicar-Test-Signature FOUND ← ウイルス検知 /root/eicar.com.txt: Removed. ← ウイルス削除
/root/eicar_com.zip: Eicar-Test-Signature FOUND ← ウイルス検知 /root/eicar_com.zip: Removed. ← ウイルス削除 /root/eicarcom2.zip: Eicar-Test-Signature FOUND ← ウイルス検知
/root/eicarcom2.zip: Removed. ← ウイルス削除
———– SCAN SUMMARY ———–
Infected files: 4 ← 4つのウイルスを検知した
Time: 23.913 sec (0 m 23 s)

# gedit /etc/cron.daily/clamdscan ← ウイルススキャン日次実行スクリプト作成 #!/bin/bash
# 設定ファイル
CONFIG=/etc/clamd.d/scan.conf
# スキャン実行 # ※ウイルス検知時は隔離ディレクトリへ隔離
CLAMSCANLOG=`mktemp`
QUARANTINEDIR=/tmp/clamdscan-quarantinedir-$(date +%Y%m%d)
mkdir -p ${QUARANTINEDIR}
clamdscan -c ${CONFIG} –move=${QUARANTINEDIR} / > ${CLAMSCANLOG} 2>&1
# ウイルス検知時のみroot宛にメール通知
if [ -z “$(grep FOUND$ ${CLAMSCANLOG})” ]; then
rm -rf ${QUARANTINEDIR}
else
grep -A 1 FOUND$ ${CLAMSCANLOG} | mail -s “Virus Found in `hostname` => ${QUARANTINEDIR}” root
fi
# スキャンログをシスログに出力 cat ${CLAMSCANLOG} | logger -t $(basename ${0})
rm -f ${CLAMSCANLOG}


# chmod +x /etc/cron.daily/clamdscan ← ウイルススキャン日次実行スクリプトへ実行権限付加
# echo ExcludePath ^/tmp/clamdscan-quarantinedir-.*/ >> /etc/clamd.d/scan.conf ← ウイルス隔離ディレクトリをスキャン対象外にする
# echo ExcludePath ^/proc/ >> /etc/clamd.d/scan.conf ← 例として/procディレクトリをスキャン対象外にする
# echo ExcludePath ^/sys/ >> /etc/clamd.d/scan.conf ← 例として/sysディレクトリをスキャン対象外にする
# systemctl restart clamd@scan ← clamd再起動(スキャン除外設定反映)

カテゴリー: AlmaLinuxに挑戦 | コメントする

5:rootkitのインストール

参考:https://centossrv.com/almalinux/chkrootkit.shtml
# git clone https://github.com/Magentron/chkrootkit.git  rootkitのダウンロード
# cp chkrootkit/chkrootkit /usr/local/bin/  保存場所の変更
# rm -rf chkrootkit  ダウンロードしたchkrootkitを削除
# chkrootkit | grep INFECTED  chkrootkit実行
Checking `chsh’… INFECTED
ありゃー、chshが汚染されている!almalinuxをインストールしたばかりなので、誤検知だとは思うが、chshで何だ?使ったことは無いので調べると、
ログイン時のシェルを変更するコマンドだった。
私はログインシェルを変更しないので、削除して置いた。
再度チェック
# chkrootkit | grep INFECTED
今度は何も表示されないのでOK

chkrootkit実行スクリプトを毎日自動実行されるディレクトリへ作成
# gedit /etc/cron.daily/chkrootkit

#!/bin/bash

LOG=/tmp/$(basename ${0})

# chkrootkit実行
chkrootkit > $LOG 2>&1

# ログ出力
cat $LOG | logger -t $(basename ${0})

# SMTPSのbindshell誤検知対応
if [ ! -z "$(grep 465 $LOG)" ] && \
   [ -z $(/usr/sbin/lsof -i:465|grep bindshell) ]; then
        sed -i '/465/d' $LOG
fi

# upstartパッケージ更新時のSuckit誤検知対応
if [ ! -z "$(grep Suckit $LOG)" ] && \
   [ -z "$(rpm -V `rpm -qf /sbin/init`)" ]; then
        sed -i '/Suckit/d' $LOG
fi

# rootkit検知時のみroot宛メール送信
[ ! -z "$(grep INFECTED $LOG)" ] && \
grep INFECTED $LOG | mail -s "chkrootkit report in `hostname`" root

chkrootkit実行スクリプトへ実行権限付加
# chmod 700 /etc/cron.daily/chkrootkit
コマンドが改竄された場合に備えて、汚染のないコマンド軍を保存しておく
# mkdir chkrootkitcmd ← chkrootkit使用コマンド退避先ディレクトリ作成
# cp `which –skip-alias awk cut echo egrep find head id ls netstat ps strings sed ssh uname` chkrootkitcmd/ ← chkrootkit使用コマンドを退避先ディレクトリへコピー # chkrootkit -p /root/chkrootkitcmd|grep INFECTED ← 試しに退避したchkrootkit使用コマンドを使用してchkrootkit実行
# zip -r chkrootkitcmd.zip chkrootkitcmd/ && rm -rf chkrootkitcmd ← chkrootkit使用コマンド退避先ディレクトリを圧縮して削除
# echo|mail -a chkrootkitcmd.zip -s chkrootkitcmd.zip root ← chkrootkit使用コマンド(圧縮版)をroot宛にメール送信
# rm -f chkrootkitcmd.zip ← chkrootkit使用コマンド(圧縮版)削除

 

カテゴリー: AlmaLinuxに挑戦 | コメントする

危険なメール


このところ危険なメールが多いなー。上の画像を見て欲い。何と私が差出人になっている。勿論こんなメールは送信していない。この時点でこのメールは破棄すること。実は差出人は偽造できるのだ。昔はデタラメなメアドを使っていたが、私のメールサーバーもそうだが、送信者が実在するか調べてから受信トレイに入れるので、デタラメなメアドは使えなくなっている。なので受信者のメアドに偽装しているのだ。
因みにこのメールはIPアドレスが157.7.88.194から発進されている。またこのメアドのプロバイダーはGMO Internet だ。何で判るかというと、ヘッダーを全て表示するにすると見る事が出来る。(thunderbirdの場合は 表示→ヘッダー→すべて)下記がそのヘッダーの一部

赤線の所が送信してきたIPアドレスで、このアドレスの持ち主を調べると(ここで調べられる) プロバイダーはGMO Internetというわけだ

カテゴリー: 危険メールや手紙、IP | コメントする

不審なメールは中国から多い


皆さんはメールを読む時何に注意しますか?
今後は是非、差出人に注意してください。上の図の赤線の所を見て下さい。ここは
@xfeamqx.cnとなっています。@マーク以降はドメインと呼ばれるもので、誰が(どのドメインから)送ったかがわかりますが、肝心なのは最後の.cnです。これは中国から送られて来ています。ETCが中国からメールを送ることは絶対にありませんので、明らかに騙そうとしていますので、絶対に本文の青線部分はクリックしてはいけません。
ただ、これは偽造出来るので信じてはいけませんが、この場合は明らかにおかしいので偽造はされていません。
差出人を偽造したメールがあるので、興味がある方はここをクリック

カテゴリー: 危険メールや手紙、IP | コメントする

4:tripwireの設定

先ずはインストールだがEPELを有効にしておく事(ここの最後を参照)
参考(https://centossrv.com/tripwire.shtml
# dnf -y install tripwire   インストール
# tripwire-setup-keyfiles  設定ファイルを作成
表示が出た後
Enter the site keyfile passphrase:  ここで自分で決めたサイトPWを入力
Verify the site keyfile passphrase:  再度入力
少しして
Enter the local keyfile passphrase:  ここで自分で決めたローカルPWを入力
Verify the local keyfile passphrase:  再度入力 する
少しして
Please enter your site passphrase:   
となるので、サイトPWを入力 再度聞いてくる、
Please enter your site passphrase:   となるので、サイトPWを入力 これで終了
# gedit /etc/tripwire/twcfg.txt     設定の修正
ROOT =/usr/sbin
POLFILE =/etc/tripwire/tw.pol
DBFILE =/var/lib/tripwire/$(HOSTNAME).twd
REPORTFILE =/var/lib/tripwire/report/$(HOSTNAME)-$(DATE).twr
SITEKEYFILE =/etc/tripwire/site.key
LOCALKEYFILE =/etc/tripwire/$(HOSTNAME)-local.key
EDITOR =/bin/vi LATEPROMPTING =false
LOOSEDIRECTORYCHECKING =true    trueに変更
MAILNOVIOLATIONS =true
EMAILREPORTLEVEL =3
REPORTLEVEL =4             4に変更
MAILMETHOD =SENDMAIL
SYSLOGREPORTING =false
MAILPROGRAM =/usr/sbin/sendmail -oi -t
で保存しておく、次にTripwire設定ファイル(暗号署名版)作成
# twadmin -m F -c /etc/tripwire/tw.cfg -S /etc/tripwire/site.key /etc/tripwire/twcfg.txt
site passphraseを聞いてくるので入力。その後設定ファイルを削除
# rm -f /etc/tripwire/twcfg.txt
ポリシーファイル最適化スクリプト作成
# gedit /etc/tripwire/twpolmake.pl

#!/usr/bin/perl
# Tripwire Policy File customize tool
# ----------------------------------------------------------------
# Copyright (C) 2003 Hiroaki Izumi
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# ----------------------------------------------------------------
# Usage:
#    perl twpolmake.pl {Pol file}
# ----------------------------------------------------------------
#
$POLFILE=$ARGV[0];

open(POL,"$POLFILE") or die "open error: $POLFILE" ;
my($myhost,$thost) ;
my($sharp,$tpath,$cond) ;
my($INRULE) = 0 ;

while (<POL>) {
    chomp;
    if (($thost) = /^HOSTNAME\s*=\s*(.*)\s*;/) {
        $myhost = `hostname` ; chomp($myhost) ;
        if ($thost ne $myhost) {
            $_="HOSTNAME=\"$myhost\";" ;
        }
    }
    elsif ( /^{/ ) {
        $INRULE=1 ;
    }
    elsif ( /^}/ ) {
        $INRULE=0 ;
    }
    elsif ($INRULE == 1 and ($sharp,$tpath,$cond) = /^(\s*\#?\s*)(\/\S+)\b(\s+->\s+.+)$/) {
        $ret = ($sharp =~ s/\#//g) ;
        if ($tpath eq '/sbin/e2fsadm' ) {
            $cond =~ s/;\s+(tune2fs.*)$/; \#$1/ ;
        }
        if (! -s $tpath) {
            $_ = "$sharp#$tpath$cond" if ($ret == 0) ;
        }
        else {
            $_ = "$sharp$tpath$cond" ;
        }
    }
    print "$_\n" ;
}
close(POL) ;

# perl /etc/tripwire/twpolmake.pl /etc/tripwire/twpol.txt > /etc/tripwire/twpol.txt.new ← ポリシーファイル最適化
# echo ! “/var/lib/tripwire/`hostname`.twd ;” >> /etc/tripwire/twpol.txt.new ← Tripwireデータベース自体をチェック対象外にする
# echo ! “/tmp/tripwire.log ;” >> /etc/tripwire/twpol.txt.new ← Tripwireログをチェック対象外にする
最適化済ポリシーファイルを元にポリシーファイル(暗号署名版)作成
# twadmin -m P -c /etc/tripwire/tw.cfg -p /etc/tripwire/tw.pol -S /etc/tripwire/site.key /etc/tripwire/twpol.txt.new
site passphraseを聞いてくるので入力。そしてポリシーファイル(テキスト版)削除
# rm -f /etc/tripwire/twpol.txt*
Tripwireデータベース作成
# tripwire -m i -s -c /etc/tripwire/tw.cfg
local passphraseを聞いてくるので入力 Tripwireチェック実行
# tripwire -m c -s -c /etc/tripwire/tw.cfg

Open Source Tripwire(R) 2.4.3.7 Integrity Check Report

Report generated by:          root
Report created on:            2023年01月17日 05時40分21秒
Database last updated on:     Never

=========================================================================
Report Summary:
=========================================================================

Host name:                    kvm5.inpac.jp
Host IP address:              133.149.213.105
Host ID:                      None
Policy file used:             /etc/tripwire/tw.pol
Configuration file used:      /etc/tripwire/tw.cfg
Database file used:           /var/lib/tripwire/kvm5.inpac.jp.twd
Command line used:            tripwire -m c -s -c /etc/tripwire/tw.cfg 

=========================================================================
Rule Summary: 
=========================================================================

-------------------------------------------------------------------------
  Section: Unix File System
-------------------------------------------------------------------------

  Rule Name                   Severity Level    Added    Removed Modified 
  ---------                       --------------    -----    -------  ---
  User binaries                   66                0        0        0        
  Tripwire Binaries               100               0        0        0        
  Libraries                       66                0        0        0        
  Operating System Utilities      100               0        0        0        
  File System and Disk Administraton Programs
                                  100               0        0        0        
  Kernel Administration Programs  100               0        0        0        
  Networking Programs             100               0        0        0        
  System Administration Programs  100               0        0        0        
  Hardware and Device Control Programs
                                  100               0        0        0        
  System Information Programs     100               0        0        0        
  (/sbin/runlevel)
  Application Information Programs
                                  100               0        0        0        
  (/sbin/rtmon)
  Critical Utility Sym-Links      100               0        0        0        
  Shell Binaries                  100               0        0        0        
  Critical system boot files      100               0        0        0        
* Tripwire Data Files             100               1        0        0        
  System boot changes             100               0        0        0        
  OS executables and libraries    100               0        0        0        
  Security Control                100               0        0        0        
  Login Scripts                   100               0        0        0        
  Critical configuration files    100               0        0        0        
* Root config files               100               0        0        1        
  Invariant Directories           66                0        0        0        
  Temporary directories           33                0        0        0        
  Critical devices                100               0        0        0        
  (/proc/kcore)

Total objects scanned:  44064
Total violations found:  2

=========================================================================
Object Summary: 
=========================================================================
# Section: Unix File System
-------------------------------------------------------------------------

-------------------------------------------------------------------------
Rule Name: Tripwire Data Files (/var/lib/tripwire)
Severity Level: 100
-------------------------------------------------------------------------

Added:
"/var/lib/tripwire/kvm5.inpac.jp.twd"

-------------------------------------------------------------------------
Rule Name: Root config files (/root)
Severity Level: 100
-------------------------------------------------------------------------

Modified:
"/root/.local/share/gnome-shell/application_state"

=========================================================================
Error Report: 
=========================================================================

No Errors

-------------------------------------------------------------------------
*** End of report ***

Open Source Tripwire 2.4 Portions copyright 2000-2018 Tripwire, Inc.  Tripwire is a registered
trademark of Tripwire, Inc. This software comes with ABSOLUTELY NO WARRANTY;
for details use --version. This is free software which may be redistributed
or modified only under certain conditions; see COPYING for details.
All rights reserved.
カテゴリー: AlmaLinuxに挑戦 | コメントする

3:firewalldの設定

# gedit firewall.sh ← ファイアウォール設定スクリプト作成

#!/bin/bash

#—————————————#
# 設定開始 #
#—————————————#

# 内部ネットワークアドレス定義
LOCALNET=192.168.xx.xx/24       伏せ字あり

#—————————————#
# 設定終了 #
#—————————————#

#
# ファイアウォール設定初期化
#
systemctl stop firewalld
rm -f /etc/firewalld/zones/*
rm -f /etc/firewalld/ipsets/*
systemctl start firewalld
firewall-cmd –reload >/dev/null

#
# 内部からのアクセスを許可
#
firewall-cmd –add-rich-rule=”rule family=”ipv4″ source address=”10.0.0.0/8″ accept” –permanent >/dev/null
firewall-cmd –add-rich-rule=”rule family=”ipv4″ source address=”172.16.0.0/12″ accept” –permanent >/dev/null
firewall-cmd –add-rich-rule=”rule family=”ipv4″ source address=”192.168.0.0/16″ accept” –permanent >/dev/null
firewall-cmd –add-rich-rule=”rule family=”ipv4″ source address=”${LOCALNET}” accept” –permanent >/dev/null

#
# SYN Cookiesを有効にする
# ※TCP SYN Flood攻撃対策
#
sysctl -w net.ipv4.tcp_syncookies=1 > /dev/null
sed -i ‘/net.ipv4.tcp_syncookies/d’ /etc/sysctl.conf
echo “net.ipv4.tcp_syncookies=1” >> /etc/sysctl.conf

#
# ブロードキャストアドレス宛pingには応答しない
# ※Smurf攻撃対策
#
sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1 > /dev/null
sed -i ‘/net.ipv4.icmp_echo_ignore_broadcasts/d’ /etc/sysctl.conf
echo “net.ipv4.icmp_echo_ignore_broadcasts=1” >> /etc/sysctl.conf

#
# ICMP Redirectパケットは拒否
#
sed -i ‘/net.ipv4.conf.*.accept_redirects/d’ /etc/sysctl.conf
for dev in `ls /proc/sys/net/ipv4/conf/`
do
sysctl -w net.ipv4.conf.$dev.accept_redirects=0 > /dev/null
echo “net.ipv4.conf.$dev.accept_redirects=0” >> /etc/sysctl.conf
done

#
# Source Routedパケットは拒否
#
sed -i ‘/net.ipv4.conf.*.accept_source_route/d’ /etc/sysctl.conf
for dev in `ls /proc/sys/net/ipv4/conf/`
do
sysctl -w net.ipv4.conf.$dev.accept_source_route=0 > /dev/null
echo “net.ipv4.conf.$dev.accept_source_route=0” >> /etc/sysctl.conf
done

#
# IPアドレスリスト取得
#
IP_LIST=/tmp/cidr.txt
CHK_IP_LIST=/tmp/IPLIST
if [ ! -f ${IP_LIST} ]; then
wget -q http://nami.jp/ipv4bycc/cidr.txt.gz
gunzip -c cidr.txt.gz > ${IP_LIST}
rm -f cidr.txt.gz
fi
rm -f ${CHK_IP_LIST}

#
# ゾーン(日本国内)作成
#

# domestic(日本国内)ゾーン作成
firewall-cmd –new-zone=domestic –permanent >/dev/null

# domestic(日本国内)IPセット作成
firewall-cmd –new-ipset=domestic –type=hash:net –permanent >/dev/null

# 日本国内のIPアドレスリスト作成
domestic_ipset=`mktemp`
for addr in `cat ${IP_LIST} | grep ^JP | awk ‘{print $2}’`
do
echo ${addr} >> ${domestic_ipset}
done

# 日本国内のIPアドレスリストをdomestic(日本国内)IPセットに登録
firewall-cmd –ipset=domestic –add-entries-from-file=${domestic_ipset} –permanent >/dev/null
rm -f ${domestic_ipset}

# domestic(日本国内)IPセットをdomestic(日本国内)ゾーンに登録
firewall-cmd –zone=domestic –add-source=ipset:domestic –permanent >/dev/null

# IPアドレス更新チェック用に退避
grep ^JP ${IP_LIST} >> $CHK_IP_LIST

# 以降,日本国内からのみアクセスを許可したい場合はdomesticゾーンにサービスを追加する

# 全国警察施設への攻撃元上位5カ国(日本・アメリカを除く)からのアクセスを破棄
# 直近1週間の状況 http://www.npa.go.jp/cyberpolice/detect/observation.html
# 前月の状況 http://www.npa.go.jp/cyberpolice/detect/index.html
# 国コード一覧 https://ja.wikipedia.org/wiki/ISO_3166-1#%E7%95%A5%E5%8F%B7%E4%B8%80%E8%A6%A7
DROP_COUNTRY_LIST=(BG HK RO CN GB)

# drop_country(アクセス禁止国)IPセット作成
firewall-cmd –new-ipset=drop_country –type=hash:net –permanent >/dev/null

# アクセス禁止国のIPアドレスリスト作成
drop_ipset=`mktemp`
for country in “${DROP_COUNTRY_LIST[@]}”
do
for addr in `cat ${IP_LIST} | grep ^${country} | awk ‘{print $2}’`
do
echo ${addr} >> ${drop_ipset}
done
grep ^${country} ${IP_LIST} >> ${CHK_IP_LIST}
done

# アクセス禁止国のIPアドレスリストをdrop_country(アクセス禁止国)IPセットに登録
firewall-cmd –ipset=drop_country –add-entries-from-file=${drop_ipset} –permanent >/dev/null
rm -f ${drop_ipset}

# drop_country(アクセス禁止国)IPセットをdropゾーンに登録
firewall-cmd –zone=drop –add-source=ipset:drop_country –permanent >/dev/null

#———————————————————-#
# 各種サービスを公開する場合の設定(ここから) #
#———————————————————-#

# 外部からのSSH(TCP22番ポート)へのアクセスを日本国内からのみ許可
# ※SSHサーバーを公開する場合のみ
firewall-cmd –remove-service=ssh –zone=public –permanent >/dev/null
firewall-cmd –add-service=ssh –zone=domestic –permanent >/dev/null

# 外部からのDNS(TCP/UDP53番ポート)へのアクセスを許可
# ※外部向けDNSサーバーを運用する場合のみ
#firewall-cmd –add-service=dns –permanent >/dev/null

# 外部からのHTTP(TCP80番ポート)へのアクセスを許可
# ※Webサーバーを公開する場合のみ
#firewall-cmd –add-service=http –permanent >/dev/null

# 外部からのHTTPS(TCP443番ポート)へのアクセスを許可
# ※Webサーバーを公開する場合のみ
#firewall-cmd –add-service=https –permanent >/dev/null

# 外部からのSMTP(TCP25番ポート)へのアクセスを許可
# ※SMTPサーバーを公開する場合のみ
#firewall-cmd –add-service=smtp –permanent >/dev/null

# 外部からのSUBMISSION(TCP587番ポート)へのアクセスを日本国内からのみ許可
# ※SMTPサーバーを公開する場合のみ
# ※SMTPSサーバー(TCP465番ポート)を公開する場合は不要
#firewall-cmd –add-service=smtp-submission –zone=domestic –permanent >/dev/null

# 外部からのSMTPS(TCP465番ポート)へのアクセスを日本国内からのみ許可
# ※SMTPSサーバーを公開する場合のみ 2>&1
#firewall-cmd –add-service=smtps –zone=domestic –permanent >/dev/null

# 外部からのPOP3(TCP110番ポート)へのアクセスを日本国内からのみ許可
# ※POP3サーバーを公開する場合のみ
#firewall-cmd –add-service=pop3 –zone=domestic –permanent >/dev/null

# 外部からのPOP3S(TCP995番ポート)へのアクセスを日本国内からのみ許可
# ※POP3Sサーバーを公開する場合のみ
#firewall-cmd –add-service=pop3s –zone=domestic –permanent >/dev/null

# 外部からのIMAP(TCP143番ポート)へのアクセスを日本国内からのみ許可
# ※IMAPサーバーを公開する場合のみ
#firewall-cmd –add-service=imap –zone=domestic –permanent >/dev/null

# 外部からのIMAPS(TCP993番ポート)へのアクセスを日本国内からのみ許可
# ※IMAPSサーバーを公開する場合のみ
#firewall-cmd –add-service=imaps –zone=domestic –permanent >/dev/null

# 外部からのL2TP over IPsec(UDP500番ポート、UDP4500番ポート)へのアクセスを日本国内からのみ許可
# ※SoftEther VPN Serverを公開する場合のみ
#firewall-cmd –add-service=ipsec –zone=domestic –permanent >/dev/null

# 外部からのUsermin(TCP20000番ポート)へのアクセスを日本国内からのみ許可
# ※Userminサーバーを公開する場合のみ
#firewall-cmd –add-port=20000/tcp –zone=domestic –permanent >/dev/null

# 外部からのSubsonic(TCP4040番ポート)へのアクセスを日本国内からのみ許可
# ※Subsonicを公開する場合のみ
#firewall-cmd –add-port=4040/tcp –zone=domestic –permanent >/dev/null

#———————————————————-#
# 各種サービスを公開する場合の設定(ここまで) #
#———————————————————-#

# 拒否IPアドレスからのアクセスはログを記録せずに破棄
# ※拒否IPアドレスは/root/deny_ipに1行ごとに記述しておくこと
# (/root/deny_ipがなければなにもしない)
if [ -s /root/deny_ip ]; then
for ip in `cat /root/deny_ip`
do
firewall-cmd –zone=drop –permanent –add-source=${ip} –permanent >/dev/null
done
fi

# ファイアウォール設定反映
firewall-cmd –reload >/dev/null

# bash firewall.sh ← ファイアウォール設定スクリプト実行
# systemctl enable firewalld

IPアドレスリストは頻繁に更新されるので、IPアドレスリスト更新チェックするスクリプト作成
# gedit /etc/cron.daily/iplist_check.sh
#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# 新旧IPLIST差分チェック件数(0を指定するとチェックしない)
# ※新旧IPLIST差分がSABUN_CHKで指定した件数を越える場合はfirewall設定スクリプトを実行しない
# ※新旧IPLIST差分チェック理由はhttp://centossrv.com/bbshtml/webpatio/1592.shtmlを参照
SABUN_CHK=100
[ $# -ne 0 ] && SABUN_CHK=${1}

# IPアドレスリスト取得
IP_LIST=/tmp/cidr.txt
CHK_IP_LIST=/tmp/IPLIST
wget -q http://nami.jp/ipv4bycc/cidr.txt.gz
gunzip -c cidr.txt.gz > $IP_LIST
rm -f cidr.txt.gz

# チェック対象IPアドレスリスト最新化
rm -f IPLIST.new
for country in `awk ‘{print $1}’ $CHK_IP_LIST |uniq`
do
grep ^$country $IP_LIST >> IPLIST.new
done

# チェック対象IPアドレスリスト更新チェック
diff -q $CHK_IP_LIST IPLIST.new > /dev/null 2>&1
if [ $? -ne 0 ]; then
if [ ${SABUN_CHK} -ne 0 ]; then
if [ $(diff $CHK_IP_LIST IPLIST.new | egrep -c ‘<|>’) -gt ${SABUN_CHK} ]; then
(
diff $CHK_IP_LIST IPLIST.new
echo
echo “firewall.sh not executed.”
) | mail -s ‘IPLIST UPDATE’ root
rm -f IPLIST.new
exit
fi
fi
/bin/mv IPLIST.new $CHK_IP_LIST
sh /root/firewall.sh > /dev/null
else
rm -f IPLIST.new
fi
# chmod +x /etc/cron.daily/iplist_check.sh ← IPアドレスリストチェックスクリプトに実行権限付加
# chmod +x firewall.sh ← firewall.shに実行権限付加
# bash firewall.sh     スクリプト実行
# systemctl enable firewalld   firewalldを起動時に有効にする

カテゴリー: AlmaLinuxに挑戦 | コメントする

2:初期設定

*日本語入力
どう言う訳か日本語の指定を指定してインストールしたが日本語の入力が出来ない!
ので、日本語入力が出来るように、右上のスイッチアイコンをクリックし、表示された比較的下に設定(歯車アイコン)があるのでクリック、表示されたウィンドウの左ペインでKeyboardをクリックし下の画像のように続ける

これで出来るようになるが、terminalやブラウザ等を選んでおかないと半角/全角キーを押しても切り替わらない。

*OSをアップデートする
# dnf upgrade

*SELINUXを無効にする
本来は有効のまま使用すべきとは思うが、経験上、有効にしておくと多くの設定で躓くので、私の力量では無効にしておきます。
# gedit /etc/selinux/config
#SELINUX=enforcing  コメントアウトして
SELINUX=disabled  追記する
このままだと再起動が必要ですが、
# setenforce 0
とコマンドを入れ、一時的に無効にしておき、作業を続けます。また、再起動時には無効になります。
RHEL9ではSELINUX=disabled オプションによる SELinux の無効化が出来なくなったとの事で、
# grubby –update-kernel ALL –args selinux=0 注:オプションはーー   selinuxを無効化
# shutdown -r now    再起動で反映される。
上記の様にしてから、念のため
# getenforce
Disabled
に成っている事を確認しておく

*インストール時に作成した一般ユーザーが管理者に出来るように設定
# usermod -G wheel ユーザー名   管理者グループであるwheelグループに入れる
# gedit /etc/pam.d/su
開いたウィンドウの7行目くらいにある1行をコメントを取る
auth required pam_wheel.so use_uid  先頭に付いていた#を削除する
で保存しておく

*aliasを設定
このホストから色々なインフォメーションやエラーがrootに送信されるので、それを自分のメアドに転送する
# gedit /etc/aliases
開いたウィンドウの一番下にある所を上記のように変更
#root:             marc     #でコメントアウト
root:               自分のメアド  追記
そして
# echo test|mail root   root宛に本文がtestのメールを送る
あれーエラーだ
postdrop: warning: unable to look up public/pickup: No such file or directory
調べてみるとpostfixが起動していないことが原因。メールコマンドがmailxからs-nailに変更になったので、インストールしていたが、最初からpostfixがインストールされていた見たい。なので
# systemctl start postfix   postfixを起動
# systemctl enable postfix  再起動時に起動する
で、無事メールがくるようになった。

追記2023/2/10: 別のコンピューターにalmalinuxをインストールして同じ設定をしたが、まだメールが来ない。メールログを確認する(/var/log/maillog)、すると
Feb 10 05:05:33 test postfix/smtp[13792]: C0F1E200B084: to=<s-report@kinryo.net>, orig_to=<root>, relay=kinryo.net[133.149.213.105]:25, delay=1088, delays=1087/0.03/0.53/0.03, dsn=4.1.8, status=deferred (host kinryo.net[133.149.213.105] said: 450 4.1.8 <root@test.locatdomain>: Sender address rejected: Domain not found (in reply to RCPT TO command))
のログがあった。つまりtest.locatdomain何て知らないよと言っているのでメールが来ない。メールサーバーはこのホストではなく、別のホストで動いているので、ドメインを変更する
# gedit /etc/hostname
test.kinryo.net  → これに変更して保存
# shutdown -r now  → 設定を反映させるために再起動
これでメールは来るが、tripwireのレポートでIPアドレスがmailサーバーが動いているサーバーのグローバルIPになっている(多分tripwireが自分のIPを調べる時にDNSをチェックしているみたい)

*夜間に自動アップデート
# dnf install dnf-automatic  → 自動アップデートをインストール
# gedit /etc/dnf/automatic.conf
# アップデートの種類
# default すべてのパッケージ
# security セキュリティ関係のみ
upgrade_type = default

# アップデート適用のタイミング
# yes 即時、自動更新したいならこちら
# no ダウンロードのみ
apply_updates = yes
上記の設定を記入後保存

*サードパーティリポジトリの追加  注:オプションはーー
*EPEL
# dnf config-manager –set-enabled crb
# dnf -y install epel-release
# dnf -y update
*Remi
# dnf -y install https://rpms.remirepo.net/enterprise/remi-release-9.rpm
# dnf -y config-manager –set-enabled remi
でEPELとRemiのリポジトリーを追加しておきます。

 

カテゴリー: AlmaLinuxに挑戦 | 1件のコメント