linux杂项笔记

SUID

-r——– 1 root root 44 Jul 30 15:12 /flag
-rwsr-xr-x 1 root root 16216 May 22 10:47 /readflag

whoami: app

普通用户无法读取/flag, 为什么执行/readflag就能从/flag中读出内容呢

A:chmod +s /readflag, 有SUID权限位的文件执行时将以文件所有者的权限运行

只对二进制文件生效,对shell脚本无效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Real UID: %d, Effective UID: %d\n", getuid(), geteuid());
// Real UID: 1000, Effective UID: 0

system("cat flag"); // 底层通过execvp启动shell,自动丢弃SUID,使用Real UID, permission deny

FILE *fp = fopen("/flag", "r"); // 检查Effective UID,成功读取文件内容
if (fp == NULL) {
perror("fopen");
exit(1);
}
char buffer[100];
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("%s", buffer);
}
fclose(fp);
return 0;
}

短命令执行

三个字符

1
2
3
4
5
6
7
8
9
10
c
# c: command not found
!!a
# ca: command not found
!!t
# cat (waiting)
!!<
# cat<
!!a
# cat a -> hello world

由于历史扩展(!)是最先进行的,所以可以使用变量,重定向符,管道等操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
e
!!c
!!h
!!o
!!$
!!{
# 这里会判定没有输入完, 需要手动结束
!!I
!!F
!!S
!!}
!!a
!!|
!!b
!!a
!!s
!!e
!!6
!!4
# echo a|base64


linux杂项笔记
http://mekrina.github.io/blogs/linux/linux杂项笔记/
作者
John Doe
发布于
2025年7月29日
许可协议