所有由ts发布的文章

Windows 10 20H2更新时总是报0xc1900204错误的解决

我的台式机Windows 10版本是20H2,再往后更新的时候总是失败,困扰两年了,其间在网上找了很多方法,基本上都说是windows update服务有问题了,重置一下、删除临时文件等办法,照着做了毫无作用。近日突然想起来用英文搜了一下,总算有所突破,终于解决了多年顽疾,成功更新到最新的22H2。

这一问题其实是Win10不知道哪一次更新时带来的一个bug,主要是内部标识的版本号错误,但这一问题始终没有解决。

解决方法就是在注册表编辑器中找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\EditionVersion这个键,修改两个值:

  • “EditionBuildNumber”=dword:00004a62 ==> 把4a62 改成 4a61.
  • “EditionBuildQfe”=dword:0000023c ==> 改成1fc
  • 改完后如下图

参考https://www.yourwindowsguide.com/2020/10/cannot-initiate-repair-install-windows-10-20h2.html#.YkmYMv9N0go

华硕路由器官版固件自动执行自定义脚本的方法

以前开放的华硕路由器官版固件开机自动执行脚本的很多方法,目前已经失效。根据网上搜索和自己实践的情况看,目前只有利用usb_mount时候的钩子自动执行脚本还可以用,前提是插一个U盘在路由器上。

开启U盘装载自动执行脚本方法如下:

nvram set script_usbmount="sh /jffs/init.sh"
nvram commit

其中/jffs/init.sh是需要自动执行的脚本,例如:

#!/bin/sh
sed -i '$a192.168.3.123 myhost' /etc/hosts

#不能通过重启服务的方式,因为会还原hosts文件,只能kill
killall -SIGHUP dnsmasq

树莓派配置V2ray客户端及透明网关

透明网关搭建

v2搭建及测试

安装v2

  1. 官方一键脚本1
    sudo bash <(curl -L -s https://install.direct/go.sh)
  2. 下载脚本安装,其实与第1种方法一样
    wget https://install.direct/go.sh
    sudo chmod +x go.sh
    sudo bash go.sh
  3. 离线安装,在网络不佳或不合适的情况下使用本地安装在第2步中下载 go.sh 同时在v2ray-core-releases下载 v2ray-linux-arm.zip ,如果树莓派中网络不佳可以在自己的PC机下载完之后迁移文件到树莓派中。
    wget -O v2ray_install.sh https://install.direct/go.sh
    chmod +x v2ray_install.sh
    sudo ./v2ray_install.sh –local v2ray-linux-arm.zip
    配置v2
  4. 后续操作默认切换到 root 权限下,填写v2的配置json。
    sudo su root
    cat > /etc/v2ray/config.json << EOF
    {
    “log”: {
    “loglevel”: “warning”
    },
    “inbounds”: [
    {
    “tag”: “proxy”,
    “port”: 1090, // 监听端口
    “listen”: “127.0.0.1”,
    “protocol”: “socks”, // 入口协议为 SOCKS 5
    “sniffing”: {
    “enabled”: true,
    “destOverride”: [
    “http”,
    “tls”
    ]
    },
    “settings”: {
    “auth”: “noauth”, //socks的认证设置,noauth 代表不认证,由于 socks 通常在客户端使用,所以这里不认证
    “udp”: true
    },
    “streamSettings”: null
    }
    ],
    “outbounds”: [
    {
    “tag”: “proxy”,
    “protocol”: “vmess”, // 出口协议
    “settings”: {
    “vnext”: [
    {
    “address”: “server.com”, // 服务器地址,请修改为你自己的服务器 IP 或域名
    “port”: 10010, // 服务器端口
    “users”: [
    {
    “id”: “b831381d-6324-4d53-ad4f-8cda48b30811”, // 用户 ID,必须与服务器端配置相同
    “alterId”: 32 // 此处的值也应当与服务器相同
    }
    ]
    }
    ]
    }
    }
    ]
    }
    EOF
    对上述配置中的protocol以及vnext 下的 address 、 port 、 id 、 alterId 进行修改,这是必须修改的内容,其含义可参照注释,其他内容可不改变,如果懂得配置的话可以自行修改其余内容。
  5. 开启v2服务下面命令中以 # 开头为注释内容。
    systemctl start v2ray
    # 或使用 service v2ray start
    ps -ef|grep v2ray
    # 或使用 service v2ray status
    正常情况下,v2已经开始运行,查看状态结果如下图。
    v2正常运行配置
    v2正常运行状态

全局代理及测试

  1. 配置全局代理编译安装 ProxyChains-NG 进行全局代理设置。
    git clone https://github.com/rofl0r/proxychains-ng.git
    cd /home/pi/proxychains-ng/
    ./configure && make && make install

    # 设置及修改配置
    cp ./src/proxychains.conf /etc/proxychains.conf
    vim /etc/proxychains.conf
    proxychains.conf配置文件的最后部分内容做以下修改:1
    2
    – socks4 127.0.0.1 9050
    + socks5 127.0.0.1 1090
  2. 测试v2通过 IP 以及 HTTP 响应码测试 v2 是否搭建成功。
    # 返回未代理前的本地公网地址
    curl ip.sb
    # 返回代理过的v2服务器地址,表示搭建成功,若长时间无响应或返回非服务端 IP 地址的内容则表示搭建失败
    proxychains4 curl ip.sb
    # 返回 200 或 301 表示搭建成功,若返回 000 或长时间无反应则表示搭建失败
    proxychains4 curl -so /dev/null -w “%{http_code}” google.com -x socks5://127.0.0.1:1090

透明网关搭建

V2配置透明代理的入站和DNS分流

将以下配置内容覆盖原先的v2配置内容,即修改 /etc/v2ray/config.json 下的内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
{
“inbounds”: [
{
“tag”:”transparent”,
“port”: 12345,
“protocol”: “dokodemo-door”,
“settings”: {
“network”: “tcp,udp”,
“followRedirect”: true
},
“sniffing”: {
“enabled”: true,
“destOverride”: [
“http”,
“tls”
]
},
“streamSettings”: {
“sockopt”: {
“tproxy”: “tproxy” // 透明代理使用 TPROXY 方式
}
}
},
{
“port”: 1090,
“protocol”: “socks”, // 入口协议为 SOCKS 5
“sniffing”: {
“enabled”: true,
“destOverride”: [“http”, “tls”]
},
“settings”: {
“auth”: “noauth”
}
}
],
“outbounds”: [
{
“tag”: “proxy”,
“protocol”: “vmess”, // 代理服务器
“settings”: {
“vnext”: [
{
“address”: “server.com”, // 服务器地址,请修改为你自己的服务器 IP 或域名
“port”: 10010, // 服务器端口
“users”: [
{
“id”: “b831381d-6324-4d53-ad4f-8cda48b30811”, // 用户 ID,必须与服务器端配置相同
“alterId”: 32 // 此处的值也应当与服务器相同
}
]
},
“streamSettings”: {
“sockopt”: {
“mark”: 255
}
},
“mux”: {
“enabled”: true
}
},
{
“tag”: “direct”,
“protocol”: “freedom”,
“settings”: {
“domainStrategy”: “UseIP”
},
“streamSettings”: {
“sockopt”: {
“mark”: 255
}
}
},
{
“tag”: “block”,
“protocol”: “blackhole”,
“settings”: {
“response”: {
“type”: “http”
}
}
},
{
“tag”: “dns-out”,
“protocol”: “dns”,
“streamSettings”: {
“sockopt”: {
“mark”: 255
}
}
}
],
“dns”: {
“servers”: [
“8.8.8.8”, // 非中中国大陆域名使用 Google 的 DNS
“1.1.1.1”, // 非中中国大陆域名使用 Cloudflare 的 DNS(备用)
“114.114.114.114”, // 114 的 DNS (备用)
{
“address”: “223.5.5.5”, //中国大陆域名使用阿里的 DNS
“port”: 53,
“domains”: [
“geosite:cn”,
“ntp.org”, // NTP 服务器
“$myserver.address” // 此处改为你 VPS 的域名
]
}
]
},
“routing”: {
“domainStrategy”: “IPOnDemand”,
“rules”: [
{ // 劫持 53 端口 UDP 流量,使用 V2Ray 的 DNS
“type”: “field”,
“inboundTag”: [
“transparent”
],
“port”: 53,
“network”: “udp”,
“outboundTag”: “dns-out”
},
{
// 直连 123 端口 UDP 流量(NTP 协议)
“type”: “field”,
“inboundTag”: [
“transparent”
],
“port”: 123,
“network”: “udp”,
“outboundTag”: “direct”
},
{
“type”: “field”,
“ip”: [
// 设置 DNS 配置中的国内 DNS 服务器地址直连,以达到 DNS 分流目的
“223.5.5.5”,
“114.114.114.114”
],
“outboundTag”: “direct”
},
{
“type”: “field”,
“ip”: [
// 设置 DNS 配置中的国内 DNS 服务器地址走代理,以达到 DNS 分流目的
“8.8.8.8”,
“1.1.1.1”
],
“outboundTag”: “proxy” // 改为你自己代理的出站 tag
},
{
// 广告拦截
“type”: “field”,
“domain”: [
“geosite:category-ads-all”
],
“outboundTag”: “block”
},
{
// BT 流量直连
“type”: “field”,
“protocol”:[“bittorrent”],
“outboundTag”: “direct”
},
{
// 直连中国大陆主流网站 ip 和 保留 ip
“type”: “field”,
“ip”: [
“geoip:private”,
“geoip:cn”
],
“outboundTag”: “direct”
},
{
// 直连中国大陆主流网站域名
“type”: “field”,
“domain”: [
“geosite:cn”
],
“outboundTag”: “direct”
}
]
}
}

同样的,对上述配置中的protocol以及 vnext 下的 address 、 port 、 id 、 alterId 进行修改,这是必须修改的内容,其含义可参照注释,其他内容可不改变,如果懂得配置的话可以自行修改其余内容。

配置透明代理规则

本文使用方法是tproxy,同时保证树莓派和其他客户端均能实现科学上网。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 设置策略路由
ip rule add fwmark 1 table 100
ip route add local 0.0.0.0/0 dev lo table 100

# 代理局域网设备
iptables -t mangle -N V2RAY
iptables -t mangle -A V2RAY -d 127.0.0.1/32 -j RETURN
iptables -t mangle -A V2RAY -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A V2RAY -d 255.255.255.255/32 -j RETURN
# 直连局域网,避免 V2Ray 无法启动时无法连网关的 SSH,如果你配置的是其他网段(如 10.x.x.x 等),则修改成自己的网段
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p tcp -j RETURN
# 直连局域网,53 端口除外(因为要使用 V2Ray 的 DNS 解析)
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p udp ! –dport 53 -j RETURN
# 给 UDP 打标记 1,转发至 12345 端口
iptables -t mangle -A V2RAY -p udp -j TPROXY –on-port 12345 –tproxy-mark 1
# 给 TCP 打标记 1,转发至 12345 端口
iptables -t mangle -A V2RAY -p tcp -j TPROXY –on-port 12345 –tproxy-mark 1
# 应用规则
iptables -t mangle -A PREROUTING -j V2RAY

# 代理网关本机
iptables -t mangle -N V2RAY_MASK
iptables -t mangle -A V2RAY_MASK -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A V2RAY_MASK -d 255.255.255.255/32 -j RETURN
# 直连局域网
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p tcp -j RETURN
# 直连局域网,53 端口除外(因为要使用 V2Ray 的 DNS)
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p udp ! –dport 53 -j RETURN
# 直连 SO_MARK 为 0xff 的流量(0xff 是 16 进制数,数值上等同与上面V2Ray 配置的 255),此规则目的是避免代理本机(网关)流量出现回环问题
iptables -t mangle -A V2RAY_MASK -j RETURN -m mark –mark 0xff
# 给 UDP 打标记,重路由
iptables -t mangle -A V2RAY_MASK -p udp -j MARK –set-mark 1
# 给 TCP 打标记,重路由
iptables -t mangle -A V2RAY_MASK -p tcp -j MARK –set-mark 1
# 应用规则
iptables -t mangle -A OUTPUT -j V2RAY_MASK

设置开机服务自启

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mkdir -p /etc/iptables && iptables-save > /etc/iptables/rules.v4

# /etc/systemd/system/ 目录下创建一个名为 tproxyrule.service 的文件,写入以下内容
[Unit]
Description=Tproxy rule
After=network.target
Wants=network.target

[Service]

Type=oneshot

#注意分号前后要有空格
ExecStart=/sbin/ip rule add fwmark 1 table 100 ; /sbin/ip route add local 0.0.0.0/0 dev lo table 100 ; /sbin/iptables-restore /etc/iptables/rules.v4

[Install]
WantedBy=multi-user.target

# 执行以下命令完成开机自启
systemctl enable tproxyrule

注意事项

  1. 下载离线版v2时如果树莓派安装的是官方镜像系统则按照文中方法,如果自行安装了64位系统则需要下载 v2ray-linux-arm64.zip
  2. v2的客户端代理端口有点区别,就是socks下和HTTP下是相差一个数的,具体可自行体会;
  3. 配置文件中需要注意树莓派所在网段以及本地代理地址。

结束

树莓派的使用还有许多可待拓展,本文只是介绍了目前最新的将树莓派作为透明网关的配置方法。

参考网址:

https://linwhitehat.github.io/Blog/2020/03/24/%E7%8E%A9%E8%BD%AC%E6%A0%91%E8%8E%93%E6%B4%BE%EF%BC%88%E4%BA%8C%EF%BC%89.html

https://toutyrater.github.io/app/tproxy.html

设置树莓派显示器作为Fluidd的屏幕

1、启用树莓派的auto login,启用console auto login

2、安装openbox

sudo apt-get install --no-install-recommends xserver-xorg x11-xserver-utils xinit xinput x11-utils openbox -y

3、安装chrome浏览器

sudo apt-get install --no-install-recommends chromium-browser -y

4、修改/etc/xdg/openbox/autostart,添加以下内容

# Disable any form of screen saver / screen blanking / power management
xset s off
xset s noblank
xset -dpms

# Allow quitting the X server with CTRL-ATL-Backspace
setxkbmap -option terminate:ctrl_alt_bksp

# Start Chromium in kiosk mode
sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' ~/.config/chromium/'Local State'
sed -i 's/"exited_cleanly":false/"exited_cleanly":true/; s/"exit_type":"[^"]\+"/"exit_type":"Normal"/' ~/.config/chromium/Default/Preferences
chromium-browser --disable-infobars --kiosk 'http://127.0.0.1'

5、修改~/.bash_profile,增加以下内容

[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && startx -- -nocursor

屏蔽刷postfix的 IP脚本

#!/bin/bash

LOGFILE="/var/log/maillog"

#统计maillog中authentication failure的IP个数与IP
grep "authentication failure" $LOGFILE|awk '{print $7}'|grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"|sort|uniq -c > af_iplist.txt

#取出AF出现大于300次时的IP
awk '$1>300 {print $2}' af_iplist.txt > block_ip_list.txt

#大于300次AF的IP添加到iptables中
cat block_ip_list.txt|while read line
do
/sbin/iptables -nL | grep $line
if [ $? != 0 ]
then
    iptables -I INPUT -s $line -j DROP
fi
done

参考网址https://blog.csdn.net/github_38816863/article/details/72614694

给Postfix增加邮件过滤

前一阵子邮件服务器被攻击,后来连续收到大量退信通知,考虑通过postfix的邮件过滤机制把所有退信通知屏蔽吊。实际执行情况看来效果很好。具体操作为:

  • 一、在main.cf里面增加:header_checks = regexp:/etc/postfix/header_checks

二、编辑/etc/postfix/header_checks,

格式为:/正则表达式/ 动作 [说明文本]

例如增加以下内容:

/^Subject:.*Deliver*/ DISCARD Delivery fail notification
/^Subject:.*Undeliver*/ DISCARD Delivery fail notification
/^Subject:.*Returned\ mail*/ DISCARD Delivery fail notification
/^Sender:.*MAILER-DAEMON*/ DISCARD Mail system notification
/^Sender:.*postmaster*/ DISCARD Mail system notification

  • 三、执行:postmap /etc/postfix/header_checks
  • 四、执行:service postfix restart
  • 修改header_checks的内容后,只需要执行一下postmap /etc/postfix/header_checks,不用重启postfix即可生效

发送邮件提示Sender address rejected: not owned by user错误

客户端工具发送邮件提示Sender address rejected: not owned by user错误,maillog日志里面也提示这个错误。这个问题一般是客户端工具上mail和mail from邮箱地址不一样造成的。


方法一(但是这种方法不好,有风险):
修改main.cf

smtpd_sender_restrictions =
#       reject_sender_login_mismatch,
#       reject_authenticated_sender_login_mismatch,
#       reject_unauthenticated_sender_login_mismatch



方法二(这种方法复杂,有效):

smtpd_sender_restrictions =
        reject_sender_login_mismatch,
        reject_authenticated_sender_login_mismatch,
        reject_unauthenticated_sender_login_mismatch
smtpd_sender_login_maps = hash:/etc/postfix/sender_login_maps,
        mysql:/etc/postfix/mysql_virtual_sender_maps.cf,
        mysql:/etc/postfix/mysql_virtual_alias_maps.cf
        
然后创建/etc/postfix/sender_login_maps文件,把mail from和from不一致的加进去即可。
格式为:A地址 A用户

[root@localhost ~]#postmap sender_login_maps
[root@localhost ~]#postfix reload


 
方法三(建议方法):
把内网网段假如到信任IP地址。

mynetworks = 127.0.0.1, 10.10.8.0/24, 10.10.9.0/24
smtpd_sender_restrictions =
        permit_mynetworks,
        reject_sender_login_mismatch,
        reject_authenticated_sender_login_mismatch,
        reject_unauthenticated_sender_login_mismatch
smtpd_sender_login_maps =
#       hash:/etc/postfix/sender_login_maps,
        mysql:/etc/postfix/mysql_virtual_sender_maps.cf,
        mysql:/etc/postfix/mysql_virtual_alias_maps.cf

当启用了以下选项后,须通过方法二的sender_login_maps文件建立发件人和登录名称的映射

reject_sender_login_mismatch,
reject_authenticated_sender_login_mismatch,

postfix疯狂外发垃圾邮件之分析与解决

本周二上班有人反应公司邮箱无法外发邮件,后来登录到服务器查看原因,不看不知道,一看吓一跳,服务器日志疯狂滚动!一看全是被其它邮件服务器拒绝的信息!由于这台服务器以前不是我管,连配置文件都find了好久,最终从十多个main.cf中找到了真正的配置文件……

下面开始分析问题与解决问题!

分析

localhost# find / -name main.cf

…….

/usr/local/etc/postfix/main.cf

/usr/opt/software/postfix-2.8.9/conf/main.cf

/etc/postfix/main.cf

这么多配置文件也不知道具体是哪个,但根据常理,最有可能的便是/etc/postfix/main.cf了。

打开/etc/postfix/main.cf来看看。

localhost# cat /etc/postfix/main.cf | grep -v # | grep -v ^$

queue_directory = /var/spool/postfix

command_directory = /usr/sbin

daemon_directory = /usr/libexec/postfix

data_directory = /var/lib/postfix

mail_owner = postfix

myhostname = mail.example.com

mydomain = example.com

myorigin = $mydomain

unknown_local_recipient_reject_code = 550

debug_peer_level = 2

debugger_command =

         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin

         ddd $daemon_directory/$process_name $process_id & sleep 5

sendmail_path = /usr/sbin/sendmail

newaliases_path = /usr/bin/newaliases

mailq_path = /usr/bin/mailq

setgid_group = maildrop 

html_directory = no

manpage_directory = /usr/local/man

sample_directory = /etc/postfix

message_size_limit = 512000000

virtual_mailbox_limit = 1024000000

readme_directory = /usr/local/share/doc/postfix

virtual_mailbox_base = /usr/opt/home/domains

virtual_uid_maps = static:1000

virtual_gid_maps = static:1000

virtual_alias_maps = $alias_maps, mysql:/etc/postfix/mysql_virtual_alias_maps.cf

virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf

virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf

smtpd_sasl_auth_enable = yes

broken_sasl_auth_clients = yes

smtpd_sasl_local_domain = $myhostname

smtpd_sasl_security_options = noanonymous

smtpd_recipient_restrictions =

        permit_mynetworks, 

        permit_sasl_authenticated,

        reject_unauth_destination,

dspam_destination_recipient_limit = 1

mydestination =

smtpd_sender_restrictions = check_sender_access hash:/etc/postfix/sender_access

smtpd_client_restrictions = 

        reject_rbl_client cblless.anti-spam.org.cn,

        reject_rbl_client cdl.anti-spam.org.cn,

        reject_rbl_client opm.blitzed.org,

        reject_rbl_client bl.spamcop.net

localhost# vi /etc/postfix/sender_access

carr.chee@aol.com        REJECT

Henry.bny5@hotmail.com   REJECT

pmck.hsr69@yahoo.com     REJECT

……..

由以上配置文件可以看到,我们对发信者的检测仅限于sender_access。仅仅只是拒绝了sender_access中拒绝的用户。而没有其它任何限制。限制条件很宽松。

截取部分日志分析

localhost# tail /var/log/maillog

Oct 22 16:36:01 localhost postfix/error[18675]: 42263221CA: to=<altoact@verizon.net>, relay=none, delay=111421, delays=105070/6348/0/2.6, dsn=4.0.0, status=deferred (delivery temporarily suspended: host relay.verizon.net[206.46.232.11] refused to talk to me: 571 Email from 124.172.224.76 is currently blocked by Verizon Online’s anti-spam system. The email sender or Email Service Provider may visit http://www.verizon.net/whitelist and request removal of the block. 121022)

Oct 22 16:36:01 localhost postfix/qmgr[32357]: 184F627BF64: from=<lindawatts338@yahoo.com.hk>, size=2338, nrcpt=50 (queue active)

Oct 22 16:36:01 localhost postfix/smtp[18300]: 1C34F26235C: to=<andrew.rawson@honeywell.com>, relay=mail2.honeywell.com[199.61.24.28]:25, delay=110287, delays=110274/11/2.4/0, dsn=4.0.0, status=deferred (host mail2.honeywell.com[199.61.24.28] refused to talk to me: 554 mail2.honeywell.com)

Oct 22 16:36:01 localhost postfix/smtp[18288]: connect to news-daily.com.inbound15.mxlogicmx.net[208.65.144.12]:25: Connection refused

发现全是一些被defferred的信息。其它邮件服务器已经拒绝我们投递。

那么再用mailq命令来查看一下队列文件。

结果发现mailq刷屏刷得没完没了了。太多缓存的邮件!

localhost#  mailq 

-Queue ID- –Size– —-Arrival Time—- -Sender/Recipient——-

5D1477C0D*     2333 Mon Oct 22 18:36:47  jrobert299@yahoo.com.hk

(delivery temporarily suspended: host mx.west.cox.net[68.6.19.3] refused to talk to me: 554 fed1rmimpi210 cox 124.172.224.76 blocked.  Error Code: IPBL0100 – Refer to Error Codes section at http://postmaster.cox.net/confluence/display/postmaster/Error+Codes for more information.)

                                         n_jhenderson@cox.net

截取其中一个例子来分析,可分为五部分来看。

1,5D1477C0D* 是指缓存邮件的ID

2,2333 是指邮件的大小。

3,jrobert299@yahoo.com.hk 是指发件人。

4,(delivery temporarily suspended: host mx.west.cox.net[68.6.19.3] refused to talk to me: 554 fed1rmimpi210 cox 124.172.224.76 blocked.  Error Code: IPBL0100 – Refer to Error Codes section at http://postmaster.cox.net/confluence/display/postmaster/Error+Codes for more information.)

通过第4段信息我们可以得到如下信息:

delivery temporarily suspended告诉我们邮件投递被延迟。

host mx.west.cox.net[68.6.19.3] refused to talk to me 告诉我们投递给mx.west.cox.net[68.6.19.3] 的邮件服务器拒收我们的信件。

124.172.224.76 我们邮件服务器的Ip地址。

Error Code: IPBL0100 – Refer to Error Codes section at http://postmaster.cox.net/confluence/display/postmaster/Error+Codes for more information.)

通过http://postmaster.cox.net/confluence/display/postmaster/Error+Codes 上查看更多拒收的原因。

5,n_jhenderson@cox.net 收件人地址!

由上面的日志我们可以知道

1,我们的邮件服务器的发件人地址被伪造了!

2,我们邮件服务器已经被很多其它邮件服务器列入了黑名单

3,收件人并非我们认识的人

结论,这个邮件服务器很可能被入侵,且被当作垃圾邮件的中继者。

那么,谁会是这个入侵者呢?倒底是什么漏洞导致的?

下面来让找到事件的源头。

首先,根据上面的分析,我们知道发件人和收件人都不是邮件服务器所在域的成员。那么我们就得考虑我们的邮件服务器是不是一个开放的中继(open relay)。

验证:

[root@mail ~]# telnet mail.example.com 25

Trying 124.172.224.76…

Connected to mail.example.com (124.172.224.76).

Escape character is ‘^]’.

220 mail.example.com ESMTP Postfix

helo aa@bb.com

250 mail.example.com

mail from:aa@bb.com

250 2.1.0 Ok

rcpt to:445335413@qq.com

554 5.7.1 <445335413@qq.com>: Relay access denied

可以看到,我们不经过认证发送邮件给qq邮箱并没有成功。所以,这并不是一个open relay的服务器!既然如此,那别人想用我的邮件服务器外发邮件,那么就必需要通过认证才可以。那么让我们用认证的方式登录并偿试伪造发信人地址外发邮件!

先将用户名和密码经过base64编码。

[root@mail ~]# perl -MMIME::Base64 -e “print encode_base64(‘123456’);”         

MTIzNDU2

[root@mail ~]# perl -MMIME::Base64 -e “print encode_base64(‘test1@example.com’);”

dGVzdDFAZXhhbXBsZS5jb20=

验证:

[root@mail ~]# telnet mail.example.com 25

Trying 124.172.224.76…

Connected to mail.example.com (124.172.224.76).

Escape character is ‘^]’.

220 mail.example.com ESMTP Postfix

auth login

334 VXNlcm5hbWU6

dGVzdDFAZXhhbXBsZS5jb20=

334 UGFzc3dvcmQ6

MTIzNDU2

235 2.7.0 Authentication successful

mail from:test@yahoo.com

250 2.1.0 Ok

rcpt to:445335413@qq.com

250 2.1.5 Ok

data

354 End data with <CR><LF>.<CR><LF>

hello inveracious test!

.

250 2.0.0 Ok: queued as 99F4C23F008

quit

221 2.0.0 Bye

Connection closed by foreign host.

上面我用认证用户test1@example.com登录后,伪造成test@yahoo.com来发信。结果显示成功!我们再回到服务器端看看有什么日志产生!

localhost# tail /var/log/maillog

Oct 26 02:54:27 localhost postfix/qmgr[40723]: 99F4C23F008: from=<test@yahoo.com>, size=197, nrcpt=1 (queue active)

Oct 26 02:54:27 localhost postfix/smtp[41094]: 99F4C23F008: to=<445335413@qq.com>, relay=mx3.qq.com[119.147.192.199]:25, delay=110, delays=109/0/0.04/0.1, dsn=5.0.0, status=bounced (host mx3.qq.com[119.147.192.199] said: 550 Mail content denied. http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=20022&&no=1000726 (in reply to end of DATA command))

Oct 26 02:54:27 localhost postfix/cleanup[41089]: 69EA423F027: message-id=<20121025185427.69EA423F027@mail.example.com>

Oct 26 02:54:27 localhost postfix/bounce[41095]: 99F4C23F008: sender non-delivery notification: 69EA423F027

Oct 26 02:54:27 localhost postfix/qmgr[40723]: 69EA423F027: from=<>, size=2203, nrcpt=1 (queue active)

Oct 26 02:54:27 localhost postfix/qmgr[40723]: 99F4C23F008: removed

日志显示邮件ID为99F4C23F008的邮件,发件人是test@yahoo.com。接收服务器是mx3.qq.com的25端口。状态是被退回。通过http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=20022&&no=1000726 可以查看到拒绝原因。

经过认证登录后的用户,可以伪造发件人随意外发邮件!

由以上信息我们可以推断,很可能是由于别人盗用了我们的账号,然后利用我们的账号伪造其它发信人,疯狂外发邮件!那么,我们现在需要从三方面着手。

1,找出真正的发件人是哪个合法用户并马上修改密码。

2,阻止邮件继续疯狂外发。

3,禁止认证用户伪造发件人外发邮件。

解决

 1,找出可能被盗号的用户!

通过maillog虽然看不到被拒绝邮件到底是谁发出的,但可以看到被拒绝投递的邮件ID.就比如下面两封邮件的ID分别为45C4E130CB 4728312BBA。这时候,我们可以查找到一些收信地址比较可疑的邮件来分析。

localhost# tail  /var/log/maillog

Oct 22 16:41:38 localhost postfix/error[18801]: 45C4E130CB: to=<blantonlarry@bellsouth.net>, relay=none, delay=101364, delays=95002/6357/0/5.3, dsn=4.0.0, status=deferred (delivery temporarily suspended: host gateway-f1.isp.att.net[204.127.217.16] refused to talk to me: 550-124.172.224.76 blocked by ldap:ou=rblmx,dc=att,dc=net 550 Error – Blocked for abuse. See http://att.net/blocks)

Oct 22 16:41:38 localhost postfix/error[18103]: 4728312BBA: to=<bhaitov@yahoo.com>, relay=none, delay=102971, delays=96576/6386/0/9.8, dsn=4.7.1, status=deferred (delivery temporarily suspended: host mta5.am0.yahoodns.net[66.94.237.139] refused to talk to me: 421 4.7.1 [TS03] All messages from 124.172.224.76 will be permanently deferred; Retrying will NOT succeed. See http://postmaster.yahoo.com/421-ts03.html)

…..

我们可以通过postcat -q命令来查看指定ID邮件的详细内容。比如我觉得ID为2FDF423FA50的邮件很可疑,那么让我们详细看看邮件:

localhost# postcat -q 2FDF423FA50

*** ENVELOPE RECORDS deferred/2/2FDF423FA50 ***

message_size:            2428            4714              50               0            2428

message_arrival_time: Wed Oct 24 06:18:07 2012

create_time: Wed Oct 24 06:18:08 2012

named_attribute: log_ident=2FDF423FA50

named_attribute: rewrite_context=remote

named_attribute: sasl_method=LOGIN     <—–sasl认证通过

named_attribute: sasl_username=info@example.com   <——-认证用户名

sender: lindawatts101@yahoo.com.hk <—–伪造的发信者

named_attribute: log_client_name=unknown

named_attribute: log_client_address=68.167.29.196  <—-登录客户端的ip

named_attribute: log_client_port=1464

named_attribute: log_message_origin=unknown[68.167.29.196]

named_attribute: log_helo_name=User

named_attribute: log_protocol_name=ESMTP

named_attribute: client_name=unknown

named_attribute: reverse_client_name=h-68-167-29-196.mclnva23.static.covad.net

named_attribute: client_address=68.167.29.196

named_attribute: client_port=1464

named_attribute: helo_name=User

named_attribute: protocol_name=ESMTP

named_attribute: client_address_type=2

named_attribute: dsn_orig_rcpt=rfc822;ajagoodin@yahoo.com

original_recipient: ajagoodin@yahoo.com

recipient: ajagoodin@yahoo.com <——收件人1

named_attribute: dsn_orig_rcpt=rfc822;ajagostinelli@cox.net

original_recipient: ajagostinelli@cox.net <——收件人2

recipient: ajagostinelli@cox.net

named_attribute: dsn_orig_rcpt=rfc822;ajagra2001@yahoo.com

original_recipient: ajagra2001@yahoo.com <——收件人3

recipient: ajagra2001@yahoo.com

………

通过上面的这封邮件,我们可以知道,这封邮件是由info@example.com.这个用户来认证登录的。登录者IP 68.167.29.196。该用户将发信人伪造成:lindawatts101@yahoo.com.hk。并且同时发送很多封邮件出去! 如果真存在lindawatts101@yahoo.com.hk这个用户的话,那这个用户信箱估计也被塞满了无数退信邮件.

很显然,上面这封邮件并不是一封正常邮件!通过这封邮件很快就可以确定这个用户的密码很可能是被别人破解了!或者邮件系统有其它漏洞,入侵者亲自建立了该用户用来群发邮件。

搜索/var/spool/postfix/defrred目录下面的ip 68.167.29.196,发现有5600邮件被阻塞着。而且还在不断增加。且用户均为info@example.com。可以确定,IP 68.167.29.196为入侵者。

       localhost#  cd /var/spool/postfix/defrred

localhost# find . -exec grep “68.167.29.196” {} \; | wc -l

5625

登录数据库查看info@example.com这个用户的创建情况。这个在webman管理后台上面是看不到的。

localhost# /usr/opt/local/mysql-5.1.47/bin/mysql -uextmail -pextmail

mysql> select * from mailbox where  username=’info@example.com’\G;

*************************** 1. row ***************************

        username: info@example.com

             uid: info

        password: $1$RpyMEokE$CK31uufL9uNk6PjqSorXa1

        clearpwd: 

            name: 

        mailhost: 

         maildir: example.com/info/Maildir/

         homedir: example.com/info

           quota: 524288000S

    netdiskquota: 524288000S

          domain: example.com

       uidnumber: 1000

       gidnumber: 1000

      createdate: 2012-04-25 15:35:54    <—–创建日期

      expiredate: 0000-00-00

          active: 1

disablepwdchange: 0

    disablesmtpd: 0

     disablesmtp: 0

  disablewebmail: 0

  disablenetdisk: 0

     disableimap: 1

     disablepop3: 0

        question: 

          answer: 

1 row in set (0.00 sec)

mysql> select * from manager;

+——————+————————————+——-+——+————+————-+———–+——————+———————+————+——–+

| username         | password                           | type  | uid  | name       | question    | answer    | disablepwdchange | createdate          | expiredate | active |

+——————+————————————+——-+——+————+————-+———–+——————+———————+————+——–+

| root@extmail.org | $1$ZwYBBBz1$mh.Uwro5vqXMwYum0eprq/ | admin | root | Super User | my question | my answer |                0 | 2007-02-14 15:10:04 | 2010-11-08 |      1 |

+——————+————————————+——-+——+————+————-+———–+——————+———————+————+——–+

管理员账号只有一个,info@example.com也不是新建的。那应该是Info@example.com的用户密码被破解了。

来看看info@example.com这个用户的登录日志。发现全是由68.167.29.196的主机登录的。

localhost# cat /var/log/maillog | grep info@example.com

Oct 24 00:32:10 localhost postfix/smtpd[4202]: 8F005249014: client=unknown[68.167.29.196], sasl_method=LOGIN, sasl_username=info@example.com

Oct 24 00:32:16 localhost postfix/smtpd[5029]: 11044249024: client=unknown[68.167.29.196], sasl_method=LOGIN, sasl_username=info@example.com

Oct 24 00:32:17 localhost postfix/smtpd[4626]: 930FB249028: client=unknown[68.167.29.196], sasl_method=LOGIN, sasl_username=info@example.com

Oct 24 00:32:17 localhost postfix/smtpd[4765]: A890624902B: client=unknown[68.167.29.196], sasl_method=LOGIN, sasl_username=info@example.com

登录extman,将info@example.com这个用户的密码改掉。

我们登录info@example.com来看看用户情况。结果发现有一万多封退信信息,由于这个账号没人用,所以一直未发现这个问题。

2,清除所有缓存垃圾邮件,阻止邮件服务器继续偿试外发!

清除defer和deferred目录下的缓存邮件

我们来瞧瞧邮件缓存目录

localhost# du -sh /var/spool/postfix/*

2.1G    /var/spool/postfix/defer

2.7G    /var/spool/postfix/deferred

可以看到,被延迟发送的邮件占用了5个g的空间!

清除邮件中的所有队列

localhost# postsuper -d ALL 

postsuper: Deleted: 292551 messages

共清除了将近30万封缓存的邮件。

如果缓存邮件里面有重要邮件,不能删除所有邮件,那么也可以写一个脚本,只清除所有属于68.167.29.196的缓存邮件。

localhost# vi deldefer.sh

#删除defferred中的缓存

cd /var/spool/postfix/deferred/

find . -exec grep 68.167.29.196 {} \; | awk ‘{print $3}’ | cut -d/ -f3 >/tmp/del.txt

for i  in `cat /tmp/del.txt`

        do

        postsuper -d “$i”

        done

rm -rf /tmp/del.txt

#删除deffer中的缓存

cd /var/spool/postfix/defer

for i in `find .|cut -d/ -f3`

        do

        postcat -q $i |grep 68.167.29.196

                if [ $? -eq 0 ];

                then

                echo $i >> /tmp/defer.txt

                postsuper -d $i

                fi

        done

rm -rf /tmp/defer.txt

脚本说明:

在删除deffer和defferred下面的缓存邮件的脚本是不同的。由于defer下的缓存邮件用cat直接查看是看不到发件人及登录ip等详细信息的,需要用postcat来查看才能显示出详细的信息。所以deffer目录里的清除脚本写法和上面defferred的有些不同。注意:在删除了deferred下面的缓存后,如果不删除defer的缓存,defer下的邮件仍然会被不停的投递出去,直到最后变为deferred之后才会放弃。所以,如果只删除deferred下面的邮件而不删除defer下面的邮件的话,过不了多久,deferred下面又会出现大量邮件,而这个邮件是由defer目录下的缓存引起的。

——————————————————-

再次查看缓存目录,容量终于恢复正常值。

localhost# du -sh /var/spool/postfix/*

162K    /var/spool/postfix/defer

 46K    /var/spool/postfix/deferred

删除info@example.com用户邮箱的退信邮件

下面该删除info@example.com用户的所有退信邮件了.通过查看邮件发现所有的垃圾邮件均是今天一天生成的。到服务器端查找并删除今天的所有邮件。

进入到info用户的邮件目录

localhost# cd /usr/opt/home/domains/example.com/info/Maildir/

可以看到有12779封邮件。

localhost# ls -l cur/ |wc -l

   12779

查找今天生成的邮件,共11589封

localhost# find cur/ -ctime -1 | wc -l     

   11589

删除所有今天的邮件

localhost# find cur/ -ctime -1 -exec rm -f {} \;

删除完毕!

注意find cur/ -ctime -1 -exec rm -f {} \; 中是rm -f !不要写成了rm -rf。否则一执行就把cur目录给删掉了。那么用户以前的邮件也全带着一起删除了!

至此,邮件服务器终于恢复了正常运行。再次用tail -f 来查看日志,不会再出现那恐怖的疯狂刷屏日志了!疯狂的服务器终于恢复了原来的悠闲状态!

3,禁止认证用户假冒发信人外发

修改main.cf配置文件,增加发信人限制功能!

localhost# vi main.cf

mynetworks = 127.0.0.0/8

smtpd_sender_restrictions =

        permit_mynetworks,

        reject_sender_login_mismatch,

        reject_non_fqdn_sender, 

        reject_authenticated_sender_login_mismatch,

        reject_unauthenticated_sender_login_mismatch,

        reject_non_fqdn_recipient,

        reject_invalid_hostname,

        reject_unknown_sender_domain,

        check_sender_access hash:/etc/postfix/sender_access

smtpd_sender_login_maps =

        mysql:/etc/postfix/mysql_virtual_sender_maps.cf,

        mysql:/etc/postfix/mysql_virtual_alias_maps.cf

localhost# postfix reload

在客户端测试效果:

客户端伪造发信人测试,test1@example.com伪造成发件人test@yahoo.com未成功!

[root@mail ~]# perl -MMIME::Base64 -e “print encode_base64(‘123456’);”         

MTIzNDU2

[root@mail ~]# perl -MMIME::Base64 -e “print encode_base64(‘test1@example.com’);”

dGVzdDFAZXhhbXBsZS5jb20=

[root@mail ~]# telnet mail.example.com 25

Trying 124.172.224.76…

Connected to mail.example.com (124.172.224.76).

Escape character is ‘^]’.

auth login

220 mail.example.com ESMTP Postfix

334 VXNlcm5hbWU6

dGVzdDFAZXhhbXBsZS5jb20=

334 UGFzc3dvcmQ6

MTIzNDU2

235 2.7.0 Authentication successful

mail from:test@yahoo.com

250 2.1.0 Ok

rcpt to:445335413@qq.com

553 5.7.1 <test@yahoo.com>: Sender address rejected: not owned by user test1@example.com

客户端用真实的地址发信测试成功

[root@mail ~]# telnet mail.example.com 25

Trying 124.172.224.76…

auConnected to mail.example.com (124.172.224.76).

Escape character is ‘^]’.                                                     220 mail.example.com ESMTP Postfix

auth login

334 VXNlcm5hbWU6

dGVzdDFAZXhhbXBsZS5jb20=

334 UGFzc3dvcmQ6

MTIzNDU2

235 2.7.0 Authentication successful

mail from:test1@example.com

250 2.1.0 Ok

rcpt to:445335413@qq.com

250 2.1.5 Ok

Ok,测试成功!

在CentOS 8系统上配置和管理防火墙(Firewall)的方法

在本文中,我们将介绍如何在CentOS 8操作系统上配置和管理防火墙(Firewall),我们还将说明基本的FirewallD概念。防火墙是一种用于监视和过滤传入和传出网络流量的方法,它通过定义一组安全规则来工作,这些安全规则确定是允许还是阻止特定流量,正确配置的防火墙是整个系统安全的最重要方面之一,CentOS 8搭载了一个名为firewalld的防火墙守护程序,它是具有D-Bus界面的完整解决方案,可让你动态管理系统的防火墙,参考在Ubuntu 18.04/16.04系统上安装和使用Firewalld的方法

基本防火墙概念

Firewalld使用区域和服务的概念,根据你要配置的区域和服务,你可以控制允许或禁止与系统进行的流量。

可以使用firewall-cmd命令行实用程序配置和管理Firewalld。

在CentOS 8操作系统中,iptables被nftables替代,作为firewalld守护程序的默认防火墙后端。

1、防火墙区域

区域是预定义的规则集,用于指定计算机连接到的网络的信任级别,你可以将网络接口和源分配给区域。

以下是FirewallD提供的区域,根据区域的信任级别从不信任到信任:

drop:删除所有传入连接,而无任何通知,仅允许传出连接。

block:所有传入连接均被拒绝,其中icmp-host禁止消息用于IPv4,icmp6-adm禁止用于IPv6n,仅允许传出连接。

public:用于不受信任的公共区域,你不信任网络上的其他计算机,但是可以允许选择的传入连接。

external:用于在系统充当网关或路由器时启用了NAT伪装的外部网络,仅允许选择的传入连接。

internal:当系统充当网关或路由器时,用于内部网络,网络上的其他系统通常是受信任的,仅允许选择的传入连接。

dmz:用于非军事区中访问网络其余部分的计算机,仅允许选择的传入连接。

work:用于工作机,网络上的其他计算机通常是受信任的,仅允许选择的传入连接。

home:用于家用机器,网络上的其他计算机通常是受信任的,仅允许选择的传入连接。

trusted:接受所有网络连接,信任网络中的所有计算机。

2、防火墙服务

防火墙服务是预定义的规则,适用于区域,并定义必要的设置以允许特定服务的传入流量,该服务使你可以轻松地在一个步骤中执行多个任务。

例如,服务可以包含有关打开端口,转发流量等的定义。

3、防火墙运行时和永久设置

Firewalld使用两个单独的配置集,即运行时配置和永久配置。

运行时配置是实际的运行配置,并且不会在重新启动后持续存在,当firewalld守护程序启动时,它将加载永久配置,该配置将成为运行时配置。

默认情况下,使用firewall-cmd实用程序对Firewalld配置进行更改时,更改将应用​​于运行时配置,要使更改永久生效,请在命令后附加–permanent选项。

要在两个配置集中应用更改,可以使用以下两种方法之一:

1]、更改运行时配置并将其永久化:

$ sudo firewall-cmd <options>

$ sudo firewall-cmd –runtime-to-permanent

2]、更改永久配置并重新加载firewald守护程序:

$ sudo firewall-cmd –permanent <options>

$ sudo firewall-cmd –reload

启用FirewallD

在CentOS 8操作系统上,默认情况下已安装并启用firewalld,如果由于某种原因未在系统上安装它,则可以通过键入以下命令来安装并启动守护程序:

$ sudo dnf install firewalld

$ sudo systemctl enable firewalld –now

可以使用以下方法检查防火墙服务的状态:

$ sudo firewall-cmd –state

如果启用了防火墙,该命令应显示正在运行,否则,你将看到未运行。

参考:CentOS 7下使用FirewallD构建动态防火墙

防火墙区域

如果尚未更改,则默认区域设置为public,所有网络接口都分配给该区域。

默认区域是用于所有未明确分配给另一个区域的区域。

可以通过键入以下命令查看默认区域:

$ sudo firewall-cmd –get-default-zone

返回:

public

要获取所有可用区域的列表,请输入:

$ sudo firewall-cmd –get-zones

返回:

block dmz drop external home internal public trusted work(阻止dmz放下外部家庭内部公共信任的工作)

要查看活动区域和分配给它们的网络接口,请执行以下操作:

$ sudo firewall-cmd –get-active-zones

下面的输出显示接口eth0和eth1已分配给公共区域:

public

interfaces: eth0 eth1

可以使用以下命令打印区域配置设置:

$ sudo firewall-cmd –zone=public –list-all

返回:

public (active)

target: default

icmp-block-inversion: no

interfaces: eth0 eth1

sources:

services: ssh dhcpv6-client

ports:

protocols:

masquerade: no

forward-ports:

source-ports:

icmp-blocks:

rich rules:

从上面的输出中,我们可以看到公共区域处于活动状态,并使用默认目标REJECT,输出还显示该区域由eth0和eth1接口使用,并允许DHCP客户端和SSH通信。

如果要检查所有可用区域的配置,请输入:

$ sudo firewall-cmd –list-all-zones

该命令将显示包含所有可用区域设置的巨大列表。

1、更改区域目标

目标为未指定的传入流量定义区域的默认行为,可以将其设置为以下选项之一:默认,接受,拒绝和删除。

要设置区域的目标,请使用–zone选项指定区域,并使用–set-target选项指定目标。

例如,要将公共区域的目标更改为DROP,可以运行:

$ sudo firewall-cmd –zone=public –set-target=DROP

2、将接口分配给其他区域

你可以为不同区域创建特定的规则集,并为其分配不同的接口,当你在计算机上有多个接口时,这特别有用。

要将接口分配给其他区域,请使用–zone选项指定区域,并使用–change-interface选项指定接口。

例如,以下命令将eth1接口分配给工作区:

$ sudo firewall-cmd –zone=work –change-interface=eth1

通过键入以下命令来验证更改:

$ sudo firewall-cmd –get-active-zones

返回:

work

interfaces: eth1

public

interfaces: eth0

3、更改默认区域

要更改默认区域,请使用–set-default-zone选项,后跟要设为默认区域的名称。

例如,要将默认区域更改为home,可以运行以下命令:

$ sudo firewall-cmd –set-default-zone=home

使用以下命令验证更改:

$ sudo firewall-cmd –get-default-zone

返回:

home

防火墙服务

使用防火墙,可以基于称为服务的预定义规则允许特定端口或源的流量。

要获取所有默认可用服务的列表,请输入:

$ sudo firewall-cmd –get-services

通过打开/usr/lib/firewalld/services目录中的关联.xml文件,可以找到有关每个服务的更多信息,例如,HTTP服务的定义如下(/usr/lib/firewalld/services/http.xml文件):

<?xml version=”1.0″ encoding=”utf-8″?>

<service>

<short>WWW (HTTP)</short>

<description>HTTP is the protocol used to serve Web pages. If you plan to make your Web server publicly available, enable this option. This option is not required for viewing pages locally or developing Web pages.</description>

<port protocol=”tcp” port=”80″/>

</service>

要仅对当前会话(运行时配置)允许公共区域中的接口允许传入的HTTP通信(80端口),请输入:

$ sudo firewall-cmd –zone=public –add-service=http

如果要修改默认区域,则可以省略–zone选项。

要验证是否已成功添加服务,请使用–list-services选项:

$ sudo firewall-cmd –zone=public –list-services

返回:

ssh dhcpv6-client http

如果你想在重启后将80端口保持打开状态,则需要再次输入相同的命令,但这一次带有–permanent选项:

$ sudo firewall-cmd –permanent –zone=public –add-service=http

使用–list-services和–permanent选项来验证你的更改:

$ sudo firewall-cmd –permanent –zone=public –list-services

返回:

ssh dhcpv6-client http

删除服务的语法与添加服务时的语法相同,只需使用–remove-service而不是–add-service选项即可:

$ sudo firewall-cmd –zone=public –remove-service=http –permanent

上面的命令从公共区域永久配置中删除http服务。

下面讲解:创建一个新的FirewallD服务。

正如我们已经提到的,默认服务存储在/usr/lib/firewalld/services目录中,创建新服务的最简单方法是将现有服务文件复制到/etc/firewalld/services目录(该目录是用户创建的服务的位置)并修改文件设置。

例如,要为Plex Media Server创建服务定义,可以使用SSH服务文件:

$ sudo cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/plexmediaserver.xml

打开新创建的plexmediaserver.xml文件,并在<short>和<description>标记内更改服务的简称和描述,你需要更改的最重要的标签是端口标签,它定义了你要打开的端口号和协议。

在下面的示例中,我们打开端口1900 UDP和32400 TCP(/etc/firewalld/services/plexmediaserver.xml文件):

/etc/firewalld/services/plexmediaserver.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<service version=”1.0″>

<short>plexmediaserver</short>

<description>Plex is a streaming media server that brings all your video, music and photo collections together and stream them to your devices at anytime and from anywhere.</description>

<port protocol=”udp” port=”1900″/>

<port protocol=”tcp” port=”32400″/>

</service>

保存文件并重新加载FirewallD服务:

$ sudo firewall-cmd –reload

现在,可以像其他任何服务一样在你的区域中使用plexmediaserver服务。

打开端口和源IP

Firewalld还允许你快速启用来自受信任IP地址或特定端口上的所有流量,而无需创建服务定义。

1、打开源IP

要允许来自特定IP地址(或范围)的所有传入流量,请使用–zone选项指定区域,并使用–add-source选项指定源IP。

例如,要在公共区域中允许来自192.168.1.10的所有传入流量,请运行:

$ sudo firewall-cmd –zone=public –add-source=192.168.1.10

使新规则持久化:

$ sudo firewall-cmd –runtime-to-permanent

使用以下命令验证更改:

$ sudo firewall-cmd –zone=public –list-sources

返回:

192.168.1.10

删除源IP的语法与添加源IP的语法相同,只需使用–remove-source,而不是–add-source选项:

$ sudo firewall-cmd –zone=public –remove-source=192.168.1.10

2、打开源端口

要允许给定端口上的所有传入流量,请使用–zone选项指定区域,并使用–add-port选项指定端口和协议。

例如,要为当前会话打开公共区域中的8080端口,请运行:

$ sudo firewall-cmd –zone=public –add-port=8080/tcp

该协议可以是tcp、udp、sctp或dccp。

验证更改:

$ sudo firewall-cmd –zone=public –list-ports

返回:

8080

要在重新启动后使端口保持打开状态,请使用–permanent选项运行同一命令,或执行以下命令,将规则添加到永久设置中:

$ sudo firewall-cmd –runtime-to-permanent

删除端口的语法与添加端口时的语法相同,只需使用–remove-port,而不是–add-port选项即可:

$ sudo firewall-cmd –zone=public –remove-port=8080/tcp

转发端口

要将流量从一个端口转发到另一个端口,请首先使用–add-masquerade选项启用对所需区域的伪装,例如,要对外部区域启用伪装,请键入:

$ sudo firewall-cmd –zone=external –add-masquerade

1、将流量从一个端口转发到IP地址上的另一个端口

在以下示例中,我们将流量从80端口转发到同一服务器上的8080端口:

$ sudo firewall-cmd –zone=external –add-forward-port=port=80:proto=tcp:toport=8080

2、将流量转发到另一个IP地址

在以下示例中,我们将流量从80端口转发到IP 10.10.10.2的服务器上的80端口:

$ sudo firewall-cmd –zone=external –add-forward-port=port=80:proto=tcp:toaddr=10.10.10.2

3、将流量转发到其他端口上的另一台服务器

在下面的示例中,我们将流量从80端口转发到IP 10.10.10.2的服务器上的8080端口:

$ sudo firewall-cmd –zone=external –add-forward-port=port=80:proto=tcp:toport=8080:toaddr=10.10.10.2

要使转发规则持久化,请使用:

$ sudo firewall-cmd –runtime-to-permanent

结论

通过上面的操作步骤,你已经了解了如何在CentOS 8系统上配置和管理防火墙服务。

确保允许系统正常运行所必需的所有传入连接,同时限制所有不必要的连接。