前言
很多挖矿病毒为了隐藏自己往往会劫持动态链接库,在劫持后会发现TAB键会无法补齐一些病毒自带的文件,所以本文就想研究下TAB补全命令以及文件的原理是什么。
概述
大部分的终端都提供了自动补全功能,不同的终端略有区别,例如 zsh、bash 等等,这里仅介绍最常用的 bash 提供的补全功能。实际上,补齐功能可以通过脚本指定命令参数如何补全,默认的补全脚本保存在 /etc/bash_completion.d 目录下。
但一般会安装bash-completion包来得到更好的补全效果。但bash-completion这个包的安装位置因不同的发行版本会有所区别,但是原理是类似的,一般会有个bash_completion的脚本,这个脚本会在shell初始化时加载,获取脚本的位置可以通过查看“/etc/profile.d/bash_completion.sh文件,同时也是通过这个文件导入的。
而在bash_completion脚本中会加载/etc/bash_completion.d/目录下的补全脚本。
内置补全命令
Bash内置有两个补全命令,分别为compgen和complete,以及提供内置变量来辅助补全功能。以下用一个示例来演示。
示例:
root@VM-0-7-centos bash_completion.d]# cat /etc/bash_completion.d/footest.bash
_footest()
{
local cul=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "exec word word2" -- $cul) )
}
complete -F _footest footest
测试footest是否自动补全:
chmod +x /etc/bash_completion.d/footest.bash
source /etc/bash_complietion.d/footest.bash #<strong>为了使footest.bash在当前会话生效</strong>
<strong><span style="color:#a31600" class="has-inline-color">#注:footest命令本身不会被补齐</span></strong>
[root@VM-0-7-centos bash_completion.d]# footest [Tab][Tab]
exec word word2
[root@VM-0-7-centos bash_completion.d]# footest word[Tab]
word word2
内置补全变量
COMP_WORDS | 类型为数组,存在当前命令行中输入的所有单词 |
COMP_CWORD | 类型为整数,当前光标处单词在COMP_WORDS中的索引 |
COMPREPLY | 类型为数组,候选的补全结果 |
COMP_WORDBREAKS | 类型为字符串,表示单词之间的分隔符 |
COMP_LINE | 类型为字符串,表示当前的命令行输入字符 |
COMP_POINT | 类型为整数,表示光标在当前命令行的哪个位置 |
Complete(补全命令)
[root@VM-0-7-centos bash_completion.d]# help complete
complete: complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...]
重点参数说明:
-F function | 执行shell 函数,函数中生成COMPREPLY作为候选的补全结果 |
-C command | 将 command 命令的执行结果作为候选的补全 结果 |
-G pattern | 将匹配 pattern的文件名作为候选的补全结果 |
-W wordlist | 分割 wordlist 中的单词,作为候选的补全结果 |
-p [name] | 列出当前所有的补全命令 |
-r [name] | 删除某个补全命令 |
Compgen(筛选命令)
这条命令用来筛选生成匹配单词的候选补全结果
[root@VM-0-7-centos bash_completion.d]# help compgen
compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
重点说明:
-W wordlist | 分割 wordlist 中的单词,生成候选补全列表 |
# compgen -W 'word1 word2 test'
word1
word2
test
# compgen -W 'word1 word2 test' word
word1
word2
Compopt(修改补全命令设置)
这个命令可以修改补全命令设置,注意了,这个命令必须在补全函数中使用,否则会报错。
[root@VM-0-7-centos bash_completion.d]# help compgen
compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
Display possible completions depending on the options.
Intended to be used from within a shell function generating possible
completions. If the optional WORD argument is supplied, matches against
WORD are generated.
Exit Status:
Returns success unless an invalid option is supplied or an error occurs.
[root@VM-0-7-centos bash_completion.d]# ^C
[root@VM-0-7-centos bash_completion.d]# help compopt
compopt: compopt [-o|+o option] [-DE] [name ...]
Modify or display completion options.
Modify the completion options for each NAME, or, if no NAMEs are supplied,
the completion currently being executed. If no OPTIONs are given, print
the completion options for each NAME or the current completion specification.
Options:
-o option Set completion option OPTION for each NAME
-D Change options for the "default" command completion
-E Change options for the "empty" command completion
Using `+o' instead of `-o' turns off the specified option.
Arguments:
Each NAME refers to a command for which a completion specification must
have previously been defined using the `complete' builtin. If no NAMEs
are supplied, compopt must be called by a function currently generating
completions, and the options for that currently-executing completion
generator are modified.
Exit Status:
Returns success unless an invalid option is supplied or NAME does not
have a completion specification defined.
重点说明:
+o option | 启用 option 配置 |
-o option | 弃用 option 配置 |
例如,设置命令补全后不要多加空格,方法如下:
compopt -o nospace