如何将shell脚本进行加密?

昨天在整理之前公司的一个运维脚本,这个脚本功能十分强大,它基本覆盖了我们Oracle数据库运维的命令。
而后,我又对改脚本进行个性化的调整,使脚本更加符合我的使用。但是脚本只是单纯的由SQL组成的shell脚本,无论谁拿到了这个脚本很快就可以山寨一份。
为了避免泄漏前公司的资料(已咨询过离职后的员工仍然可以使用,但不要传播就好了。其实给别人用估计也没法查出来),加上自己的一些小成果。
在网上查找有没办法可以将shell脚本进行加密,于是我就找到了一款工具------- Shc generic shell script compiler
百度百科居然把安装和使用的步骤了列出来了。。。。

简介

使用 shc 对 Linux shell 脚本加密.
shc是一个专业的加密shell脚本的工具.它的作用是把 shell脚本转换为一个可执行的 二进制文件,这个办法很好的解决了脚本中含有IP、密码等不希望公开的问题.
不过从网上看到的评论,虽然能够加密,但是有心的话还是能够破解的,如果对于民用的话还是足够了。
网上的下载地址几乎都没有资源,我从别的地方下了一份,需要的话可以联系我~~

安装

tar zxvf shc-3.8.tgz
cd shc-3.8
make test
make
make test
make strings
make install
使用方法:
shc -r -f script-name
运行后会生成两个文件,script-name.x 和 script-name.x.c. 其中script-name.x是加密后的可执行的 二进制文件;用./script-name即可运行,script-name.x.c是生成 script-name.x的原文件(c语言).

实例

下载到系统后,安装执行:
# tar xzvf shc-3.8.6.tgz
# cd shc-3.8.6
# mkdir -p / usr/local/man/man1
这步是必须的,不然安装过程中会报错,shc将安装命令到/usr/local/bin/目录下;将帮助文档存放在/usr/local/man/man1/目录下,如果系统中无此目录,安装时会报错,可创建此目录后再执行安装:
# make install
这是要回答yes或者y,不能直接回车,否则会报错
使用方法:shc -f script-name
“-f”选项指定需要加密的程序,若要将 shell程序script.s加密,执行:
# shc -f script.s
如果要查看执行过程中详细信息,可以添加选项“-v”。执行完成后将产生两个文件:“script.s.x.c”和“script.s.x”,命令shc首先通过shell程序生成其C语言的 源代码“script.s.x.c”,然后对它进行编译生成加密后的可执行的 二进制文件“script.s.x”,运行“script.s.x”可以看到此程序执行结果:
另shc还提供了一种设定 有效执行期限的方法,可以首先使用shc将 shell程序转化为 二进制,并加上过期时间,如:
# shc -e 18/10/2006 -m " It’s too late to run this script " -f script.s

以上都是来自百度百科,哈哈。下面我们来实际操作一下
使用-h来查看帮助

点击(此处)折叠或打开

  1. [root@ydit shc-3.8.7]# shc -h
  2. shc Version 3.8.7, Generic Script Compiler
  3. shc Copyright (c) 1994-2009 Francisco Rosales <frosal@fi.upm.es>
  4. shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script

  5.     -e %s Expiration date in dd/mm/yyyy format [none]           ----设置过期时间
  6.     -m %s Message to display upon expiration ["Please contact your provider"]  ----到期时显示的消息
  7.     -f %s File name of the script to compile    ----要编译的脚本的文件名
  8.     -i %s Inline option for the shell interpreter i.e: -e
  9.     -x %s eXec command, as a printf format i.e: exec('%s',@ARGV);
  10.     -l %s Last shell option i.e: --
  11.     -r Relax security. Make a redistributable binary
  12.     -v Verbose compilation
  13.     -D Switch ON debug exec calls [OFF]
  14.     -T Allow binary to be traceable [no]
  15.     -C Display license and exit
  16.     -A Display abstract and exit
  17.     -h Display help and exit            

  18.     Environment variables used:
  19.     Name Default Usage
  20.     CC cc C compiler command
  21.     CFLAGS <none> C compiler flags

  22.     Please consult the shc(1) man page.

点击(此处)折叠或打开

  1. 先写一个t.sh的shell脚本
  2. [root@ydit shc-3.8.7]# cat t.sh
  3. echo "cyx";
  4. 使用shc加密,发现有报错
  5. [root@ydit shc-3.8.7]# shc -e 27/07/2016 -m "contact 77954090@qq.com" -f t.sh
  6. shc: invalid first line in script: echo "cyx";
  7. shc: Success
  8. 测试发现,需要在开头添加使用的shell类型,之后就没有出现报错了。
  9. [root@ydit shc-3.8.7]# cat t.sh
  10. #!/bin/sh
  11. echo "cyx";
  12. [root@ydit shc-3.8.7]# shc -e 27/07/2016 -m "contact 77954090@qq.com" -f t.sh
  13. 运行完之后会生成两个文件,一个是加密后的可执行的二进制文件,一个是生成 script-name.x的原文件(c语言).
  14. [root@ydit shc-3.8.7]# ls -ltr
  15. total 204
  16. -rw-r-----. 1 1000 1000 17982 May 10 1996 Copying
  17. -rwxr-x---. 1 1000 1000 336 Feb 22 2003 match
  18. -rw-r-----. 1 1000 1000 1544 Jun 19 2003 shc.README
  19. -rwxr-x---. 1 1000 1000 155 Jun 20 2003 pru.sh
  20. -rwxr-x---. 1 1000 1000 52 Jun 20 2003 test.csh
  21. -rw-r-----. 1 1000 1000 3136 Jul 8 2004 shc.1
  22. -rw-r-----. 1 1000 1000 4558 Jul 8 2004 shc.html
  23. -rwxr-x---. 1 1000 1000 138 Nov 13 2004 test.bash
  24. -rwxr-x---. 1 1000 1000 142 Jan 18 2008 test.ksh
  25. -rw-r-----. 1 1000 1000 24962 Feb 11 2010 shc-3.8.7.c
  26. -rw-r-----. 1 1000 1000 1940 Feb 11 2010 Makefile
  27. -rw-r-----. 1 1000 1000 3637 Feb 11 2010 CHANGES
  28. lrwxrwxrwx. 1 1000 1000 11 Jul 25 17:51 shc.c -> shc-3.8.7.c
  29. -rwxr-xr-x. 1 root root 37213 Jul 25 17:51 shc
  30. -rwxrwxrwx. 1 root root 44323 Jul 25 17:54 dxsql
  31. -rw-r--r--. 1 root root 24 Jul 26 11:19 t.sh
  32. -rw-r--r--. 1 root root 9434 Jul 26 11:20 t.sh.x.c
  33. -rwx--x--x. 1 root root 11616 Jul 26 11:20 t.sh.x
  34. 测试t.sh.x能否使用,并查看里面的内容。发现直接使用sh无法使用,需要用./的方式。打开都是乱码。
  35. [root@ydit shc-3.8.7]# sh t.sh.x
  36. t.sh.x: t.sh.x: cannot execute binary file
  37. [root@ydit shc-3.8.7]# ./t.sh.x
  38. cyx
  39. [root@ydit shc-3.8.7]# cat t.sh.x
  40. ELF>?
  41.      @@P&@@@@@@@@@@ ` `H ( ( `( @@DDP錿d@@llQ錿/lib64/ld-linux-x86-64.so.2GNUGNU讪i=桟3E筲忕`謴0@!??艍惸箿@9驄r

这样子,我就可以将脚本加密,在其他环境使用了~~

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29123031/viewspace-2122558/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29123031/viewspace-2122558/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值