Further understanding of FIND commands by permission lookup

Source: Internet
Author: User

I have previously written an article "find finding based on file attributes or permissions", published in the journal Cyber Security and Informatization, and I forwarded it to the blog http://blog.51cto.com/yttitan/1935023
This thought to the Find command-perm option, understanding is more thorough, but yesterday in the lecture only found that some places difficult to justify, their understanding there are some deviations. So rewrite the previous article, is a correction and errata it. The system version used in this article is CentOS7.5.

The Find command finds by file permissions and requires the-perm option. The basic usage of the-PERM option is simple, in the form of "-perm mode", where mode is the right to match, and this lookup method implements exact matching.
For example, to find a normal file with a permission of 755 in the/boot directory and display the details. We set the search condition to "-perm 755", we can find that two files are found, the permissions of the two files have exactly matched the search criteria.

 [[email protected] ~]# find /boot -perm 755 -type f -ls 65030  250 -rwxr-xr-x   1 root     root       254248 4月  7  2015 /boot/efi/EFI/redhat/grub.efi    16 4125 -rwxr-xr-x   1 root     root      4222192 7月  2  2015 /boot/vmlinuz-2.6.32-573.el6.x86_64

But in more cases, we want to be able to blur the permissions. For example, look for a directory in which the group has write permissions, or find files with Write permissions for other users. In these cases, we only care if the owning group or other user has the appropriate permissions, not the overall permissions, so the exact match will not be able to meet the requirements.
The-perm option provides two ways of blurring matching: "-perm/mode" and "-perm-mode". These two fuzzy matching methods are not very well understood, the following examples illustrate the differences between them.
For example, we are looking for a permission of "220". If the permission is expressed in the form of a character, it should be "-w--w----". If it is represented in binary form, it should be "010010000".

The key here is to refer to the permission in binary form, where the number 0 means ignoring the appropriate location, and the number 1 indicates the right to match the corresponding location . Therefore, when the "220" is used as the permission lookup criteria for fuzzy matching, it indicates that the owner and the owning group should have write permission, while the other permissions are ignored.
After understanding this, the difference between "-perm/mode" and "-perm-mode" is understandable. "-perm/mode" requires a "or" relationship between the matching permissions, and "-perm-mode" requires a "and" relationship between the matching permissions. In other words, "-perm/220" means that either the owner or the owning group has write permission, and "-perm-220" means that the owner and the owning group must have write permission at the same time.
The following is verified by an instance, first we prepare some test files.

 [[email protected] ~]# mkdir /tmp/test[[email protected] ~]# touch /tmp/test/test{1,2,3}[[email protected] ~]# chmod 644 /tmp/test/test1[[email protected] ~]# chmod 664 /tmp/test/test2[[email protected] ~]# chmod 600 /tmp/test/test3

Then we do fuzzy matching in two different ways.
With "-perm/220" as a condition, find the owner or the owning group has write permissions to the file, you can see that 3 test files are eligible for the search criteria.

[[email protected] ~]# find /tmp/test -perm /220 -type f/tmp/test/test1/tmp/test/test2/tmp/test/test3

With "-perm-220" as a condition, find the owner and the owning group have write permission to the file, only/tmp/test/test2 meet the search criteria.

 [[email protected] ~]# find /tmp/test -perm -220 -type f/tmp/test/test2

So if you want to find a directory in the system where everyone has write permissions, you should specify the condition "-perm-222", and if "-perm/222" is the search condition, then the owner, the owning group, or any other user with write permission will meet the requirements.

 [[email protected] ~]# find / -perm -222 -type d -ls 2> /dev/null  6810    0 drwxrwxrwt   2 root     root          100 12月 23 03:37 /dev/shm654108    4 drwxrwxrwt   2 root     root         4096 12月 23 03:42 /var/tmp784897    4 drwxrwxrwt  12 root     root         4096 12月 23 06:58 /tmp

......

In addition to the basic permissions, the Find command also supports finding special permissions. For special permissions, the suid corresponds to the number of 4,sgid corresponding to the number is 2, the sticky bit sbit corresponds to the number is 1. If a file or directory is set with special permissions, it is represented as a 4-digit number, and special permissions are placed on the top left. For example, the/usr/bin/passwd file has a permission of "Rwsr-xr-x", which is represented as 4755 in digital form. Again, such as the/tmp directory, the permission is "RWXRWXRWT", in digital form is 1777.
Therefore, if you find all files in the system that have the SUID set, the lookup condition should be set to "4000". The same effect can be achieved regardless of whether you use "-perm-4000" or "-perm/4000" because there are only 1 permission bits to find. In the CentOS7 system, the number of such files is 27.

[[email protected] ~]# find / -perm -4000 2> /dev/null | wc -l27[[email protected] ~]# find / -perm /4000 2> /dev/null | wc -l27

Similarly, if you are looking for all directories with Sgid set, you should specify the condition "-perm-2000" or "-perm/2000". To find a directory with Sbit permissions set, you can specify the condition "-perm-1000" or "-perm/1000".
If you want to find all directories that have Sgid or sbit permissions set, you should specify the condition "-perm/3000", and if you specify the condition as "-perm-3000", you are looking for a directory that has both Sgid and sbit set.

 #查找同时设置了SGID和Sbit的目录[[email protected] ~]# find / -perm -3000 -type d -ls 2> /dev/null#查找设置了SGID或是Sbit的目录[[email protected] ~]# find / -perm /3000 -type d -ls 2> /dev/null  8400    0 drwxrwxrwt   2 root     root           40 9月 11 11:17 /dev/mqueue  8622    0 drwxrwxrwt   2 root     root           40 9月 11 11:17 /dev/shm  69      4 drwxrwxrwt   7 root     root         4096 10月 19 11:16 /var/tmp……

Further understanding of FIND commands by permission lookup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.