文章目录
前言
测试环境为
- ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]
- Ubuntu 20.04.2 LTS
利用
file = '|whoami'
puts open(file).read() # ubuntu
puts open(file).gets # ubuntu
原理
查看核心文件Kernel.rb
,在2800行左右:
# open(path [, mode [, perm]] [, opt]) -> io or nil
# open(path [, mode [, perm]] [, opt]) {|io| block } -> obj
#
# Creates an IO object connected to the given stream, file, or subprocess.
# If +path+ starts with a pipe character (<code>"|"</code>), a subprocess is
# created, connected to the caller by a pair of pipes. The returned IO
# object may be used to write to the standard input and read from the
# standard output of this subprocess.
# === Examples
#
# Open a subprocess and read its output:
#
# cmd = open("|date")
# print cmd.gets
# cmd.close
#
# Produces:
#
# Wed Apr 9 08:56:31 CDT 2003
如果+path+以一个管道字符(
|
)开头,就会创建一个子进程,通过一对管道连接到调用者。 返回的IO对象可用于向该子进程的标准输入写入和从标准输出读取。
因此可以利用open函数的特性通过管道符执行shell
实战例题
完
欢迎在评论区留言
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。