iptables禁止所有端口,iptables禁止端口访问
如果要禁掉所有UDP端口 iptables里怎么配
新建DWORD值:SMBDeviceEnabled
设为0
关闭自己的139端口,ipc和RPC漏洞存在于此。
关闭139端口的方法是在“网络和拨号连接”中“本地连接”中选取“Internet协议(TCP/IP)”属性,进入“高级TCP/IP设置”“WinS设置”里面有一项“禁用TCP/IP的NETBIOS”,打勾就关闭了139端口。
同时也关闭了UDP137、138端口。
关闭Windows默认端口139、445等 —closeport.bat的使用说明
自从Windows2000以来,Windows系统增强了网络服务功能,这同时也降低了安全性,各种蠕虫病毒一波波的肆虐,Windows的网络安全为人们所诟病。
网络本来是一个欢乐缤纷的五彩乐园,但是安全问题使美好的网络蒙上了阴影,使人们小心翼翼如履薄冰,难以在网络上自由顺畅的呼吸,网络成为许多用户心中难舍的痛难解的结。
造成这种结果的主要原因是是windows系统默认开启了一些网络功能开启一些端口,从而置广大用户于危地。
这些功能用户一般用不到,反而成为了重要的安全隐患,给了蠕虫和骇客们可乘之机,成为他们大显身手的乐土。
人们不得求助于防火墙、助手和补丁等等莫名其妙乌七八糟的东西,把一切搞得越来越复杂,因为它们往往给你带来更多的烦恼。
其实,把Windows的一些默认端口关掉,就可以解决绝大部分的问题。
这两天,研究了一下端口的问题。写了一个脚本用来关掉这些默认端口,后面有解释,我的理解不一定正确,其中或有不当之处,欢迎各位批评指正。
这个脚本我在Windows 2003 Server上测试通过,在WinXP和Win2000上面应该也能适用。
[使用说明]
将以下代码复制下来,存为closeport.bat(注意,每个命令应该在一行中)。
运行closeport,重启计算机即可。cmd下运行netstat -an,你会发现相应的端口都已经停止了。
如果希望打开所有端口,运行closeport -o,重启计算机即可。
你可以自己将脚本中你不希望的功能命令掉,然后再运行它。
[注意事项]
1.运行本程序前,最好手工停用DCOM。
方法如下,利用Windows NT/2000/XP标准集成的“dcomcnfg.exe”工具。从命令行执行,打开分布式COM属性窗口,取消“在这台计算机上启用分布式COM”选项即可。(Window 2000\XP\2003 的配置对话框有所不同)。
虽然,这个脚本可以停用DCOM,但是它是通过修改注册表的键值。我不知道效果是否与之相同。
2.重启计算机后,出现过TCP/IP Driver不能启动。因此无法上网的情况。
如果遇到这种情况。请在“设备管理器”中,选择显示隐藏的设备,按连接查看设备,找到TCP/IP protocol driver,在驱动程序页面设置为自动。重启计算机即可。
3.本脚本完全没有任何担保,请慎重使用。最好阅读后面的解释。你可以更好的了解和定制使用它。
================================
@ echo off
rem
rem closeport.bat version 0.2
rem by Spirituel@SMTH
rem
rem This file close the default ports of Windows.
rem Why and how to use it, please conduct to the README.txt.
rem I tested it on Windows 2003 Server. And it should work on WindowsXP and windows2000 as well.
rem If you understand it and find some error, you can describe it clearly and contact with me.
rem Corrections are welcome.
rem NOTICE: NO WARRANTY totally. Please use it carefully.
echo Close the default ports of Windows system.
echo [usage] Use the parameter -o to open the ports again.
echo You can change the file to customize them for yourself.
if "%1"=="-o" goto :open
@ rem -----------------------------------------------
@ rem disable some services, you can add "rem" on the command if you do not want it effects
echo on
@ rem disable the NetBT(NetBios over tcp/ip) device driver, close port TCP139/UDP137/UDP138/TCP445
reg add "HKLM\SYSTEM\CurrentControlSet\Services\NetBT" /v Start /t REG_DWORD /d 0x4 /f
@ rem disable the LmHosts(TCP/IP NetBIOS Helper) device driver, it depends on NetBT
reg add "HKLM\SYSTEM\CurrentControlSet\Services\LmHosts" /v Start /t REG_DWORD /d 0x4 /f
@ rem disable the lanmanserver(server) service, stop IPC$ net share
reg add "HKLM\SYSTEM\CurrentControlSet\Services\lanmanserver" /v Start /t REG_DWORD /d 0x4 /f
@ rem disable the dfs(Distribute File System) service, it depends on server
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Dfs" /v Start /t REG_DWORD /d 0x4 /f
@ rem disable the Browser(Computer Browser) service, it depends on server
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Browser" /v Start /t REG_DWORD /d 0x4 /f
@ rem disable the W32Time(Windows Timer) service, close port UDP123
reg add "HKLM\SYSTEM\CurrentControlSet\Services\W32Time" /v Start /t REG_DWORD /d 0x4 /f
@ rem disable the TermService(Terminal Services) service, its default port is TCP3389
reg add "HKLM\SYSTEM\CurrentControlSet\Services\TermService" /v Start /t REG_DWORD /d 0x4 /f
@ rem disable DCOM
reg add "HKLM\SOFTWARE\Microsoft\Ole" /v EnableDCOM /t REG_SZ /d N /f
goto :end
:open
@ rem -----------------------------------------------
@ rem enable some services, you can add "rem" on the command if you do not want it effects
echo on
@ rem enable the NetBT(NetBios over tcp/ip) device driver, port TCP139/UDP137/UDP138/TCP445
reg add "HKLM\SYSTEM\CurrentControlSet\Services\NetBT" /v Start /t REG_DWORD /d 0x1 /f
@ rem enable the LmHosts(TCP/IP NetBIOS Helper) device driver, it depends on NetBT
reg add "HKLM\SYSTEM\CurrentControlSet\Services\LmHosts" /v Start /t REG_DWORD /d 0x2 /f
@ rem enable the lanmanserver(server) service, stop IPC$ net share
reg add "HKLM\SYSTEM\CurrentControlSet\Services\lanmanserver" /v Start /t REG_DWORD /d 0x2 /f
@ rem enable the dfs(Distribute File System) service, it depends on server
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Dfs" /v Start /t REG_DWORD /d 0x2 /f
@ rem enable the Browser(Computer Browser) service, it depends on server
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Browser" /v Start /t REG_DWORD /d 0x2 /f
@ rem enable the W32Time(Windows Timer) service, port UDP123
reg add "HKLM\SYSTEM\CurrentControlSet\Services\W32Time" /v Start /t REG_DWORD /d 0x2 /f
@ rem enable the TermService(Terminal Services) service, its default port is TCP3389
reg add "HKLM\SYSTEM\CurrentControlSet\Services\TermService" /v Start /t REG_DWORD /d 0x2 /f
@ rem enable DCOM
reg add "HKLM\SOFTWARE\Microsoft\Ole" /v EnableDCOM /t REG_SZ /d Y /f
:end
@ rem enable tcpip device driver
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip" /v Start /t REG_DWORD /d 0x2 /f
@echo off
pause
================================
[解释说明]
Windows问题主要来自TCP135、TCP139、TCP445等默认端口、提供的IPC$默认共享,以及没有默认开启但非常危险的终端服务(Terminial Services)。这个脚本可以把它们关掉。下面是脚本中命令的解释。
1. NetBios和IPC$的问题。
reg add "HKLM\SYSTEM\CurrentControlSet\Services\NetBT" /v Start /t REG_DWORD /d 0x4 /f
禁用NetBT的驱动程序。这可以关闭TCP139/UDP137/UDP138/TCP445。
由于IPC$是利用了这些端口,因此应该已经不能使用了,这会使你的网络打印机和网络文件共享无效。
linux运维之iptables的一些操作(参考大神的)
1.安装iptables管理命令
2.加载防火墙的内核模块
3.查看已加载的模块
4.启动防火墙
首先停止firewalld
开启iptables
1.查看防火墙规则
2.清除防火墙规则
3.添加防火墙规则
4.网络连接状态
5.删除某个规则
1.禁止某个端口访问
2.规则解释:
3.禁止除跳板机以外的IP访问
4.匹配端口范围
?5.?匹配ICMP类型
6.一些操作
1、封掉10.0.0.7
2、让10.0.0.7和SSH客户端(10.0.0.1)服务器可以Ping,其它的不能Ping
3、封掉3306端口
1.从上往下依次匹配
2.一但匹配上,就不在往下匹配了
3.默认规则,默认的情况,默认规则是放行所有
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
iptables A INPUT -p tcp -m state --dport 22 -j DROP
禁止一个数据包:
tcp协议
访问的端口是22
禁止源地址是10.0.0.7的主机访问22端口
禁止源地址是10.0.0.7的主机访问任何端口
禁止源地址是10.0.0.8的主机访问80端口
禁止除了10.0.0.7以外的地址访问80端口
2条规则冲突,会以谁先谁为准
禁止10.0.0.7访问22和80端口
禁止10.0.0.7访问22到100之间的所有端口
禁止所有主机ping
放行10.0.0.7可以ping
只允许10.0.0.7可以ping
等同于上一条,优化版,只要不是10.0.0.7就不允许ping
优先级:
匹配频次最高的条件放前面
Linux——iptables 禁止 IP和端口
禁止指定 IP
禁止指定 IP段
禁止指定 IP和端口
查看当前的IP规则列表
用命令 iptables -vnL 查看效果:
参数-I是表示 Insert (添加),-D表示 Delete (删除)。后面跟的是规则, INPUT 表示入站,10.0.28.15表示要封停的IP, DROP 表示放弃连接。
限制syn并发的次数以及同一个IP 新建连接数的数量
linux-iptables多端口限制
在Linux网络应用中,我们经常需要开放一些端口给指定的IP,如果我们不对端口进行整理,就可能出现多开放一些有风险的端口,导致系统存在安全隐患。
1、连续端口,iptables默认就支持多个连续端口的规则
iptables? -A? INPUT? -s 192.168.122.0/23? -p? tcp --dport 21:23? -j? ACCEPT? #端口21 22 23
2、不连续的端口,iptables有一个mutiport的模块,需要手动指定加载一下
iptables? -A? INPUT? -s 192.168.122.0/23? -p? tcp -m multiport? --dport 21,23,27,44? -j? ACCEPT? ? #端口21 23? 27? 44
也可以配合连续IP的规则使用
iptables? -A? INPUT? -s 192.168.122.0/23? -p? tcp? -m multiport? --dport 21:23,2227:2230? -j? ACCEPT? ? ? #端口 21 22 23 2227? 2228 2229? 2230
一般在设置规则的时候,要先整理一下端口所属的服务,尽量将同一个服务的端口写在同一条,如果不是同一个服务的尽量分开,方便管理。

LInux iptables 屏蔽设置
1.修改SSH配置文件:/etc/ssh/sshd_config #找到Port 22,这里是标识默认使用22端口,修改为:
Port 22
Port 1234
/etc/init.d/sshd restart
#这样SSH端口将同时工作在22、1234上
查看防火墙规则
1、iptables -nvL
2、more /etc/sysconfig/iptables
2.添加防火墙规则
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 1234 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/rc.d/init.d/iptables restart
然后使用SSH工具 测试 你所设置的端口是否能正常使用
如果能正常使用返回到第一步,删除原来的22端口,以及修改防火墙配置文件
之所以先设置成两个端口,测试成功后再关闭一个端口,是为了方式在修改的过程中,万一出现掉线、断网、误操作等未知情况时候,还能通过另外一个端口连接上去调试以免发生连接不上的状况。
开启自动启动:chkconfig iptables on
开启不自动启动:chkconfig iptables off
附录:
设定预设规则,INPUT链默认拒绝,OUTPUT链默认接受,FORWARD链默认拒绝
iptables -F #清除预设表filter中的所有规则链的规则
iptables -X #清除预设表filter中使用者自定链中的规则
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
iptables -L -n --line #按行数显示防火墙规则
iptables -D INPUT 1 #删除INPUT表第一条
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT #来源、目的为127.0.0.1都接受,这条放最后就可以了
1、安装iptables防火墙
如果没有安装iptables需要先安装,CentOS执行:
yum install iptables
Debian/Ubuntu执行:
apt-get install iptables
2、清除已有iptables规则
iptables -F
iptables -X
iptables -Z
3、开放指定的端口
#允许本地回环接口(即运行本机访问本机)
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
# 允许已建立的或相关连的通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#允许所有本机向外的访问
iptables -A OUTPUT -j ACCEPT
# 允许访问22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#允许访问80端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#允许FTP服务的21和20端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
#如果有其他端口的话,规则也类似,稍微修改上述语句就行
#禁止其他未允许的规则访问
iptables -A INPUT -j REJECT (注意:如果22端口未加入允许规则,SSH链接会直接断开。)
iptables -A FORWARD -j REJECT
4、屏蔽IP
#如果只是想屏蔽IP的话“3、开放指定的端口”可以直接跳过。
#屏蔽单个IP的命令是
iptables -I INPUT -s 123.45.6.7 -j DROP
#封整个段即从123.0.0.1到123.255.255.254的命令
iptables -I INPUT -s 123.0.0.0/8 -j DROP
#封IP段即从123.45.0.1到123.45.255.254的命令
iptables -I INPUT -s 124.45.0.0/16 -j DROP
#封IP段即从123.45.6.1到123.45.6.254的命令是
iptables -I INPUT -s 123.45.6.0/24 -j DROP
5、查看已添加的iptables规则
iptables -L -n
v:显示详细信息,包括每条规则的匹配包数量和匹配字节数
x:在 v 的基础上,禁止自动单位换算(K、M) vps侦探
n:只显示IP地址和端口号,不将ip解析为域名
6、删除已添加的iptables规则
将所有iptables以序号标记显示,执行:
iptables -L -n --line-numbers
比如要删除INPUT里序号为8的规则,执行:
iptables -D INPUT 8
7、iptables的开机启动及规则保存
CentOS上可能会存在安装好iptables后,iptables并不开机自启动,可以执行一下:
chkconfig --level 345 iptables on
开启自动启动:chkconfig iptables on
开启不自动启动:chkconfig iptables off
将其加入开机启动。
CentOS上可以执行:
service iptables save
/etc/rc.d/init.d/iptables save
/etc/rc.d/init.d/iptables restart
保存规则。
另外更需要注意的是Debian/Ubuntu上iptables是不会保存规则的。
需要按如下步骤进行,让网卡关闭是保存iptables规则,启动时加载iptables规则:
创建/etc/network/if-post-down.d/iptables 文件,添加如下内容:
#!/bin/bash
iptables-save /etc/iptables.rules
执行:chmod +x /etc/network/if-post-down.d/iptables 添加执行权限。
创建/etc/network/if-pre-up.d/iptables 文件,添加如下内容:
#!/bin/bash
iptables-restore /etc/iptables.rules
执行:chmod +x /etc/network/if-pre-up.d/iptables 添加执行权限。
关于更多的iptables的使用方法可以执行:iptables --help或网上搜索一下iptables参数的说明。
注:每次服务在停止之前会自动将现有的规则保存
在 /etc/sysconfig/iptables 这个文件中去.
iptables实现禁用端口
iptables实现禁用端口!
这里用iptables屏蔽端口25,防止被利用发垃圾邮件导致VPS被封
用iptables屏蔽全部IP连接25端口:
上面这一条命令就够了
=======
如果需要特殊情况请看下面的命令
只允许特定ip连接25端口:
重新禁止此ip连接25端口,删除上述许可记录就可以了:
需要禁止特定ip连接25端口: