ubuntu-16.04.3 --ctstate RELATED 无法关联ftp

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
liuhuan22
帖子: 9
注册时间: 2014-10-05 9:41
系统: ubuntu12.04

ubuntu-16.04.3 --ctstate RELATED 无法关联ftp

#1

帖子 liuhuan22 » 2018-08-24 20:04

系统是ubuntu-16.04.3-desktop-amd64.iso LIVE CD
网卡一个是本机自带的enp4s0f2,另外一个网卡是安卓usb共享的USB网卡,
iptables规则就一条
iptables -A FORWARD -p tcp -m conntrack --ctstate RELATED -j LOG --log-prefix "XXX-FW "


sudo bash
/etc/init.d/network-manager stop
iptables -L
modprobe ip_tables
modprobe nf_conntrack
modprobe nf_conntrack_ftp
modprobe nf_nat_ftp
lsmod |grep ftp

希望能捕获到ftp被动模式的第一个SYN包,但是只有在ubuntu12.04上才成功,如下(usb0网卡是ubuntu12.04的,在16.04里是enp0s29u1u2):
[ 374.554805] ip_tables: (C) 2000-2006 Netfilter Core Team
[ 531.346482] nf_conntrack version 0.5.0 (16384 buckets, 65536 max)
[ 1047.835600] nf_conntrack: automatic helper assignment is deprecated and it will be removed soon. Use the iptables CT target to attach helpers instead.
[ 1048.884031] XXX-FW IN=usb0 OUT=eth0 MAC=0a:26:10:57:46:ce:2e:ca:06:dd:9f:e9:08:00:45:00:00:3c:86:8a:40:00:3f:06:41:2e:c0:a8:2a:81:c0:a8:c8:31:eb:c2:09:19:b1:8a:04:d4:00:00:00:00:a0:02:ff:ff:a6:59:00:00:02:04:05:b4 SRC=192.168.42.129 DST=192.168.200.49 LEN=60 TOS=0x00 PREC=0x00 TTL=63 ID=34442 DF PROTO=TCP SPT=60354 DPT=2329 WINDOW=65535 RES=0x00 SYN URGP=0
root@ubuntu:/lib#


ubuntu16.04 无法捕获,没有LOG输出



ifconfig enp0s29u1u2 192.168.42.130 netmask 255.255.255.0 up;
ifconfig enp4s0f2 192.168.200.48 netmask 255.255.254.0 up;


sysctl -w net.ipv4.conf.enp4s0f2.forwarding=1
sysctl -w net.ipv4.conf.enp0s29u1u2.forwarding=1



在安卓上运行 busybox ftpget 192.168.200.49 2.txt 2.txt
使用tcpdump在ubuntu16.04上,可以捕获(确实是ftp被动模式)。
头像
astolia
论坛版主
帖子: 6421
注册时间: 2008-09-18 13:11

Re: ubuntu-16.04.3 --ctstate RELATED 无法关联ftp

#2

帖子 astolia » 2018-08-24 20:32

如果内核版本在4.7以上,需要手动把nf_conntrack_helper功能打开才有效,否则conntrack的自动选择helper功能是没启用的

代码: 全选

sudo sysctl net.netfilter.nf_conntrack_helper=1
原因就是你日志里说的
nf_conntrack: automatic helper assignment is deprecated and it will be removed soon. Use the iptables CT target to attach helpers instead.

现在推荐的做法是用CT目标,见 https://home.regit.org/netfilter-en/sec ... f-helpers/ 的Using the CT target to refine security一节
liuhuan22
帖子: 9
注册时间: 2014-10-05 9:41
系统: ubuntu12.04

Re: ubuntu-16.04.3 --ctstate RELATED 无法关联ftp

#3

帖子 liuhuan22 » 2018-08-24 21:54

net.netfilter.nf_conntrack_helper = 0
iptables -A PREROUTING -t raw -p tcp --dport 2121 -d 1.2.3.4 -j CT --helper ftp
modprobe nf_conntrack nf_conntrack_helper=0
iptables -A FORWARD -m conntrack --ctstate RELATED -m helper --helper ftp -d $MY_FTP_SERVER -p tcp -j ACCEPT

旧的内核,可以获得这种行为对于大多数连接跟踪辅助模块通过设置模块的端口号为0。例如
modprobe nf_conntrack_$PROTO ports=0

iptables -A PREROUTING -t raw -p tcp --dport 21 -d 2.3.4.5 -j CT --helper ftp-0
liuhuan22
帖子: 9
注册时间: 2014-10-05 9:41
系统: ubuntu12.04

Re: ubuntu-16.04.3 --ctstate RELATED 无法关联ftp

#4

帖子 liuhuan22 » 2018-08-25 11:10

如果没有开启sysctl net.netfilter.nf_conntrack_helper=1,且此时iptables里有ftp helper策略,然后通过ftp数据包时会提示:[nf_conntrack: automatic helper assignment is deprecated and it will be removed soon. Use the iptables CT target to attach helpers instead.]

在lsmod|grep nf_conntrack已经加载的情况下,该错误只出现一次,rmmod卸载nf_conntrack模块,再加载nf_conntrack,上述提示(nf_conntrack_helper=0时)会继续出现。

nf_conntrack_helper=0时,iptables里的ftp helper策略计数器不会更新,上述提示在nf_conntrack模块没有重加载时,不会继续弹出。


root@ubuntu:~# modinfo nf_conntrack
filename: /lib/modules/4.10.0-28-generic/kernel/net/netfilter/nf_conntrack.ko
license: GPL
srcversion: BB3133F916B5F0AAC7C2F67
depends:
intree: Y
vermagic: 4.10.0-28-generic SMP mod_unload
parm: tstamp:Enable connection tracking flow timestamping. (bool)
parm: acct:Enable connection tracking flow accounting. (bool)
parm: nf_conntrack_helper:Enable automatic conntrack helper assignment (default 0) (bool)
parm: expect_hashsize:uint
root@ubuntu:~#


root@ubuntu:~# modinfo nf_conntrack_ftp
filename: /lib/modules/4.10.0-28-generic/kernel/net/netfilter/nf_conntrack_ftp.ko
alias: nfct-helper-ftp
alias: ip_conntrack_ftp
description: ftp connection tracking helper
author: Rusty Russell <rusty@rustcorp.com.au>
license: GPL
srcversion: 9F30A120DE1883DF168163F
depends: nf_conntrack
intree: Y
vermagic: 4.10.0-28-generic SMP mod_unload
parm: ports:array of ushort
parm: loose:bool
root@ubuntu:~#

root@ubuntu:~# modinfo nf_nat_ftp
filename: /lib/modules/4.10.0-28-generic/kernel/net/netfilter/nf_nat_ftp.ko
alias: ip_nat_ftp
description: ftp NAT helper
author: Rusty Russell <rusty@rustcorp.com.au>
license: GPL
srcversion: 81FA1DDF6BFA8782D1E5548
depends: nf_conntrack_ftp,nf_conntrack,nf_nat
intree: Y
vermagic: 4.10.0-28-generic SMP mod_unload
root@ubuntu:~#
liuhuan22
帖子: 9
注册时间: 2014-10-05 9:41
系统: ubuntu12.04

Re: ubuntu-16.04.3 --ctstate RELATED 无法关联ftp

#5

帖子 liuhuan22 » 2018-08-25 11:20

-m helper --helper ftp和-j CT --helper ftp的在计数器上看到的区别

iptables -traw -I PREROUTING 1 -p tcp --dport 21 -j CT --helper ftp
iptables -traw -I PREROUTING 2 -p tcp --sport 21 -j CT --helper ftp
iptables -traw -I PREROUTING 3 -p tcp -j CT --helper ftp


iptables -tmangle -A POSTROUTING -p tcp -m helper --helper ftp -j ACCEPT
iptables -tmangle -A POSTROUTING -p tcp --dport 21 -j ACCEPT
iptables -tmangle -A POSTROUTING -p tcp --sport 21 -j ACCEPT

-j CT --helper ftp只能用在raw 表里
-j CT --helper ftp会统计原始包(ftp里的tcp 21端口)和粘滞包(主动派生的包,相关包)
m helper --helper ftp 只会统计所有粘滞包,没有包含原始包(ftp里的tcp 21端口)

说的不对,欢迎纠正。。。。。。。。
liuhuan22
帖子: 9
注册时间: 2014-10-05 9:41
系统: ubuntu12.04

Re: ubuntu-16.04.3 --ctstate RELATED 无法关联ftp

#6

帖子 liuhuan22 » 2018-08-25 11:25

在ubuntu16.04 liveCD里iptables -L命令可以自动加载ip_tables模块,不需要再输入modprobe ip_tables
iptables -t nat -A POSTROUTING -o enp4s0f2 -s 192.168.42.0/24 -d 192.168.200.49 -j MASQUERADE命令可以自动加载nf_conntrack等等好多模块,不需要再输入modprobe nf_conntrack
,但是nf_nat_ftp模块必须由modprobe nf_nat_ftp命令来加载,目前没发现自动的办法。。。


有人知道吗?
回复