是否可以将 bash 脚本转换为可执行文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31280501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to convert a bash script into an executable?
提问by Kalis
The question is simple; Is it possible to convert a bash script into an executable? and if it is, how can it be done?
问题很简单;是否可以将 bash 脚本转换为可执行文件?如果是,怎么做?
回答by Charles Duffy
It's possible, but you don't want to.
这是可能的,但你不想。
- It won't be effective obfuscation.
- It won't be smaller.
- It won't be faster.
- 它不会是有效的混淆。
- 不会更小。
- 它不会更快。
It won't be smaller
不会更小
In places where support for compiling a script into a standalone executable is available, this is done by putting a copy of the interpreter into the executable. This is, thus, much larger than the script alone.
在支持将脚本编译为独立可执行文件的地方,这是通过将解释器的副本放入可执行文件来完成的。因此,这比单独的脚本大得多。
It won't be faster
不会更快
The execution model used by bash, unfortunately, is innately slow: Almost all functionality is provided by external commands; and simple commands' arguments, or even their names, can be modified by the result of expansion operations. This level of dynamicism makes effective compilation for performance impossible. (zsh supports a precompilation process, but the benefit is limited, mostly to the process of parsing itself).
不幸的是,bash 使用的执行模型天生就很慢:几乎所有功能都由外部命令提供;扩展操作的结果可以修改简单命令的参数,甚至它们的名称。这种级别的动态性使得针对性能的有效编译变得不可能。(zsh 支持预编译过程,但好处有限,主要是解析过程本身)。
It won't be effective obfuscation
这不会是有效的混淆
Take shc
, for instance: It literally passes the script's original source as a command-line argument. Thus, that source can be read simply by reading the command-line arguments out of /proc
, or using strace
.
以shc
为例:它实际上将脚本的原始源作为命令行参数传递。因此,可以通过从 中读取命令行参数/proc
或使用来简单地读取该源strace
。
As they say, "security by obscurity is no security at all" -- anyone who's reasonably competent could trivially extract any passwords or other content obfuscated in this way.
正如他们所说,“默默无闻的安全根本就没有安全性”——任何有能力的人都可以轻松提取任何密码或其他以这种方式混淆的内容。
回答by Jahid
You can use shc(as mentioned by Flavius Anton).
您可以使用shc(如 Flavius Anton 所述)。
But you need to know that this binary is not all independent. It still depends on the shell i.e if the code is written for Bash specifically and then converted with shc, it won't run without having Bash installed on the system.
但是你需要知道这个二进制文件并不都是独立的。它仍然依赖于外壳,即如果代码是专门为 Bash 编写的,然后用 shc 转换,则它不会在没有在系统上安装 Bash 的情况下运行。
Limitations:
限制:
- Size:heavily increased.
23bytes
oftest
script turned into9.6KB
of binary. - Speed:It will run slower than the plain Bash script.
- The size of the Bash script convertible is limited by the
_SC_ARG_MAX
system configuration parameter. - The binary is depended on Bash.
- It is a code obfuscation which is rendered useless against intelligent attack.
- 尺寸:大幅增加。
23bytes
的test
剧本变成9.6KB
二进制的。 - 速度:它会比普通的 Bash 脚本运行得慢。
- Bash 脚本可转换文件的大小受
_SC_ARG_MAX
系统配置参数的限制。 - 二进制文件依赖于 Bash。
- 这是一种代码混淆,对智能攻击毫无用处。
Another way is to use Bash2py:
另一种方法是使用Bash2py:
bash2pyconverts Bash source code to pythonsource code. You can then use a tool like cx_freezeto convert the Python source to binary which will run independently without bash. Don't get too excited just yet, you will see that this is useless for actual work, at least for now (bash2py<=3.2
).
bash2py将 Bash 源代码转换为python源代码。然后,您可以使用cx_freeze 之类的工具将 Python 源代码转换为二进制文件,该文件无需bash 即可独立运行。暂时不要太兴奋,您会发现这对实际工作没有用,至少现在是 ( bash2py<=3.2
)。
How To:
如何:
- Download and install Bash2py
- Install cx_freeze(or other tool)
- write a sample script, test:
#!/bin/bash echo works
- convert it to python:
bash2pyengine ./test
- A test.pypython script will be generated. Compile it using tools like cx_freeze:
cxfreeze ./test.py
- A binary file with lots of shared object file will be created (
dist
directory for cx_freeze). You can find a binary (namedtest
) there. Run it from that directory. Note that the shared object files are the dependencies for this binary. You can port this binary as long as you take all the shared object files with it.
- 下载并安装 Bash2py
- 安装 cx_freeze(或其他工具)
- 编写一个示例脚本,测试:
#!/bin/bash echo works
- 将其转换为python:
bash2pyengine ./test
- 将生成一个test.pypython 脚本。使用cx_freeze 之类的工具编译它:
cxfreeze ./test.py
- 将创建一个包含大量共享对象文件的二进制文件(
dist
cx_freeze 的目录)。您可以在test
那里找到一个二进制文件(名为)。从该目录运行它。请注意,共享对象文件是此二进制文件的依赖项。你可以移植这个二进制文件,只要你把所有的共享对象文件都带走。
Limitations:
限制:
- Size:It is a two way process and in each process size is heavily increased (super heavy), specially in cxfreeze step, as it produces a lot of shared object files and also the binary file alone becomes enormous too. For the above
test
script, original size is23bytes
,test.py -> 38bytes
,test (binary) -> 1.3MB
, and total(binary and it's dependencies) -> 6.5MB!!!
- Speed: I think the Bash code will run way more faster than this.
- All the limitations of Bash2py and freezing tool (cx_freeze or such) apply.
- Bash2py is yet to evolve to be usable. You will see a lot of red colored text/entry in Bash2py Manual Page. Red colored text means unsupportedin their context. There are lots of loosly supported features too. For example:
Array
is orange colored (now) which means "not necessarily well supported", actually it's not supported at all (yetbash2py<=3.2
). So even the Bash to Python conversion is not well performed where you will have to consider the limitation of the next step (freezing) too.
- 大小:这是一个双向进程,每个进程的大小都大大增加(超重),特别是在 cxfreeze 步骤中,因为它会产生大量的共享对象文件,而且单独的二进制文件也变得巨大。对于上述
test
脚本,原尺寸23bytes
,test.py -> 38bytes
,test (binary) -> 1.3MB
,和总(binary and it's dependencies) -> 6.5MB!!!
- 速度:我认为 Bash 代码会比这运行得更快。
- Bash2py 和冻结工具(cx_freeze 等)的所有限制都适用。
- Bash2py 尚未发展到可用。您将在Bash2py 手册页中看到很多红色文本/条目。红色文本表示在其上下文中不受支持。还有许多松散支持的功能。例如:
Array
是橙色(现在),这意味着“不一定得到很好的支持”,实际上根本不支持(还bash2py<=3.2
)。因此,即使是 Bash 到 Python 的转换也不能很好地执行,您也必须考虑下一步(冻结)的限制。
Finally:
最后:
Neither of the above is actually a real solution. And to be clear, Bash2py
is worse than shc
, as it doesn't support all Bash features (yet bash2py<=3.2
) i.e you can't write codes freely and expect it to work with Bash2py.
以上都不是真正的解决方案。需要明确的Bash2py
是shc
,它比 更糟糕,因为它不支持所有 Bash 功能(还bash2py<=3.2
),即你不能自由地编写代码并期望它与 Bash2py 一起工作。
After all this tedious effort, I can't help but agree with @Charles Duffy :
经过所有这些乏味的努力,我不禁同意@Charles Duffy:
It's possible, but you don't want to.
这是可能的,但你不想。
回答by fanton
Yes, there is. It's a small project called "shc" (Shell Compiler). You can find more info on their Github repo
就在这里。这是一个叫“特困”(小项目嘘ELL Çompiler)。你可以在他们的Github repo上找到更多信息
I know I used it about 2 years ago and it worked fine.
我知道我大约 2 年前使用过它,而且效果很好。