1、编写脚本selinux.sh,实现开启或禁用SELinux功能

[root@ansible_centos7 ~]# cat selinux.sh
#!/bin/bash
#
#************************************************************************
#Author: qiuhom
#QQ: 467697313
#mail: qiuhom467697313@qq.com
#Date: 2019-12-11
#FileName: selinux.sh
#URL: https://www.cnblogs.com/qiuhom-1874/
#Description:
#Copyright (C): 2019 All rights reserved
#************************************************************************
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
[ $UID -ne 0 ] && echo "this script must root run it" && exit 1
[ $# -ne 1 ] && echo "Usage:bash $0 <off|on>" && exit 2
if [ "$1" = "on" ];then
sed -i 's@^SELINUX=.*@SELINUX=enforcing@g' /etc/selinux/config
[ $? -eq 0 ] && action "selinux config on " /bin/true
/sbin/setenforce 1
elif [ "$1" = "off" ];then
sed -i 's@^SELINUX=.*@SELINUX=disabled@g' /etc/selinux/config
[ $? -eq 0 ] && action "selinux config off " /bin/true
/sbin/setenforce 0
else
echo "argv error , please input <on|off>"
exit 3
fi
[root@ansible_centos7 ~]#

  验证

[root@ansible_centos7 ~]# sh selinux.sh
Usage:bash selinux.sh <off|on>
[root@ansible_centos7 ~]# sh selinux.sh aa
argv error , please input <on|off>
[root@ansible_centos7 ~]# getenforce
Permissive
[root@ansible_centos7 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted [root@ansible_centos7 ~]# sh selinux.sh on
selinux config on [ OK ]
[root@ansible_centos7 ~]# getenforce
Enforcing
[root@ansible_centos7 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted [root@ansible_centos7 ~]# sh selinux.sh off
selinux config off [ OK ]
[root@ansible_centos7 ~]# getenforce
Permissive
[root@ansible_centos7 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted [root@ansible_centos7 ~]#

  说明:要想永久关闭selinux需要重启服务器,因为selinux是基于内核的一个模块,只有重启才能重新读取配置文件,临时关闭可以用setenforce 0来临时关闭,其实这种方法准确的说不是关闭selinux,是将selinux的状态切换成permissive状态,也就是说这种状态selinux只警告,并不实质上的管控linux上的资源。

2、统计/etc/fstab文件中每个文件系统类型出现的次数

[qiuhom@test ~]$ cat -A /etc/fstab|awk '!/^\$|#/{fstype[$3]++}END{print "fstype count";for(i in fstype){print i,fstype[i]}}'
fstype count
devpts 1
swap 1
sysfs 1
proc 1
tmpfs 1
iso9660 2
ext4 2
[qiuhom@test ~]$

  说明:以上命令核心思想就是利用awk数组来记录文件系统出现的次数,每出现相同的文件系统类型就将其计数加1,最后把统计的结果循环打印出来

3、提取出字符串Yd$C@M05MB%9&Bdh7dq+YVixp3vpw中的所有数字

方法一:利用grep过滤

[root@ansible_centos7 ~]# echo 'Yd$C@M05MB%9&Bdh7dq+YVixp3vpw'|grep -o '[0-9]'
0
5
9
7
3
[root@ansible_centos7 ~]#

方法二:利用awk过滤

[root@ansible_centos7 ~]# echo 'Yd$C@M05MB%9&Bdh7dq+YVixp3vpw'|awk -F "" '{for(i=1;i<=NF;i++){if($i ~ /[0-9]/){print $i}}}'
0
5
9
7
3
[root@ansible_centos7 ~]#

  说明:以上命令核心思想是循环字符串中的每一个字符,然后判断每个字符是否是数字,如果是数字就打印出来。其中-F是指定字段分割符,-F "" 表示字段分割符为空,即每一个字符都为一个字段

4、解决DOS攻击生产案例:根据web日志或者或者网络连接数,监控当某个IP 并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频 率每隔5分钟。防火墙命令为:iptables -A INPUT -s IP -j REJECT

第一步:写脚本过滤web访问日志,将访问日志中的ip统计出来,然后判断是否段时间连接达到100

[root@test ~]#cat dos.sh
#!/bin/bash
#
#************************************************************************
#Author: qiuhom
#QQ: 467697313
#mail: qiuhom467697313@qq.com
#Date: 2019-12-12
#FileName: dos.sh
#URL: https://www.cnblogs.com/qiuhom-1874/
#Description:
#Copyright (C): 2019 All rights reserved
#************************************************************************
ip=`cat /var/log/nginx/access.log|awk '{
cip[$1]++
}
END{
for(i in cip)
{
if(cip[i] >= 100){
print i
}
}
}'` iplist=`echo $ip |tr -s " " ","`
iptables -A INPUT -s $iplist -j REJECT
[ ! -e /log/bak ] && mkdir -p /log/bak
cat /var/log/nginx/access.log >> /log/bak/nginx_access.log.bak
> /var/log/nginx/access.log
[root@test ~]#

  说明:以上脚本的意思是去nginx的访问日志中统计客户端ip出现的次数,如果客户端的ip出现次数大于等于100 ,就将此ip记录到ip这个变量里,然后将变量ip用tr命令将空格替换成逗号,然后传给一个叫iplist的变量,然后把满足要求的ip统一添加到防火墙规则里进行禁用ip的访问。

第二步:制定计划任务每5分钟执行一次我们上面写的脚本

[root@test ~]#crontab -l
*/5 * * * * bash /root/dos.sh &> /dev/null

Linux安全加固及文本处理之awk实践的更多相关文章

  1. linux文本分析利器awk

    转 快速理解linux文本分析利器awk 原文链接 杜亦舒 性能与架构 awk是什么 如果工作中需要操作linux比较多,那么awk是非常值得学习的 awk是一个极其强大的文本分析工具,把文件逐行的读 ...

  2. Linux常用基本命令:三剑客命令之-awk基础用法

    awk是一个超级强大的文本格式化处理工具,他与grep, sed命令被成为linux 三剑客命令 三剑客命令的特点: grep:只要用来匹配和查找文本 sed: 编辑匹配到文本 awk: 格式化文本, ...

  3. 文本处理工具awk

    目录 gawk:模式扫描和处理语言 awk语言 awk工作原理 print awk变量 自定义变量 printf命令 awk操作符 awk的条件判断 awk的循环 awk的数组 awk的函数 awk调 ...

  4. linux的文件处理(匹配 正则表达式 egrep awk sed)和系统、核心数据备份

    文件处理 1.处理方式 匹配 正则表达式 egrep awk sed 2.文件中的处理字符 \n  新行符 换行 \t  制表符 tab键 缺省8个空格 \b  退格符 backspace键 退格键 ...

  5. Linux大神必备-文本编辑器

    导读 我们在 Linux 上不缺乏非常现代化的编辑软件,但是它们都是基于 GUI(图形界面)的编辑软件.正如你所了解的:Linux 真正的魅力在于命令行,当你正在用命令行工作时,你就需要一个可以在控制 ...

  6. linux安全加固浅谈

    难易程度:★★★阅读点:linux;python;web安全;文章作者:xiaoye文章来源:i春秋关键字:网络渗透技术 前言linux被越来越多的企业使用,因此掌握一些基本的linux安全加固是有必 ...

  7. Linux系统 vi/vim文本编辑器

    Linux系统 vi/vim文本编辑器 (一)Vim/Vi简介 (二)Vim/Vi工作模式 (三)Vim/Vi基本使用 (四)Vim/Vi应用技巧 (一)Vim/Vi简介 Vim/Vi是一个功能强大的 ...

  8. Linux的文本编辑和文本内容查看命令

    Linux的文本编辑和文本内容查看命令 1.文本编辑命令 vi:用于编辑文本文件,基本上可以分为三种模式,分别是一般模式.编辑模式.命令行模式.    一般模式:当编辑一个文件时,刚进入文件就是一般模 ...

  9. 详解Linux中的cat文本输出命令用法

    作系统 > LINUX >   详解Linux中的cat文本输出命令用法 Linux命令手册   发布时间:2016-01-14 14:14:35   作者:张映    我要评论   这篇 ...

随机推荐

  1. zhy2_rehat6_mysql02 - 5.7主从搭建.txt

    1.0------------锁库: mysql>FLUSH TABLES WITH READ LOCK; 这个命令是全局读锁定,执行了命令之后所有库所有表都被锁定只读.一般都是用在数据库联机备 ...

  2. 017.Kubernetes二进制部署检查集群

    一 验证集群功能 1.1 检查节点状态 [root@k8smaster01 ~]# kubectl get nodes 1.2 创建测试文件 [root@k8smaster01 ~]# cd /opt ...

  3. ABC135记录

    date: 2019-07-28 A - Harmony 题目大意: 给你两个不同的整数A和B,要求你找到一个整数K,同时满足|A-K|=|B-K|.找不到时,输出"IMPOSSIBLE&q ...

  4. 09-Node.js学习笔记-异步编程

    同步API,异步API 同步API:只有当前API执行完成后,才能继续执行下一个API console.log('before'); console.log('after'); 异步API:当前API ...

  5. 【使用篇二】SpringBoot使用Druid作为数据源(19)

    Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JDBC兼容数据库,包括了Oracle.MySQL.PostgreSQL.SQL Server.H2等.Druid在监 ...

  6. 权限控制终于来了!Redis 6.0新特性——ACLs

    在2019年纽约的Redis Day上,Salvatore Sanfilippo(AKA Antirez)介绍了即将发布的Redis 6.0的新特性.以下是关于ACLs的内容. ACLs简介 在过去的 ...

  7. IT兄弟连 HTML5教程 CSS3揭秘 CSS选择器2

    4  结构性伪类选择器 在学习结构性伪类选择器之前,先了解两个概念:伪类选择器和伪元素选择器.伪类选择器是CSS中已经定义好的选择器,不能随便命名.常用的伪类选择器是使用在a元素上的几种,如a:lin ...

  8. 解决python安装第三方库超时问题

    这里说明一下,配置文件中的url还可以换成下面的URL 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.u ...

  9. springaop\ cglib\ AspectJ

    元编程 vs 动态代理 vs isa代理 springaop的底层实现有两种,一种是jdk的动态代理,另一种是cglib,springaop没有用到aspectj,只是借鉴了它并添加了aspectj风 ...

  10. Netty服务端Channel注册Selector及绑定服务器端口

    当服务端Channel 创建并且初始化完成之后,会将其注册到 selector,通过语句config().group().register(channel)进行注册工作,该方法最终调用 Abstrac ...