iptables:安全应用,防火墙
windows和linux都有防火墙,企业的边缘会部署防火墙保证企业内部的局域网是安全的。针对个人电脑会有防火墙保证系统是安全的。

防火墙是唯一通道。

防火墙分类(这里的分类只是一个简单的分类,还有很多种):
1.包过滤防火墙,速度快,但是只检查包头,不检查数据区,不建立连接状态表,安全性能低。
2.应用代理防火墙。安全高,性能高。只检查包头,不检查数据区,建立连接状态表。 防火墙还可以分为硬件和软件防火墙。硬件防火墙就是一个硬件设备,软件防火墙比如windows的360和linux的开源的iptables。

iptables是linux自带的小型的防火墙。

table有4张表,filter过滤表,写的是防火墙的策略。nat表做网络相关的。mangle和raw是内核部分,不用管。
command:过滤表的策略。不允许哪个网卡进去或者出来,
match:匹配条件。匹配数据包来做。
target:匹配的数据包进行操作。就2种可能性,通过和不通过。如果没问题就通过,有问题就不通过。

filter表:针对本机,itables的默认表有:input,forward,output链。
nat表:转换数据包的源地址和目的地址。包括:prerouting,postrouting,out链。 https://www.linuxidc.com/Linux/2017-02/140556.htm
以下是centos的:
(ubuntu没有这些命令)
service iptables stop
service iptables start
service iptables restart
root@ubuntu:/# iptables -L  (查看iptables防火墙的默认策略)
Chain INPUT (policy DROP)
target prot opt source destination
ufw-before-logging-input all -- anywhere anywhere
ufw-before-input all -- anywhere anywhere
ufw-after-input all -- anywhere anywhere
ufw-after-logging-input all -- anywhere anywhere
ufw-reject-input all -- anywhere anywhere
ufw-track-input all -- anywhere anywhere
Chain FORWARD (policy DROP)
target prot opt source destination
DOCKER-ISOLATION all -- anywhere anywhere
DOCKER all -- anywhere anywhere
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
以下是centos的:
vim sysconfig/iptables :防火墙主配置文件,这里面存储的都是防火墙的策略,如果要添加策略,建议通过命令行来添加,而不是修改这个文件来添加。
*filter : 表示是一个过滤表
:INPUT ACCEPT [0:0] :这3个链都是开的
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
: RH_Firewall-1-INPUT - [0:0] : RH_Firewall-1-INPUT是防火墙当前策略的一个名字
-A INPUT -j RH_Firewall-1-INPUT
-A FORWARD -j RH_Firewall-1-INPUT
-A RH_Firewall-1-INPUT -p lo -j ACCEPT (lo同意)
-A RH_Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT (icmp同意)
-A RH_Firewall-1-INPUT -p 50 -j ACCEPT (50.51端口可以访问)
-A RH_Firewall-1-INPUT -p 51 -j ACCEPT
-A RH_Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0251 -j ACCEPT (udp可以访问)
写策略:
查看规则-L:iptables -L ,iptables -L -n ,iptables -L -n --line-number
修改默认规则-P:iptables -P INPUT DROP (备注:为链设置默认的target可用的是DROP和ACCEPT,作为最后一条规则被执行)
追加规则-A:作为最后一条规则,iptables -A INPUT -p tcp --dport 22 -j ACCEPT
删除规则-D:iptables -D INPUT 3 ,iptables -D INPUT -s 192.168.0.1 DROP
修改规则-R:iptables -R INPUT 3 -s 192.168.0.1 -j ACCEPT
清空规则-F:iptables -F ,iptables -F INPUT ,iptables -t nat -F
root@ubuntu:/# iptables -L  (默认策略,和iptables配置文件的信息是一致的)
Chain INPUT (policy DROP)
target prot opt source destination
ufw-before-logging-input all -- anywhere anywhere
ufw-before-input all -- anywhere anywhere
ufw-after-input all -- anywhere anywhere root@ubuntu:/# iptables -L -n (会把目标信息以地址的形式显示出来) iptables -L -n --line-number (显示行号,删除规则是基于行号删除的)
INPUT属于进入的概念,iptables -P INPUT DROP,-P是修改规则,规则只有3种inout,output,forward, -P表示针对inout或output或forward进行调整。vim sysconfig/iptables文件里面的:INPUT  ACCEPT  [0:0] :FORWARD  ACCEPT  [0:0]  : OUTPUT  ACCEPT  [0:0] 默认都是ACCEPT的,iptables -P  INPUT  DROP,iptables-save(修改了要保存),这样INPUT就是DROP了。  
root@ubuntu:/# iptables -L
Chain INPUT (policy DROP) :input链,都进不来
target prot opt source destination
ufw-before-logging-input all -- anywhere anywhere Chain FORWARD (policy DROP) :forward链,都不能跳转
target prot opt source destination
DOCKER-ISOLATION all -- anywhere anywhere
DOCKER all -- anywhere anywhere
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ufw-before-logging-forward all -- anywhere anywhere Chain OUTPUT (policy ACCEPT) :output链,都出得去
target prot opt source destination
ufw-before-logging-output all -- anywhere anywhere
ufw-before-output all -- anywhere anywhere Chain DOCKER (1 references)
target prot opt source destination Chain DOCKER-ISOLATION (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere Chain ufw-after-input (1 references)
target prot opt source destination
ufw-skip-to-policy-input udp -- anywhere anywhere udp dpt:netbios-ns
ufw-skip-to-policy-input udp -- anywhere anywhere udp dpt:netbios-dgm
ufw-skip-to-policy-input tcp -- anywhere anywhere tcp dpt:netbios-ssn
Chain是链,input和output是输入和输出,input链是从外面进来到主机这里来,针对进来的信息做什么策略,output链是从主机出去,针对出去的做什么策略,forward是指当要访问资源的时候必须通过我这台计算机转向,A要访问C必须经过B主机,就要通过B主机转向,B直接重定向到C上面去。

input和output是针对当前主机而言的,forward是针对其他电脑的。 iptables -L是打印当前3个链的策略信息。INPUT是DROP表示任何人访问这台电脑都是禁止的,OUTPUT是DROP表示当前电脑不能访问外部。
root@ubuntu:/# iptables -P INPUT ACCEPT (-P修改input链)
root@ubuntu:/# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ufw-before-logging-input all -- anywhere anywhere
ufw-before-input all -- anywhere anywhere
追加规则:要往input链或者output链或者forward链里面来追加规则。
root@ubuntu:/# iptables -A INPUT -p tcp --dport 22 -j ACCEPT (-p表示要使用tcp相关的协议,)
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy DROP)
num target prot opt source destination
1 ufw-before-logging-input all -- anywhere anywhere
7 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh (通过tcp协议在任意地方连接ssh都是同意)
修改规则-R:iptables -R INPUT 3 -s 192.168.0.1 -j ACCEPT
root@ubuntu:/# iptables -R INPUT 7 -s 192.168.0.1 -j DROP (7表示input链里面的第7行,-s表示不是原来的任意地址了,192.168.0.1进来的要拒绝)
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy DROP)
num target prot opt source destination
7 DROP all -- 192.168.0.1 anywhere
删除规则-D:iptables -D INPUT 3 ,iptables -D INPUT -s 192.168.0.1 DROP
root@ubuntu:/# iptables -D INPUT 7 (根据行号删除)
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy DROP) :就没有7了
num target prot opt source destination
root@ubuntu:/#  iptables -A INPUT -p  SFTP --dport 22 -j ACCEPT
root@ubuntu:/# iptables -A INPUT -p FTP --dport 21 -j ACCEPT
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
7 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
8 ACCEPT tcp -- anywhere anywhere tcp dpt:ftp 清空规则-F:iptables -F (清空所有链),iptables -F INPUT (清空input链),iptables -t nat -F
匹配条件:是从最大的范围到最小范围划分的。计算机上网是要通过网卡上网的,网卡连接的是网线,出入接口是从网卡出去进来,对应的是input和output链。源地址和目的地址是不允许某个ip进来或者访问某个ip。

-A追加规则,output链,将从网卡eth0出去和进来的都拒绝。
root@ubuntu:/# iptables -A OUTPUT -o ens33 -j DROP
root@ubuntu:/# iptables -L --line-number
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
7 DROP all -- anywhere anywhere
root@ubuntu:/# ping www.baidu.com (出不去了)(删除后就可以ping了)
PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted

root@ubuntu:/# iptables -A INPUT -s 192.168.2.114 -j DROP
root@ubuntu:/# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.2.114 anywhere root@ubuntu:/# iptables -A OUTPUT -d www.baidu.com -j DROP
root@ubuntu:/# iptables -L
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
7 DROP all -- anywhere 163.177.151.110
8 DROP all -- anywhere 163.177.151.109 root@ubuntu:/# ping www.baidu.com
PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
root@ubuntu:/# iptables -D OUTPUT 7
root@ubuntu:/# iptables -D OUTPUT 8
iptables: Index of deletion too big.
root@ubuntu:/# iptables -D OUTPUT 7
root@ubuntu:/# ping www.baidu.com
PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data.
64 bytes from 163.177.151.110: icmp_seq=1 ttl=128 time=13.7 ms

-A追加一个规则,来自于192.168.0.1网段的,tcp协议的22端口的,都禁止。

按照ip地址加端口加协议用的比较多。禁网卡用的少。

iptables的匹配规则:

不建议修改配置文件:iptables命令方式改的规则,如果不保存到配置文件,则重启后就没有了,service iptables save就是将命令行方式改的规则保存到配置文件。

linux:如果既有配置文件又有命令参数,如果通过命令方式执行的都是临时的,通过配置文件都是永久的。

linux iptables:安全应用,防火墙的更多相关文章

  1. CentOS Linux iptables 防火墙

    快速安装,配置,启动,检查 - 关闭 5002 - 5011 端口开放所有其它 yum install iptables iptables -F iptables -X iptables -Z ipt ...

  2. 在新安装的Linux系统中,防火墙默认是被禁掉的,一般也没有配置过任何防火墙的策略,所有不存在/etc/sysconfig/iptables文件。

    原因:在新安装的Linux系统中,防火墙默认是被禁掉的,一般也没有配置过任何防火墙的策略,所有不存在/etc/sysconfig/iptables文件. 解决办法: .随便写一条iptables命令配 ...

  3. linux iptables常用命令之配置生产环境iptables及优化

    在了解iptables的详细原理之前,我们先来看下如何使用iptables,以终为始,有可能会让你对iptables了解更深 所以接下来我们以配置一个生产环境下的iptables为例来讲讲它的常用命令 ...

  4. Linux iptables

    一.简介 http://liaoph.com/iptables/ 二.操作 1)查看规则 iptables -t filter -L -n iptables -t nat -L -n iptables ...

  5. LINUX系统怎么关闭防火墙?

    所谓防火墙指的是一个由软件和硬件设备组合而成.在内部网和外部网之间.专用网与公共网之间的界面上构造的保护屏障.是一种获取安全性方法的形象说法,它是一种计算机硬件和软件的结合,使Internet与Int ...

  6. Linux iptables 配置规则

    Linux iptables 防火墙配置规则 前言:把网上我感觉不错iptables的访问规则都统一在这里,以后做参考. modprobe ipt_MASQUERADE modprobe ip_con ...

  7. Linux iptables用法与NAT

    1.相关概念 2.iptables相关用法 3.NAT(DNAT与SNAT) 相关概念 防火墙除了软件及硬件的分类,也可对数据封包的取得方式来分类,可分为代理服务器(Proxy)及封包过滤机制(IP ...

  8. Centos中iptables和firewall防火墙开启、关闭、查看状态、基本设置等(转)

    iptables防火墙 1.基本操作 # 查看防火墙状态 service iptables status   # 停止防火墙 service iptables stop   # 启动防火墙 servi ...

  9. Linux iptables:场景实战一

    <Linux iptables:规则原理和基础>和<Linux iptables:规则组成>介绍了iptables的基础及iptables规则的组成,本篇通过实际操作进行ipt ...

  10. 第7章 Iptables与Firewalld防火墙。

    第7章 Iptables与Firewalld防火墙.     Chapter7_听较强节奏的音乐能够让您更长时间的投入在学习中. <Linux就该这么学> 00:00/00:00     ...

随机推荐

  1. AC日记——Tree poj 3237

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9233   Accepted: 2431 Description ...

  2. 洛谷——P1588 丢失的牛

    P1588 丢失的牛 题目描述 FJ丢失了他的一头牛,他决定追回他的牛.已知FJ和牛在一条直线上,初始位置分别为x和y,假定牛在原地不动.FJ的行走方式很特别:他每一次可以前进一步.后退一步或者直接走 ...

  3. SWF代码分析与破解之路 (YueTai VIP视频信息获取工具) Socket续篇

    引言 上一篇 <Socket与站点保密应用 (隐藏链接的视频下载)>大大咧咧地从 WEB 讲 Socket.再到 TCP/IP 等协议,又再讲到 Wireshark 怎样抓IP包分析.最还 ...

  4. C++类的大小(转)

    一个空类class A{};的大小为什么是1,因为如果不是1,当定义这个类的对象数组时候A objects[5]; objects[0]和objects[1]就在同一个地址处,就无法区分. 单继承 # ...

  5. C中參数个数可变的函数

    一.什么是可变參数 我们在C语言编程中有时会遇到一些參数个数可变的函数,比如printf()函数,其函数原型为: int printf( const char* format, ...); 它除了有一 ...

  6. c++ builder 版CreateAnonymousThread用法

    万一老师的<如今, Delphi 的多线程已经很易用了!>讲到了TThread.CreateAnonymousThread用法 如今我来讲在c++ builder使用 CreateAnon ...

  7. Android开源git40个App源代码

    (JamsMusicPlayer)非常棒的音乐播放器(new)   (F8)日程安排的软件   (Conversations)基于XMPP的应用   (Bitocle)能够在手机上查看自己github ...

  8. PE发送报文

    步骤: 1. 在 action 中使用发送报文,要指定报文在 router 端的交易名称 2. 如果使用 supe.execute(context) 来发送,不需要第一步 3. 配置从网银到 rout ...

  9. mysql查询sql中检索条件为大批量数据时处理

    当userIdArr数组值为大批量时,应如此优化代码实现

  10. python staticmethod和classmethod(转载)

    staticmethod, classmethod 分别被称为静态方法和类方法. staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象(python里只说对象总是容易产 ...