对于iptables 的一点理解

这是无意中在stackoverflow上看到的,以前还真没有注意到.

Question:

I was trying to block an ip of ( network stresser tool – a ddos websites selling ddos bots by seconds online ) the first one i was trying to block with iptables :

iptables -A INPUT -d 173.193.26.73 -j DROP
iptables -A INPUT -s 173.193.26.73 -j DROP
however i still see the ip and bandwidth still up on iftop. also still see the ip going on tcpdump.

is that normal? what’s the problem in here?

 

Answer: 

Yes, it’s normal. iptables doesn’t stop inbound traffic turning up on your physical interface, thus notching up your iftop counts, and it doesn’t stop the kernel seeing that it’s there, hence the tcpdump output. It does, however, stop the kernel passing the traffic on to anything that might be listening for it.

If you see any output traffic in response to those input packets, something’s wrong. But otherwise, no, that’s normal.

iptables 工作在5个链上, prerouting, input, forward, output, postrouting

prerouting 和 postrouting 一般是在NAT上使用,所以traffic 会流经CPU,也即是说iptables 无法阻止真实网卡的流入流量, 因此itfop 和 tcpdump 都能看到流入的流量,但是在用iptables block了这个IP的情况下,是不会有流出的流量的.

 

简单的防DDos和CC attack 的一点补救措施

其实这些措施也基本没有什么大用,在代理IP 足够多的情况下只能依靠硬件去防守,但是聊胜于无

首先可以看下hivelocity 的一篇文章

https://www.hivelocity.net/kb/how-to-check-if-your-linux-server-is-under-ddos-attack/

1 安装CSF, 这个应该在我的所有服务器上都有安装

2 check the number of active connections sorted by asc

netstat -n | grep ':80' | awk -F' ' '{print $5}' | awk -F':' '{print$1}' | sort | uniq -c | sort -n

By Desc:

netstat -n | grep ':80' | awk -F' ' '{print $5}' | awk -F':' '{print$1}' | sort | uniq -c | sort -n -r

3 install iftop

yum install epel-release -y

yum install iftop -y

iftop -n

 

4 fail2ban 在一定情况下还是有作用的

5 Check average single php-fpm instance memory usage:

ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'

Online.net Xenserver 使用RPN-SAN的诸多问题

Online.net 的系统本身就有很多问题,加上XenServer 以后问题就更多了,再加上RPN – SAN 以后,那问题就更更更多了。。。

为什么要是用RPN-SAN? 

因为online.net 的服务器很多都是纯SSD 服务器,如果我们需要放置大量的图片的话,显然是不够的,这个时候RPN-SAN 就派上用场了, 在纯dedicated server上使用SAN,那是很容易的事情,直接挂载就可以了,在XenServer上挂载iSCSI 的 SAN,则稍微麻烦点, 有几点注意事项:

  1. XenServer 7 是建立在CentOS 7 上面的, iSCSI用的port 是3260, 所以需要在防火墙上打开3260
  2. Online.net 的RPN-SAN 用的是内网,位于 10.88.0.0/14. 如果你的online.net 的服务器是用的IPMI 安装的系统,那么你需要把这台服务器自带的Private IP 配置上. 关于private ip 的 netmask,和gateway 可以先用DHCP 激活,然后自己记录下来,稍后你可以一直使用DHCP,或者按照自己的意愿使用static
  3. 在购买完了Online.net 的 RPN-SAN以后,你需要在此RPN-SAN的管理页面,给使用此RPN-SAN 的dedicated server授权,这是从security 的角度考虑
  4. 于此同时,你还需要在RPN Group里面,把RPN-SAN 和使用此SAN 的服务器放置于同一个Group里面
  5. 一般来说,RPN-SAN 和 服务器的Private IP 需要在一个网段,我们这里可以使用10.88.0.0/14 或者10.90.0.0/15
  6. XenServer 需要手动添加一条面向private network 10.88.0.0/14 或者10.90.0.0/15的 route,这样是他们互相通信,为了方便,此文下面假设我们使用的是10.90.0.0/15这个网段
  7. XenServer 通过xapi 添加static route 的办法:
xe network-list
xe network-param-set uuid=<UUID> other-config:static-routes=10.90.0.0/15/10.90.23.1

在这命令里面,我们的网卡为xenbr2,IP 为10.90.23.44, Gateway 为10.90.23.1

运行完这条命令, 如果我们使用route -n 来查看route, 会发现没有任何变化, 这是因为我们需要重启系统或者运行xe-toolstack-restart 来使新的配置生效

添加static route 很简单,但是删除就比较麻烦了. 删除的话,需要一次性删除所有的static route,然后再添加我们认为必须的route.

删除所有static route:

xe network-param-remove uuid={network UUID} param-name=other-config param-key=static-routes

If there were some needed static route, add them back, for example:

xe network-param-set uuid={network UUID} other-config:static-routes=192.168.98.0/24/192.168.1.1,192.168.99.0/24/192.168.1.1

 

After that, reboot to make them take effect.

对于storage来说,我们经常会把NIC 的MTU 改为9000, steps to modify MTU in Senserver:

  1. shutdown guests
  2. reconfigure network

xe network-param-set uuid=<network-uuid> MTU=9000

      3. reboot hosts

       4. verify proper MTU sizing

discuz x3.2 启动SSL

这是一篇别的地方看到的文章,把所有需要修改的地方都说了,很有借鉴意义,这里就转载过来做个记录

————————————————————————————————————————————–

Discuz! X3.2 本身对 HTTPS 的支持很有限,程序本身支持,但还有很多地方需要调整和修改的。
站点全部链接默认 HTTPS ,无任何 HTTP 链接通过 301 跳转到 HTTPS 。 浏览器显示绿色安全标志,无“不安全内容”提示。

具体来说,我们需要从以下 6 个方面来实现我们最终的效果。

服务器前端程序的配置
一般来说,大家多使用 Nginx 作为前端程序,关于 Nginx 开启 HTTPS 的教程,网上有很多教程,可以参考这篇文章:

https://aotu.io/notes/2016/08/16/nginx-https/
Discuz 判断服务器是否使用 SSL 的修补和完善
 Discuz 判断服务器是否使用 SSL 的代码不适合 Nginx+CGI 的情况,即 PHP-FPM 。这个时候我们需要修改以下文件:
 Discuz 采用 $_SERVER[‘ HTTPS ’] 的方式来判断 SSL ,但是因为服务器 架构问题( nginx+php-fpm ),无法采用这种方式识别,所以需要对 Discuz 程序进行一些调整(使用 $_SERVER[‘ SERVER_PORT ’] 来判断)。 source/class/discuz/discuz_application.php (约第 187 行处): 查找:
 $_G['isHTTPS'] = ($_SERVER['HTTPS'] && strtolower($_SERVER['HTTPS']) != 'off') ? true : false;
 修改为:
 $_G['isHTTPS'] = ($_SERVER['SERVER_PORT'] == 443 || $_SERVER['HTTPS'] && strtolower($_SERVER['HTTPS']) != 'off') ? true : false;
uc_server/avatar.php (约第 13 行处):
 查找:
 define('UC_API', strtolower(($_SERVER['HTTPS'] == 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'))));
 修改为:
 define('UC_API', strtolower(($_SERVER['SERVER_PORT'] == 443

除去非 HTTPS 内容避免提示”不安全内容“
Source/plugin/manyou/Service/DiscuzTips.php ,最后的那段 JS 加载脚本删除就行。
非楼主层如果有点评,那么点评者头像不是 HTTPS 开头,也需要修改一个文件来适配: template/default/forum/viewthread_node_body.htm (约 180 行),搜索 div class=”psta vm”>,将下面一行注释掉或者删除。
打开浏览器,使用开发者工具或者查看源码逐一排查加载的非 HTTPS 资源并修改。

后台设置的修改完善
在后台还有一些设置,可能会干扰 https 的使用
后台 >全局 > 站点 URL ,改为 https 开头的
后台 > 站长 > UCenter 设置 > UCenter 访问地址,修改为 https 开头的
UCenter 后台 > 应用管理 > 应用的主 URL ,修改为 https 开头。修改后可能会显示通讯失败,如果 UC 和论坛程序安装在同一机器,此失败可无视,实测可以和 UC 正常通讯不影响(测试是否正常通讯程序的 Bug ),如果 UC 和论坛程序不在一台机器上,有可能不能通讯。 另外在 后台 > 全局 > 域名设置 中的一些设置也可能使 https 失效,如果更新缓存后论坛默认连接还是 HTTP ,请删除 后台 > 全局 > 域名设置 > 应用域名 > 默认 里面的默认域名(一般去 forum.php 尾巴这里会有内容,为了 HTTPS 请删除)。

模板的调整
主要在模板的 foot.html 以及 header.html 等文件中,使用工具逐一排查模板文件中写死的 HTTP 链接,修改为 HTTPS 。

数据库的调整
在论坛这种交互社区中,经常回复发帖时会有出现主域名的链接,在没有 HTTPS 之前,链接都是 HTTP 开头,这个时候,我们需要修改数据库,运行下面的 mysql 命令更新数据库,将 HTTP 替换为 HTTPS :
进入 DZ 后台:站长 – 数据库 – 升级

UPDATE pre_forum_post SET message=REPLACE(message,’http://www.repaik.com‘,’https://www.repaik.com‘);
请将 www.repaik.com 替换为自己的域名

需要注意的是,出于安全考虑, Discuz 后台默认情况下禁止 SQL 语句直接执行,只能使用常用 SQL 当中的内容,如果想自己随意书写 SQL 升级语句,需要将程序文件 config/config_global.php 当中的$_config[admincp][runquery] 设置修改为 1 。

阿里云ECS这是个大坑

从来不用国内的服务,但是没有办法,为了速度还是只能在国内找,矮子里挑个高的,只能选择阿里云。。。但是随之而来的是各种大坑。。

ECS 封掉了25端口,也就是说你不能在上面创建任何邮件服务器。。。阿里云工单的回答是,只能使用阿里云云邮箱或者第三方,需要修改代码。。。

修改你妹妹啊。。我是直接用bash 调用postfix做发件服务,没有代码。。。

找了一晚上加一上午,目前最好的办法就是使用qq 的免费企业邮箱。。但是qq 比较鬼的是。。他在文档中没有标明有587端口。。但是你确可以使用这个端口。。。465 是ssl 端口,这样配置起来会比较麻烦。。

首先是创建qq的免费企业邮箱,得到smtp 信息

smtp:  smtp.exmail.qq.com:587

TLS

然后在centos 里面安装postfix 和 sasl 模块

sudo yum install postfix cyrus-sasl cyrus-sasl-lib cyrus-sasl-plain -y

编辑 /etc/postfix/main.cf, 然后加上

relayhost = [smtp.exmail.qq.com]:587

smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
smtp_sender_dependent_authentication = yes
smtp_generic_maps = hash:/etc/postfix/generic

在/etc/postfix/sasl_passwd 里面写用户名和密码, 比如说用户名为 [email protected], 密码为iamhippo, 那么就这么写:

[smtp.exmail.qq.com]:587 [email protected]:iamhippo

在/etc/postfix/generic 要重写mail from 的地址,假如你的hostname 是i.iamhippo.com, 那么就可以写成:

@i.iamhippo.com [email protected]

 

最后就是生成hash 数据库了:

postmap /etc/postfix/sasl_passwd
postmap /etc/postfix/generic
chmod 600 /etc/postfix/sasl_passwd*

然后我们只需要重启postfix,就大功告成

service postfix restart

/etc/fstab 详细说明

/etc/fstab 是专门用配置挂载硬盘的文件

语法为:

[Device] [Mount Point] [File System Type] [Options] [Dump] [Pass]

详细解释为:

<device>

The device/partition (by /dev location or UUID) that contain a file system.

<mount point>

The directory on your root file system (aka mount point) from which it will be possible to access the content of the device/partition (note: swap has no mount point). Mount points should not have spaces in the names.

<file system type>

Type of file system 

<options>

Mount options of access to the device/partition (see the man page for mount).

<dump>

Enable or disable backing up of the device/partition (the command dump). This field is usually set to 0, which disables it.

<pass num>

Controls the order in which fsck checks the device/partition for errors at boot time. The root device should be 1. Other partitions should be 2, or 0 to disable checking.

第一列和第二列的device 和 mount point 顾名思义,就是你想要挂载的device  和挂载的位置.

device 有两种表示方式,可以用/dev/xdx 之类的location 或者 硬件的UUID 来表示,硬件的UUID 可以用blkid 来查询

第三列的file system type 也很好理解,这里一般有auto, vfat( for FAT partition), ntfs or ntfs-3g( for NTFS partition), ext4 or ext3 or ext2 or jfs,  udf or iso9660 ( for CD/DVD), swap

第四列的option,一般用默认的defaults,但是也可以使用下面的option:

sync/async - All I/O to the file system should be done (a)synchronously.
auto - The filesystem can be mounted automatically (at bootup, or when mount is passed the -a option). This is really unnecessary as this is the default action of mount -a anyway.
noauto - The filesystem will NOT be automatically mounted at startup, or when mount passed -a. You must explicitly mount the filesystem.
dev/nodev - Interpret/Do not interpret character or block special devices on the file system.
exec / noexec - Permit/Prevent the execution of binaries from the filesystem.
suid/nosuid - Permit/Block the operation of suid, and sgid bits.
ro - Mount read-only.
rw - Mount read-write.
user - Permit any user to mount the filesystem. This automatically implies noexec, nosuid,nodev unless overridden.
nouser - Only permit root to mount the filesystem. This is also a default setting.
defaults - Use default settings. Equivalent to rw, suid, dev, exec, auto, nouser, async.
_netdev - this is a network device, mount it after bringing up the network. Only valid with fstype nfs.

第五列 dump,dump 是linux 系统中的一个备份工具,用0 来表示不备份这个区,1 表示备份。这里不适合长篇大论来讨论dump 和 fsck ,但是通过查看/etc/fstab 的例子可以看到,第五列的选项一般为0

第六列 fsck 表示fsck 是否会check这个区,一般用1 来表示根分区, 2 表示其他的分区;0表示不检查

对于第5,6列来说,

/ 一般为1 1

swap 一般为 0 0

其他分区一般为1 2

云硬盘可谓0 2

 

CentOS 开启NFS服务以及在Centos上挂在NFS

NFS 服务器这里我称之为Master服务器,IP为192.168.1.2,需要挂载NFS服务器的我称之为client,client服务器的IP为192.168.1.13

A服务器安装CentOS 6.X 64bit,首先需要安装NFS服务并且开启这个服务

yum install nfs-utils nfs-utils-lib

chkconfig nfs on 
service rpcbind start
service nfs start

其次,我们需要在Master服务器上决定我们要共享哪个文件夹,然后把文件夹目录以及符合共享,写入/etc/exports

假如说我们想共享/home目录

那么我们首先需要编辑/etc/exports

vi /etc/exports

加入以下命令:

/home 192.168.1.13(rw,sync,no_root_squash,no_subtree_check)

rw:表示client主机可以对共享的文件夹进行读和写的操作

sync: Sync confirms requests to the shared directory only once the changes have been committed.

编辑完/etc/exports以后,我们需要运行以下command来让配置生效:

exportfs -a

这样我们就完成master服务器的配置

下面我们来配置client服务器,安装nfs服务

yum install nfs-utils nfs-utils-lib

假如说我们需要把NFS挂载到/home/nfs

mkdir -p /home/nfs

mount 192.168.1.2:/home /home/nfs

这样我们就完成了挂载,可以通过df -h或者mount 来查看系统的挂载情况

但是这种挂载只是临时性的,每次我们重启,就需要重新运行一次挂载命令。为了省事,我们可以将挂载写入/etc/fstab

192.168.1.2:/home /home/nfs nfs auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0