Linux 中的权限和权限:

ls -al 的输出是当前/父目录中所有文件和目录的列表并添加一些元数据。

-l 标志表示“长”形式,或“列出有关每个文件/目录的更多信息”。这包括文件类型、大小、时间戳、权限、所有者、上次访问日期/时间等。

-a 标志表示 all 或列出所有文件/目录,包括隐藏的文件/目录(其名称开头有 . )。

输出如下所示:

1
2
3
4
5
6
7
trshpuppy@trshheap:~$ ls -al
total 100
drwxr-xr-x 16 trshpuppy trshpuppy 4096 May 17 11:21 .
drwxr-xr-x 3 root root 4096 Sep 18 2022 ..
-rw------- 1 trshpuppy trshpuppy 2546 May 19 14:32 .bash_history
-rw-r--r-- 1 trshpuppy trshpuppy 220 Sep 18 2022 .bash_logout
...

' ' 上剪切或分割输出可以帮助区分输出的不同部分:

1
2
3
4
5
6
7
trshpuppy@trshheap:~$ ls -al | cut -d ' ' -f 1
total
drwxr-xr-x
drwxr-xr-x
-rw-------
-rw-r--r--
...

-d 告诉 cut 在此“分隔符”上进行剪切(' ' 是空白字符)。 -f 标志代表 field,因此要求剪切 -f 1 意味着“给我第一个字段”。

我们可以再次细分第一字段的每个部分:

1
2
3
4
5
6
trshpuppy@trshheap:~$ ls -al | cut -c 1
t
d
d
-
...

我们使用 -c 1 告诉 cut 剪切字符,并只给我们第一个字符。忽略第一个 t (它属于 total),我们可以看到每个列出的文件和目录的类型。

  • d 表示目录
  • - 表示常规文件

接下来的 9 个字符(或位)是每个文件的权限。每组 3 个字符分别是文件所有者(第一组)、组成员(第二组)和其他人(最后一组)的权限。每组包含 3 位,一位用于读、写和执行。

1
2
3
4
5
6
7
trshpuppy@trshheap:~$ ls -al | cut -c 2-10
otal 100
rwxr-xr-x
rwxr-xr-x
rw-------
rw-r--r--
...

每个带有一组位的权限位要么是一个字母,要么是 -r 代表读取,表示该用户/组具有读取权限。 w 表示写入,x 表示执行,- 表示不授予权限。

所以,对于第一行:

1
rwxr-xr-x
  • 所有者 (rwx) 具有读取、写入和执行权限。
  • 所有者组 (r-x) 中的用户仅具有读取和执行权限。
  • 其他用户(最后一组r-x)也只有读取和执行权限。

渗透测试:

对于渗透测试,最好找到允许完全权限的文件,以便您可以写入和执行到磁盘。允许所有用户/组完全访问的目录的一个很好的示例是 /tmp 文件夹:

1
2
3
4
trshpuppy@trshheap:~$ ls -al /tmp
total 164
drwxrwxrwt 9 root root 4096 May 17 09:18 .
# the '.' in the last field denotes the parent/ root folder (/tmp)

更改权限:

如果您在 Linux 中创建文件,它将继承与您的用户上下文关联的权限。根据您的 shell 上下文,您可能会创建一个新文件,但发现您没有读取、写入或执行它的权限。

使用 chmod +

要更新文件的权限,请使用 chmod 命令:

1
2
3
4
5
6
7
8
9
10
1  trshpuppy@trshheap:~$ echo "hello" > hello.txt
2 trshpuppy@trshheap:~$ cat hello.txt
3 hello
4 trshpuppy@trshheap:~$ ls -al | grep "hello.txt"
5 -rw-r--r-- 1 trshpuppy trshpuppy 6 May 19 15:08 hello.txt
6 # I have no execution permission on the file I just made
7 # .
8 trshpuppy@trshheap:~$ chmod +rwx hello.txt
9 trshpuppy@trshheap:~$ ls -al | grep "hello.txt"
10 -rwxr-xr-x 1 trshpuppy trshpuppy 6 May 19 15:08 hello.txt

8 行,我们使用 chmod +rwx 将所有三个文件权限添加到 hello.txt 文件中。

使用 chmod umask:

Linux 中的“umask”是指用户文件创建模式掩码,它确定应用于新创建的文件和目录的“默认权限”。

默认权限是在 shell 初始化文件中设置的,例如 .bashrc.bash_profile,并且由用户启动的所有进程继承。 Umask 是从默认权限中减去的一组位,用于计算新文件的最终权限。

Umask 位:

umask 由 3 组权限位组成:用户(文件的所有者)、组和其他人。

使用 chmod 更新权限的更快方法是将 umask 位设置为您要应用的权限。为此,您只需向 chmod 提供三个数字,每个数字从 0 到 7。

每个权限组合都会生成 0 到 7 之间的数字。例如,数字 0- - -,或者根本没有权限。数字 1- - 1,或者只有执行权限。 数字 2-w- 或只有写权限。并且数字 44 - - 或仅读取权限(数字代表打开或关闭的位)。

数量 权限 总计 二进制
0 0+0+0 000
1 –x 0+0+1 001
2 -w- 0+2+0 010
3 -wx 0+2+1 011
4 r– 4+0+0 100
5 r-x 4+0+1 101
6 RW- 4+2+0 110
7 RWx 4+2+1 111

因此,要授予每个人对文件的完全权限,命令是:

1
2
3
trshpuppy@trshheap:~$ chmod 777 hello.txt
trshpuppy@trshheap:~$ ls -al | grep "hello.txt"
-rwxrwxrwx 1 trshpuppy trshpuppy 6 May 19 15:08 hello.txt

更改目录权限(递归)

chmod 命令还可用于设置在目标目录中创建的文件(新文件,在调用该命令后开始)的权限。

为此,您只需发出命令:

1
chmod g+s <directory name>

这仅适用于该目录中将来创建的文件

要更改目录及其所有内容的所有权限,请使用:

1
chmod 777 --recursive <dir name>

[!资源]