linux之find命令高阶用法及文件特殊权限 —— 让隐藏在主机上的漏洞文件一览无余!


这里是一段防爬虫文本,请读者忽略。
本文原创首发于CSDN,作者IDYS
博客首页:https://blog.csdn.net/weixin_41633902/
本文链接:https://blog.csdn.net/weixin_41633902/article/details/105748894


- 写在开头的话

  • 请记住:实践是掌握知识的最快方法
  • 如果你只是怀着看看的态度去快速浏览文章,而不去认认真真的把文章里面讲的任何一个知识点去实践一遍,那么你永远也掌握不了它
  • 生命不息,折腾不止!

- find命令详解

- 文件查找命令简介

  • 在文件系统上查找符合条件的文件

  • 文件查找命令:locate,find

    • 非实时查找(数据库查找)
    • 实时查找:find

- locate命令

  • 依赖于事先构建的索引:索引的构建是在系统较为空闲时自动进行(周期性任务);手动更新数据库(updatedb)

  • whatis命令也是根据数据库查询的,makewhatis构建数据库(索引)

  • 索引构建过程需要遍历整个根文件系统,极耗费资源

  • Windows上类似于locate的工具,如:everything

  • 工作特点:

    1. 查找速度快

    2. 模糊查找

    3. 非实时查找

  • locate KEYWORD


- find命令

  • 实时查找工具,通过遍历指定路径下的文件系统完成文件查找

  • 工具特点

    1. 查找速度略慢

    2. 精确查找

    3. 实时查找

  • 语法:find [OPTION]… [查找路径] [查找条件] [处理动作]

    • 查找路径:指定具体目标路径,默认为当前目录
    • 查找条件:指定的查找标准,可以文件名、大小、类型、权限等标准进行。默认为找出指定路径下的所有文件
    • 处理动作:对符合条件的文件做什么操作:默认输出至屏幕
  • 查找条件:

  1. 根据文件名查找
  • -name ”文件名称“ 支持使用glob,严格区分字母大小写
  • *,?,[],[^]
  • -iname ”文件名称“:查找文件不区分字母大小写
  • -regex ”PATH“:以PATTERN匹配整个文件路径字符串,而不仅是文件名称
  1. 根据属主、属组查找:
  • -user USERNAME:查找属主为指定用户的文件
  • -group GROUPNAME:查找属组为指定组名的文件
  • -uid UserID:查找属主为指定UID号的文件
  • -gid GroupID:查找属组为指定GID号的文件
  • -nouser:查找没有属主的文件
  • -nogroup:查找没有属组的文件
#在/etc/ 下查找文件名为network的文件
[root@dayuanshuai ~]# find /etc -name "network"  
/etc/sysconfig/networking/profiles/default/network
/etc/sysconfig/network
/etc/rc.d/init.d/network

#在/etc/ 下查找以文件名以network结尾的文件
[root@dayuanshuai ~]# find /etc -name "*network"
/etc/sysconfig/networking/profiles/default/network
/etc/sysconfig/network
/etc/rc.d/rc5.d/S10network
/etc/rc.d/rc4.d/S10network
/etc/rc.d/rc2.d/S10network
/etc/rc.d/rc1.d/K90network
/etc/rc.d/init.d/network
/etc/rc.d/rc0.d/K90network
/etc/rc.d/rc6.d/K90network
/etc/rc.d/rc3.d/S10network
/etc/security/console.apps/system-config-network
/etc/pam.d/system-config-network

#在/tmp目录下查找属主为root的文件,且以长格式显示,-ls代表长格式
[root@dayuanshuai ~]# find /tmp -user root -ls
130817    4 drwxrwxrwt   6 root     root         4096 4月  1 09:18 /tmp
130917    4 drwxrwxrwt   2 root     root         4096 9月 19  2019 /tmp/.ICE-unix
130818    0 -rw-------   1 root     root            0 3月 29 09:57 /tmp/yum.log
130924    4 -rw----rwx   1 root     root          761 4月  1 06:25 /tmp/grub.conf
130925   28 -rw-r-----   1 root     root        25592 4月  1 06:33 /tmp/functions

# 在/hemo目录下查找 uid为501的文件,且以长格式显示
[root@dayuanshuai ~]#   find /home -uid 501 -ls
261677    4 drwx------   8 xiao     xiao         4096 4月  1 09:18 /home/xiao
261678    4 drwxr-xr-x   2 xiao     xiao         4096 11月 12  2010 /home/xiao/.gnome2
261699    4 drwxrwxr-x   2 xiao     xiao         4096 3月 31 15:58 /home/xiao/y_n
261700    4 -rw-rw-r--   1 xiao     xiao           20 3月 31 21:06 /home/xiao/grep_big.txt
261693    8 -rw-------   1 xiao     xiao         4788 4月  1 09:18 /home/xiao/.viminfo
261679    4 -rw-r--r--   1 xiao     xiao          176 3月 23  2017 /home/xiao/.bash_profile
261680    4 drwxr-xr-x   4 xiao     xiao         4096 9月 16  2019 /home/xiao/.mozilla
  1. 根据文件类型查找
  • -type TYPE

    • f:普通文件
    • d:目录文件
    • l:符号链接文件
    • s:套接字文件
    • b:块设备文件
    • c:字符设备文件
    • p:管道文件
  • 组合条件:

    • 与:-a
    • 或:-o
    • 非:-not,!
    • !A -a !B = !(A -o B)
    • !A -o !B = !(A -a B)
在/home目录下查找没有属主或者没有属组的文件
[root@dayuanshuai ~]# find /home -nouser -o -nogroup

#长格式显示
[root@dayuanshuai ~]# find /home \( -nouser -o -nogroup \) 
# 在/tmp下查找名字不是 'fstab'且属主不是root的文件
[root@dayuanshuai ~]# find /tmp \( -not -name 'fuctions' -a -not -user root \) -ls
[root@dayuanshuai ~]# find /tmp -not \( -name 'fuctions' -o -user root \) -ls
  • 根据文件大小来查找
    • -size [ + | - ] #UNIT
    • 常用单位:k,M,G
    • #UNIT: (#-1,#]
    • -#UNIT: [0,#-1]
    • +#UNIT:(#,oo)
find /usr -size 2k -exec ls -lh {} \;
  • 根据时间戳:

    1. 以”天“为单位
    • -atime [ +|- ]#
      • # [#,#+1)
      • +# [#+1,oo)
      • -# (0,#)
    • -mtime
    • -ctime
    1. 以”分钟“为单位
    • -amin
    • -mmin
    • -cmin

注意上面使用 + 号的地方在CentOS7上变为 /

#设置当前时间为2020年 04月 24日 星期五 17:15:30 CST
[root@dayuanshuai ~]# date 0424171520.30
2020年 04月 24日 星期五 17:15:30 CST
# 设置Atime的访问时间为2020年3月12日4点12分
[root@dayuanshuai ~]# touch -a -t 202003120412.00 Atime.txt
# 查看文件
[root@dayuanshuai tmp]# stat Atime.txt 
  File: "Atime.txt"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: 803h/2051d      Inode: 130926      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-03-12 04:12:00.000000000 +0800
Modify: 2020-04-24 17:19:18.821998093 +0800
Change: 2020-04-24 17:19:18.821998093 +0800
[root@dayuanshuai tmp]# find /tmp -atime -3
  • 根据权限查找

    • -perm [ +|- ] MODE

    • MODE:精确权限匹配

    • +MODE:任何一类(u,g,o)对象的权限中只要能有一位匹配即可

      • +100
        • 400不符合
        • 500符合
        • 700符合
        • 717符合
      • +400
        • 711符合
        • 613符合
        • 447符合
    • -MODE:每一类对象都必须同时拥有为其指定的权限标准:

      • -666

        • 477不符合
        • 644不符合
        • 766符合
      • -233

        • 666不符合

        • 644不符合

        • 277符合

注意上面使用 + 号的地方在CentOS7上变为了 /

  • 处理动作

    • -print:默认的处理动作,显示至屏幕
    • -ls:类似于对查找到的文件执行”ls -l“命令
    • -delete:删除查找到的文件
    • -fls /PATH/TO/SOMEWHERE:查找到的所有文件的长格式信息保存至指定文件中
    • -ok COMMAND {} \;  对查找到的每个文件执行由COMMAND指定的命令:对于每个文件执行命令之前,都会交互式要求用户确认。
    • -exec COMMAND {}; 对查找到的每个文件执行由COMMAND指定的命令;
    • 注意:find 传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令
    • 有些命令不能接受过多参数,此时命令执行可能会失败;另一种方式可规避此问题
    • find | xargs COMMAND
[root@dayuanshuai tmp]# find /tmp -nogroup -ok chown :root {} \;

#查找在/tmp目录下,改变时间在5分钟之内的文件,
[root@dayuanshuai tmp]# find /tmp -cmin -5 -exec mv {} {}.new \;

- find与xargs

- xargs简介

  • args(argument),xargs是命令传递参数的一个过滤器,也是组合多个命令的一个工具

  • 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据

  • xargs 默认的命令是 echo,这意味着通过管道传递给 xargs 的输入将会包含换行和空白,不过通过 xargs 的处理,换行和空白将被空格取代。

  • 之所以使用这个命令是因为很多命令不支持管道来传递参数


- xargs的用法

一个简短的例子
  • 例如需要以长格式显示/etc下属主为root用户,文件类型为普通文件,权限为700的文件

  • 错误做法

[root@dayuanshuai ~]# find /etc -user root -type f -perm 700 | ls -l
总用量 24
-rw-------. 1 root root   943 8月  31 2019 anaconda-ks.cfg
-rw-r--r--. 1 root root 13195 8月  31 2019 install.log
-rw-r--r--. 1 root root  3482 8月  31 2019 install.log.syslog
  • 正确做法1
[root@dayuanshuai ~]# find /etc -user root -type f -perm 700 -exec ls -l {} \;
-rwx------. 1 root root 180 7月  10 2003 /etc/cron.daily/logrotate
-rwx------. 1 root root 927 3月  22 2017 /etc/cron.daily/makewhatis.cron
  • 正确做法2
[root@dayuanshuai ~]# find /etc -user root -type f -perm 700 | xargs ls -l
-rwx------. 1 root root 180 7月  10 2003 /etc/cron.daily/logrotate
-rwx------. 1 root root 927 3月  22 2017 /etc/cron.daily/makewhatis.cron
xargs 选项
  • -n NUM:命令在执行时一次用的argument的个数,默认个数为所有的
[xiao@dayuanshuai ~]$ cat b.txt
abxy
xyaaabxy
xay
xayyy
xayss
sssdasasd
xyxxxxxxy
xxxxxxxysss
[xiao@dayuanshuai ~]$ cat b.txt | xargs  -n2
abxy xyaaabxy
xay xayyy
xayss sssdasasd
xyxxxxxxy xxxxxxxysss
  • -d :定义定界符
[xiao@dayuanshuai xargs_test]$ cat arg1.txt 
how how how N HOW HOW N are
you N you how N
what
[xiao@dayuanshuai xargs_test]$ cat arg1.txt | xargs -dN -n2
how how how   HOW HOW 
 are
you   you how 

what
  • -0:将stdin中的特殊字符当作一般字符,将\0作为定界符
[xiao@dayuanshuai xargs_test]$ echo -e "|\n|"
|
|
[xiao@dayuanshuai xargs_test]$ echo -e "|\n|" | xargs echo  #此时将换行当作分隔符,多两个参数进行处理
| |
[xiao@dayuanshuai xargs_test]$ echo -e "|\n|" | xargs -0 echo
|
|
  • -a file 从文件读入作为stdin
#从args1.txt中读取文件内容,作为xargs的参数
[xiao@dayuanshuai xargs_test]$ xargs -a arg1.txt -dN 
how how how   HOW HOW   are
you   you how  
what

  • -e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。(这个flag前必须以空格分隔)
[xiao@dayuanshuai xargs_test]$ cat arg1.txt 
how how how N HOW HOW N are
you N you how N
what
[xiao@dayuanshuai xargs_test]$ xargs -a arg1.txt -E'are'
how how how N HOW HOW N
  • p 当每次执行一次argument的时候询问一次用户
[xiao@dayuanshuai xargs_test]$ xargs -a arg1.txt -n2
how how
how N
HOW HOW
N are
you N
you how
N what
[xiao@dayuanshuai xargs_test]$ xargs -a arg1.txt -n2 -p
/bin/echo how how ?...y
/bin/echo how N ?...how how
y
/bin/echo HOW HOW ?...how N
y
/bin/echo N are ?...HOW HOW
y
/bin/echo you N ?...N are
y
/bin/echo you how ?...you N
y
/bin/echo N what ?...you how
y
N what
  • -I:为参数指定一个替换字符串,当 -i 时默认使用{}作为替换字符串不需要直接指定,而 -I 则需要指定替换字符串
# 使用-I 指定替换字符串,而-i则默认替换字符串为{}
[xiao@dayuanshuai xargs_test]$ cat a.txt | xargs -I {} echo  {} ?
how are you ?
fine  ?
ok ?
[xiao@dayuanshuai xargs_test]$ cat a.txt | xargs -i echo  {} ?     
how are you ?
fine  ?
ok ?

- find与xargs结合使用

  • 为什么find 中已经有-exec 了还要使用xargs呢?

find将查找到的参数一次性传递给-exec执行,而系统对-exec一次执行的参数个数有限制,可能会导致-exec处理不了,从而导致溢出。而xargs可以选择一次从find匹配出来的参数中取出几个参数,然后一次又一次的取出。然后又继续执行,直到取完,或者其自定义的某个点结束为止

  • 管道|对find传递来的参数,仅仅作为一个输入参数对待,而xargs可以对传递来的参数作为文件对待
# | 仅仅将find的输出作为输入参数
[root@dayuanshuai ~]# find /etc -type f -perm 700 -user root | cat -n         
     1  /etc/cron.daily/logrotate
     2  /etc/cron.daily/makewhatis.cron
# 查看/etc/cron.daily/logrotate和/etc/cron.daily/makewhatis.cron的内容,因为使用了xargs,所以会把两个输入参数当文件看
[root@dayuanshuai ~]# find /etc -type f -perm 700 -user root | xargs -i cat -n {}                      1  #!/bin/sh
     2
     3  /usr/sbin/logrotate /etc/logrotate.conf
     4  EXITVALUE=$?   
  • 将xiao用户的家目录下所有以.txt结尾且属主为xiao的普通文件加上后缀名.xiao
[xiao@dayuanshuai ~]$ find /home/xiao -user xiao -type f -name "*.txt" | xargs -i mv {} {}.xiao

- Linux文件系统的特殊权限

- 特殊权限

  1. SUID
  2. SGID
  3. Sticky

- 基本权限

  • user
    • r:read(读)
    • w:write(写)
    • x:exec(执行)
  • group
    • r:read(读)
    • w:write(写)
    • x:exec(执行)
  • other
    • r:read(读)

    • w:write(写)

    • x:exec(执行)


- 安全上下文

  • 前提:进程有属主和属组;文件有属主和属组
  1. 任何一个可执行程序文件能不能启动为进程:取决发起者对程序文件是否拥有执行权限
  2. 启动为进程之后,其进程的属主为发起者;进程的属组为发起者所属的组
  3. 进程访问文件时的权限,取决于进程的发起者
    1. 进程的发起者,同文件的属主:则应用文件属主权限
    2. 进程的发起者,属于文件的属组;则应用文件属组权限
    3. 应用文件“其它”权限

SUID

  1. 任何一个可执行程序文件能不能启动为进程:取决发起者对程序文件是否拥有执行权限
  2. 启动为进程之后,其进程的属主为原程序文件的属主
  • 权限设定:
    • chmod u+s FILE…
    • chmod u-s FILE…
[root@dayuanshuai ~]# cp /bin/cat /tmp  #将cat二进制文件复制到/tmp下
[root@dayuanshuai tmp]# su - xiao
[xiao@dayuanshuai ~]$ /tmp/cat /etc/shadow #以xiao用户运行cat二进制文件,对/etc/shadow执行操作
/tmp/cat: /etc/shadow: 权限不够 #此时因为xiao
用户对/etc/shadow没有任何权限。所以提示权限不足
[xiao@dayuanshuai ~]$ exit
[root@dayuanshuai tmp]# chmod u+s cat #回到root用户,然后给cat文件赋予suid权限
[xiao@dayuanshuai tmp]$ /tmp/cat /etc/shadow
root:$6$/YdhVB92$IRRt7M1b2JmxWLuPgbrejaHInPufjH1nujuSn7Q98PVzaGah3kv49R468VG7Rj4So1U2FIMP/k/SACAXQx4Zv0:12157:0:99999:7:::
bin:*:17446:0:99999:7:::
daemon:*:11246:0:99999:7:::
adm:*:17546:0:99999:7:::
lp:*:17746:0:99999:7:::
sync:*:17146:0:99999:7:::
#此时当xiao用户执行/tmp/cat时候,它被赋予了属主权限,也就是root权限,所以可以查看/etc/shadow,还有我对加密密码已经修改了,然后贴上去的

SGID

  1. 默认情况下,用户创建文件时,其属组为此用户所属的基本组
  2. 一旦某目录被设定了SGID,则对此目录有写权限的用户在此目录中创建的文件所属的组为此目录的属组
  • 权限设定:
    • chmod g+s DIR…
    • chmod g-s DIR…

Sticky

  • 对于一个多人可写的目录,如果设置了sticky,则对于每一个用户来说,它仅能删除自己的文件

  • 权限设定:

    • chmod o+t DIR…
    • chmod o-t DIR…
# 查看/tmp目录,所以用户对tmp目录都有可读,可写,可执行权限,同时/tmp目录被赋予了sticky权限,这意味着在该目录下,每个用户只能删除自己的文件,不能删除他人的文件
[xiao@dayuanshuai tmp]$  ls -ld /tmp 
drwxrwxrwt. 6 root root 4096 4月  24 20:06 /tmp
#在tmp目录下查看Atime文件,可以看到属主和属组为root
[xiao@dayuanshuai tmp]$ ls -l Atime.txt 
-rw-r--r--. 1 root root 0 4月  24 17:19 Atime.txt
# 此时xia用户无法删除Atime文件
[xiao@dayuanshuai tmp]$ rm Atime.txt 
rm:是否删除有写保护的普通空文件 "Atime.txt"?Y
rm: 无法删除"Atime.txt": 不允许的操作
# /tmp和/var/tmp都设置有sticky位
[root@dayuanshuai ~]# ls -ld /tmp /var/tmp
drwxrwxrwt. 6 root root 4096 4月  24 20:06 /tmp
drwxrwxrwt. 2 root root 4096 4月   1 04:52 /var/tmp

  • SUID 权限值4
  • SGID 权限值2
  • STICKY 权限值1
    • chmod 477 /tmp/IDYS
      • 所以用户对该目录都有读写执行权限,同时IDYS目录有SUID权限
  • 权限位映射
    • SUID: 映射到user,占据属主的执行权限位
      • s:属主拥有x(执行)权限
      • S:属主没有x(执行)权限
    • SGID:  映射到group, 占据group的执行权限位
      • s: group拥有x权限
      • S:group没有x权限
    • Sticky:  映射到other, 占据ohter的执行权限位
      • t:other拥有x权限
      • T:other没有x权限

- 练习

  1. 查找/var/目录下属主为root,且属组为mail的所有文件或者目录
[root@dayuanshuai ~]# find /var -user root -group mail \( -type f -o -type d \) -ls 
523667    4 drwxrwxr-x   2 root     mail         4096 4月  1 06:24 /var/spool/mail
524429    4 -rw-------   1 root     mail         1402 4月  1 06:24 /var/spool/mail/root
  1. 查找/usr目录下不属于root、bin或xiao的所有文件或目录
[root@dayuanshuai ~]# find /usr -not \( -user root -o -user bin -o -user xiao \)
  1. 查找/etc目录下最周一周内其内容修改过,同时属主不为root,也不是xiao的文件或目录
[root@dayuanshuai ~]# find /etc -not \( -user root -o -user xiao \) -mtime -7
  1. 查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件或目录
[root@dayuanshuai ~]# find /  \( -nouser -o -nogroup \) -atime -7   
  1. 查找/etc目录下大于1M且类型为普通文件的所有文件
[root@dayuanshuai ~]# find /etc -size +1M -type f -exec ls -lh {} \; 
-rw-r--r--. 1 root root 8.1M 8月  31 2019 /etc/selinux/targeted/modules/active/policy.kern
-rw-r--r--. 1 root root 8.1M 8月  31 2019 /etc/selinux/targeted/policy/policy.24
  1. 查找/etc目录下所有用户都没有写权限的文件
# CentOS6做法
[root@dayuanshuai ~]# find /etc -not -perm +222 -ls
1309261    4 ----------   1 root     root          897 3月 30 02:55 /etc/shadow
1308691    4 -r-xr-xr-x   1 root     root         1362 11月 15  2017 /etc/rc.d/init.d/blk-availability
1308193    4 ----------   1 root     root          774 9月 19  2019 /etc/shadow-
1309385    4 ----------   1 root     root          412 3月 30 06:25 /etc/gshadow
1308185    4 ----------   1 root     root          524 3月 30 06:24 /etc/gshadow-
1308409  276 -r--r--r--   1 root     root       281419 8月 31  2019 /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
1308410  228 -r--r--r--   1 root     root       231105 8月 31  2019 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
1308412  144 -r--r--r--   1 root     root       144247 8月 31  2019 /etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
1308411  192 -r--r--r--   1 root     root       194274 8月 31  2019 /etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
1308413  168 -r--r--r--   1 root     root       168365 8月 31  2019 /etc/pki/ca-trust/extracted/java/cacerts
1308919    4 -r--r--r--   1 root     root          324 6月 20  2018 /etc/ld.so.conf.d/kernel-2.6.32-754.el6.x86_64.conf
1309520    4 -r-xr-----   1 root     root         3756 3月 29 16:56 /etc/sudoers
1308416    4 -r--------   1 root     root           45 8月 31  2019 /etc/openldap/certs/password

# CentOS7 做法
[root@dayuanshuai ~]# find /etc -not -perm +222 -ls
  1. 查找/etc目录下至少有一类用户没有执行权限的文件
[root@dayuanshuai ~]# find /etc -not -perm -111 -ls
  1. 查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件
[root@dayuanshuai ~]# find /etc/init.d -perm -113 


- 写在最后的话:

  • 无论每个知识点的难易程度如何,我都会尽力将它描绘得足够细致
  • 欢迎关注我的CSDN博客,IDYS’BLOG
  • 持续更新内容运维 | 网工 | 软件技巧
  • 如果你有什么疑问,或者是难题。欢迎评论或者私信我。你若留言,我必回复!
  • 虽然我现在还很渺小,但我会做好每一篇内容。谢谢关注!

在这里插入图片描述

原创文章 9 获赞 10 访问量 1425

猜你喜欢

转载自blog.csdn.net/weixin_41633902/article/details/105748894