華為云計算 云知識 Shc是什么
Shc是什么

簡介

Shc用來對shell腳本進行加密,它將shell腳本轉(zhuǎn)換為一個可執(zhí)行的二進制文件,從而避免shell腳本中的用戶名、密碼等重要信息暴露。

編譯和測試方式

1.選擇操作環(huán)境

本文選用華為鯤鵬云服務(wù)ECS KC1實例做測試

2.配置編譯環(huán)境

1)下載和解壓Shc軟件包。

cd /usr/local/src

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz

tar -zxvf shc-3.8.9.tgz

2)編譯和安裝Shc。

mkdir /usr/local/man/man1/ -p

cd /usr/local/src/shc-3.8.9

make install

回顯內(nèi)容如下:

[root@ecs-001 shc-3.8.9]# make install

***Installing shc and shc.1 on /usr/local

***Do you want to continue?

輸入“y”,繼續(xù)執(zhí)行。

回顯內(nèi)容如下:

***Do you want to continue? y

install -c -s shc /usr/local/bin/

install -c -m 644 shc.1 /usr/local/man/man1/

3.測試已完成編譯的軟件

1)創(chuàng)建測試腳本“hello.sh”。

cd /usr/local/src

touch hello.sh

chmod +x hello.sh

vi hello.sh

輸入以下內(nèi)容后,退出保存

#!/bin/bash

echo "test shc"

2)執(zhí)行“hello.sh”。

./hello.sh

回顯內(nèi)容如下:

[root@ecs-001 src]# ./hello.sh 

test shc

3)采用Shc對“hello.sh”加密。

cd /usr/local/src/

shc -v -f hello.sh

參數(shù)說明:

“-v”表示顯示加密過程。

“-f”指定需要加密的腳本。

回顯內(nèi)容如下:

[root@ecs-001 src]# shc -v -f hello.sh 

shc shll=bash

shc [-i]=-c

shc [-x]=exec '%s' "$@"

shc [-l]=

shc opts=

shc: cc  hello.sh.x.c -o hello.sh.x

shc: strip hello.sh.x

shc: chmod go-r hello.sh.x

加密完成后,會產(chǎn)生兩個新的文件,一個為“hello.sh.x”,一個為“hello.sh.x.c”。

其中“hello.sh.x”是新產(chǎn)生的二進制可執(zhí)行文件,“hello.sh.x.c”是新產(chǎn)生的c源文件。

4)查看產(chǎn)生的新文件。

ll | grep hello

回顯內(nèi)容如下:

[root@ecs-001 src]# ll | grep hello

-rwxr-xr-x  1 root root       28 Jul 10 16:50 hello.sh

-rwx--x--x  1 root root    68496 Jul 10 17:05 hello.sh.x

-rw-r--r--  1 root root     9605 Jul 10 17:05 hello.sh.x.c

5)執(zhí)行“hello.sh.x”測試。

./hello.sh.x

回顯內(nèi)容類似如下,表示加密成功。

[root@ecs-001 src]# ./hello.sh.x 

test shc

已知問題匯總

問題描述:

將在x86平臺上經(jīng)過Shc加密的“XXX.sh.x”文件復(fù)制到基于鯤鵬的服務(wù)器中,無法執(zhí)行,例如,將“test.sh.x”從x86平臺復(fù)制鯤鵬平臺 云服務(wù)器 執(zhí)行:

./test.sh.x

回顯內(nèi)容如下:

[root@ecs-001 test]# ./test.sh.x

-bash: ./test.sh.x: cannot execute binary file

問題原因:“test.sh.x”是c程序基于x86平臺的gcc編譯器生成的二進制可執(zhí)行文件,x86與鯤鵬的指令集不兼容,導(dǎo)致“test.sh.x”在鯤鵬服務(wù)器上不能執(zhí)行。

解決方法:

方式一:將源shell腳本重新在鯤鵬平臺云服務(wù)器上重新用Shc加密。

方式二:將“test.sh”腳本在x86平臺上經(jīng)Shc加密生成“test.sh.x.c”源文件,再復(fù)制到鯤鵬平臺的云服務(wù)器,重新由鯤鵬平臺的云服務(wù)器的gcc編譯生成二進制可執(zhí)行文件。

gcc -o test ./test.sh.x.c