selinuxexample

上传人:汽*** 文档编号:575196074 上传时间:2024-08-17 格式:PPT 页数:43 大小:527.50KB
返回 下载 相关 举报
selinuxexample_第1页
第1页 / 共43页
selinuxexample_第2页
第2页 / 共43页
selinuxexample_第3页
第3页 / 共43页
selinuxexample_第4页
第4页 / 共43页
selinuxexample_第5页
第5页 / 共43页
点击查看更多>>
资源描述

《selinuxexample》由会员分享,可在线阅读,更多相关《selinuxexample(43页珍藏版)》请在金锄头文库上搜索。

1、Selinux 下匿名FTP的使用1,确认已经启用了Selinux:rootsgzhang # getenforce Enforcing2,启动:rootsgzhang # ps -efZ |grep vsftpdroot:system_r: root 12636 1 0 20:13 ? 00:00:00 /usr/sbin/vsftpd /etc/vs1Selinux 下匿名FTP的使用1, 在匿名访问目录下创建2个文件进行测试,一个是在该目录下手动创建,这样该文件会自动继承/var/下的目录上下文的值,一个用mv命令从root目录下移动过来,这样的文件会保留root目录下的安全上下文,如下

2、rootsgzhang pub# pwd/var/rootsgzhang pub# echo just a test test.txtrootsgzhang pub# chmod 755 test.txt rootsgzhang pub# ls -Z-rwxr-xr-x root root root:object_r:public_content_t:s0 test.txt2Selinux 下匿名FTP的使用1, 在匿名访问目录下创建2个文件进行测试,一个是在该目录下手动创建,这样该文件会自动继承/var/下的目录上下文的值,一个用mv命令从root目录下移动过来,这样的文件会保留root目录

3、下的安全上下文,如下rootsgzhang # pwd/rootrootsgzhang # echo aaa123 root.txtrootsgzhang # chmod 755 /root/root.txt rootsgzhang # mv root.txt /var/rootsgzhang # ls -Z /var/-rw-r-xr-x root root root:object_r:user_home_t:s0 root.txt-rwxr-xr-x root root root:object_r:public_content_t:s0 test.txt3Selinux 下匿名FTP的使用

4、使用匿名登录测试:rootsgzhang pub# ll cd pubcd ok, cwd=/publ ls-rwxr-xr-x 1 0 0 12 Aug 23 12:19 test.txt-rwxr-xr-x 1 0 0 910974 Aug 04 02:19 yuml 发现这里看不到root.txt文件4Selinux 下匿名FTP的使用已知系统启动了Selinux,先查看系统日志,有两个工具可以收集到Selinux产生的日志,一个是setroubleshoot,对应的软件包为setroubleshoot-server-2.0.5-5.el5一个是audit,对应的软件包名称是audit-

5、1.7.13-2.el5,先使用audit工具,使用方法如下:系统中提供了audit相关的命令,常用的有audit2why和audit2allow,audit产生的日志放在/var/log/audit, 由于此文件记录的信息很多不宜直接查看,可以借助audit2why命令,首先启动audit deamonrootsgzhang audit# /etc/init.d/auditd statusauditd is stoppedrootsgzhang audit# /etc/init.d/auditd startStarting auditd: OK rootsgzhang audit# /etc

6、/init.d/auditd statusauditd (pid 4013) is running.5Selinux 下匿名FTP的使用在客户端登录FTP服务器时会出发audit deamon产生日志:rootsgzhang audit# audit2why /var/log/audit/audit.log type=AVC msg=audit(1282568240.414:268): avc: denied getattr for pid=4061comm=vsftpd path=/pub/root.txt dev=sda1 ino=3634111 scontext=root:system_

7、r: tcontext=root:object_r:user_home_t:s0 tclass=fileWas caused by:Missing or disabled TE allow rule.Allow rules may exist but be disabled by boolean settings; check boolean settings.You can see the necessary allow rules by running audit2allow with this audit message as input.AVC 是 access vector cach

8、e 的縮寫, 目的是記錄所有與 SELinux 有關的存取統計資料。 6Selinux 下匿名FTP的使用根据日志中的建议,使用audit2allow命令查看给出的建议如下:rootsgzhang audit# audit2allow offallow_ - offallow_ - offallow_ - offallow_t - off - off - off - off - onhttpd_enable_ - offt - off发现 - off,文件root.txt 的类型刚好是root:object_r:user_home_t:s0所以更改此bool值就可以8Selinux 下匿名FT

9、P的使用重新设置该bool值:rootsgzhang audit# setsebool -P 1(-P是把该修改写到文件,下次启动仍然有效)rootsgzhang audit# getsebool - on客户端登录测试:rootsgzhang audit# ll cd pubcd ok, cwd=/publ ls-rwxr-xr-x 1 0 0 7 Aug 23 12:35 root.txt-rwxr-xr-x 1 0 0 12 Aug 23 12:19 test.txt-rwxr-xr-x 1 0 0 910974 Aug 04 02:19 yum9Selinux 下匿名FTP的使用第二种

10、方法通过进程知道FTP服务启动后的主体名称是, 使用下面的方法可以知道这个主体可以访问什么样类型的客体。rootsgzhang audit# sesearch -a -s |head Found 8989 av rules: allow t unconfined_t : process sigchld ; allow t unconfined_t : fd use ; allow t syslogd_t : unix_stream_socket connectto ; allow t syslogd_t : unix_dgram_socket sendto ; allow t var_lib_

11、t : dir ioctl read getattr lock search ; allow t var_run_t : dir ioctl read write getattr lock add_name remove_name search ; allow t winbind_t : unix_stream_socket connectto ; allow t t : file read getattr ; allow t t : dir read getattr search ; 可以看出一个定义了8989条策略,上面是其中的小部分。10Selinux 下匿名FTP的使用第二种方法既然/

12、var/可以访问,那么策略里肯定是allow的,且/var/的安全上下文如下:-rwxr-xr-x root root root:object_r:public_content_t:s0 /var/通过上面的命令验证一下策略集中是否有该定义rootsgzhang audit# sesearch -a -s -t public_content_t | head 4Found 14 av rules: allow public_content_t : file ioctl read getattr lock ; allow public_content_t : dir ioctl read get

13、attr lock search ; allow public_content_t : lnk_file read getattr ; 11Selinux 下匿名FTP的使用第二种方法那么根据这个思路可以更改/var/的安全上下文即可,可用chcon命令先对刚才的改变进行还原:rootsgzhang audit# setsebool -P 0rootsgzhang audit# getsebool - offrootsgzhang audit# ls /var/ -Z -rwxr-xr-x root root root:object_r:user_home_t:s0 /var/rootsgzh

14、ang audit# chcon -t public_content_t /var/ rootsgzhang audit# ls /var/ -Z -rwxr-xr-x root root root:object_r:public_content_t:s0 /var/rootsgzhang audit# ll ls pub/root.txt -rwxr-xr-x 1 0 0 7 Aug 23 12:35 root.txt 12Selinux 下匿名FTP的使用第二种方法另外在系统启用了SElinux伪系统后的文件都有默认的安全上下文,既然在/var/下创建的文件可以自动继承,那么下面其他不同安

15、全上下文的文件如果也继承了,那么就达到了目的,使用semanage 可以查看目录或文件的默认定义的安全上下文rootsgzhang pub# semanage fcontext -l |grep /var/ftp/var/ftp(/.*)? all files system_u:object_r:public_content_t:s0 /var/(/.*)? all files system_u:object_r:bin_t:s0 /var/(/.*)? all files system_u:object_r:etc_t:s0 /var/(64)?(/.*)? all files system

16、_u:object_r:lib_t:s0 /var/(64)?/ld/*.so(./*)* regular file system_u:object_r:ld_so_t:s0 /var/(64)?/lib/*.so(./*)* regular file system_u:object_r:shlib_t:s0 /var/ regular file system_u:object_r:ls_exec_t:s0 13Selinux 下匿名FTP的使用第二种方法在体统中默认目录和文件默认的安全上下文的定义文件放在:rootsgzhang files# cat /etc/selinux/targete

17、d/contexts/ |grep /var/ftp/var/ftp(/.*)?system_u:object_r:public_content_t:s0/var/(/.*)? system_u:object_r:bin_t:s0/var/(/.*)? system_u:object_r:etc_t:s0/var/(64)?(/.*)?system_u:object_r:lib_t:s0/var/(64)?/ld/*.so(./*)*-system_u:object_r:ld_so_t:s0/var/(64)?/lib/*.so(./*)*-system_u:object_r:shlib_t:

18、s0/var/-system_u:object_r:ls_exec_t:s0 14Selinux 下匿名FTP的使用第二种方法使用restorecon恢复系统定义的安全上下文rootsgzhang pub# ls -Z -rwxr-xr-x root root root:object_r:user_home_t:s0 root.txt-rwxr-xr-x root root root:object_r:public_content_t:s0 test.txtrootsgzhang pub# restorecon -Rv /var/ftp/restorecon reset /var/ conte

19、xt root:object_r:user_home_t:s0-system_u:object_r:public_content_t:s0 (其中R是递归,v是把操作显示在屏幕上)rootsgzhang pub# ls -Z -rwxr-xr-x root root system_u:object_r:public_content_t:s0 root.txt-rwxr-xr-x root root root:object_r:public_content_t:s0 test.txt rootsgzhang files# cat /etc/selinux/targeted/contexts/ |

20、grep /var/ftp/var/ftp(/.*)?system_u:object_r:public_content_t:s015Selinux 下匿名FTP的使用第二种方法另外 使用chcon的另外一个参数 -reference也是可以的rootsgzhang pub# ls -Z-rwxr-xr-x root root system_u:object_r:user_home_t:s0 root.txt-rwxr-xr-x root root root:object_r:public_content_t:s0 test.txtrootsgzhang pub# chcon -referenc

21、e /var/ /var/ rootsgzhang pub# ls -Z-rwxr-xr-x root root root:object_r:public_content_t:s0 root.txt-rwxr-xr-x root root root:object_r:public_content_t:s0 test.txt使用reference复制一份源( /var/ )的安全上下文给目的( /var/ )16Selinux 基于用户的FTP创建一个帐号用于测试:rootsgzhang pub# useradd -d /zsgd -m zsgdrootsgzhang pub# passwd z

22、sgd (密码是aaa123)Changing password for user zsgd.New UNIX password: BAD PASSWORD: it does not contain enough DIFFERENT charactersRetype new UNIX password: passwd: all authentication tokens updated successfully.17Selinux 基于用户的FTP用创建的帐号登录FTP服务器,登录是正常的,但ls等操作异常,如下:rootsgzhang pub# lftp -u zsgd localhostP

23、assword: l ls ls: Login failed: 500 OOPS: cannot change directory:/zsgd这是使用另外一个收集日志的工具setroubleshoot进行排查,首先启动setroubleshoot的deamon:rootsgzhang init.d# /etc/init.d/setroubleshoot statussetroubleshootd is stoppedrootsgzhang init.d# /etc/init.d/setroubleshoot startStarting setroubleshootd: OK rootsgzha

24、ng init.d# /etc/init.d/setroubleshoot statussetroubleshootd (pid 6021) is running.18Selinux 基于用户的FTP使用zsgd用户登录,以便于产生日志记录rootsgzhang init.d# l zsgd localhostPassword: l ls ls: Login failed: 500 OOPS: cannot change directory:/zsgd产生日志如下:rootsgzhang # tail -f /var/log/messages |grep setroubleshootAug 2

25、3 22:17:50 sgzhang setroubleshoot: SELinux is preventing access to files with the default label, default_t. For complete SELinux messages. run sealert -l ba5ee7cd-2d06-4dc7-9ee8-364332d90eb219Selinux 基于用户的FTP日志中提示使用sealert -l ba5ee7cd-2d06-4dc7-9ee8-364332d90eb2查看详细信息:rootsgzhang # sealert -l ba5ee7

26、cd-2d06-4dc7-9ee8-364332d90eb2 (下面是部分信息下面是部分信息)Host Name sgzhangPlatform Linux sgzhang 2.6.18-164.el5PAE #1 SMP Thu Sep 3 04:10:44 EDT 2009 i686 i686Alert Count 5First Seen Sun Aug 22 20:23:59 2010Last Seen Mon Aug 23 22:17:50 2010Local ID ba5ee7cd-2d06-4dc7-9ee8-364332d90eb2Line Numbers Raw Audit M

27、essages host=sgzhang type=AVC msg=audit(1282573070.455:1056): avc: denied search for pid=6130 comm=vsftpd name=zsgd dev=sda1 ino=9591650 scontext=root:system_r: tcontext=system_u:object_r:default_t:s0 tclass=dirhost=sgzhang type=SYSCALL msg=audit(1282573070.455:1056): arch=40000003 syscall=12 succes

28、s=no exit=-13 a0=8ac0530 a1=1a1b a2=1b68fc a3=bfa1f014 items=0 ppid=6126 pid=6130 auid=0 uid=0 gid=0 euid=6683 suid=6683 fsuid=6683 egid=6683 sgid=6683 fsgid=6683 tty=(none) ses=9 comm=vsftpd exe=/usr/sbin/vsftpd subj=root:system_r: key=(null)20Selinux 基于用户的FTP但策略中已允许了的主体对default_t的客体访问,如下:rootsgzha

29、ng # sesearch -a -s |grep default |tail -n 5 allow default_t : dir ioctl read getattr lock search ; allow default_t : lnk_file read create getattr setattr unlink link rename ; allow default_t : lnk_file ioctl read getattr lock ; allow default_t : sock_file ioctl read write create getattr setattr loc

30、k append unlink link rename ; allow default_t : fifo_file ioctl read write create getattr setattr lock append unlink link rename ; 21Selinux 基于用户的FTP如果遇到这种情况,需要验证一下bool值,其实bool值是策略的补充,会发现布尔值中已经定义了ftp访问home是disablerootsgzhang # getsebool -a |grep ftpallow_ - offallow_ - offallow_ - offallow_ - offall

31、ow_t - off - off - off - off - onhttpd_enable_ - offt - off22Selinux 基于用户的FTP修改bool值:rootsgzhang # setsebool -P 1rootsgzhang # getsebool - on测试:rootsgzhang # l zsgd localhostPassword: l ls -adrwx- 4 6683 6683 4096 Aug 23 14:10 .drwxr-xr-x 34 0 0 4096 Aug 23 14:10 .-rw-r-r- 1 6683 6683 33 Aug 23 14:1

32、0 .bash_logout-rw-r-r- 1 6683 6683 176 Aug 23 14:10 .bash_profile-rw-r-r- 1 6683 6683 124 Aug 23 14:10 .bashrc23Selinux 基于用户的FTP如何屏蔽SElinux对一个主体的保护:rootsgzhang # getsebool -a |grep ftpallow_ - offallow_ - offallow_ - offallow_ - offallow_t - off - on - off - off - onhttpd_enable_ - offt - off24Selin

33、ux 基于用户的FTP如何屏蔽SElinux对一个主体的保护:rootsgzhang # setsebool -P 1rootsgzhang # getsebool -a |grep - on重启相关的主体:rootsgzhang # service vsftpd restartShutting down vsftpd: OK Starting vsftpd for vsftpd: OK 发现主体的类型发上了变化:rootsgzhang # ps -efZ |grep vsftpdroot:system_r:initrc_t:s0 /usr/sbin/vsftpd /etc/vs25Selin

34、ux 基于用户的FTP测试:rootsgzhang # l zsgd localhostPassword: l ls l ls -adrwx- 4 6683 6683 4096 Aug 23 14:10 .drwxr-xr-x 34 0 0 4096 Aug 23 14:10 .-rw-r-r- 1 6683 6683 33 Aug 23 14:10 .bash_logout-rw-r-r- 1 6683 6683 176 Aug 23 14:10 .bash_profile-rw-r-r- 1 6683 6683 124 Aug 23 14:10 .bashrc-rw-r-r- 1 6683

35、 6683 515 Aug 23 14:10 .emacsdrwxr-xr-x 3 6683 6683 4096 Aug 23 14:10 .kdedrwxr-xr-x 4 6683 6683 4096 Aug 23 14:10 .mozilla-rw-r-r- 1 6683 6683 658 Aug 23 14:10 .zshrc26Selinux 与HTTP的应用常见的问题就是存放页面的路径更换后,页面的安全上下文和httpd主体不匹配造成页面无权访问,还有就是alias所指的路径下的文件rootsgzhang html# echo test_httpd index.htmlrootsgz

36、hang html# cd rootsgzhang # pwd/rootrootsgzhang # echo test_httpd index2.htmlrootsgzhang # mv index2.html /var/rootsgzhang # cd /var/rootsgzhang html# ls -Z-rw- root root root:object_r:user_home_t:s0 index2.html-rw- root root root:object_r:httpd_sys_content_t:s0 index.htmlrootsgzhang html# chmod 755

37、 *rootsgzhang html# ls -Z-rwxr-xr-x root root root:object_r:user_home_t:s0 index2.html-rwxr-xr-x root root root:object_r:httpd_sys_content_t:s0 index.html27Selinux 与HTTP的应用访问测试:28Selinux 与HTTP的应用Alias举例:Alias /docs/ /usr/share/doc/ Options Indexes MultiViews AllowOverride None Order allow,deny Allow

38、 from all#Alias /spool/ /var/spool/ Options Indexes MultiViews AllowOverride None Order allow,deny Allow from all29Selinux 与HTTP的应用测试:30Selinux 与HTTP的应用测试:31Selinux 与HTTP的应用原因:rootsgzhang spool# ls -Z /usr/share/doc/ |head n 3drwxr-xr-x root root system_u:object_r:usr_t:s0 a2ps-4.13bdrwxr-xr-x root

39、root system_u:object_r:usr_t:s0 aalib-1.4.0drwxr-xr-x root root system_u:object_r:usr_t:s0 acl-2.2.39rootsgzhang spool# sesearch -a -s httpd_t -t usr_tFound 4 av rules: allow httpd_t usr_t : file ioctl read getattr lock ; allow httpd_t usr_t : dir ioctl read getattr lock search ; dontaudit httpd_t u

40、sr_t : dir ioctl read write getattr lock add_name remove_name search ; allow httpd_t usr_t : lnk_file ioctl read getattr lock ; 32Selinux 与HTTP的应用原因:rootsgzhang spool# ls -Z /var/spool/drwxr-xr-x root root system_u:object_r:system_cron_spool_t:s0 anacrondrwx- daemon daemon system_u:object_r:cron_spo

41、ol_t:s0 atdrwxr-xr-x daemon daemon system_u:object_r:var_spool_t:s0 authdaemondrwxrwx- smmsp smmsp system_u:object_r:mqueue_spool_t:s0 clientmqueuerootsgzhang var# sesearch -a -s httpd_t -t system_cron_spool_t这里就找不到对应的策略33Selinux 与SMB的应用共享文件:rootsgzhang var# cat /etc/samba/smb.conf |tail -n 10datapa

42、th = /datapublic = yeswritable = yes backpath = /disk2/temppublic = yeswritable = yes34Selinux 与SMB的应用启动smb进程:rootsgzhang var# ps -efZ |grep smbroot:system_r:smbd_t:s0 root 8955 1 0 Aug23 ? 00:00:00 smbd -Droot:system_r:smbd_t:s0 root 8959 8955 0 Aug23 ? 00:00:00 smbd -Droot:system_r:smbd_t:s0 root

43、9060 8955 0 00:02 ? 00:00:00 smbd D共享文件的安全上下文:rootsgzhang var# ls -Z /disk2/|grep tempdrwxrwxrwx root root system_u:object_r:default_t:s0 temprootsgzhang var# ls -Z / |grep datadrwxrwxrwx root root system_u:object_r:default_t:s0 data35Selinux 与SMB的应用访问测试:36Selinux 与SMB的应用排错:rootsgzhang var# sesearch

44、 -a -s smbd_t -t default_tFound 8 av rules: dontaudit smbd_t default_t : dir getattr ; allow smbd_t default_t : file ioctl read getattr lock ; allow smbd_t default_t : file ioctl read write create getattr setattr lock append unlink link rename ; allow smbd_t default_t : dir ioctl read getattr lock s

45、earch ; allow smbd_t default_t : dir ioctl read write create getattr setattr lock unlink link rename add_name 发现权限是允许的37Selinux 与SMB的应用看bool值:rootsgzhang var# getsebool -a |grep sambasamba_domain_controller - offsamba_enable_home_dirs - offsamba_export_all_ro - offsamba_export_all_rw - offsamba_shar

46、e_fusefs - offsamba_share_nfs - offuse_samba_home_dirs - offvirt_use_samba - off这里ro和rw都是disable,问题出在这里38Selinux 与SMB的应用修改测试:发现可以读取了,但在smb.conf中定义的写权限无法使用rootsgzhang var# setsebool -P samba_export_all_ro 1rootsgzhang var# getsebool samba_export_all_rosamba_export_all_ro - on39Selinux 与SMB的应用再修改测试:发现

47、可以了rootsgzhang temp# setsebool -P samba_export_all_rw 1rootsgzhang temp# getsebool samba_export_all_rw samba_export_all_rw - on40Selinux 策略编辑Seedit 是一个比较好的编辑工具,带GUI界面,对新手很方便rootsgzhang # rpm -qa |grep seeditseedit-2.2.0-1.cos5seedit-gui-2.2.0-1.cos5seedit-policy-2.2.0-1.cos541Selinux 策略编辑Seedit 带的GUI界面42Selinux 策略编辑追加一条httpd主体允许对 /usr访问的权限 43

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号