海运的博客

Iptables日记模块LOG使用

发布时间:April 30, 2012 // 分类:Iptables // No Comments

Iptables匹配相应规则后会触发一个动作,filter和nat表一般常用的有以下目标操作。

ACCEPT #允许数据包通过
DROP #丢弃数据包,不对该数据包进一步处理
REFECT #丢弃数据包,同时发送响应报文
--reject-with tcp-reset 返回tcp重置
--reject-with icmp-net-unreachable 返回网络不可达
--reject-with icmp-host-unreachable  返回主机不可达
RETURN #转到其它链处理
LOG #将数据包信息记录到syslog

本文就记录下LOG规则的使用,示例:进入的tcp端口为80的数据包记录到日记,错误级别err,描述前缀为INPUT,记录IP/TCP相关信息。

#https://www.haiyun.me
modprobe ipt_LOG #加载模块
iptables -A INPUT -p tcp --dport 80 -j LOG --log-level err  --log-prefix "INPUT" --log-ip-options --log-tcp-sequence
--log-level #错误级别
--log-prefix "INPUT" #描述前缀
--log-ip-options #记录IP信息
--log-tcp-sequence #记录TCP序列号

然后访问服务器80端口测试,通过dmesg查看记录的信息如下:

INPUTIN=eth0 OUT= MAC=00:0c:29:73:e0:19:8c:89:a5:65:3a:4a:08:00 SRC=192.168.1.16 DST=192.168.1.2 \
LEN=522 TOS=0x00 PREC=0x00 TTL=128 ID=27499 DF PROTO=TCP SPT=5430 DPT=80 SEQ=3847892455 ACK=3435733082 WINDOW=16344 RES=0x00 ACK PSH URGP=0 

还可以修改syslog将日志写入到文件。

vim /etc/syslog.conf #添加以下内容
kern.err           /var/log/iptables #日志文件路径
/etc/init.d/syslog restart #重启syslog服务

Linux/Centos服务器禁止udp发包防udp-flood攻击

发布时间:April 23, 2012 // 分类:网络安全 // No Comments

有的网站被恶意放上UDP发包工具攻击别人,导致流量大量流失,一般服务器只有DNS使用udp协议,其它则可禁用UDP数据包外出。
为此写了个脚本只允许目标DNS服务器的UDP数据包外出,其它UDP数据包全部拒绝,本方法仅能做到防止恶意UDP数据包发出,服务器本身做好安全设置防止被恶意放马才是王道。

#/bin/bash
#Createdby https://www.haiyun.me
#DROP UDP Flood
list=`grep nameserver /etc/resolv.conf |awk '{print $NF}'`
for i in $list
do
        iptables -A OUTPUT -p udp -d $i --dport 53 -j ACCEPT
done
iptables -A OUTPUT -p udp -j DROP
service iptables save

固定NFS启动端口便于iptables设置

发布时间:April 22, 2012 // 分类:Linux服务 // No Comments

NFS启动时会随机启动多个端口并向RPC注册,这样如果使用iptables对NFS端口进行限制就会有点麻烦,可以更改配置文件固定NFS服务相关端口。
先上张图看看NFS启动后的端口:
nfs-port.png
分配端口,编辑配置文件:

vi /etc/sysconfig/nfs 

添加:

#https://www.haiyun.me
RQUOTAD_PORT=30001
LOCKD_TCPPORT=30002
LOCKD_UDPPORT=30002
MOUNTD_PORT=30003
STATD_PORT=30004

重启portmap和nfs:

/etc/init.d/portmap restart
/etc/init.d/nfs restart

现在看看启动的端口:

rpcinfo -p localhost

2012-04-22_185346.png
iptables设置:

iptables -A INPUT -s 192.168.1.1 -p tcp --dport 111 -j ACCEPT
iptables -A INPUT -s 192.168.1.1 -p udp --dport 111 -j ACCEPT
iptables -A INPUT -s 192.168.1.1 -p tcp --dport 2049 -j ACCEPT
iptables -A INPUT -s 192.168.1.1 -p udp --dport 2049 -j ACCEPT
iptables -A INPUT -s 192.168.1.1 -p tcp --dport 30001:30004 -j ACCEPT
iptables -A INPUT -s 192.168.1.1 -p udp --dport 30001:30004 -j ACCEPT

Openwrt/Linux安装squid做透明代理缓存服务器

发布时间:April 21, 2012 // 分类:Squid,OpenWrt,Linux服务 // 3 Comments

安装Squid:

opkg update
opkg install squid
cd /etc/squid
mv squid.conf squid.conf.back
vim squid.conf

配置文件:

visible_hostname proxy.www.haiyun.me #主机名
cache_mgr onovps@www.haiyun.me #管理员邮箱
http_port 3128 transparent #监听端口3128,透明代理
icp_port 0 #单机模式
dns_nameservers 192.168.1.1 #DNS
#cache_effective_user squid #运行用户
#cache_effective_group squid #运行用户组
pid_filename /tmp/squid.pid #pid文件
error_directory /usr/share/squid/errors/Simplify_Chinese #错误提示文件
emulate_httpd_log on #开启httpd日记格式
#logformat log %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh #自定义日记模式
cache_access_log /mnt/log/access.log #日记路径
cache_log none #无cache日记
cache_store_log none #无store日记
cache_dir ufs /mnt/cache/ 2048 16 256 #硬盘缓存2G,一级目录16,二级目录256
cache_mem 16 MB #内存缓存16M
cache_swap_low 90
cache_swap_high 95
minimum_object_size 0 KB #最小缓存不限制
maximum_object_size 4096 KB #最大缓存4M
cache_vary on #开启vary缓存
connect_timeout 1 minute #连接超时1分
request_timeout 1 minutes #请求超时1分

acl QUERY urlpath_regex -i cgi-bin \?
cache deny QUERY #不缓存cgin-bin

acl bt url_regex -i ^http://.*\.torrent$
http_access deny bt    #禁止下载torrent
acl files urlpath_regex -i "/etc/squid/files.txt" #过滤下载文件后缀
acl sites dstdom_regex "/etc/squid/sites.txt"   #过滤特定网址
acl keys url_regex -i "/etc/squid/keys.txt"  #过滤特定关键词
acl nocache_sites dstdom_regex "/etc/squid/nocache_sites.txt" #指定不缓存网址
acl nocache_files urlpath_regex -i "/etc/squid/nocache_files.txt" #指定不缓存文件后缀
http_access deny files             
http_access deny sites
http_access deny keys   
cache deny nocache_sites
cache deny nocache_files

acl all src 0.0.0.0/0.0.0.0 
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl localnet src 192.168.1.0/255.255.255.0
acl SSL_ports port 443 563 10000
acl Safe_ports port 80 21 443 56370 210 1025-65535 280 488 591

http_access allow manager localhost
http_access deny manager
http_access allow localnet
http_access deny all

#acl apache rep_header Server ^Apache
broken_vary_encoding allow all #开启压缩
header_access X-Forwarded-For deny all #禁止 X-Forwarded头
header_access HTTP_VIA deny all   #禁止HTTP_VIA
header_access Via deny all #禁止Via头
refresh_pattern -i \.css$ 1440 50% 129600 reload-into-ims 
refresh_pattern -i \.xml$ 1440 50% 129600 reload-into-ims
refresh_pattern -i \.htm$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.html$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.shtml$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.png$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.jpg$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.jpeg$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.gif$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.bmp$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.js$ 1440 90% 129600 reload-into-ims

refresh_pattern -i \.mp3$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.wmv$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.rm$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.swf$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.mpeg$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.wma$ 1440 50% 2880 ignore-reload

refresh_pattern -i \.exe$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.rar$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.zip$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.gz$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.bz2$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.7z$ 1440 50% 2880 ignore-reload

squid相关指令:

squid -k reconfigure #重置
squid -k parse  #检测配置文件
squid -k shutdown #关闭
squid -k rotate  #分割日记

Openwrt下安装squid后无init脚本,管理不方便,自己简单写了个。

#/bin/bash
#Create by www.haiyun.me
case $1 in
stop)
  squid -k shutdown
  ;;
start)
  squid
  ;;
restart)
  squid -k reconfigure
  ;;
check)
  squid -k parse
  ;;
*)
  echo "Please use restart|start|stop|check"
  ;;
esac

iptables配置:

opkg install iptables-utils iptables-mod-nat-extra
iptables -t nat -A PREROUTING -i br-lan -p tcp  --dport 80 -j REDIRECT --to-ports 3128

OpenWrt路由器iptables防火墙设置

发布时间:April 18, 2012 // 分类:OpenWrt // No Comments

OpenWrt自带防火墙有点复杂,自己根据需要重新配置了下iptables,可满足一般用户需求。

#/bin/bash
#OpenWrt防火墙,适用于RG100AA
#Cretaed by www.haiyun.me
iptables -F
iptables -X
iptables -Z
iptables -P INPUT   DROP
iptables -P OUTPUT  ACCEPT
iptables -P FORWARD DROP
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i br-lan -j ACCEPT
#iptables -A INPUT -m string --algo bm --string "sex" -j DROP
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#iptables -A FORWARD -m string --algo bm --string "sex" -j DROP
iptables -A FORWARD -i br-lan -o pppoe-wan  -j ACCEPT
iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
#iptables -A FORWARD -p tcp --dport 12488 -j ACCEPT
#iptables -A FORWARD -p udp --dport 12488 -j ACCEPT
iptables -t nat -F
iptables -t nat -X
iptables -t nat -Z
iptables -t nat -P PREROUTING  ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT      ACCEPT
iptables -t nat -A POSTROUTING -i br-lan -o pppoe-wan -j MASQUERADE
#iptables -t nat -A PREROUTING -p tcp  --dport 12488  -j DNAT --to-destination 192.168.1.6
#iptables -t nat -A PREROUTING -p udp  --dport 12488  -j DNAT --to-destination 192.168.1.6
#iptables -t mangle -F
#iptables -t mangle -X
#iptables -t mangle -Z
#iptables -t mangle -A PREROUTING  -i pppoe-wan -j TTL --ttl-inc 1
#iptables -t mangle -A POSTROUTING -o pppoe-wan -j TTL --ttl-set 128
#iptables -t mangle -A POSTROUTING -o pppoe-wan -j IPID --ipid-pace 1
#iptables -I FORWARD -p tcp --tcp-flags RST RST -j DROP 
分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...