您的位置:首页 > 运维架构 > Linux

linux 网络安全

2018-09-05 10:41 302 查看
安全体系概览:
Firewalls --> TCP Wrappers --> Xinetd --> PAM -- > SELinux --> Server specific

Firewalls: netfilter/iptables
netfilter组件 内核空间,是内核一部分
iptables组件 用户空间,提供管理防火墙的手段,通过iptables插入、删除、修改规则

OSI七层模型:
应用层 ftp http smtp pop3
表示层 上三层,一般操作系统和应用的功能
会话层

传输层 源端口2222/tcp ---> 目的端口 80/tcp 传输协议TCP,UDP

网络层 源IP x.x.x.x ---> 目的IP y.y.y.y 下四层,数据流层

数据链路层 源MAC 00:50:fc:12:34:56 ---> 目的MAC(网一段主机,网关) 00:50:fc:12:34:59
物理层

TCP: 面向连接,可靠的传输协议 类似于三次握手,window...
UDP: 非面向连接,不可靠的传输协议

A (封装)---------- (解封装)B

==============iptables================
语法:
iptables [-t 要操作的表] <操作命令> [要操作的链] [规则号码] [匹配条件] [-j 匹配后的动作]
小写 大写 大写 小写 大写

示例:
iptables -L //查看,默认filter表
iptables -t filter -L
iptables -t nat -L
iptables -t raw -L
iptables -t mangle -L
=================================================================================
常见的操作命令:
-L 查看,v详细,n不反解 --line-number
-A 追加,放置最后一条
-I 插入,默认插入成第一条
-D 删除
-F 清空flush
-P 设置默认策略policy

匹配的条件:
-s 192.168.2.0/24 源地址
-d 192.168.2.1 目标地址
-p tcp|upd|icmp 协议
-i eth0 input 从eth0接口进入的数据包
-o eth0 output 从eth0出去的数据包
-p tcp --dport 80 目标端口是80,必须和-p tcp|udp 连用

处理方法:
-j ACCEPT 允许
-j DROP 丢弃
-j REJECT 拒绝
-j SNAT 源地址转换
-j DNAT 目标地址转换
-j LOG 写日志
...
=================================================================================
iptables -t filter -L INPUT
iptables -L INPUT
iptables -F INPUT
iptables -F
iptables -t nat -F

操作示例:
iptables -F
iptables -A INPUT -j REJECT //拒绝所以进入filter表INPUT链的所有数据包
iptables -I INPUT -p tcp --dport 5900 -j ACCEPT
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
iptables -I INPUT -p icmp -j ACCEPT
iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -nL

针对FTP:连接追踪模块
iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -I INPUT -p tcp --dport 21 -j ACCEPT //打开控制端口
modprobe ip_conntrack_ftp 加载连接追踪模块(临时)
lsmod |grep ftp
[root@station230 ~]# vim /etc/sysconfig/iptables-config
IPTABLES_MODULES="ip_conntrack_ftp"

标准流程
===============================================================================
[root@station230 ~]# iptables -F
[root@station230 ~]# iptables -A INPUT -i lo -j ACCEPT
[root@station230 ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@station230 ~]# iptables -A INPUT -s 192.168.2.0/24 -j ACCEPT 公司内网
[root@station230 ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@station230 ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@station230 ~]# iptables -A INPUT -p tcp --dport 21 -j ACCEPT
[root@station230 ~]# iptables -A INPUT -j REJECT
[root@station230 ~]# service iptables save
将当前规则保存到 /etc/sysconfig/iptables: [确定]
[root@station230 ~]# chkconfig iptables on
[root@station230 ~]# iptables -vnL INPUT
[root@station230 ~]# vim /etc/sysconfig/iptables-config
IPTABLES_MODULES="ip_conntrack_ftp"
[root@station230 ~]# service iptables restart

=======================================================================
小知识:
常见协议的端口 /etc/services
[root@station230 ~]# grep ^http /etc/services
服务 协议 端口/传输协议
ssh ssh 22/tcp
http http 80/tcp
https 443/tcp
dns domain 53/udp,53/tcp
mail smtp 25/tcp 发信协议
smtps 465/tcp # SMTP over SSL (TLS)
pop3 110/tcp 收信协议
pop3s 995/tcp
imap 143/tcp
imaps 993/tcp
dhcp bootps 67/udp # BOOTP server
nfs nfs 2049/tcp
samba 137,138,139/tcp
445/tcp
ftp ftp 21/tcp
ftp-data 20/tcp
ntp ntp 123/udp # Network Time Protocol
syslog syslog 514/udp
=======================================================================
真实iptables实例:
一些常见允许外网访问的服务:
网站 http 80/tcp; https 443/tcp;
邮件 mail smtp 25/tcp 发信协议
smtps 465/tcp # SMTP over SSL (TLS)
pop3 110/tcp 收信协议
pop3s 995/tcp
imap 143/tcp
远程管理: ssh 22/tcp

一些常见不允许外网访问的服务:
文件服务器:
NFS
SAMBA
FTP
[root@station230 ~]# iptables -F
[root@station230 ~]# iptables -A INPUT -i lo -j ACCEPT
[root@station230 ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@station230 ~]# iptables -A INPUT -s 192.168.2.0/24 -j ACCEPT
[root@station230 ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@station230 ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@station230 ~]# iptables -A INPUT -p tcp --dport 25 -j ACCEPT
[root@station230 ~]# iptables -A INPUT -p tcp --dport 110 -j ACCEPT
[root@station230 ~]# iptables -A INPUT -j REJECT

外网 =====> ××× =====> 内网FTP,SAMBA,NFS

========================================================================
NAT:

内网 NAT服务器 外网
client(192.168.2.80)--->eth0(192.168.2.10)NAT Server(1.1.1.254)eth1 --> Web(1.1.1.1)
==SNAT,源地址转换
作用:让内网用户可以通过NAT服务器访问外网
1. iptables
# iptables -t nat -A POSTROUTING -j SNAT --to 1.1.1.1
# service iptables save
当内网的数据包到达POSTROUTING链时,修改数据的源地址1.1.1.1

2. 打开内核的路由转发机制
# sysctl -a |grep ip_forward
net.ipv4.ip_forward = 0
# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
# sysctl -p 立即生效

=============================
echo 0 > /proc/sys/net/ipv4/ip_forward
=============================

测试:所有内网将网关指定NAT服务器的内网地址:
# links -dump 1.1.1.100
welcome to china

3. 限制上网
# iptables -A FORWARD -s 192.168.2.168 -j REJECT //拒绝某个主机上网

===DNAT,目标地址转换
外网用户访问内网服务器(必须以SNAT为基础)
内网 NAT服务器 外网
client(192.168.2.168)<---eth0(192.168.2.10)NAT Server(1.1.1.1)eth1 <-- Web(1.1.1.100)
# iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.2.168
# iptables -t nat -nL

links -dump 1.1.1.1
links -dump www.uplooking.com(1.1.1.1)

代理服务器:Squid,Nginx

正向代理(包括透明代理) squid nginx
作用:让内网用户上网,缓存(内存,硬盘),加快访问速度,节约通信带宽
访问控制ACL实现对用户上网行为进行控制(时间、网站、内容...)
防止内部主机受到×××
反向代理 squid,nginx
作用:给网站加速
==================================================================================
[root@nat-server ~]# iptables -F
[root@nat-server ~]# iptables -t nat -F
[root@nat-server ~]# service iptables save

==squid:
软件包:squid
端口: 3128/tcp默认
配置文件:/etc/squid/squid.conf
日志文件: /var/log/squid

正向代理
client(192.168.2.80)--->eth0(192.168.2.199)squid Server(1.1.1.254)eth1 --> Web(1.1.1.1)
一、配置squid
# yum -y install squid
# vim /etc/squid/squid.conf
http_port 3128 //squid监听的端口
cache_mem 16000 MB //设置squid内存缓冲大小
cache_dir ufs /var/spool/squid 50000 16 256 //设置squid硬盘缓冲大小
cache_effective_user squid
cache_effective_group squid
dns_nameservers 202.106.0.20 8.8.8.8
cache_mgr tianyun@126.com

===================================================================================
# service squid start
init_cache_dir /var/spool/squid... /etc/init.d/squid: line 62: 5504 已放弃 $SQUID -z -F -D >> /var/log/squid/squid.out 2>&1
启动 squid:/etc/init.d/squid: line 42: 5505 已放弃 $SQUID $SQUID_OPTS >> /var/log/squid/squid.out 2>&1
[失败]

visible_hostname squid
===================================================================================

# service squid start
# chkconfig squid on
[root@nat-server ~]# netstat -tnlp |grep :3128
tcp 0 0 0.0.0.0:3128 0.0.0.0:* LISTEN 5526/(squid)

从客户端测试代理服务器:
浏览器:需要手工设置代理
测试结果:代理服务默认不为任何主机代理

解决方案:
ACL,限制用户访问(时间、目标网站、内容...) 访问控制列表
# vim /etc/squid/squid.conf
/INSERT
方案一:为所有主机代理
acl all src 0.0.0.0/0.0.0.0
http_access allow all

方案二:为部分主机代理
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
acl it_net src 192.168.2.0/24
http_access allow it_net
# service squid reload

案例1:允许192.168.2.0/24网段的主机使用squid
acl it_net src 192.168.2.0/24
http_access allow it_net

案例2: 允许192.168.2.0/24网段的主机使用squid,(周一到周五 9:00-16:00)
acl it_net src 192.168.2.0/24
acl worktime time MTWHF 9:00-16:00
http_access allow it_net worktime

案例3 拒绝主机
acl wangcheng src 192.168.2.168
http_access deny wangcheng

案例4 禁止用户访问URL包含qq.com网站,-i忽略大小写
acl disable_web url_regex -i qq.com
http_access deny disable_web

案例5 禁止用户下载*.mp3 *.exe *.iso
acl disable_down urlpath_regex -i \.mp3$ \.exe$ \.iso$
http_access deny disable_down

=============================================================
acl all src 0.0.0.0/0.0.0.0
acl it_net src 192.168.2.0/24
acl hr_net src 192.168.3.0/24
acl worktime time MTWHF 9:00-16:00
acl disable_web url_regex -i qq.com
acl disable_down urlpath_regex -i \.mp3$ \.exe$ \.iso$
acl wangcheng src 192.168.2.168

http_access deny wangcheng
http_access deny disable_down
http_access deny disable_web
http_access allow it_net
http_access allow hr_net worktime
http_access deny all
=============================================================

透明代理(以正向代理为基础)
代理服务器:IP 192.168.2.10
http_port: 3128
1. iptables数据包重定向
[root@squid-server ~]# iptables -t nat -F
[root@squid-server ~]# iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 3128
[root@squid-server ~]# service iptables save

2. squid支持透明代理
[root@squid-server ~]# vim /etc/squid/squid.conf
http_port 3128 transparent
[root@squid-server ~]# service squid reload

注意:客户端必须将数据包发给代理服务器
1. 如果客户端和代理服务器在同网段,将网关指向代理服务器
2. 如果客户在不同的vlan中,通过网络设备策略将所有用户访问外网的数据包发至代理服务器

反向代理
============================
[root@squid-server ~]# service iptables stop
client(192.168.2.115)---> 前端反向代理服务器(192.168.2.10) --> Web(192.168.2.108)
本身已经可以访问Internet 前端反向代理服务器(192.168.2.168)
前端反向代理服务器(192.168.2.169)
。。。。。。。。。。。。。。。
一、配置squid
# service iptables stop
# yum -y install squid
# vim /etc/squid/squid.conf
cache_mem 16000 MB //设置squid内存缓冲大小
cache_dir ufs /var/spool/squid 50000 16 256 //设置squid硬盘缓冲大小
cache_effective_user squid
cache_effective_group squid
dns_nameservers 202.106.0.20 8.8.8.8
cache_mgr tianyun@126.com

http_port 80 vhost //反向代理
cache_peer 192.168.2.108 parent 80 0 //源站
acl all src 0.0.0.0/0.0.0.0
http_access allow all

cache_peer Web服务器地址 服务器类型 http端口 icp端口 选项
cache_peer 192.168.2.108 parent 80 0

=============================================================================
给一个源站做反向代理
cache_peer 192.168.2.108 parent 80 0

给多个源站做反向代理
cache_peer 192.168.2.100 parent 80 0 originserver weight=1 name=tianyun
cache_peer 192.168.2.120 parent 80 0 originserver weight=1 name=uplooking
cache_peer 192.168.2.130 parent 80 0 originserver weight=1 name=126
cache_peer_domain tianyun www.tianyun.com
cache_peer_domain uplooking www.uplooking.com
cache_peer_domain 126 www.126.com
==============================================================================
如果启动失败,检查80端口是否被占用

从客户端测试:必须使用域名访问
/etc/hosts
192.168.2.10 www.tianyun.com www.uplooking.com www.126.com
反向代理服务器

CDN
========================================
Content Delivery Network 内容分发网络
智能DNS(view) + 反向代理(squid,nginx)

为什么要使用CDN?
1. 解决南北互通的问题
2. 访问加速
3. 降低运营成本
4. 提高网站可靠性
5. 防DDOS×××

mailq
postqueue -p 查看邮件队列

postqueue -f 强制刷新一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iptables 安全