分类 阿里云 下的文章

公网服务器安装routeros后的安全设置

最近很多朋友尝试在vps或者云服务器上安装ros,由于服务器是公网ip,安全风险很大,在这里写一下安装routeros后的安全设置。如何在云服务器上安装routeros,可以参考 Google Cloud Platform(gcp)谷歌云安装Routeros青云主机安装routeros,附一键安装routeros脚本

routeros安装完成首次启动是没有密码的,并且所有管理端口是开放的,也就是说,任何人只要先于你设置了密码,你就失去了控制权。

如果你是公有云(如阿里云,谷歌gcp等)的ecs上安装的话,先要设置安全组或者防火墙只允许自己宽带的公网ip访问,这样可以最大限度保证自己能在第一时间设置密码。如果是普通idc的vps的话,只能加快手速了。

1,使用webfig或者winbox登陆到routeros后,第一件事就是设置密码,路径是/webfig/#System:Password

2,关闭不需要的管理端口,路径是/webfig/#IP:Services,这里建议关闭除winbox和www的所有服务,并把开放的服务修改成其他端口号

3,修改管理员账号,/webfig/#System:Users,新建管理员,记得group选full取得所有权限,使用新管理员账号登陆routeros后禁用admin账号,避免输错管理员账号密码把自己关在外面

4,配置完其他后,记得在防火墙关闭公网dns查询,/webfig/#IP:Firewall.Filter_Rules.new,chain选input,protocol选udp,dst.port写53,in.interface选你的公网接口如ether1,最后action选drop。这样就能防止你的routeros被用来做ddos的放大攻击工具攻击别人,防止云服务商或者idc因为你的攻击行为(尽管非你本意)关闭你的服务器。

大数据迁移,10tb青云elasticsearch数据迁移阿里云elasticsearch

总的来说elasticsearch数据迁移有两种方法:
1,使用elasticdump 按条迁移,优点是被迁移的index可以写入,迁移中产生的新数据会一并迁移,缺点是速度比较慢;
2,使用elasticsearch自带的快照功能,优点是快,速度取决于网络或者硬盘速度,缺点迁移过程中产生的新数据不会迁移。

这里以elasticsearch的快照为例讲一下青云elasticsearch上10tb数据迁移到阿里云的过程。

1,在青云生成有对象存储权限的access_key和secret_key,在青云对象存储新建bucket:es-trans
2,在青云elasticsearch上创建使用对象存储的快照仓库

curl -XPUT 'http://192.168.150.3:9200/_snapshot/backup/' -d' {"type": "s3","settings": {"endpoint": "s3.pek3b.qingstor.com","access_key": "***","secret_key": "***","bucket": "es-trans","compress":"true"}}'

3,创建需要迁移的index的快照
3.1正常创建快照

curl -XPUT 'http://192.168.150.3:9200/_snapshot/backup/20180505' -d '{"indices": "index1,index2"}'

3.2强制创建快照(有未分配的主分片时需要强制创建快照)

curl -XPUT 'http://192.168.150.3:9200/_snapshot/backup/20180505' -d '{"indices": "index1,index2","partial": "true"}'

4,在阿里云生成有对象存储权限的access_key_id和secret_access_key,在阿里云创建对象存储bucket:es-trans
5,使用阿里云对象存储迁移工具ossimport把青云对象存储bucket:es-trans迁移到阿里云的对象存储bucket:es-trans,参考:https://help.aliyun.com/document_detail/57053.html?spm=a2c4g.11186623.6.1209.51f4663eMbJcKo
6,阿里云elasticsearch创建使用对象存储的快照仓库

curl -XPUT 'http://elastic:***@es-cn-***.elasticsearch.aliyuncs.com:9200/_snapshot/backup/' -d' {"type": "oss","settings": {"endpoint": "http://oss-cn-beijing-internal.aliyuncs.com","access_key_id": "***","secret_access_key": "***","bucket": "es-trans","compress":"true"}}'

7,恢复快照到阿里云es
7.1快照中的index全部原样恢复

curl -XPUT 'http://elastic:***@es-cn-***.elasticsearch.aliyuncs.com:9200/_snapshot/backup/20180505/_restore'

7.2原样恢复快照中部分index

curl -XPUT 'http://elastic:***@es-cn-***.elasticsearch.aliyuncs.com:9200/_snapshot/backup/20180505/_restore' -d '{"indices":"index1"}'

7.3恢复到新名字的index,并不包含原index的别名

curl -XPUT 'http://elastic:***@es-cn-***.elasticsearch.aliyuncs.com:9200/_snapshot/backup/20180505/_restore' -d '{"indices":"index1","include_aliases": false,"rename_pattern": "index1","rename_replacement": "new_index1"}'

8,其他可能用到的命令
8.1删除多个无用index

curl -XDELETE 'http://192.168.150.3:9200/index1,index2'

8.2设置集群磁盘写入阈值为95%,默认值为85%

curl -XPUT 'http://elastic:***@es-cn-***.elasticsearch.aliyuncs.com:9200/_cluster/settings' -d '{"transient": {"cluster.routing.allocation.disk.watermark.low": "95%"}}'

8.3设置index副本为0个

curl -XPUT 'http://elastic:***@es-cn-***.elasticsearch.aliyuncs.com:9200/index1/_settings' -d '{"number_of_replicas": 0}'

9,本次迁移数据约10tb,其中青云es快照写入对象存储约1000mbps,青云对象存储到阿里云对象存储约400mbps,阿里云对象存储的快照写入阿里云es约700mbps,可一个分成多个任务,同步进行,本次迁移约2天完成,如果使用elsaticdump迁移至少8天才能完成。

阿里云云解析ddns的自动更新shell脚本

今天需要用到阿里云云解析做ddns,网上搜到到都是基于阿里云python sdk的脚本,这么一个简单功能没必要用python,找到一个openwrt的shell脚本,拿来改了一下运行环境,和环境变量,在x86下ubuntu 16.04测试通过。

原脚本地址https://github.com/h46incon/AliDDNSBash ,在此感谢

我修改过的脚本地址https://github.com/trepwq/aliyunddns

使用方法
安装依赖
首先需要一个shell

然后安装 bind-dig,curl,openssl-util。

修改脚本的setting代码段
其中DomainRecordId不清楚的话暂时不用修改,DNSServer修改为你在万网上使用的DNS服务器。 如:

AccessKeyId="MyID"
AccessKeySec="MySecret"
DomainRecordId="00000"
DomainRR="www"
DomainName="example.com"
DomainType="A"
DNSServer="dns9.hichina.com"

如果不清楚DomainRecordId的话,修改main函数,在里面调用describe_record,如:

main()
    {
    describe_record
    #update_record
    }

然后执行这个脚本。如果没问题的话,就能获取到域名的所有解析记录的列表了:

{"PageNumber":1,"TotalCount":1,"PageSize":1,"RequestId":"0000","DomainRecords":{"Record":[{"RR":"www","Status":"ENABLE","Value":"8.8.8.8","RecordId":"21332133","Type":"A","DomainName":"example.com","Locked":false,"Line":"default","TTL":"600"},]}}HttpCode:200

上面的结果中,RecordId为21332133。得到结果后再修改DomainRecordId为正确的值。

修改main函数:

main()
{
    #describe_record
    update_record
}

执行脚本即可。脚本会在本机IP地址和当前域名解析设置不同的时候调用API更新设置。

最后,在crontab里添加每分钟执行一次即可

crontab -e
* * * * * /root/ddns.sh >>/dev/null 2>&1

阿里云日志服务每天自动发送统计http 4xx 5xx数量

借助阿里云日志服务cli还可以干更多,比如统计每天http 4xx 和 5xx数量。
这个脚本重点在awk的使用,提取字段和拼接字段。

www4xx5xx.sh

#! /bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
ALIYUN_LOG_CLI_ACCESSID=
ALIYUN_LOG_CLI_ACCESSKEY=
ALIYUN_LOG_CLI_ENDPOINT=cn-beijing.log.aliyuncs.com
export ALIYUN_LOG_CLI_ACCESSID
export ALIYUN_LOG_CLI_ACCESSKEY
export ALIYUN_LOG_CLI_ENDPOINT


function portal4xx(){
    todaytime=$(date "+%Y-%m-%d %H:%M:%S %Z")
    yesterdaytime=$(date -d '1 days ago' "+%Y-%m-%d %H:%M:%S %Z")
    current4xx=$(aliyun log get_log_all --project="www" --logstore="www-nginx-access" --query="status>=400 and status<500| select status,count(*)  as count group by status ORDER BY status ASC" --from_time="$yesterdaytime" --to_time="$todaytime" --jmes-filter="join('\n', map(&to_string(@), @))" |awk -F "[\"]" '{print $10":"$12","$2":"$4}' |awk '{{printf"%s\\n",$0}}')
    curl -H "Content-type: application/json" -X POST -d '{"text": "last 24h 4xx stats:\n'"${current4xx}"'"}' https://example/incoming/xxx
}

function portal5xx(){
    todaytime=$(date "+%Y-%m-%d %H:%M:%S %Z")
    yesterdaytime=$(date -d '1 days ago' "+%Y-%m-%d %H:%M:%S %Z")
    current5xx=$(aliyun log get_log_all --project="www" --logstore="www-nginx-access" --query="status>=500 and status<600| select status,count(*)  as count group by status ORDER BY status ASC" --from_time="$yesterdaytime" --to_time="$todaytime" --jmes-filter="join('\n', map(&to_string(@), @))" |awk -F "[\"]" '{print $10":"$12","$2":"$4}' |awk '{{printf"%s\\n",$0}}')
    curl -H "Content-type: application/json" -X POST -d '{"text": "last 24h 5xx stats:\n'"${current5xx}"'"}' https://example.com/incoming/xxx
}
portal4xx
portal5xx

上面脚本中比较折腾人的是要把下面格式写成json格式发出去

"count":"2171" "status":"403"
"count":"3529" "status":"404"
"count":"283" "status":"499"

把上面格式写成json如下

{"text": "last 24h 5xx stats:\ncount:2171,status:403\ncount:3529,status:404\ncount:283,status:499"}

双引号去掉容易,难的是把多行间真实的换行替换成字符串\n,这里使用awk '{{printf"%s\n",$0}}'

阿里云负载均衡访问控制(黑名单)自动添加IP的方法(续)

https://www.willnet.net/index.php/archives/105/
上文使用了阿里云日志服务的告警,由于告警有一些局限,比如最长只能统计60分钟的日志,最短查询间隔5分钟等,现在使用阿里云日志服务的cli替代告警。
首先需要安装日志服务cli

pip install -U aliyun-log-cli

然后编写脚本
autoaddblacklist.sh

#! /bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
ALIYUN_LOG_CLI_ACCESSID=
ALIYUN_LOG_CLI_ACCESSKEY=
ALIYUN_LOG_CLI_ENDPOINT=cn-beijing.log.aliyuncs.com
export ALIYUN_LOG_CLI_ACCESSID ALIYUN_LOG_CLI_ACCESSKEY ALIYUN_LOG_CLI_ENDPOINT
function autoaddblacklist(){
    nowtime=$(date "+%Y-%m-%d %H:%M:%S %Z")
        last5minutestime=$(date -d '5 minutes ago' "+%Y-%m-%d %H:%M:%S %Z")
    searchresult=$(aliyun log get_log_all --project="www" --logstore="www-nginx-access" --query="* | select remote_addr,count(*)  as count group by remote_addr order by count desc limit 1" --from_time="$last5minutestime" --to_time="$nowtime" --jmes-filter="join('\n', map(&to_string(@), @))")
    lastip=$(cat /root/aliyun/tmp/lastip.txt)
    currentip=$(echo $searchresult | awk -F "[\"]" '{print $16}')
    count=$(echo $searchresult | awk -F "[\"]" '{print $4}')
    if [ "$count" -gt 1000 ] && [ "$currentip"x != "$lastip"x ] ;then
        echo $currentip >/root/aliyun/tmp/lastip.txt
        echo $currentip|xargs python /root/aliyun/scripts/addaclentry.py >> /var/log/aliyun-log-alarm/www/autoaddblacklist.log 2>&1
        curl -H "Content-type: application/json" -X POST -d '{"text": "found ip:'"${currentip}"' request: '"${count}"' over 1000 in 5min,added to the www blacklist."}' https://example.com/incoming/xxxxx-xxxx-xxxx
    else
        date "+%Y-%m-%d %H:%M:%S" >> /var/log/aliyun-log-alarm/www/autoaddblacklist.log && echo "nothing to do" >> /var/log/aliyun-log-alarm/www/autoaddblacklist.log
    fi
}

autoaddblacklist

按需求定时执行上面的脚本就行了