Từ Câu hỏi của bạn tôi giả sử bạn có một số máy chủ tầm thường với bộ chung của các ứng dụng như trình duyệt web, mail của khách hàng, có thể telnet và | hoặc ssh-client, có thể là ftp-client quá, có thể một số IM vv Và có tất cả các ứng dụng này làm việc, Bạn muốn bổ sung cho phép một máy chủ FTP trên máy chủ này làm việc cả ở chế độ chủ động và thụ động cho các máy khách sẽ kết nối. Dưới đây là 3 quy tắc áp dụng trong trường hợp này. Chặn các quy tắc phổ biến là tập hợp các quy tắc tối giản áp dụng cho hầu hết các máy khách. Tiếp theo là khối quy tắc cho ftp-client, nếu bạn có trên máy chủ lưu trữ của bạn. Các quy tắc cho ftp-client hơi khác so với quy tắc cho các máy khách khác: luôn có hai kết nối để cho phép truyền dữ liệu: ftp-control (cổng 21) và ftp-data (cổng 20 trong chế độ Active hoặc cổng ngẫu nhiên trong chế độ Passive). Bạn có thể sẽ không bao giờ cần các quy tắc của client cho chế độ Active vì chế độ Passive là lựa chọn duy nhất cho các mạng NAT.
Quy tắc cho máy chủ FTP nằm trong khối cuối cùng.
Vui lòng kiểm tra Bạn có ip_conntrack_ftp (có thể được đặt tên nf_conntrack_ftp) trong kernel:
> lsmod | grep conn
Nếu Bạn không có mô-đun hạt nhân này, các quy tắc của LIÊN QUAN 'sẽ không làm việc và, có lẽ hầu hết, riêng biệt Kết nối ftp-data sẽ không bắt đầu khi kết nối ftp-control chính sẽ treo ở đâu đó sau lệnh 'PORT'. Bạn vẫn có thể thực thi kết nối dữ liệu ftp trong trường hợp này, nhưng với chi phí bảo mật xuống cấp được cung cấp bởi các quy tắc được tinh chỉnh. Các tinh chỉnh nằm trong các nhận xét trước các quy tắc.
Pro
#!/bin/bash
IPT=/sbin/iptables
$IPT -F
$IPT -t nat -F
$IPT -t mangle -F
$IPT -X
$IPT -t nat -X
$IPT -t mangle -X
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT DROP
# Block of common rules #####################################################
$IPT -A OUTPUT -o lo -j ACCEPT
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -p icmp -j ACCEPT
$IPT -A INPUT -p icmp -j ACCEPT
# allow DNS queries and replies
$IPT -A OUTPUT -p udp --dport 53 -j ACCEPT
$IPT -A INPUT -p udp --sport 53 -j ACCEPT
# allow all Your possible client applications to work
$IPT -A OUTPUT -p tcp -m multiport --dports ssh,telnet,http,https,xmpp-client,aol,smtp,pop3,imap2,imap3 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
$IPT -A INPUT -p tcp -m multiport --sports ssh,telnet,http,https,xmpp-client,aol,smtp,pop3,imap2,imap3 -m state --state RELATED,ESTABLISHED -j ACCEPT
# End of block of common rules ##############################################
# If You have ftp-client too, this block of rules
# will allow it to work with external ftp servers in both modes.
#
# First, allow ftp-control at client side:
$IPT -A OUTPUT -p tcp -m tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --sport 21 -m state --state ESTABLISHED -j ACCEPT
#
# Then allow ftp-data Active Mode at client side:
# Client accepts RELATED connection from server port 20
# to client port number negotiated in ftp-control connection.
# nf_conntrack_ftp is REQUIRED at client host
# to pick up this client port number from payload of ftp-control packets,
# otherwise You are forced to use 'NEW' instead of 'RELATED'.
# And in the case of 'NEW' You allow connection to ANY port of Your host!
$IPT -A INPUT -p tcp -m tcp --sport 20 -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A OUTPUT -p tcp -m tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
#
# Finally, allow ftp-data Passive Mode at client side:
# Client starts RELATED connection from random own high port number
# to server fixed high port number negotiated in ftp-control connection.
# nf_conntrack_ftp is REQUIRED again at client host
# to pick up this client port number from payload of ftp-control packets,
# otherwise You are forced to use 'NEW' instead of 'RELATED' !
-A OUTPUT -p tcp -m tcp --sport 1024:65535 --dport 1024:65535 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --sport 1024:65535 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT
#######[ Block of rules needed for Local FTP Server ]#######
# This block of rules allows clients to access Your FTP server at this host
# either in Active or Passive mode.
# You may need to enable Passive mode in FTP server config file,
# e.g. with pasv_enable=yes in /etc/vsftpd.conf if vsftpd is Your choice.
#
# Ftp-control at server side:
# (some example rules are given below just to show
# how You can selectively restrict access to Your FTP server):
$IPT -A INPUT -s 1.2.3.0/24 -p tcp -m tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
$IPT -A INPUT -s 5.6.7.8/32 -p tcp -m tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
$IPT -A OUTPUT -p tcp -m tcp --sport 21 -m state --state ESTABLISHED -j ACCEPT
#
# Ftp-data Active Mode at server side:
# Server starts RELATED connection from server port 20
# to client port number negotiated in ftp-control connection.
# nf_conntrack_ftp is REQUIRED to pick up this client port number
# from payload of ftp-control packets,
# otherwise You are forced to use 'NEW' instead of 'RELATED' !
$IPT -A OUTPUT -p tcp -m tcp --sport 20 -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 20 -m state --state ESTABLISHED -j ACCEPT
#
# Ftp-data Passive Mode at server side:
# Server accepts RELATED client connection from random client high port number
# to own fixed high port number negotiated in ftp-control connection.
# nf_conntrack_ftp is REQUIRED to pick up this own fixed high port number
# from payload of ftp-control packets,
# otherwise You are forced to use 'NEW' instead of 'RELATED'.
# And in the case of 'NEW' You allow connection to ANY high port of Your server!
$IPT -A INPUT -p tcp -m tcp --sport 1024:65535 --dport 1024:65535 -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A OUTPUT -p tcp -m tcp --sport 1024:65535 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT
######
Cảng 80 cho FTP ?! – machineaddict
INPUT phải MỚI, ĐƯỢC THÀNH LẬP. Không phải là OUTPUT. –
Cảm ơn bạn đã sửa chữa. Đã sửa. Tôi chỉ cần gõ nó ra và không cho nó nhiều cái nhìn thứ hai. – hsanders