欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

防止Xen VPS用户自己修改IP地址的方法

程序员文章站 2023-09-11 08:36:16
这篇文章主要介绍了防止Xen VPS用户自己修改IP地址的方法,本文给出了两种方法,分别使用Xen 配置和iptables配置实现,需要的朋友可以参考下... 15-02-09...

作为 xen vps 服务商,我们分配独立的 ip 地址给 vps,我们不希望 vps 用户自己能随便修改 ip 地址,因为这样有可能和其他用户的 ip 地址造成冲突,而且造成管理上的不便,所以需要绑定 ip 给某个 vps.

解决这个问题的办法有很多,从路由器、防火墙、操作系统、xen 等层面都可以做限制。这里介绍的两个简单方法都是从 dom0 入手:一个是在 dom0 上利用 xen 配置;一个是在 dom0 上利用 iptables.

利用 xen 配置

xen 上有个 antispoof 配置选项就是来解决这个问题的,不过默认配置没有打开这个 antispoof 选项,需要修改:

复制代码
代码如下:

# vi /etc/xen/xend-config.sxp
...
(network-script 'network-bridge antispoof=yes')
...

修改 /etc/xen/scripts/vif-common.sh 里面的 frob_iptable() 函数部分,加上 iptables 一行:

复制代码
代码如下:

# vi /etc/xen/scripts/vif-common.sh
function frob_iptable()
{
...
iptables -t raw "$c" prerouting -m physdev --physdev-in "$vif" "$@" -j notrack
}

修改完 xen 配置后还需要修改 domu 的配置,给每个 domu 分配固定 ip 和 mac 地址,还有 vif 名字:

复制代码
代码如下:

# vi /etc/xen/vm01
...
vif = [ "vifname=vm01,mac=00:16:3e:7c:1f:6e,ip=172.16.39.105,bridge=xenbr0" ]
...

很多系统上 iptables 在默认情况下都不会理会网桥上的 forward 链,所以需要修改内核参数确保 bridge-nf-call-iptables=1,把这个修改可以放到 antispoofing() 函数里,这样每次 xen 配置网络的时候会自动配置内核参数:

复制代码
代码如下:

# vi /etc/xen/scripts/network-bridge
antispoofing () {
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
...
}

修改完毕后测试的话需要关闭 domu,重启 iptables 和 xend 服务,再启动 domu.

复制代码
代码如下:

# xm shutdown vm01
# /etc/init.d/iptables restart
# /etc/init.d/xend restart
# xm create vm01

上面的方法在 xen 3.x 上 测试有效,有人说在 xen 4.x 上行不通,我们下面将要介绍的方法绕开了 xen 配置,直接从 iptables 限制,在 xen 3.x 和 xen 4.x 上应该都可以用。

利用 iptables

首先在 dom0 上确定 iptables 已经开启,这里需要注意的是一定要在每个 domu 的配置文件中的 vif 部分加上 vifname, ip, mac,这样才能在 iptables 规则里面明确定义:

复制代码
代码如下:

# /etc/init.d/iptables restart</p> <p># vi /etc/xen/vm01
...
vif = [ "vifname=vm01,mac=00:16:3e:7c:1f:6e,ip=172.16.39.105,bridge=xenbr0" ]
...</p> <p># vi /etc/iptables-rules
*filter
:input accept [0:0]
:forward accept [0:0]
:output accept [0:0]
# the antispoofing rules for domus
-a forward -m state --state related,established -m physdev --physdev-out vm01 -j accept
-a forward -p udp -m physdev --physdev-in vm01 -m udp --sport 68 --dport 67 -j accept
-a forward -s 172.16.39.105/32 -m physdev --physdev-in vm01 -j accept
-a forward -d 172.16.39.105/32 -m physdev --physdev-out vm01 -j accept
# if the ip address is not allowed on that vif, log and drop it.
-a forward -m limit --limit 15/min -j log --log-prefix "dropped by firewall: " --log-level 7
-a forward -j drop
# the access rules for dom0
-a input -j accept
commit</p> <p># iptables-restore < /etc/iptables.rules

当然,别忘了:

复制代码
代码如下:

# echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables