bash 如何加密bash脚本源代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17750486/
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
How can i encrypt bash script source code?
提问by Harry Potter
I want to execute my bash scripts normally but no body see my source codes. How can i encrypt my bash script?
我想正常执行我的 bash 脚本,但没有人看到我的源代码。如何加密我的 bash 脚本?
thanks a lot.
多谢。
回答by Pablo Francisco Pérez Hidalgo
Bash is a pure interpreted language so the interpreter (bash) can only run it if it is clear text.
Bash 是一种纯解释型语言,因此解释器 (bash) 只能在它是明文时才能运行它。
You can try to obfuscate the code: How to minify/obfuscate a bash script
您可以尝试混淆代码: How to minify/obfuscate a bash script
On the other hand, you can restrict which users access to that code using system privileges.
另一方面,您可以使用系统权限限制哪些用户访问该代码。
回答by lewiguez
You could use shc. Here is an example. Not really sure if this is a great place to ask this question though. Doesn't seem super programming-related. Super User might be a better place for it. :)
你可以使用 shc。这是一个例子。不确定这是否是提出这个问题的好地方。似乎与超级编程无关。超级用户可能是一个更好的地方。:)
http://www.thegeekstuff.com/2012/05/encrypt-bash-shell-script/
http://www.thegeekstuff.com/2012/05/encrypt-bash-shell-script/
回答by Burhan Ali
Other answers and comments have already said that encryption isn't feasible so I won't go into that. I don't think that path even addresses the problem you are trying to solve.
其他答案和评论已经说过加密不可行,所以我不会讨论。我认为这条路径甚至不能解决您试图解决的问题。
You haven't given a practical example of what you are trying to achieve, so the suggestions below may not be sufficient.
您还没有给出您想要实现的实际示例,因此以下建议可能还不够。
With almost any problem it's best to start of with the simplest approach first and add complexity as needed. First of all, you may not need to do anything at all! When you execute a shell script, the process list will only show the name of the shell executing the script (bash) and the name of the script. No-one will be able to see the contents of the script this way.
对于几乎所有问题,最好先从最简单的方法开始,然后根据需要增加复杂性。首先,您可能根本不需要做任何事情!执行shell脚本时,进程列表只会显示执行脚本的shell的名称(bash)和脚本的名称。没有人能够以这种方式看到脚本的内容。
If that doesn't meet your needs then the next step would be to use standard file permissions to ensure that no-one can look at the contents of the file. ie. Remove read/write/execute permissions for group and other
如果这不能满足您的需求,那么下一步将是使用标准文件权限来确保没有人可以查看文件的内容。IE。删除组和其他人的读/写/执行权限
chmod go-rwx <name of script>
If neither of these are enough, you will have to provide more details about what you are trying to do and what your concerns are.
如果这些都不够,您将必须提供有关您正在尝试做什么以及您的担忧是什么的更多详细信息。
回答by NOOBIE
Sorry to wake up a "dead horse", just wanted to share what I have done using gpg. As mentioned earlier bash can only run it (the script) if it is in clear text.
抱歉吵醒了“死马”,只是想分享我使用gpg所做的事情。如前所述,bash 只能以明文形式运行它(脚本)。
encrypt the shell script with gpg:
使用 gpg 加密 shell 脚本:
gpg -c <your_bash_script.sh>
gpg -c <your_bash_script.sh>
This will ask you for a passphrase and confirm it.
这将要求您输入密码并进行确认。
This will create an encrypted file with a .gpg extension. the default encryption is CAST5 if you want a more strict cipher add --cipher-algo "cipher_name" (check the man pages for details)
这将创建一个扩展名为 .gpg 的加密文件。如果您想要更严格的密码,则默认加密为 CAST5 添加 --cipher-algo "cipher_name" (查看手册页了解详细信息)
<your_bash_script.sh.gpg>
<your_bash_script.sh.gpg>
decrypt your shell script with gpg:
用 gpg 解密你的 shell 脚本:
gpg -d <your_bash_script.sh.gpg>
gpg -d <your_bash_script.sh.gpg>
This will prompt you for the passphrase assigned to the file and display its contents in the on the screen.
这将提示您输入分配给文件的密码,并在屏幕上显示其内容。
if you put it all together, you have:
如果你把它们放在一起,你有:
gpg -d <your_bash_script.sh.gpg> | bash
gpg -d <your_bash_script.sh.gpg> | bash
You can even use gpg keys
您甚至可以使用 gpg 密钥
Every time you edit your script, you edit the un-encrypted version of the script or pipe the output of the decryption to a file and re-encrypt it when done.
每次编辑脚本时,都会编辑脚本的未加密版本或将解密输出通过管道传输到文件并在完成后重新加密。
回答by RoyMWell
I recommend you try submitting your script to this siteif you wish to protect it from public view.
如果您希望保护它不被公众看到,我建议您尝试将您的脚本提交到此站点。
While many will disagree with the idea of hiding the source code of a script written in an interpreted language, i understand why there's a desire for this work.
虽然许多人不同意隐藏用解释语言编写的脚本的源代码的想法,但我理解为什么需要这项工作。
As someone who has had his work stolen many times, I just dont care if "obfuscation" or "encryption" is a taboo. As long as my script is protected and it works as it did before encryption, I'm happy. Never again will I allow someone else to take credit for my work. And no, writing my script in a compiled language is not an option. I do not know how to.
作为一个多次被盗作品的人,我只是不在乎“混淆”或“加密”是否是一个禁忌。只要我的脚本受到保护并且像加密前一样工作,我就很高兴。我再也不会允许其他人因我的工作而受到赞扬。不,用编译语言编写我的脚本不是一种选择。我不知道怎么去。
Anyway, if you do not want to use the site mentioned above, try the latest version of shc. I believe they've updated it in github to address the many security concerns others have mentioned. Type the following into google "shc github" and you'll see a host of available options you can try out.
无论如何,如果您不想使用上述站点,请尝试使用最新版本的shc。我相信他们已经在 github 中对其进行了更新,以解决其他人提到的许多安全问题。在谷歌“shc github”中输入以下内容,您将看到许多可以尝试的可用选项。

