您的位置:首页 > 运维架构 > Linux

linux-selinux安全防护,加密解密

2016-05-09 09:21 573 查看
Linux安全保护模型?
1 自主访问控制DAC (rwx facl)
所有者对自己的资源负责
dr-xr-xr-x. 29 root root 4096 5月 5 14:05 /

2 MAC,强制访问控制
管理员对所有的资源负责
TCSEC标准定义的 MLS 多级安全
SELinux *

[root@room5pc00 ~]# sestatus
SELinux status: disabled
[root@room5pc00 ~]#

[root@room5pc00 ~]# cat /etc/sysconfig/selinux
# 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 - SELinux is fully disabled.
SELINUX=enforcing
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted
[root@room5pc00 ~]# reboot

[root@svr5 ~]# sestatus selinux查询
SELinux status: enabled
Current mode: enforcing
.. ..
Policy from config file: targeted

方法一,修改kernel启动参数
添加 selinux=0 以禁用
添加 selinux=1 以启用
方法二,修改文件/etc/selinux/config
设置 SELINUX=disabled 以禁用
设置 SELINUX=permissive 宽松/允许模式
设置 SELINUX=enforcing 强制模式

[root@room5pc00 ~]# getenforce
Enforcing
[root@room5pc00 ~]# setenforce 0
[root@room5pc00 ~]# getenforce
Permissive
[root@room5pc00 ~]# setenforce 1
[root@room5pc00 ~]# getenforce
Enforcing
[root@room5pc00 ~]#
++++++++++++++++++++++++++++++++++++++++++++
查看文件上下文:

[root@room5pc00 ~]# ll -Z /etc/passwd
-rw-r--r--. root root system_u:object_r:etc_t:s0 /etc/passwd

[root@room5pc00 ~]# ll -dZ /etc
drwxr-xr-x. root root system_u:object_r:etc_t:s0 /etc

查看httpd服务上下文
ps aux -Z | grep httpd
unconfined_u:system_r:httpd_t:s0 apache 3975 0.0 0.0 186060 2524 ? S 10:42 0:00 /usr/sbin/httpd

unconfined_u:system_r:httpd_t:s0 apache 3976 0.0 0.0 186060 2508 ? S 10:42 0:00 /usr/sbin/httpd

[root@room5pc00 ~]# id -Z

查看文家夹上下文
ll -dZ /var/www/html/
system_u:object_r:httpd_sys_content_t:s0
用户 : 角色 :访问类型 :选项...

一般操作规律
移动的文件,原有的上下文属性不变
复制的文件,自动继承目标位置的上下文

修改安全上下问 chcon
-t,指定访问类型
-R,递归修改

[root@room5pc00 ~]# ll -Z /var/www/html/two.html
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /var/www/html/two.html
[root@room5pc00 ~]#
[root@room5pc00 ~]#
[root@room5pc00 ~]# ll -Z /var/www/html/one.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/one.html
[root@room5pc00 ~]# chcon -t httpd_sys_content_t /var/www/html/two.html
[root@room5pc00 ~]# ll -Z /var/www/html/two.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/two.html
[root@room5pc00 ~]#

chcon -R -t httpd_sys_content_t /var/www/html

使用 restorecon 工具
恢复为所在位置的默认上下文属性
-R,递归修改

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SELinux布尔值
getsebool -a
getsebool -a | grep ftp

[root@room5pc00 ~]#
setsebool -P allow_ftpd_anon_write=1
setsebool -P allow_ftpd_full_access on
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
安装排错工具
yum -y install setroubleshoot-server setroubleshoot-plugins setroubleshoot-doc setroubleshoot

reboot

tail -f /var/log/messages (按提示操作)

May 7 11:53:04 room5pc00 setroubleshoot: SELinux is preventing /usr/sbin/vsftpd from write access on the directory stuput. For complete SELinux messages. run sealert -l ac0211d2-a4fc-4051-8157-a9ca0f771570
按提示执行run以后的指令 #sealert -l ac0211d2-a4fc-4051-8157-a9ca0f771570

#sealert -b 调出图形查错菜单
++++++++++++++++++++++++++++++++++++
为什么要给数据加密?
加密、解密

算法?

单向加密(不可逆)MD5 SHA
源数据不发生改变 加密的结果不变
生成固定长度(比如128位)的摘要文本

使用md5sum校验工具
生成MD5校验值
与软件官方提供的校验值比对

710 head -1 /etc/passwd > a.txt
711 cat a.txt
712 md5sum a.txt
713 cp a.txt b.txt
714 md5sum b.txt
715 vim b.txt
716 md5sum b.txt
717 vim b.txt
718 md5sum b.txt

++++++++++++++++++++++++++++++++++++
对称加密(加密和解密使用相同的密码) DES AES

++++++++++++++++++++++++++++++++++++

GnuPG,GNU Privacy Guard 最流行的数据加密、数字签名工具软件

[root@room5pc00 ~]# which gpg
/usr/bin/gpg
[root@room5pc00 ~]# rpm -qf /usr/bin/gpg
gnupg2-2.0.14-8.el6.x86_64
[root@room5pc00 ~]#

gpg支持的算法:
公钥:RSA, ELG, DSA
对称加密:3DES, CAST5, BLOWFISH, AES, AES256, .. ..
散列:MD5, SHA1, .. .., SHA256, SHA512

ssh -X usera@IP
echo aaaaa > /tmp/usera.txt
cd /tmp/
gpg -c usera.txt (提示输入加密的密码)
ls usera.txt.gpg
rm -rf usera.txt

ssh -X userb@ip
cd /tmp
gpg -d usera.txt.gpg > userb.txt (提示输入解密的密码)

cat userb.txt
aaaaa

++++++++++++++++++++++++++++++++++++
非对称加密(公钥加密 私钥解密) RSA DSA

1 usera 用户生成自己的秘钥对(公钥 /私钥)gpg --gen-key
2 usera用户导出自己的公钥
gpg -a --export > /tmp/UserB.pub

3 userb用户导入公钥
gpg --import /tmp/UserB.pub

4 userb用户使用导入的公钥加密数据
echo abcdedf > /tmp/test.txt
cd /tmp
gpg -e -r panglj test.txt

ls test.txt.*

5 userb用户使用自己的私钥解密
gpg -d /tmp/test.txt.gpg > usera2.txt

userb:
gpg --gen-key

[userb@room5pc00 ~]$ gpg --gen-key
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

请选择您要使用的密钥种类:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (仅用于签名)
(4) RSA (仅用于签名)
您的选择?
RSA 密钥长度应在 1024 位与 4096 位之间。
您想要用多大的密钥尺寸?(2048)1024
您所要求的密钥尺寸是 1024 位
请设定这把密钥的有效期限。
0 = 密钥永不过期
<n> = 密钥在 n 天后过期
<n>w = 密钥在 n 周后过期
<n>m = 密钥在 n 月后过期
<n>y = 密钥在 n 年后过期
密钥的有效期限是?(0)
密钥永远不会过期
以上正确吗?(y/n)y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
"Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"

真实姓名:plj
姓名至少要有五个字符长
真实姓名:panglj
电子邮件地址:plj@tedu.cn
注释:teacher
您选定了这个用户标识:
“panglj (teacher) <plj@tedu.cn>”
更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q) O

ls ~/.gnupg/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: