2017年02月

2017年02月28日
Ubuntu で iptables の設定を保存する

僕は今まで CentOS をメインで利用しており、このとき iptables は service コマンドなどで管理できました。
しかし Ubuntu は この方法ではできないようです。

一番簡単なのは iptables-persistent を利用するようです。
iptables-persistent の場合
ルールの記載
#vi /etc/iptables/iptables.rules

# iptables-restore < /etc/iptables/iptables.rules



とりあえず僕の環境では以下の方法で行いました。

参考
LinuxのUbuntuやDebianでiptablesの設定方法・設定ファイル
IptablesHowTo

・設定ファイル
/etc/network/if-pre-up.d/iptables.up
/etc/network/if-post-down.d/iptables.down
/etc/network/interfaces
/etc/sysconfig/iptables.rule # 今までと同じ方法でルールを編集したかったので


まずは起動時に設定を読み込むようにします。
# vi /etc/network/if-pre-up.d/iptables.up
----
#!/bin/sh
/sbin/iptables-restore < /etc/sysconfig/iptables.rule
----
権限付与
# chmod +x /etc/network/if-pre-up.d/iptables.up

次はiptables の初期化設定を行います。
# vi /etc/network/if-post-down.d/iptables.down
----
#!/bin/sh
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F # ルールの削除
iptables -Z # カウンターのリセット
iptables -X # 追加チェインの削除
# nat 等の設定は上記コマンドでは初期化されません。
----
権限付与
# chmod +x /etc/network/if-post-down.d/iptables.down

・インターフェースの設定で読み込むように設定
/etc/network/interfaces
pre-up /etc/network/if-pre-up.d/iptables.up
post-down /etc/network/if-post-down.d/iptables.down

※なお今回の運用では、iptables に設定するコマンドはすべてテキストファイルに書き込むことを前提としています。ipta
bles コマンドを利用して直接書き込んだ場合には、service networking restart などによって消えてしまうので注意が必要です。

あと、設定ファイルを書き換えた場合には、 iptables.up を直接実行します。



2017年02月27日
以下の設定がキモです。
bridge irb
interface Vlan1
no ip address
bridge-group 1
bridge-group 1 input-type-list 200

なお、コンソールがないとできませんでした。
もしくは、複数のIPを割り当てて、セッションが切れないように(もしくは切れても再接続できるように)しておかないと大変です。
今回の設定では、ローカルIPは interface BVI1 に割り当てているため、VLAN1で設定しているとTelnet応答がなくなります。


参考
IPv6 パス スルー設定例(ルーテッド インターフェイスの場合)

#show run
!
version 15.5
service timestamps debug datetime msec
service timestamps log datetime msec
service password-encryption
!
hostname HomeGW
!
boot-start-marker
boot-end-marker
!
!
logging buffered 51200 warnings
!
no aaa new-model
ethernet lmi ce
clock timezone GMT 9 0
!
!
!
!
!
ip nbar http-services
!
!
ip dhcp excluded-address 10.10.10.1
!
ip dhcp pool ccp-pool
import all
network 10.10.10.0 255.255.255.128
default-router 10.10.10.1
dns-server 10.10.10.1
lease 0 2
!
!
!
ip domain name mydomain
ip cef
ipv6 unicast-routing
ipv6 cef
!
!
flow record nbar-appmon
match ipv4 source address
match ipv4 destination address
match application name
collect interface output
collect counter bytes
collect counter packets
collect timestamp absolute first
collect timestamp absolute last
!
!
flow monitor application-mon
cache timeout active 60
record nbar-appmon
!
parameter-map type inspect global
max-incomplete low 18000
max-incomplete high 20000
nbar-classify
!
license udi pid C841M-4X-JSEC/K9 sn XXX
!
!
object-group network Others_dst_net
any
!
object-group network Others_src_net
any
!
object-group service Others_svc
ip
!
object-group network Web_dst_net
any
!
object-group network Web_src_net
any
!
object-group service Web_svc
ip
!
object-group network local_cws_net
!
object-group network local_lan_subnets
any
10.10.10.0 255.255.255.128
!
object-group network vpn_remote_subnets
any
!
username admin privilege 15 secret 5 XXX
!
redundancy
!
!
!
!
no cdp run
!
!
class-map type inspect match-any Others_app
match protocol https
match protocol smtp
match protocol pop3
match protocol imap
match protocol sip
match protocol ftp
match protocol dns
match protocol icmp
class-map type inspect match-any Web_app
match protocol http
class-map type inspect match-all Others
match class-map Others_app
match access-group name Others_acl
class-map type inspect match-all Web
match class-map Web_app
match access-group name Web_acl
!
policy-map type inspect LAN-WAN-POLICY
class type inspect Web
inspect
class type inspect Others
inspect
class class-default
drop log
!
zone security LAN
zone security WAN
zone security VPN
zone security DMZ
zone-pair security LAN-WAN source LAN destination WAN
service-policy type inspect LAN-WAN-POLICY
!
!
!
!
!
!
bridge irb
!
!
!
!
interface GigabitEthernet0/0
no ip address
!
interface GigabitEthernet0/1
no ip address
!
interface GigabitEthernet0/2
no ip address
!
interface GigabitEthernet0/3
no ip address
!
interface GigabitEthernet0/4
no ip address
ip tcp adjust-mss 1412
duplex auto
speed auto
pppoe enable group global
pppoe-client dial-pool-number 1
bridge-group 1
!
interface GigabitEthernet0/5
no ip address
ip tcp adjust-mss 1412
shutdown
duplex auto
speed auto
!
interface Vlan1
no ip address
bridge-group 1
bridge-group 1 input-type-list 200
!
interface Dialer1
mtu 1454
ip address negotiated
ip nat outside
ip virtual-reassembly in
encapsulation ppp
ip tcp adjust-mss 1412
dialer pool 1
dialer-group 1
ppp mtu adaptive
ppp authentication chap callin
ppp chap hostname XXX
ppp chap password 7 XXX
ppp ipcp dns request
no cdp enable
!
interface BVI1
ip address 10.10.10.1 255.255.255.0
ip nat inside
ip virtual-reassembly in
ip tcp adjust-mss 1414
!
ip forward-protocol nd
ip http server
ip http access-class 23
ip http authentication local
ip http secure-server
ip http timeout-policy idle 60 life 86400 requests 10000
!
!
ip dns server
ip nat inside source list 23 interface Dialer1 overload
ip route 0.0.0.0 0.0.0.0 Dialer1 permanent
!
ip access-list extended Others_acl
permit object-group Others_svc object-group Others_src_net object-group Others_dst_net
ip access-list extended Web_acl
permit object-group Web_svc object-group Web_src_net object-group Web_dst_net
ip access-list extended nat-list
permit ip object-group local_lan_subnets any
deny ip any any
!
dialer-list 1 protocol ip permit
!
!
access-list 23 permit 10.10.10.0 0.0.0.127
access-list 200 permit 0x86DD 0x0000
access-list 200 permit 0x0800 0x0000
access-list 200 permit 0x0806 0x0000
bridge 1 protocol ieee
bridge 1 route ip
!
!
line con 0
login local
no modem enable
line vty 0 4
access-class 23 in
privilege level 15
login local
transport input telnet ssh
line vty 5 15
access-class 23 in
privilege level 15
login local
transport input telnet ssh
!
scheduler allocate 20000 1000
ntp server ntp.jst.mfeed.ad.jp
!
end

2017年02月26日
メモです。
静的なhtmlでSSIを利用しているサイトがありました。

僕が設定した apache は標準的な設定になっており、SSIについては、 shtml の拡張子が必要でした。
ということで以下の設定を追加です。

httpd.conf に以下の内容を追加

Options Includes
AddType text/html .shtml .html
AddOutputFilter INCLUDES .shtml .html

これで html 拡張子の状態でもSSIが有効になりました。




2017年02月25日
最近送られてくるスパムメールがかなり減ってきたと思っていました。

フィルターを適切に管理しているからか、先方が面倒になって送らなくなったのか。。。それはわかりませんが、以前のようにがっつりとしたスパムは減ったように思っていたのです。

かつては、ちょっと油断したら、1日に100通とかありましたからね。

しかし先日になって、1日に1,000通近くも迷惑メールが送られてきました。その執念すごい!
これは同じ業者で、異なるIPから・・・そうはいっても、/24の範囲とかで・・・送られてきていました。

僕が管理しているメールサーバーでは、フィルターをかけているので、このような大量のスパムメールを送信してくるサーバーについては、通常DNSBLに該当するようになります。
このDNSBLに該当すれば、拒否が始まります。しかし該当するまでの1日のうちに、さっさと送りきってしまうという考えなのでしょうか。
該当するまでの短時間が勝負ですね。それもまたすごいことです。

今回のIPは 103.236.182.XX というものでした。

スパムメール発信元 IPアドレス一覧
上記情報では、なかなか闇が深そうでした。

コメント欄でもやりとりがあるようなのですが、 TOKAIコミュニケーションズ というのは、スパム発信元を調べていると、見かけることがあるように思いました。


2017年02月24日
僕の悪い癖なんですが、その場しのぎで設定をしてしまうことが、本当によくあります。
それで大体苦労しています。とくに iptables はただ設定しただけではログも出ないので、iptables が原因なのか、それ以外なのかの判別が難しいのです。

そこで最近はなるべく iptables についてはチェーンを使って管理するようにしました。

ということでメモしておきます。

/etc/sysconfig/iptables

# SMTP Port25 を許可するIP(これは結構ゆるめに。)
:Allow_SMTP_IP - [0:0]

# 日本以外のIPで拒否する場合にはここに。
# 海外からのアクセスは少ないので、国ごとにがっつりと拒否の場合が多い
:non_JP_Deny - [0:0]

# 日本からのIPで拒否する場合はここ。そして問題が起きたときは、ここの可能性が多いので原因がわかるまではフィルタを外すことも検討
:JP_Deny - [0:0]

#先頭の方に書く。
# SMTPアクセスの場合には、まず許可されたIPのチェックを行う。許可された場合には次の処理に移る
-A INPUT -j Allow_SMTP_IP -p tcp -m state --state NEW -m tcp --dport 25

#すべてのIPを以下のチェーンに流して検査する
-A INPUT -j non_JP_Deny
-A INPUT -j JP_Deny

それ以外のフィルタ(例/抜粋)
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

それぞれのフィルタに拒否/許可するIPを記載する
-A Allow_SMTP_IP -s X.X.X.X -j ACCEPT
-A JP_Deny -s X.X.X.X -j DROP
-A non_JP_Deny -s X.X.X.X -j DROP

2017年02月23日
jsonを利用するようです。・・・まったくわからないけど・・。

AWS IP アドレスの範囲

■jqのインストール
※以下の方法を使えばめっちゃ簡単
jq コマンドの Linux への速攻インストール

jq
linux 64bit をダウンロードしました。

#wget https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64


■jqインストール
#mv ./jq-linux64 /usr/local/bin/jq
#chmod +x /usr/local/bin/jq

これで利用可能になりました。


■AWSのIPリストをダウンロード
# wget https://ip-ranges.amazonaws.com/ip-ranges.json

■検索
AWSが利用しているIPアドレスを取り出してみた

jq -r '.prefixes[] | if .service == "EC2" then .ip_prefix else empty end' < ip-ranges.json


これでたくさん出てきました。

2017年02月22日
非常に簡単な方法です。

# vi /etc/postfix/main.cf
header_checks = regexp:/etc/postfix/header_checks

以下の内容を記載
/^Return-Path:.*@XXXX/ DISCARD

reject の場合にはそのように記載します。
reject は先方に拒否した旨を伝えます。discard は黙って拒否します。

ログには以下のように出てきました。
※これは reject 設定の場合です。
postfix/cleanup[13604]: 5E6D0461553: reject: header Return-Path: from local; from= to=: 5.7.1 message content rejected