みちのいに!!

自分のメモと、他にもハマる人がいそうなことを書く

くれはちゃん育成日記2日目(お名前VPSに入れたArchLinuxを設定する話)

契約したVPSには「くれは」という名前をつけました。
早速設定していきます。
といっても
http://www.karakaram.com/onamae-com-vps1-ssh
をなぞっていきますが


作業用ユーザ追加

  • m でホームディレクトリを作成する。
  • Gでグループ追加
useradd -m your_user_name -G wheel 
passwd your_user_name

rootとれるようにする

前回記事でずっとnanoで編集してたけど、vi入ってたのに気付かなかった。

vi /etc/pam.d/su

auth required pam_sheel.so use_uid
をアンコメント
:wq

sudoできるようにする

visudo

%wheel ALL=(ALL) ALL
をアンコメント
:wq

SSHできるようにする

pacman -S openssh
SSH key 作る
ssh-keygen -t rsa -C "comment"

質問はすべてデフォルトで

SSH key パーミッション変えて端末に送る
cd ~
chmod 700 .ssh
chmod 600 .ssh/*
mv .ssh/id_rsa.pub .ssh/authorized_keys
#ここでSSH起動
systemctl restart sshd 

なんかこのへんでトラブったので、一応いろいろ確認方法をメモ

#起動してるかどうか
systemctl status sshd
#ログチェック
journalctl -u sshd |tail -100

http://unix.stackexchange.com/questions/114189/where-are-my-sshd-logs

SSHD起動したら、id_rsaをダウンロード
WindowsだけどMinGW+mintty環境なので、Macと似たような感じでやっていきます。
左から秘密鍵を予めダウンロードしておくこと。

(ローカルPC)
mkdir .ssh
scp -i vpsxxxxxxx-001.key youruser@xxx.xxx.xxx.xxx:/home/youruser/.ssh/id_rsa .ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*

ssh yourusername@xxx.xxx.xxx.xxx -i id_rsa

これでつながれば成功

SSH設定

/etc/ssh/sshd_config を編集していく
参考:
http://www14.plala.or.jp/campus-note/vine_linux/server_ssh/sshd_config.html
http://www.karakaram.com/onamae-com-vps1-ssh#sshd-config
https://wiki.archlinux.org/index.php/Secure_Shell_%28%E6%97%A5%E6%9C%AC%E8%AA%9E%29
http://morinezumiiii.hatenablog.com/entry/2012/12/21/125119
プロトコルは2で
Portは適当に
Rootログインを禁止
パスワード認証は禁止

Protocol 2
Port 10022
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
AllowUsers yourusername

自動起動するようにします

systemctl enable sshd
iptables設定

https://wiki.archlinux.org/index.php/Iptables_%28%E6%97%A5%E6%9C%AC%E8%AA%9E%29
初期設定と起動と起動自動化

touch /etc/iptables/iptables.rules
systemctl start iptables
systemctl enable iptables

iptablesの設定ファイルをシェルスクリプトを利用して動的に作成 | OXY NOTES
を参考にiptables.shを作成して実行して設定

まあこんな感じで……ほとんど写しただけですが。
部分的にアンコメントしてる部分は保留で……

#!/bin/bash

#reset
iptables -F
iptables -X

iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

#ip spoofing
iptables -A INPUT -i eth0 -s 127.0.0.1/8 -j DROP
iptables -A INPUT -i eth0 -s 10.0.0.0/8 -j DROP
iptables -A INPUT -i eth0 -s 172.16.0.0/12 -j DROP
iptables -A INPUT -i eth0 -s 192.168.0.0/16 -j DROP
iptables -A INPUT -i eth0 -s 192.168.0.0/24 -j DROP

#ping protect
iptables -N PING_ATTACK
#  ping of death and ping flood
iptables -A PING_ATTACK -m length --length :85 -m limit --limit 1/s --limit-burst 4 -j ACCEPT
iptables -A PING_ATTACK -j LOG --log-prefix "[IPTABLES PINGATTACK] :" --log-level=debug
iptables -A PING_ATTACK -j DROP
iptables -A INPUT -p icmp --icmp-type 8 -j PING_ATTACK

#smurf
iptables -A INPUT -d 255.255.255.255 -j DROP
iptables -A INPUT -d 224.0.0.1 -j DROP
iptables -A INPUT -d 157.7.242.255 -j DROP
#sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1 
#syn flood cookie
#sysctl -w net.ipv4.tcp_syncookies=1 

# Auth/IDENT 113
iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset


#reject ip
if [ -s /m77/deny_ip ]; then
        for ip in 'cat /root/deny_ip'
        do
                iptables -I INPUT -s $ip -j DROP
        done
fi

#accept access from japan
if [ -s /tmp/iplist ]; then
        iptables -N ACCEPT_JP_FILTER
        sed -n 's/^JP\t//p' /tmp/iplist | while read address;
        do
                iptables -A ACCEPT_JP_FILTER -s $address -j ACCEPT
        done
fi

#Stateful Packet Inspection
iptables -A INPUT -p tcp ! --tcp-flags SYN,RST,ACK SYN -m state --state NEW -j DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#####################
##    OPEN  PORT   ##
#####################

#Loop back interface
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

#SSH
iptables -A INPUT -p tcp -m state --state NEW --dport 10022 -j ACCEPT_JP_FILTER
#HTTP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#DNS
iptables -N DNSAMP
iptables -A DNSAMP -m recent --name dnsamp --set
iptables -A DNSAMP -m recent --name dnsamp --rcheck --seconds 60 --hitcount 5 -j LOG --log-prefix "[IPTABLES DNSAMP] : " --log-level=debug
iptables -A DNSAMP -m recent --name dnsamp --rcheck --seconds 60 --hitcount 5 -j DROP
iptables -A DNSAMP -j ACCEPT

iptables -A INPUT -p udp -m state --state NEW --dport 53 -i eth0 -j DNSAMP



###################
##    LOGGING    ##
###################
iptables -A INPUT -m limit --limit 1/s -j LOG --log-prefix "[IPTABLES DROP INPUT] : " --log-level=debug
iptables -A INPUT -j DROP

###################
##   SAVESTART   ##
###################
#/etc/rc.d/init.d/iptables save
#/etc/rc.d/init.d/iptables start
#systemctl reload iptables
#systemctl restart iptables

URLはCentOSだと文句なしに動いたけど、Archだとちょっと読み替えが必要
sysctl - ArchWiki
wgetとか入ってないのでpacman -S する必要あり。
gunzipじゃなくてgzip -dを使う。
などなど(忘れた)
/root/script に iptables.sh iplist_check.sh を入れました。

明日はiplistを毎日更新できるようにします。
cronもないので、Systemd/timersを利用します。